/* * Copyright (c) 2021-2022, Matthew Olsson * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include 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 { 0.0f }; float word_spacing { 0.0f }; float horizontal_scaling { 1.0f }; float leading { 0.0f }; float font_size { 12.0f }; RefPtr font; TextRenderingMode rendering_mode { TextRenderingMode::Fill }; float rise { 0.0f }; bool knockout { true }; }; struct ClippingPaths { Gfx::Path current; Gfx::Path next; }; struct GraphicsState { Gfx::AffineTransform ctm; ClippingPaths clipping_paths; RefPtr stroke_color_space { DeviceGrayColorSpace::the() }; RefPtr paint_color_space { DeviceGrayColorSpace::the() }; ColorOrStyle stroke_style { Color::Black }; ColorOrStyle paint_style { Color::Black }; ByteString color_rendering_intent { "RelativeColorimetric"sv }; float flatness_tolerance { 0.0f }; 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 {}; }; struct RenderingPreferences { bool show_clipping_paths { false }; bool show_images { true }; bool clip_images { true }; bool clip_paths { true }; bool clip_text { true }; unsigned hash() const { return static_cast(show_clipping_paths) | static_cast(show_images) << 1; } }; class Renderer { public: static PDFErrorsOr render(Document&, Page const&, RefPtr, Color background_color, RenderingPreferences preferences); struct FontCacheKey { NonnullRefPtr font_dictionary; float font_size; bool operator==(FontCacheKey const&) const = default; }; ALWAYS_INLINE GraphicsState const& state() const { return m_graphics_state_stack.last(); } ALWAYS_INLINE TextState const& text_state() const { return state().text_state; } Gfx::AffineTransform const& calculate_text_rendering_matrix() const; PDFErrorOr render_type3_glyph(Gfx::FloatPoint, StreamObject const&, Gfx::AffineTransform const&, Optional>); private: Renderer(RefPtr, Page const&, RefPtr, Color background_color, RenderingPreferences); PDFErrorsOr render(); PDFErrorOr handle_operator(Operator const&, Optional> = {}); #define V(name, snake_name, symbol) \ PDFErrorOr handle_##snake_name(ReadonlySpan args, Optional> = {}); ENUMERATE_OPERATORS(V) #undef V PDFErrorOr handle_text_next_line_show_string(ReadonlySpan args, Optional> = {}); PDFErrorOr handle_text_next_line_show_string_set_spacing(ReadonlySpan args, Optional> = {}); class ClipRAII { public: ClipRAII(Renderer& renderer) : m_renderer(renderer) { m_renderer.activate_clip(); } ~ClipRAII() { m_renderer.deactivate_clip(); } private: Renderer& m_renderer; }; void activate_clip(); void deactivate_clip(); void begin_path_paint(); void end_path_paint(); PDFErrorOr set_graphics_state_from_dict(NonnullRefPtr); PDFErrorOr show_text(ByteString const&); struct LoadedImage { NonnullRefPtr bitmap; bool is_image_mask = false; }; PDFErrorOr load_image(NonnullRefPtr); PDFErrorOr show_image(NonnullRefPtr); void show_empty_image(int width, int height); PDFErrorOr> get_color_space_from_resources(Value const&, NonnullRefPtr); PDFErrorOr> get_color_space_from_document(NonnullRefPtr); ALWAYS_INLINE GraphicsState& state() { return m_graphics_state_stack.last(); } 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; Gfx::Path map(Gfx::Path const&) const; float line_width() const; Gfx::AffineTransform calculate_image_space_transformation(int width, int height); PDFErrorOr> get_font(FontCacheKey const&); class ScopedState; RefPtr m_document; RefPtr m_bitmap; Page const& m_page; Gfx::Painter m_painter; Gfx::AntiAliasingPainter m_anti_aliasing_painter; RenderingPreferences m_rendering_preferences; Gfx::Path m_current_path; Vector m_graphics_state_stack; Gfx::AffineTransform m_text_matrix; Gfx::AffineTransform m_text_line_matrix; bool mutable m_text_rendering_matrix_is_dirty { true }; Gfx::AffineTransform mutable m_text_rendering_matrix; HashMap> m_font_cache; }; } namespace AK { template<> struct Traits : public DefaultTraits { static unsigned hash(PDF::Renderer::FontCacheKey const& key) { return pair_int_hash(ptr_hash(key.font_dictionary.ptr()), int_hash(bit_cast(key.font_size))); } }; template<> struct Formatter : Formatter { ErrorOr format(FormatBuilder& builder, PDF::LineCapStyle const& style) { switch (style) { case PDF::LineCapStyle::ButtCap: return builder.put_string("LineCapStyle::ButtCap"sv); case PDF::LineCapStyle::RoundCap: return builder.put_string("LineCapStyle::RoundCap"sv); case PDF::LineCapStyle::SquareCap: return builder.put_string("LineCapStyle::SquareCap"sv); } } }; template<> struct Formatter : Formatter { ErrorOr format(FormatBuilder& builder, PDF::LineJoinStyle const& style) { switch (style) { case PDF::LineJoinStyle::Miter: return builder.put_string("LineJoinStyle::Miter"sv); case PDF::LineJoinStyle::Round: return builder.put_string("LineJoinStyle::Round"sv); case PDF::LineJoinStyle::Bevel: return builder.put_string("LineJoinStyle::Bevel"sv); } } }; template<> struct Formatter : Formatter { ErrorOr format(FormatBuilder& format_builder, PDF::LineDashPattern const& pattern) { StringBuilder builder; builder.append('['); bool first = true; for (auto& i : pattern.pattern) { if (!first) builder.append(", "sv); first = false; builder.appendff("{}", i); } builder.appendff("] {}", pattern.phase); return format_builder.put_string(builder.to_byte_string()); } }; template<> struct Formatter : Formatter { ErrorOr format(FormatBuilder& builder, PDF::TextRenderingMode const& style) { switch (style) { case PDF::TextRenderingMode::Fill: return builder.put_string("TextRenderingMode::Fill"sv); case PDF::TextRenderingMode::Stroke: return builder.put_string("TextRenderingMode::Stroke"sv); case PDF::TextRenderingMode::FillThenStroke: return builder.put_string("TextRenderingMode::FillThenStroke"sv); case PDF::TextRenderingMode::Invisible: return builder.put_string("TextRenderingMode::Invisible"sv); case PDF::TextRenderingMode::FillAndClip: return builder.put_string("TextRenderingMode::FillAndClip"sv); case PDF::TextRenderingMode::StrokeAndClip: return builder.put_string("TextRenderingMode::StrokeAndClip"sv); case PDF::TextRenderingMode::FillStrokeAndClip: return builder.put_string("TextRenderingMode::FillStrokeAndClip"sv); case PDF::TextRenderingMode::Clip: return builder.put_string("TextRenderingMode::Clip"sv); } } }; template<> struct Formatter : Formatter { ErrorOr format(FormatBuilder& format_builder, PDF::TextState const& state) { StringBuilder builder; builder.append("TextState {\n"sv); 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_size={}\n", state.font_size); builder.appendff(" rendering_mode={}\n", state.rendering_mode); builder.appendff(" rise={}\n", state.rise); builder.appendff(" knockout={}\n", state.knockout); builder.append(" }"sv); return format_builder.put_string(builder.to_byte_string()); } }; template<> struct Formatter : Formatter { ErrorOr format(FormatBuilder& format_builder, PDF::GraphicsState const& state) { StringBuilder builder; builder.append("GraphicsState {\n"sv); builder.appendff(" ctm={}\n", state.ctm); if (state.stroke_style.has()) { builder.appendff(" stroke_style={}\n", state.stroke_style.get()); } else { builder.appendff(" stroke_style={}\n", state.stroke_style.get>()); } if (state.paint_style.has()) { builder.appendff(" paint_style={}\n", state.paint_style.get()); } else { builder.appendff(" paint_style={}\n", state.paint_style.get>()); } builder.appendff(" color_rendering_intent={}\n", state.color_rendering_intent); builder.appendff(" flatness_tolerance={}\n", state.flatness_tolerance); 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('}'); return format_builder.put_string(builder.to_byte_string()); } }; }