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