Debian Codegraph @

podofo/test/unit/PainterTest.cpp

raw symbols SCIP
1 /**
2 * Copyright (C) 2011 by Dominik Seichter <[email protected]>
3 * Copyright (C) 2021 by Francesco Pretto <[email protected]>
4 *
5 * Licensed under GNU Library General Public 2.0 or later.
6 * Some rights reserved. See COPYING, AUTHORS.
7 */
8
9 #include <PdfTest.h>
10
11 using namespace std;
12 using namespace PoDoFo;
13
14 namespace
15 {
16 class FakeCanvas : public PdfCanvas
17 {
18 public:
19 FakeCanvas() { }
20
21 public:
22 PdfObjectStream& GetStreamForAppending(PdfStreamAppendFlags flags)
23 {
24 (void)flags;
25 return m_resourceObj.GetOrCreateStream();
26 }
27
28 PdfResources& GetOrCreateResources() override
29 {
30 PODOFO_RAISE_ERROR(PdfErrorCode::InternalLogic);
31 }
32
33 /** Get the current canvas size in PDF Units
34 * \returns a Rect containing the page size available for drawing
35 */
36 Rect GetRectRaw() const override
37 {
38 PODOFO_RAISE_ERROR(PdfErrorCode::InternalLogic);
39 }
40
41 bool HasRotation(double& teta) const override
42 {
43 teta = 0;
44 return false;
45 }
46
47 charbuff GetCopy() const
48 {
49 return m_resourceObj.MustGetStream().GetCopy();
50 }
51
52 void EnsureResourcesCreated() override
53 {
54 // Do nothing
55 }
56
57 protected:
58 PdfObject* getContentsObject() override
59 {
60 PODOFO_RAISE_ERROR(PdfErrorCode::InternalLogic);
61 }
62 PdfResources* getResources() override
63 {
64 PODOFO_RAISE_ERROR(PdfErrorCode::InternalLogic);
65 }
66 PdfElement& getElement() override
67 {
68 PODOFO_RAISE_ERROR(PdfErrorCode::InternalLogic);
69 }
70
71 private:
72 PdfObject m_resourceObj;
73 };
74 }
75
76 static string getContents(const PdfPage& page);
77 static void compareStreamContent(PdfObjectStream& stream, const string_view& expected);
78 static void drawSample(PdfPainter& painter);
79 static void drawSquareWithCross(PdfPainter& painter, double x, double y);
80
81 auto s_expected = R"(q
82 120 500 m
83 120 511.045695 111.045695 520 100 520 c
84 88.954305 520 80 511.045695 80 500 c
85 80 488.954305 88.954305 480 100 480 c
86 111.045695 480 120 488.954305 120 500 c
87 h
88 f
89 Q
90 )"sv;
91
92 TEST_CASE("TestPainter1")
93 {
94 FakeCanvas canvas;
95 PdfPainter painter;
96 painter.SetCanvas(canvas);
97 drawSample(painter);
98 painter.FinishDrawing();
99 auto copy = canvas.GetCopy();
100 REQUIRE(copy == s_expected);
101 }
102
103 TEST_CASE("TestPainter2")
104 {
105 PdfMemDocument doc;
106 auto& page = doc.GetPages().CreatePage(PdfPage::CreateStandardPageSize(PdfPageSize::A4));
107 PdfPainter painter;
108 painter.SetCanvas(page);
109 drawSample(painter);
110 painter.FinishDrawing();
111 doc.Save(TestUtils::GetTestOutputFilePath("TestPainter2.pdf"));
112
113 auto out = getContents(page);
114 REQUIRE(out == s_expected);
115 }
116
117 TEST_CASE("TestPainter3")
118 {
119 PdfMemDocument doc;
120 auto& page = doc.GetPages().CreatePage(PdfPage::CreateStandardPageSize(PdfPageSize::A4));
121 PdfPainter painter;
122 painter.SetCanvas(page);
123 painter.TextState.SetFont(doc.GetFonts().GetStandard14Font(PdfStandard14FontType::TimesRoman), 15);
124 painter.DrawText("Hello world", 100, 500, PdfDrawTextStyle::StrikeThrough | PdfDrawTextStyle::Underline);
125 painter.FinishDrawing();
126 doc.Save(TestUtils::GetTestOutputFilePath("TestPainter3.pdf"));
127
128 auto expected = R"(q
129 q
130 BT
131 /Ft5 15 Tf
132 0.75 w
133 100 500 Td
134 <0001020203040503060207> Tj
135 ET
136 100 498.5 m
137 172.075 498.5 l
138 S
139 100 503.93 m
140 172.075 503.93 l
141 S
142 Q
143 Q
144 )"sv;
145
146 auto out = getContents(page);
147 REQUIRE(out == expected);
148 }
149
150 TEST_CASE("TestPainter4")
151 {
152 PdfMemDocument doc;
153 auto& page = doc.GetPages().CreatePage(PdfPage::CreateStandardPageSize(PdfPageSize::A4));
154
155 PdfFontCreateParams params;
156 params.Encoding = PdfEncodingMapFactory::WinAnsiEncodingInstance();
157 auto& font = doc.GetFonts().GetStandard14Font(PdfStandard14FontType::Helvetica, params);
158
159 PdfPainter painter;
160 auto& operators = static_cast<PdfContentStreamOperators&>(painter);
161 painter.SetCanvas(page);
162 painter.TextState.SetFont(font, 15);
163 painter.TextObject.Begin();
164 painter.TextObject.MoveTo(100, 500);
165 painter.TextObject.AddText("Test1");
166 // Some low level operations
167 operators.TJ_Operator_Begin();
168 operators.TJ_Operator_Glyphs("_W", false);
169 operators.TJ_Operator_Delta(-500);
170 operators.TJ_Operator_Glyphs("orld", false);
171 operators.TJ_Operator_End();
172 painter.TextObject.End();
173 painter.DrawText("Test2", 100, 600, PdfDrawTextStyle::StrikeThrough);
174
175 PdfPainterPath path;
176 path.MoveTo(20, 20);
177 path.AddArcTo(150, 20, 150, 70, 50);
178 path.AddLineTo(150, 120);
179 path.AddArc(200, 120, 50, numbers::pi, numbers::pi/8, true);
180
181 auto currPoint1 = path.GetCurrentPoint();
182
183 PdfPainterPath path2;
184 path2.MoveTo(250, 120);
185 path2.AddLineTo(250, 80);
186 path.AddPath(path2, true);
187
188 auto currPoint2 = path.GetCurrentPoint();
189 painter.DrawPath(path, PdfPathDrawMode::Stroke);
190 path.Reset();
191 path.MoveTo(40, 40);
192 path.AddLineTo(100, 40);
193 path.AddLineTo(70, 80);
194 path.AddLineTo(40, 40);
195 path.AddCircle(200, 300, 60);
196 painter.DrawPath(path, PdfPathDrawMode::Fill);
197
198 drawSquareWithCross(painter, 100, 20);
199 drawSquareWithCross(painter, 100, 70);
200 drawSquareWithCross(painter, 150, 70);
201 drawSquareWithCross(painter, currPoint1.X, currPoint1.Y);
202 drawSquareWithCross(painter, currPoint2.X, currPoint2.Y);
203
204 painter.FinishDrawing();
205 doc.Save(TestUtils::GetTestOutputFilePath("TestPainter4.pdf"));
206
207 auto expected = R"(q
208 BT
209 /Ft5 15 Tf
210 100 500 Td
211 (Test1) Tj
212 [ (_W) -500 (orld) ] TJ
213
214 ET
215 q
216 BT
217 0.75 w
218 100 600 Td
219 (Test2) Tj
220 ET
221 100 604.35 m
222 137.515 604.35 l
223 S
224 Q
225 20 20 m
226 100 20 l
227 127.614237 20 150 42.385763 150 70 c
228 150 120 l
229 150 120 l
230 150 143.853715 166.850112 164.385635 190.245484 169.039264 c
231 213.640856 173.692893 237.065555 161.17213 246.193977 139.134172 c
232 250 120 l
233 250 120 m
234 250 80 l
235 S
236 40 40 m
237 100 40 l
238 70 80 l
239 40 40 l
240 260 300 m
241 260 333.137085 233.137085 360 200 360 c
242 166.862915 360 140 333.137085 140 300 c
243 140 266.862915 166.862915 240 200 240 c
244 233.137085 240 260 266.862915 260 300 c
245 h
246 f
247 q
248 0.6 w
249 97 17 6 6 re
250 S
251 0 w
252 100 17 m
253 100 23 l
254 S
255 97 20 m
256 103 20 l
257 S
258 Q
259 q
260 0.6 w
261 97 67 6 6 re
262 S
263 0 w
264 100 67 m
265 100 73 l
266 S
267 97 70 m
268 103 70 l
269 S
270 Q
271 q
272 0.6 w
273 147 67 6 6 re
274 S
275 0 w
276 150 67 m
277 150 73 l
278 S
279 147 70 m
280 153 70 l
281 S
282 Q
283 q
284 0.6 w
285 243.193977 136.134172 6 6 re
286 S
287 0 w
288 246.193977 136.134172 m
289 246.193977 142.134172 l
290 S
291 243.193977 139.134172 m
292 249.193977 139.134172 l
293 S
294 Q
295 q
296 0.6 w
297 247 77 6 6 re
298 S
299 0 w
300 250 77 m
301 250 83 l
302 S
303 247 80 m
304 253 80 l
305 S
306 Q
307 Q
308 )";
309 auto out = getContents(page);
310 REQUIRE(out == expected);
311 }
312
313 TEST_CASE("TestPainter5")
314 {
315 PdfMemDocument doc;
316 auto& page = doc.GetPages().CreatePage(PdfPage::CreateStandardPageSize(PdfPageSize::A4));
317
318 PdfFontCreateParams params;
319 params.Encoding = PdfEncodingMapFactory::WinAnsiEncodingInstance();
320 auto& font = doc.GetFonts().GetStandard14Font(PdfStandard14FontType::Helvetica, params);
321
322 PdfPainter painter;
323 painter.SetCanvas(page);
324 painter.TextState.SetFont(font, 15);
325 painter.DrawTextMultiLine("Hello\nWorld", 100, 600, 100, 40);
326
327 painter.FinishDrawing();
328 doc.Save(TestUtils::GetTestOutputFilePath("TestPainter5.pdf"));
329
330 auto expected = R"(q
331 q
332 100 600 100 40 re
333 W
334 n
335 BT
336 /Ft5 15 Tf
337 100 628.75 Td
338 (Hello) Tj
339 0 -15 Td
340 (World) Tj
341 ET
342 Q
343 Q
344 )";
345
346 auto out = getContents(page);
347 REQUIRE(out == expected);
348 }
349
350 TEST_CASE("TestPainter6")
351 {
352 PdfMemDocument doc;
353 auto& page = doc.GetPages().CreatePage(PdfPage::CreateStandardPageSize(PdfPageSize::A4));
354
355 PdfFontCreateParams params;
356 params.Encoding = PdfEncodingMapFactory::WinAnsiEncodingInstance();
357
358 PdfPainter painter;
359 painter.SetCanvas(page);
360 REQUIRE(painter.GetStateStack().Current->CurrentPoint == nullptr);
361 PdfPainterPath path;
362 path.AddRectangle(Rect(10,10, 100, 50));
363 painter.Save();
364 painter.DrawPath(path);
365 path.GetCurrentPoint() == Vector2(10, 10);
366 REQUIRE(painter.GetStateStack().Current->CurrentPoint == nullptr);
367 painter.Save();
368 auto& operators = static_cast<PdfContentStreamOperators&>(painter);
369 operators.n_Operator();
370 REQUIRE(painter.GetStateStack().Current->CurrentPoint == nullptr);
371 painter.FinishDrawing();
372 doc.Save(TestUtils::GetTestOutputFilePath("TestPainter6.pdf"));
373
374 auto expected = R"(q
375 q
376 10 10 100 50 re
377 S
378 q
379 n
380 Q
381 )";
382
383 auto out = getContents(page);
384 REQUIRE(out == expected);
385 }
386
387 TEST_CASE("TestAppend")
388 {
389 string_view example = "BT (Hello) Tj ET";
390
391 PdfMemDocument doc;
392 auto& page = doc.GetPages().CreatePage(PdfPage::CreateStandardPageSize(PdfPageSize::A4));
393
394 auto& contents = page.GetOrCreateContents();
395 auto& stream = contents.GetStreamForAppending();
396 stream.SetData(example);
397
398 compareStreamContent(stream, example);
399
400 PdfPainter painter;
401 painter.SetCanvas(page);
402 painter.GraphicsState.SetFillColor(PdfColor(1.0, 1.0, 1.0));
403 painter.FinishDrawing();
404
405 auto out = getContents(page);
406 REQUIRE(out == "q\nBT (Hello) Tj ET\nQ\nq\n1 1 1 rg\nQ\n");
407 }
408
409 TEST_CASE("TestRotate")
410 {
411 unordered_map<int, Matrix> matrices = {
412 { 90, Matrix::FromCoefficients(6.1232339957367660e-17, 1, -1, 6.1232339957367660e-17, 9.9999999999999982, 0) },
413 { 270, Matrix::FromCoefficients(-1.8369701987210297e-16, -1, 1,-1.8369701987210297e-16, 0, 20.000000000000004) },
414 };
415
416 auto test = [&](int angle)
417 {
418 PdfMemDocument doc;
419 doc.Load(TestUtils::GetTestInputFilePath(utls::Format("blank-rotated-{}.pdf", angle)));
420 auto& page = doc.GetPages().GetPageAt(0);
421 page.SetRect(Rect(0, 0, 5, 7));
422
423 auto& signature = page.CreateField<PdfSignature>("Test", Rect(2, 1, 2, 1));
424 auto xobj = doc.CreateXObjectForm(Rect(0, 0, 20, 10));
425 PdfPainter painter;
426 painter.SetCanvas(*xobj);
427 PdfPainterPath path;
428 path.MoveTo(1, 1);
429 path.AddLineTo(19, 1);
430 path.AddLineTo(10, 9);
431 path.Close();
432 painter.DrawPath(path, PdfPathDrawMode::Fill);
433 painter.FinishDrawing();
434 signature.MustGetWidget().SetAppearanceStream(*xobj);
435 auto apObj = signature.MustGetWidget().GetAppearanceStream();
436 unique_ptr<PdfXObjectForm> form;
437 (void)PdfXObject::TryCreateFromObject(*apObj, form);
438 REQUIRE(form->GetMatrix() == matrices[angle]);
439 doc.Save(TestUtils::GetTestOutputFilePath(utls::Format("Rotated-{}.pdf", angle)));
440 };
441
442 test(90);
443 test(270);
444 }
445
446 static void drawSample(PdfPainter& painter)
447 {
448 painter.DrawCircle(100, 500, 20, PdfPathDrawMode::Fill);
449 }
450
451 void compareStreamContent(PdfObjectStream& stream, const string_view& expected)
452 {
453 charbuff buffer;
454 stream.CopyTo(buffer);
455 REQUIRE(buffer == expected);
456 }
457
458 string getContents(const PdfPage& page)
459 {
460 PdfCanvasInputDevice input(page);
461 string ret;
462 StringStreamDevice output(ret);
463 input.CopyTo(output);
464 return ret;
465 }
466
467 void drawSquareWithCross(PdfPainter& painter, double x, double y)
468 {
469 painter.Save();
470 const double SquareSize = 6;
471 painter.GraphicsState.SetLineWidth(0.6);
472 painter.DrawRectangle(x - SquareSize / 2, y - SquareSize / 2, SquareSize, SquareSize);
473
474 painter.GraphicsState.SetLineWidth(0);
475 painter.DrawLine(x, y - SquareSize / 2, x, y + SquareSize / 2);
476 painter.DrawLine(x - SquareSize / 2, y, x + SquareSize / 2, y);
477 painter.Restore();
478 }