use of com.xenoage.utils.math.geom.Size2i in project Zong by Xenoage.
the class PageViewManagerTest method computeScreenPosition.
@Test
public void computeScreenPosition() {
Size2i viewSize = new Size2i(800, 600);
float viewScaling = 1;
Point2f viewScroll = new Point2f(0, 0);
ViewState viewState = new ViewState(viewSize, viewScaling, viewScroll);
// center point of the page must have screen coordinates 400/300
Point2i pos = view.computePositionPx(layoutPos(null, 0, p(50, 100)), viewState);
assertEquals(pos.x, 400);
assertEquals(pos.y, 300);
// set zoom to 200%, must still be 400/300
viewScaling = 2;
viewState = new ViewState(viewSize, viewScaling, viewScroll);
pos = view.computePositionPx(layoutPos(null, 0, p(50, 100)), viewState);
assertEquals(pos.x, 400);
assertEquals(pos.y, 300);
// scroll to -5/10
// page point 45/110 must be 400/300
viewScroll = new Point2f(-5, 10);
viewState = new ViewState(viewSize, viewScaling, viewScroll);
pos = view.computePositionPx(layoutPos(null, 0, p(45, 110)), viewState);
assertEquals(pos.x, 400);
assertEquals(pos.y, 300);
// page center point must be
// 400-(-5*MmToPx*zoom); 300-(10*MmToPx*zoom)
pos = view.computePositionPx(layoutPos(null, 0, p(50, 100)), viewState);
assertEquals(pos.x, Math.round(400 - Units.mmToPx(-5, viewScaling)));
assertEquals(pos.y, Math.round(300 - Units.mmToPx(10, viewScaling)));
}
use of com.xenoage.utils.math.geom.Size2i in project Zong by Xenoage.
the class PageViewManagerTest method computeLayoutPosition.
@Test
public void computeLayoutPosition() {
Size2i viewSize = new Size2i(800, 600);
float viewScaling = 1;
Point2f viewScroll = new Point2f(0, 0);
ViewState viewState = new ViewState(viewSize, viewScaling, viewScroll);
// default zoom and scroll: point 400/300 must
// have center position of the page (50/100)
Point2f pos = view.computeLP(new Point2i(400, 300), viewState).position;
assertEquals(pos.x, 50, Delta.DELTA_FLOAT);
assertEquals(pos.y, 100, Delta.DELTA_FLOAT);
// set zoom to 200%, must still be 50/100
viewScaling = 2;
viewState = new ViewState(viewSize, viewScaling, viewScroll);
pos = view.computeLP(new Point2i(400, 300), viewState).position;
assertEquals(pos.x, 50, Delta.DELTA_FLOAT);
assertEquals(pos.y, 100, Delta.DELTA_FLOAT);
// scroll to -5/10
// screen point 400/300 must be 45/110
viewScroll = new Point2f(-5, 10);
viewState = new ViewState(viewSize, viewScaling, viewScroll);
pos = view.computeLP(new Point2i(400, 300), viewState).position;
assertEquals(pos.x, 45, Delta.DELTA_FLOAT);
assertEquals(pos.y, 110, Delta.DELTA_FLOAT);
// screen point 0/0 must be
// 50+(-5-400*pxToMm/zoom);100+(10-300*pxToMm/zoom)
// compute relative to page 0, since coordinates are outside
pos = view.computeLP(new Point2i(0, 0), 0, viewState).position;
assertEquals(pos.x, 50 + -5 - Units.pxToMm(400, viewScaling), Delta.DELTA_FLOAT);
assertEquals(pos.y, 100 + 10 - Units.pxToMm(300, viewScaling), Delta.DELTA_FLOAT);
// when not computed relative to a given page, it must return null
assertNull(view.computeLP(new Point2i(0, 0), viewState));
// reset zoom/scroll, then screen point 800/300 must be on page 1
viewScaling = 1;
viewScroll = new Point2f(0, 0);
viewState = new ViewState(viewSize, viewScaling, viewScroll);
int pageIndex = view.computeLP(new Point2i(800, 300), viewState).pageIndex;
assertEquals(1, pageIndex);
}
use of com.xenoage.utils.math.geom.Size2i in project Zong by Xenoage.
the class GwtCanvasLayoutRenderer method paintToCanvas.
/**
* Paints the given page of the given {@link Layout} at the given zoom level
* into the given {@link CanvasElement}, which is resized to include the whole page.
* The size in pixel is also returned.
*/
public static Size2i paintToCanvas(Layout layout, int pageIndex, float zoom, com.google.gwt.canvas.client.Canvas canvas) {
Context2d context = canvas.getContext2d();
// compute size
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);
// resize canvas and coordinate space
canvas.setWidth(width + "px");
canvas.setHeight(height + "px");
// double resolution: smoother
int coordSpaceFactor = 2;
canvas.setCoordinateSpaceWidth(width * coordSpaceFactor);
canvas.setCoordinateSpaceHeight(height * coordSpaceFactor);
context.scale(coordSpaceFactor, coordSpaceFactor);
// white page
context.setFillStyle("white");
context.fillRect(0, 0, width, height);
// paint layout
LayoutRenderer.paintToCanvas(layout, pageIndex, zoom, origin, new GwtCanvas(context, CanvasFormat.Raster, CanvasDecoration.Interactive, CanvasIntegrity.Perfect));
return new Size2i(width, height);
}
use of com.xenoage.utils.math.geom.Size2i in project Zong by Xenoage.
the class WebApp method paintLayout.
private void paintLayout() {
if (scoreDoc != null) {
Size2i size = GwtCanvasLayoutRenderer.paintToCanvas(scoreDoc.getLayout(), 0, zoom, canvas);
// also resize the loading panel to the size of the score
if (loadingPanel != null) {
loadingPanel.getStyle().setWidth(size.width, PX);
loadingPanel.getStyle().setHeight(size.height, PX);
}
}
}
Aggregations