use of com.xenoage.zong.core.text.FormattedTextString in project Zong by Xenoage.
the class FormattedTextUtilsTest method createText1Para.
private FormattedText createText1Para() {
// create a formatted text with one paragraph and three adjacent strings
// with the same style: "Hallo "{style1}, "An"{style2}, "dr"{style2}"ea"{style2}
FormattedTextStyle style1 = new FormattedTextStyle(12);
FormattedTextStyle style2 = new FormattedTextStyle(14, Color.Companion.getBlue());
return fText(fPara(CList.<FormattedTextElement>ilist(new FormattedTextString("Hallo ", style1), new FormattedTextString("An", style2), new FormattedTextString("dr", style2), new FormattedTextString("ea", style2)), Alignment.Left));
}
use of com.xenoage.zong.core.text.FormattedTextString in project Zong by Xenoage.
the class FormattedTextUtilsTest method insertElementTest2.
@Test
public void insertElementTest2() {
// test case: insert element with different style at the end of an element
FormattedText text = createText1Para();
FormattedTextString insertText = new FormattedTextString("di und An", // different style
text.getParagraphs().getFirst().getElements().get(0).getStyle());
// insert at index 8: "Hallo An{di und An}drea"
FormattedText textInsert = insert(text, 8, insertText);
assertEquals(1, textInsert.getParagraphs().size());
assertEquals(4, textInsert.getParagraphs().getFirst().getElements().size());
assertEquals(new FormattedTextString("Hallo ", text.getParagraphs().getFirst().getElements().get(0).getStyle()), textInsert.getParagraphs().getFirst().getElements().get(0));
assertEquals(new FormattedTextString("An", text.getParagraphs().getFirst().getElements().get(1).getStyle()), textInsert.getParagraphs().getFirst().getElements().get(1));
assertEquals(new FormattedTextString("di und An", text.getParagraphs().getFirst().getElements().get(0).getStyle()), textInsert.getParagraphs().getFirst().getElements().get(2));
assertEquals(new FormattedTextString("drea", text.getParagraphs().getFirst().getElements().get(1).getStyle()), textInsert.getParagraphs().getFirst().getElements().get(3));
}
use of com.xenoage.zong.core.text.FormattedTextString in project Zong by Xenoage.
the class FormattedTextUtilsTest method cleanTest.
@Test
public void cleanTest() {
FormattedText text = createText1Para();
FormattedText textCleaned = FormattedTextUtils.INSTANCE.clean(text);
assertEquals(1, textCleaned.getParagraphs().size());
assertEquals(2, textCleaned.getParagraphs().getFirst().getElements().size());
assertEquals(new FormattedTextString("Hallo ", text.getParagraphs().getFirst().getElements().get(0).getStyle()), textCleaned.getParagraphs().getFirst().getElements().get(0));
assertEquals(new FormattedTextString("Andrea", text.getParagraphs().getFirst().getElements().get(1).getStyle()), textCleaned.getParagraphs().getFirst().getElements().get(1));
}
use of com.xenoage.zong.core.text.FormattedTextString 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.FormattedTextString 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();
}
Aggregations