Skip to content

Commit

Permalink
Fixing continued working of Code39 legacy parameter names (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmischler authored Oct 3, 2021
1 parent ffdbc48 commit 8b6290b
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 19 deletions.
2 changes: 1 addition & 1 deletion docs/Templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ Dimensions (except font size, which always uses points) are given in user define
* '__B__': Box - draws a rectangle around the bounding box
* '__BC__': Barcode - inserts an "Interleaved 2 of 5" type barcode
* '__C39__': Code 39 - inserts a "Code 39" type barcode
* Incompatible change: The first implementation of this type used the non-standard template keys "x", "y", "w", and "h", which are no longer valid.
* Incompatible change: A previous implementation of this type used the non-standard element keys "x", "y", "w", and "h", which are now deprecated (but still work for the moment).
* '__W__': "Write" - uses the `FPDF.write()` method to add text to the page
* _mandatory_
* __x1, y1, x2, y2__: top-left, bottom-right coordinates, defining a bounding box in most cases
Expand Down
32 changes: 19 additions & 13 deletions fpdf/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ def load_elements(self, elements):
"y2": (int, float),
"font": (str, type(None)),
"size": (int, float),
"bold": int,
"italic": int,
"underline": int,
"bold": object, # "bool or equivalent"
"italic": object,
"underline": object,
"foreground": int,
"background": int,
"align": (str, type(None)),
Expand All @@ -99,13 +99,29 @@ def load_elements(self, elements):
e["priority"] = 0
for k in ("name", "type", "x1", "y1", "y2"):
if k not in e:
if e["type"] == "C39":
# lots of legacy special casing.
# We need to do that here, so that rotation and scaling
# still work.
if k == "x1" and "x" in e:
e["x1"] = e["x"]
continue
if k == "y1" and "y" in e:
e["y1"] = e["y"]
continue
if k == "y2" and "h" in e:
e["y2"] = e["y1"] + e["h"]
continue
raise KeyError(f"Mandatory key '{k}' missing in input data")
# x2 is optional for barcode types, but needed for offset rendering
if "x2" not in e:
if e["type"] in ["B", "C39"]:
e["x2"] = 0
else:
raise KeyError("Mandatory key 'x2' missing in input data")
if not "size" in e and e["type"] == "C39":
if "w" in e:
e["size"] = e["w"]
for k, t in key_config.items():
if k in e and not isinstance(e[k], t):
raise TypeError(
Expand Down Expand Up @@ -485,16 +501,6 @@ def _code39(
"code39 arguments x/y/w/h are deprecated, please use x1/y1/y2/size instead",
PendingDeprecationWarning,
)
if x1 is None and x is not None:
x1 = x
if y1 is None and y is not None:
y1 = y
if size is None and w is not None:
size = w
if x2 is None:
x2 = x1 + size
if y2 is None and h is not None:
y2 = y1 + h
pdf = self.pdf
if pdf.fill_color.lower() != _rgb_as_str(foreground):
pdf.set_fill_color(*_rgb(foreground))
Expand Down
Binary file modified test/template/template_nominal_hardcoded.pdf
Binary file not shown.
70 changes: 65 additions & 5 deletions test/template/test_template.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pathlib import Path
from pytest import raises
from pytest import raises, warns

import qrcode

Expand Down Expand Up @@ -40,8 +40,8 @@ def test_template_nominal_hardcoded(tmp_path):
"font": "helvetica",
"size": 12,
"bold": 1,
"italic": 0,
"underline": 0,
"italic": 1,
"underline": 1,
"foreground": 0,
"align": "I",
"text": "",
Expand Down Expand Up @@ -177,6 +177,31 @@ def test_template_multipage(tmp_path):
assert_pdf_equal(tmpl, HERE / "template_multipage.pdf", tmp_path)


# pylint: disable=unused-argument
def test_template_item_access(tmp_path):
"""Testing Template() getitem/setitem."""
elements = [
{
"name": "name",
"type": "T",
"x1": 20,
"y1": 75,
"x2": 30,
"y2": 90,
"text": "default text",
}
]
templ = Template(elements=elements)
assert ("notthere" in templ) is False
with raises(FPDFException):
templ["notthere"] = "something"
defaultval = templ["name"] # find in default data
assert defaultval == "default text"
templ["name"] = "new text"
defaultval = templ["name"] # find in text data
assert defaultval == "new text"


# pylint: disable=unused-argument
def test_template_badinput(tmp_path):
"""Testing Template() with non-conforming definitions."""
Expand All @@ -191,15 +216,29 @@ def test_template_badinput(tmp_path):
"keywords",
):
with raises(TypeError):
Template(**{arg: 7})
Template(**{arg: 7}) # numeric instead of str
elements = [{}]
with raises(KeyError):
tmpl = Template(elements=elements)
elements = [{"name": "n", "type": "X"}]
with raises(KeyError):
tmpl = Template(elements=elements)
tmpl.render()
elements = [
elements = [ # missing mandatory x2
{
"name": "n",
"type": "T",
"x1": 0,
"y1": 0,
"y2": 0,
"text": "Hello!",
}
]
with raises(KeyError):
tmpl = Template(elements=elements)
tmpl["n"] = "hello"
tmpl.render()
elements = [ # malformed y2
{
"name": "n",
"type": "T",
Expand Down Expand Up @@ -244,6 +283,27 @@ def test_template_code39(tmp_path): # issue-161
assert_pdf_equal(tmpl, HERE / "template_code39.pdf", tmp_path)


def test_template_code39_legacy(tmp_path):
# check that old parameters still work
# This uses the same values as above, and compares to the same file.
elements = [
{
"name": "code39",
"type": "C39",
"x": 40,
"y": 50,
"w": 1.5,
"h": 20,
"text": "*Code 39 barcode*",
"priority": 1,
},
]
with warns(PendingDeprecationWarning):
tmpl = Template(format="A4", title="Sample Code 39 barcode", elements=elements)
tmpl.add_page()
assert_pdf_equal(tmpl, HERE / "template_code39.pdf", tmp_path)


def test_template_code39_defaultheight(tmp_path): # height <= 0 invokes default
elements = [
{
Expand Down

0 comments on commit 8b6290b

Please sign in to comment.