1 // Copyright 2016 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 "core/fpdfapi/render/cpdf_type3cache.h" 8 9 #include <map> 10 #include <memory> 11 12 #include "core/fpdfapi/font/cpdf_type3char.h" 13 #include "core/fpdfapi/font/cpdf_type3font.h" 14 #include "core/fpdfapi/render/cpdf_type3glyphs.h" 15 #include "core/fxge/fx_dib.h" 16 #include "core/fxge/fx_font.h" 17 18 namespace { 19 20 struct CPDF_UniqueKeyGen { 21 void Generate(int count, ...); 22 FX_CHAR m_Key[128]; 23 int m_KeyLen; 24 }; 25 26 void CPDF_UniqueKeyGen::Generate(int count, ...) { 27 va_list argList; 28 va_start(argList, count); 29 for (int i = 0; i < count; i++) { 30 int p = va_arg(argList, int); 31 (reinterpret_cast<uint32_t*>(m_Key))[i] = p; 32 } 33 va_end(argList); 34 m_KeyLen = count * sizeof(uint32_t); 35 } 36 37 bool IsScanLine1bpp(uint8_t* pBuf, int width) { 38 int size = width / 8; 39 for (int i = 0; i < size; i++) { 40 if (pBuf[i]) 41 return true; 42 } 43 return (width % 8) && (pBuf[width / 8] & (0xff << (8 - width % 8))); 44 } 45 46 bool IsScanLine8bpp(uint8_t* pBuf, int width) { 47 for (int i = 0; i < width; i++) { 48 if (pBuf[i] > 0x40) 49 return true; 50 } 51 return false; 52 } 53 54 int DetectFirstLastScan(const CFX_DIBitmap* pBitmap, bool bFirst) { 55 int height = pBitmap->GetHeight(); 56 int pitch = pBitmap->GetPitch(); 57 int width = pBitmap->GetWidth(); 58 int bpp = pBitmap->GetBPP(); 59 if (bpp > 8) 60 width *= bpp / 8; 61 uint8_t* pBuf = pBitmap->GetBuffer(); 62 int line = bFirst ? 0 : height - 1; 63 int line_step = bFirst ? 1 : -1; 64 int line_end = bFirst ? height : -1; 65 while (line != line_end) { 66 if (bpp == 1) { 67 if (IsScanLine1bpp(pBuf + line * pitch, width)) 68 return line; 69 } else { 70 if (IsScanLine8bpp(pBuf + line * pitch, width)) 71 return line; 72 } 73 line += line_step; 74 } 75 return -1; 76 } 77 78 } // namespace 79 80 CPDF_Type3Cache::CPDF_Type3Cache(CPDF_Type3Font* pFont) : m_pFont(pFont) {} 81 82 CPDF_Type3Cache::~CPDF_Type3Cache() { 83 for (const auto& pair : m_SizeMap) 84 delete pair.second; 85 m_SizeMap.clear(); 86 } 87 88 CFX_GlyphBitmap* CPDF_Type3Cache::LoadGlyph(uint32_t charcode, 89 const CFX_Matrix* pMatrix, 90 FX_FLOAT retinaScaleX, 91 FX_FLOAT retinaScaleY) { 92 CPDF_UniqueKeyGen keygen; 93 keygen.Generate( 94 4, FXSYS_round(pMatrix->a * 10000), FXSYS_round(pMatrix->b * 10000), 95 FXSYS_round(pMatrix->c * 10000), FXSYS_round(pMatrix->d * 10000)); 96 CFX_ByteString FaceGlyphsKey(keygen.m_Key, keygen.m_KeyLen); 97 CPDF_Type3Glyphs* pSizeCache; 98 auto it = m_SizeMap.find(FaceGlyphsKey); 99 if (it == m_SizeMap.end()) { 100 pSizeCache = new CPDF_Type3Glyphs; 101 m_SizeMap[FaceGlyphsKey] = pSizeCache; 102 } else { 103 pSizeCache = it->second; 104 } 105 auto it2 = pSizeCache->m_GlyphMap.find(charcode); 106 if (it2 != pSizeCache->m_GlyphMap.end()) 107 return it2->second; 108 109 CFX_GlyphBitmap* pGlyphBitmap = 110 RenderGlyph(pSizeCache, charcode, pMatrix, retinaScaleX, retinaScaleY); 111 pSizeCache->m_GlyphMap[charcode] = pGlyphBitmap; 112 return pGlyphBitmap; 113 } 114 115 CFX_GlyphBitmap* CPDF_Type3Cache::RenderGlyph(CPDF_Type3Glyphs* pSize, 116 uint32_t charcode, 117 const CFX_Matrix* pMatrix, 118 FX_FLOAT retinaScaleX, 119 FX_FLOAT retinaScaleY) { 120 const CPDF_Type3Char* pChar = m_pFont->LoadChar(charcode); 121 if (!pChar || !pChar->m_pBitmap) 122 return nullptr; 123 124 CFX_DIBitmap* pBitmap = pChar->m_pBitmap.get(); 125 CFX_Matrix image_matrix = pChar->m_ImageMatrix; 126 CFX_Matrix text_matrix(pMatrix->a, pMatrix->b, pMatrix->c, pMatrix->d, 0, 0); 127 image_matrix.Concat(text_matrix); 128 129 std::unique_ptr<CFX_DIBitmap> pResBitmap; 130 int left = 0; 131 int top = 0; 132 if (FXSYS_fabs(image_matrix.b) < FXSYS_fabs(image_matrix.a) / 100 && 133 FXSYS_fabs(image_matrix.c) < FXSYS_fabs(image_matrix.d) / 100) { 134 int top_line = DetectFirstLastScan(pBitmap, true); 135 int bottom_line = DetectFirstLastScan(pBitmap, false); 136 if (top_line == 0 && bottom_line == pBitmap->GetHeight() - 1) { 137 FX_FLOAT top_y = image_matrix.d + image_matrix.f; 138 FX_FLOAT bottom_y = image_matrix.f; 139 bool bFlipped = top_y > bottom_y; 140 if (bFlipped) { 141 FX_FLOAT temp = top_y; 142 top_y = bottom_y; 143 bottom_y = temp; 144 } 145 pSize->AdjustBlue(top_y, bottom_y, top_line, bottom_line); 146 pResBitmap = pBitmap->StretchTo( 147 (int)(FXSYS_round(image_matrix.a) * retinaScaleX), 148 (int)((bFlipped ? top_line - bottom_line : bottom_line - top_line) * 149 retinaScaleY)); 150 top = top_line; 151 if (image_matrix.a < 0) { 152 image_matrix.Scale(retinaScaleX, retinaScaleY); 153 left = FXSYS_round(image_matrix.e + image_matrix.a); 154 } else { 155 left = FXSYS_round(image_matrix.e); 156 } 157 } 158 } 159 if (!pResBitmap) { 160 image_matrix.Scale(retinaScaleX, retinaScaleY); 161 pResBitmap = pBitmap->TransformTo(&image_matrix, left, top); 162 } 163 if (!pResBitmap) 164 return nullptr; 165 166 CFX_GlyphBitmap* pGlyph = new CFX_GlyphBitmap; 167 pGlyph->m_Left = left; 168 pGlyph->m_Top = -top; 169 pGlyph->m_Bitmap.TakeOver(pResBitmap.get()); 170 return pGlyph; 171 } 172