OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 PDFium Authors. All rights reserved. | |
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/include/fx_ge.h" | |
6 #include "core/fxge/skia/fx_skia_device.h" | |
7 #include "fpdfsdk/include/fsdk_define.h" | |
8 #include "public/fpdfview.h" | |
9 #include "testing/fx_string_testhelpers.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 #include "third_party/skia/include/core/SkPictureRecorder.h" | |
12 | |
13 namespace { | |
14 | |
15 struct State { | |
16 enum class Change { kNo, kYes }; | |
17 enum class Save { kNo, kYes }; | |
18 enum class Clip { kNo, kSame, kDifferentPath, kDifferentMatrix }; | |
19 enum class Graphic { kNone, kPath, kText }; | |
20 | |
21 Change m_change; | |
22 Save m_save; | |
23 Clip m_clip; | |
24 Graphic m_graphic; | |
25 uint32_t m_pixel; | |
26 }; | |
27 | |
28 void EmptyTest(CFX_SkiaDeviceDriver* driver, const State&) { | |
29 driver->SaveState(); | |
30 driver->RestoreState(true); | |
31 driver->RestoreState(false); | |
32 } | |
33 | |
34 void CommonTest(CFX_SkiaDeviceDriver* driver, const State& state) { | |
35 FXTEXT_CHARPOS charPos[] = {1, 0, 1, 4, false, {0, 0, 0, 0}, false}; | |
36 CFX_Font font; | |
37 FX_FLOAT fontSize = 1; | |
38 CFX_FontCache cache; | |
39 CFX_PathData clipPath, clipPath2; | |
40 clipPath.AppendRect(0, 0, 3, 1); | |
41 clipPath2.AppendRect(0, 0, 2, 1); | |
42 CFX_Matrix clipMatrix; | |
43 CFX_Matrix clipMatrix2(1, 0, 0, 1, 0, 1); | |
44 driver->SaveState(); | |
45 CFX_PathData path1; | |
46 path1.AppendRect(0, 0, 1, 2); | |
47 CFX_Matrix matrix, matrix2; | |
48 matrix2.Translate(1, 0); | |
49 CFX_GraphStateData graphState; | |
50 if (state.m_save == State::Save::kYes) | |
51 driver->SaveState(); | |
52 if (state.m_clip != State::Clip::kNo) | |
53 driver->SetClip_PathFill(&clipPath, &clipMatrix, 0); | |
54 if (state.m_graphic == State::Graphic::kPath) { | |
55 driver->DrawPath(&path1, &matrix, &graphState, 0xFF112233, 0, | |
56 FXFILL_WINDING, 0); | |
57 } else if (state.m_graphic == State::Graphic::kText) { | |
58 driver->DrawDeviceText(SK_ARRAY_COUNT(charPos), charPos, &font, &cache, | |
59 &matrix, fontSize, 0xFF445566); | |
60 } | |
61 if (state.m_save == State::Save::kYes) | |
62 driver->RestoreState(true); | |
63 CFX_PathData path2; | |
64 path2.AppendRect(0, 0, 2, 2); | |
65 if (state.m_change == State::Change::kYes) { | |
66 if (state.m_graphic == State::Graphic::kPath) | |
67 graphState.m_LineCap = CFX_GraphStateData::LineCapRound; | |
68 else if (state.m_graphic == State::Graphic::kText) | |
69 fontSize = 2; | |
70 } | |
71 if (state.m_clip == State::Clip::kSame) | |
72 driver->SetClip_PathFill(&clipPath, &clipMatrix, 0); | |
73 else if (state.m_clip == State::Clip::kDifferentPath) | |
74 driver->SetClip_PathFill(&clipPath2, &clipMatrix, 0); | |
75 else if (state.m_clip == State::Clip::kDifferentMatrix) | |
76 driver->SetClip_PathFill(&clipPath, &clipMatrix2, 0); | |
77 if (state.m_graphic == State::Graphic::kPath) { | |
78 driver->DrawPath(&path2, &matrix2, &graphState, 0xFF112233, 0, | |
79 FXFILL_WINDING, 0); | |
80 } else if (state.m_graphic == State::Graphic::kText) { | |
81 driver->DrawDeviceText(SK_ARRAY_COUNT(charPos), charPos, &font, &cache, | |
82 &matrix2, fontSize, 0xFF445566); | |
83 } | |
84 if (state.m_save == State::Save::kYes) | |
85 driver->RestoreState(false); | |
86 driver->RestoreState(false); | |
87 } | |
88 | |
89 void OutOfSequenceClipTest(CFX_SkiaDeviceDriver* driver, const State&) { | |
90 CFX_PathData clipPath; | |
91 clipPath.AppendRect(1, 0, 3, 1); | |
92 CFX_Matrix clipMatrix; | |
93 driver->SaveState(); | |
94 driver->SetClip_PathFill(&clipPath, &clipMatrix, 0); | |
95 driver->RestoreState(true); | |
96 driver->SaveState(); | |
97 driver->SetClip_PathFill(&clipPath, &clipMatrix, 0); | |
98 driver->RestoreState(false); | |
99 driver->RestoreState(false); | |
100 | |
101 driver->SaveState(); | |
102 driver->SaveState(); | |
103 driver->SetClip_PathFill(&clipPath, &clipMatrix, 0); | |
104 driver->RestoreState(true); | |
105 driver->SetClip_PathFill(&clipPath, &clipMatrix, 0); | |
106 driver->RestoreState(false); | |
107 driver->RestoreState(false); | |
108 } | |
109 | |
110 void Harness(void (*Test)(CFX_SkiaDeviceDriver*, const State&), | |
111 const State& state) { | |
112 int h = 1; | |
113 int w = 4; | |
114 FPDF_BITMAP bitmap = FPDFBitmap_Create(w, h, 1); | |
Lei Zhang
2016/06/29 18:53:51
Just create a CFX_DIBitmap instead? That's what yo
caryclark
2016/06/29 19:25:40
I copied the code construction used by pdfium_test
| |
115 EXPECT_NE(nullptr, bitmap); | |
116 if (!bitmap) | |
117 return; | |
118 FPDFBitmap_FillRect(bitmap, 0, 0, w, h, 0x00000000); | |
119 CFX_FxgeDevice geDevice; | |
120 CFX_DIBitmap* pBitmap = CFXBitmapFromFPDFBitmap(bitmap); | |
121 geDevice.Attach(pBitmap, false, nullptr, false); | |
122 CFX_SkiaDeviceDriver* driver = | |
123 static_cast<CFX_SkiaDeviceDriver*>(geDevice.GetDeviceDriver()); | |
124 (*Test)(driver, state); | |
125 driver->Flush(); | |
126 uint32_t pixel = pBitmap->GetPixel(0, 0); | |
127 EXPECT_EQ(state.m_pixel, pixel); | |
128 #ifdef SK_DEBUG | |
129 if (!driver) // force dump to be linked in so it can be called from debugger | |
130 driver->Dump(); | |
131 #endif | |
132 } | |
133 | |
134 } // namespace | |
135 | |
136 TEST(fxge, SkiaStateEmpty) { | |
137 Harness(&EmptyTest, {}); | |
138 } | |
139 | |
140 TEST(fxge, SkiaStatePath) { | |
141 Harness(&CommonTest, {State::Change::kNo, State::Save::kYes, | |
142 State::Clip::kSame, State::Graphic::kPath, 0xFF112233}); | |
143 Harness(&CommonTest, | |
144 {State::Change::kNo, State::Save::kYes, State::Clip::kDifferentPath, | |
145 State::Graphic::kPath, 0xFF112233}); | |
146 Harness(&CommonTest, {State::Change::kNo, State::Save::kYes, State::Clip::kNo, | |
147 State::Graphic::kPath, 0xFF112233}); | |
148 Harness(&CommonTest, {State::Change::kYes, State::Save::kNo, State::Clip::kNo, | |
149 State::Graphic::kPath, 0xFF112233}); | |
150 Harness(&CommonTest, {State::Change::kNo, State::Save::kNo, State::Clip::kNo, | |
151 State::Graphic::kPath, 0xFF112233}); | |
152 } | |
153 | |
154 TEST(fxge, SkiaStateText) { | |
155 Harness(&CommonTest, | |
156 {State::Change::kNo, State::Save::kYes, State::Clip::kDifferentMatrix, | |
157 State::Graphic::kText, 0xFF445566}); | |
158 Harness(&CommonTest, {State::Change::kNo, State::Save::kYes, | |
159 State::Clip::kSame, State::Graphic::kText, 0xFF445566}); | |
160 } | |
161 | |
162 TEST(fxge, SkiaStateOOSClip) { | |
163 Harness(&OutOfSequenceClipTest, {}); | |
164 } | |
OLD | NEW |