1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
/*
* Copyright (c) 2021, Matthew Olsson <mattco@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Format.h>
#include <LibGfx/AffineTransform.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/Font.h>
#include <LibGfx/FontDatabase.h>
#include <LibGfx/Painter.h>
#include <LibGfx/Path.h>
#include <LibGfx/Point.h>
#include <LibGfx/Rect.h>
#include <LibGfx/Size.h>
#include <LibPDF/ColorSpace.h>
#include <LibPDF/Document.h>
#include <LibPDF/Object.h>
namespace PDF {
enum class LineCapStyle : u8 {
ButtCap = 0,
RoundCap = 1,
SquareCap = 2,
};
enum class LineJoinStyle : u8 {
Miter = 0,
Round = 1,
Bevel = 2,
};
struct LineDashPattern {
Vector<int> pattern;
int phase;
};
enum class TextRenderingMode : u8 {
Fill = 0,
Stroke = 1,
FillThenStroke = 2,
Invisible = 3,
FillAndClip = 4,
StrokeAndClip = 5,
FillStrokeAndClip = 6,
Clip = 7,
};
struct TextState {
float character_spacing { 3.0f };
float word_spacing { 5.0f };
float horizontal_scaling { 1.0f };
float leading { 0.0f };
FlyString font_family { "Liberation Serif" };
String font_variant { "Regular" };
float font_size { 12.0f };
TextRenderingMode rendering_mode { TextRenderingMode::Fill };
float rise { 0.0f };
bool knockout { true };
};
struct GraphicsState {
Gfx::AffineTransform ctm;
RefPtr<ColorSpace> stroke_color_space { DeviceGrayColorSpace::the() };
RefPtr<ColorSpace> paint_color_space { DeviceGrayColorSpace::the() };
Gfx::Color stroke_color { Gfx::Color::NamedColor::Black };
Gfx::Color paint_color { Gfx::Color::NamedColor::Black };
float line_width { 1.0f };
LineCapStyle line_cap_style { LineCapStyle::ButtCap };
LineJoinStyle line_join_style { LineJoinStyle::Miter };
float miter_limit { 10.0f };
LineDashPattern line_dash_pattern { {}, 0 };
TextState text_state {};
};
class Renderer {
public:
static void render(Document&, Page const&, RefPtr<Gfx::Bitmap>);
private:
Renderer(RefPtr<Document>, Page const&, RefPtr<Gfx::Bitmap>);
void render();
void handle_command(Command const&);
#define V(name, snake_name, symbol) \
void handle_##snake_name(Vector<Value> const& args);
ENUMERATE_COMMANDS(V)
#undef V
void handle_text_next_line_show_string(Vector<Value> const& args);
void handle_text_next_line_show_string_set_spacing(Vector<Value> const& args);
void set_graphics_state_from_dict(NonnullRefPtr<DictObject>);
// shift is the manual advance given in the TJ command array
void show_text(String const&, float shift = 0.0f);
RefPtr<ColorSpace> get_color_space(Value const&);
ALWAYS_INLINE GraphicsState const& state() const { return m_graphics_state_stack.last(); }
ALWAYS_INLINE GraphicsState& state() { return m_graphics_state_stack.last(); }
ALWAYS_INLINE TextState const& text_state() const { return state().text_state; }
ALWAYS_INLINE TextState& text_state() { return state().text_state; }
template<typename T>
ALWAYS_INLINE Gfx::Point<T> map(T x, T y) const;
template<typename T>
ALWAYS_INLINE Gfx::Size<T> map(Gfx::Size<T>) const;
template<typename T>
ALWAYS_INLINE Gfx::Rect<T> map(Gfx::Rect<T>) const;
Gfx::AffineTransform const& calculate_text_rendering_matrix();
RefPtr<Document> m_document;
RefPtr<Gfx::Bitmap> m_bitmap;
Page const& m_page;
Gfx::Painter m_painter;
Gfx::Path m_current_path;
Vector<GraphicsState> m_graphics_state_stack;
Gfx::AffineTransform m_text_matrix;
Gfx::AffineTransform m_text_line_matrix;
bool m_text_rendering_matrix_is_dirty { true };
Gfx::AffineTransform m_text_rendering_matrix;
};
}
namespace AK {
template<>
struct Formatter<PDF::LineCapStyle> : Formatter<StringView> {
void format(FormatBuilder& builder, PDF::LineCapStyle const& style)
{
switch (style) {
case PDF::LineCapStyle::ButtCap:
Formatter<StringView>::format(builder, "LineCapStyle::ButtCap");
break;
case PDF::LineCapStyle::RoundCap:
Formatter<StringView>::format(builder, "LineCapStyle::RoundCap");
break;
case PDF::LineCapStyle::SquareCap:
Formatter<StringView>::format(builder, "LineCapStyle::SquareCap");
break;
}
}
};
template<>
struct Formatter<PDF::LineJoinStyle> : Formatter<StringView> {
void format(FormatBuilder& builder, PDF::LineJoinStyle const& style)
{
switch (style) {
case PDF::LineJoinStyle::Miter:
Formatter<StringView>::format(builder, "LineJoinStyle::Miter");
break;
case PDF::LineJoinStyle::Round:
Formatter<StringView>::format(builder, "LineJoinStyle::Round");
break;
case PDF::LineJoinStyle::Bevel:
Formatter<StringView>::format(builder, "LineJoinStyle::Bevel");
break;
}
}
};
template<>
struct Formatter<PDF::LineDashPattern> : Formatter<StringView> {
void format(FormatBuilder& format_builder, PDF::LineDashPattern const& pattern)
{
StringBuilder builder;
builder.append("[");
bool first = true;
for (auto& i : pattern.pattern) {
if (!first)
builder.append(", ");
first = false;
builder.appendff("{}", i);
}
builder.appendff("] {}", pattern.phase);
return Formatter<StringView>::format(format_builder, builder.to_string());
}
};
template<>
struct Formatter<PDF::TextRenderingMode> : Formatter<StringView> {
void format(FormatBuilder& builder, PDF::TextRenderingMode const& style)
{
switch (style) {
case PDF::TextRenderingMode::Fill:
Formatter<StringView>::format(builder, "TextRenderingMode::Fill");
break;
case PDF::TextRenderingMode::Stroke:
Formatter<StringView>::format(builder, "TextRenderingMode::Stroke");
break;
case PDF::TextRenderingMode::FillThenStroke:
Formatter<StringView>::format(builder, "TextRenderingMode::FillThenStroke");
break;
case PDF::TextRenderingMode::Invisible:
Formatter<StringView>::format(builder, "TextRenderingMode::Invisible");
break;
case PDF::TextRenderingMode::FillAndClip:
Formatter<StringView>::format(builder, "TextRenderingMode::FillAndClip");
break;
case PDF::TextRenderingMode::StrokeAndClip:
Formatter<StringView>::format(builder, "TextRenderingMode::StrokeAndClip");
break;
case PDF::TextRenderingMode::FillStrokeAndClip:
Formatter<StringView>::format(builder, "TextRenderingMode::FillStrokeAndClip");
break;
case PDF::TextRenderingMode::Clip:
Formatter<StringView>::format(builder, "TextRenderingMode::Clip");
break;
}
}
};
template<>
struct Formatter<PDF::TextState> : Formatter<StringView> {
void format(FormatBuilder& format_builder, PDF::TextState const& state)
{
StringBuilder builder;
builder.append("TextState {\n");
builder.appendff(" character_spacing={}\n", state.character_spacing);
builder.appendff(" word_spacing={}\n", state.word_spacing);
builder.appendff(" horizontal_scaling={}\n", state.horizontal_scaling);
builder.appendff(" leading={}\n", state.leading);
builder.appendff(" font_family={}\n", state.font_family);
builder.appendff(" font_variant={}\n", state.font_variant);
builder.appendff(" font_size={}\n", state.font_size);
builder.appendff(" rendering_mode={}\n", state.rendering_mode);
builder.appendff(" rise={}\n", state.rise);
builder.appendff(" knockout={}\n", state.knockout);
builder.append(" }");
Formatter<StringView>::format(format_builder, builder.to_string());
}
};
template<>
struct Formatter<PDF::GraphicsState> : Formatter<StringView> {
void format(FormatBuilder& format_builder, PDF::GraphicsState const& state)
{
StringBuilder builder;
builder.append("GraphicsState {\n");
builder.appendff(" ctm={}\n", state.ctm);
builder.appendff(" stroke_color={}\n", state.stroke_color);
builder.appendff(" paint_color={}\n", state.paint_color);
builder.appendff(" line_width={}\n", state.line_width);
builder.appendff(" line_cap_style={}\n", state.line_cap_style);
builder.appendff(" line_join_style={}\n", state.line_join_style);
builder.appendff(" miter_limit={}\n", state.miter_limit);
builder.appendff(" line_dash_pattern={}\n", state.line_dash_pattern);
builder.appendff(" text_state={}\n", state.text_state);
builder.append("}");
Formatter<StringView>::format(format_builder, builder.to_string());
}
};
}
|