use of com.xenoage.zong.core.text.FormattedTextParagraph in project Zong by Xenoage.
the class DefaultTexts method getTempoTextNotNull.
public static FormattedText getTempoTextNotNull(Tempo tempo, SymbolPool symbolPool) {
FormattedTextStyle style = FormattedTextStyle.Companion.getDefaultStyle();
if (tempo.getText() != null) {
// use custom text
return styleText(tempo.getText(), style);
} else {
// show meaning, e.g. "♩ = 120"
CList<FormattedTextElement> elements = clist();
Fraction beat = tempo.getBaseBeat();
if (beat.equals(Companion.fr(1, 4))) {
elements.add(new FormattedTextSymbol(symbolPool.getSymbol(CommonSymbol.TextNoteQuarter), /* TODO staffStamping.is * FONT_SIZE_IN_IS */
12, FormattedTextStyle.Companion.getDefaultColor()));
} else if (beat.equals(Companion.fr(1, 2))) {
elements.add(new FormattedTextSymbol(symbolPool.getSymbol(CommonSymbol.TextNoteHalf), /* staffStamping.is * FONT_SIZE_IN_IS */
12, FormattedTextStyle.Companion.getDefaultColor()));
} else {
elements.add(new FormattedTextString(beat.toString(), style));
}
elements.add(new FormattedTextString(" = " + tempo.getBeatsPerMinute(), style));
FormattedTextParagraph paragraph = new FormattedTextParagraph(elements, Alignment.Left);
return Companion.fText(paragraph);
}
}
use of com.xenoage.zong.core.text.FormattedTextParagraph 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.zong.core.text.FormattedTextParagraph in project Zong by Xenoage.
the class FormattedTextConverter method fromStyledDocument.
/**
* Creates a {@link FormattedText} from the given {@link StyledDocument}.
*/
public static FormattedText fromStyledDocument(StyledDocument styledDoc) {
CList<FormattedTextParagraph> paragraphs = clist();
int endPos = styledDoc.getLength();
AttributeSet attribute = null;
String textelement = "";
String letter = "";
CList<FormattedTextElement> elements = clist();
Alignment alignment = null;
// paragraph, when '\n' is found
for (int i = 0; i < endPos; i++) {
try {
letter = styledDoc.getText(i, 1);
} catch (BadLocationException e) {
}
// line break: create new paragraph
if (letter.equals("\n")) {
if (!textelement.equals("")) {
elements.add(new FormattedTextString(textelement, getStyleFromAttributeSet(attribute)));
textelement = "";
}
paragraphs.add(new FormattedTextParagraph(elements, alignment));
elements = clist();
alignment = null;
} else if (attribute != styledDoc.getCharacterElement(i).getAttributes()) {
// set alignment, if not already done
if (alignment == null) {
alignment = fromAttributeSet(styledDoc.getParagraphElement(i).getAttributes());
}
// style has changed, so save old element and create a new one
if (!textelement.equals("")) {
elements.add(new FormattedTextString(textelement, getStyleFromAttributeSet(attribute)));
}
attribute = styledDoc.getCharacterElement(i).getAttributes();
textelement = letter;
} else {
// style stayed the same, so just append
textelement += letter;
}
}
if (!textelement.equals("")) {
// save the last string
elements.add(new FormattedTextString(textelement, getStyleFromAttributeSet(attribute)));
}
if (elements.size() > 0) {
// add (non-empty) paragraph
paragraphs.add(new FormattedTextParagraph(elements, alignment));
}
return new FormattedText(paragraphs);
}
use of com.xenoage.zong.core.text.FormattedTextParagraph 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);
}
Aggregations