Renderer.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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/CommonNames.h>
  8. #include <LibPDF/Renderer.h>
  9. #include <ctype.h>
  10. #include <math.h>
  11. #define RENDERER_HANDLER(name) \
  12. void Renderer::handle_##name([[maybe_unused]] const Vector<Value>& args)
  13. #define RENDERER_TODO(name) \
  14. RENDERER_HANDLER(name) \
  15. { \
  16. dbgln("[PDF::Renderer] Unsupported draw operation " #name); \
  17. TODO(); \
  18. }
  19. namespace PDF {
  20. Optional<ColorSpace::Type> ColorSpace::color_space_from_string(const FlyString& str)
  21. {
  22. #define ENUM(name) \
  23. if (str == #name) \
  24. return ColorSpace::Type::name;
  25. ENUMERATE_COLOR_SPACES(ENUM)
  26. #undef ENUM
  27. return {};
  28. }
  29. Color ColorSpace::default_color_for_color_space(ColorSpace::Type color_space)
  30. {
  31. switch (color_space) {
  32. case Type::DeviceGray:
  33. case Type::DeviceRGB:
  34. return Color::NamedColor::Black;
  35. case Type::DeviceCMYK:
  36. return Color::from_cmyk(1.0f, 1.0f, 1.0f, 0.0f);
  37. default:
  38. TODO();
  39. }
  40. }
  41. Color ColorSpace::color_from_parameters(ColorSpace::Type color_space, const Vector<Value>& args)
  42. {
  43. switch (color_space) {
  44. case Type::DeviceGray: {
  45. VERIFY(args.size() == 1);
  46. auto gray = static_cast<u8>(args[0].to_float() * 255.0f);
  47. return Color(gray, gray, gray);
  48. }
  49. case Type::DeviceRGB: {
  50. VERIFY(args.size() == 3);
  51. auto r = static_cast<u8>(args[0].to_float() * 255.0f);
  52. auto g = static_cast<u8>(args[1].to_float() * 255.0f);
  53. auto b = static_cast<u8>(args[2].to_float() * 255.0f);
  54. return Color(r, g, b);
  55. }
  56. case Type::DeviceCMYK: {
  57. VERIFY(args.size() == 4);
  58. auto c = args[0].to_float();
  59. auto m = args[1].to_float();
  60. auto y = args[2].to_float();
  61. auto k = args[3].to_float();
  62. return Color::from_cmyk(c, m, y, k);
  63. }
  64. default:
  65. TODO();
  66. }
  67. }
  68. void Renderer::render(Document& document, const Page& page, RefPtr<Gfx::Bitmap> bitmap)
  69. {
  70. Renderer(document, page, bitmap).render();
  71. }
  72. Renderer::Renderer(RefPtr<Document> document, const Page& page, RefPtr<Gfx::Bitmap> bitmap)
  73. : m_document(document)
  74. , m_bitmap(bitmap)
  75. , m_page(page)
  76. , m_painter(*bitmap)
  77. {
  78. auto media_box = m_page.media_box;
  79. m_userspace_matrix.translate(media_box.lower_left_x, media_box.lower_left_y);
  80. float width = media_box.upper_right_x - media_box.lower_left_x;
  81. float height = media_box.upper_right_y - media_box.lower_left_y;
  82. float scale_x = static_cast<float>(bitmap->width()) / width;
  83. float scale_y = static_cast<float>(bitmap->height()) / height;
  84. m_userspace_matrix.scale(scale_x, scale_y);
  85. m_graphics_state_stack.append(GraphicsState { m_userspace_matrix });
  86. m_bitmap->fill(Gfx::Color::NamedColor::White);
  87. }
  88. void Renderer::render()
  89. {
  90. // Use our own vector, as the /Content can be an array with multiple
  91. // streams which gets concatenated
  92. // FIXME: Text operators are supposed to only have effects on the current
  93. // stream object. Do the text operators treat this concatenated stream
  94. // as one stream or multiple?
  95. ByteBuffer byte_buffer;
  96. if (m_page.contents->is_array()) {
  97. auto contents = object_cast<ArrayObject>(m_page.contents);
  98. for (auto& ref : *contents) {
  99. auto bytes = m_document->resolve_to<StreamObject>(ref)->bytes();
  100. byte_buffer.append(bytes.data(), bytes.size());
  101. }
  102. } else {
  103. VERIFY(m_page.contents->is_stream());
  104. auto bytes = object_cast<StreamObject>(m_page.contents)->bytes();
  105. byte_buffer.append(bytes.data(), bytes.size());
  106. }
  107. auto commands = Parser::parse_graphics_commands(byte_buffer);
  108. for (auto& command : commands)
  109. handle_command(command);
  110. }
  111. void Renderer::handle_command(const Command& command)
  112. {
  113. switch (command.command_type()) {
  114. #define V(name, snake_name, symbol) \
  115. case CommandType::name: \
  116. handle_##snake_name(command.arguments()); \
  117. break;
  118. ENUMERATE_COMMANDS(V)
  119. #undef V
  120. case CommandType::TextNextLineShowString:
  121. handle_text_next_line_show_string(command.arguments());
  122. break;
  123. case CommandType::TextNextLineShowStringSetSpacing:
  124. handle_text_next_line_show_string_set_spacing(command.arguments());
  125. break;
  126. }
  127. }
  128. RENDERER_HANDLER(save_state)
  129. {
  130. m_graphics_state_stack.append(state());
  131. }
  132. RENDERER_HANDLER(restore_state)
  133. {
  134. m_graphics_state_stack.take_last();
  135. }
  136. RENDERER_HANDLER(concatenate_matrix)
  137. {
  138. Gfx::AffineTransform new_transform(
  139. args[0].to_float(),
  140. args[1].to_float(),
  141. args[2].to_float(),
  142. args[3].to_float(),
  143. args[4].to_float(),
  144. args[5].to_float());
  145. state().ctm.multiply(new_transform);
  146. m_text_rendering_matrix_is_dirty = true;
  147. }
  148. RENDERER_HANDLER(set_line_width)
  149. {
  150. state().line_width = args[0].to_float();
  151. }
  152. RENDERER_HANDLER(set_line_cap)
  153. {
  154. state().line_cap_style = static_cast<LineCapStyle>(args[0].as_int());
  155. }
  156. RENDERER_HANDLER(set_line_join)
  157. {
  158. state().line_join_style = static_cast<LineJoinStyle>(args[0].as_int());
  159. }
  160. RENDERER_HANDLER(set_miter_limit)
  161. {
  162. state().miter_limit = args[0].to_float();
  163. }
  164. RENDERER_HANDLER(set_dash_pattern)
  165. {
  166. auto dash_array = m_document->resolve_to<ArrayObject>(args[0]);
  167. Vector<int> pattern;
  168. for (auto& element : *dash_array)
  169. pattern.append(element.as_int());
  170. state().line_dash_pattern = LineDashPattern { pattern, args[1].as_int() };
  171. }
  172. RENDERER_TODO(set_color_rendering_intent);
  173. RENDERER_TODO(set_flatness_tolerance);
  174. RENDERER_TODO(set_graphics_state_from_dict);
  175. RENDERER_HANDLER(path_move)
  176. {
  177. m_current_path.move_to(map(args[0].to_float(), args[1].to_float()));
  178. }
  179. RENDERER_HANDLER(path_line)
  180. {
  181. VERIFY(!m_current_path.segments().is_empty());
  182. m_current_path.line_to(map(args[0].to_float(), args[1].to_float()));
  183. }
  184. RENDERER_TODO(path_cubic_bezier_curve);
  185. RENDERER_TODO(path_cubic_bezier_curve_no_first_control);
  186. RENDERER_TODO(path_cubic_bezier_curve_no_second_control);
  187. RENDERER_HANDLER(path_close)
  188. {
  189. m_current_path.close();
  190. }
  191. RENDERER_HANDLER(path_append_rect)
  192. {
  193. auto pos = map(args[0].to_float(), args[1].to_float());
  194. auto size = map(Gfx::FloatSize { args[2].to_float(), args[3].to_float() });
  195. m_current_path.move_to(pos);
  196. m_current_path.line_to({ pos.x() + size.width(), pos.y() });
  197. m_current_path.line_to({ pos.x() + size.width(), pos.y() + size.height() });
  198. m_current_path.line_to({ pos.x(), pos.y() + size.height() });
  199. m_current_path.close();
  200. }
  201. RENDERER_HANDLER(path_stroke)
  202. {
  203. m_painter.stroke_path(m_current_path, state().stroke_color, state().line_width);
  204. m_current_path.clear();
  205. }
  206. RENDERER_HANDLER(path_close_and_stroke)
  207. {
  208. m_current_path.close();
  209. handle_path_stroke(args);
  210. }
  211. RENDERER_HANDLER(path_fill_nonzero)
  212. {
  213. m_painter.fill_path(m_current_path, state().paint_color, Gfx::Painter::WindingRule::Nonzero);
  214. m_current_path.clear();
  215. }
  216. RENDERER_HANDLER(path_fill_nonzero_deprecated)
  217. {
  218. handle_path_fill_nonzero(args);
  219. }
  220. RENDERER_HANDLER(path_fill_evenodd)
  221. {
  222. m_painter.fill_path(m_current_path, state().paint_color, Gfx::Painter::WindingRule::EvenOdd);
  223. m_current_path.clear();
  224. }
  225. RENDERER_HANDLER(path_fill_stroke_nonzero)
  226. {
  227. m_painter.stroke_path(m_current_path, state().stroke_color, state().line_width);
  228. handle_path_fill_nonzero(args);
  229. }
  230. RENDERER_HANDLER(path_fill_stroke_evenodd)
  231. {
  232. m_painter.stroke_path(m_current_path, state().stroke_color, state().line_width);
  233. handle_path_fill_evenodd(args);
  234. }
  235. RENDERER_HANDLER(path_close_fill_stroke_nonzero)
  236. {
  237. m_current_path.close();
  238. handle_path_fill_stroke_nonzero(args);
  239. }
  240. RENDERER_HANDLER(path_close_fill_stroke_evenodd)
  241. {
  242. m_current_path.close();
  243. handle_path_fill_stroke_evenodd(args);
  244. }
  245. RENDERER_HANDLER(path_end)
  246. {
  247. }
  248. RENDERER_HANDLER(path_intersect_clip_nonzero)
  249. {
  250. // FIXME: Support arbitrary path clipping in the painter and utilize that here
  251. auto bounding_box = map(m_current_path.bounding_box());
  252. m_painter.add_clip_rect(bounding_box.to_type<int>());
  253. }
  254. RENDERER_HANDLER(path_intersect_clip_evenodd)
  255. {
  256. // FIXME: Support arbitrary path clipping in the painter and utilize that here
  257. auto bounding_box = map(m_current_path.bounding_box());
  258. m_painter.add_clip_rect(bounding_box.to_type<int>());
  259. }
  260. RENDERER_HANDLER(text_begin)
  261. {
  262. m_text_matrix = Gfx::AffineTransform();
  263. m_text_line_matrix = Gfx::AffineTransform();
  264. }
  265. RENDERER_HANDLER(text_end)
  266. {
  267. // FIXME: Do we need to do anything here?
  268. }
  269. RENDERER_HANDLER(text_set_char_space)
  270. {
  271. text_state().character_spacing = args[0].to_float();
  272. }
  273. RENDERER_HANDLER(text_set_word_space)
  274. {
  275. text_state().word_spacing = args[0].to_float();
  276. }
  277. RENDERER_HANDLER(text_set_horizontal_scale)
  278. {
  279. m_text_rendering_matrix_is_dirty = true;
  280. text_state().horizontal_scaling = args[0].to_float() / 100.0f;
  281. }
  282. RENDERER_HANDLER(text_set_leading)
  283. {
  284. text_state().leading = args[0].to_float();
  285. }
  286. RENDERER_HANDLER(text_set_font)
  287. {
  288. auto target_font_name = m_document->resolve_to<NameObject>(args[0])->name();
  289. auto fonts_dictionary = m_page.resources->get_dict(m_document, CommonNames::Font);
  290. auto font_dictionary = fonts_dictionary->get_dict(m_document, target_font_name);
  291. // FIXME: We do not yet have the standard 14 fonts, as some of them are not open fonts,
  292. // so we just use LiberationSerif for everything
  293. auto font_name = font_dictionary->get_name(m_document, CommonNames::BaseFont)->name().to_lowercase();
  294. auto font_view = font_name.view();
  295. bool is_bold = font_view.contains("bold");
  296. bool is_italic = font_view.contains("italic");
  297. String font_variant;
  298. if (is_bold && is_italic) {
  299. font_variant = "BoldItalic";
  300. } else if (is_bold) {
  301. font_variant = "Bold";
  302. } else if (is_italic) {
  303. font_variant = "Italic";
  304. } else {
  305. font_variant = "Regular";
  306. }
  307. auto specified_font_size = args[1].to_float();
  308. // FIXME: This scaling should occur when drawing the glyph rather than selecting the font.
  309. // This should be removed when the painter supports arbitrary bitmap scaling.
  310. specified_font_size *= state().ctm.x_scale();
  311. text_state().font = Gfx::FontDatabase::the().get("Liberation Serif", font_variant, static_cast<int>(specified_font_size));
  312. VERIFY(text_state().font);
  313. m_text_rendering_matrix_is_dirty = true;
  314. }
  315. RENDERER_HANDLER(text_set_rendering_mode)
  316. {
  317. text_state().rendering_mode = static_cast<TextRenderingMode>(args[0].as_int());
  318. }
  319. RENDERER_HANDLER(text_set_rise)
  320. {
  321. m_text_rendering_matrix_is_dirty = true;
  322. text_state().rise = args[0].to_float();
  323. }
  324. RENDERER_HANDLER(text_next_line_offset)
  325. {
  326. Gfx::AffineTransform transform(1.0f, 0.0f, 0.0f, 1.0f, args[0].to_float(), args[1].to_float());
  327. transform.multiply(m_text_line_matrix);
  328. m_text_matrix = transform;
  329. m_text_line_matrix = transform;
  330. m_text_rendering_matrix_is_dirty = true;
  331. }
  332. RENDERER_HANDLER(text_next_line_and_set_leading)
  333. {
  334. text_state().leading = -args[1].to_float();
  335. handle_text_next_line_offset(args);
  336. }
  337. RENDERER_HANDLER(text_set_matrix_and_line_matrix)
  338. {
  339. Gfx::AffineTransform new_transform(
  340. args[0].to_float(),
  341. args[1].to_float(),
  342. args[2].to_float(),
  343. args[3].to_float(),
  344. args[4].to_float(),
  345. args[5].to_float());
  346. m_text_line_matrix = new_transform;
  347. m_text_matrix = new_transform;
  348. m_text_rendering_matrix_is_dirty = true;
  349. }
  350. RENDERER_HANDLER(text_next_line)
  351. {
  352. handle_text_next_line_offset({ 0.0f, -text_state().leading });
  353. }
  354. RENDERER_HANDLER(text_show_string)
  355. {
  356. auto text = m_document->resolve_to<StringObject>(args[0])->string();
  357. show_text(text);
  358. }
  359. RENDERER_HANDLER(text_next_line_show_string)
  360. {
  361. handle_text_next_line(args);
  362. handle_text_show_string(args);
  363. }
  364. RENDERER_TODO(text_next_line_show_string_set_spacing);
  365. RENDERER_TODO(text_show_string_array);
  366. RENDERER_TODO(type3_font_set_glyph_width);
  367. RENDERER_TODO(type3_font_set_glyph_width_and_bbox);
  368. RENDERER_HANDLER(set_stroking_space)
  369. {
  370. state().stroke_color_space = get_color_space(args[0]);
  371. state().stroke_color = ColorSpace::default_color_for_color_space(state().stroke_color_space);
  372. }
  373. RENDERER_HANDLER(set_painting_space)
  374. {
  375. state().paint_color_space = get_color_space(args[0]);
  376. state().paint_color = ColorSpace::default_color_for_color_space(state().paint_color_space);
  377. }
  378. RENDERER_HANDLER(set_stroking_color)
  379. {
  380. state().stroke_color = ColorSpace::color_from_parameters(state().stroke_color_space, args);
  381. }
  382. RENDERER_TODO(set_stroking_color_extended);
  383. RENDERER_HANDLER(set_painting_color)
  384. {
  385. state().paint_color = ColorSpace::color_from_parameters(state().paint_color_space, args);
  386. }
  387. RENDERER_TODO(set_painting_color_extended);
  388. RENDERER_HANDLER(set_stroking_color_and_space_to_gray)
  389. {
  390. state().stroke_color_space = ColorSpace::Type::DeviceGray;
  391. state().stroke_color = ColorSpace::color_from_parameters(ColorSpace::Type::DeviceGray, args);
  392. }
  393. RENDERER_HANDLER(set_painting_color_and_space_to_gray)
  394. {
  395. state().paint_color_space = ColorSpace::Type::DeviceGray;
  396. state().paint_color = ColorSpace::color_from_parameters(ColorSpace::Type::DeviceGray, args);
  397. }
  398. RENDERER_HANDLER(set_stroking_color_and_space_to_rgb)
  399. {
  400. state().stroke_color_space = ColorSpace::Type::DeviceRGB;
  401. state().stroke_color = ColorSpace::color_from_parameters(ColorSpace::Type::DeviceRGB, args);
  402. }
  403. RENDERER_HANDLER(set_painting_color_and_space_to_rgb)
  404. {
  405. state().paint_color_space = ColorSpace::Type::DeviceRGB;
  406. state().paint_color = ColorSpace::color_from_parameters(ColorSpace::Type::DeviceRGB, args);
  407. }
  408. RENDERER_HANDLER(set_stroking_color_and_space_to_cmyk)
  409. {
  410. state().stroke_color_space = ColorSpace::Type::DeviceCMYK;
  411. state().stroke_color = ColorSpace::color_from_parameters(ColorSpace::Type::DeviceCMYK, args);
  412. }
  413. RENDERER_HANDLER(set_painting_color_and_space_to_cmyk)
  414. {
  415. state().paint_color_space = ColorSpace::Type::DeviceCMYK;
  416. state().paint_color = ColorSpace::color_from_parameters(ColorSpace::Type::DeviceCMYK, args);
  417. }
  418. RENDERER_TODO(shade);
  419. RENDERER_TODO(inline_image_begin);
  420. RENDERER_TODO(inline_image_begin_data);
  421. RENDERER_TODO(inline_image_end);
  422. RENDERER_TODO(paint_xobject);
  423. RENDERER_TODO(marked_content_point);
  424. RENDERER_TODO(marked_content_designate);
  425. RENDERER_TODO(marked_content_begin);
  426. RENDERER_TODO(marked_content_begin_with_property_list);
  427. RENDERER_TODO(marked_content_end);
  428. RENDERER_TODO(compatibility_begin);
  429. RENDERER_TODO(compatibility_end);
  430. template<typename T>
  431. Gfx::Point<T> Renderer::map(T x, T y) const
  432. {
  433. auto mapped = state().ctm.map(Gfx::Point<T> { x, y });
  434. return { mapped.x(), static_cast<T>(m_bitmap->height()) - mapped.y() };
  435. }
  436. template<typename T>
  437. Gfx::Size<T> Renderer::map(Gfx::Size<T> size) const
  438. {
  439. return state().ctm.map(size);
  440. }
  441. template<typename T>
  442. Gfx::Rect<T> Renderer::map(Gfx::Rect<T> rect) const
  443. {
  444. return state().ctm.map(rect);
  445. }
  446. void Renderer::show_text(const String& string, int shift)
  447. {
  448. auto utf = Utf8View(string);
  449. auto& font = text_state().font;
  450. for (auto code_point : utf) {
  451. // FIXME: Don't calculate this matrix for every character
  452. auto& text_rendering_matrix = calculate_text_rendering_matrix();
  453. auto text_position = text_rendering_matrix.map(Gfx::FloatPoint { 0.0f, 0.0f });
  454. text_position.set_y(static_cast<float>(m_bitmap->height()) - text_position.y());
  455. // FIXME: For some reason, the space character in LiberationSerif is drawn as an exclamation point
  456. if (code_point != 0x20)
  457. m_painter.draw_glyph(text_position.to_type<int>(), code_point, *text_state().font, state().paint_color);
  458. auto glyph_width = static_cast<float>(font->glyph_width(code_point));
  459. auto tx = (glyph_width - static_cast<float>(shift) / 1000.0f);
  460. tx += text_state().character_spacing;
  461. if (code_point == ' ')
  462. tx += text_state().word_spacing;
  463. tx *= text_state().horizontal_scaling;
  464. m_text_rendering_matrix_is_dirty = true;
  465. m_text_matrix = Gfx::AffineTransform(1, 0, 0, 1, tx, 0).multiply(m_text_matrix);
  466. }
  467. }
  468. ColorSpace::Type Renderer::get_color_space(const Value& value)
  469. {
  470. auto name = object_cast<NameObject>(value.as_object())->name();
  471. auto color_space_opt = ColorSpace::color_space_from_string(name);
  472. if (!color_space_opt.has_value()) {
  473. // The name is probably a key into the resource dictionary
  474. TODO();
  475. }
  476. return color_space_opt.value();
  477. }
  478. const Gfx::AffineTransform& Renderer::calculate_text_rendering_matrix()
  479. {
  480. if (m_text_rendering_matrix_is_dirty) {
  481. m_text_rendering_matrix = Gfx::AffineTransform(
  482. text_state().horizontal_scaling,
  483. 0.0f,
  484. 0.0f,
  485. 1.0f,
  486. 0.0f,
  487. text_state().rise);
  488. m_text_rendering_matrix.multiply(m_text_matrix);
  489. m_text_rendering_matrix.multiply(state().ctm);
  490. m_text_rendering_matrix_is_dirty = false;
  491. }
  492. return m_text_rendering_matrix;
  493. }
  494. }