Renderer.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright (c) 2021, 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/Bitmap.h>
  10. #include <LibGfx/Font.h>
  11. #include <LibGfx/FontDatabase.h>
  12. #include <LibGfx/Painter.h>
  13. #include <LibGfx/Path.h>
  14. #include <LibGfx/Point.h>
  15. #include <LibGfx/Rect.h>
  16. #include <LibGfx/Size.h>
  17. #include <LibPDF/ColorSpace.h>
  18. #include <LibPDF/Document.h>
  19. #include <LibPDF/Object.h>
  20. namespace PDF {
  21. enum class LineCapStyle : u8 {
  22. ButtCap = 0,
  23. RoundCap = 1,
  24. SquareCap = 2,
  25. };
  26. enum class LineJoinStyle : u8 {
  27. Miter = 0,
  28. Round = 1,
  29. Bevel = 2,
  30. };
  31. struct LineDashPattern {
  32. Vector<int> pattern;
  33. int phase;
  34. };
  35. enum class TextRenderingMode : u8 {
  36. Fill = 0,
  37. Stroke = 1,
  38. FillThenStroke = 2,
  39. Invisible = 3,
  40. FillAndClip = 4,
  41. StrokeAndClip = 5,
  42. FillStrokeAndClip = 6,
  43. Clip = 7,
  44. };
  45. struct TextState {
  46. float character_spacing { 3.0f };
  47. float word_spacing { 5.0f };
  48. float horizontal_scaling { 1.0f };
  49. float leading { 0.0f };
  50. RefPtr<Gfx::Font> font;
  51. TextRenderingMode rendering_mode { TextRenderingMode::Fill };
  52. float rise { 0.0f };
  53. bool knockout { true };
  54. };
  55. struct GraphicsState {
  56. Gfx::AffineTransform ctm;
  57. RefPtr<ColorSpace> stroke_color_space { DeviceGrayColorSpace::the() };
  58. RefPtr<ColorSpace> paint_color_space { DeviceGrayColorSpace::the() };
  59. Gfx::Color stroke_color { Gfx::Color::NamedColor::Black };
  60. Gfx::Color paint_color { Gfx::Color::NamedColor::Black };
  61. float line_width { 1.0f };
  62. LineCapStyle line_cap_style { LineCapStyle::ButtCap };
  63. LineJoinStyle line_join_style { LineJoinStyle::Miter };
  64. float miter_limit { 10.0f };
  65. LineDashPattern line_dash_pattern { {}, 0 };
  66. TextState text_state {};
  67. };
  68. class Renderer {
  69. public:
  70. static void render(Document&, const Page&, RefPtr<Gfx::Bitmap>);
  71. private:
  72. Renderer(RefPtr<Document>, const Page&, RefPtr<Gfx::Bitmap>);
  73. void render();
  74. void handle_command(const Command&);
  75. #define V(name, snake_name, symbol) \
  76. void handle_##snake_name(const Vector<Value>& args);
  77. ENUMERATE_COMMANDS(V)
  78. #undef V
  79. void handle_text_next_line_show_string(const Vector<Value>& args);
  80. void handle_text_next_line_show_string_set_spacing(const Vector<Value>& args);
  81. void set_graphics_state_from_dict(NonnullRefPtr<DictObject>);
  82. // shift is the manual advance given in the TJ command array
  83. void show_text(const String&, float shift = 0.0f);
  84. RefPtr<ColorSpace> get_color_space(const Value&);
  85. ALWAYS_INLINE const GraphicsState& state() const { return m_graphics_state_stack.last(); }
  86. ALWAYS_INLINE GraphicsState& state() { return m_graphics_state_stack.last(); }
  87. ALWAYS_INLINE const TextState& text_state() const { return state().text_state; }
  88. ALWAYS_INLINE TextState& text_state() { return state().text_state; }
  89. template<typename T>
  90. ALWAYS_INLINE Gfx::Point<T> map(T x, T y) const;
  91. template<typename T>
  92. ALWAYS_INLINE Gfx::Size<T> map(Gfx::Size<T>) const;
  93. template<typename T>
  94. ALWAYS_INLINE Gfx::Rect<T> map(Gfx::Rect<T>) const;
  95. const Gfx::AffineTransform& calculate_text_rendering_matrix();
  96. RefPtr<Document> m_document;
  97. RefPtr<Gfx::Bitmap> m_bitmap;
  98. const Page& m_page;
  99. Gfx::Painter m_painter;
  100. Gfx::Path m_current_path;
  101. Vector<GraphicsState> m_graphics_state_stack;
  102. Gfx::AffineTransform m_text_matrix;
  103. Gfx::AffineTransform m_text_line_matrix;
  104. Gfx::AffineTransform m_userspace_matrix;
  105. bool m_text_rendering_matrix_is_dirty { true };
  106. Gfx::AffineTransform m_text_rendering_matrix;
  107. };
  108. }
  109. namespace AK {
  110. template<>
  111. struct Formatter<PDF::LineCapStyle> : Formatter<StringView> {
  112. void format(FormatBuilder& builder, const PDF::LineCapStyle& style)
  113. {
  114. switch (style) {
  115. case PDF::LineCapStyle::ButtCap:
  116. Formatter<StringView>::format(builder, "LineCapStyle::ButtCap");
  117. break;
  118. case PDF::LineCapStyle::RoundCap:
  119. Formatter<StringView>::format(builder, "LineCapStyle::RoundCap");
  120. break;
  121. case PDF::LineCapStyle::SquareCap:
  122. Formatter<StringView>::format(builder, "LineCapStyle::SquareCap");
  123. break;
  124. }
  125. }
  126. };
  127. template<>
  128. struct Formatter<PDF::LineJoinStyle> : Formatter<StringView> {
  129. void format(FormatBuilder& builder, const PDF::LineJoinStyle& style)
  130. {
  131. switch (style) {
  132. case PDF::LineJoinStyle::Miter:
  133. Formatter<StringView>::format(builder, "LineJoinStyle::Miter");
  134. break;
  135. case PDF::LineJoinStyle::Round:
  136. Formatter<StringView>::format(builder, "LineJoinStyle::Round");
  137. break;
  138. case PDF::LineJoinStyle::Bevel:
  139. Formatter<StringView>::format(builder, "LineJoinStyle::Bevel");
  140. break;
  141. }
  142. }
  143. };
  144. template<>
  145. struct Formatter<PDF::LineDashPattern> : Formatter<StringView> {
  146. void format(FormatBuilder& format_builder, const PDF::LineDashPattern& pattern)
  147. {
  148. StringBuilder builder;
  149. builder.append("[");
  150. bool first = true;
  151. for (auto& i : pattern.pattern) {
  152. if (!first)
  153. builder.append(", ");
  154. first = false;
  155. builder.appendff("{}", i);
  156. }
  157. builder.appendff("] {}", pattern.phase);
  158. return Formatter<StringView>::format(format_builder, builder.to_string());
  159. }
  160. };
  161. template<>
  162. struct Formatter<PDF::TextRenderingMode> : Formatter<StringView> {
  163. void format(FormatBuilder& builder, const PDF::TextRenderingMode& style)
  164. {
  165. switch (style) {
  166. case PDF::TextRenderingMode::Fill:
  167. Formatter<StringView>::format(builder, "TextRenderingMode::Fill");
  168. break;
  169. case PDF::TextRenderingMode::Stroke:
  170. Formatter<StringView>::format(builder, "TextRenderingMode::Stroke");
  171. break;
  172. case PDF::TextRenderingMode::FillThenStroke:
  173. Formatter<StringView>::format(builder, "TextRenderingMode::FillThenStroke");
  174. break;
  175. case PDF::TextRenderingMode::Invisible:
  176. Formatter<StringView>::format(builder, "TextRenderingMode::Invisible");
  177. break;
  178. case PDF::TextRenderingMode::FillAndClip:
  179. Formatter<StringView>::format(builder, "TextRenderingMode::FillAndClip");
  180. break;
  181. case PDF::TextRenderingMode::StrokeAndClip:
  182. Formatter<StringView>::format(builder, "TextRenderingMode::StrokeAndClip");
  183. break;
  184. case PDF::TextRenderingMode::FillStrokeAndClip:
  185. Formatter<StringView>::format(builder, "TextRenderingMode::FillStrokeAndClip");
  186. break;
  187. case PDF::TextRenderingMode::Clip:
  188. Formatter<StringView>::format(builder, "TextRenderingMode::Clip");
  189. break;
  190. }
  191. }
  192. };
  193. template<>
  194. struct Formatter<PDF::TextState> : Formatter<StringView> {
  195. void format(FormatBuilder& format_builder, const PDF::TextState& state)
  196. {
  197. StringBuilder builder;
  198. builder.append("TextState {\n");
  199. builder.appendff(" character_spacing={}\n", state.character_spacing);
  200. builder.appendff(" word_spacing={}\n", state.word_spacing);
  201. builder.appendff(" horizontal_scaling={}\n", state.horizontal_scaling);
  202. builder.appendff(" leading={}\n", state.leading);
  203. builder.appendff(" font={}\n", state.font ? state.font->name() : "<null>");
  204. builder.appendff(" rendering_mode={}\n", state.rendering_mode);
  205. builder.appendff(" rise={}\n", state.rise);
  206. builder.appendff(" knockout={}\n", state.knockout);
  207. builder.append(" }");
  208. Formatter<StringView>::format(format_builder, builder.to_string());
  209. }
  210. };
  211. template<>
  212. struct Formatter<PDF::GraphicsState> : Formatter<StringView> {
  213. void format(FormatBuilder& format_builder, const PDF::GraphicsState& state)
  214. {
  215. StringBuilder builder;
  216. builder.append("GraphicsState {\n");
  217. builder.appendff(" ctm={}\n", state.ctm);
  218. builder.appendff(" stroke_color={}\n", state.stroke_color);
  219. builder.appendff(" paint_color={}\n", state.paint_color);
  220. builder.appendff(" line_width={}\n", state.line_width);
  221. builder.appendff(" line_cap_style={}\n", state.line_cap_style);
  222. builder.appendff(" line_join_style={}\n", state.line_join_style);
  223. builder.appendff(" miter_limit={}\n", state.miter_limit);
  224. builder.appendff(" line_dash_pattern={}\n", state.line_dash_pattern);
  225. builder.appendff(" text_state={}\n", state.text_state);
  226. builder.append("}");
  227. Formatter<StringView>::format(format_builder, builder.to_string());
  228. }
  229. };
  230. }