def test_low_level_api(): html = TestHTML(string="<body>") css = CSS( string=""" @page { margin: 2px; size: 8px; background: #fff } html { background: #00f; } body { background: #f00; width: 1px; height: 1px } """ ) pdf_bytes = html.write_pdf(stylesheets=[css]) assert pdf_bytes.startswith(b"%PDF") assert html.render([css]).write_pdf() == pdf_bytes png_bytes = html.write_png(stylesheets=[css]) document = html.render([css], enable_hinting=True) page, = document.pages assert page.width == 8 assert page.height == 8 assert document.write_png() == (png_bytes, 8, 8) assert document.copy([page]).write_png() == (png_bytes, 8, 8) surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 8, 8) page.paint(cairo.Context(surface)) file_obj = io.BytesIO() surface.write_to_png(file_obj) check_png_pattern(file_obj.getvalue()) surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 8, 8) context = cairo.Context(surface) # Rotate at the center context.translate(4, 4) context.rotate(-math.pi / 2) context.translate(-4, -4) page.paint(context) file_obj = io.BytesIO() surface.write_to_png(file_obj) check_png_pattern(file_obj.getvalue(), rotated=True) document = html.render([css], enable_hinting=True) page, = document.pages assert (page.width, page.height) == (8, 8) png_bytes, width, height = document.write_png(resolution=192) assert (width, height) == (16, 16) check_png_pattern(png_bytes, x2=True) def png_size(result): png_bytes, width, height = result surface = cairo.ImageSurface.create_from_png(io.BytesIO(png_bytes)) assert (surface.get_width(), surface.get_height()) == (width, height) return width, height document = html.render([css], enable_hinting=True) page, = document.pages assert (page.width, page.height) == (8, 8) # A resolution that is not multiple of 96: assert png_size(document.write_png(resolution=145.2)) == (13, 13) document = TestHTML( string=""" <style> @page:first { size: 5px 10px } @page { size: 6px 4px } p { page-break-before: always } </style> <p></p> <p></p> """ ).render() page_1, page_2 = document.pages assert (page_1.width, page_1.height) == (5, 10) assert (page_2.width, page_2.height) == (6, 4) result = document.write_png() # (Max of both widths, Sum of both heights) assert png_size(result) == (6, 14) assert document.copy([page_1, page_2]).write_png() == result assert png_size(document.copy([page_1]).write_png()) == (5, 10) assert png_size(document.copy([page_2]).write_png()) == (6, 4)
def test_low_level_api(): html = TestHTML(string='<body>') css = CSS(string=''' @page { margin: 2px; size: 8px; background: #fff } html { background: #00f; } body { background: #f00; width: 1px; height: 1px } ''') pdf_bytes = html.write_pdf(stylesheets=[css]) assert pdf_bytes.startswith(b'%PDF') assert html.render([css]).write_pdf() == pdf_bytes png_bytes = html.write_png(stylesheets=[css]) document = html.render([css], enable_hinting=True) page, = document.pages assert page.width == 8 assert page.height == 8 assert document.write_png() == (png_bytes, 8, 8) assert document.copy([page]).write_png() == (png_bytes, 8, 8) surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 8, 8) page.paint(cairo.Context(surface)) file_obj = io.BytesIO() surface.write_to_png(file_obj) check_png_pattern(file_obj.getvalue()) surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 8, 8) context = cairo.Context(surface) # Rotate at the center context.translate(4, 4) context.rotate(-math.pi / 2) context.translate(-4, -4) page.paint(context) file_obj = io.BytesIO() surface.write_to_png(file_obj) check_png_pattern(file_obj.getvalue(), rotated=True) document = html.render([css], enable_hinting=True) page, = document.pages assert (page.width, page.height) == (8, 8) png_bytes, width, height = document.write_png(resolution=192) assert (width, height) == (16, 16) check_png_pattern(png_bytes, x2=True) def png_size(result): png_bytes, width, height = result surface = cairo.ImageSurface.create_from_png(io.BytesIO(png_bytes)) assert (surface.get_width(), surface.get_height()) == (width, height) return width, height document = html.render([css], enable_hinting=True) page, = document.pages assert (page.width, page.height) == (8, 8) # A resolution that is not multiple of 96: assert png_size(document.write_png(resolution=145.2)) == (13, 13) document = TestHTML(string=''' <style> @page:first { size: 5px 10px } @page { size: 6px 4px } p { page-break-before: always } </style> <p></p> <p></p> ''').render() page_1, page_2 = document.pages assert (page_1.width, page_1.height) == (5, 10) assert (page_2.width, page_2.height) == (6, 4) result = document.write_png() # (Max of both widths, Sum of both heights) assert png_size(result) == (6, 14) assert document.copy([page_1, page_2]).write_png() == result assert png_size(document.copy([page_1]).write_png()) == (5, 10) assert png_size(document.copy([page_2]).write_png()) == (6, 4)