diff --git a/weasyprint/pdf/__init__.py b/weasyprint/pdf/__init__.py index ea68d52d0..01048513f 100644 --- a/weasyprint/pdf/__init__.py +++ b/weasyprint/pdf/__init__.py @@ -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({ diff --git a/weasyprint/pdf/stream.py b/weasyprint/pdf/stream.py index c9651eb2c..f7fda3c03 100644 --- a/weasyprint/pdf/stream.py +++ b/weasyprint/pdf/stream.py @@ -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: