Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
liZe committed Jun 25, 2024
1 parent 8fc79e3 commit bae9519
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
14 changes: 8 additions & 6 deletions weasyprint/pdf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,14 @@ def generate_pdf(document, target, zoom, **options):
pdf = pydyf.PDF()
images = {}
color_space = pydyf.Dictionary({
'xyz': pydyf.Array(('/Lab', pydyf.Dictionary({
'WhitePoint': pydyf.Array((1, 1, 1))}))),
'xyz-d50': pydyf.Array(('/Lab', pydyf.Dictionary({
'WhitePoint': pydyf.Array(D50)}))),
'xyz-d65': pydyf.Array(('/Lab', pydyf.Dictionary({
'WhitePoint': pydyf.Array(D65)}))),
'lab-d50': pydyf.Array(('/Lab', pydyf.Dictionary({
'WhitePoint': pydyf.Array(D50),
'Range': pydyf.Array((-125, 125, -125, 125)),
}))),
'lab-d65': pydyf.Array(('/Lab', pydyf.Dictionary({
'WhitePoint': pydyf.Array(D65),
'Range': pydyf.Array((-125, 125, -125, 125)),
}))),
})
pdf.add_object(color_space)
resources = pydyf.Dictionary({
Expand Down
42 changes: 19 additions & 23 deletions weasyprint/pdf/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,36 +81,32 @@ def end_text(self):
super().end_text()

def set_color(self, color, stroke=False):
space = color.space
*channels, a = color
*channels, alpha = color
if stroke:
if (space, *channels) == self._current_color_stroke:
if (color.space, *channels) == self._current_color_stroke:
return
else:
self._current_color_stroke = (space, *channels)
self._current_color_stroke = (color.space, *channels)
else:
if (space, *channels) == self._current_color:
if (color.space, *channels) == self._current_color:
return
else:
self._current_color = (space, *channels)

if space == 'srgb':
super().set_color_rgb(*channels, stroke)
self._current_color = (color.space, *channels)

if color.space in ('srgb', 'hsl', 'hwb'):
super().set_color_rgb(*color.to('srgb').coordinates, stroke)
elif color.space in ('xyz-d65', 'oklab', 'oklch'):
super().color_space('lab-d65', stroke)
lightness, a, b = color.to('lab').coordinates
super().set_color_special(None, stroke, lightness, a, b)
elif color.space in ('xyz-d50', 'lab', 'lch'):
super().color_space('lab-d50', stroke)
lightness, a, b = color.to('lab').coordinates
super().set_color_special(None, stroke, lightness, a, b)
else:
if space in ('xyz', 'xyz-d50', 'xyz-d65'):
super().color_space(space, stroke)
if space == 'xyz':
d = (1, 1, 1)
elif space == 'xyz-d50':
d = D50
elif space == 'xyz-d65':
d = D65
lab = xyz_to_lab(*channels, d)
super().set_color_special(None, stroke, *lab)
else:
LOGGER.warn('Unsupported color space %, use sRGB instead')
super().set_color_rgb(*channels, stroke)
self.set_alpha(a, stroke)
LOGGER.warn('Unsupported color space %s, use sRGB instead', color.space)
super().set_color_rgb(*channels, stroke)
self.set_alpha(alpha, stroke)

def set_font_size(self, font, size):
if (font, size) == self._current_font:
Expand Down

0 comments on commit bae9519

Please sign in to comment.