LibPDF: Propagate ColorSpace errors

This commit is contained in:
Matthew Olsson 2022-03-05 17:52:00 -07:00 committed by Andreas Kling
parent 73cf8205b4
commit d82bd885ce
Notes: sideshowbarker 2024-07-17 17:50:57 +09:00
4 changed files with 26 additions and 27 deletions

View file

@ -10,7 +10,7 @@
namespace PDF {
RefPtr<DeviceGrayColorSpace> DeviceGrayColorSpace::the()
NonnullRefPtr<DeviceGrayColorSpace> DeviceGrayColorSpace::the()
{
static auto instance = adopt_ref(*new DeviceGrayColorSpace());
return instance;
@ -23,7 +23,7 @@ Color DeviceGrayColorSpace::color(Vector<Value> const& arguments) const
return Color(gray, gray, gray);
}
RefPtr<DeviceRGBColorSpace> DeviceRGBColorSpace::the()
NonnullRefPtr<DeviceRGBColorSpace> DeviceRGBColorSpace::the()
{
static auto instance = adopt_ref(*new DeviceRGBColorSpace());
return instance;
@ -38,7 +38,7 @@ Color DeviceRGBColorSpace::color(Vector<Value> const& arguments) const
return Color(r, g, b);
}
RefPtr<DeviceCMYKColorSpace> DeviceCMYKColorSpace::the()
NonnullRefPtr<DeviceCMYKColorSpace> DeviceCMYKColorSpace::the()
{
static auto instance = adopt_ref(*new DeviceCMYKColorSpace());
return instance;
@ -54,23 +54,22 @@ Color DeviceCMYKColorSpace::color(Vector<Value> const& arguments) const
return Color::from_cmyk(c, m, y, k);
}
RefPtr<CalRGBColorSpace> CalRGBColorSpace::create(RefPtr<Document> document, Vector<Value>&& parameters)
PDFErrorOr<NonnullRefPtr<CalRGBColorSpace>> CalRGBColorSpace::create(RefPtr<Document> document, Vector<Value>&& parameters)
{
if (parameters.size() != 1)
return {};
return Error { Error::Type::MalformedPDF, "RGB color space expects one parameter" };
auto param = parameters[0];
if (!param.has<NonnullRefPtr<Object>>() || !param.get<NonnullRefPtr<Object>>()->is<DictObject>())
return {};
return Error { Error::Type::MalformedPDF, "RGB color space expects a dict parameter" };
auto dict = param.get<NonnullRefPtr<Object>>()->cast<DictObject>();
if (!dict->contains(CommonNames::WhitePoint))
return {};
return Error { Error::Type::MalformedPDF, "RGB color space expects a Whitepoint key" };
// FIXME: Propagate errors
auto white_point_array = MUST(dict->get_array(document, CommonNames::WhitePoint));
auto white_point_array = TRY(dict->get_array(document, CommonNames::WhitePoint));
if (white_point_array->size() != 3)
return {};
return Error { Error::Type::MalformedPDF, "RGB color space expects 3 Whitepoint parameters" };
auto color_space = adopt_ref(*new CalRGBColorSpace());
@ -79,10 +78,10 @@ RefPtr<CalRGBColorSpace> CalRGBColorSpace::create(RefPtr<Document> document, Vec
color_space->m_whitepoint[2] = white_point_array->at(2).to_float();
if (color_space->m_whitepoint[1] != 1.0f)
return {};
return Error { Error::Type::MalformedPDF, "RGB color space expects 2nd Whitepoint to be 1.0" };
if (dict->contains(CommonNames::BlackPoint)) {
auto black_point_array = MUST(dict->get_array(document, CommonNames::BlackPoint));
auto black_point_array = TRY(dict->get_array(document, CommonNames::BlackPoint));
if (black_point_array->size() == 3) {
color_space->m_blackpoint[0] = black_point_array->at(0).to_float();
color_space->m_blackpoint[1] = black_point_array->at(1).to_float();
@ -91,7 +90,7 @@ RefPtr<CalRGBColorSpace> CalRGBColorSpace::create(RefPtr<Document> document, Vec
}
if (dict->contains(CommonNames::Gamma)) {
auto gamma_array = MUST(dict->get_array(document, CommonNames::Gamma));
auto gamma_array = TRY(dict->get_array(document, CommonNames::Gamma));
if (gamma_array->size() == 3) {
color_space->m_gamma[0] = gamma_array->at(0).to_float();
color_space->m_gamma[1] = gamma_array->at(1).to_float();
@ -100,7 +99,7 @@ RefPtr<CalRGBColorSpace> CalRGBColorSpace::create(RefPtr<Document> document, Vec
}
if (dict->contains(CommonNames::Matrix)) {
auto matrix_array = MUST(dict->get_array(document, CommonNames::Matrix));
auto matrix_array = TRY(dict->get_array(document, CommonNames::Matrix));
if (matrix_array->size() == 3) {
color_space->m_matrix[0] = matrix_array->at(0).to_float();
color_space->m_matrix[1] = matrix_array->at(1).to_float();

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, Matthew Olsson <mattco@serenityos.org>
* Copyright (c) 2021-2022, Matthew Olsson <mattco@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -34,7 +34,7 @@ public:
class DeviceGrayColorSpace final : public ColorSpace {
public:
static RefPtr<DeviceGrayColorSpace> the();
static NonnullRefPtr<DeviceGrayColorSpace> the();
virtual ~DeviceGrayColorSpace() override = default;
@ -46,7 +46,7 @@ private:
class DeviceRGBColorSpace final : public ColorSpace {
public:
static RefPtr<DeviceRGBColorSpace> the();
static NonnullRefPtr<DeviceRGBColorSpace> the();
virtual ~DeviceRGBColorSpace() override = default;
@ -58,7 +58,7 @@ private:
class DeviceCMYKColorSpace final : public ColorSpace {
public:
static RefPtr<DeviceCMYKColorSpace> the();
static NonnullRefPtr<DeviceCMYKColorSpace> the();
virtual ~DeviceCMYKColorSpace() override = default;
@ -70,7 +70,7 @@ private:
class CalRGBColorSpace final : public ColorSpace {
public:
static RefPtr<CalRGBColorSpace> create(RefPtr<Document>, Vector<Value>&& parameters);
static PDFErrorOr<NonnullRefPtr<CalRGBColorSpace>> create(RefPtr<Document>, Vector<Value>&& parameters);
virtual ~CalRGBColorSpace() override = default;
virtual Color color(Vector<Value> const& arguments) const override;

View file

@ -413,13 +413,13 @@ RENDERER_TODO(type3_font_set_glyph_width_and_bbox);
RENDERER_HANDLER(set_stroking_space)
{
state().stroke_color_space = get_color_space(args[0]);
state().stroke_color_space = MUST(get_color_space(args[0]));
VERIFY(state().stroke_color_space);
}
RENDERER_HANDLER(set_painting_space)
{
state().paint_color_space = get_color_space(args[0]);
state().paint_color_space = MUST(get_color_space(args[0]));
VERIFY(state().paint_color_space);
}
@ -564,7 +564,7 @@ void Renderer::show_text(String const& string, float shift)
m_text_matrix = Gfx::AffineTransform(1, 0, 0, 1, delta_x, 0).multiply(m_text_matrix);
}
RefPtr<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();
@ -580,12 +580,12 @@ RefPtr<ColorSpace> Renderer::get_color_space(Value const& value)
// The color space is a complex color space with parameters that resides in
// the resource dictionary
auto color_space_resource_dict = MUST(m_page.resources->get_dict(m_document, CommonNames::ColorSpace));
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 = MUST(color_space_resource_dict->get_array(m_document, name));
name = MUST(color_space_array->get_name_at(m_document, 0))->name();
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);
@ -593,7 +593,7 @@ RefPtr<ColorSpace> Renderer::get_color_space(Value const& value)
parameters.unchecked_append(color_space_array->at(i));
if (name == CommonNames::CalRGB)
return CalRGBColorSpace::create(m_document, move(parameters));
return TRY(CalRGBColorSpace::create(m_document, move(parameters)));
TODO();
}

View file

@ -97,7 +97,7 @@ private:
void set_graphics_state_from_dict(NonnullRefPtr<DictObject>);
// shift is the manual advance given in the TJ command array
void show_text(String const&, float shift = 0.0f);
RefPtr<ColorSpace> get_color_space(Value const&);
PDFErrorOr<NonnullRefPtr<ColorSpace>> get_color_space(Value const&);
ALWAYS_INLINE GraphicsState const& state() const { return m_graphics_state_stack.last(); }
ALWAYS_INLINE GraphicsState& state() { return m_graphics_state_stack.last(); }