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