Don't bother checking pointers before delete[] and FX_Free().
[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   int full_left = full_rect.left;
142   int full_top = full_rect.top;
143   CFX_DIBExtractor src_bitmap(pSrc);
144   CFX_DIBitmap* pSrcBitmap = src_bitmap;
145   if (pSrcBitmap == NULL) {
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_AffineMatrix 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_AffineMatrix 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_AffineMatrix 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 }
224 FX_BOOL CGdiPrinterDriver::StartDIBits(const CFX_DIBSource* pSource,
225                                        int bitmap_alpha,
226                                        FX_DWORD color,
227                                        const CFX_AffineMatrix* 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 == NULL) {
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 == NULL) {
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 }
281 CPSOutput::CPSOutput(HDC hDC) {
282   m_hDC = hDC;
283   m_pBuf = NULL;
284 }
285 CPSOutput::~CPSOutput() {
286   FX_Free(m_pBuf);
287 }
288 void CPSOutput::Init() {
289   m_pBuf = FX_Alloc(FX_CHAR, 1026);
290 }
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     int ret = ExtEscape(m_hDC, PASSTHROUGH, send_len + 2, m_pBuf, 0, NULL);
301     sent_len += send_len;
302     len -= send_len;
303   }
304 }
305 CPSPrinterDriver::CPSPrinterDriver() {
306   m_pPSOutput = NULL;
307   m_bCmykOutput = FALSE;
308 }
309 CPSPrinterDriver::~CPSPrinterDriver() {
310   EndRendering();
311   delete m_pPSOutput;
312 }
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 }
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 }
366 FX_BOOL CPSPrinterDriver::StartRendering() {
367   return m_PSRenderer.StartRendering();
368 }
369 void CPSPrinterDriver::EndRendering() {
370   m_PSRenderer.EndRendering();
371 }
372 void CPSPrinterDriver::SaveState() {
373   m_PSRenderer.SaveState();
374 }
375 void CPSPrinterDriver::RestoreState(FX_BOOL bKeepSaved) {
376   m_PSRenderer.RestoreState(bKeepSaved);
377 }
378 FX_BOOL CPSPrinterDriver::SetClip_PathFill(
379     const CFX_PathData* pPathData,
380     const CFX_AffineMatrix* pObject2Device,
381     int fill_mode) {
382   m_PSRenderer.SetClip_PathFill(pPathData, pObject2Device, fill_mode);
383   return TRUE;
384 }
385 FX_BOOL CPSPrinterDriver::SetClip_PathStroke(
386     const CFX_PathData* pPathData,
387     const CFX_AffineMatrix* pObject2Device,
388     const CFX_GraphStateData* pGraphState) {
389   m_PSRenderer.SetClip_PathStroke(pPathData, pObject2Device, pGraphState);
390   return TRUE;
391 }
392 FX_BOOL CPSPrinterDriver::DrawPath(const CFX_PathData* pPathData,
393                                    const CFX_AffineMatrix* pObject2Device,
394                                    const CFX_GraphStateData* pGraphState,
395                                    FX_ARGB fill_color,
396                                    FX_ARGB stroke_color,
397                                    int fill_mode,
398                                    int alpha_flag,
399                                    void* pIccTransform,
400                                    int blend_type) {
401   if (blend_type != FXDIB_BLEND_NORMAL) {
402     return FALSE;
403   }
404   return m_PSRenderer.DrawPath(pPathData, pObject2Device, pGraphState,
405                                fill_color, stroke_color, fill_mode & 3,
406                                alpha_flag, pIccTransform);
407 }
408 FX_BOOL CPSPrinterDriver::GetClipBox(FX_RECT* pRect) {
409   *pRect = m_PSRenderer.GetClipBox();
410   return TRUE;
411 }
412 FX_BOOL CPSPrinterDriver::SetDIBits(const CFX_DIBSource* pBitmap,
413                                     FX_DWORD color,
414                                     const FX_RECT* pSrcRect,
415                                     int left,
416                                     int top,
417                                     int blend_type,
418                                     int alpha_flag,
419                                     void* pIccTransform) {
420   if (blend_type != FXDIB_BLEND_NORMAL) {
421     return FALSE;
422   }
423   return m_PSRenderer.SetDIBits(pBitmap, color, left, top, alpha_flag,
424                                 pIccTransform);
425 }
426 FX_BOOL CPSPrinterDriver::StretchDIBits(const CFX_DIBSource* pBitmap,
427                                         FX_DWORD color,
428                                         int dest_left,
429                                         int dest_top,
430                                         int dest_width,
431                                         int dest_height,
432                                         const FX_RECT* pClipRect,
433                                         FX_DWORD flags,
434                                         int alpha_flag,
435                                         void* pIccTransform,
436                                         int blend_type) {
437   if (blend_type != FXDIB_BLEND_NORMAL) {
438     return FALSE;
439   }
440   return m_PSRenderer.StretchDIBits(pBitmap, color, dest_left, dest_top,
441                                     dest_width, dest_height, flags, alpha_flag,
442                                     pIccTransform);
443 }
444 FX_BOOL CPSPrinterDriver::StartDIBits(const CFX_DIBSource* pBitmap,
445                                       int bitmap_alpha,
446                                       FX_DWORD color,
447                                       const CFX_AffineMatrix* pMatrix,
448                                       FX_DWORD render_flags,
449                                       void*& handle,
450                                       int alpha_flag,
451                                       void* pIccTransform,
452                                       int blend_type) {
453   if (blend_type != FXDIB_BLEND_NORMAL) {
454     return FALSE;
455   }
456   if (bitmap_alpha < 255) {
457     return FALSE;
458   }
459   handle = NULL;
460   return m_PSRenderer.DrawDIBits(pBitmap, color, pMatrix, render_flags,
461                                  alpha_flag, pIccTransform);
462 }
463 FX_BOOL CPSPrinterDriver::DrawDeviceText(int nChars,
464                                          const FXTEXT_CHARPOS* pCharPos,
465                                          CFX_Font* pFont,
466                                          CFX_FontCache* pCache,
467                                          const CFX_AffineMatrix* pObject2Device,
468                                          FX_FLOAT font_size,
469                                          FX_DWORD color,
470                                          int alpha_flag,
471                                          void* pIccTransform) {
472   return m_PSRenderer.DrawText(nChars, pCharPos, pFont, pCache, pObject2Device,
473                                font_size, color, alpha_flag, pIccTransform);
474 }
475 #endif