Renderer.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. /*
  2. * Copyright (c) 2021-2022, 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/Fonts/PDFFont.h>
  9. #include <LibPDF/Renderer.h>
  10. #define RENDERER_HANDLER(name) \
  11. PDFErrorOr<void> Renderer::handle_##name([[maybe_unused]] Vector<Value> const& 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. PDFErrorOr<void> Renderer::render(Document& document, Page const& page, RefPtr<Gfx::Bitmap> bitmap, RenderingPreferences rendering_preferences)
  20. {
  21. return Renderer(document, page, bitmap, rendering_preferences).render();
  22. }
  23. static void rect_path(Gfx::Path& path, float x, float y, float width, float height)
  24. {
  25. path.move_to({ x, y });
  26. path.line_to({ x + width, y });
  27. path.line_to({ x + width, y + height });
  28. path.line_to({ x, y + height });
  29. path.close();
  30. }
  31. static Gfx::Path rect_path(float x, float y, float width, float height)
  32. {
  33. Gfx::Path path;
  34. rect_path(path, x, y, width, height);
  35. return path;
  36. }
  37. template<typename T>
  38. static Gfx::Path rect_path(Gfx::Rect<T> rect)
  39. {
  40. return rect_path(rect.x(), rect.y(), rect.width(), rect.height());
  41. }
  42. Renderer::Renderer(RefPtr<Document> document, Page const& page, RefPtr<Gfx::Bitmap> bitmap, RenderingPreferences rendering_preferences)
  43. : m_document(document)
  44. , m_bitmap(bitmap)
  45. , m_page(page)
  46. , m_painter(*bitmap)
  47. , m_anti_aliasing_painter(m_painter)
  48. , m_rendering_preferences(rendering_preferences)
  49. {
  50. auto media_box = m_page.media_box;
  51. Gfx::AffineTransform userspace_matrix;
  52. userspace_matrix.translate(media_box.lower_left_x, media_box.lower_left_y);
  53. float width = media_box.width();
  54. float height = media_box.height();
  55. float scale_x = static_cast<float>(bitmap->width()) / width;
  56. float scale_y = static_cast<float>(bitmap->height()) / height;
  57. userspace_matrix.scale(scale_x, scale_y);
  58. // PDF user-space coordinate y axis increases from bottom to top, so we have to
  59. // insert a horizontal reflection about the vertical midpoint into our transformation
  60. // matrix
  61. static Gfx::AffineTransform horizontal_reflection_matrix = { 1, 0, 0, -1, 0, 0 };
  62. userspace_matrix.multiply(horizontal_reflection_matrix);
  63. userspace_matrix.translate(0.0f, -height);
  64. auto initial_clipping_path = rect_path(0, 0, width, height);
  65. m_graphics_state_stack.append(GraphicsState { userspace_matrix, { initial_clipping_path, initial_clipping_path } });
  66. m_bitmap->fill(Gfx::Color::NamedColor::White);
  67. }
  68. PDFErrorOr<void> Renderer::render()
  69. {
  70. // Use our own vector, as the /Content can be an array with multiple
  71. // streams which gets concatenated
  72. // FIXME: Text operators are supposed to only have effects on the current
  73. // stream object. Do the text operators treat this concatenated stream
  74. // as one stream or multiple?
  75. ByteBuffer byte_buffer;
  76. if (m_page.contents->is<ArrayObject>()) {
  77. auto contents = m_page.contents->cast<ArrayObject>();
  78. for (auto& ref : *contents) {
  79. auto bytes = TRY(m_document->resolve_to<StreamObject>(ref))->bytes();
  80. byte_buffer.append(bytes.data(), bytes.size());
  81. }
  82. } else {
  83. auto bytes = m_page.contents->cast<StreamObject>()->bytes();
  84. byte_buffer.append(bytes.data(), bytes.size());
  85. }
  86. auto operators = TRY(Parser::parse_operators(m_document, byte_buffer));
  87. for (auto& op : operators)
  88. TRY(handle_operator(op));
  89. return {};
  90. }
  91. PDFErrorOr<void> Renderer::handle_operator(Operator const& op)
  92. {
  93. switch (op.type()) {
  94. #define V(name, snake_name, symbol) \
  95. case OperatorType::name: \
  96. TRY(handle_##snake_name(op.arguments())); \
  97. break;
  98. ENUMERATE_OPERATORS(V)
  99. #undef V
  100. case OperatorType::TextNextLineShowString:
  101. TRY(handle_text_next_line_show_string(op.arguments()));
  102. break;
  103. case OperatorType::TextNextLineShowStringSetSpacing:
  104. TRY(handle_text_next_line_show_string_set_spacing(op.arguments()));
  105. break;
  106. }
  107. return {};
  108. }
  109. RENDERER_HANDLER(save_state)
  110. {
  111. m_graphics_state_stack.append(state());
  112. return {};
  113. }
  114. RENDERER_HANDLER(restore_state)
  115. {
  116. m_graphics_state_stack.take_last();
  117. return {};
  118. }
  119. RENDERER_HANDLER(concatenate_matrix)
  120. {
  121. Gfx::AffineTransform new_transform(
  122. args[0].to_float(),
  123. args[1].to_float(),
  124. args[2].to_float(),
  125. args[3].to_float(),
  126. args[4].to_float(),
  127. args[5].to_float());
  128. state().ctm.multiply(new_transform);
  129. m_text_rendering_matrix_is_dirty = true;
  130. return {};
  131. }
  132. RENDERER_HANDLER(set_line_width)
  133. {
  134. state().line_width = args[0].to_float();
  135. return {};
  136. }
  137. RENDERER_HANDLER(set_line_cap)
  138. {
  139. state().line_cap_style = static_cast<LineCapStyle>(args[0].get<int>());
  140. return {};
  141. }
  142. RENDERER_HANDLER(set_line_join)
  143. {
  144. state().line_join_style = static_cast<LineJoinStyle>(args[0].get<int>());
  145. return {};
  146. }
  147. RENDERER_HANDLER(set_miter_limit)
  148. {
  149. state().miter_limit = args[0].to_float();
  150. return {};
  151. }
  152. RENDERER_HANDLER(set_dash_pattern)
  153. {
  154. auto dash_array = MUST(m_document->resolve_to<ArrayObject>(args[0]));
  155. Vector<int> pattern;
  156. for (auto& element : *dash_array)
  157. pattern.append(element.to_int());
  158. state().line_dash_pattern = LineDashPattern { pattern, args[1].get<int>() };
  159. return {};
  160. }
  161. RENDERER_TODO(set_color_rendering_intent)
  162. RENDERER_TODO(set_flatness_tolerance)
  163. RENDERER_HANDLER(set_graphics_state_from_dict)
  164. {
  165. VERIFY(m_page.resources->contains(CommonNames::ExtGState));
  166. auto dict_name = MUST(m_document->resolve_to<NameObject>(args[0]))->name();
  167. auto ext_gstate_dict = MUST(m_page.resources->get_dict(m_document, CommonNames::ExtGState));
  168. auto target_dict = MUST(ext_gstate_dict->get_dict(m_document, dict_name));
  169. TRY(set_graphics_state_from_dict(target_dict));
  170. return {};
  171. }
  172. RENDERER_HANDLER(path_move)
  173. {
  174. m_current_path.move_to(map(args[0].to_float(), args[1].to_float()));
  175. return {};
  176. }
  177. RENDERER_HANDLER(path_line)
  178. {
  179. VERIFY(!m_current_path.segments().is_empty());
  180. m_current_path.line_to(map(args[0].to_float(), args[1].to_float()));
  181. return {};
  182. }
  183. RENDERER_TODO(path_cubic_bezier_curve)
  184. RENDERER_TODO(path_cubic_bezier_curve_no_first_control)
  185. RENDERER_TODO(path_cubic_bezier_curve_no_second_control)
  186. RENDERER_HANDLER(path_close)
  187. {
  188. m_current_path.close();
  189. return {};
  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. // FIXME: Why do we need to flip the y axis of rectangles here? The coordinates
  196. // in the PDF file seem to be correct, with the same flipped-ness as
  197. // everything else in a PDF file.
  198. pos.set_y(m_bitmap->height() - pos.y() - size.height());
  199. rect_path(m_current_path, pos.x(), pos.y(), size.width(), size.height());
  200. return {};
  201. }
  202. ///
  203. // Path painting operations
  204. ///
  205. void Renderer::begin_path_paint()
  206. {
  207. auto bounding_box = map(state().clipping_paths.current.bounding_box());
  208. m_painter.clear_clip_rect();
  209. if (m_rendering_preferences.show_clipping_paths) {
  210. m_painter.stroke_path(rect_path(bounding_box), Color::Black, 1);
  211. }
  212. m_painter.add_clip_rect(bounding_box.to_type<int>());
  213. }
  214. void Renderer::end_path_paint()
  215. {
  216. m_current_path.clear();
  217. m_painter.clear_clip_rect();
  218. state().clipping_paths.current = state().clipping_paths.next;
  219. }
  220. RENDERER_HANDLER(path_stroke)
  221. {
  222. begin_path_paint();
  223. m_anti_aliasing_painter.stroke_path(m_current_path, state().stroke_color, state().line_width);
  224. end_path_paint();
  225. return {};
  226. }
  227. RENDERER_HANDLER(path_close_and_stroke)
  228. {
  229. m_current_path.close();
  230. TRY(handle_path_stroke(args));
  231. return {};
  232. }
  233. RENDERER_HANDLER(path_fill_nonzero)
  234. {
  235. begin_path_paint();
  236. m_anti_aliasing_painter.fill_path(m_current_path, state().paint_color, Gfx::Painter::WindingRule::Nonzero);
  237. end_path_paint();
  238. return {};
  239. }
  240. RENDERER_HANDLER(path_fill_nonzero_deprecated)
  241. {
  242. TRY(handle_path_fill_nonzero(args));
  243. return {};
  244. }
  245. RENDERER_HANDLER(path_fill_evenodd)
  246. {
  247. begin_path_paint();
  248. m_anti_aliasing_painter.fill_path(m_current_path, state().paint_color, Gfx::Painter::WindingRule::EvenOdd);
  249. end_path_paint();
  250. return {};
  251. }
  252. RENDERER_HANDLER(path_fill_stroke_nonzero)
  253. {
  254. m_anti_aliasing_painter.stroke_path(m_current_path, state().stroke_color, state().line_width);
  255. TRY(handle_path_fill_nonzero(args));
  256. return {};
  257. }
  258. RENDERER_HANDLER(path_fill_stroke_evenodd)
  259. {
  260. m_anti_aliasing_painter.stroke_path(m_current_path, state().stroke_color, state().line_width);
  261. TRY(handle_path_fill_evenodd(args));
  262. return {};
  263. }
  264. RENDERER_HANDLER(path_close_fill_stroke_nonzero)
  265. {
  266. m_current_path.close();
  267. TRY(handle_path_fill_stroke_nonzero(args));
  268. return {};
  269. }
  270. RENDERER_HANDLER(path_close_fill_stroke_evenodd)
  271. {
  272. m_current_path.close();
  273. TRY(handle_path_fill_stroke_evenodd(args));
  274. return {};
  275. }
  276. RENDERER_HANDLER(path_end)
  277. {
  278. begin_path_paint();
  279. end_path_paint();
  280. return {};
  281. }
  282. RENDERER_HANDLER(path_intersect_clip_nonzero)
  283. {
  284. // FIXME: Support arbitrary path clipping in Path and utilize that here
  285. auto next_clipping_bbox = state().clipping_paths.next.bounding_box();
  286. next_clipping_bbox.intersect(m_current_path.bounding_box());
  287. state().clipping_paths.next = rect_path(next_clipping_bbox);
  288. return {};
  289. }
  290. RENDERER_HANDLER(path_intersect_clip_evenodd)
  291. {
  292. // FIXME: Should have different behavior than path_intersect_clip_nonzero
  293. return handle_path_intersect_clip_nonzero(args);
  294. }
  295. RENDERER_HANDLER(text_begin)
  296. {
  297. m_text_matrix = Gfx::AffineTransform();
  298. m_text_line_matrix = Gfx::AffineTransform();
  299. return {};
  300. }
  301. RENDERER_HANDLER(text_end)
  302. {
  303. // FIXME: Do we need to do anything here?
  304. return {};
  305. }
  306. RENDERER_HANDLER(text_set_char_space)
  307. {
  308. text_state().character_spacing = args[0].to_float();
  309. return {};
  310. }
  311. RENDERER_HANDLER(text_set_word_space)
  312. {
  313. text_state().word_spacing = args[0].to_float();
  314. return {};
  315. }
  316. RENDERER_HANDLER(text_set_horizontal_scale)
  317. {
  318. m_text_rendering_matrix_is_dirty = true;
  319. text_state().horizontal_scaling = args[0].to_float() / 100.0f;
  320. return {};
  321. }
  322. RENDERER_HANDLER(text_set_leading)
  323. {
  324. text_state().leading = args[0].to_float();
  325. return {};
  326. }
  327. RENDERER_HANDLER(text_set_font)
  328. {
  329. auto target_font_name = MUST(m_document->resolve_to<NameObject>(args[0]))->name();
  330. auto fonts_dictionary = MUST(m_page.resources->get_dict(m_document, CommonNames::Font));
  331. auto font_dictionary = MUST(fonts_dictionary->get_dict(m_document, target_font_name));
  332. text_state().font_size = args[1].to_float();
  333. auto& text_rendering_matrix = calculate_text_rendering_matrix();
  334. auto font_size = text_rendering_matrix.x_scale() * text_state().font_size;
  335. auto font = TRY(PDFFont::create(m_document, font_dictionary, font_size));
  336. text_state().font = font;
  337. m_text_rendering_matrix_is_dirty = true;
  338. return {};
  339. }
  340. RENDERER_HANDLER(text_set_rendering_mode)
  341. {
  342. text_state().rendering_mode = static_cast<TextRenderingMode>(args[0].get<int>());
  343. return {};
  344. }
  345. RENDERER_HANDLER(text_set_rise)
  346. {
  347. m_text_rendering_matrix_is_dirty = true;
  348. text_state().rise = args[0].to_float();
  349. return {};
  350. }
  351. RENDERER_HANDLER(text_next_line_offset)
  352. {
  353. Gfx::AffineTransform transform(1.0f, 0.0f, 0.0f, 1.0f, args[0].to_float(), args[1].to_float());
  354. m_text_line_matrix.multiply(transform);
  355. m_text_matrix = m_text_line_matrix;
  356. return {};
  357. }
  358. RENDERER_HANDLER(text_next_line_and_set_leading)
  359. {
  360. text_state().leading = -args[1].to_float();
  361. TRY(handle_text_next_line_offset(args));
  362. return {};
  363. }
  364. RENDERER_HANDLER(text_set_matrix_and_line_matrix)
  365. {
  366. Gfx::AffineTransform new_transform(
  367. args[0].to_float(),
  368. args[1].to_float(),
  369. args[2].to_float(),
  370. args[3].to_float(),
  371. args[4].to_float(),
  372. args[5].to_float());
  373. m_text_line_matrix = new_transform;
  374. m_text_matrix = new_transform;
  375. m_text_rendering_matrix_is_dirty = true;
  376. return {};
  377. }
  378. RENDERER_HANDLER(text_next_line)
  379. {
  380. TRY(handle_text_next_line_offset({ 0.0f, -text_state().leading }));
  381. return {};
  382. }
  383. RENDERER_HANDLER(text_show_string)
  384. {
  385. auto text = MUST(m_document->resolve_to<StringObject>(args[0]))->string();
  386. show_text(text);
  387. return {};
  388. }
  389. RENDERER_HANDLER(text_next_line_show_string)
  390. {
  391. TRY(handle_text_next_line(args));
  392. TRY(handle_text_show_string(args));
  393. return {};
  394. }
  395. RENDERER_TODO(text_next_line_show_string_set_spacing)
  396. RENDERER_HANDLER(text_show_string_array)
  397. {
  398. auto elements = MUST(m_document->resolve_to<ArrayObject>(args[0]))->elements();
  399. float next_shift = 0.0f;
  400. for (auto& element : elements) {
  401. if (element.has<int>()) {
  402. next_shift = element.get<int>();
  403. } else if (element.has<float>()) {
  404. next_shift = element.get<float>();
  405. } else {
  406. auto shift = next_shift / 1000.0f;
  407. m_text_matrix.translate(-shift * text_state().font_size * text_state().horizontal_scaling, 0.0f);
  408. auto str = element.get<NonnullRefPtr<Object>>()->cast<StringObject>()->string();
  409. show_text(str);
  410. }
  411. }
  412. return {};
  413. }
  414. RENDERER_TODO(type3_font_set_glyph_width)
  415. RENDERER_TODO(type3_font_set_glyph_width_and_bbox)
  416. RENDERER_HANDLER(set_stroking_space)
  417. {
  418. state().stroke_color_space = TRY(get_color_space(args[0]));
  419. VERIFY(state().stroke_color_space);
  420. return {};
  421. }
  422. RENDERER_HANDLER(set_painting_space)
  423. {
  424. state().paint_color_space = TRY(get_color_space(args[0]));
  425. VERIFY(state().paint_color_space);
  426. return {};
  427. }
  428. RENDERER_HANDLER(set_stroking_color)
  429. {
  430. state().stroke_color = state().stroke_color_space->color(args);
  431. return {};
  432. }
  433. RENDERER_HANDLER(set_stroking_color_extended)
  434. {
  435. // FIXME: Handle Pattern color spaces
  436. auto last_arg = args.last();
  437. if (last_arg.has<NonnullRefPtr<Object>>() && last_arg.get<NonnullRefPtr<Object>>()->is<NameObject>())
  438. TODO();
  439. state().stroke_color = state().stroke_color_space->color(args);
  440. return {};
  441. }
  442. RENDERER_HANDLER(set_painting_color)
  443. {
  444. state().paint_color = state().paint_color_space->color(args);
  445. return {};
  446. }
  447. RENDERER_HANDLER(set_painting_color_extended)
  448. {
  449. // FIXME: Handle Pattern color spaces
  450. auto last_arg = args.last();
  451. if (last_arg.has<NonnullRefPtr<Object>>() && last_arg.get<NonnullRefPtr<Object>>()->is<NameObject>())
  452. TODO();
  453. state().paint_color = state().paint_color_space->color(args);
  454. return {};
  455. }
  456. RENDERER_HANDLER(set_stroking_color_and_space_to_gray)
  457. {
  458. state().stroke_color_space = DeviceGrayColorSpace::the();
  459. state().stroke_color = state().stroke_color_space->color(args);
  460. return {};
  461. }
  462. RENDERER_HANDLER(set_painting_color_and_space_to_gray)
  463. {
  464. state().paint_color_space = DeviceGrayColorSpace::the();
  465. state().paint_color = state().paint_color_space->color(args);
  466. return {};
  467. }
  468. RENDERER_HANDLER(set_stroking_color_and_space_to_rgb)
  469. {
  470. state().stroke_color_space = DeviceRGBColorSpace::the();
  471. state().stroke_color = state().stroke_color_space->color(args);
  472. return {};
  473. }
  474. RENDERER_HANDLER(set_painting_color_and_space_to_rgb)
  475. {
  476. state().paint_color_space = DeviceRGBColorSpace::the();
  477. state().paint_color = state().paint_color_space->color(args);
  478. return {};
  479. }
  480. RENDERER_HANDLER(set_stroking_color_and_space_to_cmyk)
  481. {
  482. state().stroke_color_space = DeviceCMYKColorSpace::the();
  483. state().stroke_color = state().stroke_color_space->color(args);
  484. return {};
  485. }
  486. RENDERER_HANDLER(set_painting_color_and_space_to_cmyk)
  487. {
  488. state().paint_color_space = DeviceCMYKColorSpace::the();
  489. state().paint_color = state().paint_color_space->color(args);
  490. return {};
  491. }
  492. RENDERER_TODO(shade)
  493. RENDERER_TODO(inline_image_begin)
  494. RENDERER_TODO(inline_image_begin_data)
  495. RENDERER_TODO(inline_image_end)
  496. RENDERER_TODO(paint_xobject)
  497. RENDERER_HANDLER(marked_content_point)
  498. {
  499. // nop
  500. return {};
  501. }
  502. RENDERER_HANDLER(marked_content_designate)
  503. {
  504. // nop
  505. return {};
  506. }
  507. RENDERER_HANDLER(marked_content_begin)
  508. {
  509. // nop
  510. return {};
  511. }
  512. RENDERER_HANDLER(marked_content_begin_with_property_list)
  513. {
  514. // nop
  515. return {};
  516. }
  517. RENDERER_HANDLER(marked_content_end)
  518. {
  519. // nop
  520. return {};
  521. }
  522. RENDERER_TODO(compatibility_begin)
  523. RENDERER_TODO(compatibility_end)
  524. template<typename T>
  525. Gfx::Point<T> Renderer::map(T x, T y) const
  526. {
  527. auto mapped = state().ctm.map(Gfx::Point<T> { x, y });
  528. return { mapped.x(), static_cast<T>(m_bitmap->height()) - mapped.y() };
  529. }
  530. template<typename T>
  531. Gfx::Size<T> Renderer::map(Gfx::Size<T> size) const
  532. {
  533. return state().ctm.map(size);
  534. }
  535. template<typename T>
  536. Gfx::Rect<T> Renderer::map(Gfx::Rect<T> rect) const
  537. {
  538. return state().ctm.map(rect);
  539. }
  540. PDFErrorOr<void> Renderer::set_graphics_state_from_dict(NonnullRefPtr<DictObject> dict)
  541. {
  542. if (dict->contains(CommonNames::LW))
  543. TRY(handle_set_line_width({ dict->get_value(CommonNames::LW) }));
  544. if (dict->contains(CommonNames::LC))
  545. TRY(handle_set_line_cap({ dict->get_value(CommonNames::LC) }));
  546. if (dict->contains(CommonNames::LJ))
  547. TRY(handle_set_line_join({ dict->get_value(CommonNames::LJ) }));
  548. if (dict->contains(CommonNames::ML))
  549. TRY(handle_set_miter_limit({ dict->get_value(CommonNames::ML) }));
  550. if (dict->contains(CommonNames::D)) {
  551. auto array = MUST(dict->get_array(m_document, CommonNames::D));
  552. TRY(handle_set_dash_pattern(array->elements()));
  553. }
  554. if (dict->contains(CommonNames::FL))
  555. TRY(handle_set_flatness_tolerance({ dict->get_value(CommonNames::FL) }));
  556. return {};
  557. }
  558. void Renderer::show_text(String const& string)
  559. {
  560. auto& text_rendering_matrix = calculate_text_rendering_matrix();
  561. auto font_size = text_rendering_matrix.x_scale() * text_state().font_size;
  562. auto glyph_position = text_rendering_matrix.map(Gfx::FloatPoint { 0.0f, 0.0f });
  563. auto original_position = glyph_position;
  564. for (auto char_code : string.bytes()) {
  565. auto code_point = text_state().font->char_code_to_code_point(char_code);
  566. auto char_width = text_state().font->get_char_width(char_code);
  567. auto glyph_width = char_width * font_size;
  568. if (code_point != 0x20)
  569. text_state().font->draw_glyph(m_painter, glyph_position.to_type<int>(), glyph_width, char_code, state().paint_color);
  570. auto tx = glyph_width;
  571. tx += text_state().character_spacing;
  572. if (code_point == ' ')
  573. tx += text_state().word_spacing;
  574. tx *= text_state().horizontal_scaling;
  575. glyph_position += { tx, 0.0f };
  576. }
  577. // Update text matrix
  578. auto delta_x = glyph_position.x() - original_position.x();
  579. m_text_rendering_matrix_is_dirty = true;
  580. m_text_matrix.translate(delta_x / text_rendering_matrix.x_scale(), 0.0f);
  581. }
  582. PDFErrorOr<NonnullRefPtr<ColorSpace>> Renderer::get_color_space(Value const& value)
  583. {
  584. auto name = value.get<NonnullRefPtr<Object>>()->cast<NameObject>()->name();
  585. return TRY(ColorSpace::create(m_document, name, m_page));
  586. }
  587. Gfx::AffineTransform const& Renderer::calculate_text_rendering_matrix()
  588. {
  589. if (m_text_rendering_matrix_is_dirty) {
  590. m_text_rendering_matrix = Gfx::AffineTransform(
  591. text_state().horizontal_scaling,
  592. 0.0f,
  593. 0.0f,
  594. 1.0f,
  595. 0.0f,
  596. text_state().rise);
  597. m_text_rendering_matrix.multiply(state().ctm);
  598. m_text_rendering_matrix.multiply(m_text_matrix);
  599. m_text_rendering_matrix_is_dirty = false;
  600. }
  601. return m_text_rendering_matrix;
  602. }
  603. }