Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 1 | // Copyright 2017 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. |
| 4 | |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 5 | #include <map> |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 6 | #include <memory> |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 7 | #include <utility> |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 8 | #include <vector> |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 9 | |
| 10 | #include "core/fpdfapi/cpdf_modulemgr.h" |
| 11 | #include "core/fpdfapi/font/cpdf_font.h" |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 12 | #include "core/fpdfapi/font/cpdf_type1font.h" |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 13 | #include "core/fpdfapi/page/cpdf_docpagedata.h" |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 14 | #include "core/fpdfapi/page/cpdf_textobject.h" |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 15 | #include "core/fpdfapi/parser/cpdf_array.h" |
| 16 | #include "core/fpdfapi/parser/cpdf_dictionary.h" |
| 17 | #include "core/fpdfapi/parser/cpdf_document.h" |
| 18 | #include "core/fpdfapi/parser/cpdf_name.h" |
| 19 | #include "core/fpdfapi/parser/cpdf_number.h" |
| 20 | #include "core/fpdfapi/parser/cpdf_reference.h" |
| 21 | #include "core/fpdfapi/parser/cpdf_stream.h" |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 22 | #include "core/fxcrt/fx_extension.h" |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 23 | #include "core/fxge/cfx_fontmgr.h" |
| 24 | #include "core/fxge/fx_font.h" |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 25 | #include "fpdfsdk/fsdk_define.h" |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 26 | #include "public/fpdf_edit.h" |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 27 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 28 | namespace { |
| 29 | |
| 30 | CPDF_Dictionary* LoadFontDesc(CPDF_Document* pDoc, |
| 31 | const CFX_ByteString& font_name, |
| 32 | CFX_Font* pFont, |
| 33 | const uint8_t* data, |
| 34 | uint32_t size, |
| 35 | int font_type) { |
| 36 | CPDF_Dictionary* fontDesc = pDoc->NewIndirect<CPDF_Dictionary>(); |
| 37 | fontDesc->SetNewFor<CPDF_Name>("Type", "FontDescriptor"); |
| 38 | fontDesc->SetNewFor<CPDF_Name>("FontName", font_name); |
| 39 | int flags = 0; |
| 40 | if (FXFT_Is_Face_fixedwidth(pFont->GetFace())) |
| 41 | flags |= FXFONT_FIXED_PITCH; |
Ryan Harrison | da129ab | 2017-08-01 15:16:59 -0400 | [diff] [blame^] | 42 | if (font_name.Find("Serif") != FX_STRNPOS) |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 43 | flags |= FXFONT_SERIF; |
| 44 | if (FXFT_Is_Face_Italic(pFont->GetFace())) |
| 45 | flags |= FXFONT_ITALIC; |
| 46 | if (FXFT_Is_Face_Bold(pFont->GetFace())) |
| 47 | flags |= FXFONT_BOLD; |
| 48 | |
| 49 | // TODO(npm): How do I know if a font is symbolic, script, allcap, smallcap |
| 50 | flags |= FXFONT_NONSYMBOLIC; |
| 51 | |
| 52 | fontDesc->SetNewFor<CPDF_Number>("Flags", flags); |
| 53 | FX_RECT bbox; |
| 54 | pFont->GetBBox(bbox); |
| 55 | auto pBBox = pdfium::MakeUnique<CPDF_Array>(); |
| 56 | pBBox->AddNew<CPDF_Number>(bbox.left); |
| 57 | pBBox->AddNew<CPDF_Number>(bbox.bottom); |
| 58 | pBBox->AddNew<CPDF_Number>(bbox.right); |
| 59 | pBBox->AddNew<CPDF_Number>(bbox.top); |
| 60 | fontDesc->SetFor("FontBBox", std::move(pBBox)); |
| 61 | |
| 62 | // TODO(npm): calculate italic angle correctly |
| 63 | fontDesc->SetNewFor<CPDF_Number>("ItalicAngle", pFont->IsItalic() ? -12 : 0); |
| 64 | |
| 65 | fontDesc->SetNewFor<CPDF_Number>("Ascent", pFont->GetAscent()); |
| 66 | fontDesc->SetNewFor<CPDF_Number>("Descent", pFont->GetDescent()); |
| 67 | |
| 68 | // TODO(npm): calculate the capheight, stemV correctly |
| 69 | fontDesc->SetNewFor<CPDF_Number>("CapHeight", pFont->GetAscent()); |
| 70 | fontDesc->SetNewFor<CPDF_Number>("StemV", pFont->IsBold() ? 120 : 70); |
| 71 | |
| 72 | CPDF_Stream* pStream = pDoc->NewIndirect<CPDF_Stream>(); |
| 73 | pStream->SetData(data, size); |
| 74 | CFX_ByteString fontFile = |
| 75 | font_type == FPDF_FONT_TYPE1 ? "FontFile" : "FontFile2"; |
| 76 | fontDesc->SetNewFor<CPDF_Reference>(fontFile, pDoc, pStream->GetObjNum()); |
| 77 | return fontDesc; |
| 78 | } |
| 79 | |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 80 | const char ToUnicodeStart[] = |
| 81 | "/CIDInit /ProcSet findresource begin\n" |
| 82 | "12 dict begin\n" |
| 83 | "begincmap\n" |
| 84 | "/CIDSystemInfo\n" |
| 85 | "<</Registry (Adobe)\n" |
| 86 | "/Ordering (Identity)\n" |
| 87 | "/Supplement 0\n" |
| 88 | ">> def\n" |
| 89 | "/CMapName /Adobe-Identity-H def\n" |
| 90 | "CMapType 2 def\n" |
| 91 | "1 begincodespacerange\n" |
Nicolas Pena | b48912f | 2017-05-17 14:45:52 -0400 | [diff] [blame] | 92 | "<0000> <FFFFF>\n" |
| 93 | "endcodespacerange\n"; |
| 94 | |
| 95 | const char ToUnicodeEnd[] = |
| 96 | "endcmap\n" |
| 97 | "CMapName currentdict /CMap defineresource pop\n" |
| 98 | "end\n" |
| 99 | "end\n"; |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 100 | |
Henrique Nakashima | 0ba3c6d | 2017-07-05 16:21:17 -0400 | [diff] [blame] | 101 | void AddCharcode(std::ostringstream* pBuffer, uint32_t number) { |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 102 | ASSERT(number <= 0xFFFF); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 103 | *pBuffer << "<"; |
| 104 | char ans[4]; |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 105 | FXSYS_IntToFourHexChars(number, ans); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 106 | for (size_t i = 0; i < 4; ++i) |
Henrique Nakashima | 0ba3c6d | 2017-07-05 16:21:17 -0400 | [diff] [blame] | 107 | *pBuffer << ans[i]; |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 108 | *pBuffer << ">"; |
| 109 | } |
| 110 | |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 111 | // PDF spec 1.7 Section 5.9.2: "Unicode character sequences as expressed in |
| 112 | // UTF-16BE encoding." See https://en.wikipedia.org/wiki/UTF-16#Description |
Henrique Nakashima | 0ba3c6d | 2017-07-05 16:21:17 -0400 | [diff] [blame] | 113 | void AddUnicode(std::ostringstream* pBuffer, uint32_t unicode) { |
Nicolas Pena | 217644c | 2017-07-06 10:53:01 -0400 | [diff] [blame] | 114 | if (unicode >= 0xD800 && unicode <= 0xDFFF) |
| 115 | unicode = 0; |
| 116 | |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 117 | char ans[8]; |
| 118 | *pBuffer << "<"; |
| 119 | size_t numChars = FXSYS_ToUTF16BE(unicode, ans); |
| 120 | for (size_t i = 0; i < numChars; ++i) |
Henrique Nakashima | 0ba3c6d | 2017-07-05 16:21:17 -0400 | [diff] [blame] | 121 | *pBuffer << ans[i]; |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 122 | *pBuffer << ">"; |
| 123 | } |
| 124 | |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 125 | // Loads the charcode to unicode mapping into a stream |
| 126 | CPDF_Stream* LoadUnicode(CPDF_Document* pDoc, |
| 127 | const std::map<uint32_t, uint32_t>& to_unicode) { |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 128 | // A map charcode->unicode |
| 129 | std::map<uint32_t, uint32_t> char_to_uni; |
| 130 | // A map <char_start, char_end> to vector v of unicode characters of size (end |
| 131 | // - start + 1). This abbreviates: start->v[0], start+1->v[1], etc. PDF spec |
| 132 | // 1.7 Section 5.9.2 says that only the last byte of the unicode may change. |
| 133 | std::map<std::pair<uint32_t, uint32_t>, std::vector<uint32_t>> |
| 134 | map_range_vector; |
| 135 | // A map <start, end> -> unicode |
| 136 | // This abbreviates: start->unicode, start+1->unicode+1, etc. |
| 137 | // PDF spec 1.7 Section 5.9.2 says that only the last byte of the unicode may |
| 138 | // change. |
| 139 | std::map<std::pair<uint32_t, uint32_t>, uint32_t> map_range; |
| 140 | |
| 141 | // Calculate the maps |
| 142 | for (auto iter = to_unicode.begin(); iter != to_unicode.end(); ++iter) { |
| 143 | uint32_t firstCharcode = iter->first; |
| 144 | uint32_t firstUnicode = iter->second; |
| 145 | if (std::next(iter) == to_unicode.end() || |
| 146 | firstCharcode + 1 != std::next(iter)->first) { |
| 147 | char_to_uni[firstCharcode] = firstUnicode; |
| 148 | continue; |
| 149 | } |
| 150 | ++iter; |
| 151 | uint32_t curCharcode = iter->first; |
| 152 | uint32_t curUnicode = iter->second; |
| 153 | if (curCharcode % 256 == 0) { |
| 154 | char_to_uni[firstCharcode] = firstUnicode; |
| 155 | char_to_uni[curCharcode] = curUnicode; |
| 156 | continue; |
| 157 | } |
| 158 | const size_t maxExtra = 255 - (curCharcode % 256); |
| 159 | auto next_it = std::next(iter); |
| 160 | if (firstUnicode + 1 != curUnicode) { |
| 161 | // Consecutive charcodes mapping to non-consecutive unicodes |
| 162 | std::vector<uint32_t> unicodes; |
| 163 | unicodes.push_back(firstUnicode); |
| 164 | unicodes.push_back(curUnicode); |
| 165 | for (size_t i = 0; i < maxExtra; ++i) { |
| 166 | if (next_it == to_unicode.end() || curCharcode + 1 != next_it->first) |
| 167 | break; |
| 168 | ++iter; |
| 169 | ++curCharcode; |
| 170 | unicodes.push_back(iter->second); |
| 171 | next_it = std::next(iter); |
| 172 | } |
| 173 | ASSERT(iter->first - firstCharcode + 1 == unicodes.size()); |
| 174 | map_range_vector[std::make_pair(firstCharcode, iter->first)] = unicodes; |
| 175 | continue; |
| 176 | } |
| 177 | // Consecutive charcodes mapping to consecutive unicodes |
| 178 | for (size_t i = 0; i < maxExtra; ++i) { |
| 179 | if (next_it == to_unicode.end() || curCharcode + 1 != next_it->first || |
| 180 | curUnicode + 1 != next_it->second) { |
| 181 | break; |
| 182 | } |
| 183 | ++iter; |
| 184 | ++curCharcode; |
| 185 | ++curUnicode; |
| 186 | next_it = std::next(iter); |
| 187 | } |
| 188 | map_range[std::make_pair(firstCharcode, curCharcode)] = firstUnicode; |
| 189 | } |
Henrique Nakashima | 0ba3c6d | 2017-07-05 16:21:17 -0400 | [diff] [blame] | 190 | std::ostringstream buffer; |
Nicolas Pena | b48912f | 2017-05-17 14:45:52 -0400 | [diff] [blame] | 191 | buffer << ToUnicodeStart; |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 192 | // Add maps to buffer |
| 193 | buffer << static_cast<uint32_t>(char_to_uni.size()) << " beginbfchar\n"; |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 194 | for (const auto& iter : char_to_uni) { |
| 195 | AddCharcode(&buffer, iter.first); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 196 | buffer << " "; |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 197 | AddUnicode(&buffer, iter.second); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 198 | buffer << "\n"; |
| 199 | } |
| 200 | buffer << "endbfchar\n" |
| 201 | << static_cast<uint32_t>(map_range_vector.size() + map_range.size()) |
| 202 | << " beginbfrange\n"; |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 203 | for (const auto& iter : map_range_vector) { |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 204 | const std::pair<uint32_t, uint32_t>& charcodeRange = iter.first; |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 205 | AddCharcode(&buffer, charcodeRange.first); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 206 | buffer << " "; |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 207 | AddCharcode(&buffer, charcodeRange.second); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 208 | buffer << " ["; |
| 209 | const std::vector<uint32_t>& unicodes = iter.second; |
| 210 | for (size_t i = 0; i < unicodes.size(); ++i) { |
| 211 | uint32_t uni = unicodes[i]; |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 212 | AddUnicode(&buffer, uni); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 213 | if (i != unicodes.size() - 1) |
| 214 | buffer << " "; |
| 215 | } |
| 216 | buffer << "]\n"; |
| 217 | } |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 218 | for (const auto& iter : map_range) { |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 219 | const std::pair<uint32_t, uint32_t>& charcodeRange = iter.first; |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 220 | AddCharcode(&buffer, charcodeRange.first); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 221 | buffer << " "; |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 222 | AddCharcode(&buffer, charcodeRange.second); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 223 | buffer << " "; |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 224 | AddUnicode(&buffer, iter.second); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 225 | buffer << "\n"; |
| 226 | } |
Nicolas Pena | b48912f | 2017-05-17 14:45:52 -0400 | [diff] [blame] | 227 | buffer << "endbfrange\n"; |
| 228 | buffer << ToUnicodeEnd; |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 229 | // TODO(npm): Encrypt / Compress? |
Henrique Nakashima | 0ba3c6d | 2017-07-05 16:21:17 -0400 | [diff] [blame] | 230 | CPDF_Stream* stream = pDoc->NewIndirect<CPDF_Stream>(); |
| 231 | stream->SetData(&buffer); |
| 232 | return stream; |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 233 | } |
| 234 | |
Nicolas Pena | b83d870 | 2017-06-09 17:55:51 -0400 | [diff] [blame] | 235 | const uint32_t kMaxSimpleFontChar = 0xFF; |
| 236 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 237 | void* LoadSimpleFont(CPDF_Document* pDoc, |
| 238 | std::unique_ptr<CFX_Font> pFont, |
| 239 | const uint8_t* data, |
| 240 | uint32_t size, |
| 241 | int font_type) { |
| 242 | CPDF_Dictionary* fontDict = pDoc->NewIndirect<CPDF_Dictionary>(); |
| 243 | fontDict->SetNewFor<CPDF_Name>("Type", "Font"); |
| 244 | fontDict->SetNewFor<CPDF_Name>( |
| 245 | "Subtype", font_type == FPDF_FONT_TYPE1 ? "Type1" : "TrueType"); |
| 246 | CFX_ByteString name = pFont->GetFaceName(); |
| 247 | if (name.IsEmpty()) |
| 248 | name = "Unnamed"; |
| 249 | fontDict->SetNewFor<CPDF_Name>("BaseFont", name); |
| 250 | |
| 251 | uint32_t glyphIndex; |
Nicolas Pena | b83d870 | 2017-06-09 17:55:51 -0400 | [diff] [blame] | 252 | uint32_t currentChar = FXFT_Get_First_Char(pFont->GetFace(), &glyphIndex); |
| 253 | if (currentChar > kMaxSimpleFontChar || glyphIndex == 0) |
| 254 | return nullptr; |
| 255 | fontDict->SetNewFor<CPDF_Number>("FirstChar", static_cast<int>(currentChar)); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 256 | CPDF_Array* widthsArray = pDoc->NewIndirect<CPDF_Array>(); |
| 257 | while (true) { |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 258 | widthsArray->AddNew<CPDF_Number>(pFont->GetGlyphWidth(glyphIndex)); |
Nicolas Pena | b83d870 | 2017-06-09 17:55:51 -0400 | [diff] [blame] | 259 | uint32_t nextChar = |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 260 | FXFT_Get_Next_Char(pFont->GetFace(), currentChar, &glyphIndex); |
| 261 | // Simple fonts have 1-byte charcodes only. |
Nicolas Pena | b83d870 | 2017-06-09 17:55:51 -0400 | [diff] [blame] | 262 | if (nextChar > kMaxSimpleFontChar || glyphIndex == 0) |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 263 | break; |
Nicolas Pena | b83d870 | 2017-06-09 17:55:51 -0400 | [diff] [blame] | 264 | for (uint32_t i = currentChar + 1; i < nextChar; i++) |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 265 | widthsArray->AddNew<CPDF_Number>(0); |
| 266 | currentChar = nextChar; |
| 267 | } |
Nicolas Pena | b83d870 | 2017-06-09 17:55:51 -0400 | [diff] [blame] | 268 | fontDict->SetNewFor<CPDF_Number>("LastChar", static_cast<int>(currentChar)); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 269 | fontDict->SetNewFor<CPDF_Reference>("Widths", pDoc, widthsArray->GetObjNum()); |
| 270 | CPDF_Dictionary* fontDesc = |
| 271 | LoadFontDesc(pDoc, name, pFont.get(), data, size, font_type); |
| 272 | |
| 273 | fontDict->SetNewFor<CPDF_Reference>("FontDescriptor", pDoc, |
| 274 | fontDesc->GetObjNum()); |
| 275 | return pDoc->LoadFont(fontDict); |
| 276 | } |
| 277 | |
Nicolas Pena | b83d870 | 2017-06-09 17:55:51 -0400 | [diff] [blame] | 278 | const uint32_t kMaxUnicode = 0x10FFFF; |
| 279 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 280 | void* LoadCompositeFont(CPDF_Document* pDoc, |
| 281 | std::unique_ptr<CFX_Font> pFont, |
| 282 | const uint8_t* data, |
| 283 | uint32_t size, |
| 284 | int font_type) { |
| 285 | CPDF_Dictionary* fontDict = pDoc->NewIndirect<CPDF_Dictionary>(); |
| 286 | fontDict->SetNewFor<CPDF_Name>("Type", "Font"); |
| 287 | fontDict->SetNewFor<CPDF_Name>("Subtype", "Type0"); |
| 288 | // TODO(npm): Get the correct encoding, if it's not identity. |
| 289 | CFX_ByteString encoding = "Identity-H"; |
| 290 | fontDict->SetNewFor<CPDF_Name>("Encoding", encoding); |
| 291 | CFX_ByteString name = pFont->GetFaceName(); |
| 292 | if (name.IsEmpty()) |
| 293 | name = "Unnamed"; |
| 294 | fontDict->SetNewFor<CPDF_Name>( |
| 295 | "BaseFont", font_type == FPDF_FONT_TYPE1 ? name + "-" + encoding : name); |
| 296 | |
| 297 | CPDF_Dictionary* pCIDFont = pDoc->NewIndirect<CPDF_Dictionary>(); |
| 298 | pCIDFont->SetNewFor<CPDF_Name>("Type", "Font"); |
| 299 | pCIDFont->SetNewFor<CPDF_Name>("Subtype", font_type == FPDF_FONT_TYPE1 |
| 300 | ? "CIDFontType0" |
| 301 | : "CIDFontType2"); |
| 302 | pCIDFont->SetNewFor<CPDF_Name>("BaseFont", name); |
| 303 | |
| 304 | // TODO(npm): Maybe use FT_Get_CID_Registry_Ordering_Supplement to get the |
| 305 | // CIDSystemInfo |
| 306 | CPDF_Dictionary* pCIDSystemInfo = pDoc->NewIndirect<CPDF_Dictionary>(); |
| 307 | pCIDSystemInfo->SetNewFor<CPDF_Name>("Registry", "Adobe"); |
| 308 | pCIDSystemInfo->SetNewFor<CPDF_Name>("Ordering", "Identity"); |
| 309 | pCIDSystemInfo->SetNewFor<CPDF_Number>("Supplement", 0); |
| 310 | pCIDFont->SetNewFor<CPDF_Reference>("CIDSystemInfo", pDoc, |
| 311 | pCIDSystemInfo->GetObjNum()); |
| 312 | |
| 313 | CPDF_Dictionary* fontDesc = |
| 314 | LoadFontDesc(pDoc, name, pFont.get(), data, size, font_type); |
| 315 | pCIDFont->SetNewFor<CPDF_Reference>("FontDescriptor", pDoc, |
| 316 | fontDesc->GetObjNum()); |
| 317 | |
| 318 | uint32_t glyphIndex; |
Nicolas Pena | b83d870 | 2017-06-09 17:55:51 -0400 | [diff] [blame] | 319 | uint32_t currentChar = FXFT_Get_First_Char(pFont->GetFace(), &glyphIndex); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 320 | // If it doesn't have a single char, just fail |
Nicolas Pena | b83d870 | 2017-06-09 17:55:51 -0400 | [diff] [blame] | 321 | if (glyphIndex == 0 || currentChar > kMaxUnicode) |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 322 | return nullptr; |
| 323 | |
| 324 | std::map<uint32_t, uint32_t> to_unicode; |
| 325 | std::map<uint32_t, uint32_t> widths; |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 326 | while (true) { |
Nicolas Pena | b83d870 | 2017-06-09 17:55:51 -0400 | [diff] [blame] | 327 | if (currentChar > kMaxUnicode) |
Nicolas Pena | fb71fbb | 2017-05-23 13:16:09 -0400 | [diff] [blame] | 328 | break; |
| 329 | |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 330 | widths[glyphIndex] = pFont->GetGlyphWidth(glyphIndex); |
| 331 | to_unicode[glyphIndex] = currentChar; |
| 332 | currentChar = |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 333 | FXFT_Get_Next_Char(pFont->GetFace(), currentChar, &glyphIndex); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 334 | if (glyphIndex == 0) |
| 335 | break; |
| 336 | } |
| 337 | CPDF_Array* widthsArray = pDoc->NewIndirect<CPDF_Array>(); |
| 338 | for (auto it = widths.begin(); it != widths.end(); ++it) { |
| 339 | int ch = it->first; |
| 340 | int w = it->second; |
| 341 | if (std::next(it) == widths.end()) { |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 342 | // Only one char left, use format c [w] |
| 343 | auto oneW = pdfium::MakeUnique<CPDF_Array>(); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 344 | oneW->AddNew<CPDF_Number>(w); |
| 345 | widthsArray->AddNew<CPDF_Number>(ch); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 346 | widthsArray->Add(std::move(oneW)); |
| 347 | break; |
| 348 | } |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 349 | ++it; |
| 350 | int next_ch = it->first; |
| 351 | int next_w = it->second; |
| 352 | if (next_ch == ch + 1 && next_w == w) { |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 353 | // The array can have a group c_first c_last w: all CIDs in the range from |
| 354 | // c_first to c_last will have width w |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 355 | widthsArray->AddNew<CPDF_Number>(ch); |
| 356 | ch = next_ch; |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 357 | while (true) { |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 358 | auto next_it = std::next(it); |
| 359 | if (next_it == widths.end() || next_it->first != it->first + 1 || |
| 360 | next_it->second != it->second) { |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 361 | break; |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 362 | } |
| 363 | ++it; |
| 364 | ch = it->first; |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 365 | } |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 366 | widthsArray->AddNew<CPDF_Number>(ch); |
| 367 | widthsArray->AddNew<CPDF_Number>(w); |
| 368 | continue; |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 369 | } |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 370 | // Otherwise we can have a group of the form c [w1 w2 ...]: c has width |
| 371 | // w1, c+1 has width w2, etc. |
| 372 | widthsArray->AddNew<CPDF_Number>(ch); |
| 373 | auto curWidthArray = pdfium::MakeUnique<CPDF_Array>(); |
| 374 | curWidthArray->AddNew<CPDF_Number>(w); |
| 375 | curWidthArray->AddNew<CPDF_Number>(next_w); |
| 376 | while (true) { |
| 377 | auto next_it = std::next(it); |
| 378 | if (next_it == widths.end() || next_it->first != it->first + 1) |
| 379 | break; |
| 380 | ++it; |
| 381 | curWidthArray->AddNew<CPDF_Number>(static_cast<int>(it->second)); |
| 382 | } |
| 383 | widthsArray->Add(std::move(curWidthArray)); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 384 | } |
| 385 | pCIDFont->SetNewFor<CPDF_Reference>("W", pDoc, widthsArray->GetObjNum()); |
| 386 | // TODO(npm): Support vertical writing |
| 387 | |
| 388 | auto pDescendant = pdfium::MakeUnique<CPDF_Array>(); |
| 389 | pDescendant->AddNew<CPDF_Reference>(pDoc, pCIDFont->GetObjNum()); |
| 390 | fontDict->SetFor("DescendantFonts", std::move(pDescendant)); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 391 | CPDF_Stream* toUnicodeStream = LoadUnicode(pDoc, to_unicode); |
| 392 | fontDict->SetNewFor<CPDF_Reference>("ToUnicode", pDoc, |
| 393 | toUnicodeStream->GetObjNum()); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 394 | return pDoc->LoadFont(fontDict); |
| 395 | } |
| 396 | |
| 397 | } // namespace |
| 398 | |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 399 | DLLEXPORT FPDF_PAGEOBJECT STDCALL FPDFPageObj_NewTextObj(FPDF_DOCUMENT document, |
| 400 | FPDF_BYTESTRING font, |
| 401 | float font_size) { |
| 402 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 403 | if (!pDoc) |
| 404 | return nullptr; |
| 405 | |
| 406 | CPDF_Font* pFont = CPDF_Font::GetStockFont(pDoc, CFX_ByteStringC(font)); |
| 407 | if (!pFont) |
| 408 | return nullptr; |
| 409 | |
Tom Sepez | fe91c6c | 2017-05-16 15:33:20 -0700 | [diff] [blame] | 410 | auto pTextObj = pdfium::MakeUnique<CPDF_TextObject>(); |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 411 | pTextObj->m_TextState.SetFont(pFont); |
| 412 | pTextObj->m_TextState.SetFontSize(font_size); |
| 413 | pTextObj->DefaultStates(); |
Tom Sepez | fe91c6c | 2017-05-16 15:33:20 -0700 | [diff] [blame] | 414 | return pTextObj.release(); // Caller takes ownership. |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | DLLEXPORT FPDF_BOOL STDCALL FPDFText_SetText(FPDF_PAGEOBJECT text_object, |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 418 | FPDF_WIDESTRING text) { |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 419 | auto* pTextObj = static_cast<CPDF_TextObject*>(text_object); |
| 420 | if (!pTextObj) |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 421 | return false; |
| 422 | |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 423 | FX_STRSIZE len = CFX_WideString::WStringLength(text); |
| 424 | CFX_WideString encodedText = CFX_WideString::FromUTF16LE(text, len); |
| 425 | CFX_ByteString byteText; |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 426 | for (wchar_t wc : encodedText) { |
| 427 | pTextObj->GetFont()->AppendChar( |
| 428 | &byteText, pTextObj->GetFont()->CharCodeFromUnicode(wc)); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 429 | } |
| 430 | pTextObj->SetText(byteText); |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 431 | return true; |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 432 | } |
| 433 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 434 | DLLEXPORT FPDF_FONT STDCALL FPDFText_LoadFont(FPDF_DOCUMENT document, |
| 435 | const uint8_t* data, |
| 436 | uint32_t size, |
| 437 | int font_type, |
| 438 | FPDF_BOOL cid) { |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 439 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 440 | if (!pDoc || !data || size == 0 || |
| 441 | (font_type != FPDF_FONT_TYPE1 && font_type != FPDF_FONT_TRUETYPE)) { |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 442 | return nullptr; |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 443 | } |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 444 | |
| 445 | auto pFont = pdfium::MakeUnique<CFX_Font>(); |
| 446 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 447 | // TODO(npm): Maybe use FT_Get_X11_Font_Format to check format? Otherwise, we |
| 448 | // are allowing giving any font that can be loaded on freetype and setting it |
| 449 | // as any font type. |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 450 | if (!pFont->LoadEmbedded(data, size)) |
| 451 | return nullptr; |
| 452 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 453 | return cid ? LoadCompositeFont(pDoc, std::move(pFont), data, size, font_type) |
| 454 | : LoadSimpleFont(pDoc, std::move(pFont), data, size, font_type); |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 455 | } |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 456 | |
wileyrya | 864e9fb | 2017-05-26 11:38:14 -0500 | [diff] [blame] | 457 | DLLEXPORT FPDF_BOOL STDCALL FPDFText_SetFillColor(FPDF_PAGEOBJECT text_object, |
| 458 | unsigned int R, |
| 459 | unsigned int G, |
| 460 | unsigned int B, |
| 461 | unsigned int A) { |
| 462 | return FPDFPageObj_SetFillColor(text_object, R, G, B, A); |
| 463 | } |
| 464 | |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 465 | DLLEXPORT void STDCALL FPDFFont_Close(FPDF_FONT font) { |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 466 | CPDF_Font* pFont = static_cast<CPDF_Font*>(font); |
| 467 | if (!pFont) |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 468 | return; |
| 469 | |
Lei Zhang | b8a8c43 | 2017-05-04 14:10:50 -0700 | [diff] [blame] | 470 | CPDF_Document* pDoc = pFont->GetDocument(); |
| 471 | if (!pDoc) |
| 472 | return; |
| 473 | |
| 474 | CPDF_DocPageData* pPageData = pDoc->GetPageData(); |
| 475 | if (!pPageData->IsForceClear()) |
| 476 | pPageData->ReleaseFont(pFont->GetFontDict()); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | DLLEXPORT FPDF_PAGEOBJECT STDCALL |
| 480 | FPDFPageObj_CreateTextObj(FPDF_DOCUMENT document, |
| 481 | FPDF_FONT font, |
| 482 | float font_size) { |
| 483 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 484 | CPDF_Font* pFont = static_cast<CPDF_Font*>(font); |
| 485 | if (!pDoc || !pFont) |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 486 | return nullptr; |
| 487 | |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 488 | auto pTextObj = pdfium::MakeUnique<CPDF_TextObject>(); |
| 489 | pTextObj->m_TextState.SetFont(pDoc->LoadFont(pFont->GetFontDict())); |
| 490 | pTextObj->m_TextState.SetFontSize(font_size); |
| 491 | pTextObj->DefaultStates(); |
| 492 | return pTextObj.release(); |
| 493 | } |