Search in sources :

Example 6 with TextMetrics

use of com.xenoage.utils.font.TextMetrics in project Zong by Xenoage.

the class AndroidCanvas method drawText.

/**
 * {@inheritDoc}
 * The text selection is ignored.
 */
@Override
public void drawText(FormattedText text, TextSelection selection, Point2f position, boolean yIsBaseline, float frameWidth) {
    int oldTransform = canvas.save();
    canvas.translate(position.x, position.y);
    // print the text frame paragraph for paragraph
    float offsetX = 0;
    float offsetY = 0;
    for (FormattedTextParagraph p : text.getParagraphs()) {
        TextMetrics pMetrics = p.getMetrics();
        if (!yIsBaseline)
            offsetY += pMetrics.getAscent();
        // adjustment
        if (p.getAlignment() == Alignment.Center)
            offsetX = (frameWidth - pMetrics.getWidth()) / 2;
        else if (p.getAlignment() == Alignment.Right)
            offsetX = frameWidth - pMetrics.getWidth();
        else
            offsetX = 0;
        // draw elements
        for (FormattedTextElement e : p.getElements()) {
            if (e instanceof FormattedTextString) {
                // TODO - formatting
                FormattedTextString t = (FormattedTextString) e;
                Paint paint = new Paint(AndroidColorUtils.black);
                paint.setTypeface(Typeface.SERIF);
                paint.setTextSize(Units.pxToMm(t.getStyle().getFont().getSize(), 1));
                // nice, smooth drawing
                paint.setFlags(Paint.LINEAR_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
                canvas.drawText(t.getText(), offsetX, offsetY, paint);
            } else {
                // symbol
                FormattedTextSymbol fts = (FormattedTextSymbol) e;
                float scaling = fts.getScaling();
                androidSymbolsRenderer.draw((PathSymbol) fts.getSymbol(), canvas, Color.black, new Point2f(offsetX + fts.getOffsetX(), offsetY + fts.getSymbol().baselineOffset * scaling), new Point2f(scaling, scaling));
            }
            offsetX += e.getMetrics().getWidth();
        }
        // next line
        offsetY += p.getMetrics().getAscent() + p.getMetrics().getDescent() + p.getMetrics().getLeading();
    }
    canvas.restoreToCount(oldTransform);
}
Also used : FormattedTextString(com.xenoage.zong.core.text.FormattedTextString) Point2f(com.xenoage.utils.math.geom.Point2f) TextMetrics(com.xenoage.utils.font.TextMetrics) FormattedTextParagraph(com.xenoage.zong.core.text.FormattedTextParagraph) Paint(android.graphics.Paint) FormattedTextElement(com.xenoage.zong.core.text.FormattedTextElement) FormattedTextSymbol(com.xenoage.zong.core.text.FormattedTextSymbol) Paint(android.graphics.Paint)

Aggregations

TextMetrics (com.xenoage.utils.font.TextMetrics)6 Point2f (com.xenoage.utils.math.geom.Point2f)3 FormattedTextElement (com.xenoage.zong.core.text.FormattedTextElement)2 FormattedTextParagraph (com.xenoage.zong.core.text.FormattedTextParagraph)2 FormattedTextString (com.xenoage.zong.core.text.FormattedTextString)2 FormattedTextSymbol (com.xenoage.zong.core.text.FormattedTextSymbol)2 Paint (android.graphics.Paint)1 FontMetrics (android.graphics.Paint.FontMetrics)1 Context2d (com.google.gwt.canvas.dom.client.Context2d)1 JfxFontUtils.toJavaFXFont (com.xenoage.utils.jse.javafx.font.JfxFontUtils.toJavaFXFont)1 FontRenderContext (java.awt.font.FontRenderContext)1 TextLayout (java.awt.font.TextLayout)1 Font (javafx.scene.text.Font)1