1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
|
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
#include "core/include/fxge/fx_ge.h"
#if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN64_DESKTOP_
#include <windows.h>
#include "core/fxge/dib/dib_int.h"
#include "core/fxge/ge/fx_text_int.h"
#include "core/fxge/win32/win32_int.h"
#include "core/include/fxge/fx_freetype.h"
#include "core/include/fxge/fx_ge_win32.h"
#define SIZETHRESHOLD 1000
#define OUTPUTPSLEN 4096
CGdiPrinterDriver::CGdiPrinterDriver(HDC hDC)
: CGdiDeviceDriver(hDC, FXDC_PRINTER) {
m_HorzSize = ::GetDeviceCaps(m_hDC, HORZSIZE);
m_VertSize = ::GetDeviceCaps(m_hDC, VERTSIZE);
m_bSupportROP = TRUE;
}
int CGdiPrinterDriver::GetDeviceCaps(int caps_id) {
if (caps_id == FXDC_HORZ_SIZE) {
return m_HorzSize;
}
if (caps_id == FXDC_VERT_SIZE) {
return m_VertSize;
}
return CGdiDeviceDriver::GetDeviceCaps(caps_id);
}
FX_BOOL CGdiPrinterDriver::SetDIBits(const CFX_DIBSource* pSource,
uint32_t color,
const FX_RECT* pSrcRect,
int left,
int top,
int blend_type,
int alpha_flag,
void* pIccTransform) {
if (pSource->IsAlphaMask()) {
FX_RECT clip_rect(left, top, left + pSrcRect->Width(),
top + pSrcRect->Height());
return StretchDIBits(pSource, color, left - pSrcRect->left,
top - pSrcRect->top, pSource->GetWidth(),
pSource->GetHeight(), &clip_rect, 0, alpha_flag,
pIccTransform, FXDIB_BLEND_NORMAL);
}
ASSERT(pSource && !pSource->IsAlphaMask() && pSrcRect);
ASSERT(blend_type == FXDIB_BLEND_NORMAL);
if (pSource->HasAlpha()) {
return FALSE;
}
CFX_DIBExtractor temp(pSource);
CFX_DIBitmap* pBitmap = temp;
if (!pBitmap) {
return FALSE;
}
return GDI_SetDIBits(pBitmap, pSrcRect, left, top, pIccTransform);
}
FX_BOOL CGdiPrinterDriver::StretchDIBits(const CFX_DIBSource* pSource,
uint32_t color,
int dest_left,
int dest_top,
int dest_width,
int dest_height,
const FX_RECT* pClipRect,
uint32_t flags,
int alpha_flag,
void* pIccTransform,
int blend_type) {
if (pSource->IsAlphaMask()) {
int alpha = FXGETFLAG_COLORTYPE(alpha_flag)
? FXGETFLAG_ALPHA_FILL(alpha_flag)
: FXARGB_A(color);
if (pSource->GetBPP() != 1 || alpha != 255 || !m_bSupportROP) {
return FALSE;
}
if (dest_width < 0 || dest_height < 0) {
CFX_DIBitmap* pFlipped =
pSource->FlipImage(dest_width < 0, dest_height < 0);
if (!pFlipped) {
return FALSE;
}
if (dest_width < 0) {
dest_left += dest_width;
}
if (dest_height < 0) {
dest_top += dest_height;
}
FX_BOOL ret = GDI_StretchBitMask(pFlipped, dest_left, dest_top,
abs(dest_width), abs(dest_height), color,
flags, alpha_flag, pIccTransform);
delete pFlipped;
return ret;
}
CFX_DIBExtractor temp(pSource);
CFX_DIBitmap* pBitmap = temp;
if (!pBitmap) {
return FALSE;
}
return GDI_StretchBitMask(pBitmap, dest_left, dest_top, dest_width,
dest_height, color, flags, alpha_flag,
pIccTransform);
}
if (pSource->HasAlpha()) {
return FALSE;
}
if (dest_width < 0 || dest_height < 0) {
CFX_DIBitmap* pFlipped =
pSource->FlipImage(dest_width < 0, dest_height < 0);
if (!pFlipped) {
return FALSE;
}
if (dest_width < 0) {
dest_left += dest_width;
}
if (dest_height < 0) {
dest_top += dest_height;
}
FX_BOOL ret =
GDI_StretchDIBits(pFlipped, dest_left, dest_top, abs(dest_width),
abs(dest_height), flags, pIccTransform);
delete pFlipped;
return ret;
}
CFX_DIBExtractor temp(pSource);
CFX_DIBitmap* pBitmap = temp;
if (!pBitmap) {
return FALSE;
}
return GDI_StretchDIBits(pBitmap, dest_left, dest_top, dest_width,
dest_height, flags, pIccTransform);
}
static CFX_DIBitmap* Transform1bppBitmap(const CFX_DIBSource* pSrc,
const CFX_Matrix* pDestMatrix) {
ASSERT(pSrc->GetFormat() == FXDIB_1bppRgb ||
pSrc->GetFormat() == FXDIB_1bppMask ||
pSrc->GetFormat() == FXDIB_1bppCmyk);
CFX_DIBExtractor src_bitmap(pSrc);
CFX_DIBitmap* pSrcBitmap = src_bitmap;
if (!pSrcBitmap) {
return NULL;
}
int src_width = pSrcBitmap->GetWidth(), src_height = pSrcBitmap->GetHeight();
uint8_t* src_buf = pSrcBitmap->GetBuffer();
uint32_t src_pitch = pSrcBitmap->GetPitch();
FX_FLOAT dest_area = pDestMatrix->GetUnitArea();
FX_FLOAT area_scale = ((FX_FLOAT)(src_width * src_height)) / dest_area;
FX_FLOAT size_scale = FXSYS_sqrt(area_scale);
CFX_Matrix adjusted_matrix(*pDestMatrix);
adjusted_matrix.Scale(size_scale, size_scale);
CFX_FloatRect result_rect_f = adjusted_matrix.GetUnitRect();
FX_RECT result_rect = result_rect_f.GetOutterRect();
CFX_Matrix src2result;
src2result.e = adjusted_matrix.c + adjusted_matrix.e;
src2result.f = adjusted_matrix.d + adjusted_matrix.f;
src2result.a = adjusted_matrix.a / pSrcBitmap->GetWidth();
src2result.b = adjusted_matrix.b / pSrcBitmap->GetWidth();
src2result.c = -adjusted_matrix.c / pSrcBitmap->GetHeight();
src2result.d = -adjusted_matrix.d / pSrcBitmap->GetHeight();
src2result.TranslateI(-result_rect.left, -result_rect.top);
CFX_Matrix result2src;
result2src.SetReverse(src2result);
CPDF_FixedMatrix result2src_fix(result2src, 8);
int result_width = result_rect.Width();
int result_height = result_rect.Height();
CFX_DIBitmap* pTempBitmap = new CFX_DIBitmap;
if (!pTempBitmap->Create(result_width, result_height, pSrc->GetFormat())) {
delete pTempBitmap;
if (pSrcBitmap != src_bitmap) {
delete pSrcBitmap;
}
return NULL;
}
pTempBitmap->CopyPalette(pSrc->GetPalette());
uint8_t* dest_buf = pTempBitmap->GetBuffer();
int dest_pitch = pTempBitmap->GetPitch();
FXSYS_memset(dest_buf, pSrc->IsAlphaMask() ? 0 : 0xff,
dest_pitch * result_height);
if (pSrcBitmap->IsAlphaMask()) {
for (int dest_y = 0; dest_y < result_height; dest_y++) {
uint8_t* dest_scan = dest_buf + dest_y * dest_pitch;
for (int dest_x = 0; dest_x < result_width; dest_x++) {
int src_x, src_y;
result2src_fix.Transform(dest_x, dest_y, src_x, src_y);
if (src_x < 0 || src_x >= src_width || src_y < 0 ||
src_y >= src_height) {
continue;
}
if (!((src_buf + src_pitch * src_y)[src_x / 8] &
(1 << (7 - src_x % 8)))) {
continue;
}
dest_scan[dest_x / 8] |= 1 << (7 - dest_x % 8);
}
}
} else {
for (int dest_y = 0; dest_y < result_height; dest_y++) {
uint8_t* dest_scan = dest_buf + dest_y * dest_pitch;
for (int dest_x = 0; dest_x < result_width; dest_x++) {
int src_x, src_y;
result2src_fix.Transform(dest_x, dest_y, src_x, src_y);
if (src_x < 0 || src_x >= src_width || src_y < 0 ||
src_y >= src_height) {
continue;
}
if ((src_buf + src_pitch * src_y)[src_x / 8] & (1 << (7 - src_x % 8))) {
continue;
}
dest_scan[dest_x / 8] &= ~(1 << (7 - dest_x % 8));
}
}
}
if (pSrcBitmap != src_bitmap) {
delete pSrcBitmap;
}
return pTempBitmap;
}
FX_BOOL CGdiPrinterDriver::StartDIBits(const CFX_DIBSource* pSource,
int bitmap_alpha,
uint32_t color,
const CFX_Matrix* pMatrix,
uint32_t render_flags,
void*& handle,
int alpha_flag,
void* pIccTransform,
int blend_type) {
if (bitmap_alpha < 255 || pSource->HasAlpha() ||
(pSource->IsAlphaMask() && (pSource->GetBPP() != 1 || !m_bSupportROP))) {
return FALSE;
}
CFX_FloatRect unit_rect = pMatrix->GetUnitRect();
FX_RECT full_rect = unit_rect.GetOutterRect();
if (FXSYS_fabs(pMatrix->b) < 0.5f && pMatrix->a != 0 &&
FXSYS_fabs(pMatrix->c) < 0.5f && pMatrix->d != 0) {
FX_BOOL bFlipX = pMatrix->a < 0;
FX_BOOL bFlipY = pMatrix->d > 0;
return StretchDIBits(pSource, color,
bFlipX ? full_rect.right : full_rect.left,
bFlipY ? full_rect.bottom : full_rect.top,
bFlipX ? -full_rect.Width() : full_rect.Width(),
bFlipY ? -full_rect.Height() : full_rect.Height(),
NULL, 0, alpha_flag, pIccTransform, blend_type);
}
if (FXSYS_fabs(pMatrix->a) < 0.5f && FXSYS_fabs(pMatrix->d) < 0.5f) {
CFX_DIBitmap* pTransformed =
pSource->SwapXY(pMatrix->c > 0, pMatrix->b < 0);
if (!pTransformed) {
return FALSE;
}
FX_BOOL ret = StretchDIBits(
pTransformed, color, full_rect.left, full_rect.top, full_rect.Width(),
full_rect.Height(), NULL, 0, alpha_flag, pIccTransform, blend_type);
delete pTransformed;
return ret;
}
if (pSource->GetBPP() == 1) {
CFX_DIBitmap* pTransformed = Transform1bppBitmap(pSource, pMatrix);
if (!pIccTransform) {
return FALSE;
}
SaveState();
CFX_PathData path;
path.AppendRect(0, 0, 1.0f, 1.0f);
SetClip_PathFill(&path, pMatrix, WINDING);
FX_BOOL ret = StretchDIBits(
pTransformed, color, full_rect.left, full_rect.top, full_rect.Width(),
full_rect.Height(), NULL, 0, alpha_flag, pIccTransform, blend_type);
RestoreState();
delete pTransformed;
handle = NULL;
return ret;
}
return FALSE;
}
CPSOutput::CPSOutput(HDC hDC) {
m_hDC = hDC;
m_pBuf = NULL;
}
CPSOutput::~CPSOutput() {
FX_Free(m_pBuf);
}
void CPSOutput::Init() {
m_pBuf = FX_Alloc(FX_CHAR, 1026);
}
void CPSOutput::OutputPS(const FX_CHAR* str, int len) {
if (len < 0) {
len = (int)FXSYS_strlen(str);
}
int sent_len = 0;
while (len > 0) {
int send_len = len > 1024 ? 1024 : len;
*(uint16_t*)m_pBuf = send_len;
FXSYS_memcpy(m_pBuf + 2, str + sent_len, send_len);
ExtEscape(m_hDC, PASSTHROUGH, send_len + 2, m_pBuf, 0, NULL);
sent_len += send_len;
len -= send_len;
}
}
CPSPrinterDriver::CPSPrinterDriver() {
m_pPSOutput = NULL;
m_bCmykOutput = FALSE;
}
CPSPrinterDriver::~CPSPrinterDriver() {
EndRendering();
delete m_pPSOutput;
}
FX_BOOL CPSPrinterDriver::Init(HDC hDC, int pslevel, FX_BOOL bCmykOutput) {
m_hDC = hDC;
m_HorzSize = ::GetDeviceCaps(m_hDC, HORZSIZE);
m_VertSize = ::GetDeviceCaps(m_hDC, VERTSIZE);
m_Width = ::GetDeviceCaps(m_hDC, HORZRES);
m_Height = ::GetDeviceCaps(m_hDC, VERTRES);
m_nBitsPerPixel = ::GetDeviceCaps(m_hDC, BITSPIXEL);
m_pPSOutput = new CPSOutput(hDC);
((CPSOutput*)m_pPSOutput)->Init();
m_PSRenderer.Init(m_pPSOutput, pslevel, m_Width, m_Height, bCmykOutput);
m_bCmykOutput = bCmykOutput;
HRGN hRgn = ::CreateRectRgn(0, 0, 1, 1);
int ret = ::GetClipRgn(hDC, hRgn);
if (ret == 1) {
ret = ::GetRegionData(hRgn, 0, NULL);
if (ret) {
RGNDATA* pData = (RGNDATA*)FX_Alloc(uint8_t, ret);
ret = ::GetRegionData(hRgn, ret, pData);
if (ret) {
CFX_PathData path;
path.AllocPointCount(pData->rdh.nCount * 5);
for (uint32_t i = 0; i < pData->rdh.nCount; i++) {
RECT* pRect = (RECT*)(pData->Buffer + pData->rdh.nRgnSize * i);
path.AppendRect((FX_FLOAT)pRect->left, (FX_FLOAT)pRect->bottom,
(FX_FLOAT)pRect->right, (FX_FLOAT)pRect->top);
}
m_PSRenderer.SetClip_PathFill(&path, NULL, FXFILL_WINDING);
}
FX_Free(pData);
}
}
::DeleteObject(hRgn);
return TRUE;
}
int CPSPrinterDriver::GetDeviceCaps(int caps_id) {
switch (caps_id) {
case FXDC_DEVICE_CLASS:
return FXDC_PRINTER;
case FXDC_PIXEL_WIDTH:
return m_Width;
case FXDC_PIXEL_HEIGHT:
return m_Height;
case FXDC_BITS_PIXEL:
return m_nBitsPerPixel;
case FXDC_RENDER_CAPS:
return m_bCmykOutput ? FXRC_BIT_MASK | FXRC_CMYK_OUTPUT : FXRC_BIT_MASK;
case FXDC_HORZ_SIZE:
return m_HorzSize;
case FXDC_VERT_SIZE:
return m_VertSize;
}
return 0;
}
FX_BOOL CPSPrinterDriver::StartRendering() {
return m_PSRenderer.StartRendering();
}
void CPSPrinterDriver::EndRendering() {
m_PSRenderer.EndRendering();
}
void CPSPrinterDriver::SaveState() {
m_PSRenderer.SaveState();
}
void CPSPrinterDriver::RestoreState(FX_BOOL bKeepSaved) {
m_PSRenderer.RestoreState(bKeepSaved);
}
FX_BOOL CPSPrinterDriver::SetClip_PathFill(const CFX_PathData* pPathData,
const CFX_Matrix* pObject2Device,
int fill_mode) {
m_PSRenderer.SetClip_PathFill(pPathData, pObject2Device, fill_mode);
return TRUE;
}
FX_BOOL CPSPrinterDriver::SetClip_PathStroke(
const CFX_PathData* pPathData,
const CFX_Matrix* pObject2Device,
const CFX_GraphStateData* pGraphState) {
m_PSRenderer.SetClip_PathStroke(pPathData, pObject2Device, pGraphState);
return TRUE;
}
FX_BOOL CPSPrinterDriver::DrawPath(const CFX_PathData* pPathData,
const CFX_Matrix* pObject2Device,
const CFX_GraphStateData* pGraphState,
FX_ARGB fill_color,
FX_ARGB stroke_color,
int fill_mode,
int alpha_flag,
void* pIccTransform,
int blend_type) {
if (blend_type != FXDIB_BLEND_NORMAL) {
return FALSE;
}
return m_PSRenderer.DrawPath(pPathData, pObject2Device, pGraphState,
fill_color, stroke_color, fill_mode & 3,
alpha_flag, pIccTransform);
}
FX_BOOL CPSPrinterDriver::GetClipBox(FX_RECT* pRect) {
*pRect = m_PSRenderer.GetClipBox();
return TRUE;
}
FX_BOOL CPSPrinterDriver::SetDIBits(const CFX_DIBSource* pBitmap,
uint32_t color,
const FX_RECT* pSrcRect,
int left,
int top,
int blend_type,
int alpha_flag,
void* pIccTransform) {
if (blend_type != FXDIB_BLEND_NORMAL) {
return FALSE;
}
return m_PSRenderer.SetDIBits(pBitmap, color, left, top, alpha_flag,
pIccTransform);
}
FX_BOOL CPSPrinterDriver::StretchDIBits(const CFX_DIBSource* pBitmap,
uint32_t color,
int dest_left,
int dest_top,
int dest_width,
int dest_height,
const FX_RECT* pClipRect,
uint32_t flags,
int alpha_flag,
void* pIccTransform,
int blend_type) {
if (blend_type != FXDIB_BLEND_NORMAL) {
return FALSE;
}
return m_PSRenderer.StretchDIBits(pBitmap, color, dest_left, dest_top,
dest_width, dest_height, flags, alpha_flag,
pIccTransform);
}
FX_BOOL CPSPrinterDriver::StartDIBits(const CFX_DIBSource* pBitmap,
int bitmap_alpha,
uint32_t color,
const CFX_Matrix* pMatrix,
uint32_t render_flags,
void*& handle,
int alpha_flag,
void* pIccTransform,
int blend_type) {
if (blend_type != FXDIB_BLEND_NORMAL) {
return FALSE;
}
if (bitmap_alpha < 255) {
return FALSE;
}
handle = NULL;
return m_PSRenderer.DrawDIBits(pBitmap, color, pMatrix, render_flags,
alpha_flag, pIccTransform);
}
FX_BOOL CPSPrinterDriver::DrawDeviceText(int nChars,
const FXTEXT_CHARPOS* pCharPos,
CFX_Font* pFont,
CFX_FontCache* pCache,
const CFX_Matrix* pObject2Device,
FX_FLOAT font_size,
uint32_t color,
int alpha_flag,
void* pIccTransform) {
return m_PSRenderer.DrawText(nChars, pCharPos, pFont, pCache, pObject2Device,
font_size, color, alpha_flag, pIccTransform);
}
#endif
|