File size: 2,042 Bytes
b516e00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import pytest
from naramotion import (
    Image, Context, Format, Rect, Circle, Ellipse, RoundRect,
    Triangle, Line, Box, CompOp, FillRule, StrokeCap, StrokeJoin,
    LinearGradient
)


def test_context_lifecycle():
    img = Image(300, 300, Format.PRGB32)
    ctx = Context(img)
    ctx.fill_all("white")
    ctx.end()


def test_context_shapes_rendering():
    img = Image(500, 500, Format.PRGB32)
    with Context(img) as ctx:
        ctx.fill_all("#1A1A1A")

        # Rect
        ctx.fill_rect(20, 20, 80, 80, "red")
        ctx.stroke_width = 3.0
        ctx.stroke_rect(20, 20, 80, 80, "yellow")

        # Circle
        ctx.fill_circle(200, 60, 40, "blue")
        ctx.stroke_circle(200, 60, 40, "cyan")

        # Ellipse
        ctx.fill_ellipse(350, 60, 50, 30, "green")
        ctx.stroke_ellipse(350, 60, 50, 30, "lime")

        # RoundRect
        ctx.fill_round_rect(20, 150, 100, 60, 15, "magenta")
        ctx.stroke_round_rect(20, 150, 100, 60, 15, "white")

        # Triangle
        ctx.fill_triangle(180, 210, 240, 150, 300, 210, "orange")

        # Line
        ctx.stroke_width = 5.0
        ctx.stroke_line(20, 250, 480, 250, "purple")

        # Arcs and Pies
        ctx.fill_pie(100, 350, 60, 60, 0, 1.57, "gold")
        ctx.stroke_arc(250, 350, 60, 60, 0, 3.14, "white")


def test_context_state_stack():
    img = Image(200, 200, Format.PRGB32)
    with Context(img) as ctx:
        ctx.global_alpha = 0.5
        ctx.stroke_width = 10.0

        with ctx.push_state():
            ctx.global_alpha = 1.0
            ctx.stroke_width = 2.0
            assert ctx.global_alpha == 1.0
            assert ctx.stroke_width == 2.0

        assert abs(ctx.global_alpha - 0.5) < 1e-4
        assert abs(ctx.stroke_width - 10.0) < 1e-4


def test_context_clipping():
    img = Image(200, 200, Format.PRGB32)
    with Context(img) as ctx:
        ctx.clip_to_rect(50, 50, 100, 100)
        ctx.fill_all("red")
        ctx.restore_clipping()