Renderer.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. Gfx::Color stroke_color { Gfx::Color::NamedColor::Black };
  68. Gfx::Color paint_color { Gfx::Color::NamedColor::Black };
  69. float line_width { 1.0f };
  70. LineCapStyle line_cap_style { LineCapStyle::ButtCap };
  71. LineJoinStyle line_join_style { LineJoinStyle::Miter };
  72. float miter_limit { 10.0f };
  73. LineDashPattern line_dash_pattern { {}, 0 };
  74. TextState text_state {};
  75. };
  76. struct RenderingPreferences {
  77. bool show_clipping_paths { false };
  78. bool show_images { true };
  79. unsigned hash() const
  80. {
  81. return static_cast<unsigned>(show_clipping_paths) | static_cast<unsigned>(show_images) << 1;
  82. }
  83. };
  84. class Renderer {
  85. public:
  86. static PDFErrorsOr<void> render(Document&, Page const&, RefPtr<Gfx::Bitmap>, RenderingPreferences preferences);
  87. private:
  88. Renderer(RefPtr<Document>, Page const&, RefPtr<Gfx::Bitmap>, RenderingPreferences);
  89. PDFErrorsOr<void> render();
  90. PDFErrorOr<void> handle_operator(Operator const&, Optional<NonnullRefPtr<DictObject>> = {});
  91. #define V(name, snake_name, symbol) \
  92. PDFErrorOr<void> handle_##snake_name(Vector<Value> const& args, Optional<NonnullRefPtr<DictObject>> = {});
  93. ENUMERATE_OPERATORS(V)
  94. #undef V
  95. PDFErrorOr<void> handle_text_next_line_show_string(Vector<Value> const& args, Optional<NonnullRefPtr<DictObject>> = {});
  96. PDFErrorOr<void> handle_text_next_line_show_string_set_spacing(Vector<Value> const& args, Optional<NonnullRefPtr<DictObject>> = {});
  97. void begin_path_paint();
  98. void end_path_paint();
  99. PDFErrorOr<void> set_graphics_state_from_dict(NonnullRefPtr<DictObject>);
  100. PDFErrorOr<void> show_text(DeprecatedString const&);
  101. PDFErrorOr<NonnullRefPtr<Gfx::Bitmap>> load_image(NonnullRefPtr<StreamObject>);
  102. PDFErrorOr<void> show_image(NonnullRefPtr<StreamObject>);
  103. void show_empty_image(int width, int height);
  104. PDFErrorOr<NonnullRefPtr<ColorSpace>> get_color_space_from_resources(Value const&, NonnullRefPtr<DictObject>);
  105. PDFErrorOr<NonnullRefPtr<ColorSpace>> get_color_space_from_document(NonnullRefPtr<Object>);
  106. ALWAYS_INLINE GraphicsState const& state() const { return m_graphics_state_stack.last(); }
  107. ALWAYS_INLINE GraphicsState& state() { return m_graphics_state_stack.last(); }
  108. ALWAYS_INLINE TextState const& text_state() const { return state().text_state; }
  109. ALWAYS_INLINE TextState& text_state() { return state().text_state; }
  110. template<typename T>
  111. ALWAYS_INLINE Gfx::Point<T> map(T x, T y) const;
  112. template<typename T>
  113. ALWAYS_INLINE Gfx::Size<T> map(Gfx::Size<T>) const;
  114. template<typename T>
  115. ALWAYS_INLINE Gfx::Rect<T> map(Gfx::Rect<T>) const;
  116. Gfx::AffineTransform const& calculate_text_rendering_matrix();
  117. Gfx::AffineTransform calculate_image_space_transformation(int width, int height);
  118. RefPtr<Document> m_document;
  119. RefPtr<Gfx::Bitmap> m_bitmap;
  120. Page const& m_page;
  121. Gfx::Painter m_painter;
  122. Gfx::AntiAliasingPainter m_anti_aliasing_painter;
  123. RenderingPreferences m_rendering_preferences;
  124. Gfx::Path m_current_path;
  125. Vector<GraphicsState> m_graphics_state_stack;
  126. Gfx::AffineTransform m_text_matrix;
  127. Gfx::AffineTransform m_text_line_matrix;
  128. bool m_text_rendering_matrix_is_dirty { true };
  129. Gfx::AffineTransform m_text_rendering_matrix;
  130. };
  131. }
  132. namespace AK {
  133. template<>
  134. struct Formatter<PDF::LineCapStyle> : Formatter<StringView> {
  135. ErrorOr<void> format(FormatBuilder& builder, PDF::LineCapStyle const& style)
  136. {
  137. switch (style) {
  138. case PDF::LineCapStyle::ButtCap:
  139. return builder.put_string("LineCapStyle::ButtCap"sv);
  140. case PDF::LineCapStyle::RoundCap:
  141. return builder.put_string("LineCapStyle::RoundCap"sv);
  142. case PDF::LineCapStyle::SquareCap:
  143. return builder.put_string("LineCapStyle::SquareCap"sv);
  144. }
  145. }
  146. };
  147. template<>
  148. struct Formatter<PDF::LineJoinStyle> : Formatter<StringView> {
  149. ErrorOr<void> format(FormatBuilder& builder, PDF::LineJoinStyle const& style)
  150. {
  151. switch (style) {
  152. case PDF::LineJoinStyle::Miter:
  153. return builder.put_string("LineJoinStyle::Miter"sv);
  154. case PDF::LineJoinStyle::Round:
  155. return builder.put_string("LineJoinStyle::Round"sv);
  156. case PDF::LineJoinStyle::Bevel:
  157. return builder.put_string("LineJoinStyle::Bevel"sv);
  158. }
  159. }
  160. };
  161. template<>
  162. struct Formatter<PDF::LineDashPattern> : Formatter<StringView> {
  163. ErrorOr<void> format(FormatBuilder& format_builder, PDF::LineDashPattern const& pattern)
  164. {
  165. StringBuilder builder;
  166. builder.append('[');
  167. bool first = true;
  168. for (auto& i : pattern.pattern) {
  169. if (!first)
  170. builder.append(", "sv);
  171. first = false;
  172. builder.appendff("{}", i);
  173. }
  174. builder.appendff("] {}", pattern.phase);
  175. return format_builder.put_string(builder.to_deprecated_string());
  176. }
  177. };
  178. template<>
  179. struct Formatter<PDF::TextRenderingMode> : Formatter<StringView> {
  180. ErrorOr<void> format(FormatBuilder& builder, PDF::TextRenderingMode const& style)
  181. {
  182. switch (style) {
  183. case PDF::TextRenderingMode::Fill:
  184. return builder.put_string("TextRenderingMode::Fill"sv);
  185. case PDF::TextRenderingMode::Stroke:
  186. return builder.put_string("TextRenderingMode::Stroke"sv);
  187. case PDF::TextRenderingMode::FillThenStroke:
  188. return builder.put_string("TextRenderingMode::FillThenStroke"sv);
  189. case PDF::TextRenderingMode::Invisible:
  190. return builder.put_string("TextRenderingMode::Invisible"sv);
  191. case PDF::TextRenderingMode::FillAndClip:
  192. return builder.put_string("TextRenderingMode::FillAndClip"sv);
  193. case PDF::TextRenderingMode::StrokeAndClip:
  194. return builder.put_string("TextRenderingMode::StrokeAndClip"sv);
  195. case PDF::TextRenderingMode::FillStrokeAndClip:
  196. return builder.put_string("TextRenderingMode::FillStrokeAndClip"sv);
  197. case PDF::TextRenderingMode::Clip:
  198. return builder.put_string("TextRenderingMode::Clip"sv);
  199. }
  200. }
  201. };
  202. template<>
  203. struct Formatter<PDF::TextState> : Formatter<StringView> {
  204. ErrorOr<void> format(FormatBuilder& format_builder, PDF::TextState const& state)
  205. {
  206. StringBuilder builder;
  207. builder.append("TextState {\n"sv);
  208. builder.appendff(" character_spacing={}\n", state.character_spacing);
  209. builder.appendff(" word_spacing={}\n", state.word_spacing);
  210. builder.appendff(" horizontal_scaling={}\n", state.horizontal_scaling);
  211. builder.appendff(" leading={}\n", state.leading);
  212. builder.appendff(" font_size={}\n", state.font_size);
  213. builder.appendff(" rendering_mode={}\n", state.rendering_mode);
  214. builder.appendff(" rise={}\n", state.rise);
  215. builder.appendff(" knockout={}\n", state.knockout);
  216. builder.append(" }"sv);
  217. return format_builder.put_string(builder.to_deprecated_string());
  218. }
  219. };
  220. template<>
  221. struct Formatter<PDF::GraphicsState> : Formatter<StringView> {
  222. ErrorOr<void> format(FormatBuilder& format_builder, PDF::GraphicsState const& state)
  223. {
  224. StringBuilder builder;
  225. builder.append("GraphicsState {\n"sv);
  226. builder.appendff(" ctm={}\n", state.ctm);
  227. builder.appendff(" stroke_color={}\n", state.stroke_color);
  228. builder.appendff(" paint_color={}\n", state.paint_color);
  229. builder.appendff(" line_width={}\n", state.line_width);
  230. builder.appendff(" line_cap_style={}\n", state.line_cap_style);
  231. builder.appendff(" line_join_style={}\n", state.line_join_style);
  232. builder.appendff(" miter_limit={}\n", state.miter_limit);
  233. builder.appendff(" line_dash_pattern={}\n", state.line_dash_pattern);
  234. builder.appendff(" text_state={}\n", state.text_state);
  235. builder.append('}');
  236. return format_builder.put_string(builder.to_deprecated_string());
  237. }
  238. };
  239. }