Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature: Gradient patterns #1334

Merged
merged 18 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions fpdf/fpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ def __init__(
self._drawing_graphics_state_registry = GraphicsStateDictRegistry()
# map page numbers to a set of GraphicsState names:
self.graphics_style_names_per_page_number = defaultdict(set)
self.shadings_per_page_number = defaultdict(set)
self.patterns_per_page_number = defaultdict(set)
self._shading_registry = {}
self._pattern_registry = {}
andersonhc marked this conversation as resolved.
Show resolved Hide resolved

self._record_text_quad_points = False

Expand Down Expand Up @@ -1265,6 +1269,25 @@ def drawing_context(self, debug_stream=None):
# The drawing API makes use of features (notably transparency and blending modes) that were introduced in PDF 1.4:
self._set_min_pdf_version("1.4")

@contextmanager
@check_page
def use_pattern(self, shading):
"""
Create a context for using a shading pattern on the current page.
"""
if shading not in self._shading_registry:
self._shading_registry[shading] = f"Sh{len(self._shading_registry) + 1}"
pattern = shading.get_pattern()
if pattern not in self._pattern_registry:
self._pattern_registry[pattern] = "P" + str(len(self._pattern_registry) + 1)
self._out(f"/Pattern cs /{self._pattern_registry[pattern]} scn")
self.shadings_per_page_number[self.page].add(self._shading_registry[shading])
self.patterns_per_page_number[self.page].add(self._pattern_registry[pattern])
try:
yield
finally:
self._out(self.draw_color.serialize().lower())

def _current_graphic_style(self):
gs = GraphicsStyle()
gs.allow_transparency = self.allow_images_transparency
Expand Down Expand Up @@ -4852,6 +4875,7 @@ def _default_file_id(self, buffer):
# > When a file is first written, both identifiers shall be set to the same value.
id_hash = hashlib.new("md5", usedforsecurity=False) # nosec B324
id_hash.update(buffer)
print(str(buffer))
andersonhc marked this conversation as resolved.
Show resolved Hide resolved
if self.creation_date:
id_hash.update(self.creation_date.strftime("%Y%m%d%H%M%S").encode("utf8"))
hash_hex = id_hash.hexdigest().upper()
Expand Down
8 changes: 7 additions & 1 deletion fpdf/linearization.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,14 @@ def bufferize(self):
font_objs_per_index = self._add_fonts()
img_objs_per_index = self._add_images()
gfxstate_objs_per_name = self._add_gfxstates()
shading_objs_per_name = self._add_shadings()
pattern_objs_per_name = self._add_patterns()
resources_dict_obj = self._add_resources_dict(
font_objs_per_index, img_objs_per_index, gfxstate_objs_per_name
font_objs_per_index,
img_objs_per_index,
gfxstate_objs_per_name,
shading_objs_per_name,
pattern_objs_per_name,
)
# Part 9: Objects not associated with pages, if any
for embedded_file in fpdf.embedded_files:
Expand Down
70 changes: 65 additions & 5 deletions fpdf/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,14 @@ def __init__(


class PDFResources(PDFObject):
def __init__(self, proc_set, font, x_object, ext_g_state):
def __init__(self, proc_set, font, x_object, ext_g_state, shading, pattern):
super().__init__()
self.proc_set = proc_set
self.font = font
self.x_object = x_object
self.ext_g_state = ext_g_state
self.shading = shading
self.pattern = pattern


class PDFFontStream(PDFContentStream):
Expand Down Expand Up @@ -889,14 +891,37 @@ def _add_gfxstates(self):
gfxstate_objs_per_name[name] = gfxstate_obj
return gfxstate_objs_per_name

def _add_shadings(self):
shading_objs_per_name = OrderedDict()
for shading, name in self.fpdf._shading_registry.items():
for function in shading.functions:
self._add_pdf_obj(function, "function")
shading_obj = shading.get_shading_object()
self._add_pdf_obj(shading_obj, "shading")
shading_objs_per_name[name] = shading_obj
return shading_objs_per_name

def _add_patterns(self):
pattern_objs_per_name = OrderedDict()
for pattern, name in self.fpdf._pattern_registry.items():
self._add_pdf_obj(pattern, "pattern")
pattern_objs_per_name[name] = pattern
return pattern_objs_per_name

def _insert_resources(self, page_objs):
font_objs_per_index = self._add_fonts()
img_objs_per_index = self._add_images()
gfxstate_objs_per_name = self._add_gfxstates()
shading_objs_per_name = self._add_shadings()
pattern_objs_per_name = self._add_patterns()
# Insert /Resources dicts:
if self.fpdf.single_resources_object:
resources_dict_obj = self._add_resources_dict(
font_objs_per_index, img_objs_per_index, gfxstate_objs_per_name
font_objs_per_index,
img_objs_per_index,
gfxstate_objs_per_name,
shading_objs_per_name,
pattern_objs_per_name,
)
for page_obj in page_objs:
page_obj.resources = resources_dict_obj
Expand All @@ -916,22 +941,37 @@ def _insert_resources(self, page_objs):
if gfx_name
in self.fpdf.graphics_style_names_per_page_number[page_number]
}
page_shading_objs_per_name = {
shading_name: shading_objs_per_name[shading_name]
for shading_name in self.fpdf.shadings_per_page_number[page_number]
}
page_pattern_objs_per_name = {
pattern_name: pattern_objs_per_name[pattern_name]
for pattern_name in self.fpdf.patterns_per_page_number[page_number]
}
page_obj.resources = self._add_resources_dict(
page_font_objs_per_index,
page_img_objs_per_index,
page_gfxstate_objs_per_name,
page_shading_objs_per_name,
page_pattern_objs_per_name,
)

def _add_resources_dict(
self, font_objs_per_index, img_objs_per_index, gfxstate_objs_per_name
self,
font_objs_per_index,
img_objs_per_index,
gfxstate_objs_per_name,
shading_objs_per_name,
pattern_objs_per_name,
):
# From section 10.1, "Procedure Sets", of PDF 1.7 spec:
# > Beginning with PDF 1.4, this feature is considered obsolete.
# > For compatibility with existing consumer applications,
# > PDF producer applications should continue to specify procedure sets
# > (preferably, all of those listed in Table 10.1).
proc_set = "[/PDF /Text /ImageB /ImageC /ImageI]"
font, x_object, ext_g_state = None, None, None
font, x_object, ext_g_state, shading, pattern = None, None, None, None, None

if font_objs_per_index:
font = pdf_dict(
Expand All @@ -956,9 +996,29 @@ def _add_resources_dict(
for name, gfxstate_obj in gfxstate_objs_per_name.items()
}
)
if shading_objs_per_name:
shading = pdf_dict(
{
f"/{name}": pdf_ref(shading_obj.id)
for name, shading_obj in sorted(shading_objs_per_name.items())
}
)

if pattern_objs_per_name:
pattern = pdf_dict(
{
f"/{name}": pdf_ref(pattern_obj.id)
for name, pattern_obj in sorted(pattern_objs_per_name.items())
}
)

resources_obj = PDFResources(
proc_set=proc_set, font=font, x_object=x_object, ext_g_state=ext_g_state
proc_set=proc_set,
font=font,
x_object=x_object,
ext_g_state=ext_g_state,
shading=shading,
pattern=pattern,
)
self._add_pdf_obj(resources_obj)
return resources_obj
Expand Down
Loading
Loading