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