Renderer.cpp 18 KB

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