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 | |
tsepez | 6745f96 | 2017-01-04 10:09:45 -0800 | [diff] [blame] | 9 | #include <utility> |
| 10 | |
dsinclair | 39c62fd | 2016-09-29 12:49:17 -0700 | [diff] [blame] | 11 | #include "core/fpdfapi/cpdf_modulemgr.h" |
dsinclair | bc5e6d2 | 2016-10-04 11:08:49 -0700 | [diff] [blame] | 12 | #include "core/fpdfapi/font/cpdf_font.h" |
| 13 | #include "core/fpdfapi/font/cpdf_fontencoding.h" |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 14 | #include "core/fpdfapi/parser/cpdf_document.h" |
| 15 | #include "core/fpdfapi/parser/cpdf_parser.h" |
dsinclair | 1727aee | 2016-09-29 13:12:56 -0700 | [diff] [blame] | 16 | #include "core/fpdfdoc/ipvt_fontmap.h" |
dan sinclair | 89e904b | 2016-03-23 19:29:15 -0400 | [diff] [blame] | 17 | #include "fpdfsdk/pdfwindow/PWL_Wnd.h" |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 18 | #include "third_party/base/ptr_util.h" |
tsepez | 6745f96 | 2017-01-04 10:09:45 -0800 | [diff] [blame] | 19 | #include "third_party/base/stl_util.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 20 | |
Lei Zhang | 1b97664 | 2016-01-08 14:24:37 -0800 | [diff] [blame] | 21 | namespace { |
| 22 | |
| 23 | const char kDefaultFontName[] = "Helvetica"; |
| 24 | |
| 25 | const char* const g_sDEStandardFontName[] = {"Courier", |
| 26 | "Courier-Bold", |
| 27 | "Courier-BoldOblique", |
| 28 | "Courier-Oblique", |
| 29 | "Helvetica", |
| 30 | "Helvetica-Bold", |
| 31 | "Helvetica-BoldOblique", |
| 32 | "Helvetica-Oblique", |
| 33 | "Times-Roman", |
| 34 | "Times-Bold", |
| 35 | "Times-Italic", |
| 36 | "Times-BoldItalic", |
| 37 | "Symbol", |
| 38 | "ZapfDingbats"}; |
| 39 | |
| 40 | } // namespace |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 41 | |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 42 | CPWL_FontMap::CPWL_FontMap(CFX_SystemHandler* pSystemHandler) |
weili | 2d5b020 | 2016-08-03 11:06:49 -0700 | [diff] [blame] | 43 | : m_pSystemHandler(pSystemHandler) { |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 44 | ASSERT(m_pSystemHandler); |
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 | CPWL_FontMap::~CPWL_FontMap() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 48 | Empty(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 51 | CPDF_Document* CPWL_FontMap::GetDocument() { |
| 52 | if (!m_pPDFDoc) { |
| 53 | if (CPDF_ModuleMgr::Get()) { |
tsepez | e5cb0b1 | 2016-10-26 15:06:11 -0700 | [diff] [blame] | 54 | m_pPDFDoc = pdfium::MakeUnique<CPDF_Document>(nullptr); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 55 | m_pPDFDoc->CreateNewDoc(); |
| 56 | } |
| 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) { |
Tom Sepez | 193e6ca | 2017-03-14 15:53:36 -0700 | [diff] [blame] | 62 | if (pdfium::IndexInBounds(m_Data, nFontIndex) && m_Data[nFontIndex]) |
| 63 | return m_Data[nFontIndex]->pFont; |
| 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) { |
Tom Sepez | 193e6ca | 2017-03-14 15:53:36 -0700 | [diff] [blame] | 69 | if (pdfium::IndexInBounds(m_Data, nFontIndex) && m_Data[nFontIndex]) |
| 70 | return m_Data[nFontIndex]->sFontName; |
| 71 | |
tsepez | 6745f96 | 2017-01-04 10:09:45 -0800 | [diff] [blame] | 72 | return CFX_ByteString(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 73 | } |
| 74 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 75 | bool CPWL_FontMap::KnowWord(int32_t nFontIndex, uint16_t word) { |
Tom Sepez | 193e6ca | 2017-03-14 15:53:36 -0700 | [diff] [blame] | 76 | return pdfium::IndexInBounds(m_Data, nFontIndex) && m_Data[nFontIndex] && |
| 77 | CharCodeFromUnicode(nFontIndex, word) >= 0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 80 | int32_t CPWL_FontMap::GetWordFontIndex(uint16_t word, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 81 | int32_t nCharset, |
| 82 | int32_t nFontIndex) { |
| 83 | if (nFontIndex > 0) { |
| 84 | if (KnowWord(nFontIndex, word)) |
| 85 | return nFontIndex; |
| 86 | } else { |
| 87 | if (const CPWL_FontMap_Data* pData = GetFontMapData(0)) { |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 88 | if (nCharset == FXFONT_DEFAULT_CHARSET || |
| 89 | pData->nCharset == FXFONT_SYMBOL_CHARSET || |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 90 | nCharset == pData->nCharset) { |
| 91 | if (KnowWord(0, word)) |
| 92 | return 0; |
| 93 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 94 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 95 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 96 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 97 | int32_t nNewFontIndex = |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 98 | GetFontIndex(GetNativeFontName(nCharset), nCharset, true); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 99 | if (nNewFontIndex >= 0) { |
| 100 | if (KnowWord(nNewFontIndex, word)) |
| 101 | return nNewFontIndex; |
| 102 | } |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 103 | nNewFontIndex = |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 104 | GetFontIndex("Arial Unicode MS", FXFONT_DEFAULT_CHARSET, false); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 105 | if (nNewFontIndex >= 0) { |
| 106 | if (KnowWord(nNewFontIndex, word)) |
| 107 | return nNewFontIndex; |
| 108 | } |
| 109 | return -1; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 112 | int32_t CPWL_FontMap::CharCodeFromUnicode(int32_t nFontIndex, uint16_t word) { |
Tom Sepez | 193e6ca | 2017-03-14 15:53:36 -0700 | [diff] [blame] | 113 | if (!pdfium::IndexInBounds(m_Data, nFontIndex)) |
thestig | 8ea3f51 | 2016-06-27 11:55:24 -0700 | [diff] [blame] | 114 | return -1; |
| 115 | |
tsepez | 6745f96 | 2017-01-04 10:09:45 -0800 | [diff] [blame] | 116 | CPWL_FontMap_Data* pData = m_Data[nFontIndex].get(); |
| 117 | if (!pData || !pData->pFont) |
thestig | 8ea3f51 | 2016-06-27 11:55:24 -0700 | [diff] [blame] | 118 | return -1; |
| 119 | |
| 120 | if (pData->pFont->IsUnicodeCompatible()) |
| 121 | return pData->pFont->CharCodeFromUnicode(word); |
| 122 | |
| 123 | return word < 0xFF ? word : -1; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 126 | CFX_ByteString CPWL_FontMap::GetNativeFontName(int32_t nCharset) { |
tsepez | 6745f96 | 2017-01-04 10:09:45 -0800 | [diff] [blame] | 127 | for (const auto& pData : m_NativeFont) { |
| 128 | if (pData && pData->nCharset == nCharset) |
| 129 | return pData->sFontName; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 130 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 131 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 132 | CFX_ByteString sNew = GetNativeFont(nCharset); |
tsepez | 6745f96 | 2017-01-04 10:09:45 -0800 | [diff] [blame] | 133 | if (sNew.IsEmpty()) |
| 134 | return CFX_ByteString(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 135 | |
tsepez | 6745f96 | 2017-01-04 10:09:45 -0800 | [diff] [blame] | 136 | auto pNewData = pdfium::MakeUnique<CPWL_FontMap_Native>(); |
| 137 | pNewData->nCharset = nCharset; |
| 138 | pNewData->sFontName = sNew; |
| 139 | m_NativeFont.push_back(std::move(pNewData)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 140 | return sNew; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 143 | void CPWL_FontMap::Empty() { |
tsepez | 6745f96 | 2017-01-04 10:09:45 -0800 | [diff] [blame] | 144 | m_Data.clear(); |
| 145 | m_NativeFont.clear(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Lei Zhang | fcfa3b8 | 2015-12-24 21:07:28 -0800 | [diff] [blame] | 148 | void CPWL_FontMap::Initialize() { |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 149 | GetFontIndex(kDefaultFontName, FXFONT_ANSI_CHARSET, false); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 150 | } |
Lei Zhang | 60f507b | 2015-06-13 00:41:00 -0700 | [diff] [blame] | 151 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 152 | bool CPWL_FontMap::IsStandardFont(const CFX_ByteString& sFontName) { |
Wei Li | 8940993 | 2016-03-28 10:33:33 -0700 | [diff] [blame] | 153 | for (size_t i = 0; i < FX_ArraySize(g_sDEStandardFontName); ++i) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 154 | if (sFontName == g_sDEStandardFontName[i]) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 155 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 156 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 157 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 158 | return false; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 161 | int32_t CPWL_FontMap::FindFont(const CFX_ByteString& sFontName, |
| 162 | int32_t nCharset) { |
tsepez | 6745f96 | 2017-01-04 10:09:45 -0800 | [diff] [blame] | 163 | int32_t i = 0; |
| 164 | for (const auto& pData : m_Data) { |
| 165 | if (pData && |
| 166 | (nCharset == FXFONT_DEFAULT_CHARSET || nCharset == pData->nCharset) && |
| 167 | (sFontName.IsEmpty() || pData->sFontName == sFontName)) { |
| 168 | return i; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 169 | } |
tsepez | 6745f96 | 2017-01-04 10:09:45 -0800 | [diff] [blame] | 170 | ++i; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 171 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 172 | return -1; |
| 173 | } |
| 174 | |
| 175 | int32_t CPWL_FontMap::GetFontIndex(const CFX_ByteString& sFontName, |
| 176 | int32_t nCharset, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 177 | bool bFind) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 178 | int32_t nFontIndex = FindFont(EncodeFontAlias(sFontName, nCharset), nCharset); |
| 179 | if (nFontIndex >= 0) |
| 180 | return nFontIndex; |
| 181 | |
| 182 | CFX_ByteString sAlias; |
Tom Sepez | ec8ff7d | 2017-04-07 16:58:00 -0700 | [diff] [blame^] | 183 | CPDF_Font* pFont = bFind ? FindFontSameCharset(&sAlias, nCharset) : nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 184 | if (!pFont) { |
| 185 | CFX_ByteString sTemp = sFontName; |
| 186 | pFont = AddFontToDocument(GetDocument(), sTemp, nCharset); |
| 187 | sAlias = EncodeFontAlias(sTemp, nCharset); |
| 188 | } |
| 189 | AddedFont(pFont, sAlias); |
| 190 | return AddFontData(pFont, sAlias, nCharset); |
| 191 | } |
| 192 | |
Tom Sepez | ec8ff7d | 2017-04-07 16:58:00 -0700 | [diff] [blame^] | 193 | CPDF_Font* CPWL_FontMap::FindFontSameCharset(CFX_ByteString* sFontAlias, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 194 | int32_t nCharset) { |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 195 | return nullptr; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 198 | int32_t CPWL_FontMap::AddFontData(CPDF_Font* pFont, |
| 199 | const CFX_ByteString& sFontAlias, |
| 200 | int32_t nCharset) { |
tsepez | 6745f96 | 2017-01-04 10:09:45 -0800 | [diff] [blame] | 201 | auto pNewData = pdfium::MakeUnique<CPWL_FontMap_Data>(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 202 | pNewData->pFont = pFont; |
| 203 | pNewData->sFontName = sFontAlias; |
| 204 | pNewData->nCharset = nCharset; |
tsepez | 6745f96 | 2017-01-04 10:09:45 -0800 | [diff] [blame] | 205 | m_Data.push_back(std::move(pNewData)); |
| 206 | return pdfium::CollectionSize<int32_t>(m_Data) - 1; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 209 | void CPWL_FontMap::AddedFont(CPDF_Font* pFont, |
| 210 | const CFX_ByteString& sFontAlias) {} |
| 211 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 212 | CFX_ByteString CPWL_FontMap::GetNativeFont(int32_t nCharset) { |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 213 | if (nCharset == FXFONT_DEFAULT_CHARSET) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 214 | nCharset = GetNativeCharset(); |
| 215 | |
| 216 | CFX_ByteString sFontName = GetDefaultFontByCharset(nCharset); |
tsepez | 6745f96 | 2017-01-04 10:09:45 -0800 | [diff] [blame] | 217 | if (!m_pSystemHandler->FindNativeTrueTypeFont(sFontName)) |
| 218 | return CFX_ByteString(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 219 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 220 | return sFontName; |
| 221 | } |
| 222 | |
| 223 | CPDF_Font* CPWL_FontMap::AddFontToDocument(CPDF_Document* pDoc, |
| 224 | CFX_ByteString& sFontName, |
| 225 | uint8_t nCharset) { |
| 226 | if (IsStandardFont(sFontName)) |
| 227 | return AddStandardFont(pDoc, sFontName); |
| 228 | |
| 229 | return AddSystemFont(pDoc, sFontName, nCharset); |
| 230 | } |
| 231 | |
| 232 | CPDF_Font* CPWL_FontMap::AddStandardFont(CPDF_Document* pDoc, |
| 233 | CFX_ByteString& sFontName) { |
| 234 | if (!pDoc) |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 235 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 236 | |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 237 | CPDF_Font* pFont = nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 238 | |
Lei Zhang | c2fb35f | 2016-01-05 16:46:58 -0800 | [diff] [blame] | 239 | if (sFontName == "ZapfDingbats") { |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 240 | pFont = pDoc->AddStandardFont(sFontName.c_str(), nullptr); |
Lei Zhang | c2fb35f | 2016-01-05 16:46:58 -0800 | [diff] [blame] | 241 | } else { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 242 | CPDF_FontEncoding fe(PDFFONT_ENCODING_WINANSI); |
tsepez | b4c9f3f | 2016-04-13 15:41:21 -0700 | [diff] [blame] | 243 | pFont = pDoc->AddStandardFont(sFontName.c_str(), &fe); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | return pFont; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 249 | CPDF_Font* CPWL_FontMap::AddSystemFont(CPDF_Document* pDoc, |
| 250 | CFX_ByteString& sFontName, |
| 251 | uint8_t nCharset) { |
| 252 | if (!pDoc) |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 253 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 254 | |
| 255 | if (sFontName.IsEmpty()) |
| 256 | sFontName = GetNativeFont(nCharset); |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 257 | if (nCharset == FXFONT_DEFAULT_CHARSET) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 258 | nCharset = GetNativeCharset(); |
| 259 | |
dsinclair | 0e3e890 | 2016-08-24 11:39:24 -0700 | [diff] [blame] | 260 | return m_pSystemHandler->AddNativeTrueTypeFontToPDF(pDoc, sFontName, |
| 261 | nCharset); |
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 | CFX_ByteString CPWL_FontMap::EncodeFontAlias(const CFX_ByteString& sFontName, |
| 265 | int32_t nCharset) { |
| 266 | CFX_ByteString sPostfix; |
| 267 | sPostfix.Format("_%02X", nCharset); |
| 268 | return EncodeFontAlias(sFontName) + sPostfix; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 271 | CFX_ByteString CPWL_FontMap::EncodeFontAlias(const CFX_ByteString& sFontName) { |
| 272 | CFX_ByteString sRet = sFontName; |
| 273 | sRet.Remove(' '); |
| 274 | return sRet; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 277 | const CPWL_FontMap_Data* CPWL_FontMap::GetFontMapData(int32_t nIndex) const { |
Tom Sepez | 193e6ca | 2017-03-14 15:53:36 -0700 | [diff] [blame] | 278 | return pdfium::IndexInBounds(m_Data, nIndex) ? m_Data[nIndex].get() : nullptr; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 281 | int32_t CPWL_FontMap::GetNativeCharset() { |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 282 | uint8_t nCharset = FXFONT_ANSI_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 283 | int32_t iCodePage = FXSYS_GetACP(); |
| 284 | switch (iCodePage) { |
| 285 | case 932: // Japan |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 286 | nCharset = FXFONT_SHIFTJIS_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 287 | break; |
| 288 | case 936: // Chinese (PRC, Singapore) |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 289 | nCharset = FXFONT_GB2312_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 290 | break; |
| 291 | case 950: // Chinese (Taiwan; Hong Kong SAR, PRC) |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 292 | nCharset = FXFONT_GB2312_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 293 | break; |
| 294 | case 1252: // Windows 3.1 Latin 1 (US, Western Europe) |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 295 | nCharset = FXFONT_ANSI_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 296 | break; |
| 297 | case 874: // Thai |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 298 | nCharset = FXFONT_THAI_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 299 | break; |
| 300 | case 949: // Korean |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 301 | nCharset = FXFONT_HANGUL_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 302 | break; |
| 303 | case 1200: // Unicode (BMP of ISO 10646) |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 304 | nCharset = FXFONT_ANSI_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 305 | break; |
| 306 | case 1250: // Windows 3.1 Eastern European |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 307 | nCharset = FXFONT_EASTEUROPE_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 308 | break; |
| 309 | case 1251: // Windows 3.1 Cyrillic |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 310 | nCharset = FXFONT_RUSSIAN_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 311 | break; |
| 312 | case 1253: // Windows 3.1 Greek |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 313 | nCharset = FXFONT_GREEK_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 314 | break; |
| 315 | case 1254: // Windows 3.1 Turkish |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 316 | nCharset = FXFONT_TURKISH_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 317 | break; |
| 318 | case 1255: // Hebrew |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 319 | nCharset = FXFONT_HEBREW_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 320 | break; |
| 321 | case 1256: // Arabic |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 322 | nCharset = FXFONT_ARABIC_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 323 | break; |
| 324 | case 1257: // Baltic |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 325 | nCharset = FXFONT_BALTIC_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 326 | break; |
| 327 | case 1258: // Vietnamese |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 328 | nCharset = FXFONT_VIETNAMESE_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 329 | break; |
| 330 | case 1361: // Korean(Johab) |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 331 | nCharset = FXFONT_JOHAB_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 332 | break; |
| 333 | } |
| 334 | return nCharset; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 335 | } |
| 336 | |
weili | 2d5b020 | 2016-08-03 11:06:49 -0700 | [diff] [blame] | 337 | const FPDF_CharsetFontMap CPWL_FontMap::defaultTTFMap[] = { |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 338 | {FXFONT_ANSI_CHARSET, "Helvetica"}, |
| 339 | {FXFONT_GB2312_CHARSET, "SimSun"}, |
| 340 | {FXFONT_CHINESEBIG5_CHARSET, "MingLiU"}, |
| 341 | {FXFONT_SHIFTJIS_CHARSET, "MS Gothic"}, |
| 342 | {FXFONT_HANGUL_CHARSET, "Batang"}, |
| 343 | {FXFONT_RUSSIAN_CHARSET, "Arial"}, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 344 | #if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ || \ |
| 345 | _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 346 | {FXFONT_EASTEUROPE_CHARSET, "Arial"}, |
Bo Xu | dbd4c06 | 2014-05-29 11:32:56 -0700 | [diff] [blame] | 347 | #else |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 348 | {FXFONT_EASTEUROPE_CHARSET, "Tahoma"}, |
Bo Xu | dbd4c06 | 2014-05-29 11:32:56 -0700 | [diff] [blame] | 349 | #endif |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 350 | {FXFONT_ARABIC_CHARSET, "Arial"}, |
| 351 | {-1, nullptr}}; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 352 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 353 | CFX_ByteString CPWL_FontMap::GetDefaultFontByCharset(int32_t nCharset) { |
| 354 | int i = 0; |
| 355 | while (defaultTTFMap[i].charset != -1) { |
| 356 | if (nCharset == defaultTTFMap[i].charset) |
| 357 | return defaultTTFMap[i].fontname; |
| 358 | ++i; |
| 359 | } |
| 360 | return ""; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 363 | int32_t CPWL_FontMap::CharSetFromUnicode(uint16_t word, int32_t nOldCharset) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 364 | // to avoid CJK Font to show ASCII |
| 365 | if (word < 0x7F) |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 366 | return FXFONT_ANSI_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 367 | // follow the old charset |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 368 | if (nOldCharset != FXFONT_DEFAULT_CHARSET) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 369 | return nOldCharset; |
| 370 | |
| 371 | // find new charset |
| 372 | if ((word >= 0x4E00 && word <= 0x9FA5) || |
| 373 | (word >= 0xE7C7 && word <= 0xE7F3) || |
| 374 | (word >= 0x3000 && word <= 0x303F) || |
| 375 | (word >= 0x2000 && word <= 0x206F)) { |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 376 | return FXFONT_GB2312_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | if (((word >= 0x3040) && (word <= 0x309F)) || |
| 380 | ((word >= 0x30A0) && (word <= 0x30FF)) || |
| 381 | ((word >= 0x31F0) && (word <= 0x31FF)) || |
| 382 | ((word >= 0xFF00) && (word <= 0xFFEF))) { |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 383 | return FXFONT_SHIFTJIS_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | if (((word >= 0xAC00) && (word <= 0xD7AF)) || |
| 387 | ((word >= 0x1100) && (word <= 0x11FF)) || |
| 388 | ((word >= 0x3130) && (word <= 0x318F))) { |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 389 | return FXFONT_HANGUL_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | if (word >= 0x0E00 && word <= 0x0E7F) |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 393 | return FXFONT_THAI_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 394 | |
| 395 | if ((word >= 0x0370 && word <= 0x03FF) || (word >= 0x1F00 && word <= 0x1FFF)) |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 396 | return FXFONT_GREEK_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 397 | |
| 398 | if ((word >= 0x0600 && word <= 0x06FF) || (word >= 0xFB50 && word <= 0xFEFC)) |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 399 | return FXFONT_ARABIC_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 400 | |
| 401 | if (word >= 0x0590 && word <= 0x05FF) |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 402 | return FXFONT_HEBREW_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 403 | |
| 404 | if (word >= 0x0400 && word <= 0x04FF) |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 405 | return FXFONT_RUSSIAN_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 406 | |
| 407 | if (word >= 0x0100 && word <= 0x024F) |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 408 | return FXFONT_EASTEUROPE_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 409 | |
| 410 | if (word >= 0x1E00 && word <= 0x1EFF) |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 411 | return FXFONT_VIETNAMESE_CHARSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 412 | |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 413 | return FXFONT_ANSI_CHARSET; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 414 | } |