Renderer.h 8.8 KB

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