use of com.xenoage.zong.core.text.FormattedTextElement in project Zong by Xenoage.
the class FormattedTextParagraphTest method testMixedStyledTextParagraph.
@Test
public void testMixedStyledTextParagraph() {
FormattedTextParagraph text = getMixedStyleTextParagraph();
assertEquals("This is a mixed styled text!", text.getText());
List<FormattedTextElement> elements = text.getElements();
FormattedTextStyle style = ((FormattedTextString) elements.get(0)).getStyle();
assertEquals(true, style.getFont().getStyle().isSet(FontStyle.Italic));
assertEquals(true, style.getFont().getStyle().isSet(FontStyle.Underline));
assertEquals(14, style.getFont().getSize(), Delta.Df);
style = ((FormattedTextString) elements.get(1)).getStyle();
assertEquals(true, style.getFont().getStyle().isSet(FontStyle.Bold));
assertEquals(true, style.getFont().getStyle().isSet(FontStyle.Strikethrough));
assertEquals(Color.Companion.getGreen(), style.getColor());
}
use of com.xenoage.zong.core.text.FormattedTextElement 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.zong.core.text.FormattedTextElement in project Zong by Xenoage.
the class ScoreFrameLayouter method computeScoreFrameLayout.
/**
* Creates a {@link ScoreFrameLayout} from the given {@link FrameSpacing}.
*
* @param unclosedElements unclosed elements from the last frame, like slurs
* spanning over more than one frame
*/
public ScoreFrameLayout computeScoreFrameLayout(FrameSpacing frame, int frameIndex, Notations notations, List<ContinuedElement> unclosedElements, Context layouterContext, Map<Beam, BeamSpacing> beamsSpacing) {
layouterContext.saveMp();
Score score = layouterContext.score;
SymbolPool symbols = layouterContext.symbols;
StamperContext context = new StamperContext();
context.layouter = layouterContext;
context.notations = notations;
ScoreHeader header = score.getHeader();
int stavesCount = score.getStavesCount();
StavesList stavesList = score.getStavesList();
ArrayList<StaffStamping> staffStampsPool = alist();
ArrayList<Stamping> otherStampsPool = alist();
// default lyric style
FormattedTextStyle defaultLyricStyle = new FormattedTextStyle(score.getFormat().getLyricFont());
// caches
OpenSlursCache openCurvedLinesCache = new OpenSlursCache();
OpenWedges openWedges = new OpenWedges();
OpenLyricsCache openLyricsCache = new OpenLyricsCache();
LastLyrics lastLyrics = new LastLyrics();
OpenTupletsCache openTupletsCache = new OpenTupletsCache();
OpenVolta openVolta = new OpenVolta();
// add continued elements
for (ContinuedElement ce : unclosedElements) {
if (ce instanceof ContinuedSlur) {
openCurvedLinesCache.add(SlurCache.createContinued((ContinuedSlur) ce));
} else if (ce instanceof ContinuedVolta) {
openVolta.volta = (ContinuedVolta) ce;
} else if (ce instanceof ContinuedWedge) {
openWedges.wedges.add((ContinuedWedge) ce);
}
}
// create staff stampings
StaffStampings staffStampings = staffStamper.createStaffStampings(score, frame);
staffStampings.addAllTo(staffStampsPool);
context.staffStampings = staffStampings;
// go through the systems
for (int iSystem : range(frame.getSystems())) {
context.systemIndex = iSystem;
SystemSpacing system = frame.getSystems().get(iSystem);
List<StaffStamping> systemStaves = staffStampings.getAllOfSystem(iSystem);
StaffStamping systemFirstStaff = getFirst(systemStaves);
// add the part names (first system) or part abbreviations (other systems)
int iStaffInPart = 0;
for (Part part : stavesList.getParts()) {
PartNameStamper.Style style = (frameIndex == 0 && iSystem == 0 ? PartNameStamper.Style.Full : PartNameStamper.Style.Abbreviated);
addNotNull(otherStampsPool, partNameStamper.stamp(part, iStaffInPart, systemStaves, style));
iStaffInPart += part.getStavesCount();
}
// create the brackets at the beginning of the system
for (BracketGroup bracketGroup : stavesList.getBracketGroups()) {
StavesRange r = bracketGroup.getStaves();
otherStampsPool.add(new BracketStamping(systemStaves.get(r.getStart()), systemStaves.get(r.getStop()), system.getMarginLeftMm() - 1.4f, bracketGroup.getStyle()));
}
// create the barlines and measure numbers
otherStampsPool.addAll(barlinesStamper.stamp(system, systemStaves, score));
// fill the staves
for (int iStaff : range(stavesCount)) {
layouterContext.mp = layouterContext.mp.withStaff(iStaff);
context.staffIndex = iStaff;
float xMm = context.getCurrentStaffStamping().positionMm.x;
for (int iMeasure : range(system.columns)) {
int globalMeasureIndex = system.getStartMeasure() + iMeasure;
layouterContext.mp = layouterContext.mp.withMeasure(globalMeasureIndex);
context.measureIndex = globalMeasureIndex;
ColumnSpacing measureColumnSpacing = system.columns.get(iMeasure);
MeasureSpacing measure = measureColumnSpacing.getMeasures().get(iStaff);
// add leading spacing elements, if available
otherStampsPool.addAll(measureStamper.stampLeading(measure, xMm, context));
// add directions
otherStampsPool.addAll(directionStamper.stamp(context));
// add measure elements within this measure
float voicesXMm = xMm + measureColumnSpacing.getLeadingWidthMm();
otherStampsPool.addAll(measureStamper.stampMeasure(measure, voicesXMm, context));
// add voice elements within this measure
otherStampsPool.addAll(voiceStamper.stampVoices(measure, voicesXMm, staffStampings, context, defaultLyricStyle, beamsSpacing, openCurvedLinesCache, openLyricsCache, lastLyrics, openTupletsCache));
xMm += measureColumnSpacing.getWidthMm();
}
}
// create all voltas in this system, including open voltas from the last system
otherStampsPool.addAll(voltaStamper.stampSystem(systemFirstStaff, openVolta, header, defaultLyricStyle));
// create all wedges in this system
otherStampsPool.addAll(wedgeStamper.stampSystem(system, score, staffStampings, openWedges));
}
// create the collected ties and slurs
otherStampsPool.addAll(createTiesAndSlurs(openCurvedLinesCache, staffStampings, frame.getSystems().size()));
// create the open lyric underscore lines
for (Tuple3<StaffTextStamping, NoteheadStamping, Integer> openUnderscore : openLyricsCache.getUnderscores()) {
// TODO: fetch style efficiently
FormattedTextStyle style = defaultLyricStyle;
FormattedTextElement firstElement = openUnderscore.get1().getText().getFirstParagraph().getElements().getFirst();
if (firstElement instanceof FormattedTextString) {
style = ((FormattedTextString) firstElement).getStyle();
}
otherStampsPool.addAll(lyricStamper.createUnderscoreStampings(openUnderscore.get1(), openUnderscore.get2(), style, staffStampings.getAllOfStaff(openUnderscore.get3())));
}
// create tuplet brackets/numbers
for (Tuplet tuplet : openTupletsCache) {
otherStampsPool.add(tupletStamper.createTupletStamping(tuplet, openTupletsCache, symbols));
}
// collect elements that have to be continued on the next frame
ArrayList<ContinuedElement> continuedElements = alist();
for (SlurCache clc : openCurvedLinesCache) {
continuedElements.add(clc.getContinuedCurvedLine());
}
if (openVolta.volta != null)
continuedElements.add(openVolta.volta);
continuedElements.addAll(openWedges.wedges);
layouterContext.restoreMp();
return new ScoreFrameLayout(frame, staffStampsPool, otherStampsPool, continuedElements);
}
use of com.xenoage.zong.core.text.FormattedTextElement in project Zong by Xenoage.
the class FormattedTextConverter method toStyledDocument.
/**
* Creates a {@link StyledDocument} from the given {@link FormattedText}.
*/
public static StyledDocument toStyledDocument(FormattedText input) {
StyledDocument styledDoc = new DefaultStyledDocument();
// handle all paragraphs
SimpleAttributeSet lastStyle = new SimpleAttributeSet();
int line = 0;
for (FormattedTextParagraph p : input.getParagraphs()) {
// apply the alignment of the paragraph (apply style to the first letter of the paragraph)
SimpleAttributeSet paragraphStyle = new SimpleAttributeSet();
applyAlignmentToAttributeSet(p.getAlignment(), paragraphStyle);
styledDoc.setParagraphAttributes(styledDoc.getLength(), 1, paragraphStyle, true);
// handle all elements within this paragraph
for (// TODO: Symbols ?!
FormattedTextElement t : // TODO: Symbols ?!
p.getElements()) {
if (t instanceof FormattedTextString) {
FormattedTextString s = (FormattedTextString) t;
try {
lastStyle = getAttributeSetFromStyle(s.getStyle());
styledDoc.insertString(styledDoc.getLength(), s.getText(), lastStyle);
} catch (BadLocationException e) {
}
}
}
// after the paragraph (not after the last): insert a line break
if (line < input.getParagraphs().size() - 1) {
try {
styledDoc.insertString(styledDoc.getLength(), "\n", lastStyle);
} catch (BadLocationException e) {
}
}
line++;
}
return styledDoc;
}
use of com.xenoage.zong.core.text.FormattedTextElement 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());
}
Aggregations