Renderer.cpp 17 KB

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