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