Skip to content

Commit

Permalink
Merge 12c92d1 into 3b442f1
Browse files Browse the repository at this point in the history
  • Loading branch information
RedShy authored May 9, 2022
2 parents 3b442f1 + 12c92d1 commit 3d7cf4d
Show file tree
Hide file tree
Showing 12 changed files with 2,798 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This can also be enabled programmatically with `warnings.simplefilter('default',
- allowing to provide an [`AnnotationName`](https://pyfpdf.github.io/fpdf2/fpdf/enums.html#fpdf.enums.AnnotationName)
and [`AnnotationFlags`](https://pyfpdf.github.io/fpdf2/fpdf/enums.html#fpdf.enums.AnnotationFlag)
onto [text_annotation()](https://pyfpdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.text_annotation)
- allowing correctly parsing of SVG files with CSS styling (`style="..."` attribute) thanks to @RedShy

## [2.5.4] - 2022-05-05
### Added
Expand Down
5 changes: 1 addition & 4 deletions docs/SVG.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ pdf.output("my_file.pdf")
- basic cross-references
- stroke & fill coloring and opacity
- basic stroke styling
- CSS styling of SVG elements

## Currently Unsupported Notable SVG Features ##

Expand All @@ -94,10 +95,6 @@ cause unexpected results (up to and including a normal-looking SVG rendering as
a completely blank PDF). It is very likely that off-the-shelf SVGs will not be
converted fully correctly without some preprocessing.

The biggest unsupported feature is probably:

- CSS styling of SVG elements (_cf._ [issue #404](https://github.com/PyFPDF/fpdf2/issues/404))

In addition to that:

- text/tspan/textPath
Expand Down
20 changes: 20 additions & 0 deletions fpdf/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,29 @@ def optional(value, converter=lambda noop: noop):
}


@force_nodocument
def parse_style(svg_element):
"""Parse `style="..."` making it's key-value pairs element's attributes"""
try:
style = svg_element.attrib["style"]
except KeyError:
pass
else:
for element in style.split(";"):
if not element:
continue

pair = element.split(":")
if len(pair) == 2 and pair[0] and pair[1]:
attr, value = pair

svg_element.attrib[attr.strip()] = value.strip()


@force_nodocument
def apply_styles(stylable, svg_element):
"""Apply the known styles from `svg_element` to the pdf path/group `stylable`."""
parse_style(svg_element)

stylable.style.auto_close = False

Expand Down
Binary file not shown.
Binary file added test/svg/generated_pdf/Ghostscript_escher.pdf
Binary file not shown.
Binary file added test/svg/generated_pdf/simple_rect.pdf
Binary file not shown.
2 changes: 2 additions & 0 deletions test/svg/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,8 @@ def Gs(**kwargs):
# discovered while investigatin issue 358:
pytest.param(svgfile("issue_358b.svg"), id="repeated relative move"),
pytest.param(svgfile("issue_358.svg"), id="arc start & initial point"), # issue 358
pytest.param(svgfile("Ghostscript_colorcircle.svg"), id="ghostscript colorcircle"),
pytest.param(svgfile("Ghostscript_escher.svg"), id="ghostscript escher"),
)

svg_path_edge_cases = (
Expand Down
1,283 changes: 1,283 additions & 0 deletions test/svg/svg_sources/Ghostscript_colorcircle.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,437 changes: 1,437 additions & 0 deletions test/svg/svg_sources/Ghostscript_escher.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions test/svg/svg_sources/simple_rect.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions test/svg/test_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,17 @@ def test_svg_conversion_no_transparency(self, tmp_path):
assert_pdf_equal(
pdf, GENERATED_PDF_DIR / "SVG_logo_notransparency.pdf", tmp_path
)

def test_svg_conversion_priority_styles(self, tmp_path):
svg_file = parameters.svgfile("simple_rect.svg")

svg = fpdf.svg.SVGObject.from_file(svg_file)

pdf = fpdf.FPDF(unit="pt", format=(svg.width, svg.height))
pdf.set_margin(0)
pdf.allow_images_transparency = False
pdf.add_page()

svg.draw_to_page(pdf)

assert_pdf_equal(pdf, GENERATED_PDF_DIR / f"{svg_file.stem}.pdf", tmp_path)
36 changes: 36 additions & 0 deletions test/test_transitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
CoverTransition,
UncoverTransition,
FadeTransition,
Transition,
)
from test.conftest import assert_pdf_equal
import pytest

HERE = Path(__file__).resolve().parent

Expand Down Expand Up @@ -47,3 +49,37 @@ def test_transitions(tmp_path):
pdf.add_page(transition=FadeTransition())
pdf.text(x=40, y=150, txt="Page 11")
assert_pdf_equal(pdf, HERE / "transitions.pdf", tmp_path)


def test_transition_errors():
pdf = FPDF()
pdf.set_font("Helvetica", size=120)
with pytest.raises(NotImplementedError):
Transition().dict_as_string()

with pytest.raises(ValueError):
pdf.add_page(transition=SplitTransition("A", "B"))

with pytest.raises(ValueError):
pdf.add_page(transition=BlindsTransition("A"))

with pytest.raises(ValueError):
pdf.add_page(transition=BoxTransition("A"))

with pytest.raises(ValueError):
pdf.add_page(transition=WipeTransition(-1))

with pytest.raises(ValueError):
pdf.add_page(transition=GlitterTransition(-1))

with pytest.raises(ValueError):
pdf.add_page(transition=FlyTransition("A", -1))

with pytest.raises(ValueError):
pdf.add_page(transition=PushTransition(-1))

with pytest.raises(ValueError):
pdf.add_page(transition=CoverTransition(-1))

with pytest.raises(ValueError):
pdf.add_page(transition=UncoverTransition(-1))

0 comments on commit 3d7cf4d

Please sign in to comment.