Skip to content

Commit

Permalink
Fixing some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas-C committed Feb 28, 2024
1 parent ffde695 commit ce824fc
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion fpdf/drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def colors255(self):
return tuple(255 * v for v in self.colors)

def serialize(self) -> str:
return f"{0 if self.g == 0 else self.g} {self.OPERATOR}"
return f"{number_to_str(self.g)} {self.OPERATOR}"


__pdoc__["DeviceGray.OPERATOR"] = False
Expand Down
5 changes: 4 additions & 1 deletion fpdf/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ class TextEmphasis(CoerciveIntFlag):
style = B | I
"""

NONE = 0
"No emphasis"

B = 1
"Bold"

Expand All @@ -246,7 +249,7 @@ def style(self):
def coerce(cls, value):
if isinstance(value, str):
if value == "":
return 0
return cls.NONE
if value.upper() == "BOLD":
return cls.B
if value.upper() == "ITALICS":
Expand Down
6 changes: 4 additions & 2 deletions fpdf/fonts.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ class FontFace:
"fill_color",
)
family: Optional[str]
emphasis: Optional[TextEmphasis] # can be a combination: B | U
emphasis: Optional[TextEmphasis] # None means "no override"
# Whereas "" means "no emphasis"
# This can be a combination: B | U
size_pt: Optional[int]
# Colors are single number grey scales or (red, green, blue) tuples:
color: Optional[Union[DeviceGray, DeviceRGB]]
Expand All @@ -61,7 +63,7 @@ def __init__(
self, family=None, emphasis=None, size_pt=None, color=None, fill_color=None
):
self.family = family
self.emphasis = TextEmphasis.coerce(emphasis) if emphasis else None
self.emphasis = None if emphasis is None else TextEmphasis.coerce(emphasis)
self.size_pt = size_pt
self.color = None if color is None else convert_to_device_color(color)
self.fill_color = (
Expand Down
5 changes: 3 additions & 2 deletions fpdf/fpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ class Image:
class TitleStyle(FontFace):
def __init__(
self,
font_family: Optional[str] = None,
font_family: Optional[str] = None, # None means "no override"
# Whereas "" means "no emphasis"
font_style: Optional[str] = None,
font_size_pt: Optional[int] = None,
color: Union[int, tuple] = None, # grey scale or (red, green, blue),
Expand All @@ -155,7 +156,7 @@ def __init__(
):
super().__init__(
font_family,
(font_style or "") + ("U" if underline else ""),
((font_style or "") + "U") if underline else font_style,
font_size_pt,
color,
)
Expand Down
4 changes: 2 additions & 2 deletions test/drawing/test_drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ def test_device_gray(self):
gray = fpdf.drawing.DeviceGray(g=0.5)
gray_a = fpdf.drawing.DeviceGray(g=0.5, a=0.75)

assert gray.colors == (0.5,)
assert gray_a.colors == (0.5,)
assert gray.colors == (0.5, 0.5, 0.5)
assert gray_a.colors == (0.5, 0.5, 0.5)

with pytest.raises(ValueError):
fpdf.drawing.DeviceGray(g=2)
Expand Down
15 changes: 13 additions & 2 deletions test/outline/test_outline.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,20 @@ def test_toc_with_font_style_override_bold(tmp_path): # issue-1072
pdf = FPDF()
pdf.add_page()
pdf.set_font("Helvetica", "B")
pdf.set_section_title_styles(TitleStyle("Helvetica", "", 20, (0, 0, 0)))
pdf.set_section_title_styles(
TitleStyle("Helvetica", font_size_pt=20, color=(0, 0, 0))
)
pdf.start_section("foo")
assert_pdf_equal(pdf, HERE / "toc_with_font_style_override_bold1.pdf", tmp_path)

pdf = FPDF()
pdf.add_page()
pdf.set_font("Helvetica", "B")
pdf.set_section_title_styles(
TitleStyle("Helvetica", font_style="", font_size_pt=20, color=(0, 0, 0))
)
pdf.start_section("foo")
assert_pdf_equal(pdf, HERE / "toc_with_font_style_override_bold.pdf", tmp_path)
assert_pdf_equal(pdf, HERE / "toc_with_font_style_override_bold2.pdf", tmp_path)


def test_toc_with_table(tmp_path): # issue-1079
Expand Down
Binary file not shown.

0 comments on commit ce824fc

Please sign in to comment.