Renderer.h 8.8 KB

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