use of com.xenoage.utils.font.FontInfo in project Zong by Xenoage.
the class FormattedTextParagraphTest method lineBreakTest.
@Test
public void lineBreakTest() {
FormattedTextStyle style = new FormattedTextStyle(new FontInfo("Arial", 72f, null));
float widthW = new FormattedTextString("w", style).getMetrics().getWidth();
float widthMinus = new FormattedTextString("-", style).getMetrics().getWidth();
float widthSpace = new FormattedTextString(" ", style).getMetrics().getWidth();
// | : where the line break should happen
// 1st test: "www|ww"
FormattedTextParagraph paragraph = Companion.fPara(Companion.fString("wwwww", style));
float width = 3.2f * widthW;
List<FormattedTextParagraph> lines = paragraph.lineBreak(width);
assertEquals(2, lines.size());
assertEquals("www", lines.get(0).getText());
assertEquals("ww", lines.get(1).getText());
// 2nd test: "ww-|www"
paragraph = Companion.fPara(Companion.fString("ww-www", style));
width = 3.2f * widthW + widthMinus;
lines = paragraph.lineBreak(width);
assertEquals(2, lines.size());
assertEquals("ww-", lines.get(0).getText());
assertEquals("www", lines.get(1).getText());
// 3rd test: like 2nd test, but with more elements: "w"+"w-|w"+"ww"
paragraph = new FormattedTextParagraph(CList.<FormattedTextElement>ilist(new FormattedTextString("w", style), new FormattedTextString("w-w", style), new FormattedTextString("ww", style)), FormattedTextParagraph.Companion.getDefaultAlignment());
width = 3.2f * widthW + widthMinus;
lines = paragraph.lineBreak(width);
assertEquals(2, lines.size());
assertEquals("ww-", lines.get(0).getText());
assertEquals("www", lines.get(1).getText());
assertEquals(2, lines.get(0).getElements().size());
assertEquals(2, lines.get(1).getElements().size());
// 4th test: break at the right position: "www www |www"
paragraph = Companion.fPara(Companion.fString("www www www", style));
width = 7.2f * widthW + 2 * widthSpace;
lines = paragraph.lineBreak(width);
assertEquals(2, lines.size());
assertEquals("www www ", lines.get(0).getText());
assertEquals("www", lines.get(1).getText());
// 5th test: "w|w"
paragraph = Companion.fPara(Companion.fString("ww", style));
width = 1.2f * widthW;
lines = paragraph.lineBreak(width);
assertEquals(2, lines.size());
assertEquals("w", lines.get(0).getText());
assertEquals("w", lines.get(1).getText());
// 5th test: "ww", but not even enough space for one w
paragraph = Companion.fPara(Companion.fString("ww", style));
width = 0.8f * widthW;
lines = paragraph.lineBreak(width);
assertEquals(0, lines.size());
// 6th test: ". |www"
paragraph = Companion.fPara(Companion.fString(". www", style));
width = 3.1f * widthW;
lines = paragraph.lineBreak(width);
assertEquals(2, lines.size());
assertEquals(". ", lines.get(0).getText());
assertEquals("www", lines.get(1).getText());
// 7th test: " |www|ww "
paragraph = Companion.fPara(Companion.fString(" wwwww ", style));
width = 3.05f * widthW;
lines = paragraph.lineBreak(width);
assertEquals(3, lines.size());
assertEquals(" ", lines.get(0).getText());
assertEquals("www", lines.get(1).getText());
assertEquals("ww ", lines.get(2).getText());
// 8th test: "ww www w" -> "w|w |w|w|w |w"
paragraph = Companion.fPara(Companion.fString("ww www w", style));
width = 1.1f * widthW;
lines = paragraph.lineBreak(width);
assertEquals(6, lines.size());
assertEquals("w", lines.get(0).getText());
assertEquals("w ", lines.get(1).getText());
assertEquals("w", lines.get(2).getText());
assertEquals("w", lines.get(3).getText());
assertEquals("w ", lines.get(4).getText());
assertEquals("w", lines.get(5).getText());
// 9th test: like 8th test, but using several elements
paragraph = new FormattedTextParagraph(CList.<FormattedTextElement>ilist(new FormattedTextString("w", style), new FormattedTextString("w ", style), new FormattedTextString("ww", style), new FormattedTextString("w w", style)), FormattedTextParagraph.Companion.getDefaultAlignment());
width = 1.1f * widthW;
lines = paragraph.lineBreak(width);
assertEquals(6, lines.size());
assertEquals("w", lines.get(0).getText());
assertEquals("w ", lines.get(1).getText());
assertEquals("w", lines.get(2).getText());
assertEquals("w", lines.get(3).getText());
assertEquals("w ", lines.get(4).getText());
assertEquals("w", lines.get(5).getText());
// 10th test: "ww |ww"
paragraph = Companion.fPara(Companion.fString("ww ww", style));
width = 2.1f * widthW;
lines = paragraph.lineBreak(width);
assertEquals(2, lines.size());
assertEquals("ww ", lines.get(0).getText());
assertEquals("ww", lines.get(1).getText());
}
use of com.xenoage.utils.font.FontInfo in project Zong by Xenoage.
the class FontInfoReader method read.
@MaybeNull
public FontInfo read() {
IList<String> families = readFamilies();
Float size = readSize();
FontStyle style = readStyle();
return new FontInfo(families, size, style);
}
use of com.xenoage.utils.font.FontInfo in project Zong by Xenoage.
the class FormattedTextReader method readStyle.
@NonNull
public FormattedTextStyle readStyle(MxlFormattedText mxlText) {
MxlPrintStyle mxlPrintStyle = mxlText.getPrintStyle();
FontInfo font = new FontInfoReader(mxlPrintStyle.getFont(), FontInfo.defaultValue).read();
Color color = readColor(mxlPrintStyle);
return new FormattedTextStyle(font, color, Superscript.Normal);
}
use of com.xenoage.utils.font.FontInfo in project Zong by Xenoage.
the class ChordNotator method compute.
public ChordNotation compute(Chord chord, Context context, @MaybeNull Notations notations) {
Score score = context.score;
float interlineSpace = score.getInterlineSpace(context.mp);
FontInfo lyricsFont = score.getFormat().getLyricFont();
MusicContext mc = score.getMusicContext(context.mp, BeforeOrAt, Before);
// grace or normal chord?
boolean grace = chord.isGrace();
ChordWidths chordWidths = (grace ? context.settings.graceChordWidths : context.settings.chordWidths);
ChordSpacings spacings = (grace ? context.settings.spacings.graceChordSpacings : context.settings.spacings.normalChordSpacings);
// use or compute stem direction
StemDirection stemDirection = chord.getStem().getDirection();
if (stemDirection == StemDirection.Default) {
// if stem direction was not computed yet, compute it now
if (notations != null)
stemDirection = notations.getChord(chord).stemDirection;
if (stemDirection == StemDirection.Default) {
Map<Chord, StemDirection> computedStems = stemDirector.compute(chord);
stemDirection = computedStems.get(chord);
// also remember the other computed stems
if (notations != null)
for (Chord computedChord : computedStems.keySet()) notations.getChord(computedChord).stemDirection = computedStems.get(computedChord);
}
}
// notes displacement
NotesNotation notes = notesNotator.compute(chord, stemDirection, chordWidths, mc);
float leftSuspendedWidth = (notes.leftSuspended ? notes.noteheadWidthIs : 0);
// accidentals
AccidentalsNotation accs = accidentalsNotator.compute(chord, notes, chordWidths, mc);
// symbol's width: width of the noteheads and dots
float symbolWidth = notes.widthIs - leftSuspendedWidth;
float frontGap = accs.widthIs + leftSuspendedWidth;
// rear gap: empty duration-dependent space behind the chord minus the symbol's width
float rearGap = spacings.getWidth(chord.getDisplayedDuration()) - symbolWidth;
// lyric width
float lyricWidth = 0;
TextMeasurer textMeasurer = platformUtils().getTextMeasurer();
for (Lyric lyric : chord.getLyrics()) {
if (lyric != null && lyric.getText() != null) {
// width of lyric in interline spaces
FormattedText lyricText = styleText(lyric.getText(), new FormattedTextStyle(lyricsFont));
float l = lyricText.getWidth() / interlineSpace;
// for start and end syllable, request "-" more space, for middle syllables "--"
// TODO: unsymmetric - start needs space on the right, end on the left, ...
SyllableType lyricType = lyric.getSyllableType();
if (lyricType == SyllableType.Begin || lyricType == SyllableType.End) {
l += textMeasurer.measure(lyricsFont, "-").getWidth() / interlineSpace;
} else if (lyricType == SyllableType.Middle) {
l += textMeasurer.measure(lyricsFont, "--").getWidth() / interlineSpace;
}
// save width of the widest lyric
lyricWidth = Math.max(lyricWidth, l);
}
}
// compute length of the stem (if any)
float scaling = grace ? context.settings.scalingGrace : 1;
StemNotation stem = stemNotator.compute(chord.getStem(), notes.getLps(), stemDirection, context.mp.getStaff(), Companion.staffLines(mc.getLinesCount()), scaling);
// compute articulations
ArticulationsNotation arts = articulationsNotator.compute(chord, stemDirection, notes, mc.getLinesCount());
return new ChordNotation(chord, chord.getMP(), new ElementWidth(frontGap, symbolWidth, rearGap, lyricWidth), context.mp.getStaff(), notes, stemDirection, stem, accs, arts);
}
use of com.xenoage.utils.font.FontInfo in project Zong by Xenoage.
the class AwtFontUtils method fromAwtFont.
/**
* Creates a new {@link FontInfo}.
* @param font the AWT font
*/
public static FontInfo fromAwtFont(Font font) {
IList<String> families = ilist(font.getFamily());
FontStyle style = FontStyle.normal;
if (font.isBold())
style = style.with(FontStyle.Bold, true);
if (font.isItalic())
style = style.with(FontStyle.Italic, true);
return new FontInfo(families, font.getSize2D(), style);
}
Aggregations