private void buildText(List<ElementSpec> elements, TextBox box) {
    String text = box.getText();
    VisualContext vc = box.getVisualContext();
    MutableAttributeSet attr = new SimpleAttributeSet();

    attr.addAttribute(Constants.ATTRIBUTE_FONT_VARIANT, vc.getFontVariant());
    attr.addAttribute(Constants.ATTRIBUTE_TEXT_DECORATION, vc.getTextDecoration());
    attr.addAttribute(Constants.ATTRIBUTE_FONT, vc.getFont());
    attr.addAttribute(Constants.ATTRIBUTE_FOREGROUND, vc.getColor());

    attr.addAttribute(SwingBoxDocument.ElementNameAttribute, Constants.TEXT_BOX);
    attr.addAttribute(Constants.ATTRIBUTE_ANCHOR_REFERENCE, new Anchor());
    attr.addAttribute(Constants.ATTRIBUTE_BOX_REFERENCE, box);

    // elements.add(new ElementSpec(attr, ElementSpec.StartTagType));
    elements.add(
        new ElementSpec(attr, ElementSpec.ContentType, text.toCharArray(), 0, text.length()));
    // elements.add(new ElementSpec(attr, ElementSpec.EndTagType));

  }
示例#2
0
 public VisualContext create() {
   VisualContext ret = new VisualContext(this, this.factory);
   ret.viewport = viewport;
   ret.rootContext = rootContext;
   ret.em = em;
   ret.rem = rem;
   ret.ex = ex;
   ret.ch = ch;
   ret.dpi = dpi;
   ret.font = font;
   ret.fontSize = fontSize;
   ret.fontWeight = fontWeight;
   ret.fontStyle = fontStyle;
   ret.fontVariant = fontVariant;
   ret.textDecoration = new ArrayList<CSSProperty.TextDecoration>(textDecoration);
   ret.color = color;
   return ret;
 }
示例#3
0
  /**
   * Updates the context according to the given element style. The properties that are not defined
   * in the style are left unchanged.
   *
   * @param style the style data
   */
  public void update(NodeData style) {
    // setup the font
    String family = null;
    CSSProperty.FontFamily ff = style.getProperty("font-family");
    if (ff == null) family = font.getFamily(); // use current
    else if (ff == FontFamily.list_values) {
      TermList fmlspec = style.getValue(TermList.class, "font-family");
      if (fmlspec == null) family = font.getFamily();
      else family = getFontName(fmlspec);
    } else {
      if (factory != null)
        family =
            factory
                .getConfig()
                .getDefaultFont(ff.getAWTValue()); // try to translate to physical font
      if (family == null) family = ff.getAWTValue(); // could not translate - use as is
    }

    double size;
    double psize = (parent == null) ? CSSUnits.medium_font : parent.getEm();
    CSSProperty.FontSize fsize = style.getProperty("font-size");
    if (fsize == null) size = em;
    else if (fsize == CSSProperty.FontSize.length || fsize == CSSProperty.FontSize.percentage) {
      TermLengthOrPercent lenspec = style.getValue(TermLengthOrPercent.class, "font-size");
      if (lenspec != null) {
        em = psize;
        size =
            pxLength(lenspec, psize); // pixels are ok here (java is fixed to 72 dpi for font sizes)
      } else size = em;
    } else size = CSSUnits.convertFontSize(psize, fsize);
    fontSize = CSSUnits.points(size);

    if (rootContext != null) rem = rootContext.getEm();
    else rem = em; // we don't have a root context?

    CSSProperty.FontWeight weight = style.getProperty("font-weight");
    if (weight != null) fontWeight = weight;
    CSSProperty.FontStyle fstyle = style.getProperty("font-style");
    if (fstyle != null) fontStyle = fstyle;
    int fs = Font.PLAIN;
    if (representsBold(fontWeight)) fs = Font.BOLD;
    if (fontStyle == CSSProperty.FontStyle.ITALIC || fontStyle == CSSProperty.FontStyle.OBLIQUE)
      fs = fs | Font.ITALIC;

    font = new Font(family, fs, (int) Math.round(size));
    em = size;

    CSSProperty.FontVariant variant = style.getProperty("font-variant");
    if (variant != null) fontVariant = variant;
    CSSProperty.TextDecoration decor = style.getProperty("text-decoration");
    textDecoration.clear();
    if (decor != null) {
      if (decor == TextDecoration.list_values) {
        TermList list = style.getValue(TermList.class, "text-decoration");
        for (Term<?> t : list) {
          if (t.getValue() instanceof CSSProperty.TextDecoration)
            textDecoration.add((CSSProperty.TextDecoration) t.getValue());
        }
      } else if (decor != TextDecoration.NONE) textDecoration.add(decor);
    }

    // color
    TermColor clr = style.getValue(TermColor.class, "color");
    if (clr != null) color = clr.getValue();
  }