use of com.xenoage.zong.layout.Page in project Zong by Xenoage.
the class FrameTest method getAbsolutePosition.
/**
* Tests the computation of the absolute position.
*/
@Test
public void getAbsolutePosition() {
double u = Math.PI / 180d;
Layout layout = new Layout(null);
Page page = new Page(PageFormat.Companion.getDefaultValue());
layout.addPage(page);
GroupFrame frame = createFrameWithChildren();
page.addFrame(frame);
// frame
Point2f p = frame.getAbsolutePosition();
assertEquals(50, p.x, Delta.DELTA_FLOAT);
assertEquals(75, p.y, Delta.DELTA_FLOAT);
// child frame
GroupFrame child = (GroupFrame) frame.children.get(0);
p = child.getAbsolutePosition();
float r = 30;
double childX = 50 + 20 * Math.cos(r * u) + 10 * Math.sin(r * u);
double childY = 75 - 20 * Math.sin(r * u) + 10 * Math.cos(r * u);
assertEquals(childX, p.x, Delta.DELTA_FLOAT_ROUGH);
assertEquals(childY, p.y, Delta.DELTA_FLOAT_ROUGH);
// child frame of child frame
Frame child2 = child.children.get(0);
p = child2.getAbsolutePosition();
r += 30;
assertEquals(childX + -15 * Math.cos(r * u) + -10 * Math.sin(r * u), p.x, Delta.DELTA_FLOAT_ROUGH);
assertEquals(childY - -15 * Math.sin(r * u) + -10 * Math.cos(r * u), p.y, Delta.DELTA_FLOAT_ROUGH);
}
use of com.xenoage.zong.layout.Page in project Zong by Xenoage.
the class CreditsReader method addTextFrame.
/**
* Adds the given credit element as a {@link TextFrame} to the given {@link Layout}.
*/
private static void addTextFrame(MxlCredit credit, Layout layout, ScoreFormat scoreFormat) {
if (credit.getContent().getCreditContentType() == MxlCreditContentType.CreditWords) {
MxlCreditWords mxlCreditWords = (MxlCreditWords) credit.getContent();
// create formatted text
FormattedText text = createFormattedText(mxlCreditWords, scoreFormat.getLyricFont());
// compute position (read only the position of the first credit-words element)
Page firstPage = layout.getPages().get(0);
float tenths = scoreFormat.getInterlineSpace() / 10;
MxlFormattedText mxlFirstCreditWords = mxlCreditWords.getItems().get(0);
MxlPosition mxlPosition = mxlFirstCreditWords.getPrintStyle().getPosition();
Point2f offsetFromBottomInTenths = mxlPosition.getDefault().or(new Point2f(10f, 10f));
Point2f position = new Point2f(offsetFromBottomInTenths.x * tenths, firstPage.getFormat().getSize().height - offsetFromBottomInTenths.y * tenths);
// compute size
// this is the width of the widest paragraph and the height of the sum of all paragraphs
// at least 1 mm
float maxParagraphWidth = 1;
// at least 1 mm
float sumParagraphsHeight = 1;
for (FormattedTextParagraph paragraph : text.getParagraphs()) {
maxParagraphWidth = Math.max(maxParagraphWidth, paragraph.getMetrics().getWidth());
sumParagraphsHeight += paragraph.getHeightMm();
}
Size2f size = new Size2f(maxParagraphWidth, sumParagraphsHeight);
// horizontal alignment:
// try with halign first, and if not set, use justify
Alignment alignment = readAlignment(mxlFirstCreditWords.getHAlign());
if (alignment == null) {
alignment = readAlignment(mxlFirstCreditWords.getJustify());
}
if (alignment == null || alignment == Alignment.Left) {
position = position.add(size.width / 2, 0);
} else if (alignment == Alignment.Center) {
// already centered
} else if (alignment == Alignment.Right) {
position = position.add(-size.width / 2, 0);
}
// vertical alignment
MxlVAlign mxlVAlign = mxlFirstCreditWords.getVAlign();
if (mxlVAlign == MxlVAlign.Top || mxlVAlign == MxlVAlign.Unknown) {
position = position.add(0, size.height / 2);
} else if (mxlVAlign == MxlVAlign.Middle) {
// already centered
} else if (mxlVAlign == MxlVAlign.Bottom || mxlVAlign == MxlVAlign.Baseline) {
position = position.add(0, -size.height / 2);
}
// create and add TextFrame
TextFrame textFrame = new TextFrame();
textFrame.setPosition(position);
textFrame.setSize(size);
textFrame.setText(text);
layout.getPages().get(0).addFrame(textFrame);
}
}
use of com.xenoage.zong.layout.Page in project Zong by Xenoage.
the class PageViewManagerTest method setUp.
@Before
public void setUp() {
Layout layout = new Layout(null);
// 3 pages:
// ---- --------
// |1 | -2------ |3 |
// | | -------- | |
// ---- --------
layout.addPage(new Page(new PageFormat(new Size2f(100, 200), new PageMargins(0, 0, 0, 0))));
layout.addPage(new Page(new PageFormat(new Size2f(200, 100), new PageMargins(0, 0, 0, 0))));
layout.addPage(new Page(new PageFormat(new Size2f(200, 200), new PageMargins(0, 0, 0, 0))));
// create view
view = new PageViewManager(layout);
view.setPageDisplayAlignment(PageDisplayAlignment.Horizontal);
}
use of com.xenoage.zong.layout.Page in project Zong by Xenoage.
the class PageViewManager method computePageRects.
/**
* Computes the offsets of all pages in mm, relative to the upper left
* corner of the first page.
*/
private ArrayList<Rectangle2f> computePageRects() {
// horizontal center of the current page
float offsetX = 0;
// vertical center of the current page
float offsetY = 0;
List<Page> pages = layout.getPages();
ArrayList<Rectangle2f> ret = new ArrayList<>(pages.size());
if (pages.size() > 0) {
// compute offsets for all pages
float firstPageOffsetX = -pages.get(0).getFormat().getSize().width / 2;
float firstPageOffsetY = -pages.get(0).getFormat().getSize().height / 2;
for (val page : pages) {
Size2f pageSize = page.getFormat().getSize();
Point2f offset = null;
switch(pageDisplayAlignment) {
case Horizontal:
offset = new Point2f(offsetX + firstPageOffsetX, -pageSize.height / 2);
offsetX += pageSize.width + pageDisplayDistance;
break;
case Vertical:
offset = new Point2f(-pageSize.width / 2, offsetY + firstPageOffsetY);
offsetY += pageSize.height + pageDisplayDistance;
break;
}
ret.add(new Rectangle2f(offset, new Size2f(pageSize)));
}
}
return ret;
}
use of com.xenoage.zong.layout.Page in project Zong by Xenoage.
the class JfxLayoutRenderer method paintToImage.
/**
* Returns a {@link WritableImage} with the given page of the given {@link Layout}
* which is rendered at the given zoom level.
*/
public static WritableImage paintToImage(Layout layout, int pageIndex, float zoom) {
Page page = layout.getPages().get(pageIndex);
Size2f pageSize = page.getFormat().getSize();
int width = Units.mmToPxInt(pageSize.width, zoom);
int height = Units.mmToPxInt(pageSize.height, zoom);
WritableImage wim = new WritableImage(width, height);
Canvas jfxCanvas = new Canvas(width, height);
GraphicsContext context = jfxCanvas.getGraphicsContext2D();
context.setFill(Color.WHITE);
context.fillRect(0, 0, width, height);
LayoutRenderer.paintToCanvas(layout, pageIndex, zoom, origin, new JfxCanvas(context, pageSize, CanvasFormat.Raster, CanvasDecoration.Interactive, CanvasIntegrity.Perfect));
jfxCanvas.snapshot(null, wim);
return wim;
}
Aggregations