Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cfx_psrenderer_unittest.cpp
Go to the documentation of this file.
1// Copyright 2021 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "core/fxge/win32/cfx_psrenderer.h"
6
7#include <utility>
8
9#include "core/fxcrt/bytestring.h"
10#include "core/fxcrt/data_vector.h"
11#include "core/fxcrt/fx_coordinates.h"
12#include "core/fxcrt/fx_stream.h"
13#include "core/fxcrt/retain_ptr.h"
14#include "core/fxge/dib/cfx_dibitmap.h"
15#include "core/fxge/dib/fx_dib.h"
16#include "core/fxge/win32/cfx_psfonttracker.h"
17#include "testing/gtest/include/gtest/gtest.h"
18#include "third_party/abseil-cpp/absl/types/optional.h"
19#include "third_party/base/containers/span.h"
20
21namespace {
22
23DataVector<uint8_t> FakeA85Encode(pdfium::span<const uint8_t> src_span) {
24 return DataVector<uint8_t>({'d', 'u', 'm', 'm', 'y', 'a', '8', '5'});
25}
26
27class TestWriteStream final : public IFX_RetainableWriteStream {
28 public:
30
31 // IFX_RetainableWriteStream:
32 bool WriteBlock(pdfium::span<const uint8_t> buffer) override {
33 data_.insert(data_.end(), buffer.begin(), buffer.end());
34 return true;
35 }
36
37 pdfium::span<const uint8_t> GetSpan() const { return data_; }
38
39 private:
40 DataVector<uint8_t> data_;
41};
42
43} // namespace
44
46 absl::optional<ByteString> result;
47
48 result = CFX_PSRenderer::GenerateType42SfntDataForTesting("empty", {});
49 EXPECT_FALSE(result.has_value());
50
51 constexpr uint8_t kOddByteCountTestData[] = {0, 32, 55};
52 static constexpr char kExpectedOddByteCountResult[] = R"(/odd_sfnts [
53<
54002037
55>
56] def
57)";
58 result = CFX_PSRenderer::GenerateType42SfntDataForTesting(
59 "odd", kOddByteCountTestData);
60 ASSERT_TRUE(result.has_value());
61 EXPECT_STREQ(kExpectedOddByteCountResult, result.value().c_str());
62
63 // Requires padding.
64 constexpr uint8_t kEvenByteCountTestData[] = {0, 32, 66, 77};
65 static constexpr char kExpectedEvenByteCountResult[] = R"(/even_sfnts [
66<
670020424D00
68>
69] def
70)";
71 result = CFX_PSRenderer::GenerateType42SfntDataForTesting(
72 "even", kEvenByteCountTestData);
73 ASSERT_TRUE(result.has_value());
74 EXPECT_STREQ(kExpectedEvenByteCountResult, result.value().c_str());
75}
76
78 ByteString result;
79
80 static constexpr char kExpected1DescendantFontResult[] = R"(8 dict begin
81/FontType 42 def
82/FontMatrix [1 0 0 1 0 0] def
83/FontName /1descendant_0 def
84/Encoding 3 array
85dup 0 /c00 put
86dup 1 /c01 put
87dup 2 /c02 put
88readonly def
89/FontBBox [1 2 3 4] def
90/PaintType 0 def
91/CharStrings 4 dict dup begin
92/.notdef 0 def
93/c00 0 def
94/c01 1 def
95/c02 2 def
96end readonly def
97/sfnts 1descendant_sfnts def
98FontName currentdict end definefont pop
996 dict begin
100/FontName /1descendant def
101/FontType 0 def
102/FontMatrix [1 0 0 1 0 0] def
103/FMapType 2 def
104/Encoding [
1050
106] def
107/FDepVector [
108/1descendant_0 findfont
109] def
110FontName currentdict end definefont pop
111%%EndResource
112)";
113 result = CFX_PSRenderer::GenerateType42FontDictionaryForTesting(
114 "1descendant", FX_RECT(1, 2, 3, 4), /*num_glyphs=*/3,
115 /*glyphs_per_descendant_font=*/3);
116 EXPECT_STREQ(kExpected1DescendantFontResult, result.c_str());
117
118 static constexpr char kExpected2DescendantFontResult[] = R"(8 dict begin
119/FontType 42 def
120/FontMatrix [1 0 0 1 0 0] def
121/FontName /2descendant_0 def
122/Encoding 3 array
123dup 0 /c00 put
124dup 1 /c01 put
125dup 2 /c02 put
126readonly def
127/FontBBox [12 -5 34 199] def
128/PaintType 0 def
129/CharStrings 4 dict dup begin
130/.notdef 0 def
131/c00 0 def
132/c01 1 def
133/c02 2 def
134end readonly def
135/sfnts 2descendant_sfnts def
136FontName currentdict end definefont pop
1378 dict begin
138/FontType 42 def
139/FontMatrix [1 0 0 1 0 0] def
140/FontName /2descendant_1 def
141/Encoding 3 array
142dup 0 /c00 put
143dup 1 /c01 put
144readonly def
145/FontBBox [12 -5 34 199] def
146/PaintType 0 def
147/CharStrings 4 dict dup begin
148/.notdef 0 def
149/c00 3 def
150/c01 4 def
151end readonly def
152/sfnts 2descendant_sfnts def
153FontName currentdict end definefont pop
1546 dict begin
155/FontName /2descendant def
156/FontType 0 def
157/FontMatrix [1 0 0 1 0 0] def
158/FMapType 2 def
159/Encoding [
1600
1611
162] def
163/FDepVector [
164/2descendant_0 findfont
165/2descendant_1 findfont
166] def
167FontName currentdict end definefont pop
168%%EndResource
169)";
170 result = CFX_PSRenderer::GenerateType42FontDictionaryForTesting(
171 "2descendant", FX_RECT(12, -5, 34, 199), /*num_glyphs=*/5,
172 /*glyphs_per_descendant_font=*/3);
173 EXPECT_STREQ(kExpected2DescendantFontResult, result.c_str());
174}
175
177 static constexpr char kExpectedOutput[] = R"(
178save
179/im/initmatrix load def
180/n/newpath load def/m/moveto load def/l/lineto load def/c/curveto load def/h/closepath load def
181/f/fill load def/F/eofill load def/s/stroke load def/W/clip load def/W*/eoclip load def
182/rg/setrgbcolor load def/k/setcmykcolor load def
183/J/setlinecap load def/j/setlinejoin load def/w/setlinewidth load def/M/setmiterlimit load def/d/setdash load def
184/q/gsave load def/Q/grestore load def/iM/imagemask load def
185/Tj/show load def/Ff/findfont load def/Fs/scalefont load def/Sf/setfont load def
186/cm/concat load def/Cm/currentmatrix load def/mx/matrix load def/sm/setmatrix load def
187q
188[1 0 0 1 0 0]cm 10 2 1[10 0 0 -2 0 2]currentfile/ASCII85Decode filter false 1 colorimage
189dummya85
190Q
191
192restore
193)";
194 auto output_stream = pdfium::MakeRetain<TestWriteStream>();
195
196 {
197 constexpr int kWidth = 10;
198 constexpr int kHeight = 2;
199 CFX_PSFontTracker font_tracker;
200 const EncoderIface encoder_interface{&FakeA85Encode, nullptr, nullptr,
201 nullptr, nullptr};
202 CFX_PSRenderer renderer(&font_tracker, &encoder_interface);
204 kWidth, kHeight);
205
206 auto bitmap = pdfium::MakeRetain<CFX_DIBitmap>();
207 bool result = bitmap->Create(kWidth, kHeight, FXDIB_Format::k1bppRgb);
208 ASSERT_TRUE(result);
209 ASSERT_TRUE(renderer.DrawDIBits(std::move(bitmap), /*color=*/0,
211 }
212
213 ByteString output(output_stream->GetSpan());
214 EXPECT_STREQ(output.c_str(), kExpectedOutput);
215}
CFX_Matrix()=default
bool DrawDIBits(RetainPtr< const CFX_DIBBase > bitmap, uint32_t color, const CFX_Matrix &matrix, const FXDIB_ResampleOptions &options)
CFX_PSRenderer(CFX_PSFontTracker *font_tracker, const EncoderIface *encoder_iface)
void Init(const RetainPtr< IFX_RetainableWriteStream > &stream, RenderingLevel level, int width, int height)
const char * c_str() const
Definition bytestring.h:76
TEST(FXCRYPT, MD5GenerateEmtpyData)
FXDIB_Format
Definition fx_dib.h:19
#define CONSTRUCT_VIA_MAKE_RETAIN
Definition retain_ptr.h:224
constexpr FX_RECT(int l, int t, int r, int b)