Skip to content

Commit

Permalink
Enable AntiAliasing (#29)
Browse files Browse the repository at this point in the history
Rendering 10K characters with antialiasing takes ~750ms, as opposed to ~530ms without. Given that this is a rather extreme test for our purposes, we can afford to have antialiasing always enabled.
  • Loading branch information
lucach committed Jun 3, 2024
1 parent 4369ad7 commit cb78384
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pytamaro/graphic.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def __init__(self, path: Path, color: Color, pin_position: Point = None):
bounds = path.computeTightBounds()
pin_position = Point(bounds.width() / 2, bounds.height() / 2)
super().__init__(pin_position, path)
paint = Paint(color.skia_color)
paint = Paint(Color=color.skia_color, AntiAlias=True)
object.__setattr__(self, "paint", paint)

def draw(self, canvas: Canvas):
Expand Down
10 changes: 5 additions & 5 deletions tests/test_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
graphic_width, overlay, pin, rotate)
from pytamaro.point_names import bottom_left, bottom_right, top_left
from pytamaro.primitives import ellipse, rectangle, triangle
from tests.testing_utils import (HEIGHT, RADIUS, WIDTH, assert_equals_rendered,
assert_pin_tolerance, assert_repr,
assert_size, assert_size_tolerance,
assert_unique_color)
from tests.testing_utils import (HEIGHT, RADIUS, WIDTH, assert_color,
assert_equals_rendered, assert_pin_tolerance,
assert_repr, assert_size,
assert_size_tolerance, assert_unique_color)


def test_width():
Expand Down Expand Up @@ -34,7 +34,7 @@ def test_rotate_45():
c = ellipse(2 * RADIUS, 2 * RADIUS, red)
rot = rotate(45, c)
assert_size_tolerance(rot, (RADIUS * 2, RADIUS * 2))
assert_unique_color(rot, red)
assert_color(rot, red) # color might not be unique due to antialiasing


def test_rotate_pin_left_top():
Expand Down
12 changes: 6 additions & 6 deletions tests/test_primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
rotate)
from pytamaro.primitives import (circular_sector, ellipse, empty_graphic,
rectangle, text, triangle)
from tests.testing_utils import (HEIGHT, RADIUS, WIDTH,
from tests.testing_utils import (HEIGHT, RADIUS, WIDTH, assert_color,
assert_graphics_equals_tolerance, assert_repr,
assert_size, assert_unique_color,
assert_value_tolerance, pixels_colors)
Expand Down Expand Up @@ -40,7 +40,7 @@ def test_empty_graphic_repr():
def test_ellipse():
e = ellipse(WIDTH, HEIGHT, red)
assert_size(e, (WIDTH, HEIGHT))
assert_unique_color(e, red)
assert_color(e, red) # color might not be unique due to antialiasing


def test_ellipse_repr():
Expand All @@ -50,7 +50,7 @@ def test_ellipse_repr():
def test_text():
graphic = text("hello", "", 12, red)
assert graphic_width(graphic) > 0 and graphic_height(graphic) > 0
assert_unique_color(graphic, red)
assert_color(graphic, red) # color might not be unique due to antialiasing


def test_text_repr():
Expand Down Expand Up @@ -106,7 +106,7 @@ def test_circular_sector_repr():
def test_equilateral_triangle():
side = 100 # large enough
t = triangle(side, side, 60, red)
assert_unique_color(t, red)
assert_color(t, red) # color might not be unique due to antialiasing
# Assert that the number of red pixels is almost equal (2%)
# to the number of transparent pixels.
colors = Counter(pixels_colors(t))
Expand All @@ -126,8 +126,8 @@ def test_right_triangle_pinning_position():
# Most common expected to be transparent, then blue, then red.
colors = Counter(pixels_colors(t))
common = colors.most_common(3)
assert_value_tolerance(common[1][1], large_area - small_area)
assert_value_tolerance(common[2][1], small_area)
assert_value_tolerance(common[1][1], large_area - small_area, 0.05)
assert_value_tolerance(common[2][1], small_area, 0.05)


def test_triangle_repr():
Expand Down
6 changes: 5 additions & 1 deletion tests/testing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ def assert_unique_color(g: Graphic,
assert int(color.skia_color) in colors


def assert_color(g: Graphic, color: Color):
assert int(color.skia_color) in pixels_colors(g)


def assert_size(g: Graphic, expected_size: Tuple[int, int]):
assert_size_tolerance(g, expected_size, tolerance=0)


def assert_value_tolerance(actual_value: float, expected_value: float, tolerance: float = 0.02):
def assert_value_tolerance(actual_value: float, expected_value: float, tolerance: float):
assert expected_value * (1 - tolerance) <= actual_value <= expected_value * (1 + tolerance)


Expand Down

0 comments on commit cb78384

Please sign in to comment.