| 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 "fpdfsdk/pdfwindow/PWL_FontMap.h" | 7 #include "fpdfsdk/pdfwindow/PWL_FontMap.h" |
| 8 | 8 |
| 9 #include <utility> |
| 10 |
| 9 #include "core/fpdfapi/cpdf_modulemgr.h" | 11 #include "core/fpdfapi/cpdf_modulemgr.h" |
| 10 #include "core/fpdfapi/font/cpdf_font.h" | 12 #include "core/fpdfapi/font/cpdf_font.h" |
| 11 #include "core/fpdfapi/font/cpdf_fontencoding.h" | 13 #include "core/fpdfapi/font/cpdf_fontencoding.h" |
| 12 #include "core/fpdfapi/parser/cpdf_document.h" | 14 #include "core/fpdfapi/parser/cpdf_document.h" |
| 13 #include "core/fpdfapi/parser/cpdf_parser.h" | 15 #include "core/fpdfapi/parser/cpdf_parser.h" |
| 14 #include "core/fpdfdoc/ipvt_fontmap.h" | 16 #include "core/fpdfdoc/ipvt_fontmap.h" |
| 15 #include "fpdfsdk/pdfwindow/PWL_Wnd.h" | 17 #include "fpdfsdk/pdfwindow/PWL_Wnd.h" |
| 16 #include "third_party/base/ptr_util.h" | 18 #include "third_party/base/ptr_util.h" |
| 19 #include "third_party/base/stl_util.h" |
| 17 | 20 |
| 18 namespace { | 21 namespace { |
| 19 | 22 |
| 20 const char kDefaultFontName[] = "Helvetica"; | 23 const char kDefaultFontName[] = "Helvetica"; |
| 21 | 24 |
| 22 const char* const g_sDEStandardFontName[] = {"Courier", | 25 const char* const g_sDEStandardFontName[] = {"Courier", |
| 23 "Courier-Bold", | 26 "Courier-Bold", |
| 24 "Courier-BoldOblique", | 27 "Courier-BoldOblique", |
| 25 "Courier-Oblique", | 28 "Courier-Oblique", |
| 26 "Helvetica", | 29 "Helvetica", |
| (...skipping 23 matching lines...) Expand all Loading... |
| 50 if (CPDF_ModuleMgr::Get()) { | 53 if (CPDF_ModuleMgr::Get()) { |
| 51 m_pPDFDoc = pdfium::MakeUnique<CPDF_Document>(nullptr); | 54 m_pPDFDoc = pdfium::MakeUnique<CPDF_Document>(nullptr); |
| 52 m_pPDFDoc->CreateNewDoc(); | 55 m_pPDFDoc->CreateNewDoc(); |
| 53 } | 56 } |
| 54 } | 57 } |
| 55 | 58 |
| 56 return m_pPDFDoc.get(); | 59 return m_pPDFDoc.get(); |
| 57 } | 60 } |
| 58 | 61 |
| 59 CPDF_Font* CPWL_FontMap::GetPDFFont(int32_t nFontIndex) { | 62 CPDF_Font* CPWL_FontMap::GetPDFFont(int32_t nFontIndex) { |
| 60 if (nFontIndex >= 0 && nFontIndex < m_aData.GetSize()) { | 63 if (nFontIndex >= 0 && nFontIndex < pdfium::CollectionSize<int32_t>(m_Data)) { |
| 61 if (CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex)) { | 64 if (m_Data[nFontIndex]) |
| 62 return pData->pFont; | 65 return m_Data[nFontIndex]->pFont; |
| 63 } | |
| 64 } | 66 } |
| 65 | |
| 66 return nullptr; | 67 return nullptr; |
| 67 } | 68 } |
| 68 | 69 |
| 69 CFX_ByteString CPWL_FontMap::GetPDFFontAlias(int32_t nFontIndex) { | 70 CFX_ByteString CPWL_FontMap::GetPDFFontAlias(int32_t nFontIndex) { |
| 70 if (nFontIndex >= 0 && nFontIndex < m_aData.GetSize()) { | 71 if (nFontIndex >= 0 && nFontIndex < pdfium::CollectionSize<int32_t>(m_Data)) { |
| 71 if (CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex)) { | 72 if (m_Data[nFontIndex]) |
| 72 return pData->sFontName; | 73 return m_Data[nFontIndex]->sFontName; |
| 73 } | |
| 74 } | 74 } |
| 75 | 75 return CFX_ByteString(); |
| 76 return ""; | |
| 77 } | 76 } |
| 78 | 77 |
| 79 bool CPWL_FontMap::KnowWord(int32_t nFontIndex, uint16_t word) { | 78 bool CPWL_FontMap::KnowWord(int32_t nFontIndex, uint16_t word) { |
| 80 if (nFontIndex >= 0 && nFontIndex < m_aData.GetSize()) { | 79 if (nFontIndex >= 0 && nFontIndex < pdfium::CollectionSize<int32_t>(m_Data)) { |
| 81 if (m_aData.GetAt(nFontIndex)) { | 80 if (m_Data[nFontIndex]) |
| 82 return CharCodeFromUnicode(nFontIndex, word) >= 0; | 81 return CharCodeFromUnicode(nFontIndex, word) >= 0; |
| 83 } | |
| 84 } | 82 } |
| 85 | |
| 86 return false; | 83 return false; |
| 87 } | 84 } |
| 88 | 85 |
| 89 int32_t CPWL_FontMap::GetWordFontIndex(uint16_t word, | 86 int32_t CPWL_FontMap::GetWordFontIndex(uint16_t word, |
| 90 int32_t nCharset, | 87 int32_t nCharset, |
| 91 int32_t nFontIndex) { | 88 int32_t nFontIndex) { |
| 92 if (nFontIndex > 0) { | 89 if (nFontIndex > 0) { |
| 93 if (KnowWord(nFontIndex, word)) | 90 if (KnowWord(nFontIndex, word)) |
| 94 return nFontIndex; | 91 return nFontIndex; |
| 95 } else { | 92 } else { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 112 nNewFontIndex = | 109 nNewFontIndex = |
| 113 GetFontIndex("Arial Unicode MS", FXFONT_DEFAULT_CHARSET, false); | 110 GetFontIndex("Arial Unicode MS", FXFONT_DEFAULT_CHARSET, false); |
| 114 if (nNewFontIndex >= 0) { | 111 if (nNewFontIndex >= 0) { |
| 115 if (KnowWord(nNewFontIndex, word)) | 112 if (KnowWord(nNewFontIndex, word)) |
| 116 return nNewFontIndex; | 113 return nNewFontIndex; |
| 117 } | 114 } |
| 118 return -1; | 115 return -1; |
| 119 } | 116 } |
| 120 | 117 |
| 121 int32_t CPWL_FontMap::CharCodeFromUnicode(int32_t nFontIndex, uint16_t word) { | 118 int32_t CPWL_FontMap::CharCodeFromUnicode(int32_t nFontIndex, uint16_t word) { |
| 122 CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex); | 119 if (nFontIndex < 0 || nFontIndex >= pdfium::CollectionSize<int32_t>(m_Data)) |
| 123 if (!pData) | |
| 124 return -1; | 120 return -1; |
| 125 | 121 |
| 126 if (!pData->pFont) | 122 CPWL_FontMap_Data* pData = m_Data[nFontIndex].get(); |
| 123 if (!pData || !pData->pFont) |
| 127 return -1; | 124 return -1; |
| 128 | 125 |
| 129 if (pData->pFont->IsUnicodeCompatible()) | 126 if (pData->pFont->IsUnicodeCompatible()) |
| 130 return pData->pFont->CharCodeFromUnicode(word); | 127 return pData->pFont->CharCodeFromUnicode(word); |
| 131 | 128 |
| 132 return word < 0xFF ? word : -1; | 129 return word < 0xFF ? word : -1; |
| 133 } | 130 } |
| 134 | 131 |
| 135 CFX_ByteString CPWL_FontMap::GetNativeFontName(int32_t nCharset) { | 132 CFX_ByteString CPWL_FontMap::GetNativeFontName(int32_t nCharset) { |
| 136 // searching native font is slow, so we must save time | 133 for (const auto& pData : m_NativeFont) { |
| 137 for (int32_t i = 0, sz = m_aNativeFont.GetSize(); i < sz; i++) { | 134 if (pData && pData->nCharset == nCharset) |
| 138 if (CPWL_FontMap_Native* pData = m_aNativeFont.GetAt(i)) { | 135 return pData->sFontName; |
| 139 if (pData->nCharset == nCharset) | |
| 140 return pData->sFontName; | |
| 141 } | |
| 142 } | 136 } |
| 143 | 137 |
| 144 CFX_ByteString sNew = GetNativeFont(nCharset); | 138 CFX_ByteString sNew = GetNativeFont(nCharset); |
| 139 if (sNew.IsEmpty()) |
| 140 return CFX_ByteString(); |
| 145 | 141 |
| 146 if (!sNew.IsEmpty()) { | 142 auto pNewData = pdfium::MakeUnique<CPWL_FontMap_Native>(); |
| 147 CPWL_FontMap_Native* pNewData = new CPWL_FontMap_Native; | 143 pNewData->nCharset = nCharset; |
| 148 pNewData->nCharset = nCharset; | 144 pNewData->sFontName = sNew; |
| 149 pNewData->sFontName = sNew; | 145 m_NativeFont.push_back(std::move(pNewData)); |
| 150 | |
| 151 m_aNativeFont.Add(pNewData); | |
| 152 } | |
| 153 | |
| 154 return sNew; | 146 return sNew; |
| 155 } | 147 } |
| 156 | 148 |
| 157 void CPWL_FontMap::Empty() { | 149 void CPWL_FontMap::Empty() { |
| 158 { | 150 m_Data.clear(); |
| 159 for (int32_t i = 0, sz = m_aData.GetSize(); i < sz; i++) | 151 m_NativeFont.clear(); |
| 160 delete m_aData.GetAt(i); | |
| 161 | |
| 162 m_aData.RemoveAll(); | |
| 163 } | |
| 164 { | |
| 165 for (int32_t i = 0, sz = m_aNativeFont.GetSize(); i < sz; i++) | |
| 166 delete m_aNativeFont.GetAt(i); | |
| 167 | |
| 168 m_aNativeFont.RemoveAll(); | |
| 169 } | |
| 170 } | 152 } |
| 171 | 153 |
| 172 void CPWL_FontMap::Initialize() { | 154 void CPWL_FontMap::Initialize() { |
| 173 GetFontIndex(kDefaultFontName, FXFONT_ANSI_CHARSET, false); | 155 GetFontIndex(kDefaultFontName, FXFONT_ANSI_CHARSET, false); |
| 174 } | 156 } |
| 175 | 157 |
| 176 bool CPWL_FontMap::IsStandardFont(const CFX_ByteString& sFontName) { | 158 bool CPWL_FontMap::IsStandardFont(const CFX_ByteString& sFontName) { |
| 177 for (size_t i = 0; i < FX_ArraySize(g_sDEStandardFontName); ++i) { | 159 for (size_t i = 0; i < FX_ArraySize(g_sDEStandardFontName); ++i) { |
| 178 if (sFontName == g_sDEStandardFontName[i]) | 160 if (sFontName == g_sDEStandardFontName[i]) |
| 179 return true; | 161 return true; |
| 180 } | 162 } |
| 181 | 163 |
| 182 return false; | 164 return false; |
| 183 } | 165 } |
| 184 | 166 |
| 185 int32_t CPWL_FontMap::FindFont(const CFX_ByteString& sFontName, | 167 int32_t CPWL_FontMap::FindFont(const CFX_ByteString& sFontName, |
| 186 int32_t nCharset) { | 168 int32_t nCharset) { |
| 187 for (int32_t i = 0, sz = m_aData.GetSize(); i < sz; i++) { | 169 int32_t i = 0; |
| 188 if (CPWL_FontMap_Data* pData = m_aData.GetAt(i)) { | 170 for (const auto& pData : m_Data) { |
| 189 if (nCharset == FXFONT_DEFAULT_CHARSET || nCharset == pData->nCharset) { | 171 if (pData && |
| 190 if (sFontName.IsEmpty() || pData->sFontName == sFontName) | 172 (nCharset == FXFONT_DEFAULT_CHARSET || nCharset == pData->nCharset) && |
| 191 return i; | 173 (sFontName.IsEmpty() || pData->sFontName == sFontName)) { |
| 192 } | 174 return i; |
| 193 } | 175 } |
| 176 ++i; |
| 194 } | 177 } |
| 195 | |
| 196 return -1; | 178 return -1; |
| 197 } | 179 } |
| 198 | 180 |
| 199 int32_t CPWL_FontMap::GetFontIndex(const CFX_ByteString& sFontName, | 181 int32_t CPWL_FontMap::GetFontIndex(const CFX_ByteString& sFontName, |
| 200 int32_t nCharset, | 182 int32_t nCharset, |
| 201 bool bFind) { | 183 bool bFind) { |
| 202 int32_t nFontIndex = FindFont(EncodeFontAlias(sFontName, nCharset), nCharset); | 184 int32_t nFontIndex = FindFont(EncodeFontAlias(sFontName, nCharset), nCharset); |
| 203 if (nFontIndex >= 0) | 185 if (nFontIndex >= 0) |
| 204 return nFontIndex; | 186 return nFontIndex; |
| 205 | 187 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 218 } | 200 } |
| 219 | 201 |
| 220 CPDF_Font* CPWL_FontMap::FindFontSameCharset(CFX_ByteString& sFontAlias, | 202 CPDF_Font* CPWL_FontMap::FindFontSameCharset(CFX_ByteString& sFontAlias, |
| 221 int32_t nCharset) { | 203 int32_t nCharset) { |
| 222 return nullptr; | 204 return nullptr; |
| 223 } | 205 } |
| 224 | 206 |
| 225 int32_t CPWL_FontMap::AddFontData(CPDF_Font* pFont, | 207 int32_t CPWL_FontMap::AddFontData(CPDF_Font* pFont, |
| 226 const CFX_ByteString& sFontAlias, | 208 const CFX_ByteString& sFontAlias, |
| 227 int32_t nCharset) { | 209 int32_t nCharset) { |
| 228 CPWL_FontMap_Data* pNewData = new CPWL_FontMap_Data; | 210 auto pNewData = pdfium::MakeUnique<CPWL_FontMap_Data>(); |
| 229 pNewData->pFont = pFont; | 211 pNewData->pFont = pFont; |
| 230 pNewData->sFontName = sFontAlias; | 212 pNewData->sFontName = sFontAlias; |
| 231 pNewData->nCharset = nCharset; | 213 pNewData->nCharset = nCharset; |
| 232 | 214 m_Data.push_back(std::move(pNewData)); |
| 233 m_aData.Add(pNewData); | 215 return pdfium::CollectionSize<int32_t>(m_Data) - 1; |
| 234 | |
| 235 return m_aData.GetSize() - 1; | |
| 236 } | 216 } |
| 237 | 217 |
| 238 void CPWL_FontMap::AddedFont(CPDF_Font* pFont, | 218 void CPWL_FontMap::AddedFont(CPDF_Font* pFont, |
| 239 const CFX_ByteString& sFontAlias) {} | 219 const CFX_ByteString& sFontAlias) {} |
| 240 | 220 |
| 241 CFX_ByteString CPWL_FontMap::GetNativeFont(int32_t nCharset) { | 221 CFX_ByteString CPWL_FontMap::GetNativeFont(int32_t nCharset) { |
| 242 if (nCharset == FXFONT_DEFAULT_CHARSET) | 222 if (nCharset == FXFONT_DEFAULT_CHARSET) |
| 243 nCharset = GetNativeCharset(); | 223 nCharset = GetNativeCharset(); |
| 244 | 224 |
| 245 CFX_ByteString sFontName = GetDefaultFontByCharset(nCharset); | 225 CFX_ByteString sFontName = GetDefaultFontByCharset(nCharset); |
| 246 if (m_pSystemHandler->FindNativeTrueTypeFont(sFontName)) | 226 if (!m_pSystemHandler->FindNativeTrueTypeFont(sFontName)) |
| 247 return sFontName; | 227 return CFX_ByteString(); |
| 248 | 228 |
| 249 sFontName.clear(); | |
| 250 return sFontName; | 229 return sFontName; |
| 251 } | 230 } |
| 252 | 231 |
| 253 CPDF_Font* CPWL_FontMap::AddFontToDocument(CPDF_Document* pDoc, | 232 CPDF_Font* CPWL_FontMap::AddFontToDocument(CPDF_Document* pDoc, |
| 254 CFX_ByteString& sFontName, | 233 CFX_ByteString& sFontName, |
| 255 uint8_t nCharset) { | 234 uint8_t nCharset) { |
| 256 if (IsStandardFont(sFontName)) | 235 if (IsStandardFont(sFontName)) |
| 257 return AddStandardFont(pDoc, sFontName); | 236 return AddStandardFont(pDoc, sFontName); |
| 258 | 237 |
| 259 return AddSystemFont(pDoc, sFontName, nCharset); | 238 return AddSystemFont(pDoc, sFontName, nCharset); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 return EncodeFontAlias(sFontName) + sPostfix; | 277 return EncodeFontAlias(sFontName) + sPostfix; |
| 299 } | 278 } |
| 300 | 279 |
| 301 CFX_ByteString CPWL_FontMap::EncodeFontAlias(const CFX_ByteString& sFontName) { | 280 CFX_ByteString CPWL_FontMap::EncodeFontAlias(const CFX_ByteString& sFontName) { |
| 302 CFX_ByteString sRet = sFontName; | 281 CFX_ByteString sRet = sFontName; |
| 303 sRet.Remove(' '); | 282 sRet.Remove(' '); |
| 304 return sRet; | 283 return sRet; |
| 305 } | 284 } |
| 306 | 285 |
| 307 const CPWL_FontMap_Data* CPWL_FontMap::GetFontMapData(int32_t nIndex) const { | 286 const CPWL_FontMap_Data* CPWL_FontMap::GetFontMapData(int32_t nIndex) const { |
| 308 if (nIndex >= 0 && nIndex < m_aData.GetSize()) { | 287 if (nIndex < 0 || nIndex >= pdfium::CollectionSize<int32_t>(m_Data)) |
| 309 return m_aData.GetAt(nIndex); | 288 return nullptr; |
| 310 } | |
| 311 | 289 |
| 312 return nullptr; | 290 return m_Data[nIndex].get(); |
| 313 } | 291 } |
| 314 | 292 |
| 315 int32_t CPWL_FontMap::GetNativeCharset() { | 293 int32_t CPWL_FontMap::GetNativeCharset() { |
| 316 uint8_t nCharset = FXFONT_ANSI_CHARSET; | 294 uint8_t nCharset = FXFONT_ANSI_CHARSET; |
| 317 int32_t iCodePage = FXSYS_GetACP(); | 295 int32_t iCodePage = FXSYS_GetACP(); |
| 318 switch (iCodePage) { | 296 switch (iCodePage) { |
| 319 case 932: // Japan | 297 case 932: // Japan |
| 320 nCharset = FXFONT_SHIFTJIS_CHARSET; | 298 nCharset = FXFONT_SHIFTJIS_CHARSET; |
| 321 break; | 299 break; |
| 322 case 936: // Chinese (PRC, Singapore) | 300 case 936: // Chinese (PRC, Singapore) |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 439 return FXFONT_RUSSIAN_CHARSET; | 417 return FXFONT_RUSSIAN_CHARSET; |
| 440 | 418 |
| 441 if (word >= 0x0100 && word <= 0x024F) | 419 if (word >= 0x0100 && word <= 0x024F) |
| 442 return FXFONT_EASTEUROPE_CHARSET; | 420 return FXFONT_EASTEUROPE_CHARSET; |
| 443 | 421 |
| 444 if (word >= 0x1E00 && word <= 0x1EFF) | 422 if (word >= 0x1E00 && word <= 0x1EFF) |
| 445 return FXFONT_VIETNAMESE_CHARSET; | 423 return FXFONT_VIETNAMESE_CHARSET; |
| 446 | 424 |
| 447 return FXFONT_ANSI_CHARSET; | 425 return FXFONT_ANSI_CHARSET; |
| 448 } | 426 } |
| OLD | NEW |