Renderer.cpp 19 KB

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