Skip to content

Commit d574b07

Browse files
authored
Doc: adding examples of bezier curves chaining (#1210)
1 parent ddc72ff commit d574b07

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+74
-17
lines changed

docs/Shapes.md

+11-2

docs/bezier-chaining.png

42.2 KB

docs/table_with_gutter.jpg

-31 Bytes
-77 Bytes

fpdf/drawing.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,10 @@ def rgb8(r, g, b, a=None):
364364
Raises:
365365
ValueError: if any components are not in their valid interval.
366366
"""
367-
if a is not None:
367+
if a is None:
368+
if r == g == b:
369+
return DeviceGray(r / 255.0)
370+
else:
368371
a /= 255.0
369372

370373
return DeviceRGB(r / 255.0, g / 255.0, b / 255.0, a)

fpdf/fpdf.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -1857,13 +1857,9 @@ def bezier(self, point_list, closed=False, style=None):
18571857
if points not in (3, 4):
18581858
raise ValueError(
18591859
"point_list should contain 3 tuples for a quadratic curve"
1860-
"or 4 tuples for a cubic curve."
1860+
" or 4 tuples for a cubic curve."
18611861
)
1862-
1863-
if style is None:
1864-
style = RenderStyle.DF
1865-
else:
1866-
style = RenderStyle.coerce(style)
1862+
style = RenderStyle.coerce(style)
18671863

18681864
# QuadraticBezierCurve and BezierCurve make use of `initial_point` when instantiated.
18691865
# If we want to define all 3 (quad.) or 4 (cubic) points, we can set `initial_point`

test/image/image_x_align_center.pdf

-3 Bytes
Binary file not shown.

test/image/image_x_align_right.pdf

-3 Bytes
Binary file not shown.

test/image/svg_image.pdf

-4 Bytes
Binary file not shown.
-4 Bytes
Binary file not shown.
-10 Bytes
Binary file not shown.

test/image/svg_image_fit_rect.pdf

-4 Bytes
Binary file not shown.
-4 Bytes
Binary file not shown.
-4 Bytes
Binary file not shown.
-4 Bytes
Binary file not shown.
-4 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.

test/shapes/bezier_chaining.pdf

2.56 KB
Binary file not shown.

test/shapes/test_bezier.py

+57-8
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ def test_quadratic_beziers(tmp_path):
2424
pl5 = [[20, 230], (40, 280), (60, 250)]
2525

2626
pdf.set_fill_color(r=255, g=0, b=0)
27-
pdf.bezier(pl1)
27+
pdf.bezier(pl1, style="DF")
2828
pdf.set_fill_color(r=0, g=255, b=0)
29-
pdf.bezier(pl2)
29+
pdf.bezier(pl2, style="DF")
3030
pdf.set_fill_color(r=0, g=0, b=255)
31-
pdf.bezier(pl3, closed=True)
31+
pdf.bezier(pl3, style="DF", closed=True)
3232
pdf.bezier(pl4, style="F")
3333
pdf.bezier(pl5, style="D")
3434

@@ -48,11 +48,11 @@ def test_cubic_beziers(tmp_path):
4848
pl5 = [[20, 80], (40, 90), (60, 80)]
4949

5050
pdf.set_fill_color(r=255, g=0, b=0)
51-
pdf.bezier(pl1)
51+
pdf.bezier(pl1, style="DF")
5252
pdf.set_fill_color(r=0, g=255, b=0)
53-
pdf.bezier(pl2)
53+
pdf.bezier(pl2, style="DF")
5454
pdf.set_fill_color(r=0, g=0, b=255)
55-
pdf.bezier(pl3, closed=True)
55+
pdf.bezier(pl3, style="DF", closed=True)
5656
pdf.bezier(pl4, style="F")
5757
pdf.bezier(pl5, style="D")
5858

@@ -70,12 +70,12 @@ def test_bezier_line_settings(tmp_path):
7070

7171
pdf.set_fill_color(r=255, g=0, b=0)
7272
pdf.set_dash_pattern(dash=2, gap=3)
73-
pdf.bezier(pl1)
73+
pdf.bezier(pl1, style="DF")
7474

7575
pdf.set_fill_color(r=0, g=255, b=0)
7676
pdf.set_dash_pattern(dash=4, gap=6)
7777
pdf.set_line_width(2)
78-
pdf.bezier(pl2)
78+
pdf.bezier(pl2, style="DF")
7979

8080
# Reset for drawing points
8181
pdf.set_line_width(0.2)
@@ -84,3 +84,52 @@ def test_bezier_line_settings(tmp_path):
8484
draw_points(pdf, [pl1, pl2])
8585

8686
assert_pdf_equal(pdf, HERE / "bezier_curve_line_settings.pdf", tmp_path)
87+
88+
89+
def test_bezier_chaining(tmp_path):
90+
pdf = fpdf.FPDF()
91+
pdf.add_page()
92+
pdf.set_font("Helvetica", size=12)
93+
94+
pdf.x, pdf.y = 20, pdf.h / 4 - 30
95+
pdf.cell(text="Chaining cubic curves:")
96+
for i in range(4):
97+
shift = 20 if i % 2 else -20
98+
x1, y1 = (3 * i + 1) * pdf.w / 14, pdf.h / 4
99+
x2, y2 = (3 * i + 2) * pdf.w / 14, pdf.h / 4 + shift
100+
x3, y3 = (3 * i + 3) * pdf.w / 14, pdf.h / 4 + shift
101+
x4, y4 = (3 * i + 4) * pdf.w / 14, pdf.h / 4
102+
pdf.set_draw_color(
103+
{
104+
0: "#000",
105+
1: "#f00",
106+
2: "#0f0",
107+
3: "#00f",
108+
}[i % 4]
109+
)
110+
pdf.circle(x=x1 - 0.5, y=y1 - 0.5, r=1)
111+
pdf.circle(x=x2 - 0.5, y=y2 - 0.5, r=1)
112+
pdf.circle(x=x3 - 0.5, y=y3 - 0.5, r=1)
113+
pdf.circle(x=x4 - 0.5, y=y4 - 0.5, r=1)
114+
pdf.bezier(((x1, y1), (x2, y2), (x3, y3), (x4, y4)))
115+
116+
pdf.x, pdf.y = 20, pdf.h / 2 - 30
117+
pdf.cell(text="Chaining quadratic curves:")
118+
for i in range(4):
119+
shift = 20 if i % 2 else -20
120+
x1, y1 = (2 * i + 1) * pdf.w / 10, pdf.h / 2
121+
x2, y2 = (2 * i + 2) * pdf.w / 10, pdf.h / 2 + shift
122+
x3, y3 = (2 * i + 3) * pdf.w / 10, pdf.h / 2
123+
pdf.set_draw_color(
124+
{
125+
0: "#f00",
126+
1: "#0f0",
127+
2: "#00f",
128+
}[i % 3]
129+
)
130+
pdf.circle(x=x1 - 0.5, y=y1 - 0.5, r=1)
131+
pdf.circle(x=x2 - 0.5, y=y2 - 0.5, r=1)
132+
pdf.circle(x=x3 - 0.5, y=y3 - 0.5, r=1)
133+
pdf.bezier(((x1, y1), (x2, y2), (x3, y3)))
134+
135+
assert_pdf_equal(pdf, HERE / "bezier_chaining.pdf", tmp_path)
-746 Bytes
Binary file not shown.
-193 Bytes
Binary file not shown.

test/svg/generated_pdf/SVG_logo.pdf

-5 Bytes
Binary file not shown.
Binary file not shown.

test/svg/generated_pdf/arcs02.pdf

0 Bytes
Binary file not shown.

test/svg/generated_pdf/cubic01.pdf

-9 Bytes
Binary file not shown.

test/svg/generated_pdf/cubic02.pdf

-29 Bytes
Binary file not shown.

test/svg/generated_pdf/issue-1076.pdf

-7 Bytes
Binary file not shown.
-5 Bytes
Binary file not shown.

test/svg/generated_pdf/quad01.pdf

-6 Bytes
Binary file not shown.
-1 Bytes
Binary file not shown.

test/svg/generated_pdf/search.pdf

-3 Bytes
Binary file not shown.
-1 Bytes
Binary file not shown.
-2 Bytes
Binary file not shown.
1 Byte
Binary file not shown.
1 Byte
Binary file not shown.
3 Bytes
Binary file not shown.
3 Bytes
Binary file not shown.
-4 Bytes
Binary file not shown.

test/text_region/tcols_3cols.pdf

-5 Bytes
Binary file not shown.

test/text_region/tcols_images.pdf

-17 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)