Renderer.h 8.9 KB

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