1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:47:36 +00:00

LibPDF: Refactor parsing of ColorSpaces

ColorSpaces can be specified in two ways: with a stream as operands of
the color space operations (CS/cs), or as a separate PDF object, which
is then referred to by other means (e.g., from Image XObjects and other
entities). These two modes of addressing a ColorSpace are slightly
different and need to be addressed separately. However, the current
implementation embedded the full logic of the first case in the routine
that created ColorSpace objects.

This commit refactors the creation of ColorSpace to support both cases.
First, a new ColorSpaceFamily class encapsulates the static aspects of a
family, like its name or whether color space construction never requires
parameters. Then we define the supported ColorSpaceFamily objects.

On top of this also sit a breakage on how ColorSpaces are created. Two
methods are now offered: one only providing construction of no-argument
color spaces (and thus taking a simple name), and another taking an
ArrayObject, hence used to create ColorSpaces requiring arguments.

Finally, on top of *that* two ways to get a color space in the Renderer
are made available: the first creates a ColorSpace with a name and a
Resources dictionary, and another takes an Object. These model the two
addressing modes described above.
This commit is contained in:
Rodrigo Tobar 2022-11-24 12:40:24 +08:00 committed by Andreas Kling
parent 287bb0feac
commit ba16310739
4 changed files with 97 additions and 34 deletions

View file

@ -523,14 +523,14 @@ RENDERER_TODO(type3_font_set_glyph_width_and_bbox)
RENDERER_HANDLER(set_stroking_space)
{
state().stroke_color_space = TRY(get_color_space(args[0], extra_resources.value_or(m_page.resources)));
state().stroke_color_space = TRY(get_color_space_from_resources(args[0], extra_resources.value_or(m_page.resources)));
VERIFY(state().stroke_color_space);
return {};
}
RENDERER_HANDLER(set_painting_space)
{
state().paint_color_space = TRY(get_color_space(args[0], extra_resources.value_or(m_page.resources)));
state().paint_color_space = TRY(get_color_space_from_resources(args[0], extra_resources.value_or(m_page.resources)));
VERIFY(state().paint_color_space);
return {};
}
@ -759,10 +759,28 @@ void Renderer::show_text(DeprecatedString const& string)
m_text_matrix.translate(delta_x / text_rendering_matrix.x_scale(), 0.0f);
}
PDFErrorOr<NonnullRefPtr<ColorSpace>> Renderer::get_color_space(Value const& value, NonnullRefPtr<DictObject> resources)
PDFErrorOr<NonnullRefPtr<ColorSpace>> Renderer::get_color_space_from_resources(Value const& value, NonnullRefPtr<DictObject> resources)
{
auto name = value.get<NonnullRefPtr<Object>>()->cast<NameObject>()->name();
return TRY(ColorSpace::create(m_document, name, resources));
auto color_space_name = value.get<NonnullRefPtr<Object>>()->cast<NameObject>()->name();
auto maybe_color_space_family = ColorSpaceFamily::get(color_space_name);
if (!maybe_color_space_family.is_error()) {
auto color_space_family = maybe_color_space_family.release_value();
if (color_space_family.never_needs_parameters()) {
return ColorSpace::create(color_space_name);
}
}
auto color_space_resource_dict = TRY(resources->get_dict(m_document, CommonNames::ColorSpace));
auto color_space_array = TRY(color_space_resource_dict->get_array(m_document, color_space_name));
return ColorSpace::create(m_document, color_space_array);
}
PDFErrorOr<NonnullRefPtr<ColorSpace>> Renderer::get_color_space_from_document(NonnullRefPtr<Object> color_space_object)
{
// Pattern cannot be a name in these cases
if (color_space_object->is<NameObject>()) {
return ColorSpace::create(color_space_object->cast<NameObject>()->name());
}
return ColorSpace::create(m_document, color_space_object->cast<ArrayObject>());
}
Gfx::AffineTransform const& Renderer::calculate_text_rendering_matrix()