Search in sources :

Example 1 with TextMetrics

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

the class AndroidTextMeasurer method measure.

public static TextMetrics measure(Paint paint, String text) {
    FontMetrics metrics = new FontMetrics();
    paint.getFontMetrics(metrics);
    float ascent = Units.pxToMm(Math.abs(metrics.ascent), 1);
    float descent = Units.pxToMm(metrics.descent, 1);
    float leading = Units.pxToMm(metrics.leading, 1);
    float width = Units.pxToMm(paint.measureText(text), 1);
    return new TextMetrics(ascent, descent, leading, width);
}
Also used : FontMetrics(android.graphics.Paint.FontMetrics) TextMetrics(com.xenoage.utils.font.TextMetrics)

Example 2 with TextMetrics

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

the class AwtTextMeasurer method measure.

public static TextMetrics measure(Font awtFont, String text) {
    if (text.length() == 0)
        return measureEmpty(awtFont);
    TextLayout layout = new TextLayout(text, awtFont, new FontRenderContext(null, false, true));
    float ascent = Units.pxToMm(layout.getAscent(), 1);
    float descent = Units.pxToMm(layout.getDescent(), 1);
    float leading = Units.pxToMm(layout.getLeading(), 1);
    float width = Units.pxToMm((float) layout.getBounds().getWidth(), 1);
    return new TextMetrics(ascent, descent, leading, width);
}
Also used : TextMetrics(com.xenoage.utils.font.TextMetrics) FontRenderContext(java.awt.font.FontRenderContext) TextLayout(java.awt.font.TextLayout)

Example 3 with TextMetrics

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

the class GwtTextMeasurer method measure.

@Override
public TextMetrics measure(FontInfo font, String text) {
    Context2d context = getContext();
    context.setFont(GwtFontUtils.getCssFont(font));
    com.google.gwt.canvas.dom.client.TextMetrics metrics = context.measureText(text);
    float width = Units.pxToMm((float) metrics.getWidth(), 1);
    // we do not know the ascent from HTML. we use the width of "W" instead, which should be similar.
    // as the descent and the leading we usethe third of the ascent.n ha
    metrics = context.measureText("W");
    float ascent = Units.pxToMm((float) metrics.getWidth(), 1);
    return new TextMetrics(ascent, ascent / 3, ascent / 3, width);
}
Also used : Context2d(com.google.gwt.canvas.dom.client.Context2d) TextMetrics(com.xenoage.utils.font.TextMetrics)

Example 4 with TextMetrics

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

the class JfxCanvas method drawText.

@Override
public void drawText(FormattedText text, TextSelection selection, Point2f position, boolean yIsBaseline, float frameWidth) {
    context.save();
    context.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;
                context.setFill(toJavaFXColor(t.getStyle().getColor()));
                Font font = toJavaFXFont(t.getStyle().getFont());
                context.setFont(font);
                context.save();
                context.scale(Units.pxToMm_1_1, Units.pxToMm_1_1);
                context.fillText(t.getText(), offsetX / Units.pxToMm_1_1, offsetY / Units.pxToMm_1_1);
                context.restore();
            } else {
                // symbol
                FormattedTextSymbol fts = (FormattedTextSymbol) e;
                float scaling = fts.getScaling();
                SymbolsRenderer.draw(fts.getSymbol(), this, Color.Companion.getBlack(), new Point2f(offsetX + fts.getOffsetX(), offsetY + fts.getSymbol().getBaselineOffset() * scaling), new Point2f(scaling, scaling));
            }
            offsetX += e.getMetrics().getWidth();
        }
        // next line
        offsetY += p.getMetrics().getAscent() + p.getMetrics().getDescent() + p.getMetrics().getLeading();
    }
    context.restore();
}
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) FormattedTextElement(com.xenoage.zong.core.text.FormattedTextElement) FormattedTextSymbol(com.xenoage.zong.core.text.FormattedTextSymbol) Font(javafx.scene.text.Font) JfxFontUtils.toJavaFXFont(com.xenoage.utils.jse.javafx.font.JfxFontUtils.toJavaFXFont)

Example 5 with TextMetrics

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

the class GwtCanvas method drawText.

/**
 * {@inheritDoc}
 * The text selection is ignored.
 */
@Override
public void drawText(FormattedText text, TextSelection selection, Point2f position, boolean yIsBaseline, float frameWidth) {
    context.save();
    context.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) {
                FormattedTextString t = (FormattedTextString) e;
                context.setFillStyle(GwtColorUtils.createColor(t.getStyle().getColor()));
                context.save();
                context.scale(Units.pxToMm_1_1, Units.pxToMm_1_1);
                context.setFont(GwtFontUtils.getCssFont(t.getStyle().getFont()));
                context.fillText(t.getText(), offsetX / Units.pxToMm_1_1, offsetY / Units.pxToMm_1_1);
                // Debug: Paint dot at text offset
                // context.fillRect(offsetX / Units.pxToMm_1_1, offsetY / Units.pxToMm_1_1, 2, 2);
                context.restore();
            } else {
                // symbol
                FormattedTextSymbol fts = (FormattedTextSymbol) e;
                float scaling = fts.getScaling();
                SymbolsRenderer.draw(fts.getSymbol(), this, 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();
    }
    context.restore();
}
Also used : Point2f(com.xenoage.utils.math.geom.Point2f) TextMetrics(com.xenoage.utils.font.TextMetrics)

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