Example usage for java.text AttributedCharacterIterator getAttribute

List of usage examples for java.text AttributedCharacterIterator getAttribute

Introduction

In this page you can find the example usage for java.text AttributedCharacterIterator getAttribute.

Prototype

public Object getAttribute(Attribute attribute);

Source Link

Document

Returns the value of the named attribute for the current character.

Usage

From source file:org.apache.fop.svg.AbstractFOPTextPainter.java

private boolean hasUnsupportedAttributes(AttributedCharacterIterator aci) {
    boolean hasUnsupported = false;

    Font font = getFont(aci);//w ww. j  a v a 2 s  .  co m
    String text = getText(aci);
    if (hasUnsupportedGlyphs(text, font)) {
        log.trace("-> Unsupported glyphs found");
        hasUnsupported = true;
    }

    TextPaintInfo tpi = (TextPaintInfo) aci
            .getAttribute(GVTAttributedCharacterIterator.TextAttribute.PAINT_INFO);
    if ((tpi != null)
            && ((tpi.strokeStroke != null && tpi.strokePaint != null) || (tpi.strikethroughStroke != null)
                    || (tpi.underlineStroke != null) || (tpi.overlineStroke != null))) {
        log.trace("-> under/overlines etc. found");
        hasUnsupported = true;
    }

    //Alpha is not supported
    Paint foreground = (Paint) aci.getAttribute(TextAttribute.FOREGROUND);
    if (foreground instanceof Color) {
        Color col = (Color) foreground;
        if (col.getAlpha() != 255) {
            log.trace("-> transparency found");
            hasUnsupported = true;
        }
    }

    Object letSpace = aci.getAttribute(GVTAttributedCharacterIterator.TextAttribute.LETTER_SPACING);
    if (letSpace != null) {
        log.trace("-> letter spacing found");
        hasUnsupported = true;
    }

    Object wordSpace = aci.getAttribute(GVTAttributedCharacterIterator.TextAttribute.WORD_SPACING);
    if (wordSpace != null) {
        log.trace("-> word spacing found");
        hasUnsupported = true;
    }

    Object lengthAdjust = aci.getAttribute(GVTAttributedCharacterIterator.TextAttribute.LENGTH_ADJUST);
    if (lengthAdjust != null) {
        log.trace("-> length adjustments found");
        hasUnsupported = true;
    }

    Object writeMod = aci.getAttribute(GVTAttributedCharacterIterator.TextAttribute.WRITING_MODE);
    if (writeMod != null && !GVTAttributedCharacterIterator.TextAttribute.WRITING_MODE_LTR.equals(writeMod)) {
        log.trace("-> Unsupported writing modes found");
        hasUnsupported = true;
    }

    Object vertOr = aci.getAttribute(GVTAttributedCharacterIterator.TextAttribute.VERTICAL_ORIENTATION);
    if (GVTAttributedCharacterIterator.TextAttribute.ORIENTATION_ANGLE.equals(vertOr)) {
        log.trace("-> vertical orientation found");
        hasUnsupported = true;
    }

    Object rcDel = aci.getAttribute(GVTAttributedCharacterIterator.TextAttribute.TEXT_COMPOUND_DELIMITER);
    //Batik 1.6 returns null here which makes it impossible to determine whether this can
    //be painted or not, i.e. fall back to stroking. :-(
    if (rcDel != null && !(rcDel instanceof SVGOMTextElement)) {
        log.trace("-> spans found");
        hasUnsupported = true; //Filter spans
    }

    if (hasUnsupported) {
        log.trace("Unsupported attributes found in ACI, using StrokingTextPainter");
    }
    return hasUnsupported;
}

From source file:org.apache.fop.svg.AbstractFOPTextPainter.java

/**
 * Paint a single text run on the Graphics2D at a given location.
 * @param run the text run to paint//  w w w.j a v  a2  s . co  m
 * @param g2d the Graphics2D to paint to
 * @param loc the current location of the "cursor"
 * @return the new location of the "cursor" after painting the text run
 */
protected Point2D paintTextRun(StrokingTextPainter.TextRun run, Graphics2D g2d, Point2D loc) {
    AttributedCharacterIterator aci = run.getACI();
    aci.first();

    updateLocationFromACI(aci, loc);
    AffineTransform at = g2d.getTransform();
    loc = at.transform(loc, null);

    // font
    Font font = getFont(aci);
    if (font != null) {
        nativeTextHandler.setOverrideFont(font);
    }

    // color
    TextPaintInfo tpi = (TextPaintInfo) aci
            .getAttribute(GVTAttributedCharacterIterator.TextAttribute.PAINT_INFO);
    if (tpi == null) {
        return loc;
    }
    Paint foreground = tpi.fillPaint;
    if (foreground instanceof Color) {
        Color col = (Color) foreground;
        g2d.setColor(col);
    }
    g2d.setPaint(foreground);

    // text anchor
    TextNode.Anchor anchor = (TextNode.Anchor) aci
            .getAttribute(GVTAttributedCharacterIterator.TextAttribute.ANCHOR_TYPE);

    // text
    String txt = getText(aci);
    float advance = getStringWidth(txt, font);
    float tx = 0;
    if (anchor != null) {
        switch (anchor.getType()) {
        case TextNode.Anchor.ANCHOR_MIDDLE:
            tx = -advance / 2;
            break;
        case TextNode.Anchor.ANCHOR_END:
            tx = -advance;
            break;
        default: //nop
        }
    }

    // draw string
    double x = loc.getX();
    double y = loc.getY();
    try {
        try {
            nativeTextHandler.drawString(g2d, txt, (float) x + tx, (float) y);
        } catch (IOException ioe) {
            if (g2d instanceof AFPGraphics2D) {
                ((AFPGraphics2D) g2d).handleIOException(ioe);
            }
        }
    } finally {
        nativeTextHandler.setOverrideFont(null);
    }
    loc.setLocation(loc.getX() + advance, loc.getY());
    return loc;
}

From source file:org.apache.fop.svg.AbstractFOPTextPainter.java

private void updateLocationFromACI(AttributedCharacterIterator aci, Point2D loc) {
    //Adjust position of span
    Float xpos = (Float) aci.getAttribute(GVTAttributedCharacterIterator.TextAttribute.X);
    Float ypos = (Float) aci.getAttribute(GVTAttributedCharacterIterator.TextAttribute.Y);
    Float dxpos = (Float) aci.getAttribute(GVTAttributedCharacterIterator.TextAttribute.DX);
    Float dypos = (Float) aci.getAttribute(GVTAttributedCharacterIterator.TextAttribute.DY);
    if (xpos != null) {
        loc.setLocation(xpos.doubleValue(), loc.getY());
    }//from   w w  w  .  j  a  v a  2  s. c  o m
    if (ypos != null) {
        loc.setLocation(loc.getX(), ypos.doubleValue());
    }
    if (dxpos != null) {
        loc.setLocation(loc.getX() + dxpos.doubleValue(), loc.getY());
    }
    if (dypos != null) {
        loc.setLocation(loc.getX(), loc.getY() + dypos.doubleValue());
    }
}

From source file:org.apache.fop.svg.ACIUtils.java

/**
 * Tries to find matching fonts in FOP's {@link FontInfo} instance for fonts used by
 * Apache Batik. The method inspects the various GVT attributes found in the ACI.
 * @param aci the ACI to find matching fonts for
 * @param fontInfo the font info instance with FOP's fonts
 * @return an array of matching fonts/*w  w  w.  j a v a2  s.co  m*/
 */
public static Font[] findFontsForBatikACI(AttributedCharacterIterator aci, FontInfo fontInfo) {
    List<Font> fonts = new java.util.ArrayList<Font>();
    @SuppressWarnings("unchecked")
    List<GVTFontFamily> gvtFonts = (List<GVTFontFamily>) aci
            .getAttribute(GVTAttributedCharacterIterator.TextAttribute.GVT_FONT_FAMILIES);
    Float posture = (Float) aci.getAttribute(TextAttribute.POSTURE);
    Float taWeight = (Float) aci.getAttribute(TextAttribute.WEIGHT);
    Float fontSize = (Float) aci.getAttribute(TextAttribute.SIZE);

    String style = toStyle(posture);
    int weight = toCSSWeight(taWeight);
    int fsize = (int) (fontSize.floatValue() * 1000);

    String firstFontFamily = null;

    //GVT_FONT can sometimes be different from the fonts in GVT_FONT_FAMILIES
    //or GVT_FONT_FAMILIES can even be empty and only GVT_FONT is set
    /* The following code section is not available until Batik 1.7 is released. */
    GVTFont gvtFont = (GVTFont) aci.getAttribute(GVTAttributedCharacterIterator.TextAttribute.GVT_FONT);
    if (gvtFont != null) {
        String gvtFontFamily = gvtFont.getFamilyName();
        if (fontInfo.hasFont(gvtFontFamily, style, weight)) {
            FontTriplet triplet = fontInfo.fontLookup(gvtFontFamily, style, weight);
            Font f = fontInfo.getFontInstance(triplet, fsize);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Found a font that matches the GVT font: " + gvtFontFamily + ", " + weight + ", "
                        + style + " -> " + f);
            }
            fonts.add(f);
        }
        firstFontFamily = gvtFontFamily;
    }

    if (gvtFonts != null) {
        boolean haveInstanceOfSVGFontFamily = false;
        for (GVTFontFamily fam : gvtFonts) {
            if (fam instanceof SVGFontFamily) {
                haveInstanceOfSVGFontFamily = true;
            }
            String fontFamily = fam.getFamilyName();
            if (fontInfo.hasFont(fontFamily, style, weight)) {
                FontTriplet triplet = fontInfo.fontLookup(fontFamily, style, weight);
                Font f = fontInfo.getFontInstance(triplet, fsize);
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Found a font that matches the GVT font family: " + fontFamily + ", " + weight
                            + ", " + style + " -> " + f);
                }
                fonts.add(f);
            }
            if (firstFontFamily == null) {
                firstFontFamily = fontFamily;
            }
        }
        // SVG fonts are embedded fonts in the SVG document and are rarely used; however if they
        // are used but the fonts also exists in the system and are known to FOP then FOP should
        // use them; then the decision whether Batik should stroke the text should be made after
        // no matching fonts are found
        if (fonts.isEmpty() && haveInstanceOfSVGFontFamily) {
            fontInfo.notifyStrokingSVGTextAsShapes(firstFontFamily);
            return null; // Let Batik paint this text!
        }
    }
    if (fonts.isEmpty()) {
        if (firstFontFamily == null) {
            //This will probably never happen. Just to be on the safe side.
            firstFontFamily = "any";
        }
        //lookup with fallback possibility (incl. substitution notification)
        FontTriplet triplet = fontInfo.fontLookup(firstFontFamily, style, weight);
        Font f = fontInfo.getFontInstance(triplet, fsize);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Falling back to adjustable font lookup up for: " + firstFontFamily + ", " + weight + ", "
                    + style + " -> " + f);
        }
        fonts.add(f);
    }
    return fonts.toArray(new Font[fonts.size()]);
}