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.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
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
13 static void DrawAxialShading(CFX_DIBitmap* pBitmap,
14 CFX_AffineMatrix* pObject2Bitmap,
15 CPDF_Dictionary* pDict,
16 CPDF_Function** pFuncs,
20 ASSERT(pBitmap->GetFormat() == FXDIB_Argb);
21 CPDF_Array* pCoords = pDict->GetArray(FX_BSTRC("Coords"));
22 if (pCoords == NULL) {
25 FX_FLOAT start_x = pCoords->GetNumber(0);
26 FX_FLOAT start_y = pCoords->GetNumber(1);
27 FX_FLOAT end_x = pCoords->GetNumber(2);
28 FX_FLOAT end_y = pCoords->GetNumber(3);
29 FX_FLOAT t_min = 0, t_max = 1.0f;
30 CPDF_Array* pArray = pDict->GetArray(FX_BSTRC("Domain"));
32 t_min = pArray->GetNumber(0);
33 t_max = pArray->GetNumber(1);
35 FX_BOOL bStartExtend = FALSE, bEndExtend = FALSE;
36 pArray = pDict->GetArray(FX_BSTRC("Extend"));
38 bStartExtend = pArray->GetInteger(0);
39 bEndExtend = pArray->GetInteger(1);
41 int width = pBitmap->GetWidth();
42 int height = pBitmap->GetHeight();
43 FX_FLOAT x_span = end_x - start_x;
44 FX_FLOAT y_span = end_y - start_y;
45 FX_FLOAT axis_len_square =
46 FXSYS_Mul(x_span, x_span) + FXSYS_Mul(y_span, y_span);
47 CFX_AffineMatrix matrix;
48 matrix.SetReverse(*pObject2Bitmap);
49 int total_results = 0;
50 for (int j = 0; j < nFuncs; j++) {
52 total_results += pFuncs[j]->CountOutputs();
55 if (pCS->CountComponents() > total_results) {
56 total_results = pCS->CountComponents();
58 CFX_FixedBufGrow<FX_FLOAT, 16> result_array(total_results);
59 FX_FLOAT* pResults = result_array;
60 FXSYS_memset(pResults, 0, total_results * sizeof(FX_FLOAT));
61 FX_DWORD rgb_array[SHADING_STEPS];
62 for (int i = 0; i < SHADING_STEPS; i++) {
63 FX_FLOAT input = (t_max - t_min) * i / SHADING_STEPS + t_min;
65 for (int j = 0; j < nFuncs; j++) {
68 if (pFuncs[j]->Call(&input, 1, pResults + offset, nresults)) {
73 FX_FLOAT R = 0.0f, G = 0.0f, B = 0.0f;
74 pCS->GetRGB(pResults, R, G, B);
76 FXARGB_TODIB(FXARGB_MAKE(alpha, FXSYS_round(R * 255),
77 FXSYS_round(G * 255), FXSYS_round(B * 255)));
79 int pitch = pBitmap->GetPitch();
80 for (int row = 0; row < height; row++) {
81 FX_DWORD* dib_buf = (FX_DWORD*)(pBitmap->GetBuffer() + row * pitch);
82 for (int column = 0; column < width; column++) {
83 FX_FLOAT x = (FX_FLOAT)column, y = (FX_FLOAT)row;
84 matrix.Transform(x, y);
85 FX_FLOAT scale = FXSYS_Div(
86 FXSYS_Mul(x - start_x, x_span) + FXSYS_Mul(y - start_y, y_span),
88 int index = (int32_t)(scale * (SHADING_STEPS - 1));
94 } else if (index >= SHADING_STEPS) {
98 index = SHADING_STEPS - 1;
100 dib_buf[column] = rgb_array[index];
104 static void DrawRadialShading(CFX_DIBitmap* pBitmap,
105 CFX_AffineMatrix* pObject2Bitmap,
106 CPDF_Dictionary* pDict,
107 CPDF_Function** pFuncs,
109 CPDF_ColorSpace* pCS,
111 ASSERT(pBitmap->GetFormat() == FXDIB_Argb);
112 CPDF_Array* pCoords = pDict->GetArray(FX_BSTRC("Coords"));
113 if (pCoords == NULL) {
116 FX_FLOAT start_x = pCoords->GetNumber(0);
117 FX_FLOAT start_y = pCoords->GetNumber(1);
118 FX_FLOAT start_r = pCoords->GetNumber(2);
119 FX_FLOAT end_x = pCoords->GetNumber(3);
120 FX_FLOAT end_y = pCoords->GetNumber(4);
121 FX_FLOAT end_r = pCoords->GetNumber(5);
122 CFX_AffineMatrix matrix;
123 matrix.SetReverse(*pObject2Bitmap);
124 FX_FLOAT t_min = 0, t_max = 1.0f;
125 CPDF_Array* pArray = pDict->GetArray(FX_BSTRC("Domain"));
127 t_min = pArray->GetNumber(0);
128 t_max = pArray->GetNumber(1);
130 FX_BOOL bStartExtend = FALSE, bEndExtend = FALSE;
131 pArray = pDict->GetArray(FX_BSTRC("Extend"));
133 bStartExtend = pArray->GetInteger(0);
134 bEndExtend = pArray->GetInteger(1);
136 int total_results = 0;
137 for (int j = 0; j < nFuncs; j++) {
139 total_results += pFuncs[j]->CountOutputs();
142 if (pCS->CountComponents() > total_results) {
143 total_results = pCS->CountComponents();
145 CFX_FixedBufGrow<FX_FLOAT, 16> result_array(total_results);
146 FX_FLOAT* pResults = result_array;
147 FXSYS_memset(pResults, 0, total_results * sizeof(FX_FLOAT));
148 FX_DWORD rgb_array[SHADING_STEPS];
149 for (int i = 0; i < SHADING_STEPS; i++) {
150 FX_FLOAT input = (t_max - t_min) * i / SHADING_STEPS + t_min;
152 for (int j = 0; j < nFuncs; j++) {
155 if (pFuncs[j]->Call(&input, 1, pResults + offset, nresults)) {
160 FX_FLOAT R = 0.0f, G = 0.0f, B = 0.0f;
161 pCS->GetRGB(pResults, R, G, B);
163 FXARGB_TODIB(FXARGB_MAKE(alpha, FXSYS_round(R * 255),
164 FXSYS_round(G * 255), FXSYS_round(B * 255)));
166 FX_FLOAT a = FXSYS_Mul(start_x - end_x, start_x - end_x) +
167 FXSYS_Mul(start_y - end_y, start_y - end_y) -
168 FXSYS_Mul(start_r - end_r, start_r - end_r);
169 int width = pBitmap->GetWidth();
170 int height = pBitmap->GetHeight();
171 int pitch = pBitmap->GetPitch();
172 FX_BOOL bDecreasing = FALSE;
173 if (start_r > end_r) {
174 int length = (int)FXSYS_sqrt((FXSYS_Mul(start_x - end_x, start_x - end_x) +
175 FXSYS_Mul(start_y - end_y, start_y - end_y)));
176 if (length < start_r - end_r) {
180 for (int row = 0; row < height; row++) {
181 FX_DWORD* dib_buf = (FX_DWORD*)(pBitmap->GetBuffer() + row * pitch);
182 for (int column = 0; column < width; column++) {
183 FX_FLOAT x = (FX_FLOAT)column, y = (FX_FLOAT)row;
184 matrix.Transform(x, y);
185 FX_FLOAT b = -2 * (FXSYS_Mul(x - start_x, end_x - start_x) +
186 FXSYS_Mul(y - start_y, end_y - start_y) +
187 FXSYS_Mul(start_r, end_r - start_r));
188 FX_FLOAT c = FXSYS_Mul(x - start_x, x - start_x) +
189 FXSYS_Mul(y - start_y, y - start_y) -
190 FXSYS_Mul(start_r, start_r);
193 s = FXSYS_Div(-c, b);
195 FX_FLOAT b2_4ac = FXSYS_Mul(b, b) - 4 * FXSYS_Mul(a, c);
199 FX_FLOAT root = FXSYS_sqrt(b2_4ac);
202 s1 = FXSYS_Div(-b - root, 2 * a);
203 s2 = FXSYS_Div(-b + root, 2 * a);
205 s2 = FXSYS_Div(-b - root, 2 * a);
206 s1 = FXSYS_Div(-b + root, 2 * a);
209 if (s1 >= 0 || bStartExtend) {
215 if (s2 <= 1.0f || bEndExtend) {
221 if ((start_r + s * (end_r - start_r)) < 0) {
225 int index = (int32_t)(s * (SHADING_STEPS - 1));
232 if (index >= SHADING_STEPS) {
236 index = SHADING_STEPS - 1;
238 dib_buf[column] = rgb_array[index];
242 static void DrawFuncShading(CFX_DIBitmap* pBitmap,
243 CFX_AffineMatrix* pObject2Bitmap,
244 CPDF_Dictionary* pDict,
245 CPDF_Function** pFuncs,
247 CPDF_ColorSpace* pCS,
249 ASSERT(pBitmap->GetFormat() == FXDIB_Argb);
250 CPDF_Array* pDomain = pDict->GetArray(FX_BSTRC("Domain"));
251 FX_FLOAT xmin = 0, ymin = 0, xmax = 1.0f, ymax = 1.0f;
253 xmin = pDomain->GetNumber(0);
254 xmax = pDomain->GetNumber(1);
255 ymin = pDomain->GetNumber(2);
256 ymax = pDomain->GetNumber(3);
258 CFX_AffineMatrix mtDomain2Target = pDict->GetMatrix(FX_BSTRC("Matrix"));
259 CFX_AffineMatrix matrix, reverse_matrix;
260 matrix.SetReverse(*pObject2Bitmap);
261 reverse_matrix.SetReverse(mtDomain2Target);
262 matrix.Concat(reverse_matrix);
263 int width = pBitmap->GetWidth();
264 int height = pBitmap->GetHeight();
265 int pitch = pBitmap->GetPitch();
266 int total_results = 0;
267 for (int j = 0; j < nFuncs; j++) {
269 total_results += pFuncs[j]->CountOutputs();
272 if (pCS->CountComponents() > total_results) {
273 total_results = pCS->CountComponents();
275 CFX_FixedBufGrow<FX_FLOAT, 16> result_array(total_results);
276 FX_FLOAT* pResults = result_array;
277 FXSYS_memset(pResults, 0, total_results * sizeof(FX_FLOAT));
278 for (int row = 0; row < height; row++) {
279 FX_DWORD* dib_buf = (FX_DWORD*)(pBitmap->GetBuffer() + row * pitch);
280 for (int column = 0; column < width; column++) {
281 FX_FLOAT x = (FX_FLOAT)column, y = (FX_FLOAT)row;
282 matrix.Transform(x, y);
283 if (x < xmin || x > xmax || y < ymin || y > ymax) {
290 for (int j = 0; j < nFuncs; j++) {
293 if (pFuncs[j]->Call(input, 2, pResults + offset, nresults)) {
298 FX_FLOAT R = 0.0f, G = 0.0f, B = 0.0f;
299 pCS->GetRGB(pResults, R, G, B);
300 dib_buf[column] = FXARGB_TODIB(FXARGB_MAKE(
301 alpha, (int32_t)(R * 255), (int32_t)(G * 255), (int32_t)(B * 255)));
305 FX_BOOL _GetScanlineIntersect(int y,
315 if (y < y1 || y > y2) {
319 if (y < y2 || y > y1) {
323 x = x1 + FXSYS_MulDiv(x2 - x1, y - y1, y2 - y1);
326 static void DrawGouraud(CFX_DIBitmap* pBitmap,
328 CPDF_MeshVertex triangle[3]) {
329 FX_FLOAT min_y = triangle[0].y, max_y = triangle[0].y;
330 for (int i = 1; i < 3; i++) {
331 if (min_y > triangle[i].y) {
332 min_y = triangle[i].y;
334 if (max_y < triangle[i].y) {
335 max_y = triangle[i].y;
338 if (min_y == max_y) {
341 int min_yi = (int)FXSYS_floor(min_y), max_yi = (int)FXSYS_ceil(max_y);
345 if (max_yi >= pBitmap->GetHeight()) {
346 max_yi = pBitmap->GetHeight() - 1;
348 for (int y = min_yi; y <= max_yi; y++) {
350 FX_FLOAT inter_x[3], r[3], g[3], b[3];
351 for (int i = 0; i < 3; i++) {
352 CPDF_MeshVertex& vertex1 = triangle[i];
353 CPDF_MeshVertex& vertex2 = triangle[(i + 1) % 3];
354 FX_BOOL bIntersect = _GetScanlineIntersect(
355 y, vertex1.x, vertex1.y, vertex2.x, vertex2.y, inter_x[nIntersects]);
360 vertex1.r + FXSYS_MulDiv(vertex2.r - vertex1.r, y - vertex1.y,
361 vertex2.y - vertex1.y);
363 vertex1.g + FXSYS_MulDiv(vertex2.g - vertex1.g, y - vertex1.y,
364 vertex2.y - vertex1.y);
366 vertex1.b + FXSYS_MulDiv(vertex2.b - vertex1.b, y - vertex1.y,
367 vertex2.y - vertex1.y);
370 if (nIntersects != 2) {
373 int min_x, max_x, start_index, end_index;
374 if (inter_x[0] < inter_x[1]) {
375 min_x = (int)FXSYS_floor(inter_x[0]);
376 max_x = (int)FXSYS_ceil(inter_x[1]);
380 min_x = (int)FXSYS_floor(inter_x[1]);
381 max_x = (int)FXSYS_ceil(inter_x[0]);
385 int start_x = min_x, end_x = max_x;
389 if (end_x > pBitmap->GetWidth()) {
390 end_x = pBitmap->GetWidth();
393 pBitmap->GetBuffer() + y * pBitmap->GetPitch() + start_x * 4;
394 FX_FLOAT r_unit = (r[end_index] - r[start_index]) / (max_x - min_x);
395 FX_FLOAT g_unit = (g[end_index] - g[start_index]) / (max_x - min_x);
396 FX_FLOAT b_unit = (b[end_index] - b[start_index]) / (max_x - min_x);
397 FX_FLOAT R = r[start_index] + (start_x - min_x) * r_unit;
398 FX_FLOAT G = g[start_index] + (start_x - min_x) * g_unit;
399 FX_FLOAT B = b[start_index] + (start_x - min_x) * b_unit;
400 for (int x = start_x; x < end_x; x++) {
404 FXARGB_SETDIB(dib_buf,
405 FXARGB_MAKE(alpha, (int32_t)(R * 255), (int32_t)(G * 255),
406 (int32_t)(B * 255)));
411 static void DrawFreeGouraudShading(CFX_DIBitmap* pBitmap,
412 CFX_AffineMatrix* pObject2Bitmap,
413 CPDF_Stream* pShadingStream,
414 CPDF_Function** pFuncs,
416 CPDF_ColorSpace* pCS,
418 ASSERT(pBitmap->GetFormat() == FXDIB_Argb);
420 CPDF_MeshStream stream;
421 if (!stream.Load(pShadingStream, pFuncs, nFuncs, pCS))
424 CPDF_MeshVertex triangle[3];
425 FXSYS_memset(triangle, 0, sizeof(triangle));
427 while (!stream.m_BitStream.IsEOF()) {
428 CPDF_MeshVertex vertex;
429 FX_DWORD flag = stream.GetVertex(vertex, pObject2Bitmap);
431 triangle[0] = vertex;
432 for (int j = 1; j < 3; j++) {
433 stream.GetVertex(triangle[j], pObject2Bitmap);
437 triangle[0] = triangle[1];
439 triangle[1] = triangle[2];
440 triangle[2] = vertex;
442 DrawGouraud(pBitmap, alpha, triangle);
445 static void DrawLatticeGouraudShading(CFX_DIBitmap* pBitmap,
446 CFX_AffineMatrix* pObject2Bitmap,
447 CPDF_Stream* pShadingStream,
448 CPDF_Function** pFuncs,
450 CPDF_ColorSpace* pCS,
452 ASSERT(pBitmap->GetFormat() == FXDIB_Argb);
454 int row_verts = pShadingStream->GetDict()->GetInteger("VerticesPerRow");
458 CPDF_MeshStream stream;
459 if (!stream.Load(pShadingStream, pFuncs, nFuncs, pCS))
462 CPDF_MeshVertex* vertex = FX_Alloc2D(CPDF_MeshVertex, row_verts, 2);
463 if (!stream.GetVertexRow(vertex, row_verts, pObject2Bitmap)) {
469 CPDF_MeshVertex* last_row = vertex + last_index * row_verts;
470 CPDF_MeshVertex* this_row = vertex + (1 - last_index) * row_verts;
471 if (!stream.GetVertexRow(this_row, row_verts, pObject2Bitmap)) {
475 CPDF_MeshVertex triangle[3];
476 for (int i = 1; i < row_verts; i++) {
477 triangle[0] = last_row[i];
478 triangle[1] = this_row[i - 1];
479 triangle[2] = last_row[i - 1];
480 DrawGouraud(pBitmap, alpha, triangle);
481 triangle[2] = this_row[i];
482 DrawGouraud(pBitmap, alpha, triangle);
484 last_index = 1 - last_index;
488 struct Coon_BezierCoeff {
490 void FromPoints(float p0, float p1, float p2, float p3) {
491 a = -p0 + 3 * p1 - 3 * p2 + p3;
492 b = 3 * p0 - 6 * p1 + 3 * p2;
493 c = -3 * p0 + 3 * p1;
496 Coon_BezierCoeff first_half() {
497 Coon_BezierCoeff result;
504 Coon_BezierCoeff second_half() {
505 Coon_BezierCoeff result;
507 result.b = 3 * a / 8 + b / 4;
508 result.c = 3 * a / 8 + b / 2 + c / 2;
509 result.d = a / 8 + b / 4 + c / 2 + d;
512 void GetPoints(float p[4]) {
515 p[2] = b / 3 - p[0] + 2 * p[1];
516 p[3] = a + p[0] - 3 * p[1] + 3 * p[2];
518 void GetPointsReverse(float p[4]) {
521 p[1] = b / 3 - p[3] + 2 * p[2];
522 p[0] = a + p[3] - 3 * p[2] + 3 * p[1];
524 void BezierInterpol(Coon_BezierCoeff& C1,
525 Coon_BezierCoeff& C2,
526 Coon_BezierCoeff& D1,
527 Coon_BezierCoeff& D2) {
528 a = (D1.a + D2.a) / 2;
529 b = (D1.b + D2.b) / 2;
530 c = (D1.c + D2.c) / 2 - (C1.a / 8 + C1.b / 4 + C1.c / 2) +
531 (C2.a / 8 + C2.b / 4) + (-C1.d + D2.d) / 2 - (C2.a + C2.b) / 2;
532 d = C1.a / 8 + C1.b / 4 + C1.c / 2 + C1.d;
535 float dis = a + b + c;
536 return dis < 0 ? -dis : dis;
540 Coon_BezierCoeff x, y;
541 void FromPoints(float x0,
549 x.FromPoints(x0, x1, x2, x3);
550 y.FromPoints(y0, y1, y2, y3);
552 Coon_Bezier first_half() {
554 result.x = x.first_half();
555 result.y = y.first_half();
558 Coon_Bezier second_half() {
560 result.x = x.second_half();
561 result.y = y.second_half();
564 void BezierInterpol(Coon_Bezier& C1,
568 x.BezierInterpol(C1.x, C2.x, D1.x, D2.x);
569 y.BezierInterpol(C1.y, C2.y, D1.y, D2.y);
571 void GetPoints(FX_PATHPOINT* pPoints) {
575 for (i = 0; i < 4; i++) {
576 pPoints[i].m_PointX = p[i];
579 for (i = 0; i < 4; i++) {
580 pPoints[i].m_PointY = p[i];
583 void GetPointsReverse(FX_PATHPOINT* pPoints) {
586 x.GetPointsReverse(p);
587 for (i = 0; i < 4; i++) {
588 pPoints[i].m_PointX = p[i];
590 y.GetPointsReverse(p);
591 for (i = 0; i < 4; i++) {
592 pPoints[i].m_PointY = p[i];
595 float Distance() { return x.Distance() + y.Distance(); }
597 static int _BiInterpol(int c0,
605 int x1 = c0 + (c3 - c0) * x / x_scale;
606 int x2 = c1 + (c2 - c1) * x / x_scale;
607 return x1 + (x2 - x1) * y / y_scale;
610 Coon_Color() { FXSYS_memset(comp, 0, sizeof(int) * 3); }
612 void BiInterpol(Coon_Color colors[4],
617 for (int i = 0; i < 3; i++)
619 _BiInterpol(colors[0].comp[i], colors[1].comp[i], colors[2].comp[i],
620 colors[3].comp[i], x, y, x_scale, y_scale);
622 int Distance(Coon_Color& o) {
624 max = FXSYS_abs(comp[0] - o.comp[0]);
625 diff = FXSYS_abs(comp[1] - o.comp[1]);
629 diff = FXSYS_abs(comp[2] - o.comp[2]);
636 struct CPDF_PatchDrawer {
637 Coon_Color patch_colors[4];
640 CFX_RenderDevice* pDevice;
643 void Draw(int x_scale,
651 FX_BOOL bSmall = C1.Distance() < 2 && C2.Distance() < 2 &&
652 D1.Distance() < 2 && D2.Distance() < 2;
653 Coon_Color div_colors[4];
654 int d_bottom, d_left, d_top, d_right;
655 div_colors[0].BiInterpol(patch_colors, left, bottom, x_scale, y_scale);
657 div_colors[1].BiInterpol(patch_colors, left, bottom + 1, x_scale,
659 div_colors[2].BiInterpol(patch_colors, left + 1, bottom + 1, x_scale,
661 div_colors[3].BiInterpol(patch_colors, left + 1, bottom, x_scale,
663 d_bottom = div_colors[3].Distance(div_colors[0]);
664 d_left = div_colors[1].Distance(div_colors[0]);
665 d_top = div_colors[1].Distance(div_colors[2]);
666 d_right = div_colors[2].Distance(div_colors[3]);
668 #define COONCOLOR_THRESHOLD 4
670 (d_bottom < COONCOLOR_THRESHOLD && d_left < COONCOLOR_THRESHOLD &&
671 d_top < COONCOLOR_THRESHOLD && d_right < COONCOLOR_THRESHOLD)) {
672 FX_PATHPOINT* pPoints = path.GetPoints();
673 C1.GetPoints(pPoints);
674 D2.GetPoints(pPoints + 3);
675 C2.GetPointsReverse(pPoints + 6);
676 D1.GetPointsReverse(pPoints + 9);
677 int fillFlags = FXFILL_WINDING | FXFILL_FULLCOVER;
678 if (fill_mode & RENDER_NOPATHSMOOTH) {
679 fillFlags |= FXFILL_NOPATHSMOOTH;
683 FXARGB_MAKE(alpha, div_colors[0].comp[0], div_colors[0].comp[1],
684 div_colors[0].comp[2]),
687 if (d_bottom < COONCOLOR_THRESHOLD && d_top < COONCOLOR_THRESHOLD) {
689 m1.BezierInterpol(D1, D2, C1, C2);
692 Draw(x_scale, y_scale, left, bottom, C1, m1, D1.first_half(),
694 Draw(x_scale, y_scale, left, bottom + 1, m1, C2, D1.second_half(),
696 } else if (d_left < COONCOLOR_THRESHOLD &&
697 d_right < COONCOLOR_THRESHOLD) {
699 m2.BezierInterpol(C1, C2, D1, D2);
702 Draw(x_scale, y_scale, left, bottom, C1.first_half(), C2.first_half(),
704 Draw(x_scale, y_scale, left + 1, bottom, C1.second_half(),
705 C2.second_half(), m2, D2);
708 m1.BezierInterpol(D1, D2, C1, C2);
709 m2.BezierInterpol(C1, C2, D1, D2);
710 Coon_Bezier m1f = m1.first_half();
711 Coon_Bezier m1s = m1.second_half();
712 Coon_Bezier m2f = m2.first_half();
713 Coon_Bezier m2s = m2.second_half();
718 Draw(x_scale, y_scale, left, bottom, C1.first_half(), m1f,
719 D1.first_half(), m2f);
720 Draw(x_scale, y_scale, left, bottom + 1, m1f, C2.first_half(),
721 D1.second_half(), m2s);
722 Draw(x_scale, y_scale, left + 1, bottom, C1.second_half(), m1s, m2f,
724 Draw(x_scale, y_scale, left + 1, bottom + 1, m1s, C2.second_half(), m2s,
731 FX_BOOL _CheckCoonTensorPara(const CPDF_MeshStream& stream) {
732 FX_BOOL bCoorBits = (stream.m_nCoordBits == 1 || stream.m_nCoordBits == 2 ||
733 stream.m_nCoordBits == 4 || stream.m_nCoordBits == 8 ||
734 stream.m_nCoordBits == 12 || stream.m_nCoordBits == 16 ||
735 stream.m_nCoordBits == 24 || stream.m_nCoordBits == 32);
737 FX_BOOL bCompBits = (stream.m_nCompBits == 1 || stream.m_nCompBits == 2 ||
738 stream.m_nCompBits == 4 || stream.m_nCompBits == 8 ||
739 stream.m_nCompBits == 12 || stream.m_nCompBits == 16);
741 FX_BOOL bFlagBits = (stream.m_nFlagBits == 2 || stream.m_nFlagBits == 4 ||
742 stream.m_nFlagBits == 8);
744 return bCoorBits && bCompBits && bFlagBits;
747 static void DrawCoonPatchMeshes(FX_BOOL bTensor,
748 CFX_DIBitmap* pBitmap,
749 CFX_AffineMatrix* pObject2Bitmap,
750 CPDF_Stream* pShadingStream,
751 CPDF_Function** pFuncs,
753 CPDF_ColorSpace* pCS,
756 ASSERT(pBitmap->GetFormat() == FXDIB_Argb);
758 CFX_FxgeDevice device;
759 device.Attach(pBitmap);
760 CPDF_MeshStream stream;
761 if (!stream.Load(pShadingStream, pFuncs, nFuncs, pCS))
763 if (!_CheckCoonTensorPara(stream))
766 CPDF_PatchDrawer patch;
768 patch.pDevice = &device;
769 patch.fill_mode = fill_mode;
770 patch.path.SetPointCount(13);
771 FX_PATHPOINT* pPoints = patch.path.GetPoints();
772 pPoints[0].m_Flag = FXPT_MOVETO;
773 for (int i = 1; i < 13; i++) {
774 pPoints[i].m_Flag = FXPT_BEZIERTO;
776 CFX_FloatPoint coords[16];
777 for (int i = 0; i < 16; i++) {
778 coords[i].Set(0.0f, 0.0f);
781 int point_count = bTensor ? 16 : 12;
782 while (!stream.m_BitStream.IsEOF()) {
783 FX_DWORD flag = stream.GetFlag();
784 int iStartPoint = 0, iStartColor = 0, i = 0;
788 CFX_FloatPoint tempCoords[4];
789 for (i = 0; i < 4; i++) {
790 tempCoords[i] = coords[(flag * 3 + i) % 12];
792 FXSYS_memcpy(coords, tempCoords, sizeof(CFX_FloatPoint) * 4);
793 Coon_Color tempColors[2];
794 tempColors[0] = patch.patch_colors[flag];
795 tempColors[1] = patch.patch_colors[(flag + 1) % 4];
796 FXSYS_memcpy(patch.patch_colors, tempColors, sizeof(Coon_Color) * 2);
798 for (i = iStartPoint; i < point_count; i++) {
799 stream.GetCoords(coords[i].x, coords[i].y);
800 pObject2Bitmap->Transform(coords[i].x, coords[i].y);
802 for (i = iStartColor; i < 4; i++) {
803 FX_FLOAT r = 0.0f, g = 0.0f, b = 0.0f;
804 stream.GetColor(r, g, b);
805 patch.patch_colors[i].comp[0] = (int32_t)(r * 255);
806 patch.patch_colors[i].comp[1] = (int32_t)(g * 255);
807 patch.patch_colors[i].comp[2] = (int32_t)(b * 255);
809 CFX_FloatRect bbox = CFX_FloatRect::GetBBox(coords, point_count);
810 if (bbox.right <= 0 || bbox.left >= (FX_FLOAT)pBitmap->GetWidth() ||
811 bbox.top <= 0 || bbox.bottom >= (FX_FLOAT)pBitmap->GetHeight()) {
814 Coon_Bezier C1, C2, D1, D2;
815 C1.FromPoints(coords[0].x, coords[0].y, coords[11].x, coords[11].y,
816 coords[10].x, coords[10].y, coords[9].x, coords[9].y);
817 C2.FromPoints(coords[3].x, coords[3].y, coords[4].x, coords[4].y,
818 coords[5].x, coords[5].y, coords[6].x, coords[6].y);
819 D1.FromPoints(coords[0].x, coords[0].y, coords[1].x, coords[1].y,
820 coords[2].x, coords[2].y, coords[3].x, coords[3].y);
821 D2.FromPoints(coords[9].x, coords[9].y, coords[8].x, coords[8].y,
822 coords[7].x, coords[7].y, coords[6].x, coords[6].y);
823 patch.Draw(1, 1, 0, 0, C1, C2, D1, D2);
826 void CPDF_RenderStatus::DrawShading(CPDF_ShadingPattern* pPattern,
827 CFX_AffineMatrix* pMatrix,
830 FX_BOOL bAlphaMode) {
831 CPDF_Function** pFuncs = pPattern->m_pFunctions;
832 int nFuncs = pPattern->m_nFuncs;
833 CPDF_Dictionary* pDict = pPattern->m_pShadingObj->GetDict();
834 CPDF_ColorSpace* pColorSpace = pPattern->m_pCS;
835 if (pColorSpace == NULL) {
838 FX_ARGB background = 0;
839 if (!pPattern->m_bShadingObj &&
840 pPattern->m_pShadingObj->GetDict()->KeyExist(FX_BSTRC("Background"))) {
841 CPDF_Array* pBackColor =
842 pPattern->m_pShadingObj->GetDict()->GetArray(FX_BSTRC("Background"));
844 pBackColor->GetCount() >= (FX_DWORD)pColorSpace->CountComponents()) {
845 CFX_FixedBufGrow<FX_FLOAT, 16> comps(pColorSpace->CountComponents());
846 for (int i = 0; i < pColorSpace->CountComponents(); i++) {
847 comps[i] = pBackColor->GetNumber(i);
849 FX_FLOAT R = 0.0f, G = 0.0f, B = 0.0f;
850 pColorSpace->GetRGB(comps, R, G, B);
851 background = ArgbEncode(255, (int32_t)(R * 255), (int32_t)(G * 255),
855 if (pDict->KeyExist(FX_BSTRC("BBox"))) {
856 CFX_FloatRect rect = pDict->GetRect(FX_BSTRC("BBox"));
857 rect.Transform(pMatrix);
858 clip_rect.Intersect(rect.GetOutterRect());
860 CPDF_DeviceBuffer buffer;
861 buffer.Initialize(m_pContext, m_pDevice, &clip_rect, m_pCurObj, 150);
862 CFX_AffineMatrix FinalMatrix = *pMatrix;
863 FinalMatrix.Concat(*buffer.GetMatrix());
864 CFX_DIBitmap* pBitmap = buffer.GetBitmap();
865 if (pBitmap->GetBuffer() == NULL) {
868 pBitmap->Clear(background);
869 int fill_mode = m_Options.m_Flags;
870 switch (pPattern->m_ShadingType) {
871 case kInvalidShading:
874 case kFunctionBasedShading:
875 DrawFuncShading(pBitmap, &FinalMatrix, pDict, pFuncs, nFuncs, pColorSpace,
879 DrawAxialShading(pBitmap, &FinalMatrix, pDict, pFuncs, nFuncs,
883 DrawRadialShading(pBitmap, &FinalMatrix, pDict, pFuncs, nFuncs,
886 case kFreeFormGouraudTriangleMeshShading: {
887 DrawFreeGouraudShading(pBitmap, &FinalMatrix,
888 ToStream(pPattern->m_pShadingObj), pFuncs, nFuncs,
891 case kLatticeFormGouraudTriangleMeshShading: {
892 DrawLatticeGouraudShading(pBitmap, &FinalMatrix,
893 ToStream(pPattern->m_pShadingObj), pFuncs,
894 nFuncs, pColorSpace, alpha);
896 case kCoonsPatchMeshShading:
897 case kTensorProductPatchMeshShading: {
899 pPattern->m_ShadingType == kTensorProductPatchMeshShading, pBitmap,
900 &FinalMatrix, ToStream(pPattern->m_pShadingObj), pFuncs, nFuncs,
901 pColorSpace, fill_mode, alpha);
905 pBitmap->LoadChannel(FXDIB_Red, pBitmap, FXDIB_Alpha);
907 if (m_Options.m_ColorMode == RENDER_COLOR_GRAY) {
908 pBitmap->ConvertColorScale(m_Options.m_ForeColor, m_Options.m_BackColor);
910 buffer.OutputToDevice();
912 void CPDF_RenderStatus::DrawShadingPattern(CPDF_ShadingPattern* pattern,
913 CPDF_PageObject* pPageObj,
914 const CFX_AffineMatrix* pObj2Device,
916 if (!pattern->Load()) {
919 m_pDevice->SaveState();
920 if (pPageObj->m_Type == PDFPAGE_PATH) {
921 if (!SelectClipPath((CPDF_PathObject*)pPageObj, pObj2Device, bStroke)) {
922 m_pDevice->RestoreState();
925 } else if (pPageObj->m_Type == PDFPAGE_IMAGE) {
926 FX_RECT rect = pPageObj->GetBBox(pObj2Device);
927 m_pDevice->SetClip_Rect(&rect);
932 if (GetObjectClippedRect(pPageObj, pObj2Device, FALSE, rect)) {
933 m_pDevice->RestoreState();
936 CFX_AffineMatrix matrix = pattern->m_Pattern2Form;
937 matrix.Concat(*pObj2Device);
938 GetScaledMatrix(matrix);
939 int alpha = pPageObj->m_GeneralState.GetAlpha(bStroke);
940 DrawShading(pattern, &matrix, rect, alpha,
941 m_Options.m_ColorMode == RENDER_COLOR_ALPHA);
942 m_pDevice->RestoreState();
944 FX_BOOL CPDF_RenderStatus::ProcessShading(CPDF_ShadingObject* pShadingObj,
945 const CFX_AffineMatrix* pObj2Device) {
946 FX_RECT rect = pShadingObj->GetBBox(pObj2Device);
947 FX_RECT clip_box = m_pDevice->GetClipBox();
948 rect.Intersect(clip_box);
949 if (rect.IsEmpty()) {
952 CFX_AffineMatrix matrix = pShadingObj->m_Matrix;
953 matrix.Concat(*pObj2Device);
954 DrawShading(pShadingObj->m_pShading, &matrix, rect,
955 pShadingObj->m_GeneralState.GetAlpha(FALSE),
956 m_Options.m_ColorMode == RENDER_COLOR_ALPHA);
959 static CFX_DIBitmap* DrawPatternBitmap(CPDF_Document* pDoc,
960 CPDF_PageRenderCache* pCache,
961 CPDF_TilingPattern* pPattern,
962 const CFX_AffineMatrix* pObject2Device,
966 CFX_DIBitmap* pBitmap = new CFX_DIBitmap;
967 if (!pBitmap->Create(width, height,
968 pPattern->m_bColored ? FXDIB_Argb : FXDIB_8bppMask)) {
972 CFX_FxgeDevice bitmap_device;
973 bitmap_device.Attach(pBitmap);
975 CFX_FloatRect cell_bbox = pPattern->m_BBox;
976 pPattern->m_Pattern2Form.TransformRect(cell_bbox);
977 pObject2Device->TransformRect(cell_bbox);
978 CFX_FloatRect bitmap_rect(0.0f, 0.0f, (FX_FLOAT)width, (FX_FLOAT)height);
979 CFX_AffineMatrix mtAdjust;
980 mtAdjust.MatchRect(bitmap_rect, cell_bbox);
981 CFX_AffineMatrix mtPattern2Bitmap = *pObject2Device;
982 mtPattern2Bitmap.Concat(mtAdjust);
983 CPDF_RenderOptions options;
984 if (!pPattern->m_bColored) {
985 options.m_ColorMode = RENDER_COLOR_ALPHA;
987 flags |= RENDER_FORCE_HALFTONE;
988 options.m_Flags = flags;
989 CPDF_RenderContext context;
990 context.Create(pDoc, pCache, NULL);
991 context.DrawObjectList(&bitmap_device, pPattern->m_pForm, &mtPattern2Bitmap,
995 void CPDF_RenderStatus::DrawTilingPattern(CPDF_TilingPattern* pPattern,
996 CPDF_PageObject* pPageObj,
997 const CFX_AffineMatrix* pObj2Device,
999 if (!pPattern->Load()) {
1002 m_pDevice->SaveState();
1003 if (pPageObj->m_Type == PDFPAGE_PATH) {
1004 if (!SelectClipPath((CPDF_PathObject*)pPageObj, pObj2Device, bStroke)) {
1005 m_pDevice->RestoreState();
1008 } else if (pPageObj->m_Type == PDFPAGE_IMAGE) {
1009 FX_RECT rect = pPageObj->GetBBox(pObj2Device);
1010 m_pDevice->SetClip_Rect(&rect);
1014 FX_RECT clip_box = m_pDevice->GetClipBox();
1015 if (clip_box.IsEmpty()) {
1016 m_pDevice->RestoreState();
1019 CFX_Matrix dCTM = m_pDevice->GetCTM();
1020 FX_FLOAT sa = FXSYS_fabs(dCTM.a);
1021 FX_FLOAT sd = FXSYS_fabs(dCTM.d);
1022 clip_box.right = clip_box.left + (int32_t)FXSYS_ceil(clip_box.Width() * sa);
1023 clip_box.bottom = clip_box.top + (int32_t)FXSYS_ceil(clip_box.Height() * sd);
1024 CFX_AffineMatrix mtPattern2Device = pPattern->m_Pattern2Form;
1025 mtPattern2Device.Concat(*pObj2Device);
1026 GetScaledMatrix(mtPattern2Device);
1027 FX_BOOL bAligned = FALSE;
1028 if (pPattern->m_BBox.left == 0 && pPattern->m_BBox.bottom == 0 &&
1029 pPattern->m_BBox.right == pPattern->m_XStep &&
1030 pPattern->m_BBox.top == pPattern->m_YStep &&
1031 (mtPattern2Device.IsScaled() || mtPattern2Device.Is90Rotated())) {
1034 CFX_FloatRect cell_bbox = pPattern->m_BBox;
1035 mtPattern2Device.TransformRect(cell_bbox);
1036 int width = (int)FXSYS_ceil(cell_bbox.Width());
1037 int height = (int)FXSYS_ceil(cell_bbox.Height());
1044 int min_col, max_col, min_row, max_row;
1045 CFX_AffineMatrix mtDevice2Pattern;
1046 mtDevice2Pattern.SetReverse(mtPattern2Device);
1047 CFX_FloatRect clip_box_p(clip_box);
1048 clip_box_p.Transform(&mtDevice2Pattern);
1049 min_col = (int)FXSYS_ceil(
1050 FXSYS_Div(clip_box_p.left - pPattern->m_BBox.right, pPattern->m_XStep));
1051 max_col = (int)FXSYS_floor(
1052 FXSYS_Div(clip_box_p.right - pPattern->m_BBox.left, pPattern->m_XStep));
1053 min_row = (int)FXSYS_ceil(
1054 FXSYS_Div(clip_box_p.bottom - pPattern->m_BBox.top, pPattern->m_YStep));
1055 max_row = (int)FXSYS_floor(
1056 FXSYS_Div(clip_box_p.top - pPattern->m_BBox.bottom, pPattern->m_YStep));
1057 if (width > clip_box.Width() || height > clip_box.Height() ||
1058 width * height > clip_box.Width() * clip_box.Height()) {
1059 CPDF_GraphicStates* pStates = NULL;
1060 if (!pPattern->m_bColored) {
1061 pStates = CloneObjStates(pPageObj, bStroke);
1063 CPDF_Dictionary* pFormResource = NULL;
1064 if (pPattern->m_pForm->m_pFormDict) {
1066 pPattern->m_pForm->m_pFormDict->GetDict(FX_BSTRC("Resources"));
1068 for (int col = min_col; col <= max_col; col++)
1069 for (int row = min_row; row <= max_row; row++) {
1070 FX_FLOAT orig_x, orig_y;
1071 orig_x = col * pPattern->m_XStep;
1072 orig_y = row * pPattern->m_YStep;
1073 mtPattern2Device.Transform(orig_x, orig_y);
1074 CFX_AffineMatrix matrix = *pObj2Device;
1075 matrix.Translate(orig_x - mtPattern2Device.e,
1076 orig_y - mtPattern2Device.f);
1077 m_pDevice->SaveState();
1078 CPDF_RenderStatus status;
1079 status.Initialize(m_pContext, m_pDevice, NULL, NULL, this, pStates,
1080 &m_Options, pPattern->m_pForm->m_Transparency,
1081 m_bDropObjects, pFormResource);
1082 status.RenderObjectList(pPattern->m_pForm, &matrix);
1083 m_pDevice->RestoreState();
1085 m_pDevice->RestoreState();
1090 int orig_x = FXSYS_round(mtPattern2Device.e);
1091 int orig_y = FXSYS_round(mtPattern2Device.f);
1092 min_col = (clip_box.left - orig_x) / width;
1093 if (clip_box.left < orig_x) {
1096 max_col = (clip_box.right - orig_x) / width;
1097 if (clip_box.right <= orig_x) {
1100 min_row = (clip_box.top - orig_y) / height;
1101 if (clip_box.top < orig_y) {
1104 max_row = (clip_box.bottom - orig_y) / height;
1105 if (clip_box.bottom <= orig_y) {
1109 FX_FLOAT left_offset = cell_bbox.left - mtPattern2Device.e;
1110 FX_FLOAT top_offset = cell_bbox.bottom - mtPattern2Device.f;
1111 CFX_DIBitmap* pPatternBitmap = NULL;
1112 if (width * height < 16) {
1113 CFX_DIBitmap* pEnlargedBitmap =
1114 DrawPatternBitmap(m_pContext->m_pDocument, m_pContext->m_pPageCache,
1115 pPattern, pObj2Device, 8, 8, m_Options.m_Flags);
1116 pPatternBitmap = pEnlargedBitmap->StretchTo(width, height);
1117 delete pEnlargedBitmap;
1119 pPatternBitmap = DrawPatternBitmap(
1120 m_pContext->m_pDocument, m_pContext->m_pPageCache, pPattern,
1121 pObj2Device, width, height, m_Options.m_Flags);
1123 if (pPatternBitmap == NULL) {
1124 m_pDevice->RestoreState();
1127 if (m_Options.m_ColorMode == RENDER_COLOR_GRAY) {
1128 pPatternBitmap->ConvertColorScale(m_Options.m_ForeColor,
1129 m_Options.m_BackColor);
1131 FX_ARGB fill_argb = GetFillArgb(pPageObj);
1132 int clip_width = clip_box.right - clip_box.left;
1133 int clip_height = clip_box.bottom - clip_box.top;
1134 CFX_DIBitmap screen;
1135 if (!screen.Create(clip_width, clip_height, FXDIB_Argb)) {
1139 FX_DWORD* src_buf = (FX_DWORD*)pPatternBitmap->GetBuffer();
1140 for (int col = min_col; col <= max_col; col++) {
1141 for (int row = min_row; row <= max_row; row++) {
1142 int start_x, start_y;
1144 start_x = FXSYS_round(mtPattern2Device.e) + col * width - clip_box.left;
1145 start_y = FXSYS_round(mtPattern2Device.f) + row * height - clip_box.top;
1147 FX_FLOAT orig_x = col * pPattern->m_XStep;
1148 FX_FLOAT orig_y = row * pPattern->m_YStep;
1149 mtPattern2Device.Transform(orig_x, orig_y);
1150 start_x = FXSYS_round(orig_x + left_offset) - clip_box.left;
1151 start_y = FXSYS_round(orig_y + top_offset) - clip_box.top;
1153 if (width == 1 && height == 1) {
1154 if (start_x < 0 || start_x >= clip_box.Width() || start_y < 0 ||
1155 start_y >= clip_box.Height()) {
1158 FX_DWORD* dest_buf =
1159 (FX_DWORD*)(screen.GetBuffer() + screen.GetPitch() * start_y +
1161 if (pPattern->m_bColored) {
1162 *dest_buf = *src_buf;
1164 *dest_buf = (*(uint8_t*)src_buf << 24) | (fill_argb & 0xffffff);
1167 if (pPattern->m_bColored) {
1168 screen.CompositeBitmap(start_x, start_y, width, height,
1169 pPatternBitmap, 0, 0);
1171 screen.CompositeMask(start_x, start_y, width, height, pPatternBitmap,
1177 CompositeDIBitmap(&screen, clip_box.left, clip_box.top, 0, 255,
1178 FXDIB_BLEND_NORMAL, FALSE);
1179 m_pDevice->RestoreState();
1180 delete pPatternBitmap;
1182 void CPDF_RenderStatus::DrawPathWithPattern(CPDF_PathObject* pPathObj,
1183 const CFX_AffineMatrix* pObj2Device,
1186 CPDF_Pattern* pattern = pColor->GetPattern();
1187 if (pattern == NULL) {
1190 if (pattern->m_PatternType == PATTERN_TILING) {
1191 DrawTilingPattern((CPDF_TilingPattern*)pattern, pPathObj, pObj2Device,
1194 DrawShadingPattern((CPDF_ShadingPattern*)pattern, pPathObj, pObj2Device,
1198 void CPDF_RenderStatus::ProcessPathPattern(CPDF_PathObject* pPathObj,
1199 const CFX_AffineMatrix* pObj2Device,
1203 CPDF_Color& FillColor = *pPathObj->m_ColorState.GetFillColor();
1204 if (FillColor.m_pCS && FillColor.m_pCS->GetFamily() == PDFCS_PATTERN) {
1205 DrawPathWithPattern(pPathObj, pObj2Device, &FillColor, FALSE);
1210 CPDF_Color& StrokeColor = *pPathObj->m_ColorState.GetStrokeColor();
1211 if (StrokeColor.m_pCS && StrokeColor.m_pCS->GetFamily() == PDFCS_PATTERN) {
1212 DrawPathWithPattern(pPathObj, pObj2Device, &StrokeColor, TRUE);