use of com.xenoage.utils.math.geom.Size2f 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.utils.math.geom.Size2f in project Zong by Xenoage.
the class AndroidLayoutRenderer method paint.
/**
* Returns a {@link Bitmap} with the given page of the given {@link Layout}
* which is rendered at the given zoom level.
*/
public Bitmap paint(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);
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
// TODO: necessary? and really 8888 bitmap?
canvas.drawARGB(255, 255, 255, 255);
canvas.scale(zoom, zoom);
paint(layout, pageIndex, new AndroidCanvas(canvas, pageSize, CanvasFormat.Raster, CanvasDecoration.None, CanvasIntegrity.Perfect), zoom);
return bitmap;
}
use of com.xenoage.utils.math.geom.Size2f in project Zong by Xenoage.
the class FramesSpacer method compute.
public FramesSpacing compute(List<ColumnSpacing> columns, Target target, Context context, Notations notations) {
context.saveMp();
ArrayList<FrameSpacing> frames = alist();
int measuresCount = context.score.getMeasuresCount();
int iMeasure = 0;
int iSystem = 0;
int iFrame = 0;
boolean additionalFrameIteration;
while (true) {
additionalFrameIteration = false;
// find score frame
Size2f frameSize;
if (iFrame < target.areas.size()) {
// there is another existing score frame
frameSize = target.getArea(iFrame).sizeMm;
} else {
// there is no another existing score frame
if (false == target.isCompleteLayout) {
// we are not interested in the stuff after the last score frame. exit.
break;
} else if (iMeasure >= measuresCount) {
// all measures layouted. exit.
break;
} else {
// still material to layout and additional frames requested. create one.
frameSize = target.additionalArea.sizeMm;
additionalFrameIteration = true;
}
}
// some material left to layout?
if (iMeasure < measuresCount) {
// more measures to layout
context.mp = atMeasure(iMeasure);
FrameSpacing frame = frameSpacer.compute(context, iSystem, frameSize, additionalFrameIteration, columns, notations);
if (frame.getSystems().size() > 0) {
// at least one measure in this frame. remember frame
frames.add(frame);
iMeasure = frame.getEndMeasure() + 1;
iSystem += frame.getSystems().size();
} else {
// do not create endless list of empty additional frames
if (!additionalFrameIteration) {
frames.add(FrameSpacing.empty(frameSize));
} else {
break;
}
}
} else {
// no material left. empty frame.
frames.add(FrameSpacing.empty(frameSize));
}
// next frame
iFrame++;
}
context.restoreMp();
return new FramesSpacing(frames);
}
use of com.xenoage.utils.math.geom.Size2f in project Zong by Xenoage.
the class PdfPrinter method print.
/**
* Prints the given {@link Layout} into the given PDF output stream.
*/
public static void print(Layout layout, OutputStream out) {
Document document = new Document();
PdfWriter writer = null;
try {
writer = PdfWriter.getInstance(document, out);
} catch (Exception e) {
handle(warning(Voc.ErrorWhilePrinting));
}
document.open();
PdfContentByte cb = writer.getDirectContent();
It<Page> pages = it(layout.getPages());
for (Page page : pages) {
// create PDF page
Size2f pageSize = page.getFormat().getSize();
float width = Units.mmToPx(pageSize.width, 1);
float height = Units.mmToPx(pageSize.height, 1);
document.newPage();
PdfTemplate tp = cb.createTemplate(width, height);
// fill PDF page
Graphics2D g2d = new PdfGraphics2D(cb, width, height);
INSTANCE.log(Companion.remark("Printing page " + pages.getIndex() + "..."));
LayoutRenderer.paintToCanvas(layout, pages.getIndex(), new AwtCanvas(g2d, pageSize, CanvasFormat.Vector, CanvasDecoration.None, CanvasIntegrity.Perfect));
// finish page
g2d.dispose();
cb.addTemplate(tp, 0, 0);
}
document.close();
}
use of com.xenoage.utils.math.geom.Size2f in project Zong by Xenoage.
the class PrintProcess method print.
@Override
public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException {
// valid page number?
if (pageIndex < 0 || pageIndex >= layout.getPages().size()) {
return Printable.NO_SUCH_PAGE;
}
// print page
INSTANCE.log(Companion.remark("Printing page " + pageIndex + "..."));
Graphics2D g2d = (Graphics2D) g;
Size2f pageSize = layout.getPages().get(pageIndex).getFormat().getSize();
LayoutRenderer.paintToCanvas(layout, pageIndex, new AwtCanvas(g2d, pageSize, CanvasFormat.Vector, CanvasDecoration.None, CanvasIntegrity.Perfect));
return Printable.PAGE_EXISTS;
}
Aggregations