LibPDF: Move color space creation from Renderer to ColorSpace

This commit is contained in:
Matthew Olsson 2022-03-24 10:08:08 -07:00 committed by Andreas Kling
parent 8e175b4959
commit 1238e65d30
3 changed files with 52 additions and 41 deletions

View file

@ -6,10 +6,44 @@
#include <LibPDF/ColorSpace.h>
#include <LibPDF/CommonNames.h>
#include <LibPDF/Document.h>
#include <LibPDF/ObjectDerivatives.h>
namespace PDF {
PDFErrorOr<NonnullRefPtr<ColorSpace>> ColorSpace::create(Document* document, FlyString const& name, Page const& page)
{
// Simple color spaces with no parameters, which can be specified directly
if (name == CommonNames::DeviceGray)
return DeviceGrayColorSpace::the();
if (name == CommonNames::DeviceRGB)
return DeviceRGBColorSpace::the();
if (name == CommonNames::DeviceCMYK)
return DeviceCMYKColorSpace::the();
if (name == CommonNames::Pattern)
TODO();
// The color space is a complex color space with parameters that resides in
// the resource dictionary
auto color_space_resource_dict = TRY(page.resources->get_dict(document, CommonNames::ColorSpace));
if (!color_space_resource_dict->contains(name))
TODO();
auto color_space_array = TRY(color_space_resource_dict->get_array(document, name));
auto color_space_name = TRY(color_space_array->get_name_at(document, 0))->name();
Vector<Value> parameters;
parameters.ensure_capacity(color_space_array->size() - 1);
for (size_t i = 1; i < color_space_array->size(); i++)
parameters.unchecked_append(color_space_array->at(i));
if (color_space_name == CommonNames::CalRGB)
return TRY(CalRGBColorSpace::create(document, move(parameters)));
dbgln("Unknown color space: {}", color_space_name);
TODO();
}
NonnullRefPtr<DeviceGrayColorSpace> DeviceGrayColorSpace::the()
{
static auto instance = adopt_ref(*new DeviceGrayColorSpace());
@ -54,7 +88,7 @@ Color DeviceCMYKColorSpace::color(Vector<Value> const& arguments) const
return Color::from_cmyk(c, m, y, k);
}
PDFErrorOr<NonnullRefPtr<CalRGBColorSpace>> CalRGBColorSpace::create(RefPtr<Document> document, Vector<Value>&& parameters)
PDFErrorOr<NonnullRefPtr<CalRGBColorSpace>> CalRGBColorSpace::create(Document* document, Vector<Value>&& parameters)
{
if (parameters.size() != 1)
return Error { Error::Type::MalformedPDF, "RGB color space expects one parameter" };

View file

@ -25,8 +25,12 @@
namespace PDF {
class Page;
class ColorSpace : public RefCounted<ColorSpace> {
public:
static PDFErrorOr<NonnullRefPtr<ColorSpace>> create(Document*, FlyString const& name, Page const& page);
virtual ~ColorSpace() = default;
virtual Color color(Vector<Value> const& arguments) const = 0;
@ -36,9 +40,9 @@ class DeviceGrayColorSpace final : public ColorSpace {
public:
static NonnullRefPtr<DeviceGrayColorSpace> the();
virtual ~DeviceGrayColorSpace() override = default;
~DeviceGrayColorSpace() override = default;
virtual Color color(Vector<Value> const& arguments) const override;
Color color(Vector<Value> const& arguments) const override;
private:
DeviceGrayColorSpace() = default;
@ -48,9 +52,9 @@ class DeviceRGBColorSpace final : public ColorSpace {
public:
static NonnullRefPtr<DeviceRGBColorSpace> the();
virtual ~DeviceRGBColorSpace() override = default;
~DeviceRGBColorSpace() override = default;
virtual Color color(Vector<Value> const& arguments) const override;
Color color(Vector<Value> const& arguments) const override;
private:
DeviceRGBColorSpace() = default;
@ -60,9 +64,9 @@ class DeviceCMYKColorSpace final : public ColorSpace {
public:
static NonnullRefPtr<DeviceCMYKColorSpace> the();
virtual ~DeviceCMYKColorSpace() override = default;
~DeviceCMYKColorSpace() override = default;
virtual Color color(Vector<Value> const& arguments) const override;
Color color(Vector<Value> const& arguments) const override;
private:
DeviceCMYKColorSpace() = default;
@ -70,10 +74,11 @@ private:
class CalRGBColorSpace final : public ColorSpace {
public:
static PDFErrorOr<NonnullRefPtr<CalRGBColorSpace>> create(RefPtr<Document>, Vector<Value>&& parameters);
virtual ~CalRGBColorSpace() override = default;
static PDFErrorOr<NonnullRefPtr<CalRGBColorSpace>> create(Document*, Vector<Value>&& parameters);
virtual Color color(Vector<Value> const& arguments) const override;
~CalRGBColorSpace() override = default;
Color color(Vector<Value> const& arguments) const override;
private:
CalRGBColorSpace() = default;

View file

@ -462,14 +462,14 @@ RENDERER_TODO(type3_font_set_glyph_width_and_bbox)
RENDERER_HANDLER(set_stroking_space)
{
state().stroke_color_space = MUST(get_color_space(args[0]));
state().stroke_color_space = TRY(get_color_space(args[0]));
VERIFY(state().stroke_color_space);
return {};
}
RENDERER_HANDLER(set_painting_space)
{
state().paint_color_space = MUST(get_color_space(args[0]));
state().paint_color_space = TRY(get_color_space(args[0]));
VERIFY(state().paint_color_space);
return {};
}
@ -656,35 +656,7 @@ void Renderer::show_text(String const& string, float shift)
PDFErrorOr<NonnullRefPtr<ColorSpace>> Renderer::get_color_space(Value const& value)
{
auto name = value.get<NonnullRefPtr<Object>>()->cast<NameObject>()->name();
// Simple color spaces with no parameters, which can be specified directly
if (name == CommonNames::DeviceGray)
return DeviceGrayColorSpace::the();
if (name == CommonNames::DeviceRGB)
return DeviceRGBColorSpace::the();
if (name == CommonNames::DeviceCMYK)
return DeviceCMYKColorSpace::the();
if (name == CommonNames::Pattern)
TODO();
// The color space is a complex color space with parameters that resides in
// the resource dictionary
auto color_space_resource_dict = TRY(m_page.resources->get_dict(m_document, CommonNames::ColorSpace));
if (!color_space_resource_dict->contains(name))
TODO();
auto color_space_array = TRY(color_space_resource_dict->get_array(m_document, name));
name = TRY(color_space_array->get_name_at(m_document, 0))->name();
Vector<Value> parameters;
parameters.ensure_capacity(color_space_array->size() - 1);
for (size_t i = 1; i < color_space_array->size(); i++)
parameters.unchecked_append(color_space_array->at(i));
if (name == CommonNames::CalRGB)
return TRY(CalRGBColorSpace::create(m_document, move(parameters)));
TODO();
return TRY(ColorSpace::create(m_document, name, m_page));
}
Gfx::AffineTransform const& Renderer::calculate_text_rendering_matrix()