Search in sources :

Example 1 with FontInfo

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());
}
Also used : FormattedTextString(com.xenoage.zong.core.text.FormattedTextString) FormattedTextStyle(com.xenoage.zong.core.text.FormattedTextStyle) FormattedTextParagraph(com.xenoage.zong.core.text.FormattedTextParagraph) FontInfo(com.xenoage.utils.font.FontInfo) FormattedTextElement(com.xenoage.zong.core.text.FormattedTextElement) Test(org.junit.Test)

Example 2 with FontInfo

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);
}
Also used : FontStyle(com.xenoage.utils.font.FontStyle) MxlFontStyle(com.xenoage.zong.musicxml.types.enums.MxlFontStyle) FontInfo(com.xenoage.utils.font.FontInfo) MaybeNull(com.xenoage.utils.annotations.MaybeNull)

Example 3 with FontInfo

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);
}
Also used : Color(com.xenoage.utils.color.Color) FormattedTextStyle(com.xenoage.zong.core.text.FormattedTextStyle) MxlPrintStyle(com.xenoage.zong.musicxml.types.attributes.MxlPrintStyle) FontInfo(com.xenoage.utils.font.FontInfo) NonNull(com.xenoage.utils.annotations.NonNull)

Example 4 with FontInfo

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);
}
Also used : ElementWidth(com.xenoage.zong.musiclayout.spacing.ElementWidth) ChordNotation(com.xenoage.zong.musiclayout.notation.ChordNotation) FormattedTextStyle(com.xenoage.zong.core.text.FormattedTextStyle) FormattedText(com.xenoage.zong.core.text.FormattedText) MusicContext(com.xenoage.zong.core.music.MusicContext) StemNotation(com.xenoage.zong.musiclayout.notation.chord.StemNotation) Score(com.xenoage.zong.core.Score) ArticulationsNotation(com.xenoage.zong.musiclayout.notation.chord.ArticulationsNotation) TextMeasurer(com.xenoage.utils.font.TextMeasurer) ChordWidths(com.xenoage.zong.musiclayout.settings.ChordWidths) AccidentalsNotation(com.xenoage.zong.musiclayout.notation.chord.AccidentalsNotation) Lyric(com.xenoage.zong.core.music.lyric.Lyric) ChordSpacings(com.xenoage.zong.musiclayout.settings.ChordSpacings) SyllableType(com.xenoage.zong.core.music.lyric.SyllableType) NotesNotation(com.xenoage.zong.musiclayout.notation.chord.NotesNotation) FontInfo(com.xenoage.utils.font.FontInfo) StemDirection(com.xenoage.zong.core.music.chord.StemDirection) Chord(com.xenoage.zong.core.music.chord.Chord)

Example 5 with FontInfo

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);
}
Also used : FontStyle(com.xenoage.utils.font.FontStyle) FontInfo(com.xenoage.utils.font.FontInfo)

Aggregations

FontInfo (com.xenoage.utils.font.FontInfo)12 FormattedTextStyle (com.xenoage.zong.core.text.FormattedTextStyle)9 FormattedTextString (com.xenoage.zong.core.text.FormattedTextString)5 FontStyle (com.xenoage.utils.font.FontStyle)4 FormattedTextParagraph (com.xenoage.zong.core.text.FormattedTextParagraph)4 FormattedTextElement (com.xenoage.zong.core.text.FormattedTextElement)3 Test (org.junit.Test)3 Color (com.xenoage.utils.color.Color)2 Score (com.xenoage.zong.core.Score)2 Chord (com.xenoage.zong.core.music.chord.Chord)2 FormattedText (com.xenoage.zong.core.text.FormattedText)2 MaybeNull (com.xenoage.utils.annotations.MaybeNull)1 NonNull (com.xenoage.utils.annotations.NonNull)1 TextMeasurer (com.xenoage.utils.font.TextMeasurer)1 AwtFontUtils.toAwtFont (com.xenoage.utils.jse.font.AwtFontUtils.toAwtFont)1 MusicContext (com.xenoage.zong.core.music.MusicContext)1 Pitch (com.xenoage.zong.core.music.Pitch)1 StemDirection (com.xenoage.zong.core.music.chord.StemDirection)1 Words (com.xenoage.zong.core.music.direction.Words)1 TraditionalKey (com.xenoage.zong.core.music.key.TraditionalKey)1