1 // Copyright 2014 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 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "../../../include/fpdfapi/fpdf_render.h"
8 #include "../../../include/fpdfapi/fpdf_pageobj.h"
9 #include "../../../include/fxge/fx_ge.h"
10 #include "../fpdf_page/pageint.h"
11 #include "render_int.h"
12 #define SHADING_STEPS 256
_DrawAxialShading(CFX_DIBitmap * pBitmap,CFX_AffineMatrix * pObject2Bitmap,CPDF_Dictionary * pDict,CPDF_Function ** pFuncs,int nFuncs,CPDF_ColorSpace * pCS,int alpha)13 static void _DrawAxialShading(CFX_DIBitmap* pBitmap, CFX_AffineMatrix* pObject2Bitmap,
14 CPDF_Dictionary* pDict, CPDF_Function** pFuncs, int nFuncs,
15 CPDF_ColorSpace* pCS, int alpha)
16 {
17 ASSERT(pBitmap->GetFormat() == FXDIB_Argb);
18 CPDF_Array* pCoords = pDict->GetArray(FX_BSTRC("Coords"));
19 if (pCoords == NULL) {
20 return;
21 }
22 FX_FLOAT start_x = pCoords->GetNumber(0);
23 FX_FLOAT start_y = pCoords->GetNumber(1);
24 FX_FLOAT end_x = pCoords->GetNumber(2);
25 FX_FLOAT end_y = pCoords->GetNumber(3);
26 FX_FLOAT t_min = 0, t_max = 1.0f;
27 CPDF_Array* pArray = pDict->GetArray(FX_BSTRC("Domain"));
28 if (pArray) {
29 t_min = pArray->GetNumber(0);
30 t_max = pArray->GetNumber(1);
31 }
32 FX_BOOL bStartExtend = FALSE, bEndExtend = FALSE;
33 pArray = pDict->GetArray(FX_BSTRC("Extend"));
34 if (pArray) {
35 bStartExtend = pArray->GetInteger(0);
36 bEndExtend = pArray->GetInteger(1);
37 }
38 int width = pBitmap->GetWidth();
39 int height = pBitmap->GetHeight();
40 FX_FLOAT x_span = end_x - start_x;
41 FX_FLOAT y_span = end_y - start_y;
42 FX_FLOAT axis_len_square = FXSYS_Mul(x_span, x_span) + FXSYS_Mul(y_span, y_span);
43 CFX_AffineMatrix matrix;
44 matrix.SetReverse(*pObject2Bitmap);
45 int total_results = 0;
46 for (int j = 0; j < nFuncs; j ++) {
47 if (pFuncs[j]) {
48 total_results += pFuncs[j]->CountOutputs();
49 }
50 }
51 if (pCS->CountComponents() > total_results) {
52 total_results = pCS->CountComponents();
53 }
54 CFX_FixedBufGrow<FX_FLOAT, 16> result_array(total_results);
55 FX_FLOAT* pResults = result_array;
56 FXSYS_memset32(pResults, 0, total_results * sizeof(FX_FLOAT));
57 FX_DWORD rgb_array[SHADING_STEPS];
58 for (int i = 0; i < SHADING_STEPS; i ++) {
59 FX_FLOAT input = (t_max - t_min) * i / SHADING_STEPS + t_min;
60 int offset = 0;
61 for (int j = 0; j < nFuncs; j ++) {
62 if (pFuncs[j]) {
63 int nresults = 0;
64 if (pFuncs[j]->Call(&input, 1, pResults + offset, nresults)) {
65 offset += nresults;
66 }
67 }
68 }
69 FX_FLOAT R = 0.0f, G = 0.0f, B = 0.0f;
70 pCS->GetRGB(pResults, R, G, B);
71 rgb_array[i] = FXARGB_TODIB(FXARGB_MAKE(alpha, FXSYS_round(R * 255), FXSYS_round(G * 255), FXSYS_round(B * 255)));
72 }
73 int pitch = pBitmap->GetPitch();
74 for (int row = 0; row < height; row ++) {
75 FX_DWORD* dib_buf = (FX_DWORD*)(pBitmap->GetBuffer() + row * pitch);
76 for (int column = 0; column < width; column ++) {
77 FX_FLOAT x = (FX_FLOAT)column, y = (FX_FLOAT)row;
78 matrix.Transform(x, y);
79 FX_FLOAT scale = FXSYS_Div(FXSYS_Mul(x - start_x, x_span) + FXSYS_Mul(y - start_y, y_span), axis_len_square);
80 int index = (FX_INT32)(scale * (SHADING_STEPS - 1));
81 if (index < 0) {
82 if (!bStartExtend) {
83 continue;
84 }
85 index = 0;
86 } else if (index >= SHADING_STEPS) {
87 if (!bEndExtend) {
88 continue;
89 }
90 index = SHADING_STEPS - 1;
91 }
92 dib_buf[column] = rgb_array[index];
93 }
94 }
95 }
_DrawRadialShading(CFX_DIBitmap * pBitmap,CFX_AffineMatrix * pObject2Bitmap,CPDF_Dictionary * pDict,CPDF_Function ** pFuncs,int nFuncs,CPDF_ColorSpace * pCS,int alpha)96 static void _DrawRadialShading(CFX_DIBitmap* pBitmap, CFX_AffineMatrix* pObject2Bitmap,
97 CPDF_Dictionary* pDict, CPDF_Function** pFuncs, int nFuncs,
98 CPDF_ColorSpace* pCS, int alpha)
99 {
100 ASSERT(pBitmap->GetFormat() == FXDIB_Argb);
101 CPDF_Array* pCoords = pDict->GetArray(FX_BSTRC("Coords"));
102 if (pCoords == NULL) {
103 return;
104 }
105 FX_FLOAT start_x = pCoords->GetNumber(0);
106 FX_FLOAT start_y = pCoords->GetNumber(1);
107 FX_FLOAT start_r = pCoords->GetNumber(2);
108 FX_FLOAT end_x = pCoords->GetNumber(3);
109 FX_FLOAT end_y = pCoords->GetNumber(4);
110 FX_FLOAT end_r = pCoords->GetNumber(5);
111 CFX_AffineMatrix matrix;
112 matrix.SetReverse(*pObject2Bitmap);
113 FX_FLOAT t_min = 0, t_max = 1.0f;
114 CPDF_Array* pArray = pDict->GetArray(FX_BSTRC("Domain"));
115 if (pArray) {
116 t_min = pArray->GetNumber(0);
117 t_max = pArray->GetNumber(1);
118 }
119 FX_BOOL bStartExtend = FALSE, bEndExtend = FALSE;
120 pArray = pDict->GetArray(FX_BSTRC("Extend"));
121 if (pArray) {
122 bStartExtend = pArray->GetInteger(0);
123 bEndExtend = pArray->GetInteger(1);
124 }
125 int total_results = 0;
126 for (int j = 0; j < nFuncs; j ++) {
127 if (pFuncs[j]) {
128 total_results += pFuncs[j]->CountOutputs();
129 }
130 }
131 if (pCS->CountComponents() > total_results) {
132 total_results = pCS->CountComponents();
133 }
134 CFX_FixedBufGrow<FX_FLOAT, 16> result_array(total_results);
135 FX_FLOAT* pResults = result_array;
136 FXSYS_memset32(pResults, 0, total_results * sizeof(FX_FLOAT));
137 FX_DWORD rgb_array[SHADING_STEPS];
138 for (int i = 0; i < SHADING_STEPS; i ++) {
139 FX_FLOAT input = (t_max - t_min) * i / SHADING_STEPS + t_min;
140 int offset = 0;
141 for (int j = 0; j < nFuncs; j ++) {
142 if (pFuncs[j]) {
143 int nresults;
144 if (pFuncs[j]->Call(&input, 1, pResults + offset, nresults)) {
145 offset += nresults;
146 }
147 }
148 }
149 FX_FLOAT R = 0.0f, G = 0.0f, B = 0.0f;
150 pCS->GetRGB(pResults, R, G, B);
151 rgb_array[i] = FXARGB_TODIB(FXARGB_MAKE(alpha, FXSYS_round(R * 255), FXSYS_round(G * 255), FXSYS_round(B * 255)));
152 }
153 FX_FLOAT a = FXSYS_Mul(start_x - end_x, start_x - end_x) +
154 FXSYS_Mul(start_y - end_y, start_y - end_y) - FXSYS_Mul(start_r - end_r, start_r - end_r);
155 int width = pBitmap->GetWidth();
156 int height = pBitmap->GetHeight();
157 int pitch = pBitmap->GetPitch();
158 FX_BOOL bDecreasing = FALSE;
159 if (start_r > end_r) {
160 int length = (int)FXSYS_sqrt((FXSYS_Mul(start_x - end_x, start_x - end_x) + FXSYS_Mul(start_y - end_y, start_y - end_y)));
161 if (length < start_r - end_r) {
162 bDecreasing = TRUE;
163 }
164 }
165 for (int row = 0; row < height; row ++) {
166 FX_DWORD* dib_buf = (FX_DWORD*)(pBitmap->GetBuffer() + row * pitch);
167 for (int column = 0; column < width; column ++) {
168 FX_FLOAT x = (FX_FLOAT)column, y = (FX_FLOAT)row;
169 matrix.Transform(x, y);
170 FX_FLOAT b = -2 * (FXSYS_Mul(x - start_x, end_x - start_x) + FXSYS_Mul(y - start_y, end_y - start_y) +
171 FXSYS_Mul(start_r, end_r - start_r));
172 FX_FLOAT c = FXSYS_Mul(x - start_x, x - start_x) + FXSYS_Mul(y - start_y, y - start_y) -
173 FXSYS_Mul(start_r, start_r);
174 FX_FLOAT s;
175 if (a == 0) {
176 s = FXSYS_Div(-c, b);
177 } else {
178 FX_FLOAT b2_4ac = FXSYS_Mul(b, b) - 4 * FXSYS_Mul(a, c);
179 if (b2_4ac < 0) {
180 continue;
181 }
182 FX_FLOAT root = FXSYS_sqrt(b2_4ac);
183 FX_FLOAT s1, s2;
184 if (a > 0) {
185 s1 = FXSYS_Div(-b - root, 2 * a);
186 s2 = FXSYS_Div(-b + root, 2 * a);
187 } else {
188 s2 = FXSYS_Div(-b - root, 2 * a);
189 s1 = FXSYS_Div(-b + root, 2 * a);
190 }
191 if (bDecreasing) {
192 if (s1 >= 0 || bStartExtend) {
193 s = s1;
194 } else {
195 s = s2;
196 }
197 } else {
198 if (s2 <= 1.0f || bEndExtend) {
199 s = s2;
200 } else {
201 s = s1;
202 }
203 }
204 if ((start_r + s * (end_r - start_r)) < 0) {
205 continue;
206 }
207 }
208 int index = (FX_INT32)(s * (SHADING_STEPS - 1));
209 if (index < 0) {
210 if (!bStartExtend) {
211 continue;
212 }
213 index = 0;
214 }
215 if (index >= SHADING_STEPS) {
216 if (!bEndExtend) {
217 continue;
218 }
219 index = SHADING_STEPS - 1;
220 }
221 dib_buf[column] = rgb_array[index];
222 }
223 }
224 }
_DrawFuncShading(CFX_DIBitmap * pBitmap,CFX_AffineMatrix * pObject2Bitmap,CPDF_Dictionary * pDict,CPDF_Function ** pFuncs,int nFuncs,CPDF_ColorSpace * pCS,int alpha)225 static void _DrawFuncShading(CFX_DIBitmap* pBitmap, CFX_AffineMatrix* pObject2Bitmap,
226 CPDF_Dictionary* pDict, CPDF_Function** pFuncs, int nFuncs,
227 CPDF_ColorSpace* pCS, int alpha)
228 {
229 ASSERT(pBitmap->GetFormat() == FXDIB_Argb);
230 CPDF_Array* pDomain = pDict->GetArray(FX_BSTRC("Domain"));
231 FX_FLOAT xmin = 0, ymin = 0, xmax = 1.0f, ymax = 1.0f;
232 if (pDomain) {
233 xmin = pDomain->GetNumber(0);
234 xmax = pDomain->GetNumber(1);
235 ymin = pDomain->GetNumber(2);
236 ymax = pDomain->GetNumber(3);
237 }
238 CFX_AffineMatrix mtDomain2Target = pDict->GetMatrix(FX_BSTRC("Matrix"));
239 CFX_AffineMatrix matrix, reverse_matrix;
240 matrix.SetReverse(*pObject2Bitmap);
241 reverse_matrix.SetReverse(mtDomain2Target);
242 matrix.Concat(reverse_matrix);
243 int width = pBitmap->GetWidth();
244 int height = pBitmap->GetHeight();
245 int pitch = pBitmap->GetPitch();
246 int total_results = 0;
247 for (int j = 0; j < nFuncs; j ++) {
248 if (pFuncs[j]) {
249 total_results += pFuncs[j]->CountOutputs();
250 }
251 }
252 if (pCS->CountComponents() > total_results) {
253 total_results = pCS->CountComponents();
254 }
255 CFX_FixedBufGrow<FX_FLOAT, 16> result_array(total_results);
256 FX_FLOAT* pResults = result_array;
257 FXSYS_memset32(pResults, 0, total_results * sizeof(FX_FLOAT));
258 for (int row = 0; row < height; row ++) {
259 FX_DWORD* dib_buf = (FX_DWORD*)(pBitmap->GetBuffer() + row * pitch);
260 for (int column = 0; column < width; column ++) {
261 FX_FLOAT x = (FX_FLOAT)column, y = (FX_FLOAT)row;
262 matrix.Transform(x, y);
263 if (x < xmin || x > xmax || y < ymin || y > ymax) {
264 continue;
265 }
266 FX_FLOAT input[2];
267 int offset = 0;
268 input[0] = x;
269 input[1] = y;
270 for (int j = 0; j < nFuncs; j ++) {
271 if (pFuncs[j]) {
272 int nresults;
273 if (pFuncs[j]->Call(input, 2, pResults + offset, nresults)) {
274 offset += nresults;
275 }
276 }
277 }
278 FX_FLOAT R = 0.0f, G = 0.0f, B = 0.0f;
279 pCS->GetRGB(pResults, R, G, B);
280 dib_buf[column] = FXARGB_TODIB(FXARGB_MAKE(alpha, (FX_INT32)(R * 255), (FX_INT32)(G * 255), (FX_INT32)(B * 255)));
281 }
282 }
283 }
_GetScanlineIntersect(int y,FX_FLOAT x1,FX_FLOAT y1,FX_FLOAT x2,FX_FLOAT y2,FX_FLOAT & x)284 FX_BOOL _GetScanlineIntersect(int y, FX_FLOAT x1, FX_FLOAT y1, FX_FLOAT x2, FX_FLOAT y2, FX_FLOAT& x)
285 {
286 if (y1 == y2) {
287 return FALSE;
288 }
289 if (y1 < y2) {
290 if (y < y1 || y > y2) {
291 return FALSE;
292 }
293 } else {
294 if (y < y2 || y > y1) {
295 return FALSE;
296 }
297 }
298 x = x1 + FXSYS_MulDiv(x2 - x1, y - y1, y2 - y1);
299 return TRUE;
300 }
_DrawGouraud(CFX_DIBitmap * pBitmap,int alpha,CPDF_MeshVertex triangle[3])301 static void _DrawGouraud(CFX_DIBitmap* pBitmap, int alpha, CPDF_MeshVertex triangle[3])
302 {
303 FX_FLOAT min_y = triangle[0].y, max_y = triangle[0].y;
304 for (int i = 1; i < 3; i ++) {
305 if (min_y > triangle[i].y) {
306 min_y = triangle[i].y;
307 }
308 if (max_y < triangle[i].y) {
309 max_y = triangle[i].y;
310 }
311 }
312 if (min_y == max_y) {
313 return;
314 }
315 int min_yi = (int)FXSYS_floor(min_y), max_yi = (int)FXSYS_ceil(max_y);
316 if (min_yi < 0) {
317 min_yi = 0;
318 }
319 if (max_yi >= pBitmap->GetHeight()) {
320 max_yi = pBitmap->GetHeight() - 1;
321 }
322 for (int y = min_yi; y <= max_yi; y ++) {
323 int nIntersects = 0;
324 FX_FLOAT inter_x[3], r[3], g[3], b[3];
325 for (int i = 0; i < 3; i ++) {
326 CPDF_MeshVertex& vertex1 = triangle[i];
327 CPDF_MeshVertex& vertex2 = triangle[(i + 1) % 3];
328 FX_BOOL bIntersect = _GetScanlineIntersect(y, vertex1.x, vertex1.y,
329 vertex2.x, vertex2.y, inter_x[nIntersects]);
330 if (!bIntersect) {
331 continue;
332 }
333 r[nIntersects] = vertex1.r + FXSYS_MulDiv(vertex2.r - vertex1.r, y - vertex1.y, vertex2.y - vertex1.y);
334 g[nIntersects] = vertex1.g + FXSYS_MulDiv(vertex2.g - vertex1.g, y - vertex1.y, vertex2.y - vertex1.y);
335 b[nIntersects] = vertex1.b + FXSYS_MulDiv(vertex2.b - vertex1.b, y - vertex1.y, vertex2.y - vertex1.y);
336 nIntersects ++;
337 }
338 if (nIntersects != 2) {
339 continue;
340 }
341 int min_x, max_x, start_index, end_index;
342 if (inter_x[0] < inter_x[1]) {
343 min_x = (int)FXSYS_floor(inter_x[0]);
344 max_x = (int)FXSYS_ceil(inter_x[1]);
345 start_index = 0;
346 end_index = 1;
347 } else {
348 min_x = (int)FXSYS_floor(inter_x[1]);
349 max_x = (int)FXSYS_ceil(inter_x[0]);
350 start_index = 1;
351 end_index = 0;
352 }
353 int start_x = min_x, end_x = max_x;
354 if (start_x < 0) {
355 start_x = 0;
356 }
357 if (end_x > pBitmap->GetWidth()) {
358 end_x = pBitmap->GetWidth();
359 }
360 FX_LPBYTE dib_buf = pBitmap->GetBuffer() + y * pBitmap->GetPitch() + start_x * 4;
361 FX_FLOAT r_unit = (r[end_index] - r[start_index]) / (max_x - min_x);
362 FX_FLOAT g_unit = (g[end_index] - g[start_index]) / (max_x - min_x);
363 FX_FLOAT b_unit = (b[end_index] - b[start_index]) / (max_x - min_x);
364 FX_FLOAT R = r[start_index] + (start_x - min_x) * r_unit;
365 FX_FLOAT G = g[start_index] + (start_x - min_x) * g_unit;
366 FX_FLOAT B = b[start_index] + (start_x - min_x) * b_unit;
367 for (int x = start_x; x < end_x; x ++) {
368 R += r_unit;
369 G += g_unit;
370 B += b_unit;
371 FXARGB_SETDIB(dib_buf, FXARGB_MAKE(alpha, (FX_INT32)(R * 255), (FX_INT32)(G * 255), (FX_INT32)(B * 255)));
372 dib_buf += 4;
373 }
374 }
375 }
_DrawFreeGouraudShading(CFX_DIBitmap * pBitmap,CFX_AffineMatrix * pObject2Bitmap,CPDF_Stream * pShadingStream,CPDF_Function ** pFuncs,int nFuncs,CPDF_ColorSpace * pCS,int alpha)376 static void _DrawFreeGouraudShading(CFX_DIBitmap* pBitmap, CFX_AffineMatrix* pObject2Bitmap,
377 CPDF_Stream* pShadingStream, CPDF_Function** pFuncs, int nFuncs,
378 CPDF_ColorSpace* pCS, int alpha)
379 {
380 ASSERT(pBitmap->GetFormat() == FXDIB_Argb);
381 if (pShadingStream->GetType() != PDFOBJ_STREAM) {
382 return;
383 }
384 CPDF_MeshStream stream;
385 if (!stream.Load(pShadingStream, pFuncs, nFuncs, pCS)) {
386 return;
387 }
388 CPDF_MeshVertex triangle[3];
389 FXSYS_memset32(triangle, 0, sizeof(triangle));
390
391 while (!stream.m_BitStream.IsEOF()) {
392 CPDF_MeshVertex vertex;
393 FX_DWORD flag = stream.GetVertex(vertex, pObject2Bitmap);
394 if (flag == 0) {
395 triangle[0] = vertex;
396 for (int j = 1; j < 3; j ++) {
397 stream.GetVertex(triangle[j], pObject2Bitmap);
398 }
399 } else {
400 if (flag == 1) {
401 triangle[0] = triangle[1];
402 }
403 triangle[1] = triangle[2];
404 triangle[2] = vertex;
405 }
406 _DrawGouraud(pBitmap, alpha, triangle);
407 }
408 }
_DrawLatticeGouraudShading(CFX_DIBitmap * pBitmap,CFX_AffineMatrix * pObject2Bitmap,CPDF_Stream * pShadingStream,CPDF_Function ** pFuncs,int nFuncs,CPDF_ColorSpace * pCS,int alpha)409 static void _DrawLatticeGouraudShading(CFX_DIBitmap* pBitmap, CFX_AffineMatrix* pObject2Bitmap,
410 CPDF_Stream* pShadingStream, CPDF_Function** pFuncs, int nFuncs,
411 CPDF_ColorSpace* pCS, int alpha)
412 {
413 ASSERT(pBitmap->GetFormat() == FXDIB_Argb);
414 if (pShadingStream->GetType() != PDFOBJ_STREAM) {
415 return;
416 }
417 int row_verts = pShadingStream->GetDict()->GetInteger("VerticesPerRow");
418 if (row_verts < 2) {
419 return;
420 }
421 CPDF_MeshStream stream;
422 if (!stream.Load(pShadingStream, pFuncs, nFuncs, pCS)) {
423 return;
424 }
425 CPDF_MeshVertex* vertex = FX_Alloc2D(CPDF_MeshVertex, row_verts, 2);
426 if (!stream.GetVertexRow(vertex, row_verts, pObject2Bitmap)) {
427 FX_Free(vertex);
428 return;
429 }
430 int last_index = 0;
431 while (1) {
432 CPDF_MeshVertex* last_row = vertex + last_index * row_verts;
433 CPDF_MeshVertex* this_row = vertex + (1 - last_index) * row_verts;
434 if (!stream.GetVertexRow(this_row, row_verts, pObject2Bitmap)) {
435 FX_Free(vertex);
436 return;
437 }
438 CPDF_MeshVertex triangle[3];
439 for (int i = 1; i < row_verts; i ++) {
440 triangle[0] = last_row[i];
441 triangle[1] = this_row[i - 1];
442 triangle[2] = last_row[i - 1];
443 _DrawGouraud(pBitmap, alpha, triangle);
444 triangle[2] = this_row[i];
445 _DrawGouraud(pBitmap, alpha, triangle);
446 }
447 last_index = 1 - last_index;
448 }
449 FX_Free(vertex);
450 }
451 struct Coon_BezierCoeff {
452 float a, b, c, d;
FromPointsCoon_BezierCoeff453 void FromPoints(float p0, float p1, float p2, float p3)
454 {
455 a = -p0 + 3 * p1 - 3 * p2 + p3;
456 b = 3 * p0 - 6 * p1 + 3 * p2;
457 c = -3 * p0 + 3 * p1;
458 d = p0;
459 }
first_halfCoon_BezierCoeff460 Coon_BezierCoeff first_half()
461 {
462 Coon_BezierCoeff result;
463 result.a = a / 8;
464 result.b = b / 4;
465 result.c = c / 2;
466 result.d = d;
467 return result;
468 }
second_halfCoon_BezierCoeff469 Coon_BezierCoeff second_half()
470 {
471 Coon_BezierCoeff result;
472 result.a = a / 8;
473 result.b = 3 * a / 8 + b / 4;
474 result.c = 3 * a / 8 + b / 2 + c / 2;
475 result.d = a / 8 + b / 4 + c / 2 + d;
476 return result;
477 }
GetPointsCoon_BezierCoeff478 void GetPoints(float p[4])
479 {
480 p[0] = d;
481 p[1] = c / 3 + p[0];
482 p[2] = b / 3 - p[0] + 2 * p[1];
483 p[3] = a + p[0] - 3 * p[1] + 3 * p[2];
484 }
GetPointsReverseCoon_BezierCoeff485 void GetPointsReverse(float p[4])
486 {
487 p[3] = d;
488 p[2] = c / 3 + p[3];
489 p[1] = b / 3 - p[3] + 2 * p[2];
490 p[0] = a + p[3] - 3 * p[2] + 3 * p[1];
491 }
BezierInterpolCoon_BezierCoeff492 void BezierInterpol(Coon_BezierCoeff& C1, Coon_BezierCoeff& C2, Coon_BezierCoeff& D1, Coon_BezierCoeff& D2)
493 {
494 a = (D1.a + D2.a) / 2;
495 b = (D1.b + D2.b) / 2;
496 c = (D1.c + D2.c) / 2 - (C1.a / 8 + C1.b / 4 + C1.c / 2) + (C2.a / 8 + C2.b / 4) + (-C1.d + D2.d) / 2 - (C2.a + C2.b) / 2;
497 d = C1.a / 8 + C1.b / 4 + C1.c / 2 + C1.d;
498 }
DistanceCoon_BezierCoeff499 float Distance()
500 {
501 float dis = a + b + c;
502 return dis < 0 ? -dis : dis;
503 }
504 };
505 struct Coon_Bezier {
506 Coon_BezierCoeff x, y;
FromPointsCoon_Bezier507 void FromPoints(float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3)
508 {
509 x.FromPoints(x0, x1, x2, x3);
510 y.FromPoints(y0, y1, y2, y3);
511 }
first_halfCoon_Bezier512 Coon_Bezier first_half()
513 {
514 Coon_Bezier result;
515 result.x = x.first_half();
516 result.y = y.first_half();
517 return result;
518 }
second_halfCoon_Bezier519 Coon_Bezier second_half()
520 {
521 Coon_Bezier result;
522 result.x = x.second_half();
523 result.y = y.second_half();
524 return result;
525 }
BezierInterpolCoon_Bezier526 void BezierInterpol(Coon_Bezier& C1, Coon_Bezier& C2, Coon_Bezier& D1, Coon_Bezier& D2)
527 {
528 x.BezierInterpol(C1.x, C2.x, D1.x, D2.x);
529 y.BezierInterpol(C1.y, C2.y, D1.y, D2.y);
530 }
GetPointsCoon_Bezier531 void GetPoints(FX_PATHPOINT* pPoints)
532 {
533 float p[4];
534 int i;
535 x.GetPoints(p);
536 for (i = 0; i < 4; i ++) {
537 pPoints[i].m_PointX = p[i];
538 }
539 y.GetPoints(p);
540 for (i = 0; i < 4; i ++) {
541 pPoints[i].m_PointY = p[i];
542 }
543 }
GetPointsReverseCoon_Bezier544 void GetPointsReverse(FX_PATHPOINT* pPoints)
545 {
546 float p[4];
547 int i;
548 x.GetPointsReverse(p);
549 for (i = 0; i < 4; i ++) {
550 pPoints[i].m_PointX = p[i];
551 }
552 y.GetPointsReverse(p);
553 for (i = 0; i < 4; i ++) {
554 pPoints[i].m_PointY = p[i];
555 }
556 }
DistanceCoon_Bezier557 float Distance()
558 {
559 return x.Distance() + y.Distance();
560 }
561 };
_BiInterpol(int c0,int c1,int c2,int c3,int x,int y,int x_scale,int y_scale)562 static int _BiInterpol(int c0, int c1, int c2, int c3, int x, int y, int x_scale, int y_scale)
563 {
564 int x1 = c0 + (c3 - c0) * x / x_scale;
565 int x2 = c1 + (c2 - c1) * x / x_scale;
566 return x1 + (x2 - x1) * y / y_scale;
567 }
568 struct Coon_Color {
Coon_ColorCoon_Color569 Coon_Color()
570 {
571 FXSYS_memset32(comp, 0, sizeof(int) * 3);
572 }
573 int comp[3];
BiInterpolCoon_Color574 void BiInterpol(Coon_Color colors[4], int x, int y, int x_scale, int y_scale)
575 {
576 for (int i = 0; i < 3; i ++)
577 comp[i] = _BiInterpol(colors[0].comp[i], colors[1].comp[i], colors[2].comp[i], colors[3].comp[i],
578 x, y, x_scale, y_scale);
579 }
DistanceCoon_Color580 int Distance(Coon_Color& o)
581 {
582 int max, diff;
583 max = FXSYS_abs(comp[0] - o.comp[0]);
584 diff = FXSYS_abs(comp[1] - o.comp[1]);
585 if (max < diff) {
586 max = diff;
587 }
588 diff = FXSYS_abs(comp[2] - o.comp[2]);
589 if (max < diff) {
590 max = diff;
591 }
592 return max;
593 }
594 };
595 struct CPDF_PatchDrawer {
596 Coon_Color patch_colors[4];
597 int max_delta;
598 CFX_PathData path;
599 CFX_RenderDevice* pDevice;
600 int fill_mode;
601 int alpha;
DrawCPDF_PatchDrawer602 void Draw(int x_scale, int y_scale, int left, int bottom, Coon_Bezier C1, Coon_Bezier C2, Coon_Bezier D1, Coon_Bezier D2)
603 {
604 FX_BOOL bSmall = C1.Distance() < 2 && C2.Distance() < 2 && D1.Distance() < 2 && D2.Distance() < 2;
605 Coon_Color div_colors[4];
606 int d_bottom, d_left, d_top, d_right;
607 div_colors[0].BiInterpol(patch_colors, left, bottom, x_scale, y_scale);
608 if (!bSmall) {
609 div_colors[1].BiInterpol(patch_colors, left, bottom + 1, x_scale, y_scale);
610 div_colors[2].BiInterpol(patch_colors, left + 1, bottom + 1, x_scale, y_scale);
611 div_colors[3].BiInterpol(patch_colors, left + 1, bottom, x_scale, y_scale);
612 d_bottom = div_colors[3].Distance(div_colors[0]);
613 d_left = div_colors[1].Distance(div_colors[0]);
614 d_top = div_colors[1].Distance(div_colors[2]);
615 d_right = div_colors[2].Distance(div_colors[3]);
616 }
617 #define COONCOLOR_THRESHOLD 4
618 if (bSmall || (d_bottom < COONCOLOR_THRESHOLD && d_left < COONCOLOR_THRESHOLD &&
619 d_top < COONCOLOR_THRESHOLD && d_right < COONCOLOR_THRESHOLD)) {
620 FX_PATHPOINT* pPoints = path.GetPoints();
621 C1.GetPoints(pPoints);
622 D2.GetPoints(pPoints + 3);
623 C2.GetPointsReverse(pPoints + 6);
624 D1.GetPointsReverse(pPoints + 9);
625 int fillFlags = FXFILL_WINDING | FXFILL_FULLCOVER;
626 if (fill_mode & RENDER_NOPATHSMOOTH) {
627 fillFlags |= FXFILL_NOPATHSMOOTH;
628 }
629 pDevice->DrawPath(&path, NULL, NULL, FXARGB_MAKE(alpha, div_colors[0].comp[0], div_colors[0].comp[1], div_colors[0].comp[2]), 0, fillFlags);
630 } else {
631 if (d_bottom < COONCOLOR_THRESHOLD && d_top < COONCOLOR_THRESHOLD) {
632 Coon_Bezier m1;
633 m1.BezierInterpol(D1, D2, C1, C2);
634 y_scale *= 2;
635 bottom *= 2;
636 Draw(x_scale, y_scale, left, bottom, C1, m1, D1.first_half(), D2.first_half());
637 Draw(x_scale, y_scale, left, bottom + 1, m1, C2, D1.second_half(), D2.second_half());
638 } else if (d_left < COONCOLOR_THRESHOLD && d_right < COONCOLOR_THRESHOLD) {
639 Coon_Bezier m2;
640 m2.BezierInterpol(C1, C2, D1, D2);
641 x_scale *= 2;
642 left *= 2;
643 Draw(x_scale, y_scale, left, bottom, C1.first_half(), C2.first_half(), D1, m2);
644 Draw(x_scale, y_scale, left + 1, bottom, C1.second_half(), C2.second_half(), m2, D2);
645 } else {
646 Coon_Bezier m1, m2;
647 m1.BezierInterpol(D1, D2, C1, C2);
648 m2.BezierInterpol(C1, C2, D1, D2);
649 Coon_Bezier m1f = m1.first_half();
650 Coon_Bezier m1s = m1.second_half();
651 Coon_Bezier m2f = m2.first_half();
652 Coon_Bezier m2s = m2.second_half();
653 x_scale *= 2;
654 y_scale *= 2;
655 left *= 2;
656 bottom *= 2;
657 Draw(x_scale, y_scale, left, bottom, C1.first_half(), m1f, D1.first_half(), m2f);
658 Draw(x_scale, y_scale, left, bottom + 1, m1f, C2.first_half(), D1.second_half(), m2s);
659 Draw(x_scale, y_scale, left + 1, bottom, C1.second_half(), m1s, m2f, D2.first_half());
660 Draw(x_scale, y_scale, left + 1, bottom + 1, m1s, C2.second_half(), m2s, D2.second_half());
661 }
662 }
663 }
664 };
665
_CheckCoonTensorPara(const CPDF_MeshStream & stream)666 FX_BOOL _CheckCoonTensorPara(const CPDF_MeshStream &stream)
667 {
668 FX_BOOL bCoorBits = ( stream.m_nCoordBits== 1 ||
669 stream.m_nCoordBits == 2 ||
670 stream.m_nCoordBits == 4 ||
671 stream.m_nCoordBits == 8 ||
672 stream.m_nCoordBits == 12 ||
673 stream.m_nCoordBits == 16 ||
674 stream.m_nCoordBits == 24 ||
675 stream.m_nCoordBits == 32 );
676
677 FX_BOOL bCompBits = ( stream.m_nCompBits == 1 ||
678 stream.m_nCompBits == 2 ||
679 stream.m_nCompBits == 4 ||
680 stream.m_nCompBits == 8 ||
681 stream.m_nCompBits == 12 ||
682 stream.m_nCompBits == 16 );
683
684 FX_BOOL bFlagBits = ( stream.m_nFlagBits == 2 ||
685 stream.m_nFlagBits == 4 ||
686 stream.m_nFlagBits == 8 );
687
688 return bCoorBits && bCompBits && bFlagBits;
689 }
690
_DrawCoonPatchMeshes(FX_BOOL bTensor,CFX_DIBitmap * pBitmap,CFX_AffineMatrix * pObject2Bitmap,CPDF_Stream * pShadingStream,CPDF_Function ** pFuncs,int nFuncs,CPDF_ColorSpace * pCS,int fill_mode,int alpha)691 static void _DrawCoonPatchMeshes(FX_BOOL bTensor, CFX_DIBitmap* pBitmap, CFX_AffineMatrix* pObject2Bitmap,
692 CPDF_Stream* pShadingStream, CPDF_Function** pFuncs, int nFuncs,
693 CPDF_ColorSpace* pCS, int fill_mode, int alpha)
694 {
695 ASSERT(pBitmap->GetFormat() == FXDIB_Argb);
696 if (pShadingStream->GetType() != PDFOBJ_STREAM) {
697 return;
698 }
699 CFX_FxgeDevice device;
700 device.Attach(pBitmap);
701 CPDF_MeshStream stream;
702 if (!stream.Load(pShadingStream, pFuncs, nFuncs, pCS)) {
703 return;
704 }
705
706 if (!_CheckCoonTensorPara(stream)) {
707 return;
708 }
709
710 CPDF_PatchDrawer patch;
711 patch.alpha = alpha;
712 patch.pDevice = &device;
713 patch.fill_mode = fill_mode;
714 patch.path.SetPointCount(13);
715 FX_PATHPOINT* pPoints = patch.path.GetPoints();
716 pPoints[0].m_Flag = FXPT_MOVETO;
717 for (int i = 1; i < 13; i ++) {
718 pPoints[i].m_Flag = FXPT_BEZIERTO;
719 }
720 CFX_FloatPoint coords[16];
721 for (int i = 0; i < 16; i ++) {
722 coords[i].Set(0.0f, 0.0f);
723 }
724
725 int point_count = bTensor ? 16 : 12;
726 while (!stream.m_BitStream.IsEOF()) {
727 FX_DWORD flag = stream.GetFlag();
728 int iStartPoint = 0, iStartColor = 0, i = 0;
729 if (flag) {
730 iStartPoint = 4;
731 iStartColor = 2;
732 CFX_FloatPoint tempCoords[4];
733 for (i = 0; i < 4; i ++) {
734 tempCoords[i] = coords[(flag * 3 + i) % 12];
735 }
736 FXSYS_memcpy32(coords, tempCoords, sizeof(CFX_FloatPoint) * 4);
737 Coon_Color tempColors[2];
738 tempColors[0] = patch.patch_colors[flag];
739 tempColors[1] = patch.patch_colors[(flag + 1) % 4];
740 FXSYS_memcpy32(patch.patch_colors, tempColors, sizeof(Coon_Color) * 2);
741 }
742 for (i = iStartPoint; i < point_count; i ++) {
743 stream.GetCoords(coords[i].x, coords[i].y);
744 pObject2Bitmap->Transform(coords[i].x, coords[i].y);
745 }
746 for (i = iStartColor; i < 4; i ++) {
747 FX_FLOAT r=0.0f, g=0.0f, b=0.0f;
748 stream.GetColor(r, g, b);
749 patch.patch_colors[i].comp[0] = (FX_INT32)(r * 255);
750 patch.patch_colors[i].comp[1] = (FX_INT32)(g * 255);
751 patch.patch_colors[i].comp[2] = (FX_INT32)(b * 255);
752 }
753 CFX_FloatRect bbox = CFX_FloatRect::GetBBox(coords, point_count);
754 if (bbox.right <= 0 || bbox.left >= (FX_FLOAT)pBitmap->GetWidth() || bbox.top <= 0 ||
755 bbox.bottom >= (FX_FLOAT)pBitmap->GetHeight()) {
756 continue;
757 }
758 Coon_Bezier C1, C2, D1, D2;
759 C1.FromPoints(coords[0].x, coords[0].y, coords[11].x, coords[11].y, coords[10].x, coords[10].y,
760 coords[9].x, coords[9].y);
761 C2.FromPoints(coords[3].x, coords[3].y, coords[4].x, coords[4].y, coords[5].x, coords[5].y,
762 coords[6].x, coords[6].y);
763 D1.FromPoints(coords[0].x, coords[0].y, coords[1].x, coords[1].y, coords[2].x, coords[2].y,
764 coords[3].x, coords[3].y);
765 D2.FromPoints(coords[9].x, coords[9].y, coords[8].x, coords[8].y, coords[7].x, coords[7].y,
766 coords[6].x, coords[6].y);
767 patch.Draw(1, 1, 0, 0, C1, C2, D1, D2);
768 }
769 }
DrawShading(CPDF_ShadingPattern * pPattern,CFX_AffineMatrix * pMatrix,FX_RECT & clip_rect,int alpha,FX_BOOL bAlphaMode)770 void CPDF_RenderStatus::DrawShading(CPDF_ShadingPattern* pPattern, CFX_AffineMatrix* pMatrix,
771 FX_RECT& clip_rect, int alpha, FX_BOOL bAlphaMode)
772 {
773 CPDF_Function** pFuncs = pPattern->m_pFunctions;
774 int nFuncs = pPattern->m_nFuncs;
775 CPDF_Dictionary* pDict = pPattern->m_pShadingObj->GetDict();
776 CPDF_ColorSpace* pColorSpace = pPattern->m_pCS;
777 if (pColorSpace == NULL) {
778 return;
779 }
780 FX_ARGB background = 0;
781 if (!pPattern->m_bShadingObj && pPattern->m_pShadingObj->GetDict()->KeyExist(FX_BSTRC("Background"))) {
782 CPDF_Array* pBackColor = pPattern->m_pShadingObj->GetDict()->GetArray(FX_BSTRC("Background"));
783 if (pBackColor && pBackColor->GetCount() >= (FX_DWORD)pColorSpace->CountComponents()) {
784 CFX_FixedBufGrow<FX_FLOAT, 16> comps(pColorSpace->CountComponents());
785 for (int i = 0; i < pColorSpace->CountComponents(); i ++) {
786 comps[i] = pBackColor->GetNumber(i);
787 }
788 FX_FLOAT R = 0.0f, G = 0.0f, B = 0.0f;
789 pColorSpace->GetRGB(comps, R, G, B);
790 background = ArgbEncode(255, (FX_INT32)(R * 255), (FX_INT32)(G * 255), (FX_INT32)(B * 255));
791 }
792 }
793 if (pDict->KeyExist(FX_BSTRC("BBox"))) {
794 CFX_FloatRect rect = pDict->GetRect(FX_BSTRC("BBox"));
795 rect.Transform(pMatrix);
796 clip_rect.Intersect(rect.GetOutterRect());
797 }
798 CPDF_DeviceBuffer buffer;
799 buffer.Initialize(m_pContext, m_pDevice, &clip_rect, m_pCurObj, 150);
800 CFX_AffineMatrix FinalMatrix = *pMatrix;
801 FinalMatrix.Concat(*buffer.GetMatrix());
802 CFX_DIBitmap* pBitmap = buffer.GetBitmap();
803 if (pBitmap->GetBuffer() == NULL) {
804 return;
805 }
806 pBitmap->Clear(background);
807 int fill_mode = m_Options.m_Flags;
808 switch (pPattern->m_ShadingType) {
809 case 1:
810 _DrawFuncShading(pBitmap, &FinalMatrix, pDict, pFuncs, nFuncs, pColorSpace, alpha);
811 break;
812 case 2:
813 _DrawAxialShading(pBitmap, &FinalMatrix, pDict, pFuncs, nFuncs, pColorSpace, alpha);
814 break;
815 case 3:
816 _DrawRadialShading(pBitmap, &FinalMatrix, pDict, pFuncs, nFuncs, pColorSpace, alpha);
817 break;
818 case 4: {
819 _DrawFreeGouraudShading(pBitmap, &FinalMatrix, (CPDF_Stream*)pPattern->m_pShadingObj,
820 pFuncs, nFuncs, pColorSpace, alpha);
821 }
822 break;
823 case 5: {
824 _DrawLatticeGouraudShading(pBitmap, &FinalMatrix, (CPDF_Stream*)pPattern->m_pShadingObj,
825 pFuncs, nFuncs, pColorSpace, alpha);
826 }
827 break;
828 case 6:
829 case 7: {
830 _DrawCoonPatchMeshes(pPattern->m_ShadingType - 6, pBitmap, &FinalMatrix, (CPDF_Stream*)pPattern->m_pShadingObj,
831 pFuncs, nFuncs, pColorSpace, fill_mode, alpha);
832 }
833 break;
834 }
835 if (bAlphaMode) {
836 pBitmap->LoadChannel(FXDIB_Red, pBitmap, FXDIB_Alpha);
837 }
838 if (m_Options.m_ColorMode == RENDER_COLOR_GRAY) {
839 pBitmap->ConvertColorScale(m_Options.m_ForeColor, m_Options.m_BackColor);
840 }
841 buffer.OutputToDevice();
842 }
DrawShadingPattern(CPDF_ShadingPattern * pattern,CPDF_PageObject * pPageObj,const CFX_AffineMatrix * pObj2Device,FX_BOOL bStroke)843 void CPDF_RenderStatus::DrawShadingPattern(CPDF_ShadingPattern* pattern, CPDF_PageObject* pPageObj, const CFX_AffineMatrix* pObj2Device, FX_BOOL bStroke)
844 {
845 if (!pattern->Load()) {
846 return;
847 }
848 m_pDevice->SaveState();
849 if (pPageObj->m_Type == PDFPAGE_PATH) {
850 if (!SelectClipPath((CPDF_PathObject*)pPageObj, pObj2Device, bStroke)) {
851 m_pDevice->RestoreState();
852 return;
853 }
854 } else if (pPageObj->m_Type == PDFPAGE_IMAGE) {
855 FX_RECT rect = pPageObj->GetBBox(pObj2Device);
856 m_pDevice->SetClip_Rect(&rect);
857 } else {
858 return;
859 }
860 FX_RECT rect;
861 if (GetObjectClippedRect(pPageObj, pObj2Device, FALSE, rect)) {
862 m_pDevice->RestoreState();
863 return;
864 }
865 CFX_AffineMatrix matrix = pattern->m_Pattern2Form;
866 matrix.Concat(*pObj2Device);
867 GetScaledMatrix(matrix);
868 int alpha = pPageObj->m_GeneralState.GetAlpha(bStroke);
869 DrawShading(pattern, &matrix, rect, alpha, m_Options.m_ColorMode == RENDER_COLOR_ALPHA);
870 m_pDevice->RestoreState();
871 }
ProcessShading(CPDF_ShadingObject * pShadingObj,const CFX_AffineMatrix * pObj2Device)872 FX_BOOL CPDF_RenderStatus::ProcessShading(CPDF_ShadingObject* pShadingObj, const CFX_AffineMatrix* pObj2Device)
873 {
874 FX_RECT rect = pShadingObj->GetBBox(pObj2Device);
875 FX_RECT clip_box = m_pDevice->GetClipBox();
876 rect.Intersect(clip_box);
877 if (rect.IsEmpty()) {
878 return TRUE;
879 }
880 CFX_AffineMatrix matrix = pShadingObj->m_Matrix;
881 matrix.Concat(*pObj2Device);
882 DrawShading(pShadingObj->m_pShading, &matrix, rect, pShadingObj->m_GeneralState.GetAlpha(FALSE),
883 m_Options.m_ColorMode == RENDER_COLOR_ALPHA);
884 return TRUE;
885 }
DrawPatternBitmap(CPDF_Document * pDoc,CPDF_PageRenderCache * pCache,CPDF_TilingPattern * pPattern,const CFX_AffineMatrix * pObject2Device,int width,int height,int flags)886 static CFX_DIBitmap* DrawPatternBitmap(CPDF_Document* pDoc, CPDF_PageRenderCache* pCache,
887 CPDF_TilingPattern* pPattern, const CFX_AffineMatrix* pObject2Device,
888 int width, int height, int flags)
889 {
890 CFX_DIBitmap* pBitmap = new CFX_DIBitmap;
891 if (!pBitmap->Create(width, height, pPattern->m_bColored ? FXDIB_Argb : FXDIB_8bppMask)) {
892 delete pBitmap;
893 return NULL;
894 }
895 CFX_FxgeDevice bitmap_device;
896 bitmap_device.Attach(pBitmap);
897 pBitmap->Clear(0);
898 CFX_FloatRect cell_bbox = pPattern->m_BBox;
899 pPattern->m_Pattern2Form.TransformRect(cell_bbox);
900 pObject2Device->TransformRect(cell_bbox);
901 CFX_FloatRect bitmap_rect(0.0f, 0.0f, (FX_FLOAT)width, (FX_FLOAT)height);
902 CFX_AffineMatrix mtAdjust;
903 mtAdjust.MatchRect(bitmap_rect, cell_bbox);
904 CFX_AffineMatrix mtPattern2Bitmap = *pObject2Device;
905 mtPattern2Bitmap.Concat(mtAdjust);
906 CPDF_RenderOptions options;
907 if (!pPattern->m_bColored) {
908 options.m_ColorMode = RENDER_COLOR_ALPHA;
909 }
910 flags |= RENDER_FORCE_HALFTONE;
911 options.m_Flags = flags;
912 CPDF_RenderContext context;
913 context.Create(pDoc, pCache, NULL);
914 context.DrawObjectList(&bitmap_device, pPattern->m_pForm, &mtPattern2Bitmap, &options);
915 return pBitmap;
916 }
DrawTilingPattern(CPDF_TilingPattern * pPattern,CPDF_PageObject * pPageObj,const CFX_AffineMatrix * pObj2Device,FX_BOOL bStroke)917 void CPDF_RenderStatus::DrawTilingPattern(CPDF_TilingPattern* pPattern, CPDF_PageObject* pPageObj, const CFX_AffineMatrix* pObj2Device, FX_BOOL bStroke)
918 {
919 if (!pPattern->Load()) {
920 return;
921 }
922 m_pDevice->SaveState();
923 if (pPageObj->m_Type == PDFPAGE_PATH) {
924 if (!SelectClipPath((CPDF_PathObject*)pPageObj, pObj2Device, bStroke)) {
925 m_pDevice->RestoreState();
926 return;
927 }
928 } else if (pPageObj->m_Type == PDFPAGE_IMAGE) {
929 FX_RECT rect = pPageObj->GetBBox(pObj2Device);
930 m_pDevice->SetClip_Rect(&rect);
931 } else {
932 return;
933 }
934 FX_RECT clip_box = m_pDevice->GetClipBox();
935 if (clip_box.IsEmpty()) {
936 m_pDevice->RestoreState();
937 return;
938 }
939 CFX_Matrix dCTM = m_pDevice->GetCTM();
940 FX_FLOAT sa = FXSYS_fabs(dCTM.a);
941 FX_FLOAT sd = FXSYS_fabs(dCTM.d);
942 clip_box.right = clip_box.left + (FX_INT32)FXSYS_ceil(clip_box.Width() * sa);
943 clip_box.bottom = clip_box.top + (FX_INT32)FXSYS_ceil(clip_box.Height() * sd);
944 CFX_AffineMatrix mtPattern2Device = pPattern->m_Pattern2Form;
945 mtPattern2Device.Concat(*pObj2Device);
946 GetScaledMatrix(mtPattern2Device);
947 FX_BOOL bAligned = FALSE;
948 if (pPattern->m_BBox.left == 0 && pPattern->m_BBox.bottom == 0 &&
949 pPattern->m_BBox.right == pPattern->m_XStep && pPattern->m_BBox.top == pPattern->m_YStep &&
950 (mtPattern2Device.IsScaled() || mtPattern2Device.Is90Rotated())) {
951 bAligned = TRUE;
952 }
953 CFX_FloatRect cell_bbox = pPattern->m_BBox;
954 mtPattern2Device.TransformRect(cell_bbox);
955 int width = (int)FXSYS_ceil(cell_bbox.Width());
956 int height = (int)FXSYS_ceil(cell_bbox.Height());
957 if (width == 0) {
958 width = 1;
959 }
960 if (height == 0) {
961 height = 1;
962 }
963 int min_col, max_col, min_row, max_row;
964 CFX_AffineMatrix mtDevice2Pattern;
965 mtDevice2Pattern.SetReverse(mtPattern2Device);
966 CFX_FloatRect clip_box_p(clip_box);
967 clip_box_p.Transform(&mtDevice2Pattern);
968 min_col = (int)FXSYS_ceil(FXSYS_Div(clip_box_p.left - pPattern->m_BBox.right, pPattern->m_XStep));
969 max_col = (int)FXSYS_floor(FXSYS_Div(clip_box_p.right - pPattern->m_BBox.left, pPattern->m_XStep));
970 min_row = (int)FXSYS_ceil(FXSYS_Div(clip_box_p.bottom - pPattern->m_BBox.top, pPattern->m_YStep));
971 max_row = (int)FXSYS_floor(FXSYS_Div(clip_box_p.top - pPattern->m_BBox.bottom, pPattern->m_YStep));
972 if (width > clip_box.Width() || height > clip_box.Height() || width * height > clip_box.Width() * clip_box.Height()) {
973 CPDF_GraphicStates* pStates = NULL;
974 if (!pPattern->m_bColored) {
975 pStates = CloneObjStates(pPageObj, bStroke);
976 }
977 CPDF_Dictionary* pFormResource = NULL;
978 if (pPattern->m_pForm->m_pFormDict) {
979 pFormResource = pPattern->m_pForm->m_pFormDict->GetDict(FX_BSTRC("Resources"));
980 }
981 for (int col = min_col; col <= max_col; col ++)
982 for (int row = min_row; row <= max_row; row ++) {
983 FX_FLOAT orig_x, orig_y;
984 orig_x = col * pPattern->m_XStep;
985 orig_y = row * pPattern->m_YStep;
986 mtPattern2Device.Transform(orig_x, orig_y);
987 CFX_AffineMatrix matrix = *pObj2Device;
988 matrix.Translate(orig_x - mtPattern2Device.e, orig_y - mtPattern2Device.f);
989 m_pDevice->SaveState();
990 CPDF_RenderStatus status;
991 status.Initialize(m_pContext, m_pDevice, NULL, NULL, this, pStates, &m_Options,
992 pPattern->m_pForm->m_Transparency, m_bDropObjects, pFormResource);
993 status.RenderObjectList(pPattern->m_pForm, &matrix);
994 m_pDevice->RestoreState();
995 }
996 m_pDevice->RestoreState();
997 if (pStates) {
998 delete pStates;
999 }
1000 return;
1001 }
1002 if (bAligned) {
1003 int orig_x = FXSYS_round(mtPattern2Device.e);
1004 int orig_y = FXSYS_round(mtPattern2Device.f);
1005 min_col = (clip_box.left - orig_x) / width;
1006 if (clip_box.left < orig_x) {
1007 min_col --;
1008 }
1009 max_col = (clip_box.right - orig_x) / width;
1010 if (clip_box.right <= orig_x) {
1011 max_col --;
1012 }
1013 min_row = (clip_box.top - orig_y) / height;
1014 if (clip_box.top < orig_y) {
1015 min_row --;
1016 }
1017 max_row = (clip_box.bottom - orig_y) / height;
1018 if (clip_box.bottom <= orig_y) {
1019 max_row --;
1020 }
1021 }
1022 FX_FLOAT left_offset = cell_bbox.left - mtPattern2Device.e;
1023 FX_FLOAT top_offset = cell_bbox.bottom - mtPattern2Device.f;
1024 CFX_DIBitmap* pPatternBitmap = NULL;
1025 if (width * height < 16) {
1026 CFX_DIBitmap* pEnlargedBitmap = DrawPatternBitmap(m_pContext->m_pDocument, m_pContext->m_pPageCache, pPattern, pObj2Device, 8, 8, m_Options.m_Flags);
1027 pPatternBitmap = pEnlargedBitmap->StretchTo(width, height);
1028 delete pEnlargedBitmap;
1029 } else {
1030 pPatternBitmap = DrawPatternBitmap(m_pContext->m_pDocument, m_pContext->m_pPageCache, pPattern, pObj2Device, width, height, m_Options.m_Flags);
1031 }
1032 if (pPatternBitmap == NULL) {
1033 m_pDevice->RestoreState();
1034 return;
1035 }
1036 if (m_Options.m_ColorMode == RENDER_COLOR_GRAY) {
1037 pPatternBitmap->ConvertColorScale(m_Options.m_ForeColor, m_Options.m_BackColor);
1038 }
1039 FX_ARGB fill_argb = GetFillArgb(pPageObj);
1040 int clip_width = clip_box.right - clip_box.left;
1041 int clip_height = clip_box.bottom - clip_box.top;
1042 CFX_DIBitmap screen;
1043 if (!screen.Create(clip_width, clip_height, FXDIB_Argb)) {
1044 return;
1045 }
1046 screen.Clear(0);
1047 FX_DWORD* src_buf = (FX_DWORD*)pPatternBitmap->GetBuffer();
1048 for (int col = min_col; col <= max_col; col ++) {
1049 for (int row = min_row; row <= max_row; row ++) {
1050 int start_x, start_y;
1051 if (bAligned) {
1052 start_x = FXSYS_round(mtPattern2Device.e) + col * width - clip_box.left;
1053 start_y = FXSYS_round(mtPattern2Device.f) + row * height - clip_box.top;
1054 } else {
1055 FX_FLOAT orig_x = col * pPattern->m_XStep;
1056 FX_FLOAT orig_y = row * pPattern->m_YStep;
1057 mtPattern2Device.Transform(orig_x, orig_y);
1058 start_x = FXSYS_round(orig_x + left_offset) - clip_box.left;
1059 start_y = FXSYS_round(orig_y + top_offset) - clip_box.top;
1060 }
1061 if (width == 1 && height == 1) {
1062 if (start_x < 0 || start_x >= clip_box.Width() || start_y < 0 || start_y >= clip_box.Height()) {
1063 continue;
1064 }
1065 FX_DWORD* dest_buf = (FX_DWORD*)(screen.GetBuffer() + screen.GetPitch() * start_y + start_x * 4);
1066 if (pPattern->m_bColored) {
1067 *dest_buf = *src_buf;
1068 } else {
1069 *dest_buf = (*(FX_LPBYTE)src_buf << 24) | (fill_argb & 0xffffff);
1070 }
1071 } else {
1072 if (pPattern->m_bColored) {
1073 screen.CompositeBitmap(start_x, start_y, width, height, pPatternBitmap, 0, 0);
1074 } else {
1075 screen.CompositeMask(start_x, start_y, width, height, pPatternBitmap, fill_argb, 0, 0);
1076 }
1077 }
1078 }
1079 }
1080 CompositeDIBitmap(&screen, clip_box.left, clip_box.top, 0, 255, FXDIB_BLEND_NORMAL, FALSE);
1081 m_pDevice->RestoreState();
1082 delete pPatternBitmap;
1083 }
DrawPathWithPattern(CPDF_PathObject * pPathObj,const CFX_AffineMatrix * pObj2Device,CPDF_Color * pColor,FX_BOOL bStroke)1084 void CPDF_RenderStatus::DrawPathWithPattern(CPDF_PathObject* pPathObj, const CFX_AffineMatrix* pObj2Device, CPDF_Color* pColor, FX_BOOL bStroke)
1085 {
1086 CPDF_Pattern* pattern = pColor->GetPattern();
1087 if (pattern == NULL) {
1088 return;
1089 }
1090 if(pattern->m_PatternType == PATTERN_TILING) {
1091 DrawTilingPattern((CPDF_TilingPattern*)pattern, pPathObj, pObj2Device, bStroke);
1092 } else {
1093 DrawShadingPattern((CPDF_ShadingPattern*)pattern, pPathObj, pObj2Device, bStroke);
1094 }
1095 }
ProcessPathPattern(CPDF_PathObject * pPathObj,const CFX_AffineMatrix * pObj2Device,int & filltype,FX_BOOL & bStroke)1096 void CPDF_RenderStatus::ProcessPathPattern(CPDF_PathObject* pPathObj, const CFX_AffineMatrix* pObj2Device, int& filltype, FX_BOOL& bStroke)
1097 {
1098 if(filltype) {
1099 CPDF_Color& FillColor = *pPathObj->m_ColorState.GetFillColor();
1100 if(FillColor.m_pCS && FillColor.m_pCS->GetFamily() == PDFCS_PATTERN) {
1101 DrawPathWithPattern(pPathObj, pObj2Device, &FillColor, FALSE);
1102 filltype = 0;
1103 }
1104 }
1105 if(bStroke) {
1106 CPDF_Color& StrokeColor = *pPathObj->m_ColorState.GetStrokeColor();
1107 if(StrokeColor.m_pCS && StrokeColor.m_pCS->GetFamily() == PDFCS_PATTERN) {
1108 DrawPathWithPattern(pPathObj, pObj2Device, &StrokeColor, TRUE);
1109 bStroke = FALSE;
1110 }
1111 }
1112 }
1113