mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 06:17:35 +00:00
LibPDF: Move color space creation from Renderer to ColorSpace
This commit is contained in:
parent
8e175b4959
commit
1238e65d30
3 changed files with 52 additions and 41 deletions
|
@ -6,10 +6,44 @@
|
||||||
|
|
||||||
#include <LibPDF/ColorSpace.h>
|
#include <LibPDF/ColorSpace.h>
|
||||||
#include <LibPDF/CommonNames.h>
|
#include <LibPDF/CommonNames.h>
|
||||||
|
#include <LibPDF/Document.h>
|
||||||
#include <LibPDF/ObjectDerivatives.h>
|
#include <LibPDF/ObjectDerivatives.h>
|
||||||
|
|
||||||
namespace PDF {
|
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()
|
NonnullRefPtr<DeviceGrayColorSpace> DeviceGrayColorSpace::the()
|
||||||
{
|
{
|
||||||
static auto instance = adopt_ref(*new DeviceGrayColorSpace());
|
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);
|
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)
|
if (parameters.size() != 1)
|
||||||
return Error { Error::Type::MalformedPDF, "RGB color space expects one parameter" };
|
return Error { Error::Type::MalformedPDF, "RGB color space expects one parameter" };
|
||||||
|
|
|
@ -25,8 +25,12 @@
|
||||||
|
|
||||||
namespace PDF {
|
namespace PDF {
|
||||||
|
|
||||||
|
class Page;
|
||||||
|
|
||||||
class ColorSpace : public RefCounted<ColorSpace> {
|
class ColorSpace : public RefCounted<ColorSpace> {
|
||||||
public:
|
public:
|
||||||
|
static PDFErrorOr<NonnullRefPtr<ColorSpace>> create(Document*, FlyString const& name, Page const& page);
|
||||||
|
|
||||||
virtual ~ColorSpace() = default;
|
virtual ~ColorSpace() = default;
|
||||||
|
|
||||||
virtual Color color(Vector<Value> const& arguments) const = 0;
|
virtual Color color(Vector<Value> const& arguments) const = 0;
|
||||||
|
@ -36,9 +40,9 @@ class DeviceGrayColorSpace final : public ColorSpace {
|
||||||
public:
|
public:
|
||||||
static NonnullRefPtr<DeviceGrayColorSpace> the();
|
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:
|
private:
|
||||||
DeviceGrayColorSpace() = default;
|
DeviceGrayColorSpace() = default;
|
||||||
|
@ -48,9 +52,9 @@ class DeviceRGBColorSpace final : public ColorSpace {
|
||||||
public:
|
public:
|
||||||
static NonnullRefPtr<DeviceRGBColorSpace> the();
|
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:
|
private:
|
||||||
DeviceRGBColorSpace() = default;
|
DeviceRGBColorSpace() = default;
|
||||||
|
@ -60,9 +64,9 @@ class DeviceCMYKColorSpace final : public ColorSpace {
|
||||||
public:
|
public:
|
||||||
static NonnullRefPtr<DeviceCMYKColorSpace> the();
|
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:
|
private:
|
||||||
DeviceCMYKColorSpace() = default;
|
DeviceCMYKColorSpace() = default;
|
||||||
|
@ -70,10 +74,11 @@ private:
|
||||||
|
|
||||||
class CalRGBColorSpace final : public ColorSpace {
|
class CalRGBColorSpace final : public ColorSpace {
|
||||||
public:
|
public:
|
||||||
static PDFErrorOr<NonnullRefPtr<CalRGBColorSpace>> create(RefPtr<Document>, Vector<Value>&& parameters);
|
static PDFErrorOr<NonnullRefPtr<CalRGBColorSpace>> create(Document*, Vector<Value>&& parameters);
|
||||||
virtual ~CalRGBColorSpace() override = default;
|
|
||||||
|
|
||||||
virtual Color color(Vector<Value> const& arguments) const override;
|
~CalRGBColorSpace() override = default;
|
||||||
|
|
||||||
|
Color color(Vector<Value> const& arguments) const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
CalRGBColorSpace() = default;
|
CalRGBColorSpace() = default;
|
||||||
|
|
|
@ -462,14 +462,14 @@ RENDERER_TODO(type3_font_set_glyph_width_and_bbox)
|
||||||
|
|
||||||
RENDERER_HANDLER(set_stroking_space)
|
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);
|
VERIFY(state().stroke_color_space);
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
RENDERER_HANDLER(set_painting_space)
|
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);
|
VERIFY(state().paint_color_space);
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
@ -656,35 +656,7 @@ void Renderer::show_text(String const& string, float shift)
|
||||||
PDFErrorOr<NonnullRefPtr<ColorSpace>> Renderer::get_color_space(Value const& value)
|
PDFErrorOr<NonnullRefPtr<ColorSpace>> Renderer::get_color_space(Value const& value)
|
||||||
{
|
{
|
||||||
auto name = value.get<NonnullRefPtr<Object>>()->cast<NameObject>()->name();
|
auto name = value.get<NonnullRefPtr<Object>>()->cast<NameObject>()->name();
|
||||||
|
return TRY(ColorSpace::create(m_document, name, m_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(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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Gfx::AffineTransform const& Renderer::calculate_text_rendering_matrix()
|
Gfx::AffineTransform const& Renderer::calculate_text_rendering_matrix()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue