5#include "core/fxge/skia/fx_skia_device.h"
11#include "core/fpdfapi/page/cpdf_page.h"
12#include "core/fpdfapi/render/cpdf_pagerendercontext.h"
13#include "core/fxcrt/fx_codepage.h"
14#include "core/fxge/cfx_defaultrenderdevice.h"
15#include "core/fxge/cfx_fillrenderoptions.h"
16#include "core/fxge/cfx_font.h"
17#include "core/fxge/cfx_graphstatedata.h"
18#include "core/fxge/cfx_path.h"
19#include "core/fxge/cfx_renderdevice.h"
20#include "core/fxge/cfx_textrenderoptions.h"
21#include "core/fxge/dib/cfx_dibitmap.h"
22#include "core/fxge/text_char_pos.h"
23#include "fpdfsdk/cpdfsdk_helpers.h"
24#include "fpdfsdk/cpdfsdk_renderpage.h"
25#include "public/cpp/fpdf_scopers.h"
26#include "public/fpdfview.h"
27#include "testing/embedder_test.h"
28#include "testing/gmock/include/gmock/gmock.h"
29#include "testing/gtest/include/gtest/gtest.h"
30#include "third_party/skia/include/core/SkCanvas.h"
31#include "third_party/skia/include/core/SkImage.h"
32#include "third_party/skia/include/core/SkSize.h"
33#include "third_party/skia/include/utils/SkNoDrawCanvas.h"
37using ::testing::NiceMock;
38using ::testing::SizeIs;
39using ::testing::WithArg;
42 enum class Change { kNo, kYes };
43 enum class Save { kNo, kYes };
44 enum class Clip { kNo, kSame, kDifferentPath, kDifferentMatrix };
45 enum class Graphic { kNone, kPath, kText };
54void EmptyTest(CFX_SkiaDeviceDriver* driver,
const State&) {
60void CommonTest(CFX_SkiaDeviceDriver* driver,
const State& state) {
62 charPos[0].m_Origin = CFX_PointF(0, 1);
89 if (state.m_save == State::Save::kYes)
91 if (state.m_clip != State::Clip::kNo)
93 if (state.m_graphic == State::Graphic::kPath) {
96 }
else if (state.m_graphic == State::Graphic::kText) {
97 driver->DrawDeviceText(charPos, &font, matrix, fontSize, 0xFF445566,
100 if (state.m_save == State::Save::kYes)
104 if (state.m_change == State::Change::kYes) {
105 if (state.m_graphic == State::Graphic::kPath)
107 else if (state.m_graphic == State::Graphic::kText)
110 if (state.m_clip == State::Clip::kSame)
112 else if (state.m_clip == State::Clip::kDifferentPath)
114 else if (state.m_clip == State::Clip::kDifferentMatrix)
116 if (state.m_graphic == State::Graphic::kPath) {
119 }
else if (state.m_graphic == State::Graphic::kText) {
120 driver->DrawDeviceText(charPos, &font, matrix2, fontSize, 0xFF445566,
123 if (state.m_save == State::Save::kYes)
128void OutOfSequenceClipTest(CFX_SkiaDeviceDriver* driver,
const State&) {
149void Harness(
void (*Test)(CFX_SkiaDeviceDriver*,
const State&),
150 const State& state) {
151 constexpr int kWidth = 4;
152 constexpr int kHeight = 1;
156 FPDFBitmap_FillRect(bitmap.get(), 0, 0, kWidth, kHeight, 0x00000000));
157 RetainPtr<CFX_DIBitmap> pBitmap(CFXDIBitmapFromFPDFBitmap(bitmap.get()));
158 auto driver = CFX_SkiaDeviceDriver::Create(pBitmap,
false,
nullptr,
false);
160 (*Test)(driver.get(), state);
161 uint32_t pixel = pBitmap->GetPixelForTesting(0, 0);
162 EXPECT_EQ(state.m_pixel, pixel);
165void RenderPageToSkCanvas(FPDF_PAGE page,
173 auto context =
std::make_unique<CPDF_PageRenderContext>();
174 CPDF_PageRenderContext* unowned_context = context.get();
177 cpdf_page->SetRenderContext(
std::move(context));
179 auto default_device =
std::make_unique<CFX_DefaultRenderDevice>();
180 CHECK(default_device->AttachCanvas(canvas));
181 unowned_context->m_pDevice =
std::move(default_device);
184 size_x
, size_y
, 0
, 0
,
189class MockCanvas :
public SkNoDrawCanvas {
191 MockCanvas(
int width,
int height) : SkNoDrawCanvas(width, height) {}
198 const SkSamplingOptions&,
209 if (!CFX_DefaultRenderDevice::UseSkiaRenderer()) {
212 Harness(&EmptyTest, {});
216 if (!CFX_DefaultRenderDevice::UseSkiaRenderer()) {
219 Harness(&CommonTest, {State::Change::kNo, State::Save::kYes,
220 State::Clip::kSame, State::Graphic::kPath, 0xFF112233});
222 {State::Change::kNo, State::Save::kYes, State::Clip::kDifferentPath,
223 State::Graphic::kPath, 0xFF112233});
224 Harness(&CommonTest, {State::Change::kNo, State::Save::kYes, State::Clip::kNo,
225 State::Graphic::kPath, 0xFF112233});
226 Harness(&CommonTest, {State::Change::kYes, State::Save::kNo, State::Clip::kNo,
227 State::Graphic::kPath, 0xFF112233});
228 Harness(&CommonTest, {State::Change::kNo, State::Save::kNo, State::Clip::kNo,
229 State::Graphic::kPath, 0xFF112233});
233 if (!CFX_DefaultRenderDevice::UseSkiaRenderer()) {
238 {State::Change::kNo, State::Save::kYes, State::Clip::kDifferentMatrix,
239 State::Graphic::kText, 0xFF445566});
240 Harness(&CommonTest, {State::Change::kNo, State::Save::kYes,
241 State::Clip::kSame, State::Graphic::kText, 0xFF445566});
245 if (!CFX_DefaultRenderDevice::UseSkiaRenderer()) {
248 Harness(&OutOfSequenceClipTest, {});
252 static constexpr int kImageWidth = 5100;
253 static constexpr int kImageHeight = 6600;
257 static constexpr int kPageToImageFactor = 20;
258 static constexpr int kPageWidth = kImageWidth / kPageToImageFactor;
259 static constexpr int kPageHeight = kImageHeight / kPageToImageFactor;
262 GTEST_SKIP() <<
"Skia is not the default renderer";
265 ASSERT_TRUE(OpenDocument(
"bug_2034.pdf"));
266 FPDF_PAGE page = LoadPage(0);
269 std::set<
int> image_ids;
270 NiceMock<MockCanvas> canvas(kPageWidth, kPageHeight / 2);
271 EXPECT_CALL(canvas, onDrawImageRect2)
272 .WillRepeatedly(WithArg<0>([&image_ids](
const SkImage* image) {
274 image_ids.insert(image->uniqueID());
278 EXPECT_EQ(SkISize::Make(kImageWidth, kImageHeight), image->dimensions())
279 <<
"Actual image dimensions: " << image->width() <<
"x"
284 RenderPageToSkCanvas(page, 0, 0,
285 kPageWidth, kPageHeight, canvas);
288 RenderPageToSkCanvas(page, 0, -kPageHeight / 2,
289 kPageWidth, kPageHeight, canvas);
291 EXPECT_THAT(image_ids, SizeIs(1));
static bool UseSkiaRenderer()
void LoadSubst(const ByteString &face_name, bool bTrueType, uint32_t flags, int weight, int italic_angle, FX_CodePage code_page, bool bVertical)
void Translate(int32_t x, int32_t y)
constexpr CFX_Matrix(float a1, float b1, float c1, float d1, float e1, float f1)
void AppendRect(float left, float bottom, float right, float top)
void SaveState() override
bool SetClip_PathFill(const CFX_Path &path, const CFX_Matrix *pObject2Device, const CFX_FillRenderOptions &fill_options) override
bool DrawPath(const CFX_Path &path, const CFX_Matrix *pObject2Device, const CFX_GraphStateData *pGraphState, uint32_t fill_color, uint32_t stroke_color, const CFX_FillRenderOptions &fill_options) override
void RestoreState(bool bKeepSaved) override
RenderContextClearer(CPDF_Page *pPage)
CPDF_Page * CPDFPageFromFPDFPage(FPDF_PAGE page)
void CPDFSDK_RenderPageWithContext(CPDF_PageRenderContext *pContext, CPDF_Page *pPage, int start_x, int start_y, int size_x, int size_y, int rotate, int flags, const FPDF_COLORSCHEME *color_scheme, bool need_to_restore, CPDFSDK_PauseAdapter *pause)
TEST_F(FPDFParserDecodeEmbedderTest, Bug552046)
FPDF_EXPORT FPDF_BITMAP FPDF_CALLCONV FPDFBitmap_Create(int width, int height, int alpha)
TEST(FXCRYPT, CryptToBase16)
constexpr CFX_FillRenderOptions()
static constexpr CFX_FillRenderOptions WindingOptions()
constexpr CFX_TextRenderOptions(AliasingType type)