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 <crtdbg.h> 8 9 #include <algorithm> 10 #include <memory> 11 #include <vector> 12 13 #include "core/fxcodec/fx_codec.h" 14 #include "core/fxcrt/cfx_maybe_owned.h" 15 #include "core/fxcrt/fx_memory.h" 16 #include "core/fxcrt/fx_system.h" 17 #include "core/fxge/cfx_windowsdevice.h" 18 #include "core/fxge/dib/dib_int.h" 19 #include "core/fxge/fx_font.h" 20 #include "core/fxge/fx_freetype.h" 21 #include "core/fxge/ge/cfx_folderfontinfo.h" 22 #include "core/fxge/ge/fx_text_int.h" 23 #include "core/fxge/ifx_systemfontinfo.h" 24 #include "core/fxge/win32/cfx_windowsdib.h" 25 #include "core/fxge/win32/dwrite_int.h" 26 #include "core/fxge/win32/win32_int.h" 27 #include "third_party/base/ptr_util.h" 28 #include "third_party/base/stl_util.h" 29 30 #ifndef _SKIA_SUPPORT_ 31 #include "core/fxge/agg/fx_agg_driver.h" 32 #endif 33 34 namespace { 35 36 const struct { 37 const FX_CHAR* m_pFaceName; 38 const FX_CHAR* m_pVariantName; 39 } g_VariantNames[] = { 40 {"DFKai-SB", "\x19\x6A\x77\x69\xD4\x9A"}, 41 }; 42 43 const struct { 44 const FX_CHAR* m_pName; 45 const FX_CHAR* m_pWinName; 46 bool m_bBold; 47 bool m_bItalic; 48 } g_Base14Substs[] = { 49 {"Courier", "Courier New", false, false}, 50 {"Courier-Bold", "Courier New", true, false}, 51 {"Courier-BoldOblique", "Courier New", true, true}, 52 {"Courier-Oblique", "Courier New", false, true}, 53 {"Helvetica", "Arial", false, false}, 54 {"Helvetica-Bold", "Arial", true, false}, 55 {"Helvetica-BoldOblique", "Arial", true, true}, 56 {"Helvetica-Oblique", "Arial", false, true}, 57 {"Times-Roman", "Times New Roman", false, false}, 58 {"Times-Bold", "Times New Roman", true, false}, 59 {"Times-BoldItalic", "Times New Roman", true, true}, 60 {"Times-Italic", "Times New Roman", false, true}, 61 }; 62 63 struct FontNameMap { 64 const FX_CHAR* m_pSubFontName; 65 const FX_CHAR* m_pSrcFontName; 66 }; 67 const FontNameMap g_JpFontNameMap[] = { 68 {"MS Mincho", "Heiseimin-W3"}, 69 {"MS Gothic", "Jun101-Light"}, 70 }; 71 72 bool GetSubFontName(CFX_ByteString* name) { 73 for (size_t i = 0; i < FX_ArraySize(g_JpFontNameMap); ++i) { 74 if (!FXSYS_stricmp(name->c_str(), g_JpFontNameMap[i].m_pSrcFontName)) { 75 *name = g_JpFontNameMap[i].m_pSubFontName; 76 return true; 77 } 78 } 79 return false; 80 } 81 82 bool IsGDIEnabled() { 83 // If GDI is disabled then GetDC for the desktop will fail. 84 HDC hdc = ::GetDC(nullptr); 85 if (!hdc) 86 return false; 87 ::ReleaseDC(nullptr, hdc); 88 return true; 89 } 90 91 HPEN CreatePen(const CFX_GraphStateData* pGraphState, 92 const CFX_Matrix* pMatrix, 93 uint32_t argb) { 94 FX_FLOAT width; 95 FX_FLOAT scale = 1.f; 96 if (pMatrix) 97 scale = FXSYS_fabs(pMatrix->a) > FXSYS_fabs(pMatrix->b) 98 ? FXSYS_fabs(pMatrix->a) 99 : FXSYS_fabs(pMatrix->b); 100 if (pGraphState) { 101 width = scale * pGraphState->m_LineWidth; 102 } else { 103 width = 1.0f; 104 } 105 uint32_t PenStyle = PS_GEOMETRIC; 106 if (width < 1) { 107 width = 1; 108 } 109 if (pGraphState->m_DashCount) { 110 PenStyle |= PS_USERSTYLE; 111 } else { 112 PenStyle |= PS_SOLID; 113 } 114 switch (pGraphState->m_LineCap) { 115 case 0: 116 PenStyle |= PS_ENDCAP_FLAT; 117 break; 118 case 1: 119 PenStyle |= PS_ENDCAP_ROUND; 120 break; 121 case 2: 122 PenStyle |= PS_ENDCAP_SQUARE; 123 break; 124 } 125 switch (pGraphState->m_LineJoin) { 126 case 0: 127 PenStyle |= PS_JOIN_MITER; 128 break; 129 case 1: 130 PenStyle |= PS_JOIN_ROUND; 131 break; 132 case 2: 133 PenStyle |= PS_JOIN_BEVEL; 134 break; 135 } 136 int a; 137 FX_COLORREF rgb; 138 ArgbDecode(argb, a, rgb); 139 LOGBRUSH lb; 140 lb.lbColor = rgb; 141 lb.lbStyle = BS_SOLID; 142 lb.lbHatch = 0; 143 std::vector<uint32_t> dashes; 144 if (pGraphState->m_DashCount) { 145 dashes.resize(pGraphState->m_DashCount); 146 for (int i = 0; i < pGraphState->m_DashCount; i++) { 147 dashes[i] = FXSYS_round( 148 pMatrix ? pMatrix->TransformDistance(pGraphState->m_DashArray[i]) 149 : pGraphState->m_DashArray[i]); 150 dashes[i] = std::max(dashes[i], 1U); 151 } 152 } 153 return ExtCreatePen(PenStyle, (DWORD)FXSYS_ceil(width), &lb, 154 pGraphState->m_DashCount, 155 reinterpret_cast<const DWORD*>(dashes.data())); 156 } 157 158 HBRUSH CreateBrush(uint32_t argb) { 159 int a; 160 FX_COLORREF rgb; 161 ArgbDecode(argb, a, rgb); 162 return CreateSolidBrush(rgb); 163 } 164 165 void SetPathToDC(HDC hDC, 166 const CFX_PathData* pPathData, 167 const CFX_Matrix* pMatrix) { 168 BeginPath(hDC); 169 170 const std::vector<FX_PATHPOINT>& pPoints = pPathData->GetPoints(); 171 for (size_t i = 0; i < pPoints.size(); i++) { 172 CFX_PointF pos = pPoints[i].m_Point; 173 if (pMatrix) 174 pos = pMatrix->Transform(pos); 175 176 CFX_Point screen(FXSYS_round(pos.x), FXSYS_round(pos.y)); 177 FXPT_TYPE point_type = pPoints[i].m_Type; 178 if (point_type == FXPT_TYPE::MoveTo) { 179 MoveToEx(hDC, screen.x, screen.y, nullptr); 180 } else if (point_type == FXPT_TYPE::LineTo) { 181 if (pPoints[i].m_Point == pPoints[i - 1].m_Point) 182 screen.x++; 183 184 LineTo(hDC, screen.x, screen.y); 185 } else if (point_type == FXPT_TYPE::BezierTo) { 186 POINT lppt[3]; 187 lppt[0].x = screen.x; 188 lppt[0].y = screen.y; 189 190 pos = pPoints[i + 1].m_Point; 191 if (pMatrix) 192 pos = pMatrix->Transform(pos); 193 194 lppt[1].x = FXSYS_round(pos.x); 195 lppt[1].y = FXSYS_round(pos.y); 196 197 pos = pPoints[i + 2].m_Point; 198 if (pMatrix) 199 pos = pMatrix->Transform(pos); 200 201 lppt[2].x = FXSYS_round(pos.x); 202 lppt[2].y = FXSYS_round(pos.y); 203 PolyBezierTo(hDC, lppt, 3); 204 i += 2; 205 } 206 if (pPoints[i].m_CloseFigure) 207 CloseFigure(hDC); 208 } 209 EndPath(hDC); 210 } 211 212 #ifdef _SKIA_SUPPORT_ 213 // TODO(caryclark) This antigrain function is duplicated here to permit 214 // removing the last remaining dependency. Eventually, this will be elminiated 215 // altogether and replace by Skia code. 216 217 struct rect_base { 218 FX_FLOAT x1; 219 FX_FLOAT y1; 220 FX_FLOAT x2; 221 FX_FLOAT y2; 222 }; 223 224 unsigned clip_liang_barsky(FX_FLOAT x1, 225 FX_FLOAT y1, 226 FX_FLOAT x2, 227 FX_FLOAT y2, 228 const rect_base& clip_box, 229 FX_FLOAT* x, 230 FX_FLOAT* y) { 231 const FX_FLOAT nearzero = 1e-30f; 232 FX_FLOAT deltax = x2 - x1; 233 FX_FLOAT deltay = y2 - y1; 234 unsigned np = 0; 235 if (deltax == 0) 236 deltax = (x1 > clip_box.x1) ? -nearzero : nearzero; 237 FX_FLOAT xin, xout; 238 if (deltax > 0) { 239 xin = clip_box.x1; 240 xout = clip_box.x2; 241 } else { 242 xin = clip_box.x2; 243 xout = clip_box.x1; 244 } 245 FX_FLOAT tinx = (xin - x1) / deltax; 246 if (deltay == 0) 247 deltay = (y1 > clip_box.y1) ? -nearzero : nearzero; 248 FX_FLOAT yin, yout; 249 if (deltay > 0) { 250 yin = clip_box.y1; 251 yout = clip_box.y2; 252 } else { 253 yin = clip_box.y2; 254 yout = clip_box.y1; 255 } 256 FX_FLOAT tiny = (yin - y1) / deltay; 257 FX_FLOAT tin1, tin2; 258 if (tinx < tiny) { 259 tin1 = tinx; 260 tin2 = tiny; 261 } else { 262 tin1 = tiny; 263 tin2 = tinx; 264 } 265 if (tin1 <= 1.0f) { 266 if (0 < tin1) { 267 *x++ = xin; 268 *y++ = yin; 269 ++np; 270 } 271 if (tin2 <= 1.0f) { 272 FX_FLOAT toutx = (xout - x1) / deltax; 273 FX_FLOAT touty = (yout - y1) / deltay; 274 FX_FLOAT tout1 = (toutx < touty) ? toutx : touty; 275 if (tin2 > 0 || tout1 > 0) { 276 if (tin2 <= tout1) { 277 if (tin2 > 0) { 278 if (tinx > tiny) { 279 *x++ = xin; 280 *y++ = y1 + (deltay * tinx); 281 } else { 282 *x++ = x1 + (deltax * tiny); 283 *y++ = yin; 284 } 285 ++np; 286 } 287 if (tout1 < 1.0f) { 288 if (toutx < touty) { 289 *x++ = xout; 290 *y++ = y1 + (deltay * toutx); 291 } else { 292 *x++ = x1 + (deltax * touty); 293 *y++ = yout; 294 } 295 } else { 296 *x++ = x2; 297 *y++ = y2; 298 } 299 ++np; 300 } else { 301 if (tinx > tiny) { 302 *x++ = xin; 303 *y++ = yout; 304 } else { 305 *x++ = xout; 306 *y++ = yin; 307 } 308 ++np; 309 } 310 } 311 } 312 } 313 return np; 314 } 315 #endif // _SKIA_SUPPORT_ 316 317 class CFX_Win32FallbackFontInfo final : public CFX_FolderFontInfo { 318 public: 319 CFX_Win32FallbackFontInfo() {} 320 ~CFX_Win32FallbackFontInfo() override {} 321 322 // CFX_FolderFontInfo: 323 void* MapFont(int weight, 324 bool bItalic, 325 int charset, 326 int pitch_family, 327 const FX_CHAR* family, 328 int& iExact) override; 329 }; 330 331 class CFX_Win32FontInfo final : public IFX_SystemFontInfo { 332 public: 333 CFX_Win32FontInfo(); 334 ~CFX_Win32FontInfo() override; 335 336 // IFX_SystemFontInfo 337 bool EnumFontList(CFX_FontMapper* pMapper) override; 338 void* MapFont(int weight, 339 bool bItalic, 340 int charset, 341 int pitch_family, 342 const FX_CHAR* face, 343 int& iExact) override; 344 void* GetFont(const FX_CHAR* face) override { return nullptr; } 345 uint32_t GetFontData(void* hFont, 346 uint32_t table, 347 uint8_t* buffer, 348 uint32_t size) override; 349 bool GetFaceName(void* hFont, CFX_ByteString& name) override; 350 bool GetFontCharset(void* hFont, int& charset) override; 351 void DeleteFont(void* hFont) override; 352 353 bool IsOpenTypeFromDiv(const LOGFONTA* plf); 354 bool IsSupportFontFormDiv(const LOGFONTA* plf); 355 void AddInstalledFont(const LOGFONTA* plf, uint32_t FontType); 356 void GetGBPreference(CFX_ByteString& face, int weight, int picth_family); 357 void GetJapanesePreference(CFX_ByteString& face, 358 int weight, 359 int picth_family); 360 CFX_ByteString FindFont(const CFX_ByteString& name); 361 362 HDC m_hDC; 363 CFX_FontMapper* m_pMapper; 364 CFX_ByteString m_LastFamily; 365 CFX_ByteString m_KaiTi, m_FangSong; 366 }; 367 368 int CALLBACK FontEnumProc(const LOGFONTA* plf, 369 const TEXTMETRICA* lpntme, 370 uint32_t FontType, 371 LPARAM lParam) { 372 CFX_Win32FontInfo* pFontInfo = reinterpret_cast<CFX_Win32FontInfo*>(lParam); 373 pFontInfo->AddInstalledFont(plf, FontType); 374 return 1; 375 } 376 377 CFX_Win32FontInfo::CFX_Win32FontInfo() : m_hDC(CreateCompatibleDC(nullptr)) {} 378 379 CFX_Win32FontInfo::~CFX_Win32FontInfo() { 380 DeleteDC(m_hDC); 381 } 382 383 bool CFX_Win32FontInfo::IsOpenTypeFromDiv(const LOGFONTA* plf) { 384 HFONT hFont = CreateFontIndirectA(plf); 385 bool ret = false; 386 uint32_t font_size = GetFontData(hFont, 0, nullptr, 0); 387 if (font_size != GDI_ERROR && font_size >= sizeof(uint32_t)) { 388 uint32_t lVersion = 0; 389 GetFontData(hFont, 0, (uint8_t*)(&lVersion), sizeof(uint32_t)); 390 lVersion = (((uint32_t)(uint8_t)(lVersion)) << 24) | 391 ((uint32_t)((uint8_t)(lVersion >> 8))) << 16 | 392 ((uint32_t)((uint8_t)(lVersion >> 16))) << 8 | 393 ((uint8_t)(lVersion >> 24)); 394 if (lVersion == FXBSTR_ID('O', 'T', 'T', 'O') || lVersion == 0x00010000 || 395 lVersion == FXBSTR_ID('t', 't', 'c', 'f') || 396 lVersion == FXBSTR_ID('t', 'r', 'u', 'e') || lVersion == 0x00020000) { 397 ret = true; 398 } 399 } 400 DeleteFont(hFont); 401 return ret; 402 } 403 404 bool CFX_Win32FontInfo::IsSupportFontFormDiv(const LOGFONTA* plf) { 405 HFONT hFont = CreateFontIndirectA(plf); 406 bool ret = false; 407 uint32_t font_size = GetFontData(hFont, 0, nullptr, 0); 408 if (font_size != GDI_ERROR && font_size >= sizeof(uint32_t)) { 409 uint32_t lVersion = 0; 410 GetFontData(hFont, 0, (uint8_t*)(&lVersion), sizeof(uint32_t)); 411 lVersion = (((uint32_t)(uint8_t)(lVersion)) << 24) | 412 ((uint32_t)((uint8_t)(lVersion >> 8))) << 16 | 413 ((uint32_t)((uint8_t)(lVersion >> 16))) << 8 | 414 ((uint8_t)(lVersion >> 24)); 415 if (lVersion == FXBSTR_ID('O', 'T', 'T', 'O') || lVersion == 0x00010000 || 416 lVersion == FXBSTR_ID('t', 't', 'c', 'f') || 417 lVersion == FXBSTR_ID('t', 'r', 'u', 'e') || lVersion == 0x00020000 || 418 (lVersion & 0xFFFF0000) == FXBSTR_ID(0x80, 0x01, 0x00, 0x00) || 419 (lVersion & 0xFFFF0000) == FXBSTR_ID('%', '!', 0, 0)) { 420 ret = true; 421 } 422 } 423 DeleteFont(hFont); 424 return ret; 425 } 426 427 void CFX_Win32FontInfo::AddInstalledFont(const LOGFONTA* plf, 428 uint32_t FontType) { 429 CFX_ByteString name(plf->lfFaceName); 430 if (name[0] == '@') 431 return; 432 433 if (name == m_LastFamily) { 434 m_pMapper->AddInstalledFont(name, plf->lfCharSet); 435 return; 436 } 437 if (!(FontType & TRUETYPE_FONTTYPE)) { 438 if (!(FontType & DEVICE_FONTTYPE) || !IsSupportFontFormDiv(plf)) 439 return; 440 } 441 442 m_pMapper->AddInstalledFont(name, plf->lfCharSet); 443 m_LastFamily = name; 444 } 445 446 bool CFX_Win32FontInfo::EnumFontList(CFX_FontMapper* pMapper) { 447 m_pMapper = pMapper; 448 LOGFONTA lf; 449 FXSYS_memset(&lf, 0, sizeof(LOGFONTA)); 450 lf.lfCharSet = FXFONT_DEFAULT_CHARSET; 451 lf.lfFaceName[0] = 0; 452 lf.lfPitchAndFamily = 0; 453 EnumFontFamiliesExA(m_hDC, &lf, (FONTENUMPROCA)FontEnumProc, (uintptr_t) this, 454 0); 455 return true; 456 } 457 458 CFX_ByteString CFX_Win32FontInfo::FindFont(const CFX_ByteString& name) { 459 if (!m_pMapper) 460 return name; 461 462 for (size_t i = 0; i < m_pMapper->m_InstalledTTFonts.size(); ++i) { 463 CFX_ByteString thisname = m_pMapper->m_InstalledTTFonts[i]; 464 if (thisname.Left(name.GetLength()) == name) 465 return m_pMapper->m_InstalledTTFonts[i]; 466 } 467 for (size_t i = 0; i < m_pMapper->m_LocalizedTTFonts.size(); ++i) { 468 CFX_ByteString thisname = m_pMapper->m_LocalizedTTFonts[i].first; 469 if (thisname.Left(name.GetLength()) == name) 470 return m_pMapper->m_LocalizedTTFonts[i].second; 471 } 472 return CFX_ByteString(); 473 } 474 475 void* CFX_Win32FallbackFontInfo::MapFont(int weight, 476 bool bItalic, 477 int charset, 478 int pitch_family, 479 const FX_CHAR* cstr_face, 480 int& iExact) { 481 void* font = GetSubstFont(cstr_face); 482 if (font) { 483 iExact = 1; 484 return font; 485 } 486 bool bCJK = true; 487 switch (charset) { 488 case FXFONT_SHIFTJIS_CHARSET: 489 case FXFONT_GB2312_CHARSET: 490 case FXFONT_CHINESEBIG5_CHARSET: 491 case FXFONT_HANGUL_CHARSET: 492 break; 493 default: 494 bCJK = false; 495 break; 496 } 497 return FindFont(weight, bItalic, charset, pitch_family, cstr_face, !bCJK); 498 } 499 500 void CFX_Win32FontInfo::GetGBPreference(CFX_ByteString& face, 501 int weight, 502 int picth_family) { 503 if (face.Find("KaiTi") >= 0 || face.Find("\xbf\xac") >= 0) { 504 if (m_KaiTi.IsEmpty()) { 505 m_KaiTi = FindFont("KaiTi"); 506 if (m_KaiTi.IsEmpty()) { 507 m_KaiTi = "SimSun"; 508 } 509 } 510 face = m_KaiTi; 511 } else if (face.Find("FangSong") >= 0 || face.Find("\xb7\xc2\xcb\xce") >= 0) { 512 if (m_FangSong.IsEmpty()) { 513 m_FangSong = FindFont("FangSong"); 514 if (m_FangSong.IsEmpty()) { 515 m_FangSong = "SimSun"; 516 } 517 } 518 face = m_FangSong; 519 } else if (face.Find("SimSun") >= 0 || face.Find("\xcb\xce") >= 0) { 520 face = "SimSun"; 521 } else if (face.Find("SimHei") >= 0 || face.Find("\xba\xda") >= 0) { 522 face = "SimHei"; 523 } else if (!(picth_family & FF_ROMAN) && weight > 550) { 524 face = "SimHei"; 525 } else { 526 face = "SimSun"; 527 } 528 } 529 530 void CFX_Win32FontInfo::GetJapanesePreference(CFX_ByteString& face, 531 int weight, 532 int picth_family) { 533 if (face.Find("Gothic") >= 0 || 534 face.Find("\x83\x53\x83\x56\x83\x62\x83\x4e") >= 0) { 535 if (face.Find("PGothic") >= 0 || 536 face.Find("\x82\x6f\x83\x53\x83\x56\x83\x62\x83\x4e") >= 0) { 537 face = "MS PGothic"; 538 } else if (face.Find("UI Gothic") >= 0) { 539 face = "MS UI Gothic"; 540 } else { 541 if (face.Find("HGSGothicM") >= 0 || face.Find("HGMaruGothicMPRO") >= 0) { 542 face = "MS PGothic"; 543 } else { 544 face = "MS Gothic"; 545 } 546 } 547 return; 548 } 549 if (face.Find("Mincho") >= 0 || face.Find("\x96\xbe\x92\xa9") >= 0) { 550 if (face.Find("PMincho") >= 0 || 551 face.Find("\x82\x6f\x96\xbe\x92\xa9") >= 0) { 552 face = "MS PMincho"; 553 } else { 554 face = "MS Mincho"; 555 } 556 return; 557 } 558 if (GetSubFontName(&face)) 559 return; 560 561 if (!(picth_family & FF_ROMAN) && weight > 400) { 562 face = "MS PGothic"; 563 } else { 564 face = "MS PMincho"; 565 } 566 } 567 568 void* CFX_Win32FontInfo::MapFont(int weight, 569 bool bItalic, 570 int charset, 571 int pitch_family, 572 const FX_CHAR* cstr_face, 573 int& iExact) { 574 CFX_ByteString face = cstr_face; 575 int iBaseFont; 576 for (iBaseFont = 0; iBaseFont < 12; iBaseFont++) 577 if (face == CFX_ByteStringC(g_Base14Substs[iBaseFont].m_pName)) { 578 face = g_Base14Substs[iBaseFont].m_pWinName; 579 weight = g_Base14Substs[iBaseFont].m_bBold ? FW_BOLD : FW_NORMAL; 580 bItalic = g_Base14Substs[iBaseFont].m_bItalic; 581 iExact = true; 582 break; 583 } 584 if (charset == FXFONT_ANSI_CHARSET || charset == FXFONT_SYMBOL_CHARSET) { 585 charset = FXFONT_DEFAULT_CHARSET; 586 } 587 int subst_pitch_family = pitch_family; 588 switch (charset) { 589 case FXFONT_SHIFTJIS_CHARSET: 590 subst_pitch_family = FF_ROMAN; 591 break; 592 case FXFONT_CHINESEBIG5_CHARSET: 593 case FXFONT_HANGUL_CHARSET: 594 case FXFONT_GB2312_CHARSET: 595 subst_pitch_family = 0; 596 break; 597 } 598 HFONT hFont = 599 ::CreateFontA(-10, 0, 0, 0, weight, bItalic, 0, 0, charset, 600 OUT_TT_ONLY_PRECIS, 0, 0, subst_pitch_family, face.c_str()); 601 char facebuf[100]; 602 HFONT hOldFont = (HFONT)::SelectObject(m_hDC, hFont); 603 ::GetTextFaceA(m_hDC, 100, facebuf); 604 ::SelectObject(m_hDC, hOldFont); 605 if (face.EqualNoCase(facebuf)) 606 return hFont; 607 608 CFX_WideString wsFace = CFX_WideString::FromLocal(facebuf); 609 for (size_t i = 0; i < FX_ArraySize(g_VariantNames); ++i) { 610 if (face != g_VariantNames[i].m_pFaceName) 611 continue; 612 613 const unsigned short* pName = reinterpret_cast<const unsigned short*>( 614 g_VariantNames[i].m_pVariantName); 615 FX_STRSIZE len = CFX_WideString::WStringLength(pName); 616 CFX_WideString wsName = CFX_WideString::FromUTF16LE(pName, len); 617 if (wsFace == wsName) 618 return hFont; 619 } 620 ::DeleteObject(hFont); 621 if (charset == FXFONT_DEFAULT_CHARSET) 622 return nullptr; 623 624 switch (charset) { 625 case FXFONT_SHIFTJIS_CHARSET: 626 GetJapanesePreference(face, weight, pitch_family); 627 break; 628 case FXFONT_GB2312_CHARSET: 629 GetGBPreference(face, weight, pitch_family); 630 break; 631 case FXFONT_HANGUL_CHARSET: 632 face = "Gulim"; 633 break; 634 case FXFONT_CHINESEBIG5_CHARSET: 635 if (face.Find("MSung") >= 0) { 636 face = "MingLiU"; 637 } else { 638 face = "PMingLiU"; 639 } 640 break; 641 } 642 hFont = 643 ::CreateFontA(-10, 0, 0, 0, weight, bItalic, 0, 0, charset, 644 OUT_TT_ONLY_PRECIS, 0, 0, subst_pitch_family, face.c_str()); 645 return hFont; 646 } 647 648 void CFX_Win32FontInfo::DeleteFont(void* hFont) { 649 ::DeleteObject(hFont); 650 } 651 652 uint32_t CFX_Win32FontInfo::GetFontData(void* hFont, 653 uint32_t table, 654 uint8_t* buffer, 655 uint32_t size) { 656 HFONT hOldFont = (HFONT)::SelectObject(m_hDC, (HFONT)hFont); 657 table = FXDWORD_GET_MSBFIRST(reinterpret_cast<uint8_t*>(&table)); 658 size = ::GetFontData(m_hDC, table, 0, buffer, size); 659 ::SelectObject(m_hDC, hOldFont); 660 if (size == GDI_ERROR) { 661 return 0; 662 } 663 return size; 664 } 665 666 bool CFX_Win32FontInfo::GetFaceName(void* hFont, CFX_ByteString& name) { 667 char facebuf[100]; 668 HFONT hOldFont = (HFONT)::SelectObject(m_hDC, (HFONT)hFont); 669 int ret = ::GetTextFaceA(m_hDC, 100, facebuf); 670 ::SelectObject(m_hDC, hOldFont); 671 if (ret == 0) { 672 return false; 673 } 674 name = facebuf; 675 return true; 676 } 677 678 bool CFX_Win32FontInfo::GetFontCharset(void* hFont, int& charset) { 679 TEXTMETRIC tm; 680 HFONT hOldFont = (HFONT)::SelectObject(m_hDC, (HFONT)hFont); 681 ::GetTextMetrics(m_hDC, &tm); 682 ::SelectObject(m_hDC, hOldFont); 683 charset = tm.tmCharSet; 684 return true; 685 } 686 687 } // namespace 688 689 int g_pdfium_print_postscript_level = 0; 690 691 std::unique_ptr<IFX_SystemFontInfo> IFX_SystemFontInfo::CreateDefault( 692 const char** pUnused) { 693 if (IsGDIEnabled()) 694 return std::unique_ptr<IFX_SystemFontInfo>(new CFX_Win32FontInfo); 695 696 // Select the fallback font information class if GDI is disabled. 697 CFX_Win32FallbackFontInfo* pInfoFallback = new CFX_Win32FallbackFontInfo; 698 // Construct the font path manually, SHGetKnownFolderPath won't work under 699 // a restrictive sandbox. 700 CHAR windows_path[MAX_PATH] = {}; 701 DWORD path_len = ::GetWindowsDirectoryA(windows_path, MAX_PATH); 702 if (path_len > 0 && path_len < MAX_PATH) { 703 CFX_ByteString fonts_path(windows_path); 704 fonts_path += "\\Fonts"; 705 pInfoFallback->AddPath(fonts_path.AsStringC()); 706 } 707 return std::unique_ptr<IFX_SystemFontInfo>(pInfoFallback); 708 } 709 710 void CFX_GEModule::InitPlatform() { 711 CWin32Platform* pPlatformData = new CWin32Platform; 712 OSVERSIONINFO ver; 713 ver.dwOSVersionInfoSize = sizeof(ver); 714 GetVersionEx(&ver); 715 pPlatformData->m_bHalfTone = ver.dwMajorVersion >= 5; 716 if (IsGDIEnabled()) 717 pPlatformData->m_GdiplusExt.Load(); 718 m_pPlatformData = pPlatformData; 719 m_pFontMgr->SetSystemFontInfo(IFX_SystemFontInfo::CreateDefault(nullptr)); 720 } 721 722 void CFX_GEModule::DestroyPlatform() { 723 delete (CWin32Platform*)m_pPlatformData; 724 m_pPlatformData = nullptr; 725 } 726 727 CGdiDeviceDriver::CGdiDeviceDriver(HDC hDC, int device_class) { 728 m_hDC = hDC; 729 m_DeviceClass = device_class; 730 CWin32Platform* pPlatform = 731 (CWin32Platform*)CFX_GEModule::Get()->GetPlatformData(); 732 SetStretchBltMode(hDC, pPlatform->m_bHalfTone ? HALFTONE : COLORONCOLOR); 733 DWORD obj_type = GetObjectType(m_hDC); 734 m_bMetafileDCType = obj_type == OBJ_ENHMETADC || obj_type == OBJ_ENHMETAFILE; 735 if (obj_type == OBJ_MEMDC) { 736 HBITMAP hBitmap = CreateBitmap(1, 1, 1, 1, nullptr); 737 hBitmap = (HBITMAP)SelectObject(m_hDC, hBitmap); 738 BITMAP bitmap; 739 GetObject(hBitmap, sizeof bitmap, &bitmap); 740 m_nBitsPerPixel = bitmap.bmBitsPixel; 741 m_Width = bitmap.bmWidth; 742 m_Height = abs(bitmap.bmHeight); 743 hBitmap = (HBITMAP)SelectObject(m_hDC, hBitmap); 744 DeleteObject(hBitmap); 745 } else { 746 m_nBitsPerPixel = ::GetDeviceCaps(m_hDC, BITSPIXEL); 747 m_Width = ::GetDeviceCaps(m_hDC, HORZRES); 748 m_Height = ::GetDeviceCaps(m_hDC, VERTRES); 749 } 750 if (m_DeviceClass != FXDC_DISPLAY) { 751 m_RenderCaps = FXRC_BIT_MASK; 752 } else { 753 m_RenderCaps = FXRC_GET_BITS | FXRC_BIT_MASK; 754 } 755 } 756 757 CGdiDeviceDriver::~CGdiDeviceDriver() {} 758 759 int CGdiDeviceDriver::GetDeviceCaps(int caps_id) const { 760 switch (caps_id) { 761 case FXDC_DEVICE_CLASS: 762 return m_DeviceClass; 763 case FXDC_PIXEL_WIDTH: 764 return m_Width; 765 case FXDC_PIXEL_HEIGHT: 766 return m_Height; 767 case FXDC_BITS_PIXEL: 768 return m_nBitsPerPixel; 769 case FXDC_RENDER_CAPS: 770 return m_RenderCaps; 771 } 772 return 0; 773 } 774 775 void CGdiDeviceDriver::SaveState() { 776 SaveDC(m_hDC); 777 } 778 779 void CGdiDeviceDriver::RestoreState(bool bKeepSaved) { 780 RestoreDC(m_hDC, -1); 781 if (bKeepSaved) 782 SaveDC(m_hDC); 783 } 784 785 bool CGdiDeviceDriver::GDI_SetDIBits(CFX_DIBitmap* pBitmap1, 786 const FX_RECT* pSrcRect, 787 int left, 788 int top) { 789 if (m_DeviceClass == FXDC_PRINTER) { 790 std::unique_ptr<CFX_DIBitmap> pBitmap = pBitmap1->FlipImage(false, true); 791 if (!pBitmap) 792 return false; 793 794 if (pBitmap->IsCmykImage() && !pBitmap->ConvertFormat(FXDIB_Rgb)) 795 return false; 796 797 int width = pSrcRect->Width(), height = pSrcRect->Height(); 798 LPBYTE pBuffer = pBitmap->GetBuffer(); 799 CFX_ByteString info = CFX_WindowsDIB::GetBitmapInfo(pBitmap.get()); 800 ((BITMAPINFOHEADER*)info.c_str())->biHeight *= -1; 801 FX_RECT dst_rect(0, 0, width, height); 802 dst_rect.Intersect(0, 0, pBitmap->GetWidth(), pBitmap->GetHeight()); 803 int dst_width = dst_rect.Width(); 804 int dst_height = dst_rect.Height(); 805 ::StretchDIBits(m_hDC, left, top, dst_width, dst_height, 0, 0, dst_width, 806 dst_height, pBuffer, (BITMAPINFO*)info.c_str(), 807 DIB_RGB_COLORS, SRCCOPY); 808 } else { 809 CFX_MaybeOwned<CFX_DIBitmap> pBitmap(pBitmap1); 810 if (pBitmap->IsCmykImage()) { 811 pBitmap = pBitmap->CloneConvert(FXDIB_Rgb).release(); 812 if (!pBitmap) 813 return false; 814 } 815 int width = pSrcRect->Width(), height = pSrcRect->Height(); 816 LPBYTE pBuffer = pBitmap->GetBuffer(); 817 CFX_ByteString info = CFX_WindowsDIB::GetBitmapInfo(pBitmap.Get()); 818 ::SetDIBitsToDevice(m_hDC, left, top, width, height, pSrcRect->left, 819 pBitmap->GetHeight() - pSrcRect->bottom, 0, 820 pBitmap->GetHeight(), pBuffer, 821 (BITMAPINFO*)info.c_str(), DIB_RGB_COLORS); 822 } 823 return true; 824 } 825 826 bool CGdiDeviceDriver::GDI_StretchDIBits(CFX_DIBitmap* pBitmap1, 827 int dest_left, 828 int dest_top, 829 int dest_width, 830 int dest_height, 831 uint32_t flags) { 832 CFX_DIBitmap* pBitmap = pBitmap1; 833 if (!pBitmap || dest_width == 0 || dest_height == 0) 834 return false; 835 836 if (pBitmap->IsCmykImage() && !pBitmap->ConvertFormat(FXDIB_Rgb)) 837 return false; 838 839 CFX_ByteString info = CFX_WindowsDIB::GetBitmapInfo(pBitmap); 840 if ((int64_t)abs(dest_width) * abs(dest_height) < 841 (int64_t)pBitmap1->GetWidth() * pBitmap1->GetHeight() * 4 || 842 (flags & FXDIB_INTERPOL) || (flags & FXDIB_BICUBIC_INTERPOL)) { 843 SetStretchBltMode(m_hDC, HALFTONE); 844 } else { 845 SetStretchBltMode(m_hDC, COLORONCOLOR); 846 } 847 CFX_MaybeOwned<CFX_DIBitmap> pToStrechBitmap(pBitmap); 848 if (m_DeviceClass == FXDC_PRINTER && 849 ((int64_t)pBitmap->GetWidth() * pBitmap->GetHeight() > 850 (int64_t)abs(dest_width) * abs(dest_height))) { 851 pToStrechBitmap = pBitmap->StretchTo(dest_width, dest_height); 852 } 853 CFX_ByteString toStrechBitmapInfo = 854 CFX_WindowsDIB::GetBitmapInfo(pToStrechBitmap.Get()); 855 ::StretchDIBits(m_hDC, dest_left, dest_top, dest_width, dest_height, 0, 0, 856 pToStrechBitmap->GetWidth(), pToStrechBitmap->GetHeight(), 857 pToStrechBitmap->GetBuffer(), 858 (BITMAPINFO*)toStrechBitmapInfo.c_str(), DIB_RGB_COLORS, 859 SRCCOPY); 860 return true; 861 } 862 863 bool CGdiDeviceDriver::GDI_StretchBitMask(CFX_DIBitmap* pBitmap1, 864 int dest_left, 865 int dest_top, 866 int dest_width, 867 int dest_height, 868 uint32_t bitmap_color, 869 uint32_t flags) { 870 CFX_DIBitmap* pBitmap = pBitmap1; 871 if (!pBitmap || dest_width == 0 || dest_height == 0) 872 return false; 873 874 int width = pBitmap->GetWidth(), height = pBitmap->GetHeight(); 875 struct { 876 BITMAPINFOHEADER bmiHeader; 877 uint32_t bmiColors[2]; 878 } bmi; 879 FXSYS_memset(&bmi.bmiHeader, 0, sizeof(BITMAPINFOHEADER)); 880 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); 881 bmi.bmiHeader.biBitCount = 1; 882 bmi.bmiHeader.biCompression = BI_RGB; 883 bmi.bmiHeader.biHeight = -height; 884 bmi.bmiHeader.biPlanes = 1; 885 bmi.bmiHeader.biWidth = width; 886 if (m_nBitsPerPixel != 1) { 887 SetStretchBltMode(m_hDC, HALFTONE); 888 } 889 bmi.bmiColors[0] = 0xffffff; 890 bmi.bmiColors[1] = 0; 891 892 HBRUSH hPattern = CreateSolidBrush(bitmap_color & 0xffffff); 893 HBRUSH hOld = (HBRUSH)SelectObject(m_hDC, hPattern); 894 895 // In PDF, when image mask is 1, use device bitmap; when mask is 0, use brush 896 // bitmap. 897 // A complete list of the boolen operations is as follows: 898 899 /* P(bitmap_color) S(ImageMask) D(DeviceBitmap) Result 900 * 0 0 0 0 901 * 0 0 1 0 902 * 0 1 0 0 903 * 0 1 1 1 904 * 1 0 0 1 905 * 1 0 1 1 906 * 1 1 0 0 907 * 1 1 1 1 908 */ 909 // The boolen codes is B8. Based on 910 // http://msdn.microsoft.com/en-us/library/aa932106.aspx, the ROP3 code is 911 // 0xB8074A 912 913 ::StretchDIBits(m_hDC, dest_left, dest_top, dest_width, dest_height, 0, 0, 914 width, height, pBitmap->GetBuffer(), (BITMAPINFO*)&bmi, 915 DIB_RGB_COLORS, 0xB8074A); 916 917 SelectObject(m_hDC, hOld); 918 DeleteObject(hPattern); 919 920 return true; 921 } 922 923 bool CGdiDeviceDriver::GetClipBox(FX_RECT* pRect) { 924 return !!(::GetClipBox(m_hDC, (RECT*)pRect)); 925 } 926 927 void* CGdiDeviceDriver::GetPlatformSurface() const { 928 return (void*)m_hDC; 929 } 930 931 void CGdiDeviceDriver::DrawLine(FX_FLOAT x1, 932 FX_FLOAT y1, 933 FX_FLOAT x2, 934 FX_FLOAT y2) { 935 if (!m_bMetafileDCType) { // EMF drawing is not bound to the DC. 936 int startOutOfBoundsFlag = (x1 < 0) | ((x1 > m_Width) << 1) | 937 ((y1 < 0) << 2) | ((y1 > m_Height) << 3); 938 int endOutOfBoundsFlag = (x2 < 0) | ((x2 > m_Width) << 1) | 939 ((y2 < 0) << 2) | ((y2 > m_Height) << 3); 940 if (startOutOfBoundsFlag & endOutOfBoundsFlag) 941 return; 942 943 if (startOutOfBoundsFlag || endOutOfBoundsFlag) { 944 FX_FLOAT x[2]; 945 FX_FLOAT y[2]; 946 int np; 947 #ifdef _SKIA_SUPPORT_ 948 // TODO(caryclark) temporary replacement of antigrain in line function 949 // to permit removing antigrain altogether 950 rect_base rect = {0.0f, 0.0f, (FX_FLOAT)(m_Width), (FX_FLOAT)(m_Height)}; 951 np = clip_liang_barsky(x1, y1, x2, y2, rect, x, y); 952 #else 953 agg::rect_base<FX_FLOAT> rect(0.0f, 0.0f, (FX_FLOAT)(m_Width), 954 (FX_FLOAT)(m_Height)); 955 np = agg::clip_liang_barsky<FX_FLOAT>(x1, y1, x2, y2, rect, x, y); 956 #endif 957 if (np == 0) 958 return; 959 960 if (np == 1) { 961 x2 = x[0]; 962 y2 = y[0]; 963 } else { 964 ASSERT(np == 2); 965 x1 = x[0]; 966 y1 = y[0]; 967 x2 = x[1]; 968 y2 = y[1]; 969 } 970 } 971 } 972 973 MoveToEx(m_hDC, FXSYS_round(x1), FXSYS_round(y1), nullptr); 974 LineTo(m_hDC, FXSYS_round(x2), FXSYS_round(y2)); 975 } 976 977 bool CGdiDeviceDriver::DrawPath(const CFX_PathData* pPathData, 978 const CFX_Matrix* pMatrix, 979 const CFX_GraphStateData* pGraphState, 980 uint32_t fill_color, 981 uint32_t stroke_color, 982 int fill_mode, 983 int blend_type) { 984 if (blend_type != FXDIB_BLEND_NORMAL) 985 return false; 986 987 CWin32Platform* pPlatform = 988 (CWin32Platform*)CFX_GEModule::Get()->GetPlatformData(); 989 if (!(pGraphState || stroke_color == 0) && 990 !pPlatform->m_GdiplusExt.IsAvailable()) { 991 CFX_FloatRect bbox_f = pPathData->GetBoundingBox(); 992 if (pMatrix) 993 pMatrix->TransformRect(bbox_f); 994 995 FX_RECT bbox = bbox_f.GetInnerRect(); 996 if (bbox.Width() <= 0) { 997 return DrawCosmeticLine( 998 (FX_FLOAT)(bbox.left), (FX_FLOAT)(bbox.top), (FX_FLOAT)(bbox.left), 999 (FX_FLOAT)(bbox.bottom + 1), fill_color, FXDIB_BLEND_NORMAL); 1000 } 1001 if (bbox.Height() <= 0) { 1002 return DrawCosmeticLine((FX_FLOAT)(bbox.left), (FX_FLOAT)(bbox.top), 1003 (FX_FLOAT)(bbox.right + 1), (FX_FLOAT)(bbox.top), 1004 fill_color, FXDIB_BLEND_NORMAL); 1005 } 1006 } 1007 int fill_alpha = FXARGB_A(fill_color); 1008 int stroke_alpha = FXARGB_A(stroke_color); 1009 bool bDrawAlpha = (fill_alpha > 0 && fill_alpha < 255) || 1010 (stroke_alpha > 0 && stroke_alpha < 255 && pGraphState); 1011 if (!pPlatform->m_GdiplusExt.IsAvailable() && bDrawAlpha) 1012 return false; 1013 1014 if (pPlatform->m_GdiplusExt.IsAvailable()) { 1015 if (bDrawAlpha || 1016 ((m_DeviceClass != FXDC_PRINTER && !(fill_mode & FXFILL_FULLCOVER)) || 1017 (pGraphState && pGraphState->m_DashCount))) { 1018 if (!((!pMatrix || !pMatrix->WillScale()) && pGraphState && 1019 pGraphState->m_LineWidth == 1.f && 1020 (pPathData->GetPoints().size() == 5 || 1021 pPathData->GetPoints().size() == 4) && 1022 pPathData->IsRect())) { 1023 if (pPlatform->m_GdiplusExt.DrawPath(m_hDC, pPathData, pMatrix, 1024 pGraphState, fill_color, 1025 stroke_color, fill_mode)) { 1026 return true; 1027 } 1028 } 1029 } 1030 } 1031 int old_fill_mode = fill_mode; 1032 fill_mode &= 3; 1033 HPEN hPen = nullptr; 1034 HBRUSH hBrush = nullptr; 1035 if (pGraphState && stroke_alpha) { 1036 SetMiterLimit(m_hDC, pGraphState->m_MiterLimit, nullptr); 1037 hPen = CreatePen(pGraphState, pMatrix, stroke_color); 1038 hPen = (HPEN)SelectObject(m_hDC, hPen); 1039 } 1040 if (fill_mode && fill_alpha) { 1041 SetPolyFillMode(m_hDC, fill_mode); 1042 hBrush = CreateBrush(fill_color); 1043 hBrush = (HBRUSH)SelectObject(m_hDC, hBrush); 1044 } 1045 if (pPathData->GetPoints().size() == 2 && pGraphState && 1046 pGraphState->m_DashCount) { 1047 CFX_PointF pos1 = pPathData->GetPoint(0); 1048 CFX_PointF pos2 = pPathData->GetPoint(1); 1049 if (pMatrix) { 1050 pos1 = pMatrix->Transform(pos1); 1051 pos2 = pMatrix->Transform(pos2); 1052 } 1053 DrawLine(pos1.x, pos1.y, pos2.x, pos2.y); 1054 } else { 1055 SetPathToDC(m_hDC, pPathData, pMatrix); 1056 if (pGraphState && stroke_alpha) { 1057 if (fill_mode && fill_alpha) { 1058 if (old_fill_mode & FX_FILL_TEXT_MODE) { 1059 StrokeAndFillPath(m_hDC); 1060 } else { 1061 FillPath(m_hDC); 1062 SetPathToDC(m_hDC, pPathData, pMatrix); 1063 StrokePath(m_hDC); 1064 } 1065 } else { 1066 StrokePath(m_hDC); 1067 } 1068 } else if (fill_mode && fill_alpha) { 1069 FillPath(m_hDC); 1070 } 1071 } 1072 if (hPen) { 1073 hPen = (HPEN)SelectObject(m_hDC, hPen); 1074 DeleteObject(hPen); 1075 } 1076 if (hBrush) { 1077 hBrush = (HBRUSH)SelectObject(m_hDC, hBrush); 1078 DeleteObject(hBrush); 1079 } 1080 return true; 1081 } 1082 1083 bool CGdiDeviceDriver::FillRectWithBlend(const FX_RECT* pRect, 1084 uint32_t fill_color, 1085 int blend_type) { 1086 if (blend_type != FXDIB_BLEND_NORMAL) 1087 return false; 1088 1089 int alpha; 1090 FX_COLORREF rgb; 1091 ArgbDecode(fill_color, alpha, rgb); 1092 if (alpha == 0) 1093 return true; 1094 1095 if (alpha < 255) 1096 return false; 1097 1098 HBRUSH hBrush = CreateSolidBrush(rgb); 1099 ::FillRect(m_hDC, (RECT*)pRect, hBrush); 1100 DeleteObject(hBrush); 1101 return true; 1102 } 1103 1104 bool CGdiDeviceDriver::SetClip_PathFill(const CFX_PathData* pPathData, 1105 const CFX_Matrix* pMatrix, 1106 int fill_mode) { 1107 if (pPathData->GetPoints().size() == 5) { 1108 CFX_FloatRect rectf; 1109 if (pPathData->IsRect(pMatrix, &rectf)) { 1110 FX_RECT rect = rectf.GetOuterRect(); 1111 IntersectClipRect(m_hDC, rect.left, rect.top, rect.right, rect.bottom); 1112 return true; 1113 } 1114 } 1115 SetPathToDC(m_hDC, pPathData, pMatrix); 1116 SetPolyFillMode(m_hDC, fill_mode & 3); 1117 SelectClipPath(m_hDC, RGN_AND); 1118 return true; 1119 } 1120 1121 bool CGdiDeviceDriver::SetClip_PathStroke( 1122 const CFX_PathData* pPathData, 1123 const CFX_Matrix* pMatrix, 1124 const CFX_GraphStateData* pGraphState) { 1125 HPEN hPen = CreatePen(pGraphState, pMatrix, 0xff000000); 1126 hPen = (HPEN)SelectObject(m_hDC, hPen); 1127 SetPathToDC(m_hDC, pPathData, pMatrix); 1128 WidenPath(m_hDC); 1129 SetPolyFillMode(m_hDC, WINDING); 1130 bool ret = !!SelectClipPath(m_hDC, RGN_AND); 1131 hPen = (HPEN)SelectObject(m_hDC, hPen); 1132 DeleteObject(hPen); 1133 return ret; 1134 } 1135 1136 bool CGdiDeviceDriver::DrawCosmeticLine(FX_FLOAT x1, 1137 FX_FLOAT y1, 1138 FX_FLOAT x2, 1139 FX_FLOAT y2, 1140 uint32_t color, 1141 int blend_type) { 1142 if (blend_type != FXDIB_BLEND_NORMAL) 1143 return false; 1144 1145 int a; 1146 FX_COLORREF rgb; 1147 ArgbDecode(color, a, rgb); 1148 if (a == 0) 1149 return true; 1150 1151 HPEN hPen = CreatePen(PS_SOLID, 1, rgb); 1152 hPen = (HPEN)SelectObject(m_hDC, hPen); 1153 MoveToEx(m_hDC, FXSYS_round(x1), FXSYS_round(y1), nullptr); 1154 LineTo(m_hDC, FXSYS_round(x2), FXSYS_round(y2)); 1155 hPen = (HPEN)SelectObject(m_hDC, hPen); 1156 DeleteObject(hPen); 1157 return true; 1158 } 1159 1160 CGdiDisplayDriver::CGdiDisplayDriver(HDC hDC) 1161 : CGdiDeviceDriver(hDC, FXDC_DISPLAY) { 1162 CWin32Platform* pPlatform = 1163 (CWin32Platform*)CFX_GEModule::Get()->GetPlatformData(); 1164 if (pPlatform->m_GdiplusExt.IsAvailable()) { 1165 m_RenderCaps |= FXRC_ALPHA_PATH | FXRC_ALPHA_IMAGE; 1166 } 1167 } 1168 1169 CGdiDisplayDriver::~CGdiDisplayDriver() {} 1170 1171 bool CGdiDisplayDriver::GetDIBits(CFX_DIBitmap* pBitmap, int left, int top) { 1172 bool ret = false; 1173 int width = pBitmap->GetWidth(); 1174 int height = pBitmap->GetHeight(); 1175 HBITMAP hbmp = CreateCompatibleBitmap(m_hDC, width, height); 1176 HDC hDCMemory = CreateCompatibleDC(m_hDC); 1177 HBITMAP holdbmp = (HBITMAP)SelectObject(hDCMemory, hbmp); 1178 BitBlt(hDCMemory, 0, 0, width, height, m_hDC, left, top, SRCCOPY); 1179 SelectObject(hDCMemory, holdbmp); 1180 BITMAPINFO bmi; 1181 FXSYS_memset(&bmi, 0, sizeof bmi); 1182 bmi.bmiHeader.biSize = sizeof bmi.bmiHeader; 1183 bmi.bmiHeader.biBitCount = pBitmap->GetBPP(); 1184 bmi.bmiHeader.biHeight = -height; 1185 bmi.bmiHeader.biPlanes = 1; 1186 bmi.bmiHeader.biWidth = width; 1187 if (pBitmap->GetBPP() > 8 && !pBitmap->IsCmykImage()) { 1188 ret = ::GetDIBits(hDCMemory, hbmp, 0, height, pBitmap->GetBuffer(), &bmi, 1189 DIB_RGB_COLORS) == height; 1190 } else { 1191 CFX_DIBitmap bitmap; 1192 if (bitmap.Create(width, height, FXDIB_Rgb)) { 1193 bmi.bmiHeader.biBitCount = 24; 1194 ::GetDIBits(hDCMemory, hbmp, 0, height, bitmap.GetBuffer(), &bmi, 1195 DIB_RGB_COLORS); 1196 ret = pBitmap->TransferBitmap(0, 0, width, height, &bitmap, 0, 0); 1197 } else { 1198 ret = false; 1199 } 1200 } 1201 if (pBitmap->HasAlpha() && ret) 1202 pBitmap->LoadChannel(FXDIB_Alpha, 0xff); 1203 1204 DeleteObject(hbmp); 1205 DeleteObject(hDCMemory); 1206 return ret; 1207 } 1208 1209 bool CGdiDisplayDriver::SetDIBits(const CFX_DIBSource* pSource, 1210 uint32_t color, 1211 const FX_RECT* pSrcRect, 1212 int left, 1213 int top, 1214 int blend_type) { 1215 ASSERT(blend_type == FXDIB_BLEND_NORMAL); 1216 if (pSource->IsAlphaMask()) { 1217 int width = pSource->GetWidth(), height = pSource->GetHeight(); 1218 int alpha = FXARGB_A(color); 1219 if (pSource->GetBPP() != 1 || alpha != 255) { 1220 CFX_DIBitmap background; 1221 if (!background.Create(width, height, FXDIB_Rgb32) || 1222 !GetDIBits(&background, left, top) || 1223 !background.CompositeMask(0, 0, width, height, pSource, color, 0, 0, 1224 FXDIB_BLEND_NORMAL, nullptr, false, 0, 1225 nullptr)) { 1226 return false; 1227 } 1228 FX_RECT src_rect(0, 0, width, height); 1229 return SetDIBits(&background, 0, &src_rect, left, top, 1230 FXDIB_BLEND_NORMAL); 1231 } 1232 FX_RECT clip_rect(left, top, left + pSrcRect->Width(), 1233 top + pSrcRect->Height()); 1234 return StretchDIBits(pSource, color, left - pSrcRect->left, 1235 top - pSrcRect->top, width, height, &clip_rect, 0, 1236 FXDIB_BLEND_NORMAL); 1237 } 1238 int width = pSrcRect->Width(), height = pSrcRect->Height(); 1239 if (pSource->HasAlpha()) { 1240 CFX_DIBitmap bitmap; 1241 if (!bitmap.Create(width, height, FXDIB_Rgb) || 1242 !GetDIBits(&bitmap, left, top) || 1243 !bitmap.CompositeBitmap(0, 0, width, height, pSource, pSrcRect->left, 1244 pSrcRect->top, FXDIB_BLEND_NORMAL, nullptr, 1245 false, nullptr)) { 1246 return false; 1247 } 1248 FX_RECT src_rect(0, 0, width, height); 1249 return SetDIBits(&bitmap, 0, &src_rect, left, top, FXDIB_BLEND_NORMAL); 1250 } 1251 CFX_DIBExtractor temp(pSource); 1252 CFX_DIBitmap* pBitmap = temp.GetBitmap(); 1253 if (!pBitmap) 1254 return false; 1255 return GDI_SetDIBits(pBitmap, pSrcRect, left, top); 1256 } 1257 1258 bool CGdiDisplayDriver::UseFoxitStretchEngine(const CFX_DIBSource* pSource, 1259 uint32_t color, 1260 int dest_left, 1261 int dest_top, 1262 int dest_width, 1263 int dest_height, 1264 const FX_RECT* pClipRect, 1265 int render_flags) { 1266 FX_RECT bitmap_clip = *pClipRect; 1267 if (dest_width < 0) 1268 dest_left += dest_width; 1269 1270 if (dest_height < 0) 1271 dest_top += dest_height; 1272 1273 bitmap_clip.Offset(-dest_left, -dest_top); 1274 std::unique_ptr<CFX_DIBitmap> pStretched( 1275 pSource->StretchTo(dest_width, dest_height, render_flags, &bitmap_clip)); 1276 if (!pStretched) 1277 return true; 1278 1279 FX_RECT src_rect(0, 0, pStretched->GetWidth(), pStretched->GetHeight()); 1280 return SetDIBits(pStretched.get(), color, &src_rect, pClipRect->left, 1281 pClipRect->top, FXDIB_BLEND_NORMAL); 1282 } 1283 1284 bool CGdiDisplayDriver::StretchDIBits(const CFX_DIBSource* pSource, 1285 uint32_t color, 1286 int dest_left, 1287 int dest_top, 1288 int dest_width, 1289 int dest_height, 1290 const FX_RECT* pClipRect, 1291 uint32_t flags, 1292 int blend_type) { 1293 ASSERT(pSource && pClipRect); 1294 if (flags || dest_width > 10000 || dest_width < -10000 || 1295 dest_height > 10000 || dest_height < -10000) { 1296 return UseFoxitStretchEngine(pSource, color, dest_left, dest_top, 1297 dest_width, dest_height, pClipRect, flags); 1298 } 1299 if (pSource->IsAlphaMask()) { 1300 FX_RECT image_rect; 1301 image_rect.left = dest_width > 0 ? dest_left : dest_left + dest_width; 1302 image_rect.right = dest_width > 0 ? dest_left + dest_width : dest_left; 1303 image_rect.top = dest_height > 0 ? dest_top : dest_top + dest_height; 1304 image_rect.bottom = dest_height > 0 ? dest_top + dest_height : dest_top; 1305 FX_RECT clip_rect = image_rect; 1306 clip_rect.Intersect(*pClipRect); 1307 clip_rect.Offset(-image_rect.left, -image_rect.top); 1308 int clip_width = clip_rect.Width(), clip_height = clip_rect.Height(); 1309 std::unique_ptr<CFX_DIBitmap> pStretched( 1310 pSource->StretchTo(dest_width, dest_height, flags, &clip_rect)); 1311 if (!pStretched) 1312 return true; 1313 1314 CFX_DIBitmap background; 1315 if (!background.Create(clip_width, clip_height, FXDIB_Rgb32) || 1316 !GetDIBits(&background, image_rect.left + clip_rect.left, 1317 image_rect.top + clip_rect.top) || 1318 !background.CompositeMask( 1319 0, 0, clip_width, clip_height, pStretched.get(), color, 0, 0, 1320 FXDIB_BLEND_NORMAL, nullptr, false, 0, nullptr)) { 1321 return false; 1322 } 1323 1324 FX_RECT src_rect(0, 0, clip_width, clip_height); 1325 return SetDIBits(&background, 0, &src_rect, 1326 image_rect.left + clip_rect.left, 1327 image_rect.top + clip_rect.top, FXDIB_BLEND_NORMAL); 1328 } 1329 if (pSource->HasAlpha()) { 1330 CWin32Platform* pPlatform = 1331 (CWin32Platform*)CFX_GEModule::Get()->GetPlatformData(); 1332 if (pPlatform->m_GdiplusExt.IsAvailable() && !pSource->IsCmykImage()) { 1333 CFX_DIBExtractor temp(pSource); 1334 CFX_DIBitmap* pBitmap = temp.GetBitmap(); 1335 if (!pBitmap) 1336 return false; 1337 return pPlatform->m_GdiplusExt.StretchDIBits( 1338 m_hDC, pBitmap, dest_left, dest_top, dest_width, dest_height, 1339 pClipRect, flags); 1340 } 1341 return UseFoxitStretchEngine(pSource, color, dest_left, dest_top, 1342 dest_width, dest_height, pClipRect, flags); 1343 } 1344 CFX_DIBExtractor temp(pSource); 1345 CFX_DIBitmap* pBitmap = temp.GetBitmap(); 1346 if (!pBitmap) 1347 return false; 1348 return GDI_StretchDIBits(pBitmap, dest_left, dest_top, dest_width, 1349 dest_height, flags); 1350 } 1351 1352 bool CGdiDisplayDriver::StartDIBits(const CFX_DIBSource* pBitmap, 1353 int bitmap_alpha, 1354 uint32_t color, 1355 const CFX_Matrix* pMatrix, 1356 uint32_t render_flags, 1357 void*& handle, 1358 int blend_type) { 1359 return false; 1360 } 1361 1362 CFX_WindowsDevice::CFX_WindowsDevice(HDC hDC) { 1363 SetDeviceDriver(pdfium::WrapUnique(CreateDriver(hDC))); 1364 } 1365 1366 CFX_WindowsDevice::~CFX_WindowsDevice() {} 1367 1368 HDC CFX_WindowsDevice::GetDC() const { 1369 IFX_RenderDeviceDriver* pRDD = GetDeviceDriver(); 1370 return pRDD ? reinterpret_cast<HDC>(pRDD->GetPlatformSurface()) : nullptr; 1371 } 1372 1373 // static 1374 IFX_RenderDeviceDriver* CFX_WindowsDevice::CreateDriver(HDC hDC) { 1375 int device_type = ::GetDeviceCaps(hDC, TECHNOLOGY); 1376 int obj_type = ::GetObjectType(hDC); 1377 bool use_printer = device_type == DT_RASPRINTER || 1378 device_type == DT_PLOTTER || obj_type == OBJ_ENHMETADC; 1379 1380 if (!use_printer) 1381 return new CGdiDisplayDriver(hDC); 1382 1383 if (g_pdfium_print_postscript_level == 2 || 1384 g_pdfium_print_postscript_level == 3) { 1385 return new CPSPrinterDriver(hDC, g_pdfium_print_postscript_level, false); 1386 } 1387 return new CGdiPrinterDriver(hDC); 1388 } 1389