def test_identity(self): assert CanvasUnit.from_pt(1).pt == 1 assert CanvasUnit.from_in(1).inches == 1 assert CanvasUnit.from_cm(1).cm == 1 assert CanvasUnit.from_mm(1).mm == 1 assert CanvasUnit.from_px(1).px == 1 assert CanvasUnit.from_pango(1).pango == 1 assert CanvasUnit.from_pt(2).pt == 2 assert CanvasUnit.from_in(2).inches == 2 assert CanvasUnit.from_cm(2).cm == 2 assert CanvasUnit.from_mm(2).mm == 2 assert CanvasUnit.from_px(2).px == 2 assert CanvasUnit.from_pango(2).pango == 2
def test_unit_scale(self): surface_types = ['png', 'svg', 'pdf'] for surface_type in surface_types: path = Path(__file__).parent\ .joinpath('output/canvas_builder_unit_scale.%s' % surface_type) path.unlink(missing_ok=True) canvas_builder = CanvasBuilder() canvas_builder.set_path(path) canvas_builder.set_size(CanvasUnit.from_in(1), CanvasUnit.from_in(1)) canvas = canvas_builder.build() UnitScale().draw(canvas) canvas.close() assert path.exists()
def close(self): # Special edge case for ImageSurfaces if isinstance(self.surface, cairo.ImageSurface): self.surface.write_to_png(self.path_as_posix) self.surface.finish() # Cairo sets the dots-per-image as 72 pixels-per-inch, when it should # be 96. We also want to take into account the scale, since the number # of inches should not change. We use Pillow to adjust the DPI. if isinstance(self.surface, cairo.ImageSurface): image = Image.open(self.path_as_posix) dpi = CanvasUnit.from_in(1).px * self.scale image.save(self.path_as_posix, dpi=(dpi, dpi))
def test_comparisons(self): assert CanvasUnit.from_in(1).pt > CanvasUnit.from_cm(1).pt assert CanvasUnit.from_cm(1).pt > CanvasUnit.from_mm(1).pt assert CanvasUnit.from_mm(1).pt > CanvasUnit.from_pt(1).pt assert CanvasUnit.from_pt(1).pt > CanvasUnit.from_px(1).pt assert CanvasUnit.from_px(1).pt > CanvasUnit.from_pango(1).pt
def from_in(cls, x: float, y: float) -> 'CanvasCoordinate': return CanvasCoordinate(CanvasUnit.from_in(x), CanvasUnit.from_in(y))
def test_init(self): bbox = CanvasBbox(Cc.from_in(1, 2), Cu.from_in(3), Cu.from_in(4)) assert bbox.pos == Cc.from_in(1, 2) assert bbox.width == Cu.from_in(3) assert bbox.height == Cu.from_in(4)