| 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 "core/fxge/include/fx_font.h" | 7 #include "core/fxge/include/fx_font.h" |
| 8 | 8 |
| 9 #include "core/fpdfapi/fpdf_font/include/cpdf_font.h" | 9 #include "core/fpdfapi/fpdf_font/include/cpdf_font.h" |
| 10 #include "core/fxge/ge/fx_text_int.h" | 10 #include "core/fxge/ge/fx_text_int.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 FXFM_ENCODING_ADOBE_LATIN_1, FXFM_ENCODING_OLD_LATIN_2, | 25 FXFM_ENCODING_ADOBE_LATIN_1, FXFM_ENCODING_OLD_LATIN_2, |
| 26 FXFM_ENCODING_APPLE_ROMAN, | 26 FXFM_ENCODING_APPLE_ROMAN, |
| 27 }; | 27 }; |
| 28 | 28 |
| 29 CFX_UnicodeEncodingEx* _FXFM_CreateFontEncoding(CFX_Font* pFont, | 29 CFX_UnicodeEncodingEx* _FXFM_CreateFontEncoding(CFX_Font* pFont, |
| 30 uint32_t nEncodingID) { | 30 uint32_t nEncodingID) { |
| 31 if (FXFT_Select_Charmap(pFont->GetFace(), nEncodingID)) | 31 if (FXFT_Select_Charmap(pFont->GetFace(), nEncodingID)) |
| 32 return nullptr; | 32 return nullptr; |
| 33 return new CFX_UnicodeEncodingEx(pFont, nEncodingID); | 33 return new CFX_UnicodeEncodingEx(pFont, nEncodingID); |
| 34 } | 34 } |
| 35 |
| 36 unsigned long FTStreamRead(FXFT_Stream stream, |
| 37 unsigned long offset, |
| 38 unsigned char* buffer, |
| 39 unsigned long count) { |
| 40 if (count == 0) |
| 41 return 0; |
| 42 |
| 43 IFX_FileRead* pFile = static_cast<IFX_FileRead*>(stream->descriptor.pointer); |
| 44 return pFile->ReadBlock(buffer, offset, count) ? count : 0; |
| 45 } |
| 46 |
| 47 void FTStreamClose(FXFT_Stream stream) {} |
| 48 |
| 49 FX_BOOL LoadFileImp(FXFT_Library library, |
| 50 FXFT_Face* Face, |
| 51 IFX_FileRead* pFile, |
| 52 int32_t faceIndex, |
| 53 std::unique_ptr<FXFT_StreamRec>* stream) { |
| 54 std::unique_ptr<FXFT_StreamRec> stream1(new FXFT_StreamRec()); |
| 55 stream1->base = nullptr; |
| 56 stream1->size = static_cast<unsigned long>(pFile->GetSize()); |
| 57 stream1->pos = 0; |
| 58 stream1->descriptor.pointer = pFile; |
| 59 stream1->close = FTStreamClose; |
| 60 stream1->read = FTStreamRead; |
| 61 FXFT_Open_Args args; |
| 62 args.flags = FT_OPEN_STREAM; |
| 63 args.stream = stream1.get(); |
| 64 if (FXFT_Open_Face(library, &args, faceIndex, Face)) |
| 65 return FALSE; |
| 66 if (stream) |
| 67 *stream = std::move(stream1); |
| 68 return TRUE; |
| 69 } |
| 35 #endif // PDF_ENABLE_XFA | 70 #endif // PDF_ENABLE_XFA |
| 36 | 71 |
| 37 FXFT_Face FT_LoadFont(const uint8_t* pData, int size) { | 72 FXFT_Face FT_LoadFont(const uint8_t* pData, int size) { |
| 38 return CFX_GEModule::Get()->GetFontMgr()->GetFixedFace(pData, size, 0); | 73 return CFX_GEModule::Get()->GetFontMgr()->GetFixedFace(pData, size, 0); |
| 39 } | 74 } |
| 40 | 75 |
| 41 } // namespace | 76 } // namespace |
| 42 | 77 |
| 43 CFX_Font::CFX_Font() | 78 CFX_Font::CFX_Font() |
| 44 #ifdef PDF_ENABLE_XFA | 79 #ifdef PDF_ENABLE_XFA |
| 45 : m_bLogic(FALSE), | 80 : m_bLogic(FALSE), |
| 46 m_pOwnedStream(nullptr), | 81 m_pOwnedStream(nullptr), |
| 47 m_Face(nullptr), | 82 m_Face(nullptr), |
| 48 #else | 83 #else |
| 49 : m_Face(nullptr), | 84 : m_Face(nullptr), |
| 50 #endif // PDF_ENABLE_XFA | 85 #endif // PDF_ENABLE_XFA |
| 51 m_pSubstFont(nullptr), | |
| 52 m_pFontDataAllocation(nullptr), | |
| 53 m_pFontData(nullptr), | 86 m_pFontData(nullptr), |
| 54 m_pGsubData(nullptr), | 87 m_pGsubData(nullptr), |
| 55 m_dwSize(0), | 88 m_dwSize(0), |
| 56 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | 89 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
| 57 m_pPlatformFont(nullptr), | 90 m_pPlatformFont(nullptr), |
| 58 #endif | 91 #endif |
| 59 m_bEmbedded(FALSE), | 92 m_bEmbedded(FALSE), |
| 60 m_bVertical(FALSE) { | 93 m_bVertical(FALSE) { |
| 61 } | 94 } |
| 62 | 95 |
| 63 #ifdef PDF_ENABLE_XFA | 96 #ifdef PDF_ENABLE_XFA |
| 64 FX_BOOL CFX_Font::LoadClone(const CFX_Font* pFont) { | 97 FX_BOOL CFX_Font::LoadClone(const CFX_Font* pFont) { |
| 65 if (!pFont) | 98 if (!pFont) |
| 66 return FALSE; | 99 return FALSE; |
| 67 | 100 |
| 68 m_bLogic = TRUE; | 101 m_bLogic = TRUE; |
| 69 if (pFont->m_pSubstFont) { | 102 if (pFont->m_pSubstFont) { |
| 70 m_pSubstFont = new CFX_SubstFont; | 103 m_pSubstFont.reset(new CFX_SubstFont); |
| 71 m_pSubstFont->m_Charset = pFont->m_pSubstFont->m_Charset; | 104 m_pSubstFont->m_Charset = pFont->m_pSubstFont->m_Charset; |
| 72 m_pSubstFont->m_ExtHandle = pFont->m_pSubstFont->m_ExtHandle; | 105 m_pSubstFont->m_ExtHandle = pFont->m_pSubstFont->m_ExtHandle; |
| 73 m_pSubstFont->m_SubstFlags = pFont->m_pSubstFont->m_SubstFlags; | 106 m_pSubstFont->m_SubstFlags = pFont->m_pSubstFont->m_SubstFlags; |
| 74 m_pSubstFont->m_Weight = pFont->m_pSubstFont->m_Weight; | 107 m_pSubstFont->m_Weight = pFont->m_pSubstFont->m_Weight; |
| 75 m_pSubstFont->m_Family = pFont->m_pSubstFont->m_Family; | 108 m_pSubstFont->m_Family = pFont->m_pSubstFont->m_Family; |
| 76 m_pSubstFont->m_ItalicAngle = pFont->m_pSubstFont->m_ItalicAngle; | 109 m_pSubstFont->m_ItalicAngle = pFont->m_pSubstFont->m_ItalicAngle; |
| 77 } | 110 } |
| 78 if (pFont->m_OtfFontData.GetSize()) { | 111 if (pFont->m_OtfFontData.GetSize()) { |
| 79 m_OtfFontData.AttachData(pFont->m_OtfFontData.GetBuffer(), | 112 m_OtfFontData.AttachData(pFont->m_OtfFontData.GetBuffer(), |
| 80 pFont->m_OtfFontData.GetSize()); | 113 pFont->m_OtfFontData.GetSize()); |
| 81 } | 114 } |
| 82 m_Face = pFont->m_Face; | 115 m_Face = pFont->m_Face; |
| 83 m_bEmbedded = pFont->m_bEmbedded; | 116 m_bEmbedded = pFont->m_bEmbedded; |
| 84 m_bVertical = pFont->m_bVertical; | 117 m_bVertical = pFont->m_bVertical; |
| 85 m_dwSize = pFont->m_dwSize; | 118 m_dwSize = pFont->m_dwSize; |
| 86 m_pFontData = pFont->m_pFontData; | 119 m_pFontData = pFont->m_pFontData; |
| 87 m_pGsubData = pFont->m_pGsubData; | 120 m_pGsubData = pFont->m_pGsubData; |
| 88 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | 121 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
| 89 m_pPlatformFont = pFont->m_pPlatformFont; | 122 m_pPlatformFont = pFont->m_pPlatformFont; |
| 90 #endif | 123 #endif |
| 91 m_pOwnedStream = pFont->m_pOwnedStream; | 124 m_pOwnedStream = pFont->m_pOwnedStream; |
| 92 return TRUE; | 125 return TRUE; |
| 93 } | 126 } |
| 94 #endif // PDF_ENABLE_XFA | 127 #endif // PDF_ENABLE_XFA |
| 95 | 128 |
| 96 CFX_Font::~CFX_Font() { | 129 CFX_Font::~CFX_Font() { |
| 97 delete m_pSubstFont; | |
| 98 FX_Free(m_pFontDataAllocation); | |
| 99 #ifdef PDF_ENABLE_XFA | 130 #ifdef PDF_ENABLE_XFA |
| 100 if (m_bLogic) { | 131 if (m_bLogic) { |
| 101 m_OtfFontData.DetachBuffer(); | 132 m_OtfFontData.DetachBuffer(); |
| 102 return; | 133 return; |
| 103 } | 134 } |
| 104 #endif // PDF_ENABLE_XFA | 135 #endif // PDF_ENABLE_XFA |
| 105 if (m_Face) { | 136 if (m_Face) { |
| 106 #ifndef PDF_ENABLE_XFA | 137 #ifndef PDF_ENABLE_XFA |
| 107 if (FXFT_Get_Face_External_Stream(m_Face)) { | 138 if (FXFT_Get_Face_External_Stream(m_Face)) { |
| 108 FXFT_Clear_Face_External_Stream(m_Face); | 139 FXFT_Clear_Face_External_Stream(m_Face); |
| 109 } | 140 } |
| 110 #endif // PDF_ENABLE_XFA | 141 #endif // PDF_ENABLE_XFA |
| 111 if (m_bEmbedded) { | 142 if (m_bEmbedded) { |
| 112 DeleteFace(); | 143 DeleteFace(); |
| 113 } else { | 144 } else { |
| 114 CFX_GEModule::Get()->GetFontMgr()->ReleaseFace(m_Face); | 145 CFX_GEModule::Get()->GetFontMgr()->ReleaseFace(m_Face); |
| 115 } | 146 } |
| 116 } | 147 } |
| 117 #ifdef PDF_ENABLE_XFA | 148 #ifdef PDF_ENABLE_XFA |
| 118 FX_Free(m_pOwnedStream); | 149 FX_Free(m_pOwnedStream); |
| 119 #endif // PDF_ENABLE_XFA | 150 #endif // PDF_ENABLE_XFA |
| 120 FX_Free(m_pGsubData); | 151 FX_Free(m_pGsubData); |
| 121 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ && !defined _SKIA_SUPPORT_ | 152 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ && !defined _SKIA_SUPPORT_ |
| 122 ReleasePlatformResource(); | 153 ReleasePlatformResource(); |
| 123 #endif | 154 #endif |
| 124 } | 155 } |
| 156 |
| 125 void CFX_Font::DeleteFace() { | 157 void CFX_Font::DeleteFace() { |
| 126 FXFT_Done_Face(m_Face); | 158 FXFT_Done_Face(m_Face); |
| 127 m_Face = nullptr; | 159 m_Face = nullptr; |
| 128 } | 160 } |
| 161 |
| 129 void CFX_Font::LoadSubst(const CFX_ByteString& face_name, | 162 void CFX_Font::LoadSubst(const CFX_ByteString& face_name, |
| 130 FX_BOOL bTrueType, | 163 FX_BOOL bTrueType, |
| 131 uint32_t flags, | 164 uint32_t flags, |
| 132 int weight, | 165 int weight, |
| 133 int italic_angle, | 166 int italic_angle, |
| 134 int CharsetCP, | 167 int CharsetCP, |
| 135 FX_BOOL bVertical) { | 168 FX_BOOL bVertical) { |
| 136 m_bEmbedded = FALSE; | 169 m_bEmbedded = FALSE; |
| 137 m_bVertical = bVertical; | 170 m_bVertical = bVertical; |
| 138 m_pSubstFont = new CFX_SubstFont; | 171 m_pSubstFont.reset(new CFX_SubstFont); |
| 139 m_Face = CFX_GEModule::Get()->GetFontMgr()->FindSubstFont( | 172 m_Face = CFX_GEModule::Get()->GetFontMgr()->FindSubstFont( |
| 140 face_name, bTrueType, flags, weight, italic_angle, CharsetCP, | 173 face_name, bTrueType, flags, weight, italic_angle, CharsetCP, |
| 141 m_pSubstFont); | 174 m_pSubstFont.get()); |
| 142 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | 175 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
| 143 if (m_pSubstFont->m_ExtHandle) { | 176 if (m_pSubstFont->m_ExtHandle) { |
| 144 m_pPlatformFont = m_pSubstFont->m_ExtHandle; | 177 m_pPlatformFont = m_pSubstFont->m_ExtHandle; |
| 145 m_pSubstFont->m_ExtHandle = nullptr; | 178 m_pSubstFont->m_ExtHandle = nullptr; |
| 146 } | 179 } |
| 147 #endif | 180 #endif |
| 148 if (m_Face) { | 181 if (m_Face) { |
| 149 m_pFontData = FXFT_Get_Face_Stream_Base(m_Face); | 182 m_pFontData = FXFT_Get_Face_Stream_Base(m_Face); |
| 150 m_dwSize = FXFT_Get_Face_Stream_Size(m_Face); | 183 m_dwSize = FXFT_Get_Face_Stream_Size(m_Face); |
| 151 } | 184 } |
| 152 } | 185 } |
| 186 |
| 153 #ifdef PDF_ENABLE_XFA | 187 #ifdef PDF_ENABLE_XFA |
| 154 extern "C" { | |
| 155 unsigned long _FTStreamRead(FXFT_Stream stream, | |
| 156 unsigned long offset, | |
| 157 unsigned char* buffer, | |
| 158 unsigned long count) { | |
| 159 if (count == 0) { | |
| 160 return 0; | |
| 161 } | |
| 162 IFX_FileRead* pFile = (IFX_FileRead*)stream->descriptor.pointer; | |
| 163 int res = pFile->ReadBlock(buffer, offset, count); | |
| 164 if (res) { | |
| 165 return count; | |
| 166 } | |
| 167 return 0; | |
| 168 } | |
| 169 void _FTStreamClose(FXFT_Stream stream) {} | |
| 170 }; | |
| 171 FX_BOOL _LoadFile(FXFT_Library library, | |
| 172 FXFT_Face* Face, | |
| 173 IFX_FileRead* pFile, | |
| 174 FXFT_Stream* stream, | |
| 175 int32_t faceIndex = 0) { | |
| 176 FXFT_Stream stream1 = (FXFT_Stream)FX_Alloc(uint8_t, sizeof(FXFT_StreamRec)); | |
| 177 stream1->base = nullptr; | |
| 178 stream1->size = (unsigned long)pFile->GetSize(); | |
| 179 stream1->pos = 0; | |
| 180 stream1->descriptor.pointer = pFile; | |
| 181 stream1->close = _FTStreamClose; | |
| 182 stream1->read = _FTStreamRead; | |
| 183 FXFT_Open_Args args; | |
| 184 args.flags = FT_OPEN_STREAM; | |
| 185 args.stream = stream1; | |
| 186 if (FXFT_Open_Face(library, &args, faceIndex, Face)) { | |
| 187 FX_Free(stream1); | |
| 188 return FALSE; | |
| 189 } | |
| 190 if (stream) { | |
| 191 *stream = stream1; | |
| 192 } | |
| 193 return TRUE; | |
| 194 } | |
| 195 | |
| 196 FX_BOOL CFX_Font::LoadFile(IFX_FileRead* pFile, | 188 FX_BOOL CFX_Font::LoadFile(IFX_FileRead* pFile, |
| 197 int nFaceIndex, | 189 int nFaceIndex, |
| 198 int* pFaceCount) { | 190 int* pFaceCount) { |
| 199 m_bEmbedded = FALSE; | 191 m_bEmbedded = FALSE; |
| 200 | 192 |
| 201 CFX_FontMgr* pFontMgr = CFX_GEModule::Get()->GetFontMgr(); | 193 CFX_FontMgr* pFontMgr = CFX_GEModule::Get()->GetFontMgr(); |
| 202 pFontMgr->InitFTLibrary(); | 194 pFontMgr->InitFTLibrary(); |
| 203 FXFT_Library library = pFontMgr->GetFTLibrary(); | 195 FXFT_Library library = pFontMgr->GetFTLibrary(); |
| 204 | 196 |
| 205 FXFT_Stream stream = nullptr; | 197 std::unique_ptr<FXFT_StreamRec> stream; |
| 206 if (!_LoadFile(library, &m_Face, pFile, &stream, nFaceIndex)) | 198 if (!LoadFileImp(library, &m_Face, pFile, nFaceIndex, &stream)) |
| 207 return FALSE; | 199 return FALSE; |
| 208 | 200 |
| 209 if (pFaceCount) | 201 if (pFaceCount) |
| 210 *pFaceCount = (int)m_Face->num_faces; | 202 *pFaceCount = (int)m_Face->num_faces; |
| 211 m_pOwnedStream = stream; | 203 m_pOwnedStream = stream.release(); |
| 212 FXFT_Set_Pixel_Sizes(m_Face, 0, 64); | 204 FXFT_Set_Pixel_Sizes(m_Face, 0, 64); |
| 213 return TRUE; | 205 return TRUE; |
| 214 } | 206 } |
| 215 #endif // PDF_ENABLE_XFA | 207 #endif // PDF_ENABLE_XFA |
| 216 | 208 |
| 217 int CFX_Font::GetGlyphWidth(uint32_t glyph_index) { | 209 int CFX_Font::GetGlyphWidth(uint32_t glyph_index) { |
| 218 if (!m_Face) { | 210 if (!m_Face) { |
| 219 return 0; | 211 return 0; |
| 220 } | 212 } |
| 221 if (m_pSubstFont && (m_pSubstFont->m_SubstFlags & FXFONT_SUBST_MM)) { | 213 if (m_pSubstFont && (m_pSubstFont->m_SubstFlags & FXFONT_SUBST_MM)) { |
| 222 AdjustMMParams(glyph_index, 0, 0); | 214 AdjustMMParams(glyph_index, 0, 0); |
| 223 } | 215 } |
| 224 int err = FXFT_Load_Glyph( | 216 int err = FXFT_Load_Glyph( |
| 225 m_Face, glyph_index, | 217 m_Face, glyph_index, |
| 226 FXFT_LOAD_NO_SCALE | FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH); | 218 FXFT_LOAD_NO_SCALE | FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH); |
| 227 if (err) { | 219 if (err) { |
| 228 return 0; | 220 return 0; |
| 229 } | 221 } |
| 230 int width = EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), | 222 int width = EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), |
| 231 FXFT_Get_Glyph_HoriAdvance(m_Face)); | 223 FXFT_Get_Glyph_HoriAdvance(m_Face)); |
| 232 return width; | 224 return width; |
| 233 } | 225 } |
| 234 | 226 |
| 235 FX_BOOL CFX_Font::LoadEmbedded(const uint8_t* data, uint32_t size) { | 227 FX_BOOL CFX_Font::LoadEmbedded(const uint8_t* data, uint32_t size) { |
| 236 m_pFontDataAllocation = FX_Alloc(uint8_t, size); | 228 std::vector<uint8_t> temp(data, data + size); |
| 237 FXSYS_memcpy(m_pFontDataAllocation, data, size); | 229 m_pFontDataAllocation.swap(temp); |
| 238 m_Face = FT_LoadFont(m_pFontDataAllocation, size); | 230 m_Face = FT_LoadFont(m_pFontDataAllocation.data(), size); |
| 239 m_pFontData = m_pFontDataAllocation; | 231 m_pFontData = m_pFontDataAllocation.data(); |
| 240 m_bEmbedded = TRUE; | 232 m_bEmbedded = TRUE; |
| 241 m_dwSize = size; | 233 m_dwSize = size; |
| 242 return !!m_Face; | 234 return !!m_Face; |
| 243 } | 235 } |
| 244 | 236 |
| 245 FX_BOOL CFX_Font::IsTTFont() const { | 237 FX_BOOL CFX_Font::IsTTFont() const { |
| 246 if (!m_Face) | 238 if (!m_Face) |
| 247 return FALSE; | 239 return FALSE; |
| 248 return FXFT_Is_Face_TT_OT(m_Face) == FXFT_FACE_FLAG_SFNT; | 240 return FXFT_Is_Face_TT_OT(m_Face) == FXFT_FACE_FLAG_SFNT; |
| 249 } | 241 } |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 531 for (size_t i = 0; i < FX_ArraySize(g_EncodingID); ++i) { | 523 for (size_t i = 0; i < FX_ArraySize(g_EncodingID); ++i) { |
| 532 CFX_UnicodeEncodingEx* pFontEncoding = | 524 CFX_UnicodeEncodingEx* pFontEncoding = |
| 533 _FXFM_CreateFontEncoding(pFont, g_EncodingID[i]); | 525 _FXFM_CreateFontEncoding(pFont, g_EncodingID[i]); |
| 534 if (pFontEncoding) { | 526 if (pFontEncoding) { |
| 535 return pFontEncoding; | 527 return pFontEncoding; |
| 536 } | 528 } |
| 537 } | 529 } |
| 538 return nullptr; | 530 return nullptr; |
| 539 } | 531 } |
| 540 #endif // PDF_ENABLE_XFA | 532 #endif // PDF_ENABLE_XFA |
| OLD | NEW |