Search in sources :

Example 21 with AttributedString

use of java.text.AttributedString in project poi by apache.

the class DrawTextParagraph method mapFontCharset.

/**
     * Map text charset depending on font family.
     * Currently this only maps for wingdings font (into unicode private use area)
     *
     * @param text the raw text
     * @param fontFamily the font family
     * @return AttributedString with mapped codepoints
     *
     * @see <a href="http://stackoverflow.com/questions/8692095">Drawing exotic fonts in a java applet</a>
     * @see StringUtil#mapMsCodepointString(String)
     */
protected String mapFontCharset(String text, String fontFamily) {
    // TODO: find a real charset mapping solution instead of hard coding for Wingdings
    String attStr = text;
    if ("Wingdings".equalsIgnoreCase(fontFamily)) {
        // wingdings doesn't contain high-surrogates, so chars are ok
        boolean changed = false;
        char[] chrs = attStr.toCharArray();
        for (int i = 0; i < chrs.length; i++) {
            // only change valid chars
            if ((0x20 <= chrs[i] && chrs[i] <= 0x7f) || (0xa0 <= chrs[i] && chrs[i] <= 0xff)) {
                chrs[i] |= 0xf000;
                changed = true;
            }
        }
        if (changed) {
            attStr = new String(chrs);
        }
    }
    return attStr;
}
Also used : AttributedString(java.text.AttributedString) Paint(java.awt.Paint)

Example 22 with AttributedString

use of java.text.AttributedString in project poi by apache.

the class SheetUtil method getDefaultCharWidth.

/**
     * Get default character width using the Workbook's default font
     *
     * @param wb the workbook to get the default character width from
     * @return default character width in pixels
     */
@Internal
public static int getDefaultCharWidth(final Workbook wb) {
    Font defaultFont = wb.getFontAt((short) 0);
    AttributedString str = new AttributedString(String.valueOf(defaultChar));
    copyAttributes(defaultFont, str, 0, 1);
    TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext);
    return (int) layout.getAdvance();
}
Also used : AttributedString(java.text.AttributedString) Font(org.apache.poi.ss.usermodel.Font) TextLayout(java.awt.font.TextLayout) Internal(org.apache.poi.util.Internal)

Example 23 with AttributedString

use of java.text.AttributedString in project intellij-community by JetBrains.

the class SimpleColoredComponent method createAndCacheTextLayout.

private TextLayout createAndCacheTextLayout(int fragmentIndex, Font basefont, FontRenderContext fontRenderContext) {
    String text = myFragments.get(fragmentIndex);
    AttributedString string = new AttributedString(text);
    int start = 0;
    int end = text.length();
    AttributedCharacterIterator it = string.getIterator(new AttributedCharacterIterator.Attribute[0], start, end);
    Font currentFont = basefont;
    int currentIndex = start;
    for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) {
        Font font = basefont;
        if (!font.canDisplay(c)) {
            for (SuitableFontProvider provider : SuitableFontProvider.EP_NAME.getExtensions()) {
                font = provider.getFontAbleToDisplay(c, basefont.getSize(), basefont.getStyle(), basefont.getFamily());
                if (font != null)
                    break;
            }
        }
        int i = it.getIndex();
        if (!Comparing.equal(currentFont, font)) {
            if (i > currentIndex) {
                string.addAttribute(TextAttribute.FONT, currentFont, currentIndex, i);
            }
            currentFont = font;
            currentIndex = i;
        }
    }
    if (currentIndex < end) {
        string.addAttribute(TextAttribute.FONT, currentFont, currentIndex, end);
    }
    TextLayout layout = new TextLayout(string.getIterator(), fontRenderContext);
    if (fragmentIndex >= myLayouts.size()) {
        myLayouts.addAll(Collections.nCopies(fragmentIndex - myLayouts.size() + 1, null));
    }
    myLayouts.set(fragmentIndex, layout);
    myLayoutFont = getBaseFont();
    return layout;
}
Also used : AttributedString(java.text.AttributedString) AttributedString(java.text.AttributedString) AttributedCharacterIterator(java.text.AttributedCharacterIterator) TextLayout(java.awt.font.TextLayout)

Example 24 with AttributedString

use of java.text.AttributedString in project poi by apache.

the class HwmfGraphics method drawString.

public void drawString(byte[] text, Rectangle2D bounds, int[] dx) {
    HwmfFont font = prop.getFont();
    if (font == null || text == null || text.length == 0) {
        return;
    }
    double fontH = getFontHeight(font);
    // TODO: another approx. ...
    double fontW = fontH / 1.8;
    int len = text.length;
    Charset charset = (font.getCharSet().getCharset() == null) ? DEFAULT_CHARSET : font.getCharSet().getCharset();
    String textString = new String(text, charset);
    AttributedString as = new AttributedString(textString);
    if (dx == null || dx.length == 0) {
        addAttributes(as, font);
    } else {
        int[] dxNormed = dx;
        //dxNormed[1] = 14 textString.get(1) = U+30ED
        if (textString.length() != text.length) {
            int codePoints = textString.codePointCount(0, textString.length());
            dxNormed = new int[codePoints];
            int dxPosition = 0;
            for (int offset = 0; offset < textString.length(); ) {
                dxNormed[offset] = dx[dxPosition];
                int[] chars = new int[1];
                int cp = textString.codePointAt(offset);
                chars[0] = cp;
                //now figure out how many bytes it takes to encode that
                //code point in the charset
                int byteLength = new String(chars, 0, chars.length).getBytes(charset).length;
                dxPosition += byteLength;
                offset += Character.charCount(cp);
            }
        }
        for (int i = 0; i < dxNormed.length; i++) {
            addAttributes(as, font);
            // therefore we need to add the additional/suffix width to the next char
            if (i < dxNormed.length - 1) {
                as.addAttribute(TextAttribute.TRACKING, (dxNormed[i] - fontW) / fontH, i + 1, i + 2);
            }
        }
    }
    double angle = Math.toRadians(-font.getEscapement() / 10.);
    final AffineTransform at = graphicsCtx.getTransform();
    try {
        graphicsCtx.translate(bounds.getX(), bounds.getY() + fontH);
        graphicsCtx.rotate(angle);
        if (prop.getBkMode() == HwmfBkMode.OPAQUE) {
            // TODO: validate bounds
            graphicsCtx.setBackground(prop.getBackgroundColor().getColor());
            graphicsCtx.fill(new Rectangle2D.Double(0, 0, bounds.getWidth(), bounds.getHeight()));
        }
        graphicsCtx.setColor(prop.getTextColor().getColor());
        // (float)bounds.getX(), (float)bounds.getY());
        graphicsCtx.drawString(as.getIterator(), 0, 0);
    } finally {
        graphicsCtx.setTransform(at);
    }
}
Also used : AttributedString(java.text.AttributedString) Rectangle2D(java.awt.geom.Rectangle2D) Charset(java.nio.charset.Charset) AffineTransform(java.awt.geom.AffineTransform) AttributedString(java.text.AttributedString) HwmfFont(org.apache.poi.hwmf.record.HwmfFont) TexturePaint(java.awt.TexturePaint) Paint(java.awt.Paint)

Example 25 with AttributedString

use of java.text.AttributedString in project poi by apache.

the class BaseTestBugzillaIssues method computeCellWidthManually.

// FIXME: this function is a self-fulfilling prophecy: this test will always pass as long
// as the code-under-test and the testcase code are written the same way (have the same bugs). 
private double computeCellWidthManually(Cell cell0, Font font) {
    final FontRenderContext fontRenderContext = new FontRenderContext(null, true, true);
    RichTextString rt = cell0.getRichStringCellValue();
    String[] lines = rt.getString().split("\\n");
    assertEquals(1, lines.length);
    String txt = lines[0] + "0";
    AttributedString str = new AttributedString(txt);
    copyAttributes(font, str, txt.length());
    // TODO: support rich text fragments
    /*if (rt.numFormattingRuns() > 0) {
        }*/
    TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext);
    double frameWidth = getFrameWidth(layout);
    return ((frameWidth / 1) / 8);
}
Also used : AttributedString(java.text.AttributedString) FontRenderContext(java.awt.font.FontRenderContext) AttributedString(java.text.AttributedString) TextLayout(java.awt.font.TextLayout)

Aggregations

AttributedString (java.text.AttributedString)86 AttributedCharacterIterator (java.text.AttributedCharacterIterator)37 TextLayout (java.awt.font.TextLayout)29 LineBreakMeasurer (java.awt.font.LineBreakMeasurer)16 FontRenderContext (java.awt.font.FontRenderContext)13 Font (java.awt.Font)11 Point (java.awt.Point)11 Paint (java.awt.Paint)10 Graphics2D (java.awt.Graphics2D)9 WeakHashMap (java.util.WeakHashMap)8 Color (java.awt.Color)4 Bidi (java.text.Bidi)4 ArrayList (java.util.ArrayList)4 TreeSet (java.util.TreeSet)4 Rectangle (java.awt.Rectangle)3 HashSet (java.util.HashSet)3 Dimension (java.awt.Dimension)2 Insets (java.awt.Insets)2 AffineTransform (java.awt.geom.AffineTransform)2 Rectangle2D (java.awt.geom.Rectangle2D)2