Renderer.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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_TODO(set_graphics_state_from_dict);
  125. RENDERER_HANDLER(path_move)
  126. {
  127. m_current_path.move_to(map(args[0].to_float(), args[1].to_float()));
  128. }
  129. RENDERER_HANDLER(path_line)
  130. {
  131. VERIFY(!m_current_path.segments().is_empty());
  132. m_current_path.line_to(map(args[0].to_float(), args[1].to_float()));
  133. }
  134. RENDERER_TODO(path_cubic_bezier_curve);
  135. RENDERER_TODO(path_cubic_bezier_curve_no_first_control);
  136. RENDERER_TODO(path_cubic_bezier_curve_no_second_control);
  137. RENDERER_HANDLER(path_close)
  138. {
  139. m_current_path.close();
  140. }
  141. RENDERER_HANDLER(path_append_rect)
  142. {
  143. auto pos = map(args[0].to_float(), args[1].to_float());
  144. auto size = map(Gfx::FloatSize { args[2].to_float(), args[3].to_float() });
  145. m_current_path.move_to(pos);
  146. m_current_path.line_to({ pos.x() + size.width(), pos.y() });
  147. m_current_path.line_to({ pos.x() + size.width(), pos.y() + size.height() });
  148. m_current_path.line_to({ pos.x(), pos.y() + size.height() });
  149. m_current_path.close();
  150. }
  151. RENDERER_HANDLER(path_stroke)
  152. {
  153. m_painter.stroke_path(m_current_path, state().stroke_color, state().line_width);
  154. m_current_path.clear();
  155. }
  156. RENDERER_HANDLER(path_close_and_stroke)
  157. {
  158. m_current_path.close();
  159. handle_path_stroke(args);
  160. }
  161. RENDERER_HANDLER(path_fill_nonzero)
  162. {
  163. m_painter.fill_path(m_current_path, state().paint_color, Gfx::Painter::WindingRule::Nonzero);
  164. m_current_path.clear();
  165. }
  166. RENDERER_HANDLER(path_fill_nonzero_deprecated)
  167. {
  168. handle_path_fill_nonzero(args);
  169. }
  170. RENDERER_HANDLER(path_fill_evenodd)
  171. {
  172. m_painter.fill_path(m_current_path, state().paint_color, Gfx::Painter::WindingRule::EvenOdd);
  173. m_current_path.clear();
  174. }
  175. RENDERER_HANDLER(path_fill_stroke_nonzero)
  176. {
  177. m_painter.stroke_path(m_current_path, state().stroke_color, state().line_width);
  178. handle_path_fill_nonzero(args);
  179. }
  180. RENDERER_HANDLER(path_fill_stroke_evenodd)
  181. {
  182. m_painter.stroke_path(m_current_path, state().stroke_color, state().line_width);
  183. handle_path_fill_evenodd(args);
  184. }
  185. RENDERER_HANDLER(path_close_fill_stroke_nonzero)
  186. {
  187. m_current_path.close();
  188. handle_path_fill_stroke_nonzero(args);
  189. }
  190. RENDERER_HANDLER(path_close_fill_stroke_evenodd)
  191. {
  192. m_current_path.close();
  193. handle_path_fill_stroke_evenodd(args);
  194. }
  195. RENDERER_HANDLER(path_end)
  196. {
  197. }
  198. RENDERER_HANDLER(path_intersect_clip_nonzero)
  199. {
  200. // FIXME: Support arbitrary path clipping in the painter and utilize that here
  201. auto bounding_box = map(m_current_path.bounding_box());
  202. m_painter.add_clip_rect(bounding_box.to_type<int>());
  203. }
  204. RENDERER_HANDLER(path_intersect_clip_evenodd)
  205. {
  206. // FIXME: Support arbitrary path clipping in the painter and utilize that here
  207. auto bounding_box = map(m_current_path.bounding_box());
  208. m_painter.add_clip_rect(bounding_box.to_type<int>());
  209. }
  210. RENDERER_HANDLER(text_begin)
  211. {
  212. m_text_matrix = Gfx::AffineTransform();
  213. m_text_line_matrix = Gfx::AffineTransform();
  214. }
  215. RENDERER_HANDLER(text_end)
  216. {
  217. // FIXME: Do we need to do anything here?
  218. }
  219. RENDERER_HANDLER(text_set_char_space)
  220. {
  221. text_state().character_spacing = args[0].to_float();
  222. }
  223. RENDERER_HANDLER(text_set_word_space)
  224. {
  225. text_state().word_spacing = args[0].to_float();
  226. }
  227. RENDERER_HANDLER(text_set_horizontal_scale)
  228. {
  229. m_text_rendering_matrix_is_dirty = true;
  230. text_state().horizontal_scaling = args[0].to_float() / 100.0f;
  231. }
  232. RENDERER_HANDLER(text_set_leading)
  233. {
  234. text_state().leading = args[0].to_float();
  235. }
  236. RENDERER_HANDLER(text_set_font)
  237. {
  238. auto target_font_name = m_document->resolve_to<NameObject>(args[0])->name();
  239. auto fonts_dictionary = m_page.resources->get_dict(m_document, CommonNames::Font);
  240. auto font_dictionary = fonts_dictionary->get_dict(m_document, target_font_name);
  241. // FIXME: We do not yet have the standard 14 fonts, as some of them are not open fonts,
  242. // so we just use LiberationSerif for everything
  243. auto font_name = font_dictionary->get_name(m_document, CommonNames::BaseFont)->name().to_lowercase();
  244. auto font_view = font_name.view();
  245. bool is_bold = font_view.contains("bold");
  246. bool is_italic = font_view.contains("italic");
  247. String font_variant;
  248. if (is_bold && is_italic) {
  249. font_variant = "BoldItalic";
  250. } else if (is_bold) {
  251. font_variant = "Bold";
  252. } else if (is_italic) {
  253. font_variant = "Italic";
  254. } else {
  255. font_variant = "Regular";
  256. }
  257. auto specified_font_size = args[1].to_float();
  258. // FIXME: This scaling should occur when drawing the glyph rather than selecting the font.
  259. // This should be removed when the painter supports arbitrary bitmap scaling.
  260. specified_font_size *= state().ctm.x_scale();
  261. text_state().font = Gfx::FontDatabase::the().get("Liberation Serif", font_variant, static_cast<int>(specified_font_size));
  262. VERIFY(text_state().font);
  263. m_text_rendering_matrix_is_dirty = true;
  264. }
  265. RENDERER_HANDLER(text_set_rendering_mode)
  266. {
  267. text_state().rendering_mode = static_cast<TextRenderingMode>(args[0].as_int());
  268. }
  269. RENDERER_HANDLER(text_set_rise)
  270. {
  271. m_text_rendering_matrix_is_dirty = true;
  272. text_state().rise = args[0].to_float();
  273. }
  274. RENDERER_HANDLER(text_next_line_offset)
  275. {
  276. Gfx::AffineTransform transform(1.0f, 0.0f, 0.0f, 1.0f, args[0].to_float(), args[1].to_float());
  277. transform.multiply(m_text_line_matrix);
  278. m_text_matrix = transform;
  279. m_text_line_matrix = transform;
  280. m_text_rendering_matrix_is_dirty = true;
  281. }
  282. RENDERER_HANDLER(text_next_line_and_set_leading)
  283. {
  284. text_state().leading = -args[1].to_float();
  285. handle_text_next_line_offset(args);
  286. }
  287. RENDERER_HANDLER(text_set_matrix_and_line_matrix)
  288. {
  289. Gfx::AffineTransform new_transform(
  290. args[0].to_float(),
  291. args[1].to_float(),
  292. args[2].to_float(),
  293. args[3].to_float(),
  294. args[4].to_float(),
  295. args[5].to_float());
  296. m_text_line_matrix = new_transform;
  297. m_text_matrix = new_transform;
  298. m_text_rendering_matrix_is_dirty = true;
  299. }
  300. RENDERER_HANDLER(text_next_line)
  301. {
  302. handle_text_next_line_offset({ 0.0f, -text_state().leading });
  303. }
  304. RENDERER_HANDLER(text_show_string)
  305. {
  306. auto text = m_document->resolve_to<StringObject>(args[0])->string();
  307. show_text(text);
  308. }
  309. RENDERER_HANDLER(text_next_line_show_string)
  310. {
  311. handle_text_next_line(args);
  312. handle_text_show_string(args);
  313. }
  314. RENDERER_TODO(text_next_line_show_string_set_spacing);
  315. RENDERER_TODO(text_show_string_array);
  316. RENDERER_TODO(type3_font_set_glyph_width);
  317. RENDERER_TODO(type3_font_set_glyph_width_and_bbox);
  318. RENDERER_HANDLER(set_stroking_space)
  319. {
  320. state().stroke_color_space = get_color_space(args[0]);
  321. VERIFY(state().stroke_color_space);
  322. }
  323. RENDERER_HANDLER(set_painting_space)
  324. {
  325. state().paint_color_space = get_color_space(args[0]);
  326. VERIFY(state().paint_color_space);
  327. }
  328. RENDERER_HANDLER(set_stroking_color)
  329. {
  330. state().stroke_color = state().stroke_color_space->color(args);
  331. }
  332. RENDERER_TODO(set_stroking_color_extended);
  333. RENDERER_HANDLER(set_painting_color)
  334. {
  335. state().paint_color = state().paint_color_space->color(args);
  336. }
  337. RENDERER_TODO(set_painting_color_extended);
  338. RENDERER_HANDLER(set_stroking_color_and_space_to_gray)
  339. {
  340. state().stroke_color_space = DeviceGrayColorSpace::the();
  341. state().stroke_color = state().stroke_color_space->color(args);
  342. }
  343. RENDERER_HANDLER(set_painting_color_and_space_to_gray)
  344. {
  345. state().paint_color_space = DeviceGrayColorSpace::the();
  346. state().paint_color = state().paint_color_space->color(args);
  347. }
  348. RENDERER_HANDLER(set_stroking_color_and_space_to_rgb)
  349. {
  350. state().stroke_color_space = DeviceRGBColorSpace::the();
  351. state().stroke_color = state().stroke_color_space->color(args);
  352. }
  353. RENDERER_HANDLER(set_painting_color_and_space_to_rgb)
  354. {
  355. state().paint_color_space = DeviceRGBColorSpace::the();
  356. state().paint_color = state().paint_color_space->color(args);
  357. }
  358. RENDERER_HANDLER(set_stroking_color_and_space_to_cmyk)
  359. {
  360. state().stroke_color_space = DeviceCMYKColorSpace::the();
  361. state().stroke_color = state().stroke_color_space->color(args);
  362. }
  363. RENDERER_HANDLER(set_painting_color_and_space_to_cmyk)
  364. {
  365. state().paint_color_space = DeviceCMYKColorSpace::the();
  366. state().paint_color = state().paint_color_space->color(args);
  367. }
  368. RENDERER_TODO(shade);
  369. RENDERER_TODO(inline_image_begin);
  370. RENDERER_TODO(inline_image_begin_data);
  371. RENDERER_TODO(inline_image_end);
  372. RENDERER_TODO(paint_xobject);
  373. RENDERER_TODO(marked_content_point);
  374. RENDERER_TODO(marked_content_designate);
  375. RENDERER_TODO(marked_content_begin);
  376. RENDERER_TODO(marked_content_begin_with_property_list);
  377. RENDERER_TODO(marked_content_end);
  378. RENDERER_TODO(compatibility_begin);
  379. RENDERER_TODO(compatibility_end);
  380. template<typename T>
  381. Gfx::Point<T> Renderer::map(T x, T y) const
  382. {
  383. auto mapped = state().ctm.map(Gfx::Point<T> { x, y });
  384. return { mapped.x(), static_cast<T>(m_bitmap->height()) - mapped.y() };
  385. }
  386. template<typename T>
  387. Gfx::Size<T> Renderer::map(Gfx::Size<T> size) const
  388. {
  389. return state().ctm.map(size);
  390. }
  391. template<typename T>
  392. Gfx::Rect<T> Renderer::map(Gfx::Rect<T> rect) const
  393. {
  394. return state().ctm.map(rect);
  395. }
  396. void Renderer::show_text(const String& string, int shift)
  397. {
  398. auto utf = Utf8View(string);
  399. auto& font = text_state().font;
  400. for (auto code_point : utf) {
  401. // FIXME: Don't calculate this matrix for every character
  402. auto& text_rendering_matrix = calculate_text_rendering_matrix();
  403. auto text_position = text_rendering_matrix.map(Gfx::FloatPoint { 0.0f, 0.0f });
  404. text_position.set_y(static_cast<float>(m_bitmap->height()) - text_position.y());
  405. text_position.set_y(text_position.y() - static_cast<float>(font->baseline()));
  406. // FIXME: For some reason, the space character in LiberationSerif is drawn as an exclamation point
  407. if (code_point != 0x20)
  408. m_painter.draw_glyph(text_position.to_type<int>(), code_point, *text_state().font, state().paint_color);
  409. auto glyph_width = static_cast<float>(font->glyph_width(code_point));
  410. auto tx = (glyph_width - static_cast<float>(shift) / 1000.0f);
  411. tx += text_state().character_spacing;
  412. if (code_point == ' ')
  413. tx += text_state().word_spacing;
  414. tx *= text_state().horizontal_scaling;
  415. m_text_rendering_matrix_is_dirty = true;
  416. m_text_matrix = Gfx::AffineTransform(1, 0, 0, 1, tx, 0).multiply(m_text_matrix);
  417. }
  418. }
  419. RefPtr<ColorSpace> Renderer::get_color_space(const Value& value)
  420. {
  421. auto name = object_cast<NameObject>(value.as_object())->name();
  422. // Simple color spaces with no parameters, which can be specified directly
  423. if (name == CommonNames::DeviceGray)
  424. return DeviceGrayColorSpace::the();
  425. if (name == CommonNames::DeviceRGB)
  426. return DeviceRGBColorSpace::the();
  427. if (name == CommonNames::DeviceCMYK)
  428. return DeviceCMYKColorSpace::the();
  429. if (name == CommonNames::Pattern)
  430. TODO();
  431. // The color space is a complex color space with parameters that resides in
  432. // the resource dictionary
  433. auto color_space_resource_dict = m_page.resources->get_dict(m_document, CommonNames::ColorSpace);
  434. if (!color_space_resource_dict->contains(name))
  435. TODO();
  436. auto color_space_array = color_space_resource_dict->get_array(m_document, name);
  437. name = color_space_array->get_name_at(m_document, 0)->name();
  438. Vector<Value> parameters;
  439. parameters.ensure_capacity(color_space_array->size() - 1);
  440. for (size_t i = 1; i < color_space_array->size(); i++)
  441. parameters.unchecked_append(color_space_array->at(i));
  442. if (name == CommonNames::CalRGB)
  443. return CalRGBColorSpace::create(m_document, move(parameters));
  444. TODO();
  445. }
  446. const Gfx::AffineTransform& Renderer::calculate_text_rendering_matrix()
  447. {
  448. if (m_text_rendering_matrix_is_dirty) {
  449. m_text_rendering_matrix = Gfx::AffineTransform(
  450. text_state().horizontal_scaling,
  451. 0.0f,
  452. 0.0f,
  453. 1.0f,
  454. 0.0f,
  455. text_state().rise);
  456. m_text_rendering_matrix.multiply(m_text_matrix);
  457. m_text_rendering_matrix.multiply(state().ctm);
  458. m_text_rendering_matrix_is_dirty = false;
  459. }
  460. return m_text_rendering_matrix;
  461. }
  462. }