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