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