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