use of com.xenoage.zong.layout.Layout in project Zong by Xenoage.
the class ScoreDocScoreView method updateScreen.
@Override
public void updateScreen(Size2i screenSizePx, float zoom) {
this.screenSizePx = screenSizePx;
this.zoom = zoom;
// recompute the layout, so that each page fits into the available screen space
Size2f pageSizeMm = pxToMm(screenSizePx, zoom);
float marginMm = pxToMm(marginPx, zoom);
PageFormat pageFormat = new PageFormat(pageSizeMm, new PageMargins(marginMm, marginMm, marginMm, marginMm));
Size2f frameSizeMm = new Size2f(pageFormat.getUseableWidth(), pageFormat.getUseableHeight());
// first page needs space for title text
titleTextHeightPx = screenSizePx.height / 20f;
float firstFrameOffsetY = pxToMm(titleTextHeightPx, zoom);
Size2f firstFrameSizeMm = new Size2f(frameSizeMm.width, frameSizeMm.height - firstFrameOffsetY);
// delete unnecessary layout information, like system distances or system breaks
Score score = doc.getScore();
score.setFormat(new ScoreFormat());
ScoreHeader header = score.getHeader();
for (int i : range(header.getSystemLayouts())) {
SystemLayout sl = header.getSystemLayout(i);
if (sl != null)
sl.setDistance(SystemLayout.defaultDistance);
}
for (int i : range(header.getColumnHeaders())) {
ColumnHeader ch = header.getColumnHeader(i);
if (ch.getMeasureBreak() != null)
ch.setBreak(null);
}
// layout the score to find out the needed space
Context context = new Context(score, App.getSymbolPool(), doc.getLayout().getDefaults().getLayoutSettings());
Target target = new Target(alist(new ScoreLayoutArea(firstFrameSizeMm)), new ScoreLayoutArea(frameSizeMm), true);
ScoreLayouter layouter = new ScoreLayouter(context, target);
ScoreLayout scoreLayout = layouter.createScoreLayout();
// create and fill at least one page
Layout layout = new Layout(doc.getLayout().getDefaults());
ScoreFrameChain chain = null;
for (int i = 0; i < scoreLayout.frames.size(); i++) {
Page page = new Page(pageFormat);
Point2f position;
Size2f size;
if (i == 0) {
// first page
position = new Point2f(pageSizeMm.width / 2, pageSizeMm.height / 2 + firstFrameOffsetY);
size = firstFrameSizeMm;
} else {
// other pages
position = new Point2f(pageSizeMm.width / 2, pageSizeMm.height / 2);
size = frameSizeMm;
}
ScoreFrame frame = new ScoreFrame();
frame.setPosition(position);
frame.setSize(size);
page.addFrame(frame);
layout.addPage(page);
if (chain == null) {
chain = new ScoreFrameChain(score);
chain.setScoreLayout(scoreLayout);
}
chain.add(frame);
}
this.layout = layout;
}
use of com.xenoage.zong.layout.Layout in project Zong by Xenoage.
the class Frame method getCenterLP.
/**
* Gets the {@link LayoutPos} of the center of the frame.
* If this frame is not part of a layout, the page index -1 is returned.
*/
public final LayoutPos getCenterLP() {
Point2f pos = getAbsolutePosition();
Layout layout = getParentLayout();
Page page = getParentPage();
int pageIndex = (page != null ? page.getIndex() : -1);
return layoutPos(layout, pageIndex, pos);
}
use of com.xenoage.zong.layout.Layout 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.Layout 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.Layout in project Zong by Xenoage.
the class OpenAction method loadDocument.
/**
* Loads the {@link Doc} at the given URL and stores information about
* the score in the database, if it is not already present.
*/
public Tuple2<ScoreDoc, Doc> loadDocument(String url, @MaybeNull UUID publicID) throws SQLException {
Connection db = Webserver.instance.getDBConnection();
ScoreDoc scoreDoc;
// public ID of the document
if (publicID == null)
publicID = UUID.randomUUID();
// may not exist yet
PreparedStatement stmt = stmt(db, "SELECT public_id FROM docs WHERE public_id = ?", publicID);
ResultSet res = stmt.executeQuery();
boolean error = res.next();
stmt.close();
if (error)
throw new SQLException("A document with this public ID already exists");
// load MusicXML document
try {
// open local or remote file
InputStream inputStream;
if (URLUtils.isAbsoluteURL(url)) {
inputStream = new URL(url).openStream();
} else {
inputStream = new FileInputStream(Webserver.webPath + url);
}
MusicXmlScoreDocFileInput in = new MusicXmlScoreDocFileInput();
scoreDoc = in.read(new JseInputStream(inputStream), null);
} catch (FileNotFoundException ex) {
throw new RuntimeException("file not found");
} catch (MalformedURLException ex) {
throw new RuntimeException("invalid URL: " + url);
} catch (IOException ex) {
throw new RuntimeException("can not read from URL: " + url);
}
// register file in database, if not already known
Layout layout = scoreDoc.getLayout();
boolean isDocKnown = Database.exists(db, "docs", "url = ?", "" + url);
if (!isDocKnown) {
Database.insert(db, "docs", "url, public_id, pages, last_access", "" + url, "" + publicID, layout.getPages().size(), unixTime());
}
// read information about the document
Doc doc = Doc.fromDB(db, "" + url);
// for new documents: save information
if (!isDocKnown) {
// page information
for (int iPage : range(layout.getPages())) {
Size2f pageSize = layout.getPages().get(iPage).getFormat().getSize();
new Page(doc.id, iPage, pageSize.width, pageSize.height).insertIntoDB(db);
}
}
return t(scoreDoc, doc);
}
Aggregations