Search in sources :

Example 6 with FormattedTextStyle

use of com.xenoage.zong.core.text.FormattedTextStyle 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 7 with FormattedTextStyle

use of com.xenoage.zong.core.text.FormattedTextStyle in project Zong by Xenoage.

the class CreditsReader method createFormattedText.

/**
 * Creates a {@link FormattedText} and returns it.
 */
private static FormattedText createFormattedText(MxlCreditWords mxlCreditWords, FontInfo defaultFont) {
    CList<FormattedTextParagraph> paragraphs = clist();
    CList<FormattedTextElement> elements = clist();
    // iterate through all credit-words elements.
    // currently we are only interested in credit-words elements on page 1.
    Alignment alignment = FormattedTextParagraph.Companion.getDefaultAlignment();
    for (MxlFormattedText mxlFormattedText : mxlCreditWords.getItems()) {
        // read text. if empty, ignore this element
        String textContent = mxlFormattedText.getValue();
        if (textContent.length() == 0)
            continue;
        // apply horizontal alignment, if set, otherwise keep the old value
        Alignment newAlignment = readAlignment(mxlFormattedText.getJustify());
        if (newAlignment != null) {
            alignment = newAlignment;
        }
        // since we use paragraphs but MusicXML doesn't, split
        // the text where there are line breaks.
        String[] textLines = textContent.split("\n");
        boolean endsWithLineBreak = textContent.endsWith("\n");
        // new paragraphs for the following lines
        for (int iLine = 0; iLine < textLines.length; iLine++) {
            if (iLine > 0) {
                // not the first line: we have to create a new paragraph
                paragraphs.add(new FormattedTextParagraph(elements.close(), alignment));
                elements = clist();
            }
            // read line
            String textLine = textLines[iLine];
            if (textLine.length() > 0) {
                // font
                FontInfo fontInfo = new FontInfoReader(mxlFormattedText.getPrintStyle().getFont(), defaultFont).read();
                // color
                Color color = mxlFormattedText.getPrintStyle().getColor().getValue();
                // create text element
                FormattedTextElement textElement = new FormattedTextString(textLine, new FormattedTextStyle(fontInfo, color, null));
                elements.add(textElement);
            }
        }
        if (endsWithLineBreak) {
            // create a new paragraph
            paragraphs.add(new FormattedTextParagraph(elements.close(), alignment));
            elements = clist();
        }
    }
    // add non-empty paragraph at the end
    if (elements.size() > 0) {
        paragraphs.add(new FormattedTextParagraph(elements.close(), alignment));
    }
    return new FormattedText(paragraphs.close());
}
Also used : FormattedTextString(com.xenoage.zong.core.text.FormattedTextString) MxlFormattedText(com.xenoage.zong.musicxml.types.MxlFormattedText) Color(com.xenoage.utils.color.Color) FormattedTextStyle(com.xenoage.zong.core.text.FormattedTextStyle) FormattedTextString(com.xenoage.zong.core.text.FormattedTextString) FormattedText(com.xenoage.zong.core.text.FormattedText) MxlFormattedText(com.xenoage.zong.musicxml.types.MxlFormattedText) Alignment(com.xenoage.zong.core.text.Alignment) OtherReader.readAlignment(com.xenoage.zong.io.musicxml.in.readers.OtherReader.readAlignment) FormattedTextParagraph(com.xenoage.zong.core.text.FormattedTextParagraph) FontInfoReader(com.xenoage.zong.io.musicxml.in.readers.FontInfoReader) FormattedTextElement(com.xenoage.zong.core.text.FormattedTextElement) FontInfo(com.xenoage.utils.font.FontInfo)

Example 8 with FormattedTextStyle

use of com.xenoage.zong.core.text.FormattedTextStyle in project Zong by Xenoage.

the class StrategyTest method getScore.

@Override
public Score getScore() {
    // collect test material
    List<Example> examples = getAllExamples();
    // text style
    FormattedTextStyle style = Companion.getDefaultStyle().withFont(new FontInfo("Arial", 6f, FontStyle.normal));
    // one chord in each measure
    Score score = ScoreFactory.create1Staff();
    Cursor cursor = new Cursor(score, mp0, true);
    cursor.write(new TimeSignature(TimeType.Companion.getTime_3_4()));
    for (int i : range(examples)) {
        Example example = examples.get(i);
        cursor.setMp(atElement(0, i, 0, 0));
        // write key
        int fifths = ((TraditionalKey) example.getContext().getKey()).getFifths();
        cursor.write((ColumnElement) new TraditionalKey(fifths));
        // write example name (each 2nd example one line down for better reading)
        String text = (i % 2 == 1 ? "\n" : "") + example.getName();
        cursor.write((MeasureElement) new Words(styleText(text, style)));
        // write chord with all accidentals from context (or a rest)
        Map<Pitch, Integer> accs = example.getContext().getAccidentals();
        if (accs.size() > 0) {
            Pitch[] pitches = new Pitch[accs.size()];
            int accIndex = 0;
            for (Pitch acc : accs.keySet()) {
                pitches[accIndex] = Companion.pi(acc.getStep(), accs.get(acc), acc.getOctave());
                accIndex++;
            }
            Chord accsChords = ChordFactory.chord(pitches, Companion.get_1$4());
            cursor.write(accsChords);
        } else {
            cursor.write(new Rest(Companion.get_1$4()));
        }
        // write a rest
        cursor.write(new Rest(Companion.get_1$4()));
        // write the tested chord
        Chord testedChord = ChordFactory.chord(example.getPitches().toArray(new Pitch[0]), Companion.get_1$4());
        cursor.write(testedChord);
    }
    return score;
}
Also used : FormattedTextStyle(com.xenoage.zong.core.text.FormattedTextStyle) TraditionalKey(com.xenoage.zong.core.music.key.TraditionalKey) Cursor(com.xenoage.zong.io.selection.Cursor) TimeSignature(com.xenoage.zong.core.music.time.TimeSignature) Score(com.xenoage.zong.core.Score) Rest(com.xenoage.zong.core.music.rest.Rest) Example(material.accidentals.Example) Words(com.xenoage.zong.core.music.direction.Words) Pitch(com.xenoage.zong.core.music.Pitch) FontInfo(com.xenoage.utils.font.FontInfo) Chord(com.xenoage.zong.core.music.chord.Chord)

Example 9 with FormattedTextStyle

use of com.xenoage.zong.core.text.FormattedTextStyle in project Zong by Xenoage.

the class FormattedTextParagraphTest method testFormattedTextParagraph.

@Test
public void testFormattedTextParagraph() {
    FormattedTextStyle style = new FormattedTextStyle(new FontInfo((String) null, null, fontStyle(FontStyle.Bold)), Color.Companion.getRed(), Superscript.Super);
    FormattedTextParagraph paragraph = Companion.fPara(Companion.fString("This is a formatted text.", style));
    assertEquals("This is a formatted text.", paragraph.getText());
    style = ((FormattedTextString) paragraph.getElements().getFirst()).getStyle();
    assertEquals(true, style.getFont().getStyle().isSet(FontStyle.Bold));
    assertEquals(false, style.getFont().getStyle().isSet(FontStyle.Italic));
    assertEquals(false, style.getFont().getStyle().isSet(FontStyle.Underline));
    assertEquals(false, style.getFont().getStyle().isSet(FontStyle.Strikethrough));
    assertEquals(Color.Companion.getRed(), style.getColor());
    assertEquals(Superscript.Super, style.getSuperscript());
}
Also used : FormattedTextStyle(com.xenoage.zong.core.text.FormattedTextStyle) FormattedTextParagraph(com.xenoage.zong.core.text.FormattedTextParagraph) FormattedTextString(com.xenoage.zong.core.text.FormattedTextString) FormattedTextString.fString(com.xenoage.zong.core.text.FormattedTextString.fString) FontInfo(com.xenoage.utils.font.FontInfo) Test(org.junit.Test)

Example 10 with FormattedTextStyle

use of com.xenoage.zong.core.text.FormattedTextStyle in project Zong by Xenoage.

the class FormattedTextParagraphTest method getMixedStyleTextParagraph.

public static FormattedTextParagraph getMixedStyleTextParagraph() {
    FormattedTextStyle style1 = new FormattedTextStyle(new FontInfo(new JLabel().getFont().getName(), 14f, fontStyle(FontStyle.Italic, FontStyle.Underline)));
    FormattedTextStyle style2 = new FormattedTextStyle(new FontInfo(new JLabel().getFont().getName(), 14f, fontStyle(FontStyle.Bold, FontStyle.Italic, FontStyle.Underline, FontStyle.Strikethrough)), Color.Companion.getGreen(), null);
    FormattedTextParagraph paragraph = new FormattedTextParagraph(CList.<FormattedTextElement>ilist(new FormattedTextString("This is ", style1), new FormattedTextString("a mixed styled text!", style2)), FormattedTextParagraph.Companion.getDefaultAlignment());
    return paragraph;
}
Also used : FormattedTextString(com.xenoage.zong.core.text.FormattedTextString) FormattedTextStyle(com.xenoage.zong.core.text.FormattedTextStyle) FormattedTextParagraph(com.xenoage.zong.core.text.FormattedTextParagraph) JLabel(javax.swing.JLabel) FontInfo(com.xenoage.utils.font.FontInfo)

Aggregations

FormattedTextStyle (com.xenoage.zong.core.text.FormattedTextStyle)17 FormattedTextString (com.xenoage.zong.core.text.FormattedTextString)10 FontInfo (com.xenoage.utils.font.FontInfo)9 FormattedTextElement (com.xenoage.zong.core.text.FormattedTextElement)7 FormattedTextParagraph (com.xenoage.zong.core.text.FormattedTextParagraph)6 FormattedText (com.xenoage.zong.core.text.FormattedText)4 Test (org.junit.Test)4 Score (com.xenoage.zong.core.Score)3 Color (com.xenoage.utils.color.Color)2 Chord (com.xenoage.zong.core.music.chord.Chord)2 Alignment (com.xenoage.zong.core.text.Alignment)2 StaffStamping (com.xenoage.zong.musiclayout.stampings.StaffStamping)2 MaybeNull (com.xenoage.utils.annotations.MaybeNull)1 NonNull (com.xenoage.utils.annotations.NonNull)1 FontStyle (com.xenoage.utils.font.FontStyle)1 TextMeasurer (com.xenoage.utils.font.TextMeasurer)1 Fraction (com.xenoage.utils.math.Fraction)1 Point2f (com.xenoage.utils.math.geom.Point2f)1 MeasureNumbering (com.xenoage.zong.core.format.MeasureNumbering)1 ColumnHeader (com.xenoage.zong.core.header.ColumnHeader)1