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