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 | |
dsinclair | 39c62fd | 2016-09-29 12:49:17 -0700 | [diff] [blame^] | 9 | #include "core/fpdfapi/cpdf_modulemgr.h" |
dsinclair | 166bc16 | 2016-09-29 11:58:38 -0700 | [diff] [blame] | 10 | #include "core/fpdfapi/fpdf_font/cpdf_font.h" |
| 11 | #include "core/fpdfapi/fpdf_font/cpdf_fontencoding.h" |
dsinclair | c6c425a | 2016-09-29 12:01:30 -0700 | [diff] [blame] | 12 | #include "core/fpdfapi/fpdf_parser/cpdf_document.h" |
| 13 | #include "core/fpdfapi/fpdf_parser/cpdf_parser.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()) { |
thestig | d4c34f2 | 2016-09-28 17:04:51 -0700 | [diff] [blame] | 50 | m_pPDFDoc = WrapUnique(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)) { |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 96 | if (nCharset == FXFONT_DEFAULT_CHARSET || |
| 97 | pData->nCharset == FXFONT_SYMBOL_CHARSET || |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 98 | nCharset == pData->nCharset) { |
| 99 | if (KnowWord(0, word)) |
| 100 | return 0; |
| 101 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 102 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 103 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 104 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 105 | int32_t nNewFontIndex = |
| 106 | GetFontIndex(GetNativeFontName(nCharset), nCharset, TRUE); |
| 107 | if (nNewFontIndex >= 0) { |
| 108 | if (KnowWord(nNewFontIndex, word)) |
| 109 | return nNewFontIndex; |
| 110 | } |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 111 | nNewFontIndex = |
| 112 | GetFontIndex("Arial Unicode MS", FXFONT_DEFAULT_CHARSET, FALSE); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 113 | if (nNewFontIndex >= 0) { |
| 114 | if (KnowWord(nNewFontIndex, word)) |
| 115 | return nNewFontIndex; |
| 116 | } |
| 117 | return -1; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 120 | int32_t CPWL_FontMap::CharCodeFromUnicode(int32_t nFontIndex, uint16_t word) { |
thestig | 8ea3f51 | 2016-06-27 11:55:24 -0700 | [diff] [blame] | 121 | CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex); |
| 122 | if (!pData) |
| 123 | return -1; |
| 124 | |
| 125 | if (!pData->pFont) |
| 126 | return -1; |
| 127 | |
| 128 | if (pData->pFont->IsUnicodeCompatible()) |
| 129 | return pData->pFont->CharCodeFromUnicode(word); |
| 130 | |
| 131 | return word < 0xFF ? word : -1; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 134 | CFX_ByteString CPWL_FontMap::GetNativeFontName(int32_t nCharset) { |
| 135 | // searching native font is slow, so we must save time |
| 136 | for (int32_t i = 0, sz = m_aNativeFont.GetSize(); i < sz; i++) { |
| 137 | if (CPWL_FontMap_Native* pData = m_aNativeFont.GetAt(i)) { |
| 138 | if (pData->nCharset == nCharset) |
| 139 | return pData->sFontName; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 140 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 141 | } |
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 | CFX_ByteString sNew = GetNativeFont(nCharset); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 144 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 145 | if (!sNew.IsEmpty()) { |
| 146 | CPWL_FontMap_Native* pNewData = new CPWL_FontMap_Native; |
| 147 | pNewData->nCharset = nCharset; |
| 148 | pNewData->sFontName = sNew; |
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.Add(pNewData); |
| 151 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 152 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 153 | return sNew; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 156 | void CPWL_FontMap::Empty() { |
| 157 | { |
| 158 | for (int32_t i = 0, sz = m_aData.GetSize(); i < sz; i++) |
| 159 | delete m_aData.GetAt(i); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 160 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 161 | m_aData.RemoveAll(); |
| 162 | } |
| 163 | { |
| 164 | for (int32_t i = 0, sz = m_aNativeFont.GetSize(); i < sz; i++) |
| 165 | delete m_aNativeFont.GetAt(i); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 166 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 167 | m_aNativeFont.RemoveAll(); |
| 168 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Lei Zhang | fcfa3b8 | 2015-12-24 21:07:28 -0800 | [diff] [blame] | 171 | void CPWL_FontMap::Initialize() { |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 172 | GetFontIndex(kDefaultFontName, FXFONT_ANSI_CHARSET, FALSE); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 173 | } |
Lei Zhang | 60f507b | 2015-06-13 00:41:00 -0700 | [diff] [blame] | 174 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 175 | FX_BOOL CPWL_FontMap::IsStandardFont(const CFX_ByteString& sFontName) { |
Wei Li | 8940993 | 2016-03-28 10:33:33 -0700 | [diff] [blame] | 176 | for (size_t i = 0; i < FX_ArraySize(g_sDEStandardFontName); ++i) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 177 | if (sFontName == g_sDEStandardFontName[i]) |
| 178 | return TRUE; |
| 179 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 180 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 181 | return FALSE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 184 | int32_t CPWL_FontMap::FindFont(const CFX_ByteString& sFontName, |
| 185 | int32_t nCharset) { |
| 186 | for (int32_t i = 0, sz = m_aData.GetSize(); i < sz; i++) { |
| 187 | if (CPWL_FontMap_Data* pData = m_aData.GetAt(i)) { |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 188 | if (nCharset == FXFONT_DEFAULT_CHARSET || nCharset == pData->nCharset) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 189 | if (sFontName.IsEmpty() || pData->sFontName == sFontName) |
| 190 | return i; |
| 191 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 192 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 193 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 194 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 195 | return -1; |
| 196 | } |
| 197 | |
| 198 | int32_t CPWL_FontMap::GetFontIndex(const CFX_ByteString& sFontName, |
| 199 | int32_t nCharset, |
| 200 | FX_BOOL bFind) { |
| 201 | int32_t nFontIndex = FindFont(EncodeFontAlias(sFontName, nCharset), nCharset); |
| 202 | if (nFontIndex >= 0) |
| 203 | return nFontIndex; |
| 204 | |
| 205 | CFX_ByteString sAlias; |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 206 | CPDF_Font* pFont = nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 207 | if (bFind) |
| 208 | pFont = FindFontSameCharset(sAlias, nCharset); |
| 209 | |
| 210 | if (!pFont) { |
| 211 | CFX_ByteString sTemp = sFontName; |
| 212 | pFont = AddFontToDocument(GetDocument(), sTemp, nCharset); |
| 213 | sAlias = EncodeFontAlias(sTemp, nCharset); |
| 214 | } |
| 215 | AddedFont(pFont, sAlias); |
| 216 | return AddFontData(pFont, sAlias, nCharset); |
| 217 | } |
| 218 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 219 | CPDF_Font* CPWL_FontMap::FindFontSameCharset(CFX_ByteString& sFontAlias, |
| 220 | int32_t nCharset) { |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 221 | return nullptr; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 224 | int32_t CPWL_FontMap::AddFontData(CPDF_Font* pFont, |
| 225 | const CFX_ByteString& sFontAlias, |
| 226 | int32_t nCharset) { |
| 227 | CPWL_FontMap_Data* pNewData = new CPWL_FontMap_Data; |
| 228 | pNewData->pFont = pFont; |
| 229 | pNewData->sFontName = sFontAlias; |
| 230 | pNewData->nCharset = nCharset; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 231 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 232 | m_aData.Add(pNewData); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 233 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 234 | return m_aData.GetSize() - 1; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 237 | void CPWL_FontMap::AddedFont(CPDF_Font* pFont, |
| 238 | const CFX_ByteString& sFontAlias) {} |
| 239 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 240 | CFX_ByteString CPWL_FontMap::GetNativeFont(int32_t nCharset) { |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 241 | if (nCharset == FXFONT_DEFAULT_CHARSET) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 242 | nCharset = GetNativeCharset(); |
| 243 | |
| 244 | CFX_ByteString sFontName = GetDefaultFontByCharset(nCharset); |
dsinclair | 0e3e890 | 2016-08-24 11:39:24 -0700 | [diff] [blame] | 245 | if (m_pSystemHandler->FindNativeTrueTypeFont(sFontName)) |
| 246 | return sFontName; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 247 | |
dsinclair | 0e3e890 | 2016-08-24 11:39:24 -0700 | [diff] [blame] | 248 | sFontName.clear(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 249 | return sFontName; |
| 250 | } |
| 251 | |
| 252 | CPDF_Font* CPWL_FontMap::AddFontToDocument(CPDF_Document* pDoc, |
| 253 | CFX_ByteString& sFontName, |
| 254 | uint8_t nCharset) { |
| 255 | if (IsStandardFont(sFontName)) |
| 256 | return AddStandardFont(pDoc, sFontName); |
| 257 | |
| 258 | return AddSystemFont(pDoc, sFontName, nCharset); |
| 259 | } |
| 260 | |
| 261 | CPDF_Font* CPWL_FontMap::AddStandardFont(CPDF_Document* pDoc, |
| 262 | CFX_ByteString& sFontName) { |
| 263 | if (!pDoc) |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 264 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 265 | |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 266 | CPDF_Font* pFont = nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 267 | |
Lei Zhang | c2fb35f | 2016-01-05 16:46:58 -0800 | [diff] [blame] | 268 | if (sFontName == "ZapfDingbats") { |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 269 | pFont = pDoc->AddStandardFont(sFontName.c_str(), nullptr); |
Lei Zhang | c2fb35f | 2016-01-05 16:46:58 -0800 | [diff] [blame] | 270 | } else { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 271 | CPDF_FontEncoding fe(PDFFONT_ENCODING_WINANSI); |
tsepez | b4c9f3f | 2016-04-13 15:41:21 -0700 | [diff] [blame] | 272 | pFont = pDoc->AddStandardFont(sFontName.c_str(), &fe); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | return pFont; |
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 | CPDF_Font* CPWL_FontMap::AddSystemFont(CPDF_Document* pDoc, |
| 279 | CFX_ByteString& sFontName, |
| 280 | uint8_t nCharset) { |
| 281 | if (!pDoc) |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 282 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 283 | |
| 284 | if (sFontName.IsEmpty()) |
| 285 | sFontName = GetNativeFont(nCharset); |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 286 | if (nCharset == FXFONT_DEFAULT_CHARSET) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 287 | nCharset = GetNativeCharset(); |
| 288 | |
dsinclair | 0e3e890 | 2016-08-24 11:39:24 -0700 | [diff] [blame] | 289 | return m_pSystemHandler->AddNativeTrueTypeFontToPDF(pDoc, sFontName, |
| 290 | nCharset); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 293 | CFX_ByteString CPWL_FontMap::EncodeFontAlias(const CFX_ByteString& sFontName, |
| 294 | int32_t nCharset) { |
| 295 | CFX_ByteString sPostfix; |
| 296 | sPostfix.Format("_%02X", nCharset); |
| 297 | return EncodeFontAlias(sFontName) + sPostfix; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 300 | CFX_ByteString CPWL_FontMap::EncodeFontAlias(const CFX_ByteString& sFontName) { |
| 301 | CFX_ByteString sRet = sFontName; |
| 302 | sRet.Remove(' '); |
| 303 | return sRet; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 306 | const CPWL_FontMap_Data* CPWL_FontMap::GetFontMapData(int32_t nIndex) const { |
| 307 | if (nIndex >= 0 && nIndex < m_aData.GetSize()) { |
| 308 | return m_aData.GetAt(nIndex); |
| 309 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 310 | |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 311 | return nullptr; |
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 | int32_t CPWL_FontMap::GetNativeCharset() { |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 315 | uint8_t nCharset = FXFONT_ANSI_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 316 | int32_t iCodePage = FXSYS_GetACP(); |
| 317 | switch (iCodePage) { |
| 318 | case 932: // Japan |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 319 | nCharset = FXFONT_SHIFTJIS_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 320 | break; |
| 321 | case 936: // Chinese (PRC, Singapore) |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 322 | nCharset = FXFONT_GB2312_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 323 | break; |
| 324 | case 950: // Chinese (Taiwan; Hong Kong SAR, PRC) |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 325 | nCharset = FXFONT_GB2312_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 326 | break; |
| 327 | case 1252: // Windows 3.1 Latin 1 (US, Western Europe) |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 328 | nCharset = FXFONT_ANSI_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 329 | break; |
| 330 | case 874: // Thai |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 331 | nCharset = FXFONT_THAI_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 332 | break; |
| 333 | case 949: // Korean |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 334 | nCharset = FXFONT_HANGUL_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 335 | break; |
| 336 | case 1200: // Unicode (BMP of ISO 10646) |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 337 | nCharset = FXFONT_ANSI_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 338 | break; |
| 339 | case 1250: // Windows 3.1 Eastern European |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 340 | nCharset = FXFONT_EASTEUROPE_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 341 | break; |
| 342 | case 1251: // Windows 3.1 Cyrillic |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 343 | nCharset = FXFONT_RUSSIAN_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 344 | break; |
| 345 | case 1253: // Windows 3.1 Greek |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 346 | nCharset = FXFONT_GREEK_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 347 | break; |
| 348 | case 1254: // Windows 3.1 Turkish |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 349 | nCharset = FXFONT_TURKISH_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 350 | break; |
| 351 | case 1255: // Hebrew |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 352 | nCharset = FXFONT_HEBREW_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 353 | break; |
| 354 | case 1256: // Arabic |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 355 | nCharset = FXFONT_ARABIC_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 356 | break; |
| 357 | case 1257: // Baltic |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 358 | nCharset = FXFONT_BALTIC_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 359 | break; |
| 360 | case 1258: // Vietnamese |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 361 | nCharset = FXFONT_VIETNAMESE_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 362 | break; |
| 363 | case 1361: // Korean(Johab) |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 364 | nCharset = FXFONT_JOHAB_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 365 | break; |
| 366 | } |
| 367 | return nCharset; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 368 | } |
| 369 | |
weili | 2d5b020 | 2016-08-03 11:06:49 -0700 | [diff] [blame] | 370 | const FPDF_CharsetFontMap CPWL_FontMap::defaultTTFMap[] = { |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 371 | {FXFONT_ANSI_CHARSET, "Helvetica"}, |
| 372 | {FXFONT_GB2312_CHARSET, "SimSun"}, |
| 373 | {FXFONT_CHINESEBIG5_CHARSET, "MingLiU"}, |
| 374 | {FXFONT_SHIFTJIS_CHARSET, "MS Gothic"}, |
| 375 | {FXFONT_HANGUL_CHARSET, "Batang"}, |
| 376 | {FXFONT_RUSSIAN_CHARSET, "Arial"}, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 377 | #if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ || \ |
| 378 | _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 379 | {FXFONT_EASTEUROPE_CHARSET, "Arial"}, |
Bo Xu | dbd4c06 | 2014-05-29 11:32:56 -0700 | [diff] [blame] | 380 | #else |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 381 | {FXFONT_EASTEUROPE_CHARSET, "Tahoma"}, |
Bo Xu | dbd4c06 | 2014-05-29 11:32:56 -0700 | [diff] [blame] | 382 | #endif |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 383 | {FXFONT_ARABIC_CHARSET, "Arial"}, |
| 384 | {-1, nullptr}}; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 385 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 386 | CFX_ByteString CPWL_FontMap::GetDefaultFontByCharset(int32_t nCharset) { |
| 387 | int i = 0; |
| 388 | while (defaultTTFMap[i].charset != -1) { |
| 389 | if (nCharset == defaultTTFMap[i].charset) |
| 390 | return defaultTTFMap[i].fontname; |
| 391 | ++i; |
| 392 | } |
| 393 | return ""; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 396 | int32_t CPWL_FontMap::CharSetFromUnicode(uint16_t word, int32_t nOldCharset) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 397 | // to avoid CJK Font to show ASCII |
| 398 | if (word < 0x7F) |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 399 | return FXFONT_ANSI_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 400 | // follow the old charset |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 401 | if (nOldCharset != FXFONT_DEFAULT_CHARSET) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 402 | return nOldCharset; |
| 403 | |
| 404 | // find new charset |
| 405 | if ((word >= 0x4E00 && word <= 0x9FA5) || |
| 406 | (word >= 0xE7C7 && word <= 0xE7F3) || |
| 407 | (word >= 0x3000 && word <= 0x303F) || |
| 408 | (word >= 0x2000 && word <= 0x206F)) { |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 409 | return FXFONT_GB2312_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | if (((word >= 0x3040) && (word <= 0x309F)) || |
| 413 | ((word >= 0x30A0) && (word <= 0x30FF)) || |
| 414 | ((word >= 0x31F0) && (word <= 0x31FF)) || |
| 415 | ((word >= 0xFF00) && (word <= 0xFFEF))) { |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 416 | return FXFONT_SHIFTJIS_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | if (((word >= 0xAC00) && (word <= 0xD7AF)) || |
| 420 | ((word >= 0x1100) && (word <= 0x11FF)) || |
| 421 | ((word >= 0x3130) && (word <= 0x318F))) { |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 422 | return FXFONT_HANGUL_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | if (word >= 0x0E00 && word <= 0x0E7F) |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 426 | return FXFONT_THAI_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 427 | |
| 428 | if ((word >= 0x0370 && word <= 0x03FF) || (word >= 0x1F00 && word <= 0x1FFF)) |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 429 | return FXFONT_GREEK_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 430 | |
| 431 | if ((word >= 0x0600 && word <= 0x06FF) || (word >= 0xFB50 && word <= 0xFEFC)) |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 432 | return FXFONT_ARABIC_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 433 | |
| 434 | if (word >= 0x0590 && word <= 0x05FF) |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 435 | return FXFONT_HEBREW_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 436 | |
| 437 | if (word >= 0x0400 && word <= 0x04FF) |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 438 | return FXFONT_RUSSIAN_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 439 | |
| 440 | if (word >= 0x0100 && word <= 0x024F) |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 441 | return FXFONT_EASTEUROPE_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 442 | |
| 443 | if (word >= 0x1E00 && word <= 0x1EFF) |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 444 | return FXFONT_VIETNAMESE_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 445 | |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 446 | return FXFONT_ANSI_CHARSET; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 447 | } |