Ejemplo n.º 1
0
  /**
   * Allows reuse of the multi-line text element for computing bounds of changed font/text content
   *
   * @param fd
   */
  public final void reuse(Label la, double forceWrappingSize) {

    final Font f = (Font) xs.createFont(la.getCaption().getFont());
    fm = g2d.getFontMetrics(f);
    final FontRenderContext frc = g2d.getFontRenderContext();

    cachedwidth = Double.NaN;

    String s = la.getCaption().getValue();
    if (s == null) {
      s = IConstants.NULL_STRING;
    } else {
      // trim leading and trailing spaces.
      s = s.trim();
    }

    if (s.length() == 0) // TextLayout DOESN'T LIKE EMPTY STRINGS
    {
      s = IConstants.ONE_SPACE;
    }
    String[] sa = splitOnBreaks(s, forceWrappingSize, f);
    if (sa == null) {
      iLineCount = 1;
      oText = s;
      tla = new TextLayout[1];
      fsa = new String[1];
      tla[0] = new TextLayout(s, f.getAttributes(), frc);
      fsa[0] = s;
    } else {
      iLineCount = sa.length;
      oText = sa;
      tla = new TextLayout[iLineCount];
      fsa = new String[iLineCount];
      for (int i = 0; i < iLineCount; i++) {
        tla[i] = new TextLayout(sa[i], f.getAttributes(), frc);
        fsa[i] = sa[i];
      }
    }
    ins = la.getInsets().scaledInstance(pointsToPixels());

    if (forceWrappingSize > 0) {
      // update label with new broken content.
      StringBuffer sb = new StringBuffer();
      for (int i = 0; i < fsa.length; i++) {
        sb.append(fsa[i]).append("\n"); // $NON-NLS-1$
      }

      if (sb.length() > 0) {
        sb.deleteCharAt(sb.length() - 1);
      }

      la.getCaption().setValue(sb.toString());
    }
  }
Ejemplo n.º 2
0
 private void formatoLink(JLabel lbl) {
   lbl.setForeground(new Color(0, 0, 205));
   Font font = lbl.getFont();
   Map attributes = font.getAttributes();
   attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
   lbl.setFont(font.deriveFont(attributes));
 }
Ejemplo n.º 3
0
 public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
   Font font = (Font) source;
   Map attributes = font.getAttributes();
   writer.startNode("attributes"); // <attributes>
   context.convertAnother(attributes);
   writer.endNode(); // </attributes>
 }
Ejemplo n.º 4
0
 public AffineTransform getBaselineTransform() {
   Font font = source.getFont();
   if (font.hasLayoutAttributes()) {
     return AttributeValues.getBaselineTransform(font.getAttributes());
   }
   return null;
 }
Ejemplo n.º 5
0
 /**
  * This routine goes through the attributes and sets the font before calling the actual string
  * drawing routine
  *
  * @param iter
  */
 protected void doAttributes(AttributedCharacterIterator iter) {
   underline = false;
   Set set = iter.getAttributes().keySet();
   for (Iterator iterator = set.iterator(); iterator.hasNext(); ) {
     AttributedCharacterIterator.Attribute attribute =
         (AttributedCharacterIterator.Attribute) iterator.next();
     if (!(attribute instanceof TextAttribute)) continue;
     TextAttribute textattribute = (TextAttribute) attribute;
     if (textattribute.equals(TextAttribute.FONT)) {
       Font font = (Font) iter.getAttributes().get(textattribute);
       setFont(font);
     } else if (textattribute.equals(TextAttribute.UNDERLINE)) {
       if (iter.getAttributes().get(textattribute) == TextAttribute.UNDERLINE_ON) underline = true;
     } else if (textattribute.equals(TextAttribute.SIZE)) {
       Object obj = iter.getAttributes().get(textattribute);
       if (obj instanceof Integer) {
         int i = ((Integer) obj).intValue();
         setFont(getFont().deriveFont(getFont().getStyle(), i));
       } else if (obj instanceof Float) {
         float f = ((Float) obj).floatValue();
         setFont(getFont().deriveFont(getFont().getStyle(), f));
       }
     } else if (textattribute.equals(TextAttribute.FOREGROUND)) {
       setColor((Color) iter.getAttributes().get(textattribute));
     } else if (textattribute.equals(TextAttribute.FAMILY)) {
       Font font = getFont();
       Map fontAttributes = font.getAttributes();
       fontAttributes.put(TextAttribute.FAMILY, iter.getAttributes().get(textattribute));
       setFont(font.deriveFont(fontAttributes));
     } else if (textattribute.equals(TextAttribute.POSTURE)) {
       Font font = getFont();
       Map fontAttributes = font.getAttributes();
       fontAttributes.put(TextAttribute.POSTURE, iter.getAttributes().get(textattribute));
       setFont(font.deriveFont(fontAttributes));
     } else if (textattribute.equals(TextAttribute.WEIGHT)) {
       Font font = getFont();
       Map fontAttributes = font.getAttributes();
       fontAttributes.put(TextAttribute.WEIGHT, iter.getAttributes().get(textattribute));
       setFont(font.deriveFont(fontAttributes));
     }
   }
 }
Ejemplo n.º 6
0
  /**
   * @param s
   * @return
   */
  private String[] splitOnBreaks(String s, double maxSize, Font ft) {
    List<String> al = new ArrayList<String>();

    // check hard break first
    int i = 0, j;
    do {
      j = s.indexOf('\n', i);

      if (j == -1) {
        j = s.length();
      }
      String ss = s.substring(i, j);
      if (ss != null && ss.length() > 0) {
        al.add(ss);
      }

      i = j + 1;

    } while (j != -1 && j < s.length());

    // check wrapping
    if (maxSize > 0) {
      List<String> nal = new ArrayList<String>();

      for (Iterator<String> itr = al.iterator(); itr.hasNext(); ) {
        String ns = itr.next();

        AttributedString as = new AttributedString(ns, ft.getAttributes());
        LineBreakMeasurer lbm = new LineBreakMeasurer(as.getIterator(), g2d.getFontRenderContext());

        while (lbm.getPosition() < ns.length()) {
          int next = lbm.nextOffset((float) maxSize);

          String ss = ns.substring(lbm.getPosition(), next);
          lbm.setPosition(next);

          nal.add(ss);
        }
      }

      al = nal;
    }

    final int n = al.size();
    if (n == 1 || n == 0) {
      return null;
    }

    final String[] sa = new String[n];
    for (i = 0; i < al.size(); i++) {
      sa[i] = al.get(i);
    }
    return sa;
  }
Ejemplo n.º 7
0
  private void finishInit() {
    font = source.getFont();

    Map<TextAttribute, ?> atts = font.getAttributes();
    baseTX = AttributeValues.getBaselineTransform(atts);
    if (baseTX == null) {
      cm = source.getCoreMetrics();
    } else {
      AffineTransform charTX = AttributeValues.getCharTransform(atts);
      if (charTX == null) {
        charTX = new AffineTransform();
      }
      font = font.deriveFont(charTX);

      LineMetrics lm =
          font.getLineMetrics(
              source.getChars(),
              source.getStart(),
              source.getStart() + source.getLength(),
              source.getFRC());
      cm = CoreMetrics.get(lm);
    }
  }
Ejemplo n.º 8
0
  /** @see Graphics2D#drawString(String, float, float) */
  public void drawString(String s, float x, float y) {
    if (s.length() == 0) return;
    setFillPaint();
    if (onlyShapes) {
      drawGlyphVector(
          this.font.layoutGlyphVector(
              getFontRenderContext(),
              s.toCharArray(),
              0,
              s.length(),
              java.awt.Font.LAYOUT_LEFT_TO_RIGHT),
          x,
          y);
      //            Use the following line to compile in JDK 1.3
      //            drawGlyphVector(this.font.createGlyphVector(getFontRenderContext(), s), x, y);
    } else {
      boolean restoreTextRenderingMode = false;
      AffineTransform at = getTransform();
      AffineTransform at2 = getTransform();
      at2.translate(x, y);
      at2.concatenate(font.getTransform());
      setTransform(at2);
      AffineTransform inverse = this.normalizeMatrix();
      AffineTransform flipper = AffineTransform.getScaleInstance(1, -1);
      inverse.concatenate(flipper);
      double[] mx = new double[6];
      inverse.getMatrix(mx);
      cb.beginText();
      cb.setFontAndSize(baseFont, fontSize);
      // Check if we need to simulate an italic font.
      // When there are different fonts for italic, bold, italic bold
      // the font.getName() will be different from the font.getFontName()
      // value. When they are the same value then we are normally dealing
      // with a single font that has been made into an italic or bold
      // font.
      if (font.isItalic() && font.getFontName().equals(font.getName())) {
        float angle = baseFont.getFontDescriptor(BaseFont.ITALICANGLE, 1000);
        float angle2 = font.getItalicAngle();
        // We don't have an italic version of this font so we need
        // to set the font angle ourselves to produce an italic font.
        if (angle2 == 0) {
          // The JavaVM didn't have an angle setting for making
          // the font an italic font so use a default of
          // italic angle of 15 degrees.
          angle2 = 15.0f;
        } else {
          // This sign of the angle for Java and PDF seams
          // seams to be reversed.
          angle2 = -angle2;
        }
        if (angle == 0) {
          mx[2] = angle2 / 100.0f;
        }
      }
      cb.setTextMatrix(
          (float) mx[0], (float) mx[1], (float) mx[2], (float) mx[3], (float) mx[4], (float) mx[5]);
      Float fontTextAttributeWidth = (Float) font.getAttributes().get(TextAttribute.WIDTH);
      fontTextAttributeWidth =
          (fontTextAttributeWidth == null) ? TextAttribute.WIDTH_REGULAR : fontTextAttributeWidth;
      if (!TextAttribute.WIDTH_REGULAR.equals(fontTextAttributeWidth))
        cb.setHorizontalScaling(100.0f / fontTextAttributeWidth.floatValue());

      // Check if we need to simulate a bold font.
      // Do nothing if the BaseFont is already bold. This test is not foolproof but it will work
      // most of the times.
      if (baseFont.getPostscriptFontName().toLowerCase().indexOf("bold") < 0) {
        // Get the weight of the font so we can detect fonts with a weight
        // that makes them bold, but the Font.isBold() value is false.
        Float weight = (Float) font.getAttributes().get(TextAttribute.WEIGHT);
        if (weight == null) {
          weight = (font.isBold()) ? TextAttribute.WEIGHT_BOLD : TextAttribute.WEIGHT_REGULAR;
        }
        if ((font.isBold() || (weight.floatValue() >= TextAttribute.WEIGHT_SEMIBOLD.floatValue()))
            && (font.getFontName().equals(font.getName()))) {
          // Simulate a bold font.
          float strokeWidth =
              font.getSize2D()
                  * (weight.floatValue() - TextAttribute.WEIGHT_REGULAR.floatValue())
                  / 30f;
          if (strokeWidth != 1) {
            if (realPaint instanceof Color) {
              cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE);
              cb.setLineWidth(strokeWidth);
              Color color = (Color) realPaint;
              int alpha = color.getAlpha();
              if (alpha != currentStrokeGState) {
                currentStrokeGState = alpha;
                PdfGState gs = strokeGState[alpha];
                if (gs == null) {
                  gs = new PdfGState();
                  gs.setStrokeOpacity(alpha / 255f);
                  strokeGState[alpha] = gs;
                }
                cb.setGState(gs);
              }
              cb.setColorStroke(color);
              restoreTextRenderingMode = true;
            }
          }
        }
      }

      double width = 0;
      if (font.getSize2D() > 0) {
        float scale = 1000 / font.getSize2D();
        Font derivedFont = font.deriveFont(AffineTransform.getScaleInstance(scale, scale));
        width = derivedFont.getStringBounds(s, getFontRenderContext()).getWidth();
        if (derivedFont.isTransformed()) width /= scale;
      }
      // if the hyperlink flag is set add an action to the text
      Object url = getRenderingHint(HyperLinkKey.KEY_INSTANCE);
      if (url != null && !url.equals(HyperLinkKey.VALUE_HYPERLINKKEY_OFF)) {
        float scale = 1000 / font.getSize2D();
        Font derivedFont = font.deriveFont(AffineTransform.getScaleInstance(scale, scale));
        double height = derivedFont.getStringBounds(s, getFontRenderContext()).getHeight();
        if (derivedFont.isTransformed()) height /= scale;
        double leftX = cb.getXTLM();
        double leftY = cb.getYTLM();
        PdfAction action = new PdfAction(url.toString());
        cb.setAction(
            action,
            (float) leftX,
            (float) leftY,
            (float) (leftX + width),
            (float) (leftY + height));
      }
      if (s.length() > 1) {
        float adv = ((float) width - baseFont.getWidthPoint(s, fontSize)) / (s.length() - 1);
        cb.setCharacterSpacing(adv);
      }
      cb.showText(s);
      if (s.length() > 1) {
        cb.setCharacterSpacing(0);
      }
      if (!TextAttribute.WIDTH_REGULAR.equals(fontTextAttributeWidth)) cb.setHorizontalScaling(100);

      // Restore the original TextRenderingMode if needed.
      if (restoreTextRenderingMode) {
        cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL);
      }

      cb.endText();
      setTransform(at);
      if (underline) {
        // These two are supposed to be taken from the .AFM file
        // int UnderlinePosition = -100;
        int UnderlineThickness = 50;
        //
        double d = asPoints(UnderlineThickness, (int) fontSize);
        Stroke savedStroke = originalStroke;
        setStroke(new BasicStroke((float) d));
        y = (float) (y + asPoints(UnderlineThickness, (int) fontSize));
        Line2D line = new Line2D.Double(x, y, width + x, y);
        draw(line);
        setStroke(savedStroke);
      }
    }
  }