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