Renderer.cpp 20 KB

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