/* * Copyright (c) 2021, Matthew Olsson * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include #include #include #include #include #include #define ENUMERATE_COLOR_SPACES(V) \ V(DeviceGray) \ V(DeviceRGB) \ V(DeviceCMYK) \ V(CalGray) \ V(CalRGB) \ V(Lab) \ V(ICCBased) \ V(Indexed) \ V(Pattern) \ V(Separation) \ V(DeviceN) namespace PDF { enum class LineCapStyle : u8 { ButtCap = 0, RoundCap = 1, SquareCap = 2, }; enum class LineJoinStyle : u8 { Miter = 0, Round = 1, Bevel = 2, }; struct LineDashPattern { Vector pattern; int phase; }; enum class TextRenderingMode : u8 { Fill = 0, Stroke = 1, FillThenStroke = 2, Invisible = 3, FillAndClip = 4, StrokeAndClip = 5, FillStrokeAndClip = 6, Clip = 7, }; struct TextState { float character_spacing { 3.0f }; float word_spacing { 5.0f }; float horizontal_scaling { 1.0f }; float leading { 0.0f }; RefPtr font; TextRenderingMode rendering_mode { TextRenderingMode::Fill }; float rise { 0.0f }; bool knockout { true }; }; class ColorSpace { public: enum class Type { #define ENUM(name) name, ENUMERATE_COLOR_SPACES(ENUM) #undef ENUM }; static Optional color_space_from_string(const FlyString&); static Color default_color_for_color_space(ColorSpace::Type); static Color color_from_parameters(ColorSpace::Type color_space, const Vector& args); }; struct GraphicsState { Gfx::AffineTransform ctm; ColorSpace::Type stroke_color_space { ColorSpace::Type::DeviceGray }; ColorSpace::Type paint_color_space { ColorSpace::Type::DeviceGray }; Gfx::Color stroke_color { Gfx::Color::NamedColor::Black }; Gfx::Color paint_color { Gfx::Color::NamedColor::Black }; float line_width { 1.0f }; LineCapStyle line_cap_style { LineCapStyle::ButtCap }; LineJoinStyle line_join_style { LineJoinStyle::Miter }; float miter_limit { 10.0f }; LineDashPattern line_dash_pattern { {}, 0 }; TextState text_state {}; }; class Renderer { public: static void render(Document&, const Page&, RefPtr); private: Renderer(RefPtr, const Page&, RefPtr); void render(); void handle_command(const Command&); #define V(name, snake_name, symbol) \ void handle_##snake_name(const Vector& args); ENUMERATE_COMMANDS(V) #undef V void handle_text_next_line_show_string(const Vector& args); void handle_text_next_line_show_string_set_spacing(const Vector& args); // shift is the manual advance given in the TJ command array void show_text(const String&, int shift = 0); ColorSpace::Type get_color_space(const Value&); ALWAYS_INLINE const GraphicsState& state() const { return m_graphics_state_stack.last(); } ALWAYS_INLINE GraphicsState& state() { return m_graphics_state_stack.last(); } ALWAYS_INLINE const TextState& text_state() const { return state().text_state; } ALWAYS_INLINE TextState& text_state() { return state().text_state; } template ALWAYS_INLINE Gfx::Point map(T x, T y) const; template ALWAYS_INLINE Gfx::Size map(Gfx::Size) const; template ALWAYS_INLINE Gfx::Rect map(Gfx::Rect) const; const Gfx::AffineTransform& calculate_text_rendering_matrix(); RefPtr m_document; RefPtr m_bitmap; const Page& m_page; Gfx::Painter m_painter; Gfx::Path m_current_path; Vector m_graphics_state_stack; Gfx::AffineTransform m_text_matrix; Gfx::AffineTransform m_text_line_matrix; Gfx::AffineTransform m_userspace_matrix; bool m_text_rendering_matrix_is_dirty { true }; Gfx::AffineTransform m_text_rendering_matrix; }; } namespace AK { template<> struct Formatter : Formatter { void format(FormatBuilder& builder, const PDF::LineCapStyle& style) { switch (style) { case PDF::LineCapStyle::ButtCap: Formatter::format(builder, "LineCapStyle::ButtCap"); break; case PDF::LineCapStyle::RoundCap: Formatter::format(builder, "LineCapStyle::RoundCap"); break; case PDF::LineCapStyle::SquareCap: Formatter::format(builder, "LineCapStyle::SquareCap"); break; } } }; template<> struct Formatter : Formatter { void format(FormatBuilder& builder, const PDF::LineJoinStyle& style) { switch (style) { case PDF::LineJoinStyle::Miter: Formatter::format(builder, "LineJoinStyle::Miter"); break; case PDF::LineJoinStyle::Round: Formatter::format(builder, "LineJoinStyle::Round"); break; case PDF::LineJoinStyle::Bevel: Formatter::format(builder, "LineJoinStyle::Bevel"); break; } } }; template<> struct Formatter : Formatter { void format(FormatBuilder& format_builder, const PDF::LineDashPattern& pattern) { StringBuilder builder; builder.append("["); bool first = true; for (auto& i : pattern.pattern) { if (!first) builder.append(", "); first = false; builder.appendff("{}", i); } builder.appendff("] {}", pattern.phase); return Formatter::format(format_builder, builder.to_string()); } }; template<> struct Formatter : Formatter { void format(FormatBuilder& builder, const PDF::TextRenderingMode& style) { switch (style) { case PDF::TextRenderingMode::Fill: Formatter::format(builder, "TextRenderingMode::Fill"); break; case PDF::TextRenderingMode::Stroke: Formatter::format(builder, "TextRenderingMode::Stroke"); break; case PDF::TextRenderingMode::FillThenStroke: Formatter::format(builder, "TextRenderingMode::FillThenStroke"); break; case PDF::TextRenderingMode::Invisible: Formatter::format(builder, "TextRenderingMode::Invisible"); break; case PDF::TextRenderingMode::FillAndClip: Formatter::format(builder, "TextRenderingMode::FillAndClip"); break; case PDF::TextRenderingMode::StrokeAndClip: Formatter::format(builder, "TextRenderingMode::StrokeAndClip"); break; case PDF::TextRenderingMode::FillStrokeAndClip: Formatter::format(builder, "TextRenderingMode::FillStrokeAndClip"); break; case PDF::TextRenderingMode::Clip: Formatter::format(builder, "TextRenderingMode::Clip"); break; } } }; template<> struct Formatter : Formatter { void format(FormatBuilder& format_builder, const PDF::TextState& state) { StringBuilder builder; builder.append("TextState {\n"); builder.appendff(" character_spacing={}\n", state.character_spacing); builder.appendff(" word_spacing={}\n", state.word_spacing); builder.appendff(" horizontal_scaling={}\n", state.horizontal_scaling); builder.appendff(" leading={}\n", state.leading); builder.appendff(" font={}\n", state.font ? state.font->name() : ""); builder.appendff(" rendering_mode={}\n", state.rendering_mode); builder.appendff(" rise={}\n", state.rise); builder.appendff(" knockout={}\n", state.knockout); builder.append(" }"); Formatter::format(format_builder, builder.to_string()); } }; template<> struct Formatter : Formatter { void format(FormatBuilder& format_builder, const PDF::GraphicsState& state) { StringBuilder builder; builder.append("GraphicsState {\n"); builder.appendff(" ctm={}\n", state.ctm); builder.appendff(" stroke_color={}\n", state.stroke_color); builder.appendff(" paint_color={}\n", state.paint_color); builder.appendff(" line_width={}\n", state.line_width); builder.appendff(" line_cap_style={}\n", state.line_cap_style); builder.appendff(" line_join_style={}\n", state.line_join_style); builder.appendff(" miter_limit={}\n", state.miter_limit); builder.appendff(" line_dash_pattern={}\n", state.line_dash_pattern); builder.appendff(" text_state={}\n", state.text_state); builder.append("}"); Formatter::format(format_builder, builder.to_string()); } }; }