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 #include "../../../include/fpdfapi/fpdf_render.h" 9 #include "../../../include/fpdfapi/fpdf_pageobj.h" 10 #include "../fpdf_page/pageint.h" 11 #include "render_int.h" 12 extern FX_BOOL IsAvailableMatrix(const CFX_AffineMatrix& matrix); 13 CPDF_Type3Cache::~CPDF_Type3Cache() 14 { 15 FX_POSITION pos = m_SizeMap.GetStartPosition(); 16 CFX_ByteString Key; 17 CPDF_Type3Glyphs* pSizeCache = NULL; 18 while(pos) { 19 pSizeCache = (CPDF_Type3Glyphs*)m_SizeMap.GetNextValue(pos); 20 delete pSizeCache; 21 } 22 m_SizeMap.RemoveAll(); 23 } 24 CFX_GlyphBitmap* CPDF_Type3Cache::LoadGlyph(FX_DWORD charcode, const CFX_AffineMatrix* pMatrix, FX_FLOAT retinaScaleX, FX_FLOAT retinaScaleY) 25 { 26 _CPDF_UniqueKeyGen keygen; 27 keygen.Generate(4, FXSYS_round(pMatrix->a * 10000), FXSYS_round(pMatrix->b * 10000), 28 FXSYS_round(pMatrix->c * 10000), FXSYS_round(pMatrix->d * 10000)); 29 CFX_ByteStringC FaceGlyphsKey(keygen.m_Key, keygen.m_KeyLen); 30 CPDF_Type3Glyphs* pSizeCache = NULL; 31 if(!m_SizeMap.Lookup(FaceGlyphsKey, (void*&)pSizeCache)) { 32 pSizeCache = new CPDF_Type3Glyphs; 33 m_SizeMap.SetAt(FaceGlyphsKey, pSizeCache); 34 } 35 CFX_GlyphBitmap* pGlyphBitmap; 36 if(pSizeCache->m_GlyphMap.Lookup((FX_LPVOID)(FX_UINTPTR)charcode, (void*&)pGlyphBitmap)) { 37 return pGlyphBitmap; 38 } 39 pGlyphBitmap = RenderGlyph(pSizeCache, charcode, pMatrix, retinaScaleX, retinaScaleY); 40 pSizeCache->m_GlyphMap.SetAt((FX_LPVOID)(FX_UINTPTR)charcode, pGlyphBitmap); 41 return pGlyphBitmap; 42 } 43 CPDF_Type3Glyphs::~CPDF_Type3Glyphs() 44 { 45 FX_POSITION pos = m_GlyphMap.GetStartPosition(); 46 FX_LPVOID Key; 47 CFX_GlyphBitmap* pGlyphBitmap; 48 while(pos) { 49 m_GlyphMap.GetNextAssoc(pos, Key, (void*&)pGlyphBitmap); 50 delete pGlyphBitmap; 51 } 52 } 53 static int _AdjustBlue(FX_FLOAT pos, int& count, int blues[]) 54 { 55 FX_FLOAT min_distance = 1000000.0f * 1.0f; 56 int closest_pos = -1; 57 for (int i = 0; i < count; i ++) { 58 FX_FLOAT distance = (FX_FLOAT)FXSYS_fabs(pos - (FX_FLOAT)blues[i]); 59 if (distance < 1.0f * 80.0f / 100.0f && distance < min_distance) { 60 min_distance = distance; 61 closest_pos = i; 62 } 63 } 64 if (closest_pos >= 0) { 65 return blues[closest_pos]; 66 } 67 int new_pos = FXSYS_round(pos); 68 if (count == TYPE3_MAX_BLUES) { 69 return new_pos; 70 } 71 blues[count++] = new_pos; 72 return new_pos; 73 } 74 void CPDF_Type3Glyphs::AdjustBlue(FX_FLOAT top, FX_FLOAT bottom, int& top_line, int& bottom_line) 75 { 76 top_line = _AdjustBlue(top, m_TopBlueCount, m_TopBlue); 77 bottom_line = _AdjustBlue(bottom, m_BottomBlueCount, m_BottomBlue); 78 } 79 static FX_BOOL _IsScanLine1bpp(FX_LPBYTE pBuf, int width) 80 { 81 int size = width / 8; 82 for (int i = 0; i < size; i ++) 83 if (pBuf[i]) { 84 return TRUE; 85 } 86 if (width % 8) 87 if (pBuf[width / 8] & (0xff << (8 - width % 8))) { 88 return TRUE; 89 } 90 return FALSE; 91 } 92 static FX_BOOL _IsScanLine8bpp(FX_LPBYTE pBuf, int width) 93 { 94 for (int i = 0; i < width; i ++) 95 if (pBuf[i] > 0x40) { 96 return TRUE; 97 } 98 return FALSE; 99 } 100 static int _DetectFirstLastScan(const CFX_DIBitmap* pBitmap, FX_BOOL bFirst) 101 { 102 int height = pBitmap->GetHeight(), pitch = pBitmap->GetPitch(), width = pBitmap->GetWidth(); 103 int bpp = pBitmap->GetBPP(); 104 if (bpp > 8) { 105 width *= bpp / 8; 106 } 107 FX_LPBYTE pBuf = pBitmap->GetBuffer(); 108 int line = bFirst ? 0 : height - 1; 109 int line_step = bFirst ? 1 : -1; 110 int line_end = bFirst ? height : -1; 111 while (line != line_end) { 112 if (bpp == 1) { 113 if (_IsScanLine1bpp(pBuf + line * pitch, width)) { 114 return line; 115 } 116 } else { 117 if (_IsScanLine8bpp(pBuf + line * pitch, width)) { 118 return line; 119 } 120 } 121 line += line_step; 122 } 123 return -1; 124 } 125 CFX_GlyphBitmap* CPDF_Type3Cache::RenderGlyph(CPDF_Type3Glyphs* pSize, FX_DWORD charcode, const CFX_AffineMatrix* pMatrix, FX_FLOAT retinaScaleX, FX_FLOAT retinaScaleY) 126 { 127 CPDF_Type3Char* pChar = m_pFont->LoadChar(charcode); 128 if (pChar == NULL || pChar->m_pBitmap == NULL) { 129 return NULL; 130 } 131 CFX_DIBitmap* pBitmap = pChar->m_pBitmap; 132 CFX_AffineMatrix image_matrix, text_matrix; 133 image_matrix = pChar->m_ImageMatrix; 134 text_matrix.Set(pMatrix->a, pMatrix->b, pMatrix->c, pMatrix->d, 0, 0); 135 image_matrix.Concat(text_matrix); 136 CFX_DIBitmap* pResBitmap = NULL; 137 int left, top; 138 if (FXSYS_fabs(image_matrix.b) < FXSYS_fabs(image_matrix.a) / 100 && FXSYS_fabs(image_matrix.c) < FXSYS_fabs(image_matrix.d) / 100) { 139 int top_line, bottom_line; 140 top_line = _DetectFirstLastScan(pBitmap, TRUE); 141 bottom_line = _DetectFirstLastScan(pBitmap, FALSE); 142 if (top_line == 0 && bottom_line == pBitmap->GetHeight() - 1) { 143 FX_FLOAT top_y = image_matrix.d + image_matrix.f; 144 FX_FLOAT bottom_y = image_matrix.f; 145 FX_BOOL bFlipped = top_y > bottom_y; 146 if (bFlipped) { 147 FX_FLOAT temp = top_y; 148 top_y = bottom_y; 149 bottom_y = temp; 150 } 151 pSize->AdjustBlue(top_y, bottom_y, top_line, bottom_line); 152 pResBitmap = pBitmap->StretchTo((int)(FXSYS_round(image_matrix.a) * retinaScaleX), (int)((bFlipped ? top_line - bottom_line : bottom_line - top_line) * retinaScaleY)); 153 top = top_line; 154 if (image_matrix.a < 0) { 155 image_matrix.Scale(retinaScaleX, retinaScaleY); 156 left = FXSYS_round(image_matrix.e + image_matrix.a); 157 } else { 158 left = FXSYS_round(image_matrix.e); 159 } 160 } else { 161 } 162 } 163 if (pResBitmap == NULL) { 164 image_matrix.Scale(retinaScaleX, retinaScaleY); 165 pResBitmap = pBitmap->TransformTo(&image_matrix, left, top); 166 } 167 if (pResBitmap == NULL) { 168 return NULL; 169 } 170 CFX_GlyphBitmap* pGlyph = new CFX_GlyphBitmap; 171 pGlyph->m_Left = left; 172 pGlyph->m_Top = -top; 173 pGlyph->m_Bitmap.TakeOver(pResBitmap); 174 delete pResBitmap; 175 return pGlyph; 176 } 177 void _CPDF_UniqueKeyGen::Generate(int count, ...) 178 { 179 va_list argList; 180 va_start(argList, count); 181 for (int i = 0; i < count; i ++) { 182 int p = va_arg(argList, int); 183 ((FX_DWORD*)m_Key)[i] = p; 184 } 185 va_end(argList); 186 m_KeyLen = count * sizeof(FX_DWORD); 187 } 188 FX_BOOL CPDF_RenderStatus::ProcessText(const CPDF_TextObject* textobj, const CFX_AffineMatrix* pObj2Device, CFX_PathData* pClippingPath) 189 { 190 if(textobj->m_nChars == 0) { 191 return TRUE; 192 } 193 int text_render_mode = textobj->m_TextState.GetObject()->m_TextMode; 194 if (text_render_mode == 3) { 195 return TRUE; 196 } 197 CPDF_Font* pFont = textobj->m_TextState.GetFont(); 198 if (pFont->GetFontType() == PDFFONT_TYPE3) { 199 return ProcessType3Text(textobj, pObj2Device); 200 } 201 FX_BOOL bFill = FALSE, bStroke = FALSE, bClip = FALSE; 202 if (pClippingPath) { 203 bClip = TRUE; 204 } else { 205 switch (text_render_mode) { 206 case 0: 207 case 4: 208 bFill = TRUE; 209 break; 210 case 1: 211 case 5: 212 if (pFont->GetFace() == NULL && !(pFont->GetSubstFont()->m_SubstFlags & FXFONT_SUBST_GLYPHPATH)) { 213 bFill = TRUE; 214 } else { 215 bStroke = TRUE; 216 } 217 break; 218 case 2: 219 case 6: 220 if (pFont->GetFace() == NULL && !(pFont->GetSubstFont()->m_SubstFlags & FXFONT_SUBST_GLYPHPATH)) { 221 bFill = TRUE; 222 } else { 223 bFill = bStroke = TRUE; 224 } 225 break; 226 case 3: 227 case 7: 228 return TRUE; 229 default: 230 bFill = TRUE; 231 } 232 } 233 FX_ARGB stroke_argb = 0, fill_argb = 0; 234 FX_BOOL bPattern = FALSE; 235 if (bStroke) { 236 if (textobj->m_ColorState.GetStrokeColor()->IsPattern()) { 237 bPattern = TRUE; 238 } else { 239 stroke_argb = GetStrokeArgb(textobj); 240 } 241 } 242 if (bFill) { 243 if (textobj->m_ColorState.GetFillColor()->IsPattern()) { 244 bPattern = TRUE; 245 } else { 246 fill_argb = GetFillArgb(textobj); 247 } 248 } 249 CFX_AffineMatrix text_matrix; 250 textobj->GetTextMatrix(&text_matrix); 251 if(IsAvailableMatrix(text_matrix) == FALSE) { 252 return TRUE; 253 } 254 FX_FLOAT font_size = textobj->m_TextState.GetFontSize(); 255 if (bPattern) { 256 DrawTextPathWithPattern(textobj, pObj2Device, pFont, font_size, &text_matrix, bFill, bStroke); 257 return TRUE; 258 } 259 if (bClip || bStroke) { 260 const CFX_AffineMatrix* pDeviceMatrix = pObj2Device; 261 CFX_AffineMatrix device_matrix; 262 if (bStroke) { 263 const FX_FLOAT* pCTM = textobj->m_TextState.GetObject()->m_CTM; 264 if (pCTM[0] != 1.0f || pCTM[3] != 1.0f) { 265 CFX_AffineMatrix ctm(pCTM[0], pCTM[1], pCTM[2], pCTM[3], 0, 0); 266 text_matrix.ConcatInverse(ctm); 267 device_matrix.Copy(ctm); 268 device_matrix.Concat(*pObj2Device); 269 pDeviceMatrix = &device_matrix; 270 } 271 } 272 int flag = 0; 273 if (bStroke && bFill) { 274 flag |= FX_FILL_STROKE; 275 flag |= FX_STROKE_TEXT_MODE; 276 } 277 const CPDF_GeneralStateData* pGeneralData = ((CPDF_PageObject*)textobj)->m_GeneralState; 278 if (pGeneralData && pGeneralData->m_StrokeAdjust) { 279 flag |= FX_STROKE_ADJUST; 280 } 281 if (m_Options.m_Flags & RENDER_NOTEXTSMOOTH) { 282 flag |= FXFILL_NOPATHSMOOTH; 283 } 284 return CPDF_TextRenderer::DrawTextPath(m_pDevice, textobj->m_nChars, textobj->m_pCharCodes, textobj->m_pCharPos, pFont, font_size, 285 &text_matrix, pDeviceMatrix, textobj->m_GraphState, fill_argb, stroke_argb, pClippingPath, flag); 286 } 287 text_matrix.Concat(*pObj2Device); 288 return CPDF_TextRenderer::DrawNormalText(m_pDevice, textobj->m_nChars, textobj->m_pCharCodes, textobj->m_pCharPos, pFont, font_size, 289 &text_matrix, fill_argb, &m_Options); 290 } 291 CPDF_Type3Cache* CPDF_RenderStatus::GetCachedType3(CPDF_Type3Font* pFont) 292 { 293 if (pFont->m_pDocument == NULL) { 294 return NULL; 295 } 296 pFont->m_pDocument->GetPageData()->GetFont(pFont->GetFontDict(), FALSE); 297 return pFont->m_pDocument->GetRenderData()->GetCachedType3(pFont); 298 } 299 static void ReleaseCachedType3(CPDF_Type3Font* pFont) 300 { 301 if (pFont->m_pDocument == NULL) { 302 return; 303 } 304 pFont->m_pDocument->GetRenderData()->ReleaseCachedType3(pFont); 305 pFont->m_pDocument->GetPageData()->ReleaseFont(pFont->GetFontDict()); 306 } 307 FX_BOOL CPDF_Type3Char::LoadBitmap(CPDF_RenderContext* pContext) 308 { 309 if (m_pBitmap != NULL || m_pForm == NULL) { 310 return TRUE; 311 } 312 if (m_pForm->CountObjects() == 1 && !m_bColored) { 313 CPDF_PageObject *pPageObj = m_pForm->GetObjectAt(m_pForm->GetFirstObjectPosition()); 314 if (pPageObj->m_Type == PDFPAGE_IMAGE) { 315 CPDF_ImageObject* pImage = (CPDF_ImageObject*)pPageObj; 316 m_ImageMatrix = pImage->m_Matrix; 317 const CFX_DIBSource* pSource = pImage->m_pImage->LoadDIBSource(); 318 if (pSource) { 319 m_pBitmap = pSource->Clone(); 320 delete pSource; 321 } 322 delete m_pForm; 323 m_pForm = NULL; 324 return TRUE; 325 } 326 if (pPageObj->m_Type == PDFPAGE_INLINES) { 327 CPDF_InlineImages *pInlines = (CPDF_InlineImages *)pPageObj; 328 if (pInlines->m_pStream) { 329 m_ImageMatrix = pInlines->m_Matrices[0]; 330 CPDF_DIBSource dibsrc; 331 if (!dibsrc.Load(pContext->m_pDocument, pInlines->m_pStream, NULL, NULL, NULL, NULL)) { 332 return FALSE; 333 } 334 m_pBitmap = dibsrc.Clone(); 335 delete m_pForm; 336 m_pForm = NULL; 337 return TRUE; 338 } 339 } 340 } 341 return FALSE; 342 } 343 class CPDF_RefType3Cache 344 { 345 public: 346 CPDF_RefType3Cache(CPDF_Type3Font* pType3Font) 347 { 348 m_dwCount = 0; 349 m_pType3Font = pType3Font; 350 } 351 ~CPDF_RefType3Cache() 352 { 353 while(m_dwCount--) { 354 ReleaseCachedType3(m_pType3Font); 355 } 356 } 357 FX_DWORD m_dwCount; 358 CPDF_Type3Font* m_pType3Font; 359 }; 360 FX_BOOL CPDF_RenderStatus::ProcessType3Text(const CPDF_TextObject* textobj, const CFX_AffineMatrix* pObj2Device) 361 { 362 CPDF_Type3Font* pType3Font = textobj->m_TextState.GetFont()->GetType3Font(); 363 for (int j = 0; j < m_Type3FontCache.GetSize(); j++) 364 if ((CPDF_Type3Font*)m_Type3FontCache.GetAt(j) == pType3Font) { 365 return TRUE; 366 } 367 CFX_Matrix dCTM = m_pDevice->GetCTM(); 368 FX_FLOAT sa = FXSYS_fabs(dCTM.a); 369 FX_FLOAT sd = FXSYS_fabs(dCTM.d); 370 CFX_AffineMatrix text_matrix; 371 textobj->GetTextMatrix(&text_matrix); 372 CFX_AffineMatrix char_matrix = pType3Font->GetFontMatrix(); 373 FX_FLOAT font_size = textobj->m_TextState.GetFontSize(); 374 char_matrix.Scale(font_size, font_size); 375 FX_ARGB fill_argb = GetFillArgb(textobj, TRUE); 376 int fill_alpha = FXARGB_A(fill_argb); 377 int device_class = m_pDevice->GetDeviceClass(); 378 FXTEXT_GLYPHPOS* pGlyphAndPos = NULL; 379 if (device_class == FXDC_DISPLAY) { 380 pGlyphAndPos = FX_Alloc(FXTEXT_GLYPHPOS, textobj->m_nChars); 381 } else if (fill_alpha < 255) { 382 return FALSE; 383 } 384 CPDF_RefType3Cache refTypeCache(pType3Font); 385 FX_DWORD *pChars = textobj->m_pCharCodes; 386 if (textobj->m_nChars == 1) { 387 pChars = (FX_DWORD*)(&textobj->m_pCharCodes); 388 } 389 for (int iChar = 0; iChar < textobj->m_nChars; iChar ++) { 390 FX_DWORD charcode = pChars[iChar]; 391 if (charcode == (FX_DWORD) - 1) { 392 continue; 393 } 394 CPDF_Type3Char* pType3Char = pType3Font->LoadChar(charcode); 395 if (pType3Char == NULL) { 396 continue; 397 } 398 CFX_AffineMatrix matrix = char_matrix; 399 matrix.e += iChar ? textobj->m_pCharPos[iChar - 1] : 0; 400 matrix.Concat(text_matrix); 401 matrix.Concat(*pObj2Device); 402 if (!pType3Char->LoadBitmap(m_pContext)) { 403 if (pGlyphAndPos) { 404 for (int i = 0; i < iChar; i ++) { 405 FXTEXT_GLYPHPOS& glyph = pGlyphAndPos[i]; 406 if (glyph.m_pGlyph == NULL) { 407 continue; 408 } 409 m_pDevice->SetBitMask(&glyph.m_pGlyph->m_Bitmap, 410 glyph.m_OriginX + glyph.m_pGlyph->m_Left, 411 glyph.m_OriginY - glyph.m_pGlyph->m_Top, fill_argb); 412 } 413 FX_Free(pGlyphAndPos); 414 pGlyphAndPos = NULL; 415 } 416 CPDF_GraphicStates* pStates = CloneObjStates(textobj, FALSE); 417 CPDF_RenderOptions Options = m_Options; 418 Options.m_Flags |= RENDER_FORCE_HALFTONE | RENDER_RECT_AA; 419 Options.m_Flags &= ~RENDER_FORCE_DOWNSAMPLE; 420 CPDF_Dictionary* pFormResource = NULL; 421 if (pType3Char->m_pForm && pType3Char->m_pForm->m_pFormDict) { 422 pFormResource = pType3Char->m_pForm->m_pFormDict->GetDict(FX_BSTRC("Resources")); 423 } 424 if (fill_alpha == 255) { 425 CPDF_RenderStatus status; 426 status.Initialize(m_pContext, m_pDevice, NULL, NULL, this, pStates, &Options, 427 pType3Char->m_pForm->m_Transparency, m_bDropObjects, pFormResource, FALSE, pType3Char, fill_argb); 428 status.m_Type3FontCache.Append(m_Type3FontCache); 429 status.m_Type3FontCache.Add(pType3Font); 430 m_pDevice->SaveState(); 431 status.RenderObjectList(pType3Char->m_pForm, &matrix); 432 m_pDevice->RestoreState(); 433 } else { 434 CFX_FloatRect rect_f = pType3Char->m_pForm->CalcBoundingBox(); 435 rect_f.Transform(&matrix); 436 FX_RECT rect = rect_f.GetOutterRect(); 437 CFX_FxgeDevice bitmap_device; 438 if (!bitmap_device.Create((int)(rect.Width() * sa), (int)(rect.Height() * sd), FXDIB_Argb)) { 439 return TRUE; 440 } 441 bitmap_device.GetBitmap()->Clear(0); 442 CPDF_RenderStatus status; 443 status.Initialize(m_pContext, &bitmap_device, NULL, NULL, this, pStates, &Options, 444 pType3Char->m_pForm->m_Transparency, m_bDropObjects, pFormResource, FALSE, pType3Char, fill_argb); 445 status.m_Type3FontCache.Append(m_Type3FontCache); 446 status.m_Type3FontCache.Add(pType3Font); 447 matrix.TranslateI(-rect.left, -rect.top); 448 matrix.Scale(sa, sd); 449 status.RenderObjectList(pType3Char->m_pForm, &matrix); 450 m_pDevice->SetDIBits(bitmap_device.GetBitmap(), rect.left, rect.top); 451 } 452 delete pStates; 453 } else if (pType3Char->m_pBitmap) { 454 if (device_class == FXDC_DISPLAY) { 455 CPDF_Type3Cache* pCache = GetCachedType3(pType3Font); 456 refTypeCache.m_dwCount++; 457 CFX_GlyphBitmap* pBitmap = pCache->LoadGlyph(charcode, &matrix, sa, sd); 458 if (pBitmap == NULL) { 459 continue; 460 } 461 int origin_x = FXSYS_round(matrix.e); 462 int origin_y = FXSYS_round(matrix.f); 463 if (pGlyphAndPos) { 464 pGlyphAndPos[iChar].m_pGlyph = pBitmap; 465 pGlyphAndPos[iChar].m_OriginX = origin_x; 466 pGlyphAndPos[iChar].m_OriginY = origin_y; 467 } else { 468 m_pDevice->SetBitMask(&pBitmap->m_Bitmap, origin_x + pBitmap->m_Left, origin_y - pBitmap->m_Top, fill_argb); 469 } 470 } else { 471 CFX_AffineMatrix image_matrix = pType3Char->m_ImageMatrix; 472 image_matrix.Concat(matrix); 473 CPDF_ImageRenderer renderer; 474 if (renderer.Start(this, pType3Char->m_pBitmap, fill_argb, 255, &image_matrix, 0, FALSE)) { 475 renderer.Continue(NULL); 476 } 477 if (!renderer.m_Result) { 478 return FALSE; 479 } 480 } 481 } 482 } 483 if (pGlyphAndPos) { 484 FX_RECT rect = FXGE_GetGlyphsBBox(pGlyphAndPos, textobj->m_nChars, 0, sa, sd); 485 CFX_DIBitmap bitmap; 486 if (!bitmap.Create((int)(rect.Width() * sa), (int)(rect.Height() * sd), FXDIB_8bppMask)) { 487 FX_Free(pGlyphAndPos); 488 return TRUE; 489 } 490 bitmap.Clear(0); 491 for (int iChar = 0; iChar < textobj->m_nChars; iChar ++) { 492 FXTEXT_GLYPHPOS& glyph = pGlyphAndPos[iChar]; 493 if (glyph.m_pGlyph == NULL) { 494 continue; 495 } 496 bitmap.TransferBitmap((int)((glyph.m_OriginX + glyph.m_pGlyph->m_Left - rect.left) * sa), 497 (int)((glyph.m_OriginY - glyph.m_pGlyph->m_Top - rect.top) * sd), 498 glyph.m_pGlyph->m_Bitmap.GetWidth(), glyph.m_pGlyph->m_Bitmap.GetHeight(), 499 &glyph.m_pGlyph->m_Bitmap, 0, 0); 500 } 501 m_pDevice->SetBitMask(&bitmap, rect.left, rect.top, fill_argb); 502 FX_Free(pGlyphAndPos); 503 } 504 return TRUE; 505 } 506 class CPDF_CharPosList 507 { 508 public: 509 CPDF_CharPosList(); 510 ~CPDF_CharPosList(); 511 void Load(int nChars, FX_DWORD* pCharCodes, FX_FLOAT* pCharPos, CPDF_Font* pFont, FX_FLOAT font_size); 512 FXTEXT_CHARPOS* m_pCharPos; 513 FX_DWORD m_nChars; 514 }; 515 FX_FLOAT _CIDTransformToFloat(FX_BYTE ch); 516 CPDF_CharPosList::CPDF_CharPosList() 517 { 518 m_pCharPos = NULL; 519 } 520 CPDF_CharPosList::~CPDF_CharPosList() 521 { 522 if (m_pCharPos) { 523 FX_Free(m_pCharPos); 524 } 525 } 526 void CPDF_CharPosList::Load(int nChars, FX_DWORD* pCharCodes, FX_FLOAT* pCharPos, CPDF_Font* pFont, 527 FX_FLOAT FontSize) 528 { 529 m_pCharPos = FX_Alloc(FXTEXT_CHARPOS, nChars); 530 m_nChars = 0; 531 CPDF_CIDFont* pCIDFont = pFont->GetCIDFont(); 532 FX_BOOL bVertWriting = pCIDFont && pCIDFont->IsVertWriting(); 533 for (int iChar = 0; iChar < nChars; iChar ++) { 534 FX_DWORD CharCode = nChars == 1 ? (FX_DWORD)(FX_UINTPTR)pCharCodes : pCharCodes[iChar]; 535 if (CharCode == (FX_DWORD) - 1) { 536 continue; 537 } 538 FX_BOOL bVert = FALSE; 539 FXTEXT_CHARPOS& charpos = m_pCharPos[m_nChars++]; 540 if (pCIDFont) { 541 charpos.m_bFontStyle = pCIDFont->IsFontStyleFromCharCode(CharCode); 542 } 543 charpos.m_GlyphIndex = pFont->GlyphFromCharCode(CharCode, &bVert); 544 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ 545 charpos.m_ExtGID = pFont->GlyphFromCharCodeExt(CharCode); 546 #endif 547 if (!pFont->IsEmbedded() && pFont->GetFontType() != PDFFONT_CIDFONT) { 548 charpos.m_FontCharWidth = pFont->GetCharWidthF(CharCode); 549 } else { 550 charpos.m_FontCharWidth = 0; 551 } 552 charpos.m_OriginX = iChar ? pCharPos[iChar - 1] : 0; 553 charpos.m_OriginY = 0; 554 charpos.m_bGlyphAdjust = FALSE; 555 if (pCIDFont == NULL) { 556 continue; 557 } 558 FX_WORD CID = pCIDFont->CIDFromCharCode(CharCode); 559 if (bVertWriting) { 560 charpos.m_OriginY = charpos.m_OriginX; 561 charpos.m_OriginX = 0; 562 short vx, vy; 563 pCIDFont->GetVertOrigin(CID, vx, vy); 564 charpos.m_OriginX -= FontSize * vx / 1000; 565 charpos.m_OriginY -= FontSize * vy / 1000; 566 } 567 FX_LPCBYTE pTransform = pCIDFont->GetCIDTransform(CID); 568 if (pTransform && !bVert) { 569 charpos.m_AdjustMatrix[0] = _CIDTransformToFloat(pTransform[0]); 570 charpos.m_AdjustMatrix[2] = _CIDTransformToFloat(pTransform[2]); 571 charpos.m_AdjustMatrix[1] = _CIDTransformToFloat(pTransform[1]); 572 charpos.m_AdjustMatrix[3] = _CIDTransformToFloat(pTransform[3]); 573 charpos.m_OriginX += _CIDTransformToFloat(pTransform[4]) * FontSize; 574 charpos.m_OriginY += _CIDTransformToFloat(pTransform[5]) * FontSize; 575 charpos.m_bGlyphAdjust = TRUE; 576 } 577 } 578 } 579 FX_BOOL CPDF_TextRenderer::DrawTextPath(CFX_RenderDevice* pDevice, int nChars, FX_DWORD* pCharCodes, FX_FLOAT* pCharPos, 580 CPDF_Font* pFont, FX_FLOAT font_size, 581 const CFX_AffineMatrix* pText2User, const CFX_AffineMatrix* pUser2Device, 582 const CFX_GraphStateData* pGraphState, 583 FX_ARGB fill_argb, FX_ARGB stroke_argb, CFX_PathData* pClippingPath, int nFlag) 584 { 585 CFX_FontCache* pCache = pFont->m_pDocument ? pFont->m_pDocument->GetRenderData()->GetFontCache() : NULL; 586 CPDF_CharPosList CharPosList; 587 CharPosList.Load(nChars, pCharCodes, pCharPos, pFont, font_size); 588 return pDevice->DrawTextPath(CharPosList.m_nChars, CharPosList.m_pCharPos, 589 &pFont->m_Font, pCache, font_size, pText2User, pUser2Device, 590 pGraphState, fill_argb, stroke_argb, pClippingPath, nFlag); 591 } 592 void CPDF_TextRenderer::DrawTextString(CFX_RenderDevice* pDevice, int left, int top, CPDF_Font* pFont, int height, 593 const CFX_ByteString& str, FX_ARGB argb) 594 { 595 FX_RECT font_bbox; 596 pFont->GetFontBBox(font_bbox); 597 FX_FLOAT font_size = (FX_FLOAT)height * 1000.0f / (FX_FLOAT)(font_bbox.top - font_bbox.bottom); 598 FX_FLOAT origin_x = (FX_FLOAT)left; 599 FX_FLOAT origin_y = (FX_FLOAT)top + font_size * (FX_FLOAT)font_bbox.top / 1000.0f; 600 CFX_AffineMatrix matrix(1.0f, 0, 0, -1.0f, 0, 0); 601 DrawTextString(pDevice, origin_x, origin_y, pFont, font_size, &matrix, str, argb); 602 } 603 void CPDF_TextRenderer::DrawTextString(CFX_RenderDevice* pDevice, FX_FLOAT origin_x, FX_FLOAT origin_y, CPDF_Font* pFont, FX_FLOAT font_size, 604 const CFX_AffineMatrix* pMatrix, const CFX_ByteString& str, FX_ARGB fill_argb, 605 FX_ARGB stroke_argb, const CFX_GraphStateData* pGraphState, const CPDF_RenderOptions* pOptions) 606 { 607 int nChars = pFont->CountChar(str, str.GetLength()); 608 if (nChars == 0) { 609 return; 610 } 611 FX_DWORD charcode; 612 int offset = 0; 613 FX_DWORD* pCharCodes; 614 FX_FLOAT* pCharPos; 615 if (nChars == 1) { 616 charcode = pFont->GetNextChar(str, str.GetLength(), offset); 617 pCharCodes = (FX_DWORD*)(FX_UINTPTR)charcode; 618 pCharPos = NULL; 619 } else { 620 pCharCodes = FX_Alloc(FX_DWORD, nChars); 621 pCharPos = FX_Alloc(FX_FLOAT, nChars - 1); 622 FX_FLOAT cur_pos = 0; 623 for (int i = 0; i < nChars; i ++) { 624 pCharCodes[i] = pFont->GetNextChar(str, str.GetLength(), offset); 625 if (i) { 626 pCharPos[i - 1] = cur_pos; 627 } 628 cur_pos += pFont->GetCharWidthF(pCharCodes[i]) * font_size / 1000; 629 } 630 } 631 CFX_AffineMatrix matrix; 632 if (pMatrix) { 633 matrix = *pMatrix; 634 } 635 matrix.e = origin_x; 636 matrix.f = origin_y; 637 if (pFont->GetFontType() == PDFFONT_TYPE3) 638 ; 639 else if (stroke_argb == 0) { 640 DrawNormalText(pDevice, nChars, pCharCodes, pCharPos, pFont, font_size, &matrix, fill_argb, pOptions); 641 } else 642 DrawTextPath(pDevice, nChars, pCharCodes, pCharPos, pFont, font_size, &matrix, NULL, pGraphState, 643 fill_argb, stroke_argb, NULL); 644 if (nChars > 1) { 645 FX_Free(pCharCodes); 646 FX_Free(pCharPos); 647 } 648 } 649 FX_BOOL CPDF_TextRenderer::DrawNormalText(CFX_RenderDevice* pDevice, int nChars, FX_DWORD* pCharCodes, FX_FLOAT* pCharPos, 650 CPDF_Font* pFont, FX_FLOAT font_size, 651 const CFX_AffineMatrix* pText2Device, 652 FX_ARGB fill_argb, const CPDF_RenderOptions* pOptions) 653 { 654 CFX_FontCache* pCache = pFont->m_pDocument ? pFont->m_pDocument->GetRenderData()->GetFontCache() : NULL; 655 CPDF_CharPosList CharPosList; 656 CharPosList.Load(nChars, pCharCodes, pCharPos, pFont, font_size); 657 int FXGE_flags = 0; 658 if (pOptions) { 659 FX_DWORD dwFlags = pOptions->m_Flags; 660 if (dwFlags & RENDER_CLEARTYPE) { 661 FXGE_flags |= FXTEXT_CLEARTYPE; 662 if (dwFlags & RENDER_BGR_STRIPE) { 663 FXGE_flags |= FXTEXT_BGR_STRIPE; 664 } 665 } 666 if (dwFlags & RENDER_NOTEXTSMOOTH) { 667 FXGE_flags |= FXTEXT_NOSMOOTH; 668 } 669 if (dwFlags & RENDER_PRINTGRAPHICTEXT) { 670 FXGE_flags |= FXTEXT_PRINTGRAPHICTEXT; 671 } 672 if (dwFlags & RENDER_NO_NATIVETEXT) { 673 FXGE_flags |= FXTEXT_NO_NATIVETEXT; 674 } 675 if (dwFlags & RENDER_PRINTIMAGETEXT) { 676 FXGE_flags |= FXTEXT_PRINTIMAGETEXT; 677 } 678 } else { 679 FXGE_flags = FXTEXT_CLEARTYPE; 680 } 681 if (pFont->GetFontType() & PDFFONT_CIDFONT) { 682 FXGE_flags |= FXFONT_CIDFONT; 683 } 684 return pDevice->DrawNormalText(CharPosList.m_nChars, CharPosList.m_pCharPos, &pFont->m_Font, pCache, font_size, pText2Device, fill_argb, FXGE_flags); 685 } 686 void CPDF_RenderStatus::DrawTextPathWithPattern(const CPDF_TextObject* textobj, const CFX_AffineMatrix* pObj2Device, 687 CPDF_Font* pFont, FX_FLOAT font_size, 688 const CFX_AffineMatrix* pTextMatrix, FX_BOOL bFill, FX_BOOL bStroke) 689 { 690 if (!bStroke) { 691 CPDF_PathObject path; 692 CPDF_TextObject* pCopy = new CPDF_TextObject; 693 pCopy->Copy(textobj); 694 path.m_bStroke = FALSE; 695 path.m_FillType = FXFILL_WINDING; 696 path.m_ClipPath.AppendTexts(&pCopy, 1); 697 path.m_ColorState = textobj->m_ColorState; 698 path.m_Path.New()->AppendRect(textobj->m_Left, textobj->m_Bottom, textobj->m_Right, textobj->m_Top); 699 path.m_Left = textobj->m_Left; 700 path.m_Bottom = textobj->m_Bottom; 701 path.m_Right = textobj->m_Right; 702 path.m_Top = textobj->m_Top; 703 RenderSingleObject(&path, pObj2Device); 704 return; 705 } 706 CFX_FontCache* pCache; 707 if (pFont->m_pDocument) { 708 pCache = pFont->m_pDocument->GetRenderData()->GetFontCache(); 709 } else { 710 pCache = CFX_GEModule::Get()->GetFontCache(); 711 } 712 CFX_FaceCache* pFaceCache = pCache->GetCachedFace(&pFont->m_Font); 713 FX_FONTCACHE_DEFINE(pCache, &pFont->m_Font); 714 CPDF_CharPosList CharPosList; 715 CharPosList.Load(textobj->m_nChars, textobj->m_pCharCodes, textobj->m_pCharPos, pFont, font_size); 716 for (FX_DWORD i = 0; i < CharPosList.m_nChars; i ++) { 717 FXTEXT_CHARPOS& charpos = CharPosList.m_pCharPos[i]; 718 const CFX_PathData* pPath = pFaceCache->LoadGlyphPath(&pFont->m_Font, charpos.m_GlyphIndex, 719 charpos.m_FontCharWidth); 720 if (pPath == NULL) { 721 continue; 722 } 723 CPDF_PathObject path; 724 path.m_GraphState = textobj->m_GraphState; 725 path.m_ColorState = textobj->m_ColorState; 726 CFX_AffineMatrix matrix; 727 if (charpos.m_bGlyphAdjust) 728 matrix.Set(charpos.m_AdjustMatrix[0], charpos.m_AdjustMatrix[1], 729 charpos.m_AdjustMatrix[2], charpos.m_AdjustMatrix[3], 0, 0); 730 matrix.Concat(font_size, 0, 0, font_size, charpos.m_OriginX, charpos.m_OriginY); 731 path.m_Path.New()->Append(pPath, &matrix); 732 path.m_Matrix = *pTextMatrix; 733 path.m_bStroke = bStroke; 734 path.m_FillType = bFill ? FXFILL_WINDING : 0; 735 path.CalcBoundingBox(); 736 ProcessPath(&path, pObj2Device); 737 } 738 } 739 CFX_PathData* CPDF_Font::LoadGlyphPath(FX_DWORD charcode, int dest_width) 740 { 741 int glyph_index = GlyphFromCharCode(charcode); 742 if (m_Font.m_Face == NULL) { 743 return NULL; 744 } 745 return m_Font.LoadGlyphPath(glyph_index, dest_width); 746 } 747