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