2021-05-10 20:50:39 +03:00
|
|
|
/*
|
2022-03-06 03:30:55 +03:00
|
|
|
* Copyright (c) 2021-2022, Matthew Olsson <mattco@serenityos.org>
|
2021-05-10 20:50:39 +03:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/Utf8View.h>
|
2021-05-24 18:15:43 +03:00
|
|
|
#include <LibPDF/CommonNames.h>
|
2022-03-08 22:10:06 +03:00
|
|
|
#include <LibPDF/Fonts.h>
|
2021-05-10 20:50:39 +03:00
|
|
|
#include <LibPDF/Renderer.h>
|
|
|
|
|
2021-05-23 07:09:33 +03:00
|
|
|
#define RENDERER_HANDLER(name) \
|
2022-03-06 04:12:58 +03:00
|
|
|
PDFErrorOr<void> Renderer::handle_##name([[maybe_unused]] Vector<Value> const& args)
|
2021-05-23 07:09:33 +03:00
|
|
|
|
|
|
|
#define RENDERER_TODO(name) \
|
|
|
|
RENDERER_HANDLER(name) \
|
|
|
|
{ \
|
|
|
|
dbgln("[PDF::Renderer] Unsupported draw operation " #name); \
|
|
|
|
TODO(); \
|
|
|
|
}
|
|
|
|
|
2021-05-10 20:50:39 +03:00
|
|
|
namespace PDF {
|
|
|
|
|
2022-03-06 04:12:58 +03:00
|
|
|
PDFErrorOr<void> Renderer::render(Document& document, Page const& page, RefPtr<Gfx::Bitmap> bitmap)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
2022-03-06 04:12:58 +03:00
|
|
|
return Renderer(document, page, bitmap).render();
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2021-06-01 21:16:11 +03:00
|
|
|
Renderer::Renderer(RefPtr<Document> document, Page const& page, RefPtr<Gfx::Bitmap> bitmap)
|
2021-05-10 20:50:39 +03:00
|
|
|
: m_document(document)
|
|
|
|
, m_bitmap(bitmap)
|
|
|
|
, m_page(page)
|
|
|
|
, m_painter(*bitmap)
|
|
|
|
{
|
|
|
|
auto media_box = m_page.media_box;
|
|
|
|
|
2021-05-28 21:55:51 +03:00
|
|
|
Gfx::AffineTransform userspace_matrix;
|
|
|
|
userspace_matrix.translate(media_box.lower_left_x, media_box.lower_left_y);
|
2021-05-10 20:50:39 +03:00
|
|
|
|
|
|
|
float width = media_box.upper_right_x - media_box.lower_left_x;
|
|
|
|
float height = media_box.upper_right_y - media_box.lower_left_y;
|
|
|
|
float scale_x = static_cast<float>(bitmap->width()) / width;
|
|
|
|
float scale_y = static_cast<float>(bitmap->height()) / height;
|
2021-05-28 21:55:51 +03:00
|
|
|
userspace_matrix.scale(scale_x, scale_y);
|
2021-05-10 20:50:39 +03:00
|
|
|
|
2021-05-28 22:18:11 +03:00
|
|
|
// PDF user-space coordinate y axis increases from bottom to top, so we have to
|
|
|
|
// insert a horizontal reflection about the vertical midpoint into our transformation
|
|
|
|
// matrix
|
|
|
|
|
|
|
|
static Gfx::AffineTransform horizontal_reflection_matrix = { 1, 0, 0, -1, 0, 0 };
|
|
|
|
|
|
|
|
userspace_matrix.multiply(horizontal_reflection_matrix);
|
|
|
|
userspace_matrix.translate(0.0f, -height);
|
|
|
|
|
2021-05-28 21:55:51 +03:00
|
|
|
m_graphics_state_stack.append(GraphicsState { userspace_matrix });
|
2021-05-10 20:50:39 +03:00
|
|
|
|
|
|
|
m_bitmap->fill(Gfx::Color::NamedColor::White);
|
|
|
|
}
|
|
|
|
|
2022-03-06 04:12:58 +03:00
|
|
|
PDFErrorOr<void> Renderer::render()
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
|
|
|
// Use our own vector, as the /Content can be an array with multiple
|
|
|
|
// streams which gets concatenated
|
|
|
|
// FIXME: Text operators are supposed to only have effects on the current
|
|
|
|
// stream object. Do the text operators treat this concatenated stream
|
|
|
|
// as one stream or multiple?
|
|
|
|
ByteBuffer byte_buffer;
|
|
|
|
|
2022-03-06 03:30:55 +03:00
|
|
|
if (m_page.contents->is<ArrayObject>()) {
|
|
|
|
auto contents = m_page.contents->cast<ArrayObject>();
|
2021-05-10 20:50:39 +03:00
|
|
|
for (auto& ref : *contents) {
|
2022-03-06 04:12:58 +03:00
|
|
|
auto bytes = TRY(m_document->resolve_to<StreamObject>(ref))->bytes();
|
2021-05-10 20:50:39 +03:00
|
|
|
byte_buffer.append(bytes.data(), bytes.size());
|
|
|
|
}
|
|
|
|
} else {
|
2022-03-06 03:30:55 +03:00
|
|
|
auto bytes = m_page.contents->cast<StreamObject>()->bytes();
|
2021-05-10 20:50:39 +03:00
|
|
|
byte_buffer.append(bytes.data(), bytes.size());
|
|
|
|
}
|
|
|
|
|
2022-03-23 05:22:45 +03:00
|
|
|
auto commands = TRY(Parser::parse_graphics_commands(m_document, byte_buffer));
|
2021-05-10 20:50:39 +03:00
|
|
|
|
|
|
|
for (auto& command : commands)
|
2022-03-06 04:12:58 +03:00
|
|
|
TRY(handle_command(command));
|
|
|
|
|
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2022-03-06 04:12:58 +03:00
|
|
|
PDFErrorOr<void> Renderer::handle_command(Command const& command)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
|
|
|
switch (command.command_type()) {
|
2022-03-06 04:12:58 +03:00
|
|
|
#define V(name, snake_name, symbol) \
|
|
|
|
case CommandType::name: \
|
|
|
|
TRY(handle_##snake_name(command.arguments())); \
|
2021-05-23 07:09:33 +03:00
|
|
|
break;
|
2021-05-10 20:50:39 +03:00
|
|
|
ENUMERATE_COMMANDS(V)
|
|
|
|
#undef V
|
|
|
|
case CommandType::TextNextLineShowString:
|
2022-03-06 04:12:58 +03:00
|
|
|
TRY(handle_text_next_line_show_string(command.arguments()));
|
2021-05-23 07:09:33 +03:00
|
|
|
break;
|
|
|
|
case CommandType::TextNextLineShowStringSetSpacing:
|
2022-03-06 04:12:58 +03:00
|
|
|
TRY(handle_text_next_line_show_string_set_spacing(command.arguments()));
|
2021-05-23 07:09:33 +03:00
|
|
|
break;
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
2022-03-06 04:12:58 +03:00
|
|
|
|
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 07:09:33 +03:00
|
|
|
RENDERER_HANDLER(save_state)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
|
|
|
m_graphics_state_stack.append(state());
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 07:09:33 +03:00
|
|
|
RENDERER_HANDLER(restore_state)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
|
|
|
m_graphics_state_stack.take_last();
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 07:09:33 +03:00
|
|
|
RENDERER_HANDLER(concatenate_matrix)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
|
|
|
Gfx::AffineTransform new_transform(
|
|
|
|
args[0].to_float(),
|
|
|
|
args[1].to_float(),
|
|
|
|
args[2].to_float(),
|
|
|
|
args[3].to_float(),
|
|
|
|
args[4].to_float(),
|
|
|
|
args[5].to_float());
|
|
|
|
|
|
|
|
state().ctm.multiply(new_transform);
|
|
|
|
m_text_rendering_matrix_is_dirty = true;
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 07:09:33 +03:00
|
|
|
RENDERER_HANDLER(set_line_width)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
|
|
|
state().line_width = args[0].to_float();
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 07:09:33 +03:00
|
|
|
RENDERER_HANDLER(set_line_cap)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
2021-09-19 21:56:05 +03:00
|
|
|
state().line_cap_style = static_cast<LineCapStyle>(args[0].get<int>());
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 07:09:33 +03:00
|
|
|
RENDERER_HANDLER(set_line_join)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
2021-09-19 21:56:05 +03:00
|
|
|
state().line_join_style = static_cast<LineJoinStyle>(args[0].get<int>());
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 07:09:33 +03:00
|
|
|
RENDERER_HANDLER(set_miter_limit)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
|
|
|
state().miter_limit = args[0].to_float();
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 07:09:33 +03:00
|
|
|
RENDERER_HANDLER(set_dash_pattern)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
2022-03-06 03:30:55 +03:00
|
|
|
auto dash_array = MUST(m_document->resolve_to<ArrayObject>(args[0]));
|
2021-05-10 20:50:39 +03:00
|
|
|
Vector<int> pattern;
|
|
|
|
for (auto& element : *dash_array)
|
2021-09-19 21:56:05 +03:00
|
|
|
pattern.append(element.get<int>());
|
|
|
|
state().line_dash_pattern = LineDashPattern { pattern, args[1].get<int>() };
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2022-03-06 04:12:58 +03:00
|
|
|
RENDERER_TODO(set_color_rendering_intent)
|
|
|
|
RENDERER_TODO(set_flatness_tolerance)
|
2021-05-28 00:22:24 +03:00
|
|
|
|
|
|
|
RENDERER_HANDLER(set_graphics_state_from_dict)
|
|
|
|
{
|
|
|
|
VERIFY(m_page.resources->contains(CommonNames::ExtGState));
|
2022-03-06 03:30:55 +03:00
|
|
|
auto dict_name = MUST(m_document->resolve_to<NameObject>(args[0]))->name();
|
|
|
|
auto ext_gstate_dict = MUST(m_page.resources->get_dict(m_document, CommonNames::ExtGState));
|
|
|
|
auto target_dict = MUST(ext_gstate_dict->get_dict(m_document, dict_name));
|
2022-03-06 04:12:58 +03:00
|
|
|
TRY(set_graphics_state_from_dict(target_dict));
|
|
|
|
return {};
|
2021-05-28 00:22:24 +03:00
|
|
|
}
|
2021-05-10 20:50:39 +03:00
|
|
|
|
2021-05-23 21:43:28 +03:00
|
|
|
RENDERER_HANDLER(path_move)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
2021-05-23 21:43:28 +03:00
|
|
|
m_current_path.move_to(map(args[0].to_float(), args[1].to_float()));
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 07:09:33 +03:00
|
|
|
RENDERER_HANDLER(path_line)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
2021-05-23 21:43:28 +03:00
|
|
|
VERIFY(!m_current_path.segments().is_empty());
|
|
|
|
m_current_path.line_to(map(args[0].to_float(), args[1].to_float()));
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2022-03-06 04:12:58 +03:00
|
|
|
RENDERER_TODO(path_cubic_bezier_curve)
|
|
|
|
RENDERER_TODO(path_cubic_bezier_curve_no_first_control)
|
|
|
|
RENDERER_TODO(path_cubic_bezier_curve_no_second_control)
|
2021-05-23 07:09:33 +03:00
|
|
|
|
|
|
|
RENDERER_HANDLER(path_close)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
2021-05-23 21:43:28 +03:00
|
|
|
m_current_path.close();
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 07:09:33 +03:00
|
|
|
RENDERER_HANDLER(path_append_rect)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
|
|
|
auto pos = map(args[0].to_float(), args[1].to_float());
|
|
|
|
auto size = map(Gfx::FloatSize { args[2].to_float(), args[3].to_float() });
|
|
|
|
|
2021-05-23 21:43:28 +03:00
|
|
|
m_current_path.move_to(pos);
|
|
|
|
m_current_path.line_to({ pos.x() + size.width(), pos.y() });
|
|
|
|
m_current_path.line_to({ pos.x() + size.width(), pos.y() + size.height() });
|
|
|
|
m_current_path.line_to({ pos.x(), pos.y() + size.height() });
|
|
|
|
m_current_path.close();
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 07:09:33 +03:00
|
|
|
RENDERER_HANDLER(path_stroke)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
2021-05-23 21:43:28 +03:00
|
|
|
m_painter.stroke_path(m_current_path, state().stroke_color, state().line_width);
|
|
|
|
m_current_path.clear();
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 07:09:33 +03:00
|
|
|
RENDERER_HANDLER(path_close_and_stroke)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
2021-05-23 21:43:28 +03:00
|
|
|
m_current_path.close();
|
2022-03-06 04:12:58 +03:00
|
|
|
TRY(handle_path_stroke(args));
|
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 07:09:33 +03:00
|
|
|
RENDERER_HANDLER(path_fill_nonzero)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
2021-05-23 21:43:28 +03:00
|
|
|
m_painter.fill_path(m_current_path, state().paint_color, Gfx::Painter::WindingRule::Nonzero);
|
|
|
|
m_current_path.clear();
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 07:09:33 +03:00
|
|
|
RENDERER_HANDLER(path_fill_nonzero_deprecated)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
2022-03-06 04:12:58 +03:00
|
|
|
TRY(handle_path_fill_nonzero(args));
|
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 07:09:33 +03:00
|
|
|
RENDERER_HANDLER(path_fill_evenodd)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
2021-05-23 21:43:28 +03:00
|
|
|
m_painter.fill_path(m_current_path, state().paint_color, Gfx::Painter::WindingRule::EvenOdd);
|
|
|
|
m_current_path.clear();
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 07:09:33 +03:00
|
|
|
RENDERER_HANDLER(path_fill_stroke_nonzero)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
2021-05-23 21:43:28 +03:00
|
|
|
m_painter.stroke_path(m_current_path, state().stroke_color, state().line_width);
|
2022-03-06 04:12:58 +03:00
|
|
|
TRY(handle_path_fill_nonzero(args));
|
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 07:09:33 +03:00
|
|
|
RENDERER_HANDLER(path_fill_stroke_evenodd)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
2021-05-23 21:43:28 +03:00
|
|
|
m_painter.stroke_path(m_current_path, state().stroke_color, state().line_width);
|
2022-03-06 04:12:58 +03:00
|
|
|
TRY(handle_path_fill_evenodd(args));
|
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 07:09:33 +03:00
|
|
|
RENDERER_HANDLER(path_close_fill_stroke_nonzero)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
2021-05-23 21:43:28 +03:00
|
|
|
m_current_path.close();
|
2022-03-06 04:12:58 +03:00
|
|
|
TRY(handle_path_fill_stroke_nonzero(args));
|
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 07:09:33 +03:00
|
|
|
RENDERER_HANDLER(path_close_fill_stroke_evenodd)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
2021-05-23 21:43:28 +03:00
|
|
|
m_current_path.close();
|
2022-03-06 04:12:58 +03:00
|
|
|
TRY(handle_path_fill_stroke_evenodd(args));
|
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 07:09:33 +03:00
|
|
|
RENDERER_HANDLER(path_end)
|
|
|
|
{
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-23 07:09:33 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 21:43:28 +03:00
|
|
|
RENDERER_HANDLER(path_intersect_clip_nonzero)
|
|
|
|
{
|
|
|
|
// FIXME: Support arbitrary path clipping in the painter and utilize that here
|
|
|
|
auto bounding_box = map(m_current_path.bounding_box());
|
|
|
|
m_painter.add_clip_rect(bounding_box.to_type<int>());
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-23 21:43:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
RENDERER_HANDLER(path_intersect_clip_evenodd)
|
|
|
|
{
|
|
|
|
// FIXME: Support arbitrary path clipping in the painter and utilize that here
|
|
|
|
auto bounding_box = map(m_current_path.bounding_box());
|
|
|
|
m_painter.add_clip_rect(bounding_box.to_type<int>());
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-23 21:43:28 +03:00
|
|
|
}
|
2021-05-23 07:09:33 +03:00
|
|
|
|
|
|
|
RENDERER_HANDLER(text_begin)
|
|
|
|
{
|
|
|
|
m_text_matrix = Gfx::AffineTransform();
|
|
|
|
m_text_line_matrix = Gfx::AffineTransform();
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-23 07:09:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
RENDERER_HANDLER(text_end)
|
|
|
|
{
|
|
|
|
// FIXME: Do we need to do anything here?
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-23 07:09:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
RENDERER_HANDLER(text_set_char_space)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
|
|
|
text_state().character_spacing = args[0].to_float();
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 07:09:33 +03:00
|
|
|
RENDERER_HANDLER(text_set_word_space)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
|
|
|
text_state().word_spacing = args[0].to_float();
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 07:09:33 +03:00
|
|
|
RENDERER_HANDLER(text_set_horizontal_scale)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
|
|
|
m_text_rendering_matrix_is_dirty = true;
|
|
|
|
text_state().horizontal_scaling = args[0].to_float() / 100.0f;
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 07:09:33 +03:00
|
|
|
RENDERER_HANDLER(text_set_leading)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
|
|
|
text_state().leading = args[0].to_float();
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 07:09:33 +03:00
|
|
|
RENDERER_HANDLER(text_set_font)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
2022-03-06 03:30:55 +03:00
|
|
|
auto target_font_name = MUST(m_document->resolve_to<NameObject>(args[0]))->name();
|
|
|
|
auto fonts_dictionary = MUST(m_page.resources->get_dict(m_document, CommonNames::Font));
|
|
|
|
auto font_dictionary = MUST(fonts_dictionary->get_dict(m_document, target_font_name));
|
2022-03-08 22:10:06 +03:00
|
|
|
auto font = TRY(PDFFont::create(m_document, font_dictionary));
|
|
|
|
text_state().font = font;
|
2021-05-10 20:50:39 +03:00
|
|
|
|
|
|
|
// FIXME: We do not yet have the standard 14 fonts, as some of them are not open fonts,
|
2022-03-08 22:10:06 +03:00
|
|
|
// so we just use LiberationSerif for everything
|
2021-05-10 20:50:39 +03:00
|
|
|
|
2022-03-06 03:30:55 +03:00
|
|
|
auto font_name = MUST(font_dictionary->get_name(m_document, CommonNames::BaseFont))->name().to_lowercase();
|
2021-05-10 20:50:39 +03:00
|
|
|
auto font_view = font_name.view();
|
|
|
|
bool is_bold = font_view.contains("bold");
|
|
|
|
bool is_italic = font_view.contains("italic");
|
|
|
|
|
|
|
|
String font_variant;
|
|
|
|
|
|
|
|
if (is_bold && is_italic) {
|
|
|
|
font_variant = "BoldItalic";
|
|
|
|
} else if (is_bold) {
|
|
|
|
font_variant = "Bold";
|
|
|
|
} else if (is_italic) {
|
|
|
|
font_variant = "Italic";
|
|
|
|
} else {
|
|
|
|
font_variant = "Regular";
|
|
|
|
}
|
|
|
|
|
2021-05-28 21:55:51 +03:00
|
|
|
text_state().font_size = args[1].to_float();
|
|
|
|
text_state().font_variant = font_variant;
|
2021-05-10 20:50:39 +03:00
|
|
|
|
|
|
|
m_text_rendering_matrix_is_dirty = true;
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 07:09:33 +03:00
|
|
|
RENDERER_HANDLER(text_set_rendering_mode)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
2021-09-19 21:56:05 +03:00
|
|
|
text_state().rendering_mode = static_cast<TextRenderingMode>(args[0].get<int>());
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 07:09:33 +03:00
|
|
|
RENDERER_HANDLER(text_set_rise)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
|
|
|
m_text_rendering_matrix_is_dirty = true;
|
|
|
|
text_state().rise = args[0].to_float();
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 07:09:33 +03:00
|
|
|
RENDERER_HANDLER(text_next_line_offset)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
|
|
|
Gfx::AffineTransform transform(1.0f, 0.0f, 0.0f, 1.0f, args[0].to_float(), args[1].to_float());
|
|
|
|
transform.multiply(m_text_line_matrix);
|
|
|
|
m_text_matrix = transform;
|
|
|
|
m_text_line_matrix = transform;
|
|
|
|
m_text_rendering_matrix_is_dirty = true;
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 07:09:33 +03:00
|
|
|
RENDERER_HANDLER(text_next_line_and_set_leading)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
|
|
|
text_state().leading = -args[1].to_float();
|
2022-03-06 04:12:58 +03:00
|
|
|
TRY(handle_text_next_line_offset(args));
|
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 07:09:33 +03:00
|
|
|
RENDERER_HANDLER(text_set_matrix_and_line_matrix)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
|
|
|
Gfx::AffineTransform new_transform(
|
|
|
|
args[0].to_float(),
|
|
|
|
args[1].to_float(),
|
|
|
|
args[2].to_float(),
|
|
|
|
args[3].to_float(),
|
|
|
|
args[4].to_float(),
|
|
|
|
args[5].to_float());
|
|
|
|
m_text_line_matrix = new_transform;
|
|
|
|
m_text_matrix = new_transform;
|
|
|
|
m_text_rendering_matrix_is_dirty = true;
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 07:09:33 +03:00
|
|
|
RENDERER_HANDLER(text_next_line)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
2022-03-06 04:12:58 +03:00
|
|
|
TRY(handle_text_next_line_offset({ 0.0f, -text_state().leading }));
|
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 07:09:33 +03:00
|
|
|
RENDERER_HANDLER(text_show_string)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
2022-03-06 03:30:55 +03:00
|
|
|
auto text = MUST(m_document->resolve_to<StringObject>(args[0]))->string();
|
2021-05-10 20:50:39 +03:00
|
|
|
show_text(text);
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 07:09:33 +03:00
|
|
|
RENDERER_HANDLER(text_next_line_show_string)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
2022-03-06 04:12:58 +03:00
|
|
|
TRY(handle_text_next_line(args));
|
|
|
|
TRY(handle_text_show_string(args));
|
|
|
|
return {};
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2022-03-06 04:12:58 +03:00
|
|
|
RENDERER_TODO(text_next_line_show_string_set_spacing)
|
2021-05-28 00:53:10 +03:00
|
|
|
|
|
|
|
RENDERER_HANDLER(text_show_string_array)
|
|
|
|
{
|
2022-03-06 03:30:55 +03:00
|
|
|
auto elements = MUST(m_document->resolve_to<ArrayObject>(args[0]))->elements();
|
2021-05-28 00:53:10 +03:00
|
|
|
float next_shift = 0.0f;
|
|
|
|
|
|
|
|
for (auto& element : elements) {
|
2021-09-19 21:56:05 +03:00
|
|
|
if (element.has<int>()) {
|
|
|
|
next_shift = element.get<int>();
|
|
|
|
} else if (element.has<float>()) {
|
|
|
|
next_shift = element.get<float>();
|
2021-05-28 00:53:10 +03:00
|
|
|
} else {
|
2022-03-06 03:30:55 +03:00
|
|
|
auto str = element.get<NonnullRefPtr<Object>>()->cast<StringObject>()->string();
|
2021-05-28 00:53:10 +03:00
|
|
|
show_text(str, next_shift);
|
|
|
|
}
|
|
|
|
}
|
2022-03-06 04:12:58 +03:00
|
|
|
|
|
|
|
return {};
|
2021-05-28 00:53:10 +03:00
|
|
|
}
|
|
|
|
|
2022-03-06 04:12:58 +03:00
|
|
|
RENDERER_TODO(type3_font_set_glyph_width)
|
|
|
|
RENDERER_TODO(type3_font_set_glyph_width_and_bbox)
|
2021-05-23 22:53:38 +03:00
|
|
|
|
|
|
|
RENDERER_HANDLER(set_stroking_space)
|
|
|
|
{
|
2022-03-24 20:08:08 +03:00
|
|
|
state().stroke_color_space = TRY(get_color_space(args[0]));
|
2021-05-28 00:01:37 +03:00
|
|
|
VERIFY(state().stroke_color_space);
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-23 22:53:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
RENDERER_HANDLER(set_painting_space)
|
|
|
|
{
|
2022-03-24 20:08:08 +03:00
|
|
|
state().paint_color_space = TRY(get_color_space(args[0]));
|
2021-05-28 00:01:37 +03:00
|
|
|
VERIFY(state().paint_color_space);
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-23 22:53:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
RENDERER_HANDLER(set_stroking_color)
|
|
|
|
{
|
2021-05-28 00:01:37 +03:00
|
|
|
state().stroke_color = state().stroke_color_space->color(args);
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-23 22:53:38 +03:00
|
|
|
}
|
|
|
|
|
2022-03-24 20:24:00 +03:00
|
|
|
RENDERER_HANDLER(set_stroking_color_extended)
|
|
|
|
{
|
|
|
|
// FIXME: Handle Pattern color spaces
|
|
|
|
auto last_arg = args.last();
|
|
|
|
if (last_arg.has<NonnullRefPtr<Object>>() && last_arg.get<NonnullRefPtr<Object>>()->is<NameObject>())
|
|
|
|
TODO();
|
|
|
|
|
|
|
|
state().stroke_color = state().stroke_color_space->color(args);
|
|
|
|
return {};
|
|
|
|
}
|
2021-05-23 22:53:38 +03:00
|
|
|
|
|
|
|
RENDERER_HANDLER(set_painting_color)
|
|
|
|
{
|
2021-05-28 00:01:37 +03:00
|
|
|
state().paint_color = state().paint_color_space->color(args);
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-23 22:53:38 +03:00
|
|
|
}
|
|
|
|
|
2022-03-24 20:24:00 +03:00
|
|
|
RENDERER_HANDLER(set_painting_color_extended)
|
|
|
|
{
|
|
|
|
// FIXME: Handle Pattern color spaces
|
|
|
|
auto last_arg = args.last();
|
|
|
|
if (last_arg.has<NonnullRefPtr<Object>>() && last_arg.get<NonnullRefPtr<Object>>()->is<NameObject>())
|
|
|
|
TODO();
|
|
|
|
|
|
|
|
state().paint_color = state().paint_color_space->color(args);
|
|
|
|
return {};
|
|
|
|
}
|
2021-05-23 22:53:38 +03:00
|
|
|
|
|
|
|
RENDERER_HANDLER(set_stroking_color_and_space_to_gray)
|
|
|
|
{
|
2021-05-28 00:01:37 +03:00
|
|
|
state().stroke_color_space = DeviceGrayColorSpace::the();
|
|
|
|
state().stroke_color = state().stroke_color_space->color(args);
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-23 22:53:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
RENDERER_HANDLER(set_painting_color_and_space_to_gray)
|
|
|
|
{
|
2021-05-28 00:01:37 +03:00
|
|
|
state().paint_color_space = DeviceGrayColorSpace::the();
|
|
|
|
state().paint_color = state().paint_color_space->color(args);
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-23 22:53:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
RENDERER_HANDLER(set_stroking_color_and_space_to_rgb)
|
|
|
|
{
|
2021-05-28 00:01:37 +03:00
|
|
|
state().stroke_color_space = DeviceRGBColorSpace::the();
|
|
|
|
state().stroke_color = state().stroke_color_space->color(args);
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-23 22:53:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
RENDERER_HANDLER(set_painting_color_and_space_to_rgb)
|
|
|
|
{
|
2021-05-28 00:01:37 +03:00
|
|
|
state().paint_color_space = DeviceRGBColorSpace::the();
|
|
|
|
state().paint_color = state().paint_color_space->color(args);
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-23 22:53:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
RENDERER_HANDLER(set_stroking_color_and_space_to_cmyk)
|
|
|
|
{
|
2021-05-28 00:01:37 +03:00
|
|
|
state().stroke_color_space = DeviceCMYKColorSpace::the();
|
|
|
|
state().stroke_color = state().stroke_color_space->color(args);
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
2021-05-23 22:53:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
RENDERER_HANDLER(set_painting_color_and_space_to_cmyk)
|
|
|
|
{
|
2021-05-28 00:01:37 +03:00
|
|
|
state().paint_color_space = DeviceCMYKColorSpace::the();
|
|
|
|
state().paint_color = state().paint_color_space->color(args);
|
2022-03-06 04:12:58 +03:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
RENDERER_TODO(shade)
|
|
|
|
RENDERER_TODO(inline_image_begin)
|
|
|
|
RENDERER_TODO(inline_image_begin_data)
|
|
|
|
RENDERER_TODO(inline_image_end)
|
|
|
|
RENDERER_TODO(paint_xobject)
|
2022-03-06 08:35:33 +03:00
|
|
|
|
|
|
|
RENDERER_HANDLER(marked_content_point)
|
|
|
|
{
|
|
|
|
// nop
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
RENDERER_HANDLER(marked_content_designate)
|
|
|
|
{
|
|
|
|
// nop
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
RENDERER_HANDLER(marked_content_begin)
|
|
|
|
{
|
|
|
|
// nop
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
RENDERER_HANDLER(marked_content_begin_with_property_list)
|
|
|
|
{
|
|
|
|
// nop
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
RENDERER_HANDLER(marked_content_end)
|
|
|
|
{
|
|
|
|
// nop
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-03-06 04:12:58 +03:00
|
|
|
RENDERER_TODO(compatibility_begin)
|
|
|
|
RENDERER_TODO(compatibility_end)
|
2021-05-23 07:09:33 +03:00
|
|
|
|
2021-05-10 20:50:39 +03:00
|
|
|
template<typename T>
|
|
|
|
Gfx::Point<T> Renderer::map(T x, T y) const
|
|
|
|
{
|
|
|
|
auto mapped = state().ctm.map(Gfx::Point<T> { x, y });
|
|
|
|
return { mapped.x(), static_cast<T>(m_bitmap->height()) - mapped.y() };
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
Gfx::Size<T> Renderer::map(Gfx::Size<T> size) const
|
|
|
|
{
|
|
|
|
return state().ctm.map(size);
|
|
|
|
}
|
|
|
|
|
2021-05-23 22:53:38 +03:00
|
|
|
template<typename T>
|
|
|
|
Gfx::Rect<T> Renderer::map(Gfx::Rect<T> rect) const
|
|
|
|
{
|
|
|
|
return state().ctm.map(rect);
|
|
|
|
}
|
|
|
|
|
2022-03-06 04:12:58 +03:00
|
|
|
PDFErrorOr<void> Renderer::set_graphics_state_from_dict(NonnullRefPtr<DictObject> dict)
|
2021-05-28 00:22:24 +03:00
|
|
|
{
|
|
|
|
if (dict->contains(CommonNames::LW))
|
2022-03-06 04:12:58 +03:00
|
|
|
TRY(handle_set_line_width({ dict->get_value(CommonNames::LW) }));
|
2021-05-28 00:22:24 +03:00
|
|
|
|
|
|
|
if (dict->contains(CommonNames::LC))
|
2022-03-06 04:12:58 +03:00
|
|
|
TRY(handle_set_line_cap({ dict->get_value(CommonNames::LC) }));
|
2021-05-28 00:22:24 +03:00
|
|
|
|
|
|
|
if (dict->contains(CommonNames::LJ))
|
2022-03-06 04:12:58 +03:00
|
|
|
TRY(handle_set_line_join({ dict->get_value(CommonNames::LJ) }));
|
2021-05-28 00:22:24 +03:00
|
|
|
|
|
|
|
if (dict->contains(CommonNames::ML))
|
2022-03-06 04:12:58 +03:00
|
|
|
TRY(handle_set_miter_limit({ dict->get_value(CommonNames::ML) }));
|
2021-05-28 00:22:24 +03:00
|
|
|
|
2022-03-06 04:12:58 +03:00
|
|
|
if (dict->contains(CommonNames::D)) {
|
|
|
|
auto array = MUST(dict->get_array(m_document, CommonNames::D));
|
|
|
|
TRY(handle_set_dash_pattern(array->elements()));
|
|
|
|
}
|
2021-05-28 00:22:24 +03:00
|
|
|
|
|
|
|
if (dict->contains(CommonNames::FL))
|
2022-03-06 04:12:58 +03:00
|
|
|
TRY(handle_set_flatness_tolerance({ dict->get_value(CommonNames::FL) }));
|
|
|
|
|
|
|
|
return {};
|
2021-05-28 00:22:24 +03:00
|
|
|
}
|
|
|
|
|
2021-06-01 21:16:11 +03:00
|
|
|
void Renderer::show_text(String const& string, float shift)
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
2021-05-28 21:55:51 +03:00
|
|
|
auto& text_rendering_matrix = calculate_text_rendering_matrix();
|
|
|
|
|
|
|
|
auto font_size = static_cast<int>(text_rendering_matrix.x_scale() * text_state().font_size);
|
|
|
|
auto font = Gfx::FontDatabase::the().get(text_state().font_family, text_state().font_variant, font_size);
|
|
|
|
VERIFY(font);
|
|
|
|
|
|
|
|
auto glyph_position = text_rendering_matrix.map(Gfx::FloatPoint { 0.0f, 0.0f });
|
|
|
|
// Account for the reversed font baseline
|
|
|
|
glyph_position.set_y(glyph_position.y() - static_cast<float>(font->baseline()));
|
2021-05-10 20:50:39 +03:00
|
|
|
|
2021-05-28 21:55:51 +03:00
|
|
|
auto original_position = glyph_position;
|
2021-05-10 20:50:39 +03:00
|
|
|
|
2022-03-08 22:10:06 +03:00
|
|
|
for (auto char_code : string.bytes()) {
|
|
|
|
auto code_point = text_state().font->char_code_to_code_point(char_code);
|
|
|
|
|
2021-06-01 11:01:11 +03:00
|
|
|
if (code_point != 0x20)
|
2021-05-28 21:55:51 +03:00
|
|
|
m_painter.draw_glyph(glyph_position.to_type<int>(), code_point, *font, state().paint_color);
|
2021-05-10 20:50:39 +03:00
|
|
|
|
2021-06-01 11:01:11 +03:00
|
|
|
auto glyph_width = static_cast<float>(font->glyph_width(code_point));
|
2021-05-28 00:53:10 +03:00
|
|
|
auto tx = (glyph_width - shift / 1000.0f);
|
2021-05-10 20:50:39 +03:00
|
|
|
tx += text_state().character_spacing;
|
|
|
|
|
2021-06-01 11:01:11 +03:00
|
|
|
if (code_point == ' ')
|
2021-05-10 20:50:39 +03:00
|
|
|
tx += text_state().word_spacing;
|
|
|
|
|
|
|
|
tx *= text_state().horizontal_scaling;
|
|
|
|
|
2021-05-28 21:55:51 +03:00
|
|
|
glyph_position += { tx, 0.0f };
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
2021-05-28 21:55:51 +03:00
|
|
|
|
|
|
|
// Update text matrix
|
|
|
|
auto delta_x = glyph_position.x() - original_position.x();
|
|
|
|
m_text_rendering_matrix_is_dirty = true;
|
|
|
|
m_text_matrix = Gfx::AffineTransform(1, 0, 0, 1, delta_x, 0).multiply(m_text_matrix);
|
2021-05-10 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
2022-03-06 03:52:00 +03:00
|
|
|
PDFErrorOr<NonnullRefPtr<ColorSpace>> Renderer::get_color_space(Value const& value)
|
2021-05-23 22:53:38 +03:00
|
|
|
{
|
2022-03-06 03:30:55 +03:00
|
|
|
auto name = value.get<NonnullRefPtr<Object>>()->cast<NameObject>()->name();
|
2022-03-24 20:08:08 +03:00
|
|
|
return TRY(ColorSpace::create(m_document, name, m_page));
|
2021-05-23 22:53:38 +03:00
|
|
|
}
|
|
|
|
|
2021-06-01 21:16:11 +03:00
|
|
|
Gfx::AffineTransform const& Renderer::calculate_text_rendering_matrix()
|
2021-05-10 20:50:39 +03:00
|
|
|
{
|
|
|
|
if (m_text_rendering_matrix_is_dirty) {
|
|
|
|
m_text_rendering_matrix = Gfx::AffineTransform(
|
|
|
|
text_state().horizontal_scaling,
|
|
|
|
0.0f,
|
|
|
|
0.0f,
|
|
|
|
1.0f,
|
|
|
|
0.0f,
|
|
|
|
text_state().rise);
|
|
|
|
m_text_rendering_matrix.multiply(state().ctm);
|
2022-03-07 00:27:04 +03:00
|
|
|
m_text_rendering_matrix.multiply(m_text_matrix);
|
2021-05-10 20:50:39 +03:00
|
|
|
m_text_rendering_matrix_is_dirty = false;
|
|
|
|
}
|
|
|
|
return m_text_rendering_matrix;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|