Renderer.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Copyright (c) 2021-2022, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Format.h>
  8. #include <LibGfx/AffineTransform.h>
  9. #include <LibGfx/AntiAliasingPainter.h>
  10. #include <LibGfx/Bitmap.h>
  11. #include <LibGfx/Font/Font.h>
  12. #include <LibGfx/Font/FontDatabase.h>
  13. #include <LibGfx/Painter.h>
  14. #include <LibGfx/Path.h>
  15. #include <LibGfx/Point.h>
  16. #include <LibGfx/Rect.h>
  17. #include <LibGfx/Size.h>
  18. #include <LibPDF/ColorSpace.h>
  19. #include <LibPDF/Document.h>
  20. #include <LibPDF/Fonts/PDFFont.h>
  21. #include <LibPDF/Object.h>
  22. namespace PDF {
  23. enum class LineCapStyle : u8 {
  24. ButtCap = 0,
  25. RoundCap = 1,
  26. SquareCap = 2,
  27. };
  28. enum class LineJoinStyle : u8 {
  29. Miter = 0,
  30. Round = 1,
  31. Bevel = 2,
  32. };
  33. struct LineDashPattern {
  34. Vector<int> pattern;
  35. int phase;
  36. };
  37. enum class TextRenderingMode : u8 {
  38. Fill = 0,
  39. Stroke = 1,
  40. FillThenStroke = 2,
  41. Invisible = 3,
  42. FillAndClip = 4,
  43. StrokeAndClip = 5,
  44. FillStrokeAndClip = 6,
  45. Clip = 7,
  46. };
  47. struct TextState {
  48. float character_spacing { 0.0f };
  49. float word_spacing { 0.0f };
  50. float horizontal_scaling { 1.0f };
  51. float leading { 0.0f };
  52. float font_size { 12.0f };
  53. RefPtr<PDFFont> font;
  54. TextRenderingMode rendering_mode { TextRenderingMode::Fill };
  55. float rise { 0.0f };
  56. bool knockout { true };
  57. };
  58. struct ClippingPaths {
  59. Gfx::Path current;
  60. Gfx::Path next;
  61. };
  62. struct GraphicsState {
  63. Gfx::AffineTransform ctm;
  64. ClippingPaths clipping_paths;
  65. RefPtr<ColorSpace> stroke_color_space { DeviceGrayColorSpace::the() };
  66. RefPtr<ColorSpace> paint_color_space { DeviceGrayColorSpace::the() };
  67. ColorOrStyle stroke_style { Color::Black };
  68. ColorOrStyle paint_style { Color::Black };
  69. ByteString color_rendering_intent { "RelativeColorimetric"sv };
  70. float flatness_tolerance { 0.0f };
  71. float line_width { 1.0f };
  72. LineCapStyle line_cap_style { LineCapStyle::ButtCap };
  73. LineJoinStyle line_join_style { LineJoinStyle::Miter };
  74. float miter_limit { 10.0f };
  75. LineDashPattern line_dash_pattern { {}, 0 };
  76. TextState text_state {};
  77. };
  78. struct RenderingPreferences {
  79. bool show_clipping_paths { false };
  80. bool show_images { true };
  81. bool clip_images { true };
  82. bool clip_paths { true };
  83. bool clip_text { true };
  84. unsigned hash() const
  85. {
  86. return static_cast<unsigned>(show_clipping_paths) | static_cast<unsigned>(show_images) << 1;
  87. }
  88. };
  89. class Renderer {
  90. public:
  91. static PDFErrorsOr<void> render(Document&, Page const&, RefPtr<Gfx::Bitmap>, Color background_color, RenderingPreferences preferences);
  92. struct FontCacheKey {
  93. NonnullRefPtr<DictObject> font_dictionary;
  94. float font_size;
  95. bool operator==(FontCacheKey const&) const = default;
  96. };
  97. ALWAYS_INLINE GraphicsState const& state() const { return m_graphics_state_stack.last(); }
  98. ALWAYS_INLINE TextState const& text_state() const { return state().text_state; }
  99. Gfx::AffineTransform const& calculate_text_rendering_matrix() const;
  100. PDFErrorOr<void> render_type3_glyph(Gfx::FloatPoint, StreamObject const&, Gfx::AffineTransform const&, Optional<NonnullRefPtr<DictObject>>);
  101. private:
  102. Renderer(RefPtr<Document>, Page const&, RefPtr<Gfx::Bitmap>, Color background_color, RenderingPreferences);
  103. PDFErrorsOr<void> render();
  104. PDFErrorOr<void> handle_operator(Operator const&, Optional<NonnullRefPtr<DictObject>> = {});
  105. #define V(name, snake_name, symbol) \
  106. PDFErrorOr<void> handle_##snake_name(ReadonlySpan<Value> args, Optional<NonnullRefPtr<DictObject>> = {});
  107. ENUMERATE_OPERATORS(V)
  108. #undef V
  109. PDFErrorOr<void> handle_text_next_line_show_string(ReadonlySpan<Value> args, Optional<NonnullRefPtr<DictObject>> = {});
  110. PDFErrorOr<void> handle_text_next_line_show_string_set_spacing(ReadonlySpan<Value> args, Optional<NonnullRefPtr<DictObject>> = {});
  111. class ClipRAII {
  112. public:
  113. ClipRAII(Renderer& renderer)
  114. : m_renderer(renderer)
  115. {
  116. m_renderer.activate_clip();
  117. }
  118. ~ClipRAII() { m_renderer.deactivate_clip(); }
  119. private:
  120. Renderer& m_renderer;
  121. };
  122. void activate_clip();
  123. void deactivate_clip();
  124. void begin_path_paint();
  125. void end_path_paint();
  126. PDFErrorOr<void> set_graphics_state_from_dict(NonnullRefPtr<DictObject>);
  127. PDFErrorOr<void> show_text(ByteString const&);
  128. struct LoadedImage {
  129. NonnullRefPtr<Gfx::Bitmap> bitmap;
  130. bool is_image_mask = false;
  131. };
  132. PDFErrorOr<LoadedImage> load_image(NonnullRefPtr<StreamObject>);
  133. PDFErrorOr<void> show_image(NonnullRefPtr<StreamObject>);
  134. void show_empty_image(int width, int height);
  135. PDFErrorOr<NonnullRefPtr<ColorSpace>> get_color_space_from_resources(Value const&, NonnullRefPtr<DictObject>);
  136. PDFErrorOr<NonnullRefPtr<ColorSpace>> get_color_space_from_document(NonnullRefPtr<Object>);
  137. ALWAYS_INLINE GraphicsState& state() { return m_graphics_state_stack.last(); }
  138. ALWAYS_INLINE TextState& text_state() { return state().text_state; }
  139. template<typename T>
  140. ALWAYS_INLINE Gfx::Point<T> map(T x, T y) const;
  141. template<typename T>
  142. ALWAYS_INLINE Gfx::Size<T> map(Gfx::Size<T>) const;
  143. template<typename T>
  144. ALWAYS_INLINE Gfx::Rect<T> map(Gfx::Rect<T>) const;
  145. Gfx::Path map(Gfx::Path const&) const;
  146. float line_width() const;
  147. Gfx::AffineTransform calculate_image_space_transformation(int width, int height);
  148. PDFErrorOr<NonnullRefPtr<PDFFont>> get_font(FontCacheKey const&);
  149. class ScopedState;
  150. RefPtr<Document> m_document;
  151. RefPtr<Gfx::Bitmap> m_bitmap;
  152. Page const& m_page;
  153. Gfx::Painter m_painter;
  154. Gfx::AntiAliasingPainter m_anti_aliasing_painter;
  155. RenderingPreferences m_rendering_preferences;
  156. Gfx::Path m_current_path;
  157. Vector<GraphicsState> m_graphics_state_stack;
  158. Gfx::AffineTransform m_text_matrix;
  159. Gfx::AffineTransform m_text_line_matrix;
  160. bool mutable m_text_rendering_matrix_is_dirty { true };
  161. Gfx::AffineTransform mutable m_text_rendering_matrix;
  162. HashMap<FontCacheKey, NonnullRefPtr<PDFFont>> m_font_cache;
  163. };
  164. }
  165. namespace AK {
  166. template<>
  167. struct Traits<PDF::Renderer::FontCacheKey> : public DefaultTraits<PDF::Renderer::FontCacheKey> {
  168. static unsigned hash(PDF::Renderer::FontCacheKey const& key)
  169. {
  170. return pair_int_hash(ptr_hash(key.font_dictionary.ptr()), int_hash(bit_cast<u32>(key.font_size)));
  171. }
  172. };
  173. template<>
  174. struct Formatter<PDF::LineCapStyle> : Formatter<StringView> {
  175. ErrorOr<void> format(FormatBuilder& builder, PDF::LineCapStyle const& style)
  176. {
  177. switch (style) {
  178. case PDF::LineCapStyle::ButtCap:
  179. return builder.put_string("LineCapStyle::ButtCap"sv);
  180. case PDF::LineCapStyle::RoundCap:
  181. return builder.put_string("LineCapStyle::RoundCap"sv);
  182. case PDF::LineCapStyle::SquareCap:
  183. return builder.put_string("LineCapStyle::SquareCap"sv);
  184. }
  185. }
  186. };
  187. template<>
  188. struct Formatter<PDF::LineJoinStyle> : Formatter<StringView> {
  189. ErrorOr<void> format(FormatBuilder& builder, PDF::LineJoinStyle const& style)
  190. {
  191. switch (style) {
  192. case PDF::LineJoinStyle::Miter:
  193. return builder.put_string("LineJoinStyle::Miter"sv);
  194. case PDF::LineJoinStyle::Round:
  195. return builder.put_string("LineJoinStyle::Round"sv);
  196. case PDF::LineJoinStyle::Bevel:
  197. return builder.put_string("LineJoinStyle::Bevel"sv);
  198. }
  199. }
  200. };
  201. template<>
  202. struct Formatter<PDF::LineDashPattern> : Formatter<StringView> {
  203. ErrorOr<void> format(FormatBuilder& format_builder, PDF::LineDashPattern const& pattern)
  204. {
  205. StringBuilder builder;
  206. builder.append('[');
  207. bool first = true;
  208. for (auto& i : pattern.pattern) {
  209. if (!first)
  210. builder.append(", "sv);
  211. first = false;
  212. builder.appendff("{}", i);
  213. }
  214. builder.appendff("] {}", pattern.phase);
  215. return format_builder.put_string(builder.to_byte_string());
  216. }
  217. };
  218. template<>
  219. struct Formatter<PDF::TextRenderingMode> : Formatter<StringView> {
  220. ErrorOr<void> format(FormatBuilder& builder, PDF::TextRenderingMode const& style)
  221. {
  222. switch (style) {
  223. case PDF::TextRenderingMode::Fill:
  224. return builder.put_string("TextRenderingMode::Fill"sv);
  225. case PDF::TextRenderingMode::Stroke:
  226. return builder.put_string("TextRenderingMode::Stroke"sv);
  227. case PDF::TextRenderingMode::FillThenStroke:
  228. return builder.put_string("TextRenderingMode::FillThenStroke"sv);
  229. case PDF::TextRenderingMode::Invisible:
  230. return builder.put_string("TextRenderingMode::Invisible"sv);
  231. case PDF::TextRenderingMode::FillAndClip:
  232. return builder.put_string("TextRenderingMode::FillAndClip"sv);
  233. case PDF::TextRenderingMode::StrokeAndClip:
  234. return builder.put_string("TextRenderingMode::StrokeAndClip"sv);
  235. case PDF::TextRenderingMode::FillStrokeAndClip:
  236. return builder.put_string("TextRenderingMode::FillStrokeAndClip"sv);
  237. case PDF::TextRenderingMode::Clip:
  238. return builder.put_string("TextRenderingMode::Clip"sv);
  239. }
  240. }
  241. };
  242. template<>
  243. struct Formatter<PDF::TextState> : Formatter<StringView> {
  244. ErrorOr<void> format(FormatBuilder& format_builder, PDF::TextState const& state)
  245. {
  246. StringBuilder builder;
  247. builder.append("TextState {\n"sv);
  248. builder.appendff(" character_spacing={}\n", state.character_spacing);
  249. builder.appendff(" word_spacing={}\n", state.word_spacing);
  250. builder.appendff(" horizontal_scaling={}\n", state.horizontal_scaling);
  251. builder.appendff(" leading={}\n", state.leading);
  252. builder.appendff(" font_size={}\n", state.font_size);
  253. builder.appendff(" rendering_mode={}\n", state.rendering_mode);
  254. builder.appendff(" rise={}\n", state.rise);
  255. builder.appendff(" knockout={}\n", state.knockout);
  256. builder.append(" }"sv);
  257. return format_builder.put_string(builder.to_byte_string());
  258. }
  259. };
  260. template<>
  261. struct Formatter<PDF::GraphicsState> : Formatter<StringView> {
  262. ErrorOr<void> format(FormatBuilder& format_builder, PDF::GraphicsState const& state)
  263. {
  264. StringBuilder builder;
  265. builder.append("GraphicsState {\n"sv);
  266. builder.appendff(" ctm={}\n", state.ctm);
  267. if (state.stroke_style.has<Color>()) {
  268. builder.appendff(" stroke_style={}\n", state.stroke_style.get<Color>());
  269. } else {
  270. builder.appendff(" stroke_style={}\n", state.stroke_style.get<NonnullRefPtr<Gfx::PaintStyle>>());
  271. }
  272. if (state.paint_style.has<Color>()) {
  273. builder.appendff(" paint_style={}\n", state.paint_style.get<Color>());
  274. } else {
  275. builder.appendff(" paint_style={}\n", state.paint_style.get<NonnullRefPtr<Gfx::PaintStyle>>());
  276. }
  277. builder.appendff(" color_rendering_intent={}\n", state.color_rendering_intent);
  278. builder.appendff(" flatness_tolerance={}\n", state.flatness_tolerance);
  279. builder.appendff(" line_width={}\n", state.line_width);
  280. builder.appendff(" line_cap_style={}\n", state.line_cap_style);
  281. builder.appendff(" line_join_style={}\n", state.line_join_style);
  282. builder.appendff(" miter_limit={}\n", state.miter_limit);
  283. builder.appendff(" line_dash_pattern={}\n", state.line_dash_pattern);
  284. builder.appendff(" text_state={}\n", state.text_state);
  285. builder.append('}');
  286. return format_builder.put_string(builder.to_byte_string());
  287. }
  288. };
  289. }