Search in sources :

Example 96 with TextLayout

use of java.awt.font.TextLayout in project poi by apache.

the class SLGraphics method drawString.

/**
     * Renders the text specified by the specified <code>String</code>,
     * using the current text attribute state in the <code>Graphics2D</code> context.
     * The baseline of the first character is at position
     * (<i>x</i>,&nbsp;<i>y</i>) in the User Space.
     * The rendering attributes applied include the <code>Clip</code>,
     * <code>Transform</code>, <code>Paint</code>, <code>Font</code> and
     * <code>Composite</code> attributes. For characters in script systems
     * such as Hebrew and Arabic, the glyphs can be rendered from right to
     * left, in which case the coordinate supplied is the location of the
     * leftmost character on the baseline.
     * @param s the <code>String</code> to be rendered
     * @param x the x coordinate of the location where the
     * <code>String</code> should be rendered
     * @param y the y coordinate of the location where the
     * <code>String</code> should be rendered
     * @throws NullPointerException if <code>str</code> is
     *         <code>null</code>
     * @see #setPaint
     * @see java.awt.Graphics#setColor
     * @see java.awt.Graphics#setFont
     * @see #setTransform
     * @see #setComposite
     * @see #setClip
     */
public void drawString(String s, float x, float y) {
    TextBox<?, ?> txt = _group.createTextBox();
    TextRun rt = txt.getTextParagraphs().get(0).getTextRuns().get(0);
    rt.setFontSize((double) _font.getSize());
    rt.setFontFamily(_font.getFamily());
    if (getColor() != null)
        rt.setFontColor(DrawPaint.createSolidPaint(getColor()));
    if (_font.isBold())
        rt.setBold(true);
    if (_font.isItalic())
        rt.setItalic(true);
    txt.setText(s);
    txt.setInsets(new Insets2D(0, 0, 0, 0));
    txt.setWordWrap(false);
    txt.setHorizontalCentered(false);
    txt.setVerticalAlignment(VerticalAlignment.MIDDLE);
    TextLayout layout = new TextLayout(s, _font, getFontRenderContext());
    float ascent = layout.getAscent();
    float width = (float) Math.floor(layout.getAdvance());
    /**
         * Even if top and bottom margins are set to 0 PowerPoint
         * always sets extra space between the text and its bounding box.
         *
         * The approximation height = ascent*2 works good enough in most cases
         */
    float height = ascent * 2;
    /*
          In powerpoint anchor of a shape is its top left corner.
          Java graphics sets string coordinates by the baseline of the first character
          so we need to shift up by the height of the textbox
        */
    y -= height / 2 + ascent / 2;
    /*
          In powerpoint anchor of a shape is its top left corner.
          Java graphics sets string coordinates by the baseline of the first character
          so we need to shift down by the height of the textbox
        */
    txt.setAnchor(new Rectangle((int) x, (int) y, (int) width, (int) height));
}
Also used : Rectangle(java.awt.Rectangle) TextRun(org.apache.poi.sl.usermodel.TextRun) Insets2D(org.apache.poi.sl.usermodel.Insets2D) TextLayout(java.awt.font.TextLayout)

Example 97 with TextLayout

use of java.awt.font.TextLayout in project poi by apache.

the class DrawTextParagraph method tab2space.

/**
     * Replace a tab with the effective number of white spaces.
     */
private String tab2space(TextRun tr) {
    AttributedString string = new AttributedString(" ");
    String fontFamily = tr.getFontFamily();
    if (fontFamily == null) {
        fontFamily = "Lucida Sans";
    }
    string.addAttribute(TextAttribute.FAMILY, fontFamily);
    Double fs = tr.getFontSize();
    if (fs == null) {
        fs = 12d;
    }
    string.addAttribute(TextAttribute.SIZE, fs.floatValue());
    TextLayout l = new TextLayout(string.getIterator(), new FontRenderContext(null, true, true));
    double wspace = l.getAdvance();
    Double tabSz = paragraph.getDefaultTabSize();
    if (tabSz == null) {
        tabSz = wspace * 4;
    }
    int numSpaces = (int) Math.ceil(tabSz / wspace);
    StringBuilder buf = new StringBuilder();
    for (int i = 0; i < numSpaces; i++) {
        buf.append(' ');
    }
    return buf.toString();
}
Also used : AttributedString(java.text.AttributedString) AttributedString(java.text.AttributedString) FontRenderContext(java.awt.font.FontRenderContext) Paint(java.awt.Paint) TextLayout(java.awt.font.TextLayout)

Example 98 with TextLayout

use of java.awt.font.TextLayout in project poi by apache.

the class DrawTextParagraph method breakText.

/**
     * break text into lines, each representing a line of text that fits in the wrapping width
     *
     * @param graphics The drawing context for computing text-lengths.
     */
protected void breakText(Graphics2D graphics) {
    lines.clear();
    DrawFactory fact = DrawFactory.getInstance(graphics);
    StringBuilder text = new StringBuilder();
    AttributedString at = getAttributedString(graphics, text);
    boolean emptyParagraph = ("".equals(text.toString().trim()));
    AttributedCharacterIterator it = at.getIterator();
    LineBreakMeasurer measurer = new LineBreakMeasurer(it, graphics.getFontRenderContext());
    for (; ; ) {
        int startIndex = measurer.getPosition();
        // add a pixel to compensate rounding errors
        double wrappingWidth = getWrappingWidth(lines.size() == 0, graphics) + 1;
        // shape width can be smaller that the sum of insets (this was proved by a test file)
        if (wrappingWidth < 0) {
            wrappingWidth = 1;
        }
        int nextBreak = text.indexOf("\n", startIndex + 1);
        if (nextBreak == -1) {
            nextBreak = it.getEndIndex();
        }
        TextLayout layout = measurer.nextLayout((float) wrappingWidth, nextBreak, true);
        if (layout == null) {
            // layout can be null if the entire word at the current position
            // does not fit within the wrapping width. Try with requireNextWord=false.
            layout = measurer.nextLayout((float) wrappingWidth, nextBreak, false);
        }
        if (layout == null) {
            // exit if can't break any more
            break;
        }
        int endIndex = measurer.getPosition();
        // skip over new line breaks (we paint 'clear' text runs not starting or ending with \n)
        if (endIndex < it.getEndIndex() && text.charAt(endIndex) == '\n') {
            measurer.setPosition(endIndex + 1);
        }
        TextAlign hAlign = paragraph.getTextAlign();
        if (hAlign == TextAlign.JUSTIFY || hAlign == TextAlign.JUSTIFY_LOW) {
            layout = layout.getJustifiedLayout((float) wrappingWidth);
        }
        AttributedString str = (emptyParagraph) ? // we will not paint empty paragraphs
        null : new AttributedString(it, startIndex, endIndex);
        DrawTextFragment line = fact.getTextFragment(layout, str);
        lines.add(line);
        maxLineHeight = Math.max(maxLineHeight, line.getHeight());
        if (endIndex == it.getEndIndex()) {
            break;
        }
    }
    rawText = text.toString();
}
Also used : AttributedString(java.text.AttributedString) TextAlign(org.apache.poi.sl.usermodel.TextParagraph.TextAlign) LineBreakMeasurer(java.awt.font.LineBreakMeasurer) Paint(java.awt.Paint) AttributedCharacterIterator(java.text.AttributedCharacterIterator) TextLayout(java.awt.font.TextLayout)

Example 99 with TextLayout

use of java.awt.font.TextLayout in project chipKIT32-MAX by chipKIT32.

the class CompositionTextManager method getTextLayout.

private TextLayout getTextLayout(AttributedCharacterIterator text, int committed_count) {
    AttributedString composed = new AttributedString(text, committed_count, text.getEndIndex());
    Font font = textArea.getPainter().getFont();
    FontRenderContext context = ((Graphics2D) (textArea.getPainter().getGraphics())).getFontRenderContext();
    composed.addAttribute(TextAttribute.FONT, font);
    TextLayout layout = new TextLayout(composed.getIterator(), context);
    return layout;
}
Also used : AttributedString(java.text.AttributedString) FontRenderContext(java.awt.font.FontRenderContext) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D) TextLayout(java.awt.font.TextLayout)

Example 100 with TextLayout

use of java.awt.font.TextLayout in project jgnash by ccavanaugh.

the class PrintableCheckLayout method drawPayee.

private void drawPayee(final Graphics2D g2, final CheckObject object, final float offset, final String payee) {
    float payeeX = object.getX();
    float payeeY = object.getY() + offset;
    if (payee != null && !payee.isEmpty()) {
        TextLayout textLayout = new TextLayout(payee, font, frc);
        textLayout.draw(g2, payeeX, payeeY);
    }
    if (testPrint) {
        TextLayout payeeText = new TextLayout("ORDER OF", testPrintFont, frc);
        double width = payeeText.getBounds().getWidth();
        payeeText.draw(g2, (float) (payeeX - width - space), payeeY);
        LineMetrics metrics = testPrintFont.getLineMetrics("PAY TO THE", frc);
        float y = payeeY - (float) payeeText.getBounds().getHeight() - metrics.getDescent() - metrics.getLeading();
        payeeText = new TextLayout("PAY TO THE", testPrintFont, frc);
        payeeText.draw(g2, (float) (payeeX - width - space), y);
    }
}
Also used : LineMetrics(java.awt.font.LineMetrics) TextLayout(java.awt.font.TextLayout)

Aggregations

TextLayout (java.awt.font.TextLayout)104 AttributedString (java.text.AttributedString)32 Graphics2D (java.awt.Graphics2D)24 FontRenderContext (java.awt.font.FontRenderContext)21 Font (java.awt.Font)18 AttributedCharacterIterator (java.text.AttributedCharacterIterator)17 LineBreakMeasurer (java.awt.font.LineBreakMeasurer)16 Point (java.awt.Point)11 Rectangle (java.awt.Rectangle)9 Paint (java.awt.Paint)8 AffineTransform (java.awt.geom.AffineTransform)8 Rectangle2D (java.awt.geom.Rectangle2D)8 Color (java.awt.Color)7 Shape (java.awt.Shape)6 TextLayoutInfo (g4p_controls.StyledString.TextLayoutInfo)5 Dimension (java.awt.Dimension)5 TextHitInfo (java.awt.font.TextHitInfo)4 ArrayList (java.util.ArrayList)4 Graphics (java.awt.Graphics)3 Insets (java.awt.Insets)3