| OLD | NEW |
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 CGdiPrinterDriver::~CGdiPrinterDriver() {} | 59 CGdiPrinterDriver::~CGdiPrinterDriver() {} |
| 60 | 60 |
| 61 int CGdiPrinterDriver::GetDeviceCaps(int caps_id) const { | 61 int CGdiPrinterDriver::GetDeviceCaps(int caps_id) const { |
| 62 if (caps_id == FXDC_HORZ_SIZE) | 62 if (caps_id == FXDC_HORZ_SIZE) |
| 63 return m_HorzSize; | 63 return m_HorzSize; |
| 64 if (caps_id == FXDC_VERT_SIZE) | 64 if (caps_id == FXDC_VERT_SIZE) |
| 65 return m_VertSize; | 65 return m_VertSize; |
| 66 return CGdiDeviceDriver::GetDeviceCaps(caps_id); | 66 return CGdiDeviceDriver::GetDeviceCaps(caps_id); |
| 67 } | 67 } |
| 68 | 68 |
| 69 FX_BOOL CGdiPrinterDriver::SetDIBits(const CFX_DIBSource* pSource, | 69 bool CGdiPrinterDriver::SetDIBits(const CFX_DIBSource* pSource, |
| 70 uint32_t color, | 70 uint32_t color, |
| 71 const FX_RECT* pSrcRect, | 71 const FX_RECT* pSrcRect, |
| 72 int left, | 72 int left, |
| 73 int top, | 73 int top, |
| 74 int blend_type) { | 74 int blend_type) { |
| 75 if (pSource->IsAlphaMask()) { | 75 if (pSource->IsAlphaMask()) { |
| 76 FX_RECT clip_rect(left, top, left + pSrcRect->Width(), | 76 FX_RECT clip_rect(left, top, left + pSrcRect->Width(), |
| 77 top + pSrcRect->Height()); | 77 top + pSrcRect->Height()); |
| 78 return StretchDIBits(pSource, color, left - pSrcRect->left, | 78 return StretchDIBits(pSource, color, left - pSrcRect->left, |
| 79 top - pSrcRect->top, pSource->GetWidth(), | 79 top - pSrcRect->top, pSource->GetWidth(), |
| 80 pSource->GetHeight(), &clip_rect, 0, | 80 pSource->GetHeight(), &clip_rect, 0, |
| 81 FXDIB_BLEND_NORMAL); | 81 FXDIB_BLEND_NORMAL); |
| 82 } | 82 } |
| 83 ASSERT(pSource && !pSource->IsAlphaMask() && pSrcRect); | 83 ASSERT(pSource && !pSource->IsAlphaMask() && pSrcRect); |
| 84 ASSERT(blend_type == FXDIB_BLEND_NORMAL); | 84 ASSERT(blend_type == FXDIB_BLEND_NORMAL); |
| 85 if (pSource->HasAlpha()) | 85 if (pSource->HasAlpha()) |
| 86 return FALSE; | 86 return false; |
| 87 | 87 |
| 88 CFX_DIBExtractor temp(pSource); | 88 CFX_DIBExtractor temp(pSource); |
| 89 CFX_DIBitmap* pBitmap = temp.GetBitmap(); | 89 CFX_DIBitmap* pBitmap = temp.GetBitmap(); |
| 90 if (!pBitmap) | 90 if (!pBitmap) |
| 91 return FALSE; | 91 return false; |
| 92 | 92 |
| 93 return GDI_SetDIBits(pBitmap, pSrcRect, left, top); | 93 return GDI_SetDIBits(pBitmap, pSrcRect, left, top); |
| 94 } | 94 } |
| 95 | 95 |
| 96 FX_BOOL CGdiPrinterDriver::StretchDIBits(const CFX_DIBSource* pSource, | 96 bool CGdiPrinterDriver::StretchDIBits(const CFX_DIBSource* pSource, |
| 97 uint32_t color, | 97 uint32_t color, |
| 98 int dest_left, | 98 int dest_left, |
| 99 int dest_top, | 99 int dest_top, |
| 100 int dest_width, | 100 int dest_width, |
| 101 int dest_height, | 101 int dest_height, |
| 102 const FX_RECT* pClipRect, | 102 const FX_RECT* pClipRect, |
| 103 uint32_t flags, | 103 uint32_t flags, |
| 104 int blend_type) { | 104 int blend_type) { |
| 105 if (pSource->IsAlphaMask()) { | 105 if (pSource->IsAlphaMask()) { |
| 106 int alpha = FXARGB_A(color); | 106 int alpha = FXARGB_A(color); |
| 107 if (pSource->GetBPP() != 1 || alpha != 255) | 107 if (pSource->GetBPP() != 1 || alpha != 255) |
| 108 return FALSE; | 108 return false; |
| 109 | 109 |
| 110 if (dest_width < 0 || dest_height < 0) { | 110 if (dest_width < 0 || dest_height < 0) { |
| 111 std::unique_ptr<CFX_DIBitmap> pFlipped( | 111 std::unique_ptr<CFX_DIBitmap> pFlipped( |
| 112 pSource->FlipImage(dest_width < 0, dest_height < 0)); | 112 pSource->FlipImage(dest_width < 0, dest_height < 0)); |
| 113 if (!pFlipped) | 113 if (!pFlipped) |
| 114 return FALSE; | 114 return false; |
| 115 | 115 |
| 116 if (dest_width < 0) | 116 if (dest_width < 0) |
| 117 dest_left += dest_width; | 117 dest_left += dest_width; |
| 118 if (dest_height < 0) | 118 if (dest_height < 0) |
| 119 dest_top += dest_height; | 119 dest_top += dest_height; |
| 120 | 120 |
| 121 return GDI_StretchBitMask(pFlipped.get(), dest_left, dest_top, | 121 return GDI_StretchBitMask(pFlipped.get(), dest_left, dest_top, |
| 122 abs(dest_width), abs(dest_height), color, | 122 abs(dest_width), abs(dest_height), color, |
| 123 flags); | 123 flags); |
| 124 } | 124 } |
| 125 | 125 |
| 126 CFX_DIBExtractor temp(pSource); | 126 CFX_DIBExtractor temp(pSource); |
| 127 CFX_DIBitmap* pBitmap = temp.GetBitmap(); | 127 CFX_DIBitmap* pBitmap = temp.GetBitmap(); |
| 128 if (!pBitmap) | 128 if (!pBitmap) |
| 129 return FALSE; | 129 return false; |
| 130 return GDI_StretchBitMask(pBitmap, dest_left, dest_top, dest_width, | 130 return GDI_StretchBitMask(pBitmap, dest_left, dest_top, dest_width, |
| 131 dest_height, color, flags); | 131 dest_height, color, flags); |
| 132 } | 132 } |
| 133 | 133 |
| 134 if (pSource->HasAlpha()) | 134 if (pSource->HasAlpha()) |
| 135 return FALSE; | 135 return false; |
| 136 | 136 |
| 137 if (dest_width < 0 || dest_height < 0) { | 137 if (dest_width < 0 || dest_height < 0) { |
| 138 std::unique_ptr<CFX_DIBitmap> pFlipped( | 138 std::unique_ptr<CFX_DIBitmap> pFlipped( |
| 139 pSource->FlipImage(dest_width < 0, dest_height < 0)); | 139 pSource->FlipImage(dest_width < 0, dest_height < 0)); |
| 140 if (!pFlipped) | 140 if (!pFlipped) |
| 141 return FALSE; | 141 return false; |
| 142 | 142 |
| 143 if (dest_width < 0) | 143 if (dest_width < 0) |
| 144 dest_left += dest_width; | 144 dest_left += dest_width; |
| 145 if (dest_height < 0) | 145 if (dest_height < 0) |
| 146 dest_top += dest_height; | 146 dest_top += dest_height; |
| 147 | 147 |
| 148 return GDI_StretchDIBits(pFlipped.get(), dest_left, dest_top, | 148 return GDI_StretchDIBits(pFlipped.get(), dest_left, dest_top, |
| 149 abs(dest_width), abs(dest_height), flags); | 149 abs(dest_width), abs(dest_height), flags); |
| 150 } | 150 } |
| 151 | 151 |
| 152 CFX_DIBExtractor temp(pSource); | 152 CFX_DIBExtractor temp(pSource); |
| 153 CFX_DIBitmap* pBitmap = temp.GetBitmap(); | 153 CFX_DIBitmap* pBitmap = temp.GetBitmap(); |
| 154 if (!pBitmap) | 154 if (!pBitmap) |
| 155 return FALSE; | 155 return false; |
| 156 return GDI_StretchDIBits(pBitmap, dest_left, dest_top, dest_width, | 156 return GDI_StretchDIBits(pBitmap, dest_left, dest_top, dest_width, |
| 157 dest_height, flags); | 157 dest_height, flags); |
| 158 } | 158 } |
| 159 | 159 |
| 160 FX_BOOL CGdiPrinterDriver::StartDIBits(const CFX_DIBSource* pSource, | 160 bool CGdiPrinterDriver::StartDIBits(const CFX_DIBSource* pSource, |
| 161 int bitmap_alpha, | 161 int bitmap_alpha, |
| 162 uint32_t color, | 162 uint32_t color, |
| 163 const CFX_Matrix* pMatrix, | 163 const CFX_Matrix* pMatrix, |
| 164 uint32_t render_flags, | 164 uint32_t render_flags, |
| 165 void*& handle, | 165 void*& handle, |
| 166 int blend_type) { | 166 int blend_type) { |
| 167 if (bitmap_alpha < 255 || pSource->HasAlpha() || | 167 if (bitmap_alpha < 255 || pSource->HasAlpha() || |
| 168 (pSource->IsAlphaMask() && (pSource->GetBPP() != 1))) { | 168 (pSource->IsAlphaMask() && (pSource->GetBPP() != 1))) { |
| 169 return FALSE; | 169 return false; |
| 170 } | 170 } |
| 171 CFX_FloatRect unit_rect = pMatrix->GetUnitRect(); | 171 CFX_FloatRect unit_rect = pMatrix->GetUnitRect(); |
| 172 FX_RECT full_rect = unit_rect.GetOuterRect(); | 172 FX_RECT full_rect = unit_rect.GetOuterRect(); |
| 173 if (FXSYS_fabs(pMatrix->b) < 0.5f && pMatrix->a != 0 && | 173 if (FXSYS_fabs(pMatrix->b) < 0.5f && pMatrix->a != 0 && |
| 174 FXSYS_fabs(pMatrix->c) < 0.5f && pMatrix->d != 0) { | 174 FXSYS_fabs(pMatrix->c) < 0.5f && pMatrix->d != 0) { |
| 175 FX_BOOL bFlipX = pMatrix->a < 0; | 175 bool bFlipX = pMatrix->a < 0; |
| 176 FX_BOOL bFlipY = pMatrix->d > 0; | 176 bool bFlipY = pMatrix->d > 0; |
| 177 return StretchDIBits(pSource, color, | 177 return StretchDIBits(pSource, color, |
| 178 bFlipX ? full_rect.right : full_rect.left, | 178 bFlipX ? full_rect.right : full_rect.left, |
| 179 bFlipY ? full_rect.bottom : full_rect.top, | 179 bFlipY ? full_rect.bottom : full_rect.top, |
| 180 bFlipX ? -full_rect.Width() : full_rect.Width(), | 180 bFlipX ? -full_rect.Width() : full_rect.Width(), |
| 181 bFlipY ? -full_rect.Height() : full_rect.Height(), | 181 bFlipY ? -full_rect.Height() : full_rect.Height(), |
| 182 nullptr, 0, blend_type); | 182 nullptr, 0, blend_type); |
| 183 } | 183 } |
| 184 if (FXSYS_fabs(pMatrix->a) >= 0.5f || FXSYS_fabs(pMatrix->d) >= 0.5f) | 184 if (FXSYS_fabs(pMatrix->a) >= 0.5f || FXSYS_fabs(pMatrix->d) >= 0.5f) |
| 185 return FALSE; | 185 return false; |
| 186 | 186 |
| 187 std::unique_ptr<CFX_DIBitmap> pTransformed( | 187 std::unique_ptr<CFX_DIBitmap> pTransformed( |
| 188 pSource->SwapXY(pMatrix->c > 0, pMatrix->b < 0)); | 188 pSource->SwapXY(pMatrix->c > 0, pMatrix->b < 0)); |
| 189 if (!pTransformed) | 189 if (!pTransformed) |
| 190 return FALSE; | 190 return false; |
| 191 | 191 |
| 192 return StretchDIBits(pTransformed.get(), color, full_rect.left, full_rect.top, | 192 return StretchDIBits(pTransformed.get(), color, full_rect.left, full_rect.top, |
| 193 full_rect.Width(), full_rect.Height(), nullptr, 0, | 193 full_rect.Width(), full_rect.Height(), nullptr, 0, |
| 194 blend_type); | 194 blend_type); |
| 195 } | 195 } |
| 196 | 196 |
| 197 FX_BOOL CGdiPrinterDriver::DrawDeviceText(int nChars, | 197 bool CGdiPrinterDriver::DrawDeviceText(int nChars, |
| 198 const FXTEXT_CHARPOS* pCharPos, | 198 const FXTEXT_CHARPOS* pCharPos, |
| 199 CFX_Font* pFont, | 199 CFX_Font* pFont, |
| 200 const CFX_Matrix* pObject2Device, | 200 const CFX_Matrix* pObject2Device, |
| 201 FX_FLOAT font_size, | 201 FX_FLOAT font_size, |
| 202 uint32_t color) { | 202 uint32_t color) { |
| 203 #if defined(PDFIUM_PRINT_TEXT_WITH_GDI) | 203 #if defined(PDFIUM_PRINT_TEXT_WITH_GDI) |
| 204 if (!g_pdfium_print_text_with_gdi) | 204 if (!g_pdfium_print_text_with_gdi) |
| 205 return FALSE; | 205 return false; |
| 206 | 206 |
| 207 if (nChars < 1 || !pFont || !pFont->IsEmbedded() || !pFont->IsTTFont()) | 207 if (nChars < 1 || !pFont || !pFont->IsEmbedded() || !pFont->IsTTFont()) |
| 208 return FALSE; | 208 return false; |
| 209 | 209 |
| 210 // Scale factor used to minimize the kerning problems caused by rounding | 210 // Scale factor used to minimize the kerning problems caused by rounding |
| 211 // errors below. Value choosen based on the title of https://crbug.com/18383 | 211 // errors below. Value choosen based on the title of https://crbug.com/18383 |
| 212 const double kScaleFactor = 10; | 212 const double kScaleFactor = 10; |
| 213 | 213 |
| 214 // Font | 214 // Font |
| 215 // | 215 // |
| 216 // Note that |pFont| has the actual font to render with embedded within, but | 216 // Note that |pFont| has the actual font to render with embedded within, but |
| 217 // but unfortunately AddFontMemResourceEx() does not seem to cooperate. | 217 // but unfortunately AddFontMemResourceEx() does not seem to cooperate. |
| 218 // Loading font data to memory seems to work, but then enumerating the fonts | 218 // Loading font data to memory seems to work, but then enumerating the fonts |
| (...skipping 10 matching lines...) Expand all Loading... |
| 229 lf.lfItalic = pFont->IsItalic(); | 229 lf.lfItalic = pFont->IsItalic(); |
| 230 lf.lfCharSet = DEFAULT_CHARSET; | 230 lf.lfCharSet = DEFAULT_CHARSET; |
| 231 | 231 |
| 232 const CFX_WideString wsName = pFont->GetFaceName().UTF8Decode(); | 232 const CFX_WideString wsName = pFont->GetFaceName().UTF8Decode(); |
| 233 int iNameLen = std::min(wsName.GetLength(), LF_FACESIZE - 1); | 233 int iNameLen = std::min(wsName.GetLength(), LF_FACESIZE - 1); |
| 234 memcpy(lf.lfFaceName, wsName.c_str(), sizeof(lf.lfFaceName[0]) * iNameLen); | 234 memcpy(lf.lfFaceName, wsName.c_str(), sizeof(lf.lfFaceName[0]) * iNameLen); |
| 235 lf.lfFaceName[iNameLen] = 0; | 235 lf.lfFaceName[iNameLen] = 0; |
| 236 | 236 |
| 237 HFONT hFont = CreateFontIndirect(&lf); | 237 HFONT hFont = CreateFontIndirect(&lf); |
| 238 if (!hFont) | 238 if (!hFont) |
| 239 return FALSE; | 239 return false; |
| 240 | 240 |
| 241 ScopedState state(m_hDC, hFont); | 241 ScopedState state(m_hDC, hFont); |
| 242 size_t nTextMetricSize = GetOutlineTextMetrics(m_hDC, 0, nullptr); | 242 size_t nTextMetricSize = GetOutlineTextMetrics(m_hDC, 0, nullptr); |
| 243 if (nTextMetricSize == 0) { | 243 if (nTextMetricSize == 0) { |
| 244 // Give up and fail if there is no way to get the font to try again. | 244 // Give up and fail if there is no way to get the font to try again. |
| 245 if (!g_pdfium_typeface_accessible_func) | 245 if (!g_pdfium_typeface_accessible_func) |
| 246 return FALSE; | 246 return false; |
| 247 | 247 |
| 248 // Try to get the font. Any letter will do. | 248 // Try to get the font. Any letter will do. |
| 249 g_pdfium_typeface_accessible_func(&lf, L"A", 1); | 249 g_pdfium_typeface_accessible_func(&lf, L"A", 1); |
| 250 nTextMetricSize = GetOutlineTextMetrics(m_hDC, 0, nullptr); | 250 nTextMetricSize = GetOutlineTextMetrics(m_hDC, 0, nullptr); |
| 251 if (nTextMetricSize == 0) | 251 if (nTextMetricSize == 0) |
| 252 return FALSE; | 252 return false; |
| 253 } | 253 } |
| 254 | 254 |
| 255 std::vector<BYTE> buf(nTextMetricSize); | 255 std::vector<BYTE> buf(nTextMetricSize); |
| 256 OUTLINETEXTMETRIC* pTextMetric = | 256 OUTLINETEXTMETRIC* pTextMetric = |
| 257 reinterpret_cast<OUTLINETEXTMETRIC*>(buf.data()); | 257 reinterpret_cast<OUTLINETEXTMETRIC*>(buf.data()); |
| 258 if (GetOutlineTextMetrics(m_hDC, nTextMetricSize, pTextMetric) == 0) | 258 if (GetOutlineTextMetrics(m_hDC, nTextMetricSize, pTextMetric) == 0) |
| 259 return FALSE; | 259 return false; |
| 260 | 260 |
| 261 // If the selected font is not the requested font, then bail out. This can | 261 // If the selected font is not the requested font, then bail out. This can |
| 262 // happen with web fonts, for example. | 262 // happen with web fonts, for example. |
| 263 wchar_t* wsSelectedName = reinterpret_cast<wchar_t*>( | 263 wchar_t* wsSelectedName = reinterpret_cast<wchar_t*>( |
| 264 buf.data() + reinterpret_cast<size_t>(pTextMetric->otmpFaceName)); | 264 buf.data() + reinterpret_cast<size_t>(pTextMetric->otmpFaceName)); |
| 265 if (wsName != wsSelectedName) | 265 if (wsName != wsSelectedName) |
| 266 return FALSE; | 266 return false; |
| 267 | 267 |
| 268 // Transforms | 268 // Transforms |
| 269 SetGraphicsMode(m_hDC, GM_ADVANCED); | 269 SetGraphicsMode(m_hDC, GM_ADVANCED); |
| 270 XFORM xform; | 270 XFORM xform; |
| 271 xform.eM11 = pObject2Device->GetA() / kScaleFactor; | 271 xform.eM11 = pObject2Device->GetA() / kScaleFactor; |
| 272 xform.eM12 = pObject2Device->GetB() / kScaleFactor; | 272 xform.eM12 = pObject2Device->GetB() / kScaleFactor; |
| 273 xform.eM21 = -pObject2Device->GetC() / kScaleFactor; | 273 xform.eM21 = -pObject2Device->GetC() / kScaleFactor; |
| 274 xform.eM22 = -pObject2Device->GetD() / kScaleFactor; | 274 xform.eM22 = -pObject2Device->GetD() / kScaleFactor; |
| 275 xform.eDx = pObject2Device->GetE(); | 275 xform.eDx = pObject2Device->GetE(); |
| 276 xform.eDy = pObject2Device->GetF(); | 276 xform.eDy = pObject2Device->GetF(); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 304 spacing[i] = FXSYS_round(fPixelSpacing); | 304 spacing[i] = FXSYS_round(fPixelSpacing); |
| 305 fPreviousOriginX = fOriginX - (fPixelSpacing - spacing[i]); | 305 fPreviousOriginX = fOriginX - (fPixelSpacing - spacing[i]); |
| 306 | 306 |
| 307 wsText += charpos.m_GlyphIndex; | 307 wsText += charpos.m_GlyphIndex; |
| 308 } | 308 } |
| 309 | 309 |
| 310 // Draw | 310 // Draw |
| 311 SetTextAlign(m_hDC, TA_LEFT | TA_BASELINE); | 311 SetTextAlign(m_hDC, TA_LEFT | TA_BASELINE); |
| 312 if (ExtTextOutW(m_hDC, 0, 0, ETO_GLYPH_INDEX, nullptr, wsText.c_str(), nChars, | 312 if (ExtTextOutW(m_hDC, 0, 0, ETO_GLYPH_INDEX, nullptr, wsText.c_str(), nChars, |
| 313 nChars > 1 ? &spacing[1] : nullptr)) { | 313 nChars > 1 ? &spacing[1] : nullptr)) { |
| 314 return TRUE; | 314 return true; |
| 315 } | 315 } |
| 316 | 316 |
| 317 // Give up and fail if there is no way to get the font to try again. | 317 // Give up and fail if there is no way to get the font to try again. |
| 318 if (!g_pdfium_typeface_accessible_func) | 318 if (!g_pdfium_typeface_accessible_func) |
| 319 return FALSE; | 319 return false; |
| 320 | 320 |
| 321 // Try to get the font and draw again. | 321 // Try to get the font and draw again. |
| 322 g_pdfium_typeface_accessible_func(&lf, wsText.c_str(), nChars); | 322 g_pdfium_typeface_accessible_func(&lf, wsText.c_str(), nChars); |
| 323 return !!ExtTextOutW(m_hDC, 0, 0, ETO_GLYPH_INDEX, nullptr, wsText.c_str(), | 323 return !!ExtTextOutW(m_hDC, 0, 0, ETO_GLYPH_INDEX, nullptr, wsText.c_str(), |
| 324 nChars, nChars > 1 ? &spacing[1] : nullptr); | 324 nChars, nChars > 1 ? &spacing[1] : nullptr); |
| 325 #else | 325 #else |
| 326 return FALSE; | 326 return false; |
| 327 #endif | 327 #endif |
| 328 } | 328 } |
| OLD | NEW |