FileDocCategorySizeDatePackage
TextDecorator.javaAPI DocAndroid 1.5 API14922Wed May 06 22:41:54 BST 2009org.apache.harmony.awt.gl.font

TextDecorator

public class TextDecorator extends Object
This class is responsible for rendering text decorations like underline, strikethrough, text with background, etc.

Fields Summary
private static final TextDecorator
inst
Constructors Summary
private TextDecorator()

      
Methods Summary
static voiddrawTextDecorations(TextRunSegment trs, java.awt.Graphics2D g2d, float xOffset, float yOffset)
Renders the text decorations

param
trs - text run segment
param
g2d - graphics to render to
param
xOffset - offset in X direction to the upper left corner of the layout from the origin of the graphics
param
yOffset - offset in Y direction to the upper left corner of the layout from the origin of the graphics

        Decoration d = trs.decoration;

        if (!d.ulOn && d.imUlStroke == null && !d.strikeThrough) {
            return; // Nothing to do
        }

        float left = xOffset + (float) trs.getLogicalBounds().getMinX();
        float right = xOffset + (float) trs.getLogicalBounds().getMaxX();

        Stroke savedStroke = g2d.getStroke();

        d.getStrokes(trs.metrics);

        if (d.strikeThrough) {
            float y = trs.y + yOffset + trs.metrics.strikethroughOffset;
            g2d.setStroke(d.strikeThroughStroke);
            g2d.draw(new Line2D.Float(left, y, right, y));
        }

        if (d.ulOn) {
            float y = trs.y + yOffset + trs.metrics.underlineOffset;
            g2d.setStroke(d.ulStroke);
            g2d.draw(new Line2D.Float(left, y, right, y));
        }

        if (d.imUlStroke != null) {
            float y = trs.y + yOffset + trs.metrics.underlineOffset;
            g2d.setStroke(d.imUlStroke);
            g2d.draw(new Line2D.Float(left, y, right, y));
            if (d.imUlStroke2 != null) {
                y++;
                g2d.setStroke(d.imUlStroke2);
                g2d.draw(new Line2D.Float(left, y, right, y));
            }
        }

        g2d.setStroke(savedStroke);
    
static java.awt.ShapeextendOutline(TextRunSegment trs, java.awt.Shape segmentOutline, org.apache.harmony.awt.gl.font.TextDecorator$Decoration d)
Extends the outline of the text run segment to include text decorations.

param
trs - text segment
param
segmentOutline - outline of the undecorated text
param
d - decoration
return
extended outline

        if (d == null || !d.ulOn && d.imUlStroke == null && !d.strikeThrough) {
            return segmentOutline; // Nothing to do
        }

        Area res = new Area(segmentOutline);

        float left = (float) trs.getLogicalBounds().getMinX() - trs.x;
        float right = (float) trs.getLogicalBounds().getMaxX() - trs.x;

        d.getStrokes(trs.metrics);

        if (d.strikeThrough) {
            float y = trs.metrics.strikethroughOffset;
            res.add(new Area(d.strikeThroughStroke.createStrokedShape(
                    new Line2D.Float(left, y, right, y)
            )));
        }

        if (d.ulOn) {
            float y = trs.metrics.underlineOffset;
            res.add(new Area(d.ulStroke.createStrokedShape(
                    new Line2D.Float(left, y, right, y)
            )));
        }

        if (d.imUlStroke != null) {
            float y = trs.metrics.underlineOffset;
            res.add(new Area(d.imUlStroke.createStrokedShape(
                    new Line2D.Float(left, y, right, y)
            )));

            if (d.imUlStroke2 != null) {
                y++;
                res.add(new Area(d.imUlStroke2.createStrokedShape(
                        new Line2D.Float(left, y, right, y)
                )));
            }
        }

        return res;
    
static java.awt.geom.Rectangle2DextendVisualBounds(TextRunSegment trs, java.awt.geom.Rectangle2D segmentBounds, org.apache.harmony.awt.gl.font.TextDecorator$Decoration d)
Extends the visual bounds of the text run segment to include text decorations.

param
trs - text segment
param
segmentBounds - bounds of the undecorated text
param
d - decoration
return
extended bounds

        if (d == null) {
            return segmentBounds;
        }
        double minx = segmentBounds.getMinX();
        double miny = segmentBounds.getMinY();
        double maxx = segmentBounds.getMaxX();
        double maxy = segmentBounds.getMaxY();

        Rectangle2D lb = trs.getLogicalBounds();

        if (d.swapBfFg || d.bg != null) {
            minx = Math.min(lb.getMinX() - trs.x, minx);
            miny = Math.min(lb.getMinY() - trs.y, miny);
            maxx = Math.max(lb.getMaxX() - trs.x, maxx);
            maxy = Math.max(lb.getMaxY() - trs.y, maxy);
        }

        if (d.ulOn || d.imUlStroke != null || d.strikeThrough) {
            minx = Math.min(lb.getMinX() - trs.x, minx);
            maxx = Math.max(lb.getMaxX() - trs.x, maxx);

            d.getStrokes(trs.metrics);

            if (d.ulStroke != null) {
                maxy = Math.max(
                        maxy,
                        trs.metrics.underlineOffset +
                        d.ulStroke.getLineWidth()
                );
            }

            if (d.imUlStroke != null) {
                maxy = Math.max(
                        maxy,
                        trs.metrics.underlineOffset +
                        d.imUlStroke.getLineWidth() +
                        (d.imUlStroke2 == null ? 0 : d.imUlStroke2.getLineWidth())
                );
            }
        }

        return new Rectangle2D.Double(minx, miny, maxx-minx, maxy-miny);
    
static org.apache.harmony.awt.gl.font.TextDecorator$DecorationgetDecoration(java.util.Map attributes)
Creates Decoration object from the set of text attributes

param
attributes - text attributes
return
Decoration object

        if (attributes == null) {
            return null; // It is for plain text
        }

        Object underline = attributes.get(TextAttribute.UNDERLINE);
        boolean hasStandardUnderline = underline == TextAttribute.UNDERLINE_ON;

        Object imUnderline = attributes.get(TextAttribute.INPUT_METHOD_UNDERLINE);
        Integer imUl = (Integer) imUnderline;

        boolean swapBgFg =
                TextAttribute.SWAP_COLORS_ON.equals(
                        attributes.get(TextAttribute.SWAP_COLORS)
                );

        boolean strikeThrough =
                TextAttribute.STRIKETHROUGH_ON.equals(
                        attributes.get(TextAttribute.STRIKETHROUGH)
                );

        Paint fg = (Paint) attributes.get(TextAttribute.FOREGROUND);
        Paint bg = (Paint) attributes.get(TextAttribute.BACKGROUND);

        if (
                !hasStandardUnderline &&
                imUnderline == null &&
                fg == null &&
                bg == null &&
                !swapBgFg &&
                !strikeThrough
        ) {
            return null;
        }
        return new Decoration(imUl, swapBgFg, strikeThrough, bg, fg, hasStandardUnderline);
    
static org.apache.harmony.awt.gl.font.TextDecoratorgetInstance()

        return inst;
    
static voidprepareGraphics(TextRunSegment trs, java.awt.Graphics2D g2d, float xOffset, float yOffset)
Fills the background before drawing if needed.

param
trs - text segment
param
g2d - graphics to draw to
param
xOffset - offset in X direction to the upper left corner of the layout from the origin of the graphics
param
yOffset - offset in Y direction to the upper left corner of the layout from the origin of the graphics

        Decoration d = trs.decoration;

        if (d.fg == null && d.bg == null && d.swapBfFg == false) {
            return; // Nothing to do
        }

        d.graphicsPaint = g2d.getPaint();

        if (d.fg == null) {
            d.fg = d.graphicsPaint;
        }

        if (d.swapBfFg) {
            // Fill background area
            g2d.setPaint(d.fg);
            Rectangle2D bgArea = trs.getLogicalBounds();
            Rectangle2D toFill =
                    new Rectangle2D.Double(
                            bgArea.getX() + xOffset,
                            bgArea.getY() + yOffset,
                            bgArea.getWidth(),
                            bgArea.getHeight()
                    );
            g2d.fill(toFill);

            // Set foreground color
            g2d.setPaint(d.bg == null ? Color.WHITE : d.bg);
        } else {
            if (d.bg != null) { // Fill background area
                g2d.setPaint(d.bg);
                Rectangle2D bgArea = trs.getLogicalBounds();
                Rectangle2D toFill =
                        new Rectangle2D.Double(
                                bgArea.getX() + xOffset,
                                bgArea.getY() + yOffset,
                                bgArea.getWidth(),
                                bgArea.getHeight()
                        );
                g2d.fill(toFill);
            }

            // Set foreground color
            g2d.setPaint(d.fg);
        }
    
static voidrestoreGraphics(org.apache.harmony.awt.gl.font.TextDecorator$Decoration d, java.awt.Graphics2D g2d)
Restores the original state of the graphics if needed

param
d - decoration
param
g2d - graphics

        if (d.fg == null && d.bg == null && d.swapBfFg == false) {
            return; // Nothing to do
        }

        g2d.setPaint(d.graphicsPaint);