John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 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. |
Lei Zhang | 60f507b | 2015-06-13 00:41:00 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
dan sinclair | 89e904b | 2016-03-23 19:29:15 -0400 | [diff] [blame] | 7 | #include "fpdfsdk/pdfwindow/PWL_FontMap.h" |
Lei Zhang | c2fb35f | 2016-01-05 16:46:58 -0800 | [diff] [blame] | 8 | |
dan sinclair | 61b2fc7 | 2016-03-23 19:21:44 -0400 | [diff] [blame] | 9 | #include "core/fpdfapi/fpdf_font/include/cpdf_font.h" |
| 10 | #include "core/fpdfapi/fpdf_font/include/cpdf_fontencoding.h" |
Dan Sinclair | aa403d3 | 2016-03-15 14:57:22 -0400 | [diff] [blame] | 11 | #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" |
dsinclair | cedaa55 | 2016-08-24 11:12:19 -0700 | [diff] [blame^] | 12 | #include "core/fpdfapi/fpdf_parser/include/cpdf_parser.h" |
Dan Sinclair | aa403d3 | 2016-03-15 14:57:22 -0400 | [diff] [blame] | 13 | #include "core/fpdfapi/include/cpdf_modulemgr.h" |
dsinclair | c7a7349 | 2016-04-05 12:01:42 -0700 | [diff] [blame] | 14 | #include "core/fpdfdoc/include/ipvt_fontmap.h" |
dan sinclair | 89e904b | 2016-03-23 19:29:15 -0400 | [diff] [blame] | 15 | #include "fpdfsdk/pdfwindow/PWL_Wnd.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 16 | |
Lei Zhang | 1b97664 | 2016-01-08 14:24:37 -0800 | [diff] [blame] | 17 | namespace { |
| 18 | |
| 19 | const char kDefaultFontName[] = "Helvetica"; |
| 20 | |
| 21 | const char* const g_sDEStandardFontName[] = {"Courier", |
| 22 | "Courier-Bold", |
| 23 | "Courier-BoldOblique", |
| 24 | "Courier-Oblique", |
| 25 | "Helvetica", |
| 26 | "Helvetica-Bold", |
| 27 | "Helvetica-BoldOblique", |
| 28 | "Helvetica-Oblique", |
| 29 | "Times-Roman", |
| 30 | "Times-Bold", |
| 31 | "Times-Italic", |
| 32 | "Times-BoldItalic", |
| 33 | "Symbol", |
| 34 | "ZapfDingbats"}; |
| 35 | |
| 36 | } // namespace |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 37 | |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 38 | CPWL_FontMap::CPWL_FontMap(CFX_SystemHandler* pSystemHandler) |
weili | 2d5b020 | 2016-08-03 11:06:49 -0700 | [diff] [blame] | 39 | : m_pSystemHandler(pSystemHandler) { |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 40 | ASSERT(m_pSystemHandler); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 43 | CPWL_FontMap::~CPWL_FontMap() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 44 | Empty(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 45 | } |
| 46 | |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 47 | void CPWL_FontMap::SetSystemHandler(CFX_SystemHandler* pSystemHandler) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 48 | m_pSystemHandler = pSystemHandler; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 51 | CPDF_Document* CPWL_FontMap::GetDocument() { |
| 52 | if (!m_pPDFDoc) { |
| 53 | if (CPDF_ModuleMgr::Get()) { |
dsinclair | cedaa55 | 2016-08-24 11:12:19 -0700 | [diff] [blame^] | 54 | m_pPDFDoc.reset(new CPDF_Document(std::unique_ptr<CPDF_Parser>())); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 55 | m_pPDFDoc->CreateNewDoc(); |
| 56 | } |
| 57 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 58 | |
weili | 2d5b020 | 2016-08-03 11:06:49 -0700 | [diff] [blame] | 59 | return m_pPDFDoc.get(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 62 | CPDF_Font* CPWL_FontMap::GetPDFFont(int32_t nFontIndex) { |
| 63 | if (nFontIndex >= 0 && nFontIndex < m_aData.GetSize()) { |
| 64 | if (CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex)) { |
| 65 | return pData->pFont; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 66 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 67 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 68 | |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 69 | return nullptr; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 72 | CFX_ByteString CPWL_FontMap::GetPDFFontAlias(int32_t nFontIndex) { |
| 73 | if (nFontIndex >= 0 && nFontIndex < m_aData.GetSize()) { |
| 74 | if (CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex)) { |
| 75 | return pData->sFontName; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 76 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 77 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 78 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 79 | return ""; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 82 | FX_BOOL CPWL_FontMap::KnowWord(int32_t nFontIndex, uint16_t word) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 83 | if (nFontIndex >= 0 && nFontIndex < m_aData.GetSize()) { |
| 84 | if (m_aData.GetAt(nFontIndex)) { |
| 85 | return CharCodeFromUnicode(nFontIndex, word) >= 0; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 86 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 87 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 88 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 89 | return FALSE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 92 | int32_t CPWL_FontMap::GetWordFontIndex(uint16_t word, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 93 | int32_t nCharset, |
| 94 | int32_t nFontIndex) { |
| 95 | if (nFontIndex > 0) { |
| 96 | if (KnowWord(nFontIndex, word)) |
| 97 | return nFontIndex; |
| 98 | } else { |
| 99 | if (const CPWL_FontMap_Data* pData = GetFontMapData(0)) { |
| 100 | if (nCharset == DEFAULT_CHARSET || pData->nCharset == SYMBOL_CHARSET || |
| 101 | nCharset == pData->nCharset) { |
| 102 | if (KnowWord(0, word)) |
| 103 | return 0; |
| 104 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 105 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 106 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 107 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 108 | int32_t nNewFontIndex = |
| 109 | GetFontIndex(GetNativeFontName(nCharset), nCharset, TRUE); |
| 110 | if (nNewFontIndex >= 0) { |
| 111 | if (KnowWord(nNewFontIndex, word)) |
| 112 | return nNewFontIndex; |
| 113 | } |
| 114 | nNewFontIndex = GetFontIndex("Arial Unicode MS", DEFAULT_CHARSET, FALSE); |
| 115 | if (nNewFontIndex >= 0) { |
| 116 | if (KnowWord(nNewFontIndex, word)) |
| 117 | return nNewFontIndex; |
| 118 | } |
| 119 | return -1; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 122 | int32_t CPWL_FontMap::CharCodeFromUnicode(int32_t nFontIndex, uint16_t word) { |
thestig | 8ea3f51 | 2016-06-27 11:55:24 -0700 | [diff] [blame] | 123 | CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex); |
| 124 | if (!pData) |
| 125 | return -1; |
| 126 | |
| 127 | if (!pData->pFont) |
| 128 | return -1; |
| 129 | |
| 130 | if (pData->pFont->IsUnicodeCompatible()) |
| 131 | return pData->pFont->CharCodeFromUnicode(word); |
| 132 | |
| 133 | return word < 0xFF ? word : -1; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 136 | CFX_ByteString CPWL_FontMap::GetNativeFontName(int32_t nCharset) { |
| 137 | // searching native font is slow, so we must save time |
| 138 | for (int32_t i = 0, sz = m_aNativeFont.GetSize(); i < sz; i++) { |
| 139 | if (CPWL_FontMap_Native* pData = m_aNativeFont.GetAt(i)) { |
| 140 | if (pData->nCharset == nCharset) |
| 141 | return pData->sFontName; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 142 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 143 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 144 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 145 | CFX_ByteString sNew = GetNativeFont(nCharset); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 146 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 147 | if (!sNew.IsEmpty()) { |
| 148 | CPWL_FontMap_Native* pNewData = new CPWL_FontMap_Native; |
| 149 | pNewData->nCharset = nCharset; |
| 150 | pNewData->sFontName = sNew; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 151 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 152 | m_aNativeFont.Add(pNewData); |
| 153 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 154 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 155 | return sNew; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 158 | void CPWL_FontMap::Empty() { |
| 159 | { |
| 160 | for (int32_t i = 0, sz = m_aData.GetSize(); i < sz; i++) |
| 161 | delete m_aData.GetAt(i); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 162 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 163 | m_aData.RemoveAll(); |
| 164 | } |
| 165 | { |
| 166 | for (int32_t i = 0, sz = m_aNativeFont.GetSize(); i < sz; i++) |
| 167 | delete m_aNativeFont.GetAt(i); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 168 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 169 | m_aNativeFont.RemoveAll(); |
| 170 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Lei Zhang | fcfa3b8 | 2015-12-24 21:07:28 -0800 | [diff] [blame] | 173 | void CPWL_FontMap::Initialize() { |
Lei Zhang | 1b97664 | 2016-01-08 14:24:37 -0800 | [diff] [blame] | 174 | GetFontIndex(kDefaultFontName, ANSI_CHARSET, FALSE); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 175 | } |
Lei Zhang | 60f507b | 2015-06-13 00:41:00 -0700 | [diff] [blame] | 176 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 177 | FX_BOOL CPWL_FontMap::IsStandardFont(const CFX_ByteString& sFontName) { |
Wei Li | 8940993 | 2016-03-28 10:33:33 -0700 | [diff] [blame] | 178 | for (size_t i = 0; i < FX_ArraySize(g_sDEStandardFontName); ++i) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 179 | if (sFontName == g_sDEStandardFontName[i]) |
| 180 | return TRUE; |
| 181 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 182 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 183 | return FALSE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 186 | int32_t CPWL_FontMap::FindFont(const CFX_ByteString& sFontName, |
| 187 | int32_t nCharset) { |
| 188 | for (int32_t i = 0, sz = m_aData.GetSize(); i < sz; i++) { |
| 189 | if (CPWL_FontMap_Data* pData = m_aData.GetAt(i)) { |
| 190 | if (nCharset == DEFAULT_CHARSET || nCharset == pData->nCharset) { |
| 191 | if (sFontName.IsEmpty() || pData->sFontName == sFontName) |
| 192 | return i; |
| 193 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 194 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 195 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 196 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 197 | return -1; |
| 198 | } |
| 199 | |
| 200 | int32_t CPWL_FontMap::GetFontIndex(const CFX_ByteString& sFontName, |
| 201 | int32_t nCharset, |
| 202 | FX_BOOL bFind) { |
| 203 | int32_t nFontIndex = FindFont(EncodeFontAlias(sFontName, nCharset), nCharset); |
| 204 | if (nFontIndex >= 0) |
| 205 | return nFontIndex; |
| 206 | |
| 207 | CFX_ByteString sAlias; |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 208 | CPDF_Font* pFont = nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 209 | if (bFind) |
| 210 | pFont = FindFontSameCharset(sAlias, nCharset); |
| 211 | |
| 212 | if (!pFont) { |
| 213 | CFX_ByteString sTemp = sFontName; |
| 214 | pFont = AddFontToDocument(GetDocument(), sTemp, nCharset); |
| 215 | sAlias = EncodeFontAlias(sTemp, nCharset); |
| 216 | } |
| 217 | AddedFont(pFont, sAlias); |
| 218 | return AddFontData(pFont, sAlias, nCharset); |
| 219 | } |
| 220 | |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 221 | int32_t CPWL_FontMap::GetPWLFontIndex(uint16_t word, int32_t nCharset) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 222 | int32_t nFind = -1; |
| 223 | |
| 224 | for (int32_t i = 0, sz = m_aData.GetSize(); i < sz; i++) { |
| 225 | if (CPWL_FontMap_Data* pData = m_aData.GetAt(i)) { |
| 226 | if (pData->nCharset == nCharset) { |
| 227 | nFind = i; |
| 228 | break; |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | CPDF_Font* pNewFont = GetPDFFont(nFind); |
| 234 | |
| 235 | if (!pNewFont) |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 236 | return -1; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 237 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 238 | CFX_ByteString sAlias = EncodeFontAlias("Arial_Chrome", nCharset); |
| 239 | AddedFont(pNewFont, sAlias); |
| 240 | |
| 241 | return AddFontData(pNewFont, sAlias, nCharset); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 244 | CPDF_Font* CPWL_FontMap::FindFontSameCharset(CFX_ByteString& sFontAlias, |
| 245 | int32_t nCharset) { |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 246 | return nullptr; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 249 | int32_t CPWL_FontMap::AddFontData(CPDF_Font* pFont, |
| 250 | const CFX_ByteString& sFontAlias, |
| 251 | int32_t nCharset) { |
| 252 | CPWL_FontMap_Data* pNewData = new CPWL_FontMap_Data; |
| 253 | pNewData->pFont = pFont; |
| 254 | pNewData->sFontName = sFontAlias; |
| 255 | pNewData->nCharset = nCharset; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 256 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 257 | m_aData.Add(pNewData); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 258 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 259 | return m_aData.GetSize() - 1; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 262 | void CPWL_FontMap::AddedFont(CPDF_Font* pFont, |
| 263 | const CFX_ByteString& sFontAlias) {} |
| 264 | |
| 265 | CFX_ByteString CPWL_FontMap::GetFontName(int32_t nFontIndex) { |
| 266 | if (nFontIndex >= 0 && nFontIndex < m_aData.GetSize()) { |
| 267 | if (CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex)) { |
| 268 | return pData->sFontName; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | return ""; |
| 273 | } |
| 274 | |
| 275 | CFX_ByteString CPWL_FontMap::GetNativeFont(int32_t nCharset) { |
| 276 | if (nCharset == DEFAULT_CHARSET) |
| 277 | nCharset = GetNativeCharset(); |
| 278 | |
| 279 | CFX_ByteString sFontName = GetDefaultFontByCharset(nCharset); |
| 280 | if (m_pSystemHandler) { |
thestig | 907a522 | 2016-06-21 14:38:27 -0700 | [diff] [blame] | 281 | if (m_pSystemHandler->FindNativeTrueTypeFont(sFontName)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 282 | return sFontName; |
| 283 | |
thestig | 907a522 | 2016-06-21 14:38:27 -0700 | [diff] [blame] | 284 | sFontName.clear(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 285 | } |
| 286 | return sFontName; |
| 287 | } |
| 288 | |
| 289 | CPDF_Font* CPWL_FontMap::AddFontToDocument(CPDF_Document* pDoc, |
| 290 | CFX_ByteString& sFontName, |
| 291 | uint8_t nCharset) { |
| 292 | if (IsStandardFont(sFontName)) |
| 293 | return AddStandardFont(pDoc, sFontName); |
| 294 | |
| 295 | return AddSystemFont(pDoc, sFontName, nCharset); |
| 296 | } |
| 297 | |
| 298 | CPDF_Font* CPWL_FontMap::AddStandardFont(CPDF_Document* pDoc, |
| 299 | CFX_ByteString& sFontName) { |
| 300 | if (!pDoc) |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 301 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 302 | |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 303 | CPDF_Font* pFont = nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 304 | |
Lei Zhang | c2fb35f | 2016-01-05 16:46:58 -0800 | [diff] [blame] | 305 | if (sFontName == "ZapfDingbats") { |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 306 | pFont = pDoc->AddStandardFont(sFontName.c_str(), nullptr); |
Lei Zhang | c2fb35f | 2016-01-05 16:46:58 -0800 | [diff] [blame] | 307 | } else { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 308 | CPDF_FontEncoding fe(PDFFONT_ENCODING_WINANSI); |
tsepez | b4c9f3f | 2016-04-13 15:41:21 -0700 | [diff] [blame] | 309 | pFont = pDoc->AddStandardFont(sFontName.c_str(), &fe); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | return pFont; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 315 | CPDF_Font* CPWL_FontMap::AddSystemFont(CPDF_Document* pDoc, |
| 316 | CFX_ByteString& sFontName, |
| 317 | uint8_t nCharset) { |
| 318 | if (!pDoc) |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 319 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 320 | |
| 321 | if (sFontName.IsEmpty()) |
| 322 | sFontName = GetNativeFont(nCharset); |
| 323 | if (nCharset == DEFAULT_CHARSET) |
| 324 | nCharset = GetNativeCharset(); |
| 325 | |
| 326 | if (m_pSystemHandler) |
| 327 | return m_pSystemHandler->AddNativeTrueTypeFontToPDF(pDoc, sFontName, |
| 328 | nCharset); |
| 329 | |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 330 | return nullptr; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 333 | CFX_ByteString CPWL_FontMap::EncodeFontAlias(const CFX_ByteString& sFontName, |
| 334 | int32_t nCharset) { |
| 335 | CFX_ByteString sPostfix; |
| 336 | sPostfix.Format("_%02X", nCharset); |
| 337 | return EncodeFontAlias(sFontName) + sPostfix; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 340 | CFX_ByteString CPWL_FontMap::EncodeFontAlias(const CFX_ByteString& sFontName) { |
| 341 | CFX_ByteString sRet = sFontName; |
| 342 | sRet.Remove(' '); |
| 343 | return sRet; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 346 | int32_t CPWL_FontMap::GetFontMapCount() const { |
| 347 | return m_aData.GetSize(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 350 | const CPWL_FontMap_Data* CPWL_FontMap::GetFontMapData(int32_t nIndex) const { |
| 351 | if (nIndex >= 0 && nIndex < m_aData.GetSize()) { |
| 352 | return m_aData.GetAt(nIndex); |
| 353 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 354 | |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 355 | return nullptr; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 358 | int32_t CPWL_FontMap::GetNativeCharset() { |
| 359 | uint8_t nCharset = ANSI_CHARSET; |
| 360 | int32_t iCodePage = FXSYS_GetACP(); |
| 361 | switch (iCodePage) { |
| 362 | case 932: // Japan |
| 363 | nCharset = SHIFTJIS_CHARSET; |
| 364 | break; |
| 365 | case 936: // Chinese (PRC, Singapore) |
| 366 | nCharset = GB2312_CHARSET; |
| 367 | break; |
| 368 | case 950: // Chinese (Taiwan; Hong Kong SAR, PRC) |
| 369 | nCharset = GB2312_CHARSET; |
| 370 | break; |
| 371 | case 1252: // Windows 3.1 Latin 1 (US, Western Europe) |
| 372 | nCharset = ANSI_CHARSET; |
| 373 | break; |
| 374 | case 874: // Thai |
| 375 | nCharset = THAI_CHARSET; |
| 376 | break; |
| 377 | case 949: // Korean |
| 378 | nCharset = HANGUL_CHARSET; |
| 379 | break; |
| 380 | case 1200: // Unicode (BMP of ISO 10646) |
| 381 | nCharset = ANSI_CHARSET; |
| 382 | break; |
| 383 | case 1250: // Windows 3.1 Eastern European |
| 384 | nCharset = EASTEUROPE_CHARSET; |
| 385 | break; |
| 386 | case 1251: // Windows 3.1 Cyrillic |
| 387 | nCharset = RUSSIAN_CHARSET; |
| 388 | break; |
| 389 | case 1253: // Windows 3.1 Greek |
| 390 | nCharset = GREEK_CHARSET; |
| 391 | break; |
| 392 | case 1254: // Windows 3.1 Turkish |
| 393 | nCharset = TURKISH_CHARSET; |
| 394 | break; |
| 395 | case 1255: // Hebrew |
| 396 | nCharset = HEBREW_CHARSET; |
| 397 | break; |
| 398 | case 1256: // Arabic |
| 399 | nCharset = ARABIC_CHARSET; |
| 400 | break; |
| 401 | case 1257: // Baltic |
| 402 | nCharset = BALTIC_CHARSET; |
| 403 | break; |
| 404 | case 1258: // Vietnamese |
| 405 | nCharset = VIETNAMESE_CHARSET; |
| 406 | break; |
| 407 | case 1361: // Korean(Johab) |
| 408 | nCharset = JOHAB_CHARSET; |
| 409 | break; |
| 410 | } |
| 411 | return nCharset; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 412 | } |
| 413 | |
weili | 2d5b020 | 2016-08-03 11:06:49 -0700 | [diff] [blame] | 414 | const FPDF_CharsetFontMap CPWL_FontMap::defaultTTFMap[] = { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 415 | {ANSI_CHARSET, "Helvetica"}, {GB2312_CHARSET, "SimSun"}, |
| 416 | {CHINESEBIG5_CHARSET, "MingLiU"}, {SHIFTJIS_CHARSET, "MS Gothic"}, |
| 417 | {HANGUL_CHARSET, "Batang"}, {RUSSIAN_CHARSET, "Arial"}, |
| 418 | #if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ || \ |
| 419 | _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
| 420 | {EASTEUROPE_CHARSET, "Arial"}, |
Bo Xu | dbd4c06 | 2014-05-29 11:32:56 -0700 | [diff] [blame] | 421 | #else |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 422 | {EASTEUROPE_CHARSET, "Tahoma"}, |
Bo Xu | dbd4c06 | 2014-05-29 11:32:56 -0700 | [diff] [blame] | 423 | #endif |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 424 | {ARABIC_CHARSET, "Arial"}, {-1, nullptr}}; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 425 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 426 | CFX_ByteString CPWL_FontMap::GetDefaultFontByCharset(int32_t nCharset) { |
| 427 | int i = 0; |
| 428 | while (defaultTTFMap[i].charset != -1) { |
| 429 | if (nCharset == defaultTTFMap[i].charset) |
| 430 | return defaultTTFMap[i].fontname; |
| 431 | ++i; |
| 432 | } |
| 433 | return ""; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 436 | int32_t CPWL_FontMap::CharSetFromUnicode(uint16_t word, int32_t nOldCharset) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 437 | // to avoid CJK Font to show ASCII |
| 438 | if (word < 0x7F) |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 439 | return ANSI_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 440 | // follow the old charset |
| 441 | if (nOldCharset != DEFAULT_CHARSET) |
| 442 | return nOldCharset; |
| 443 | |
| 444 | // find new charset |
| 445 | if ((word >= 0x4E00 && word <= 0x9FA5) || |
| 446 | (word >= 0xE7C7 && word <= 0xE7F3) || |
| 447 | (word >= 0x3000 && word <= 0x303F) || |
| 448 | (word >= 0x2000 && word <= 0x206F)) { |
| 449 | return GB2312_CHARSET; |
| 450 | } |
| 451 | |
| 452 | if (((word >= 0x3040) && (word <= 0x309F)) || |
| 453 | ((word >= 0x30A0) && (word <= 0x30FF)) || |
| 454 | ((word >= 0x31F0) && (word <= 0x31FF)) || |
| 455 | ((word >= 0xFF00) && (word <= 0xFFEF))) { |
| 456 | return SHIFTJIS_CHARSET; |
| 457 | } |
| 458 | |
| 459 | if (((word >= 0xAC00) && (word <= 0xD7AF)) || |
| 460 | ((word >= 0x1100) && (word <= 0x11FF)) || |
| 461 | ((word >= 0x3130) && (word <= 0x318F))) { |
| 462 | return HANGUL_CHARSET; |
| 463 | } |
| 464 | |
| 465 | if (word >= 0x0E00 && word <= 0x0E7F) |
| 466 | return THAI_CHARSET; |
| 467 | |
| 468 | if ((word >= 0x0370 && word <= 0x03FF) || (word >= 0x1F00 && word <= 0x1FFF)) |
| 469 | return GREEK_CHARSET; |
| 470 | |
| 471 | if ((word >= 0x0600 && word <= 0x06FF) || (word >= 0xFB50 && word <= 0xFEFC)) |
| 472 | return ARABIC_CHARSET; |
| 473 | |
| 474 | if (word >= 0x0590 && word <= 0x05FF) |
| 475 | return HEBREW_CHARSET; |
| 476 | |
| 477 | if (word >= 0x0400 && word <= 0x04FF) |
| 478 | return RUSSIAN_CHARSET; |
| 479 | |
| 480 | if (word >= 0x0100 && word <= 0x024F) |
| 481 | return EASTEUROPE_CHARSET; |
| 482 | |
| 483 | if (word >= 0x1E00 && word <= 0x1EFF) |
| 484 | return VIETNAMESE_CHARSET; |
| 485 | |
| 486 | return ANSI_CHARSET; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 487 | } |
| 488 | |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 489 | CPWL_DocFontMap::CPWL_DocFontMap(CFX_SystemHandler* pSystemHandler, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 490 | CPDF_Document* pAttachedDoc) |
| 491 | : CPWL_FontMap(pSystemHandler), m_pAttachedDoc(pAttachedDoc) {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 492 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 493 | CPWL_DocFontMap::~CPWL_DocFontMap() {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 494 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 495 | CPDF_Document* CPWL_DocFontMap::GetDocument() { |
| 496 | return m_pAttachedDoc; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 497 | } |