Skip to content

Commit

Permalink
Move code writing pycairo images to a global function
Browse files Browse the repository at this point in the history
This will allow follow-up CLs to reuse this code for generating other
images using pycairo.

Bug: 364549423
Change-Id: I340bea1153a82145f06c859e44fc4828d9a38d8c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5891403
Reviewed-by: Andres Ricardo Perez <andresrperez@chromium.org>
Commit-Queue: Jean-Philippe Gravel <jpgravel@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1361438}
  • Loading branch information
graveljp authored and chromium-wpt-export-bot committed Sep 28, 2024
1 parent dbf4b1c commit 04fb624
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions html/canvas/tools/gentestutilsunion.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,20 @@ def _preprocess_code(jinja_env: jinja2.Environment, code: str,
return code


def _write_cairo_images(pycairo_code: str, output_files: _OutputPaths,
canvas_types: FrozenSet[_CanvasType]) -> None:
"""Creates a png from pycairo code, for the specified canvas types."""
if _CanvasType.HTML_CANVAS in canvas_types:
full_code = (f'{pycairo_code}\n'
f'surface.write_to_png("{output_files.element}")\n')
eval(compile(full_code, '<string>', 'exec'), {'cairo': cairo})

if {_CanvasType.OFFSCREEN_CANVAS, _CanvasType.WORKER} & canvas_types:
full_code = (f'{pycairo_code}\n'
f'surface.write_to_png("{output_files.offscreen}")\n')
eval(compile(full_code, '<string>', 'exec'), {'cairo': cairo})


class _Variant():

def __init__(self, params: _MutableTestParams) -> None:
Expand Down Expand Up @@ -474,7 +488,6 @@ def finalize_params(self, jinja_env: jinja2.Environment,
def generate_expected_image(self, output_dirs: _OutputPaths) -> None:
"""Creates a reference image using Cairo and save filename in params."""
expected = self.params['expected']
name = self.params['name']

if expected == 'green':
self._params['expected_img'] = '/images/green-100x50.png'
Expand All @@ -487,23 +500,10 @@ def generate_expected_image(self, output_dirs: _OutputPaths) -> None:
r'surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, \1, \2)'
r'\ncr = cairo.Context(surface)', expected)

output_paths = output_dirs.sub_path(name)
if _CanvasType.HTML_CANVAS in self.params['canvas_types']:
expected_canvas = (
f'{expected}\n'
f'surface.write_to_png("{output_paths.element}.png")\n')
eval(compile(expected_canvas, f'<test {name}>', 'exec'), {},
{'cairo': cairo})

if {_CanvasType.OFFSCREEN_CANVAS, _CanvasType.WORKER
} & self.params['canvas_types']:
expected_offscreen = (
f'{expected}\n'
f'surface.write_to_png("{output_paths.offscreen}.png")\n')
eval(compile(expected_offscreen, f'<test {name}>', 'exec'), {},
{'cairo': cairo})

self._params['expected_img'] = f'{name}.png'
img_filename = f'{self.params["name"]}.png'
_write_cairo_images(expected, output_dirs.sub_path(img_filename),
self.params['canvas_types'])
self._params['expected_img'] = img_filename


class _VariantGrid:
Expand Down

0 comments on commit 04fb624

Please sign in to comment.