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; |
| 42 | if (font_name.Find("Serif") > -1) |
| 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 | |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 101 | void AddCharcode(CFX_ByteTextBuf* pBuffer, uint32_t number) { |
| 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) |
| 107 | pBuffer->AppendChar(ans[i]); |
| 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 |
| 113 | void AddUnicode(CFX_ByteTextBuf* pBuffer, uint32_t unicode) { |
| 114 | char ans[8]; |
| 115 | *pBuffer << "<"; |
| 116 | size_t numChars = FXSYS_ToUTF16BE(unicode, ans); |
| 117 | for (size_t i = 0; i < numChars; ++i) |
| 118 | pBuffer->AppendChar(ans[i]); |
| 119 | *pBuffer << ">"; |
| 120 | } |
| 121 | |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 122 | // Loads the charcode to unicode mapping into a stream |
| 123 | CPDF_Stream* LoadUnicode(CPDF_Document* pDoc, |
| 124 | const std::map<uint32_t, uint32_t>& to_unicode) { |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 125 | // A map charcode->unicode |
| 126 | std::map<uint32_t, uint32_t> char_to_uni; |
| 127 | // A map <char_start, char_end> to vector v of unicode characters of size (end |
| 128 | // - start + 1). This abbreviates: start->v[0], start+1->v[1], etc. PDF spec |
| 129 | // 1.7 Section 5.9.2 says that only the last byte of the unicode may change. |
| 130 | std::map<std::pair<uint32_t, uint32_t>, std::vector<uint32_t>> |
| 131 | map_range_vector; |
| 132 | // A map <start, end> -> unicode |
| 133 | // This abbreviates: start->unicode, start+1->unicode+1, etc. |
| 134 | // PDF spec 1.7 Section 5.9.2 says that only the last byte of the unicode may |
| 135 | // change. |
| 136 | std::map<std::pair<uint32_t, uint32_t>, uint32_t> map_range; |
| 137 | |
| 138 | // Calculate the maps |
| 139 | for (auto iter = to_unicode.begin(); iter != to_unicode.end(); ++iter) { |
| 140 | uint32_t firstCharcode = iter->first; |
| 141 | uint32_t firstUnicode = iter->second; |
| 142 | if (std::next(iter) == to_unicode.end() || |
| 143 | firstCharcode + 1 != std::next(iter)->first) { |
| 144 | char_to_uni[firstCharcode] = firstUnicode; |
| 145 | continue; |
| 146 | } |
| 147 | ++iter; |
| 148 | uint32_t curCharcode = iter->first; |
| 149 | uint32_t curUnicode = iter->second; |
| 150 | if (curCharcode % 256 == 0) { |
| 151 | char_to_uni[firstCharcode] = firstUnicode; |
| 152 | char_to_uni[curCharcode] = curUnicode; |
| 153 | continue; |
| 154 | } |
| 155 | const size_t maxExtra = 255 - (curCharcode % 256); |
| 156 | auto next_it = std::next(iter); |
| 157 | if (firstUnicode + 1 != curUnicode) { |
| 158 | // Consecutive charcodes mapping to non-consecutive unicodes |
| 159 | std::vector<uint32_t> unicodes; |
| 160 | unicodes.push_back(firstUnicode); |
| 161 | unicodes.push_back(curUnicode); |
| 162 | for (size_t i = 0; i < maxExtra; ++i) { |
| 163 | if (next_it == to_unicode.end() || curCharcode + 1 != next_it->first) |
| 164 | break; |
| 165 | ++iter; |
| 166 | ++curCharcode; |
| 167 | unicodes.push_back(iter->second); |
| 168 | next_it = std::next(iter); |
| 169 | } |
| 170 | ASSERT(iter->first - firstCharcode + 1 == unicodes.size()); |
| 171 | map_range_vector[std::make_pair(firstCharcode, iter->first)] = unicodes; |
| 172 | continue; |
| 173 | } |
| 174 | // Consecutive charcodes mapping to consecutive unicodes |
| 175 | for (size_t i = 0; i < maxExtra; ++i) { |
| 176 | if (next_it == to_unicode.end() || curCharcode + 1 != next_it->first || |
| 177 | curUnicode + 1 != next_it->second) { |
| 178 | break; |
| 179 | } |
| 180 | ++iter; |
| 181 | ++curCharcode; |
| 182 | ++curUnicode; |
| 183 | next_it = std::next(iter); |
| 184 | } |
| 185 | map_range[std::make_pair(firstCharcode, curCharcode)] = firstUnicode; |
| 186 | } |
Nicolas Pena | b48912f | 2017-05-17 14:45:52 -0400 | [diff] [blame] | 187 | CFX_ByteTextBuf buffer; |
| 188 | buffer << ToUnicodeStart; |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 189 | // Add maps to buffer |
| 190 | buffer << static_cast<uint32_t>(char_to_uni.size()) << " beginbfchar\n"; |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 191 | for (const auto& iter : char_to_uni) { |
| 192 | AddCharcode(&buffer, iter.first); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 193 | buffer << " "; |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 194 | AddUnicode(&buffer, iter.second); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 195 | buffer << "\n"; |
| 196 | } |
| 197 | buffer << "endbfchar\n" |
| 198 | << static_cast<uint32_t>(map_range_vector.size() + map_range.size()) |
| 199 | << " beginbfrange\n"; |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 200 | for (const auto& iter : map_range_vector) { |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 201 | const std::pair<uint32_t, uint32_t>& charcodeRange = iter.first; |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 202 | AddCharcode(&buffer, charcodeRange.first); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 203 | buffer << " "; |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 204 | AddCharcode(&buffer, charcodeRange.second); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 205 | buffer << " ["; |
| 206 | const std::vector<uint32_t>& unicodes = iter.second; |
| 207 | for (size_t i = 0; i < unicodes.size(); ++i) { |
| 208 | uint32_t uni = unicodes[i]; |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 209 | AddUnicode(&buffer, uni); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 210 | if (i != unicodes.size() - 1) |
| 211 | buffer << " "; |
| 212 | } |
| 213 | buffer << "]\n"; |
| 214 | } |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 215 | for (const auto& iter : map_range) { |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 216 | const std::pair<uint32_t, uint32_t>& charcodeRange = iter.first; |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 217 | AddCharcode(&buffer, charcodeRange.first); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 218 | buffer << " "; |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 219 | AddCharcode(&buffer, charcodeRange.second); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 220 | buffer << " "; |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 221 | AddUnicode(&buffer, iter.second); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 222 | buffer << "\n"; |
| 223 | } |
Nicolas Pena | b48912f | 2017-05-17 14:45:52 -0400 | [diff] [blame] | 224 | buffer << "endbfrange\n"; |
| 225 | buffer << ToUnicodeEnd; |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 226 | // TODO(npm): Encrypt / Compress? |
| 227 | uint32_t bufferSize = buffer.GetSize(); |
| 228 | auto pDict = pdfium::MakeUnique<CPDF_Dictionary>(); |
| 229 | pDict->SetNewFor<CPDF_Number>("Length", static_cast<int>(bufferSize)); |
| 230 | return pDoc->NewIndirect<CPDF_Stream>(buffer.DetachBuffer(), bufferSize, |
| 231 | std::move(pDict)); |
| 232 | } |
| 233 | |
Nicolas Pena | b83d870 | 2017-06-09 17:55:51 -0400 | [diff] [blame] | 234 | const uint32_t kMaxSimpleFontChar = 0xFF; |
| 235 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 236 | void* LoadSimpleFont(CPDF_Document* pDoc, |
| 237 | std::unique_ptr<CFX_Font> pFont, |
| 238 | const uint8_t* data, |
| 239 | uint32_t size, |
| 240 | int font_type) { |
| 241 | CPDF_Dictionary* fontDict = pDoc->NewIndirect<CPDF_Dictionary>(); |
| 242 | fontDict->SetNewFor<CPDF_Name>("Type", "Font"); |
| 243 | fontDict->SetNewFor<CPDF_Name>( |
| 244 | "Subtype", font_type == FPDF_FONT_TYPE1 ? "Type1" : "TrueType"); |
| 245 | CFX_ByteString name = pFont->GetFaceName(); |
| 246 | if (name.IsEmpty()) |
| 247 | name = "Unnamed"; |
| 248 | fontDict->SetNewFor<CPDF_Name>("BaseFont", name); |
| 249 | |
| 250 | uint32_t glyphIndex; |
Nicolas Pena | b83d870 | 2017-06-09 17:55:51 -0400 | [diff] [blame] | 251 | uint32_t currentChar = FXFT_Get_First_Char(pFont->GetFace(), &glyphIndex); |
| 252 | if (currentChar > kMaxSimpleFontChar || glyphIndex == 0) |
| 253 | return nullptr; |
| 254 | fontDict->SetNewFor<CPDF_Number>("FirstChar", static_cast<int>(currentChar)); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 255 | CPDF_Array* widthsArray = pDoc->NewIndirect<CPDF_Array>(); |
| 256 | while (true) { |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 257 | widthsArray->AddNew<CPDF_Number>(pFont->GetGlyphWidth(glyphIndex)); |
Nicolas Pena | b83d870 | 2017-06-09 17:55:51 -0400 | [diff] [blame] | 258 | uint32_t nextChar = |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 259 | FXFT_Get_Next_Char(pFont->GetFace(), currentChar, &glyphIndex); |
| 260 | // Simple fonts have 1-byte charcodes only. |
Nicolas Pena | b83d870 | 2017-06-09 17:55:51 -0400 | [diff] [blame] | 261 | if (nextChar > kMaxSimpleFontChar || glyphIndex == 0) |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 262 | break; |
Nicolas Pena | b83d870 | 2017-06-09 17:55:51 -0400 | [diff] [blame] | 263 | for (uint32_t i = currentChar + 1; i < nextChar; i++) |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 264 | widthsArray->AddNew<CPDF_Number>(0); |
| 265 | currentChar = nextChar; |
| 266 | } |
Nicolas Pena | b83d870 | 2017-06-09 17:55:51 -0400 | [diff] [blame] | 267 | fontDict->SetNewFor<CPDF_Number>("LastChar", static_cast<int>(currentChar)); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 268 | fontDict->SetNewFor<CPDF_Reference>("Widths", pDoc, widthsArray->GetObjNum()); |
| 269 | CPDF_Dictionary* fontDesc = |
| 270 | LoadFontDesc(pDoc, name, pFont.get(), data, size, font_type); |
| 271 | |
| 272 | fontDict->SetNewFor<CPDF_Reference>("FontDescriptor", pDoc, |
| 273 | fontDesc->GetObjNum()); |
| 274 | return pDoc->LoadFont(fontDict); |
| 275 | } |
| 276 | |
Nicolas Pena | b83d870 | 2017-06-09 17:55:51 -0400 | [diff] [blame] | 277 | const uint32_t kMaxUnicode = 0x10FFFF; |
| 278 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 279 | void* LoadCompositeFont(CPDF_Document* pDoc, |
| 280 | std::unique_ptr<CFX_Font> pFont, |
| 281 | const uint8_t* data, |
| 282 | uint32_t size, |
| 283 | int font_type) { |
| 284 | CPDF_Dictionary* fontDict = pDoc->NewIndirect<CPDF_Dictionary>(); |
| 285 | fontDict->SetNewFor<CPDF_Name>("Type", "Font"); |
| 286 | fontDict->SetNewFor<CPDF_Name>("Subtype", "Type0"); |
| 287 | // TODO(npm): Get the correct encoding, if it's not identity. |
| 288 | CFX_ByteString encoding = "Identity-H"; |
| 289 | fontDict->SetNewFor<CPDF_Name>("Encoding", encoding); |
| 290 | CFX_ByteString name = pFont->GetFaceName(); |
| 291 | if (name.IsEmpty()) |
| 292 | name = "Unnamed"; |
| 293 | fontDict->SetNewFor<CPDF_Name>( |
| 294 | "BaseFont", font_type == FPDF_FONT_TYPE1 ? name + "-" + encoding : name); |
| 295 | |
| 296 | CPDF_Dictionary* pCIDFont = pDoc->NewIndirect<CPDF_Dictionary>(); |
| 297 | pCIDFont->SetNewFor<CPDF_Name>("Type", "Font"); |
| 298 | pCIDFont->SetNewFor<CPDF_Name>("Subtype", font_type == FPDF_FONT_TYPE1 |
| 299 | ? "CIDFontType0" |
| 300 | : "CIDFontType2"); |
| 301 | pCIDFont->SetNewFor<CPDF_Name>("BaseFont", name); |
| 302 | |
| 303 | // TODO(npm): Maybe use FT_Get_CID_Registry_Ordering_Supplement to get the |
| 304 | // CIDSystemInfo |
| 305 | CPDF_Dictionary* pCIDSystemInfo = pDoc->NewIndirect<CPDF_Dictionary>(); |
| 306 | pCIDSystemInfo->SetNewFor<CPDF_Name>("Registry", "Adobe"); |
| 307 | pCIDSystemInfo->SetNewFor<CPDF_Name>("Ordering", "Identity"); |
| 308 | pCIDSystemInfo->SetNewFor<CPDF_Number>("Supplement", 0); |
| 309 | pCIDFont->SetNewFor<CPDF_Reference>("CIDSystemInfo", pDoc, |
| 310 | pCIDSystemInfo->GetObjNum()); |
| 311 | |
| 312 | CPDF_Dictionary* fontDesc = |
| 313 | LoadFontDesc(pDoc, name, pFont.get(), data, size, font_type); |
| 314 | pCIDFont->SetNewFor<CPDF_Reference>("FontDescriptor", pDoc, |
| 315 | fontDesc->GetObjNum()); |
| 316 | |
| 317 | uint32_t glyphIndex; |
Nicolas Pena | b83d870 | 2017-06-09 17:55:51 -0400 | [diff] [blame] | 318 | uint32_t currentChar = FXFT_Get_First_Char(pFont->GetFace(), &glyphIndex); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 319 | // If it doesn't have a single char, just fail |
Nicolas Pena | b83d870 | 2017-06-09 17:55:51 -0400 | [diff] [blame] | 320 | if (glyphIndex == 0 || currentChar > kMaxUnicode) |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 321 | return nullptr; |
| 322 | |
| 323 | std::map<uint32_t, uint32_t> to_unicode; |
| 324 | std::map<uint32_t, uint32_t> widths; |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 325 | while (true) { |
Nicolas Pena | b83d870 | 2017-06-09 17:55:51 -0400 | [diff] [blame] | 326 | if (currentChar > kMaxUnicode) |
Nicolas Pena | fb71fbb | 2017-05-23 13:16:09 -0400 | [diff] [blame] | 327 | break; |
| 328 | |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 329 | widths[glyphIndex] = pFont->GetGlyphWidth(glyphIndex); |
| 330 | to_unicode[glyphIndex] = currentChar; |
| 331 | currentChar = |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 332 | FXFT_Get_Next_Char(pFont->GetFace(), currentChar, &glyphIndex); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 333 | if (glyphIndex == 0) |
| 334 | break; |
| 335 | } |
| 336 | CPDF_Array* widthsArray = pDoc->NewIndirect<CPDF_Array>(); |
| 337 | for (auto it = widths.begin(); it != widths.end(); ++it) { |
| 338 | int ch = it->first; |
| 339 | int w = it->second; |
| 340 | if (std::next(it) == widths.end()) { |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 341 | // Only one char left, use format c [w] |
| 342 | auto oneW = pdfium::MakeUnique<CPDF_Array>(); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 343 | oneW->AddNew<CPDF_Number>(w); |
| 344 | widthsArray->AddNew<CPDF_Number>(ch); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 345 | widthsArray->Add(std::move(oneW)); |
| 346 | break; |
| 347 | } |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 348 | ++it; |
| 349 | int next_ch = it->first; |
| 350 | int next_w = it->second; |
| 351 | if (next_ch == ch + 1 && next_w == w) { |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 352 | // The array can have a group c_first c_last w: all CIDs in the range from |
| 353 | // c_first to c_last will have width w |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 354 | widthsArray->AddNew<CPDF_Number>(ch); |
| 355 | ch = next_ch; |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 356 | while (true) { |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 357 | auto next_it = std::next(it); |
| 358 | if (next_it == widths.end() || next_it->first != it->first + 1 || |
| 359 | next_it->second != it->second) { |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 360 | break; |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 361 | } |
| 362 | ++it; |
| 363 | ch = it->first; |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 364 | } |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 365 | widthsArray->AddNew<CPDF_Number>(ch); |
| 366 | widthsArray->AddNew<CPDF_Number>(w); |
| 367 | continue; |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 368 | } |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 369 | // Otherwise we can have a group of the form c [w1 w2 ...]: c has width |
| 370 | // w1, c+1 has width w2, etc. |
| 371 | widthsArray->AddNew<CPDF_Number>(ch); |
| 372 | auto curWidthArray = pdfium::MakeUnique<CPDF_Array>(); |
| 373 | curWidthArray->AddNew<CPDF_Number>(w); |
| 374 | curWidthArray->AddNew<CPDF_Number>(next_w); |
| 375 | while (true) { |
| 376 | auto next_it = std::next(it); |
| 377 | if (next_it == widths.end() || next_it->first != it->first + 1) |
| 378 | break; |
| 379 | ++it; |
| 380 | curWidthArray->AddNew<CPDF_Number>(static_cast<int>(it->second)); |
| 381 | } |
| 382 | widthsArray->Add(std::move(curWidthArray)); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 383 | } |
| 384 | pCIDFont->SetNewFor<CPDF_Reference>("W", pDoc, widthsArray->GetObjNum()); |
| 385 | // TODO(npm): Support vertical writing |
| 386 | |
| 387 | auto pDescendant = pdfium::MakeUnique<CPDF_Array>(); |
| 388 | pDescendant->AddNew<CPDF_Reference>(pDoc, pCIDFont->GetObjNum()); |
| 389 | fontDict->SetFor("DescendantFonts", std::move(pDescendant)); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 390 | CPDF_Stream* toUnicodeStream = LoadUnicode(pDoc, to_unicode); |
| 391 | fontDict->SetNewFor<CPDF_Reference>("ToUnicode", pDoc, |
| 392 | toUnicodeStream->GetObjNum()); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 393 | return pDoc->LoadFont(fontDict); |
| 394 | } |
| 395 | |
| 396 | } // namespace |
| 397 | |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 398 | DLLEXPORT FPDF_PAGEOBJECT STDCALL FPDFPageObj_NewTextObj(FPDF_DOCUMENT document, |
| 399 | FPDF_BYTESTRING font, |
| 400 | float font_size) { |
| 401 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 402 | if (!pDoc) |
| 403 | return nullptr; |
| 404 | |
| 405 | CPDF_Font* pFont = CPDF_Font::GetStockFont(pDoc, CFX_ByteStringC(font)); |
| 406 | if (!pFont) |
| 407 | return nullptr; |
| 408 | |
Tom Sepez | fe91c6c | 2017-05-16 15:33:20 -0700 | [diff] [blame] | 409 | auto pTextObj = pdfium::MakeUnique<CPDF_TextObject>(); |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 410 | pTextObj->m_TextState.SetFont(pFont); |
| 411 | pTextObj->m_TextState.SetFontSize(font_size); |
| 412 | pTextObj->DefaultStates(); |
Tom Sepez | fe91c6c | 2017-05-16 15:33:20 -0700 | [diff] [blame] | 413 | return pTextObj.release(); // Caller takes ownership. |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | DLLEXPORT FPDF_BOOL STDCALL FPDFText_SetText(FPDF_PAGEOBJECT text_object, |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 417 | FPDF_WIDESTRING text) { |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 418 | auto* pTextObj = static_cast<CPDF_TextObject*>(text_object); |
| 419 | if (!pTextObj) |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 420 | return false; |
| 421 | |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 422 | FX_STRSIZE len = CFX_WideString::WStringLength(text); |
| 423 | CFX_WideString encodedText = CFX_WideString::FromUTF16LE(text, len); |
| 424 | CFX_ByteString byteText; |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 425 | for (wchar_t wc : encodedText) { |
| 426 | pTextObj->GetFont()->AppendChar( |
| 427 | &byteText, pTextObj->GetFont()->CharCodeFromUnicode(wc)); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 428 | } |
| 429 | pTextObj->SetText(byteText); |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 430 | return true; |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 431 | } |
| 432 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 433 | DLLEXPORT FPDF_FONT STDCALL FPDFText_LoadFont(FPDF_DOCUMENT document, |
| 434 | const uint8_t* data, |
| 435 | uint32_t size, |
| 436 | int font_type, |
| 437 | FPDF_BOOL cid) { |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 438 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 439 | if (!pDoc || !data || size == 0 || |
| 440 | (font_type != FPDF_FONT_TYPE1 && font_type != FPDF_FONT_TRUETYPE)) { |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 441 | return nullptr; |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 442 | } |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 443 | |
| 444 | auto pFont = pdfium::MakeUnique<CFX_Font>(); |
| 445 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 446 | // TODO(npm): Maybe use FT_Get_X11_Font_Format to check format? Otherwise, we |
| 447 | // are allowing giving any font that can be loaded on freetype and setting it |
| 448 | // as any font type. |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 449 | if (!pFont->LoadEmbedded(data, size)) |
| 450 | return nullptr; |
| 451 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 452 | return cid ? LoadCompositeFont(pDoc, std::move(pFont), data, size, font_type) |
| 453 | : LoadSimpleFont(pDoc, std::move(pFont), data, size, font_type); |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 454 | } |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 455 | |
wileyrya | 864e9fb | 2017-05-26 11:38:14 -0500 | [diff] [blame] | 456 | DLLEXPORT FPDF_BOOL STDCALL FPDFText_SetFillColor(FPDF_PAGEOBJECT text_object, |
| 457 | unsigned int R, |
| 458 | unsigned int G, |
| 459 | unsigned int B, |
| 460 | unsigned int A) { |
| 461 | return FPDFPageObj_SetFillColor(text_object, R, G, B, A); |
| 462 | } |
| 463 | |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 464 | DLLEXPORT void STDCALL FPDFFont_Close(FPDF_FONT font) { |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 465 | CPDF_Font* pFont = static_cast<CPDF_Font*>(font); |
| 466 | if (!pFont) |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 467 | return; |
| 468 | |
Lei Zhang | b8a8c43 | 2017-05-04 14:10:50 -0700 | [diff] [blame] | 469 | CPDF_Document* pDoc = pFont->GetDocument(); |
| 470 | if (!pDoc) |
| 471 | return; |
| 472 | |
| 473 | CPDF_DocPageData* pPageData = pDoc->GetPageData(); |
| 474 | if (!pPageData->IsForceClear()) |
| 475 | pPageData->ReleaseFont(pFont->GetFontDict()); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | DLLEXPORT FPDF_PAGEOBJECT STDCALL |
| 479 | FPDFPageObj_CreateTextObj(FPDF_DOCUMENT document, |
| 480 | FPDF_FONT font, |
| 481 | float font_size) { |
| 482 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
Nicolas Pena | 54b9166 | 2017-05-05 16:49:30 -0400 | [diff] [blame] | 483 | CPDF_Font* pFont = static_cast<CPDF_Font*>(font); |
| 484 | if (!pDoc || !pFont) |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 485 | return nullptr; |
| 486 | |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 487 | auto pTextObj = pdfium::MakeUnique<CPDF_TextObject>(); |
| 488 | pTextObj->m_TextState.SetFont(pDoc->LoadFont(pFont->GetFontDict())); |
| 489 | pTextObj->m_TextState.SetFontSize(font_size); |
| 490 | pTextObj->DefaultStates(); |
| 491 | return pTextObj.release(); |
| 492 | } |