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