Renderer.h 8.1 KB

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