use of com.xenoage.zong.core.text.FormattedText in project Zong by Xenoage.
the class VoltaStamper method stampInStaff.
/**
* Creates a {@link VoltaStamping} for the given volta spanning
* from the given start measure to the given end measure on the given staff
* (global measure indices). Left and right hooks and the caption are optional.
* The measure indices must be within the staff.
*/
private VoltaStamping stampInStaff(Volta volta, int startMeasureIndex, int endMeasureIndex, StaffStamping staff, FormattedTextStyle textStyle, boolean drawCaption, boolean drawLeftHook, boolean drawRightHook) {
// get start and end x coordinate of measure
float x1 = staff.system.getMeasureStartMm(startMeasureIndex) + staff.is / 2;
float x2 = staff.system.getMeasureEndMm(endMeasureIndex) - staff.is / 2;
// line position of volta line: 5 IS over top line
float lp = (staff.linesCount - 1 + 5) * 2;
// caption
FormattedText caption = null;
if (drawCaption && volta.getCaption().length() > 0) {
caption = Companion.fText(volta.getCaption(), textStyle, Alignment.Left);
}
// create stamping
return new VoltaStamping(volta, lp, x1, x2, caption, drawLeftHook, drawRightHook, staff);
}
use of com.xenoage.zong.core.text.FormattedText in project Zong by Xenoage.
the class FormattedTextConverter method fromStyledDocument.
/**
* Creates a {@link FormattedText} from the given {@link StyledDocument}.
*/
public static FormattedText fromStyledDocument(StyledDocument styledDoc) {
CList<FormattedTextParagraph> paragraphs = clist();
int endPos = styledDoc.getLength();
AttributeSet attribute = null;
String textelement = "";
String letter = "";
CList<FormattedTextElement> elements = clist();
Alignment alignment = null;
// paragraph, when '\n' is found
for (int i = 0; i < endPos; i++) {
try {
letter = styledDoc.getText(i, 1);
} catch (BadLocationException e) {
}
// line break: create new paragraph
if (letter.equals("\n")) {
if (!textelement.equals("")) {
elements.add(new FormattedTextString(textelement, getStyleFromAttributeSet(attribute)));
textelement = "";
}
paragraphs.add(new FormattedTextParagraph(elements, alignment));
elements = clist();
alignment = null;
} else if (attribute != styledDoc.getCharacterElement(i).getAttributes()) {
// set alignment, if not already done
if (alignment == null) {
alignment = fromAttributeSet(styledDoc.getParagraphElement(i).getAttributes());
}
// style has changed, so save old element and create a new one
if (!textelement.equals("")) {
elements.add(new FormattedTextString(textelement, getStyleFromAttributeSet(attribute)));
}
attribute = styledDoc.getCharacterElement(i).getAttributes();
textelement = letter;
} else {
// style stayed the same, so just append
textelement += letter;
}
}
if (!textelement.equals("")) {
// save the last string
elements.add(new FormattedTextString(textelement, getStyleFromAttributeSet(attribute)));
}
if (elements.size() > 0) {
// add (non-empty) paragraph
paragraphs.add(new FormattedTextParagraph(elements, alignment));
}
return new FormattedText(paragraphs);
}
Aggregations