Renderer.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. ErrorOr<void> format(FormatBuilder& builder, PDF::LineCapStyle const& style)
  114. {
  115. switch (style) {
  116. case PDF::LineCapStyle::ButtCap:
  117. return Formatter<StringView>::format(builder, "LineCapStyle::ButtCap");
  118. case PDF::LineCapStyle::RoundCap:
  119. return Formatter<StringView>::format(builder, "LineCapStyle::RoundCap");
  120. case PDF::LineCapStyle::SquareCap:
  121. return Formatter<StringView>::format(builder, "LineCapStyle::SquareCap");
  122. }
  123. }
  124. };
  125. template<>
  126. struct Formatter<PDF::LineJoinStyle> : Formatter<StringView> {
  127. ErrorOr<void> format(FormatBuilder& builder, PDF::LineJoinStyle const& style)
  128. {
  129. switch (style) {
  130. case PDF::LineJoinStyle::Miter:
  131. return Formatter<StringView>::format(builder, "LineJoinStyle::Miter");
  132. case PDF::LineJoinStyle::Round:
  133. return Formatter<StringView>::format(builder, "LineJoinStyle::Round");
  134. case PDF::LineJoinStyle::Bevel:
  135. return Formatter<StringView>::format(builder, "LineJoinStyle::Bevel");
  136. }
  137. }
  138. };
  139. template<>
  140. struct Formatter<PDF::LineDashPattern> : Formatter<StringView> {
  141. ErrorOr<void> format(FormatBuilder& format_builder, PDF::LineDashPattern const& pattern)
  142. {
  143. StringBuilder builder;
  144. builder.append("[");
  145. bool first = true;
  146. for (auto& i : pattern.pattern) {
  147. if (!first)
  148. builder.append(", ");
  149. first = false;
  150. builder.appendff("{}", i);
  151. }
  152. builder.appendff("] {}", pattern.phase);
  153. return Formatter<StringView>::format(format_builder, builder.to_string());
  154. }
  155. };
  156. template<>
  157. struct Formatter<PDF::TextRenderingMode> : Formatter<StringView> {
  158. ErrorOr<void> format(FormatBuilder& builder, PDF::TextRenderingMode const& style)
  159. {
  160. switch (style) {
  161. case PDF::TextRenderingMode::Fill:
  162. return Formatter<StringView>::format(builder, "TextRenderingMode::Fill");
  163. case PDF::TextRenderingMode::Stroke:
  164. return Formatter<StringView>::format(builder, "TextRenderingMode::Stroke");
  165. case PDF::TextRenderingMode::FillThenStroke:
  166. return Formatter<StringView>::format(builder, "TextRenderingMode::FillThenStroke");
  167. case PDF::TextRenderingMode::Invisible:
  168. return Formatter<StringView>::format(builder, "TextRenderingMode::Invisible");
  169. case PDF::TextRenderingMode::FillAndClip:
  170. return Formatter<StringView>::format(builder, "TextRenderingMode::FillAndClip");
  171. case PDF::TextRenderingMode::StrokeAndClip:
  172. return Formatter<StringView>::format(builder, "TextRenderingMode::StrokeAndClip");
  173. case PDF::TextRenderingMode::FillStrokeAndClip:
  174. return Formatter<StringView>::format(builder, "TextRenderingMode::FillStrokeAndClip");
  175. case PDF::TextRenderingMode::Clip:
  176. return Formatter<StringView>::format(builder, "TextRenderingMode::Clip");
  177. }
  178. }
  179. };
  180. template<>
  181. struct Formatter<PDF::TextState> : Formatter<StringView> {
  182. ErrorOr<void> format(FormatBuilder& format_builder, PDF::TextState const& state)
  183. {
  184. StringBuilder builder;
  185. builder.append("TextState {\n");
  186. builder.appendff(" character_spacing={}\n", state.character_spacing);
  187. builder.appendff(" word_spacing={}\n", state.word_spacing);
  188. builder.appendff(" horizontal_scaling={}\n", state.horizontal_scaling);
  189. builder.appendff(" leading={}\n", state.leading);
  190. builder.appendff(" font_family={}\n", state.font_family);
  191. builder.appendff(" font_variant={}\n", state.font_variant);
  192. builder.appendff(" font_size={}\n", state.font_size);
  193. builder.appendff(" rendering_mode={}\n", state.rendering_mode);
  194. builder.appendff(" rise={}\n", state.rise);
  195. builder.appendff(" knockout={}\n", state.knockout);
  196. builder.append(" }");
  197. return Formatter<StringView>::format(format_builder, builder.to_string());
  198. }
  199. };
  200. template<>
  201. struct Formatter<PDF::GraphicsState> : Formatter<StringView> {
  202. ErrorOr<void> format(FormatBuilder& format_builder, PDF::GraphicsState const& state)
  203. {
  204. StringBuilder builder;
  205. builder.append("GraphicsState {\n");
  206. builder.appendff(" ctm={}\n", state.ctm);
  207. builder.appendff(" stroke_color={}\n", state.stroke_color);
  208. builder.appendff(" paint_color={}\n", state.paint_color);
  209. builder.appendff(" line_width={}\n", state.line_width);
  210. builder.appendff(" line_cap_style={}\n", state.line_cap_style);
  211. builder.appendff(" line_join_style={}\n", state.line_join_style);
  212. builder.appendff(" miter_limit={}\n", state.miter_limit);
  213. builder.appendff(" line_dash_pattern={}\n", state.line_dash_pattern);
  214. builder.appendff(" text_state={}\n", state.text_state);
  215. builder.append("}");
  216. return Formatter<StringView>::format(format_builder, builder.to_string());
  217. }
  218. };
  219. }