Renderer.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. * Copyright (c) 2021, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Utf8View.h>
  7. #include <LibPDF/Renderer.h>
  8. #include <ctype.h>
  9. #include <math.h>
  10. #define RENDERER_HANDLER(name) \
  11. void Renderer::handle_##name([[maybe_unused]] const Vector<Value>& args)
  12. #define RENDERER_TODO(name) \
  13. RENDERER_HANDLER(name) \
  14. { \
  15. dbgln("[PDF::Renderer] Unsupported draw operation " #name); \
  16. TODO(); \
  17. }
  18. namespace PDF {
  19. void Renderer::render(Document& document, const Page& page, RefPtr<Gfx::Bitmap> bitmap)
  20. {
  21. Renderer(document, page, bitmap).render();
  22. }
  23. Renderer::Renderer(RefPtr<Document> document, const Page& page, RefPtr<Gfx::Bitmap> bitmap)
  24. : m_document(document)
  25. , m_bitmap(bitmap)
  26. , m_page(page)
  27. , m_painter(*bitmap)
  28. {
  29. auto media_box = m_page.media_box;
  30. m_userspace_matrix.translate(media_box.lower_left_x, media_box.lower_left_y);
  31. float width = media_box.upper_right_x - media_box.lower_left_x;
  32. float height = media_box.upper_right_y - media_box.lower_left_y;
  33. float scale_x = static_cast<float>(bitmap->width()) / width;
  34. float scale_y = static_cast<float>(bitmap->height()) / height;
  35. m_userspace_matrix.scale(scale_x, scale_y);
  36. m_graphics_state_stack.append(GraphicsState { m_userspace_matrix });
  37. m_bitmap->fill(Gfx::Color::NamedColor::White);
  38. }
  39. void Renderer::render()
  40. {
  41. // Use our own vector, as the /Content can be an array with multiple
  42. // streams which gets concatenated
  43. // FIXME: Text operators are supposed to only have effects on the current
  44. // stream object. Do the text operators treat this concatenated stream
  45. // as one stream or multiple?
  46. ByteBuffer byte_buffer;
  47. if (m_page.contents->is_array()) {
  48. auto contents = object_cast<ArrayObject>(m_page.contents);
  49. for (auto& ref : *contents) {
  50. auto bytes = m_document->resolve_to<StreamObject>(ref)->bytes();
  51. byte_buffer.append(bytes.data(), bytes.size());
  52. }
  53. } else {
  54. VERIFY(m_page.contents->is_stream());
  55. auto bytes = object_cast<StreamObject>(m_page.contents)->bytes();
  56. byte_buffer.append(bytes.data(), bytes.size());
  57. }
  58. auto commands = Parser::parse_graphics_commands(byte_buffer);
  59. for (auto& command : commands)
  60. handle_command(command);
  61. }
  62. void Renderer::handle_command(const Command& command)
  63. {
  64. switch (command.command_type()) {
  65. #define V(name, snake_name, symbol) \
  66. case CommandType::name: \
  67. handle_##snake_name(command.arguments()); \
  68. break;
  69. ENUMERATE_COMMANDS(V)
  70. #undef V
  71. case CommandType::TextNextLineShowString:
  72. handle_text_next_line_show_string(command.arguments());
  73. break;
  74. case CommandType::TextNextLineShowStringSetSpacing:
  75. handle_text_next_line_show_string_set_spacing(command.arguments());
  76. break;
  77. }
  78. }
  79. RENDERER_HANDLER(save_state)
  80. {
  81. m_graphics_state_stack.append(state());
  82. }
  83. RENDERER_HANDLER(restore_state)
  84. {
  85. m_graphics_state_stack.take_last();
  86. }
  87. RENDERER_HANDLER(concatenate_matrix)
  88. {
  89. Gfx::AffineTransform new_transform(
  90. args[0].to_float(),
  91. args[1].to_float(),
  92. args[2].to_float(),
  93. args[3].to_float(),
  94. args[4].to_float(),
  95. args[5].to_float());
  96. state().ctm.multiply(new_transform);
  97. m_text_rendering_matrix_is_dirty = true;
  98. }
  99. RENDERER_HANDLER(set_line_width)
  100. {
  101. state().line_width = args[0].to_float();
  102. }
  103. RENDERER_HANDLER(set_line_cap)
  104. {
  105. state().line_cap_style = static_cast<LineCapStyle>(args[0].as_int());
  106. }
  107. RENDERER_HANDLER(set_line_join)
  108. {
  109. state().line_join_style = static_cast<LineJoinStyle>(args[0].as_int());
  110. }
  111. RENDERER_HANDLER(set_miter_limit)
  112. {
  113. state().miter_limit = args[0].to_float();
  114. }
  115. RENDERER_HANDLER(set_dash_pattern)
  116. {
  117. auto dash_array = m_document->resolve_to<ArrayObject>(args[0]);
  118. Vector<int> pattern;
  119. for (auto& element : *dash_array)
  120. pattern.append(element.as_int());
  121. state().line_dash_pattern = LineDashPattern { pattern, args[1].as_int() };
  122. }
  123. RENDERER_TODO(set_color_rendering_intent);
  124. RENDERER_TODO(set_flatness_tolerance);
  125. RENDERER_TODO(set_graphics_state_from_dict);
  126. RENDERER_HANDLER(path_begin)
  127. {
  128. m_path = Gfx::Path();
  129. }
  130. RENDERER_HANDLER(path_line)
  131. {
  132. m_path.line_to(map(args[0].to_float(), args[1].to_float()));
  133. }
  134. RENDERER_TODO(path_cubic_bezier_curve);
  135. RENDERER_TODO(path_cubic_bezier_curve_no_first_control);
  136. RENDERER_TODO(path_cubic_bezier_curve_no_second_control);
  137. RENDERER_HANDLER(path_close)
  138. {
  139. m_path.close();
  140. }
  141. RENDERER_HANDLER(path_append_rect)
  142. {
  143. auto pos = map(args[0].to_float(), args[1].to_float());
  144. auto size = map(Gfx::FloatSize { args[2].to_float(), args[3].to_float() });
  145. m_path.move_to(pos);
  146. m_path.line_to({ pos.x() + size.width(), pos.y() });
  147. m_path.line_to({ pos.x() + size.width(), pos.y() + size.height() });
  148. m_path.line_to({ pos.x(), pos.y() + size.height() });
  149. m_path.close();
  150. }
  151. RENDERER_HANDLER(path_stroke)
  152. {
  153. m_painter.stroke_path(m_path, state().stroke_color, state().line_width);
  154. }
  155. RENDERER_HANDLER(path_close_and_stroke)
  156. {
  157. m_path.close();
  158. handle_path_stroke(args);
  159. }
  160. RENDERER_HANDLER(path_fill_nonzero)
  161. {
  162. m_painter.fill_path(m_path, state().paint_color, Gfx::Painter::WindingRule::Nonzero);
  163. }
  164. RENDERER_HANDLER(path_fill_nonzero_deprecated)
  165. {
  166. handle_path_fill_nonzero(args);
  167. }
  168. RENDERER_HANDLER(path_fill_evenodd)
  169. {
  170. m_painter.fill_path(m_path, state().paint_color, Gfx::Painter::WindingRule::EvenOdd);
  171. }
  172. RENDERER_HANDLER(path_fill_stroke_nonzero)
  173. {
  174. m_painter.stroke_path(m_path, state().stroke_color, state().line_width);
  175. handle_path_fill_nonzero(args);
  176. }
  177. RENDERER_HANDLER(path_fill_stroke_evenodd)
  178. {
  179. m_painter.stroke_path(m_path, state().stroke_color, state().line_width);
  180. handle_path_fill_evenodd(args);
  181. }
  182. RENDERER_HANDLER(path_close_fill_stroke_nonzero)
  183. {
  184. m_path.close();
  185. handle_path_fill_stroke_nonzero(args);
  186. }
  187. RENDERER_HANDLER(path_close_fill_stroke_evenodd)
  188. {
  189. m_path.close();
  190. handle_path_fill_stroke_evenodd(args);
  191. }
  192. RENDERER_HANDLER(path_end)
  193. {
  194. }
  195. RENDERER_TODO(path_intersect_clip_nonzero);
  196. RENDERER_TODO(path_intersect_clip_evenodd);
  197. RENDERER_HANDLER(text_begin)
  198. {
  199. m_text_matrix = Gfx::AffineTransform();
  200. m_text_line_matrix = Gfx::AffineTransform();
  201. }
  202. RENDERER_HANDLER(text_end)
  203. {
  204. // FIXME: Do we need to do anything here?
  205. }
  206. RENDERER_HANDLER(text_set_char_space)
  207. {
  208. text_state().character_spacing = args[0].to_float();
  209. }
  210. RENDERER_HANDLER(text_set_word_space)
  211. {
  212. text_state().word_spacing = args[0].to_float();
  213. }
  214. RENDERER_HANDLER(text_set_horizontal_scale)
  215. {
  216. m_text_rendering_matrix_is_dirty = true;
  217. text_state().horizontal_scaling = args[0].to_float() / 100.0f;
  218. }
  219. RENDERER_HANDLER(text_set_leading)
  220. {
  221. text_state().leading = args[0].to_float();
  222. }
  223. RENDERER_HANDLER(text_set_font)
  224. {
  225. auto target_font_name = m_document->resolve_to<NameObject>(args[0])->name();
  226. auto fonts_dictionary = m_page.resources->get_dict(m_document, "Font");
  227. auto font_dictionary = fonts_dictionary->get_dict(m_document, target_font_name);
  228. // FIXME: We do not yet have the standard 14 fonts, as some of them are not open fonts,
  229. // so we just use LiberationSerif for everything
  230. auto font_name = font_dictionary->get_name(m_document, "BaseFont")->name().to_lowercase();
  231. auto font_view = font_name.view();
  232. bool is_bold = font_view.contains("bold");
  233. bool is_italic = font_view.contains("italic");
  234. String font_variant;
  235. if (is_bold && is_italic) {
  236. font_variant = "BoldItalic";
  237. } else if (is_bold) {
  238. font_variant = "Bold";
  239. } else if (is_italic) {
  240. font_variant = "Italic";
  241. } else {
  242. font_variant = "Regular";
  243. }
  244. auto specified_font_size = args[1].to_float();
  245. // FIXME: This scaling should occur when drawing the glyph rather than selecting the font.
  246. // This should be removed when the painter supports arbitrary bitmap scaling.
  247. specified_font_size *= state().ctm.x_scale();
  248. text_state().font = Gfx::FontDatabase::the().get("Liberation Serif", font_variant, static_cast<int>(specified_font_size));
  249. VERIFY(text_state().font);
  250. m_text_rendering_matrix_is_dirty = true;
  251. }
  252. RENDERER_HANDLER(text_set_rendering_mode)
  253. {
  254. text_state().rendering_mode = static_cast<TextRenderingMode>(args[0].as_int());
  255. }
  256. RENDERER_HANDLER(text_set_rise)
  257. {
  258. m_text_rendering_matrix_is_dirty = true;
  259. text_state().rise = args[0].to_float();
  260. }
  261. RENDERER_HANDLER(text_next_line_offset)
  262. {
  263. Gfx::AffineTransform transform(1.0f, 0.0f, 0.0f, 1.0f, args[0].to_float(), args[1].to_float());
  264. transform.multiply(m_text_line_matrix);
  265. m_text_matrix = transform;
  266. m_text_line_matrix = transform;
  267. m_text_rendering_matrix_is_dirty = true;
  268. }
  269. RENDERER_HANDLER(text_next_line_and_set_leading)
  270. {
  271. text_state().leading = -args[1].to_float();
  272. handle_text_next_line_offset(args);
  273. }
  274. RENDERER_HANDLER(text_set_matrix_and_line_matrix)
  275. {
  276. Gfx::AffineTransform new_transform(
  277. args[0].to_float(),
  278. args[1].to_float(),
  279. args[2].to_float(),
  280. args[3].to_float(),
  281. args[4].to_float(),
  282. args[5].to_float());
  283. m_text_line_matrix = new_transform;
  284. m_text_matrix = new_transform;
  285. m_text_rendering_matrix_is_dirty = true;
  286. }
  287. RENDERER_HANDLER(text_next_line)
  288. {
  289. handle_text_next_line_offset({ 0.0f, -text_state().leading });
  290. }
  291. RENDERER_HANDLER(text_show_string)
  292. {
  293. auto text = m_document->resolve_to<StringObject>(args[0])->string();
  294. show_text(text);
  295. }
  296. RENDERER_HANDLER(text_next_line_show_string)
  297. {
  298. handle_text_next_line(args);
  299. handle_text_show_string(args);
  300. }
  301. RENDERER_TODO(text_next_line_show_string_set_spacing);
  302. RENDERER_TODO(text_show_string_array);
  303. RENDERER_TODO(type3_font_set_glyph_width);
  304. RENDERER_TODO(type3_font_set_glyph_width_and_bbox);
  305. RENDERER_TODO(color_set_stroking_space);
  306. RENDERER_TODO(color_set_painting_space);
  307. RENDERER_TODO(color_set_stroking);
  308. RENDERER_TODO(color_set_stroking_extended);
  309. RENDERER_TODO(color_set_painting);
  310. RENDERER_TODO(color_set_painting_extended);
  311. RENDERER_TODO(color_set_stroking_space_to_gray);
  312. RENDERER_TODO(color_set_painting_space_to_gray);
  313. RENDERER_TODO(color_set_stroking_space_to_rgb);
  314. RENDERER_TODO(color_set_painting_space_to_rgb);
  315. RENDERER_TODO(color_set_stroking_space_to_cmyk);
  316. RENDERER_TODO(color_set_painting_space_to_cmyk);
  317. RENDERER_TODO(shade);
  318. RENDERER_TODO(inline_image_begin);
  319. RENDERER_TODO(inline_image_begin_data);
  320. RENDERER_TODO(inline_image_end);
  321. RENDERER_TODO(paint_xobject);
  322. RENDERER_TODO(marked_content_point);
  323. RENDERER_TODO(marked_content_designate);
  324. RENDERER_TODO(marked_content_begin);
  325. RENDERER_TODO(marked_content_begin_with_property_list);
  326. RENDERER_TODO(marked_content_end);
  327. RENDERER_TODO(compatibility_begin);
  328. RENDERER_TODO(compatibility_end);
  329. template<typename T>
  330. Gfx::Point<T> Renderer::map(T x, T y) const
  331. {
  332. auto mapped = state().ctm.map(Gfx::Point<T> { x, y });
  333. return { mapped.x(), static_cast<T>(m_bitmap->height()) - mapped.y() };
  334. }
  335. template<typename T>
  336. Gfx::Size<T> Renderer::map(Gfx::Size<T> size) const
  337. {
  338. return state().ctm.map(size);
  339. }
  340. void Renderer::show_text(const String& string, int shift)
  341. {
  342. auto utf = Utf8View(string);
  343. auto& font = text_state().font;
  344. for (auto codepoint : utf) {
  345. // FIXME: Don't calculate this matrix for every character
  346. auto& text_rendering_matrix = calculate_text_rendering_matrix();
  347. auto text_position = text_rendering_matrix.map(Gfx::FloatPoint { 0.0f, 0.0f });
  348. text_position.set_y(static_cast<float>(m_bitmap->height()) - text_position.y());
  349. // FIXME: For some reason, the space character in LiberationSerif is drawn as an exclamation point
  350. if (codepoint != 0x20)
  351. m_painter.draw_glyph(text_position.to_type<int>(), codepoint, *text_state().font, state().paint_color);
  352. auto glyph_width = static_cast<float>(font->glyph_width(codepoint));
  353. auto tx = (glyph_width - static_cast<float>(shift) / 1000.0f);
  354. tx += text_state().character_spacing;
  355. if (codepoint == ' ')
  356. tx += text_state().word_spacing;
  357. tx *= text_state().horizontal_scaling;
  358. m_text_rendering_matrix_is_dirty = true;
  359. m_text_matrix = Gfx::AffineTransform(1, 0, 0, 1, tx, 0).multiply(m_text_matrix);
  360. }
  361. }
  362. const Gfx::AffineTransform& Renderer::calculate_text_rendering_matrix()
  363. {
  364. if (m_text_rendering_matrix_is_dirty) {
  365. m_text_rendering_matrix = Gfx::AffineTransform(
  366. text_state().horizontal_scaling,
  367. 0.0f,
  368. 0.0f,
  369. 1.0f,
  370. 0.0f,
  371. text_state().rise);
  372. m_text_rendering_matrix.multiply(m_text_matrix);
  373. m_text_rendering_matrix.multiply(state().ctm);
  374. m_text_rendering_matrix_is_dirty = false;
  375. }
  376. return m_text_rendering_matrix;
  377. }
  378. }