forked from py-pdf/fpdf2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FPDF.bezier() method exposing PaintedPath.curve_to (py-pdf#1174)
* Began implementation of bezier method. * Focus on cubic Bezier curves for now, added to doc page on shapes, and added one test. * Image for shapes doc page * Remove code paste comment from writing Bezier test * FPDF.bezier(): Remove unused variable, add extra space following * Mention new Cubic bezier method in CHANGELOG.md * Make Bezier curves as open paths by default * Quadratic or cubic curves with bezier(), Draw Bezier curves as open paths by default * Error handling in FPDF.bezier() and adding closed paths to tests * Accept style in .bezier(), convert it to PathPaintRule * Fix style errors reported by pylint * Fix accidentally calling boolean attribute as if it were function * Show styles functionality in Bezier curve testing * Test that line width and dash pattern are updated from FPDF object; Remove generate=True from Bezier tests; clean up point drawing function for bezier tests. * Update template PDFs for Bezier test. * Remove debug_stream param from fdpf.bezier(), Ran black * Do not mention removed parameter in docstring for fpdf.bezier() * Fix merge conflict in and mention Bezier curves in CHANGELOG.md
- Loading branch information
Showing
8 changed files
with
169 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
from pathlib import Path | ||
|
||
import fpdf | ||
from test.conftest import assert_pdf_equal | ||
|
||
|
||
HERE = Path(__file__).resolve().parent | ||
|
||
|
||
def draw_points(pdf, point_lists): | ||
for pl in point_lists: | ||
for p in pl: | ||
pdf.circle(x=p[0], y=p[1], r=1, style="FD") | ||
|
||
|
||
def test_quadratic_beziers(tmp_path): | ||
pdf = fpdf.FPDF(unit="mm") | ||
pdf.add_page() | ||
|
||
pl1 = [(20, 40), (40, 20), (60, 40)] | ||
pl2 = [(20, 80), (50, 100), (60, 80)] | ||
pl3 = [(20, 120), (40, 110), (60, 120)] | ||
pl4 = [(20, 170), (40, 160), (60, 170)] | ||
pl5 = [[20, 230], (40, 280), (60, 250)] | ||
|
||
pdf.set_fill_color(r=255, g=0, b=0) | ||
pdf.bezier(pl1) | ||
pdf.set_fill_color(r=0, g=255, b=0) | ||
pdf.bezier(pl2) | ||
pdf.set_fill_color(r=0, g=0, b=255) | ||
pdf.bezier(pl3, closed=True) | ||
pdf.bezier(pl4, style="F") | ||
pdf.bezier(pl5, style="D") | ||
|
||
draw_points(pdf, [pl1, pl2, pl3, pl4, pl5]) | ||
|
||
assert_pdf_equal(pdf, HERE / "quadratic_bezier_curve.pdf", tmp_path) | ||
|
||
|
||
def test_cubic_beziers(tmp_path): | ||
pdf = fpdf.FPDF(unit="mm") | ||
pdf.add_page() | ||
|
||
pl1 = [(120, 40), (140, 30), (160, 49), (180, 50)] | ||
pl2 = [(120, 80), (150, 100), (160, 80), (180, 80)] | ||
pl3 = [(120, 120), (140, 130), (160, 140), (180, 120)] | ||
pl4 = [(20, 20), (40, 10), (60, 20)] | ||
pl5 = [[20, 80], (40, 90), (60, 80)] | ||
|
||
pdf.set_fill_color(r=255, g=0, b=0) | ||
pdf.bezier(pl1) | ||
pdf.set_fill_color(r=0, g=255, b=0) | ||
pdf.bezier(pl2) | ||
pdf.set_fill_color(r=0, g=0, b=255) | ||
pdf.bezier(pl3, closed=True) | ||
pdf.bezier(pl4, style="F") | ||
pdf.bezier(pl5, style="D") | ||
|
||
draw_points(pdf, [pl1, pl2, pl3, pl4, pl5]) | ||
|
||
assert_pdf_equal(pdf, HERE / "cubic_bezier_curve.pdf", tmp_path) | ||
|
||
|
||
def test_bezier_line_settings(tmp_path): | ||
pdf = fpdf.FPDF(unit="mm") | ||
pdf.add_page() | ||
|
||
pl1 = [(120, 40), (140, 30), (160, 49), (180, 50)] | ||
pl2 = [(20, 80), (50, 100), (60, 80)] | ||
|
||
pdf.set_fill_color(r=255, g=0, b=0) | ||
pdf.set_dash_pattern(dash=2, gap=3) | ||
pdf.bezier(pl1) | ||
|
||
pdf.set_fill_color(r=0, g=255, b=0) | ||
pdf.set_dash_pattern(dash=4, gap=6) | ||
pdf.set_line_width(2) | ||
pdf.bezier(pl2) | ||
|
||
# Reset for drawing points | ||
pdf.set_line_width(0.2) | ||
pdf.set_dash_pattern(0, 0, 0) | ||
|
||
draw_points(pdf, [pl1, pl2]) | ||
|
||
assert_pdf_equal(pdf, HERE / "bezier_curve_line_settings.pdf", tmp_path) |