Skip to content

Commit

Permalink
fitz/utils.py: Fixed issues #1913 and #1917.
Browse files Browse the repository at this point in the history
Changing the default value of "color" parameter from None to (0,) causes color
"black" if letting default it.

New verbose parameter in Document.subset_fonts() suppresses messages by default.
  • Loading branch information
JorjMcKie authored and julian-smith-artifex-com committed Oct 19, 2022
1 parent e4ea5fd commit b5f6f75
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions fitz/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1798,7 +1798,7 @@ def insert_page(
height: float = 842,
fontname: str = "helv",
fontfile: OptStr = None,
color: OptSeq = None,
color: OptSeq = (0,),
) -> int:
"""Create a new PDF page and insert some text.
Expand All @@ -1824,7 +1824,7 @@ def draw_line(
page: Page,
p1: point_like,
p2: point_like,
color: OptSeq = None,
color: OptSeq = (0,),
dashes: OptStr = None,
width: float = 1,
lineCap: int = 0,
Expand Down Expand Up @@ -1860,7 +1860,7 @@ def draw_squiggle(
p1: point_like,
p2: point_like,
breadth: float = 2,
color: OptSeq = None,
color: OptSeq = (0,),
dashes: OptStr = None,
width: float = 1,
lineCap: int = 0,
Expand Down Expand Up @@ -1896,7 +1896,7 @@ def draw_zigzag(
p1: point_like,
p2: point_like,
breadth: float = 2,
color: OptSeq = None,
color: OptSeq = (0,),
dashes: OptStr = None,
width: float = 1,
lineCap: int = 0,
Expand Down Expand Up @@ -1930,7 +1930,7 @@ def draw_zigzag(
def draw_rect(
page: Page,
rect: rect_like,
color: OptSeq = None,
color: OptSeq = (0,),
fill: OptSeq = None,
dashes: OptStr = None,
width: float = 1,
Expand Down Expand Up @@ -1965,7 +1965,7 @@ def draw_rect(
def draw_quad(
page: Page,
quad: quad_like,
color: OptSeq = None,
color: OptSeq = (0,),
fill: OptSeq = None,
dashes: OptStr = None,
width: float = 1,
Expand Down Expand Up @@ -2000,7 +2000,7 @@ def draw_quad(
def draw_polyline(
page: Page,
points: list,
color: OptSeq = None,
color: OptSeq = (0,),
fill: OptSeq = None,
dashes: OptStr = None,
width: float = 1,
Expand Down Expand Up @@ -2038,7 +2038,7 @@ def draw_circle(
page: Page,
center: point_like,
radius: float,
color: OptSeq = None,
color: OptSeq = (0,),
fill: OptSeq = None,
morph: OptSeq = None,
dashes: OptStr = None,
Expand Down Expand Up @@ -2072,7 +2072,7 @@ def draw_circle(
def draw_oval(
page: Page,
rect: typing.Union[rect_like, quad_like],
color: OptSeq = None,
color: OptSeq = (0,),
fill: OptSeq = None,
dashes: OptStr = None,
morph: OptSeq = None,
Expand Down Expand Up @@ -2109,7 +2109,7 @@ def draw_curve(
p1: point_like,
p2: point_like,
p3: point_like,
color: OptSeq = None,
color: OptSeq = (0,),
fill: OptSeq = None,
dashes: OptStr = None,
width: float = 1,
Expand Down Expand Up @@ -2149,7 +2149,7 @@ def draw_bezier(
p2: point_like,
p3: point_like,
p4: point_like,
color: OptSeq = None,
color: OptSeq = (0,),
fill: OptSeq = None,
dashes: OptStr = None,
width: float = 1,
Expand Down Expand Up @@ -2188,7 +2188,7 @@ def draw_sector(
center: point_like,
point: point_like,
beta: float,
color: OptSeq = None,
color: OptSeq = (0,),
fill: OptSeq = None,
dashes: OptStr = None,
fullSector: bool = True,
Expand Down Expand Up @@ -3817,7 +3817,7 @@ def pixlen(x):
def finish(
self,
width: float = 1,
color: OptSeq = None,
color: OptSeq = (0,),
fill: OptSeq = None,
lineCap: int = 0,
lineJoin: int = 0,
Expand Down Expand Up @@ -5057,7 +5057,7 @@ def recover_char_quad(line_dir: tuple, span: dict, char: dict) -> Quad:
# -------------------------------------------------------------------
# Building font subsets using fontTools
# -------------------------------------------------------------------
def subset_fonts(doc: Document) -> None:
def subset_fonts(doc: Document, verbose: bool = False) -> None:
"""Build font subsets of a PDF. Requires package 'fontTools'.
Eligible fonts are potentially replaced by smaller versions. Page text is
Expand Down Expand Up @@ -5301,7 +5301,8 @@ def find_buffer_by_name(name):
# -----------------
repl_fontnames(doc) # populate font information
if not font_buffers: # nothing found to do
print("No fonts to subset.")
if verbose:
print("No fonts to subset.")
return 0

old_fontsize = 0
Expand Down Expand Up @@ -5333,9 +5334,11 @@ def find_buffer_by_name(name):
fontname = list(name_set)[0]
if new_buffer == None or len(new_buffer) >= len(old_buffer):
# subset was not created or did not get smaller
print("Cannot subset '%s'." % fontname)
if verbose:
print(f"Cannot subset '{fontname}'.")
continue
print("Built subset of font '%s'." % fontname)
if verbose:
print(f"Built subset of font '{fontname}'.")
val = doc._insert_font(fontbuffer=new_buffer) # store subset font in PDF
new_xref = val[0] # get its xref
set_subset_fontname(new_xref) # tag fontname as subset font
Expand Down

0 comments on commit b5f6f75

Please sign in to comment.