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);
}
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);
}
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);
}
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();
}
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();
}
Aggregations