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 | 95e854f | 2015-06-13 00:58:06 -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 | |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 7 | #include "core/fpdfapi/parser/cpdf_document.h" |
Tom Sepez | 461a26d | 2015-06-02 14:29:00 -0700 | [diff] [blame] | 8 | |
thestig | 823df2a | 2016-07-29 06:15:39 -0700 | [diff] [blame] | 9 | #include <memory> |
Wei Li | 215816b | 2016-01-14 12:29:02 -0800 | [diff] [blame] | 10 | #include <set> |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 11 | #include <utility> |
thestig | 823df2a | 2016-07-29 06:15:39 -0700 | [diff] [blame] | 12 | #include <vector> |
Wei Li | 215816b | 2016-01-14 12:29:02 -0800 | [diff] [blame] | 13 | |
dsinclair | 39c62fd | 2016-09-29 12:49:17 -0700 | [diff] [blame] | 14 | #include "core/fpdfapi/cpdf_modulemgr.h" |
dsinclair | bc5e6d2 | 2016-10-04 11:08:49 -0700 | [diff] [blame] | 15 | #include "core/fpdfapi/font/cpdf_fontencoding.h" |
tsepez | 9fa95ef | 2016-10-11 12:29:38 -0700 | [diff] [blame] | 16 | #include "core/fpdfapi/page/cpdf_docpagedata.h" |
Dan Sinclair | 5acacd3 | 2017-05-25 14:04:59 -0400 | [diff] [blame] | 17 | #include "core/fpdfapi/page/cpdf_iccprofile.h" |
dsinclair | 41872fa | 2016-10-04 11:29:35 -0700 | [diff] [blame] | 18 | #include "core/fpdfapi/page/cpdf_pagemodule.h" |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 19 | #include "core/fpdfapi/parser/cpdf_array.h" |
| 20 | #include "core/fpdfapi/parser/cpdf_dictionary.h" |
tsepez | c09625c | 2016-11-07 11:46:09 -0800 | [diff] [blame] | 21 | #include "core/fpdfapi/parser/cpdf_linearized_header.h" |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 22 | #include "core/fpdfapi/parser/cpdf_name.h" |
tsepez | 10a2853 | 2016-10-10 18:45:55 -0700 | [diff] [blame] | 23 | #include "core/fpdfapi/parser/cpdf_number.h" |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 24 | #include "core/fpdfapi/parser/cpdf_parser.h" |
| 25 | #include "core/fpdfapi/parser/cpdf_reference.h" |
| 26 | #include "core/fpdfapi/parser/cpdf_stream.h" |
dsinclair | 37b6d14 | 2016-11-21 17:05:58 -0800 | [diff] [blame] | 27 | #include "core/fpdfapi/parser/cpdf_string.h" |
Nicolas Pena | 0ef7ba0 | 2017-01-04 16:18:57 -0500 | [diff] [blame] | 28 | #include "core/fpdfapi/render/cpdf_dibsource.h" |
npm | 935c631 | 2016-11-17 10:47:43 -0800 | [diff] [blame] | 29 | #include "core/fpdfapi/render/cpdf_docrenderdata.h" |
dsinclair | 8a4e286 | 2016-09-29 13:43:30 -0700 | [diff] [blame] | 30 | #include "core/fxcodec/JBig2_DocumentContext.h" |
Dan Sinclair | f51a02a | 2017-04-19 12:46:53 -0400 | [diff] [blame] | 31 | #include "core/fxcrt/fx_codepage.h" |
dsinclair | 74a34fc | 2016-09-29 16:41:42 -0700 | [diff] [blame] | 32 | #include "core/fxge/cfx_unicodeencoding.h" |
| 33 | #include "core/fxge/fx_font.h" |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 34 | #include "third_party/base/ptr_util.h" |
Wei Li | 215816b | 2016-01-14 12:29:02 -0800 | [diff] [blame] | 35 | #include "third_party/base/stl_util.h" |
| 36 | |
| 37 | namespace { |
| 38 | |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 39 | const int FX_MAX_PAGE_LEVEL = 1024; |
| 40 | |
npm | e9988dd | 2016-09-16 13:57:22 -0700 | [diff] [blame] | 41 | void InsertWidthArrayImpl(int* widths, int size, CPDF_Array* pWidthArray) { |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 42 | int i; |
| 43 | for (i = 1; i < size; i++) { |
| 44 | if (widths[i] != *widths) |
| 45 | break; |
| 46 | } |
| 47 | if (i == size) { |
| 48 | int first = pWidthArray->GetIntegerAt(pWidthArray->GetCount() - 1); |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 49 | pWidthArray->AddNew<CPDF_Number>(first + size - 1); |
| 50 | pWidthArray->AddNew<CPDF_Number>(*widths); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 51 | } else { |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 52 | CPDF_Array* pWidthArray1 = pWidthArray->AddNew<CPDF_Array>(); |
npm | 01b67ed | 2016-09-12 13:38:52 -0700 | [diff] [blame] | 53 | for (i = 0; i < size; i++) |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 54 | pWidthArray1->AddNew<CPDF_Number>(widths[i]); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 55 | } |
| 56 | FX_Free(widths); |
| 57 | } |
| 58 | |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 59 | #if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
npm | e9988dd | 2016-09-16 13:57:22 -0700 | [diff] [blame] | 60 | void InsertWidthArray(HDC hDC, int start, int end, CPDF_Array* pWidthArray) { |
| 61 | int size = end - start + 1; |
| 62 | int* widths = FX_Alloc(int, size); |
| 63 | GetCharWidth(hDC, start, end, widths); |
| 64 | InsertWidthArrayImpl(widths, size, pWidthArray); |
| 65 | } |
| 66 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 67 | ByteString FPDF_GetPSNameFromTT(HDC hDC) { |
| 68 | ByteString result; |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 69 | DWORD size = ::GetFontData(hDC, 'eman', 0, nullptr, 0); |
| 70 | if (size != GDI_ERROR) { |
| 71 | LPBYTE buffer = FX_Alloc(BYTE, size); |
| 72 | ::GetFontData(hDC, 'eman', 0, buffer, size); |
| 73 | result = GetNameFromTT(buffer, size, 6); |
| 74 | FX_Free(buffer); |
| 75 | } |
| 76 | return result; |
| 77 | } |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 78 | #endif // _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 79 | |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 80 | void InsertWidthArray1(CFX_Font* pFont, |
| 81 | CFX_UnicodeEncoding* pEncoding, |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 82 | wchar_t start, |
| 83 | wchar_t end, |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 84 | CPDF_Array* pWidthArray) { |
| 85 | int size = end - start + 1; |
| 86 | int* widths = FX_Alloc(int, size); |
| 87 | int i; |
| 88 | for (i = 0; i < size; i++) { |
| 89 | int glyph_index = pEncoding->GlyphFromCharCode(start + i); |
| 90 | widths[i] = pFont->GetGlyphWidth(glyph_index); |
| 91 | } |
npm | e9988dd | 2016-09-16 13:57:22 -0700 | [diff] [blame] | 92 | InsertWidthArrayImpl(widths, size, pWidthArray); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Wei Li | 215816b | 2016-01-14 12:29:02 -0800 | [diff] [blame] | 95 | int CountPages(CPDF_Dictionary* pPages, |
| 96 | std::set<CPDF_Dictionary*>* visited_pages) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 97 | int count = pPages->GetIntegerFor("Count"); |
npm | 01b67ed | 2016-09-12 13:38:52 -0700 | [diff] [blame] | 98 | if (count > 0 && count < FPDF_PAGE_MAX_NUM) |
Wei Li | 215816b | 2016-01-14 12:29:02 -0800 | [diff] [blame] | 99 | return count; |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 100 | CPDF_Array* pKidList = pPages->GetArrayFor("Kids"); |
npm | 01b67ed | 2016-09-12 13:38:52 -0700 | [diff] [blame] | 101 | if (!pKidList) |
Wei Li | 215816b | 2016-01-14 12:29:02 -0800 | [diff] [blame] | 102 | return 0; |
Wei Li | 215816b | 2016-01-14 12:29:02 -0800 | [diff] [blame] | 103 | count = 0; |
Wei Li | e1aebd4 | 2016-04-11 10:02:09 -0700 | [diff] [blame] | 104 | for (size_t i = 0; i < pKidList->GetCount(); i++) { |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 105 | CPDF_Dictionary* pKid = pKidList->GetDictAt(i); |
npm | 01b67ed | 2016-09-12 13:38:52 -0700 | [diff] [blame] | 106 | if (!pKid || pdfium::ContainsKey(*visited_pages, pKid)) |
Wei Li | 215816b | 2016-01-14 12:29:02 -0800 | [diff] [blame] | 107 | continue; |
Wei Li | 215816b | 2016-01-14 12:29:02 -0800 | [diff] [blame] | 108 | if (pKid->KeyExist("Kids")) { |
| 109 | // Use |visited_pages| to help detect circular references of pages. |
Tom Sepez | 9940619 | 2016-03-09 17:37:12 -0800 | [diff] [blame] | 110 | pdfium::ScopedSetInsertion<CPDF_Dictionary*> local_add(visited_pages, |
| 111 | pKid); |
Wei Li | 215816b | 2016-01-14 12:29:02 -0800 | [diff] [blame] | 112 | count += CountPages(pKid, visited_pages); |
| 113 | } else { |
| 114 | // This page is a leaf node. |
| 115 | count++; |
| 116 | } |
| 117 | } |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 118 | pPages->SetNewFor<CPDF_Number>("Count", count); |
Wei Li | 215816b | 2016-01-14 12:29:02 -0800 | [diff] [blame] | 119 | return count; |
| 120 | } |
| 121 | |
npm | 01b67ed | 2016-09-12 13:38:52 -0700 | [diff] [blame] | 122 | int CalculateFlags(bool bold, |
| 123 | bool italic, |
| 124 | bool fixedPitch, |
| 125 | bool serif, |
| 126 | bool script, |
| 127 | bool symbolic) { |
| 128 | int flags = 0; |
| 129 | if (bold) |
Nicolas Pena | 412fa65 | 2017-02-24 16:16:02 -0500 | [diff] [blame] | 130 | flags |= FXFONT_BOLD; |
npm | 01b67ed | 2016-09-12 13:38:52 -0700 | [diff] [blame] | 131 | if (italic) |
Nicolas Pena | 412fa65 | 2017-02-24 16:16:02 -0500 | [diff] [blame] | 132 | flags |= FXFONT_ITALIC; |
npm | 01b67ed | 2016-09-12 13:38:52 -0700 | [diff] [blame] | 133 | if (fixedPitch) |
Nicolas Pena | 412fa65 | 2017-02-24 16:16:02 -0500 | [diff] [blame] | 134 | flags |= FXFONT_FIXED_PITCH; |
npm | 01b67ed | 2016-09-12 13:38:52 -0700 | [diff] [blame] | 135 | if (serif) |
Nicolas Pena | 412fa65 | 2017-02-24 16:16:02 -0500 | [diff] [blame] | 136 | flags |= FXFONT_SERIF; |
npm | 01b67ed | 2016-09-12 13:38:52 -0700 | [diff] [blame] | 137 | if (script) |
Nicolas Pena | 412fa65 | 2017-02-24 16:16:02 -0500 | [diff] [blame] | 138 | flags |= FXFONT_SCRIPT; |
npm | 01b67ed | 2016-09-12 13:38:52 -0700 | [diff] [blame] | 139 | if (symbolic) |
Nicolas Pena | 412fa65 | 2017-02-24 16:16:02 -0500 | [diff] [blame] | 140 | flags |= FXFONT_SYMBOLIC; |
npm | 01b67ed | 2016-09-12 13:38:52 -0700 | [diff] [blame] | 141 | else |
Nicolas Pena | 412fa65 | 2017-02-24 16:16:02 -0500 | [diff] [blame] | 142 | flags |= FXFONT_NONSYMBOLIC; |
npm | 01b67ed | 2016-09-12 13:38:52 -0700 | [diff] [blame] | 143 | return flags; |
| 144 | } |
| 145 | |
| 146 | void ProcessNonbCJK(CPDF_Dictionary* pBaseDict, |
| 147 | bool bold, |
| 148 | bool italic, |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 149 | ByteString basefont, |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 150 | std::unique_ptr<CPDF_Array> pWidths) { |
npm | 01b67ed | 2016-09-12 13:38:52 -0700 | [diff] [blame] | 151 | if (bold && italic) |
| 152 | basefont += ",BoldItalic"; |
| 153 | else if (bold) |
| 154 | basefont += ",Bold"; |
| 155 | else if (italic) |
| 156 | basefont += ",Italic"; |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 157 | pBaseDict->SetNewFor<CPDF_Name>("Subtype", "TrueType"); |
| 158 | pBaseDict->SetNewFor<CPDF_Name>("BaseFont", basefont); |
| 159 | pBaseDict->SetNewFor<CPDF_Number>("FirstChar", 32); |
| 160 | pBaseDict->SetNewFor<CPDF_Number>("LastChar", 255); |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 161 | pBaseDict->SetFor("Widths", std::move(pWidths)); |
npm | 01b67ed | 2016-09-12 13:38:52 -0700 | [diff] [blame] | 162 | } |
| 163 | |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 164 | std::unique_ptr<CPDF_Dictionary> CalculateFontDesc( |
| 165 | CPDF_Document* pDoc, |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 166 | ByteString basefont, |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 167 | int flags, |
| 168 | int italicangle, |
| 169 | int ascend, |
| 170 | int descend, |
| 171 | std::unique_ptr<CPDF_Array> bbox, |
| 172 | int32_t stemV) { |
tsepez | 70c4afd | 2016-11-15 11:33:44 -0800 | [diff] [blame] | 173 | auto pFontDesc = |
| 174 | pdfium::MakeUnique<CPDF_Dictionary>(pDoc->GetByteStringPool()); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 175 | pFontDesc->SetNewFor<CPDF_Name>("Type", "FontDescriptor"); |
| 176 | pFontDesc->SetNewFor<CPDF_Name>("FontName", basefont); |
| 177 | pFontDesc->SetNewFor<CPDF_Number>("Flags", flags); |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 178 | pFontDesc->SetFor("FontBBox", std::move(bbox)); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 179 | pFontDesc->SetNewFor<CPDF_Number>("ItalicAngle", italicangle); |
| 180 | pFontDesc->SetNewFor<CPDF_Number>("Ascent", ascend); |
| 181 | pFontDesc->SetNewFor<CPDF_Number>("Descent", descend); |
| 182 | pFontDesc->SetNewFor<CPDF_Number>("StemV", stemV); |
npm | e9988dd | 2016-09-16 13:57:22 -0700 | [diff] [blame] | 183 | return pFontDesc; |
| 184 | } |
| 185 | |
Wei Li | 215816b | 2016-01-14 12:29:02 -0800 | [diff] [blame] | 186 | } // namespace |
Wei Li | aa0f69a | 2016-01-07 10:49:33 -0800 | [diff] [blame] | 187 | |
dsinclair | cedaa55 | 2016-08-24 11:12:19 -0700 | [diff] [blame] | 188 | CPDF_Document::CPDF_Document(std::unique_ptr<CPDF_Parser> pParser) |
dsinclair | a61c01e | 2016-08-24 10:31:23 -0700 | [diff] [blame] | 189 | : CPDF_IndirectObjectHolder(), |
dsinclair | cedaa55 | 2016-08-24 11:12:19 -0700 | [diff] [blame] | 190 | m_pParser(std::move(pParser)), |
dsinclair | d647a6b | 2016-04-26 13:13:20 -0700 | [diff] [blame] | 191 | m_pRootDict(nullptr), |
npm | ec64cee | 2016-11-04 12:54:51 -0700 | [diff] [blame] | 192 | m_iNextPageToTraverse(0), |
Nicolas Pena | 83c5eac | 2017-01-03 14:33:20 -0500 | [diff] [blame] | 193 | m_bReachedMaxPageLevel(false), |
dsinclair | d647a6b | 2016-04-26 13:13:20 -0700 | [diff] [blame] | 194 | m_bLinearized(false), |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 195 | m_iFirstPageNo(0), |
dsinclair | d647a6b | 2016-04-26 13:13:20 -0700 | [diff] [blame] | 196 | m_dwFirstPageObjNum(0), |
Tom Sepez | 012ae89 | 2017-04-25 16:39:34 -0700 | [diff] [blame] | 197 | m_pDocPage(pdfium::MakeUnique<CPDF_DocPageData>(this)), |
| 198 | m_pDocRender(pdfium::MakeUnique<CPDF_DocRenderData>(this)) { |
dsinclair | a61c01e | 2016-08-24 10:31:23 -0700 | [diff] [blame] | 199 | if (pParser) |
| 200 | SetLastObjNum(m_pParser->GetLastObjNum()); |
| 201 | } |
dsinclair | d647a6b | 2016-04-26 13:13:20 -0700 | [diff] [blame] | 202 | |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 203 | CPDF_Document::~CPDF_Document() { |
npm | fd5ae3f | 2016-10-21 09:42:33 -0700 | [diff] [blame] | 204 | CPDF_ModuleMgr::Get()->GetPageModule()->ClearStockFont(this); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 205 | } |
dsinclair | d647a6b | 2016-04-26 13:13:20 -0700 | [diff] [blame] | 206 | |
tsepez | 5ab31ef | 2016-11-07 13:49:17 -0800 | [diff] [blame] | 207 | std::unique_ptr<CPDF_Object> CPDF_Document::ParseIndirectObject( |
| 208 | uint32_t objnum) { |
dsinclair | a61c01e | 2016-08-24 10:31:23 -0700 | [diff] [blame] | 209 | return m_pParser ? m_pParser->ParseIndirectObject(this, objnum) : nullptr; |
| 210 | } |
| 211 | |
dsinclair | 260f5fb | 2016-08-17 12:45:26 -0700 | [diff] [blame] | 212 | void CPDF_Document::LoadDocInternal() { |
dsinclair | 03bd7c7 | 2016-08-23 20:13:41 -0700 | [diff] [blame] | 213 | SetLastObjNum(m_pParser->GetLastObjNum()); |
dsinclair | 260f5fb | 2016-08-17 12:45:26 -0700 | [diff] [blame] | 214 | |
dsinclair | 03bd7c7 | 2016-08-23 20:13:41 -0700 | [diff] [blame] | 215 | CPDF_Object* pRootObj = GetOrParseIndirectObject(m_pParser->GetRootObjNum()); |
dsinclair | f85e7e2 | 2016-08-16 11:43:23 -0700 | [diff] [blame] | 216 | if (!pRootObj) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 217 | return; |
dsinclair | f85e7e2 | 2016-08-16 11:43:23 -0700 | [diff] [blame] | 218 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 219 | m_pRootDict = pRootObj->GetDict(); |
dsinclair | f85e7e2 | 2016-08-16 11:43:23 -0700 | [diff] [blame] | 220 | if (!m_pRootDict) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 221 | return; |
dsinclair | f85e7e2 | 2016-08-16 11:43:23 -0700 | [diff] [blame] | 222 | |
Henrique Nakashima | b73ce7b | 2017-06-19 16:04:34 -0400 | [diff] [blame] | 223 | LoadDocumentInfo(); |
| 224 | } |
| 225 | |
| 226 | void CPDF_Document::LoadDocumentInfo() { |
Lei Zhang | d78ef3b | 2017-10-20 17:23:40 -0700 | [diff] [blame] | 227 | if (!m_pParser) |
| 228 | return; |
| 229 | |
dsinclair | 03bd7c7 | 2016-08-23 20:13:41 -0700 | [diff] [blame] | 230 | CPDF_Object* pInfoObj = GetOrParseIndirectObject(m_pParser->GetInfoObjNum()); |
dsinclair | f85e7e2 | 2016-08-16 11:43:23 -0700 | [diff] [blame] | 231 | if (pInfoObj) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 232 | m_pInfoDict = pInfoObj->GetDict(); |
dsinclair | f85e7e2 | 2016-08-16 11:43:23 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | void CPDF_Document::LoadDoc() { |
| 236 | LoadDocInternal(); |
tsepez | 0fa4717 | 2017-01-09 07:01:36 -0800 | [diff] [blame] | 237 | LoadPages(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 238 | } |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 239 | |
art-snake | 240dec5 | 2016-11-07 08:42:04 -0800 | [diff] [blame] | 240 | void CPDF_Document::LoadLinearizedDoc( |
tsepez | c09625c | 2016-11-07 11:46:09 -0800 | [diff] [blame] | 241 | const CPDF_LinearizedHeader* pLinearizationParams) { |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 242 | m_bLinearized = true; |
dsinclair | f85e7e2 | 2016-08-16 11:43:23 -0700 | [diff] [blame] | 243 | LoadDocInternal(); |
tsepez | 0fa4717 | 2017-01-09 07:01:36 -0800 | [diff] [blame] | 244 | m_PageList.resize(pLinearizationParams->GetPageCount()); |
art-snake | 240dec5 | 2016-11-07 08:42:04 -0800 | [diff] [blame] | 245 | m_iFirstPageNo = pLinearizationParams->GetFirstPageNo(); |
| 246 | m_dwFirstPageObjNum = pLinearizationParams->GetFirstPageObjNum(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 247 | } |
dsinclair | d647a6b | 2016-04-26 13:13:20 -0700 | [diff] [blame] | 248 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 249 | void CPDF_Document::LoadPages() { |
tsepez | 0fa4717 | 2017-01-09 07:01:36 -0800 | [diff] [blame] | 250 | m_PageList.resize(RetrievePageCount()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 251 | } |
dsinclair | d647a6b | 2016-04-26 13:13:20 -0700 | [diff] [blame] | 252 | |
npm | ec64cee | 2016-11-04 12:54:51 -0700 | [diff] [blame] | 253 | CPDF_Dictionary* CPDF_Document::TraversePDFPages(int iPage, |
| 254 | int* nPagesToGo, |
| 255 | size_t level) { |
Nicolas Pena | 83c5eac | 2017-01-03 14:33:20 -0500 | [diff] [blame] | 256 | if (*nPagesToGo < 0 || m_bReachedMaxPageLevel) |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 257 | return nullptr; |
tsepez | 0fa4717 | 2017-01-09 07:01:36 -0800 | [diff] [blame] | 258 | |
npm | ec64cee | 2016-11-04 12:54:51 -0700 | [diff] [blame] | 259 | CPDF_Dictionary* pPages = m_pTreeTraversal[level].first; |
| 260 | CPDF_Array* pKidList = pPages->GetArrayFor("Kids"); |
| 261 | if (!pKidList) { |
Nicolas Pena | 478c226 | 2017-03-21 14:06:04 -0400 | [diff] [blame] | 262 | m_pTreeTraversal.pop_back(); |
npm | ec64cee | 2016-11-04 12:54:51 -0700 | [diff] [blame] | 263 | if (*nPagesToGo != 1) |
| 264 | return nullptr; |
tsepez | 0fa4717 | 2017-01-09 07:01:36 -0800 | [diff] [blame] | 265 | m_PageList[iPage] = pPages->GetObjNum(); |
npm | ec64cee | 2016-11-04 12:54:51 -0700 | [diff] [blame] | 266 | return pPages; |
| 267 | } |
npm | ec64cee | 2016-11-04 12:54:51 -0700 | [diff] [blame] | 268 | if (level >= FX_MAX_PAGE_LEVEL) { |
| 269 | m_pTreeTraversal.pop_back(); |
Nicolas Pena | 83c5eac | 2017-01-03 14:33:20 -0500 | [diff] [blame] | 270 | m_bReachedMaxPageLevel = true; |
npm | ec64cee | 2016-11-04 12:54:51 -0700 | [diff] [blame] | 271 | return nullptr; |
| 272 | } |
npm | ec64cee | 2016-11-04 12:54:51 -0700 | [diff] [blame] | 273 | CPDF_Dictionary* page = nullptr; |
| 274 | for (size_t i = m_pTreeTraversal[level].second; i < pKidList->GetCount(); |
| 275 | i++) { |
| 276 | if (*nPagesToGo == 0) |
| 277 | break; |
Artem Strygin | 3fab4e3 | 2017-08-23 22:16:13 +0300 | [diff] [blame] | 278 | pKidList->ConvertToIndirectObjectAt(i, this); |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 279 | CPDF_Dictionary* pKid = pKidList->GetDictAt(i); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 280 | if (!pKid) { |
npm | ec64cee | 2016-11-04 12:54:51 -0700 | [diff] [blame] | 281 | (*nPagesToGo)--; |
| 282 | m_pTreeTraversal[level].second++; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 283 | continue; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 284 | } |
npm | ec64cee | 2016-11-04 12:54:51 -0700 | [diff] [blame] | 285 | if (pKid == pPages) { |
| 286 | m_pTreeTraversal[level].second++; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 287 | continue; |
npm | ec64cee | 2016-11-04 12:54:51 -0700 | [diff] [blame] | 288 | } |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 289 | if (!pKid->KeyExist("Kids")) { |
tsepez | 0fa4717 | 2017-01-09 07:01:36 -0800 | [diff] [blame] | 290 | m_PageList[iPage - (*nPagesToGo) + 1] = pKid->GetObjNum(); |
npm | ec64cee | 2016-11-04 12:54:51 -0700 | [diff] [blame] | 291 | (*nPagesToGo)--; |
| 292 | m_pTreeTraversal[level].second++; |
| 293 | if (*nPagesToGo == 0) { |
| 294 | page = pKid; |
| 295 | break; |
| 296 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 297 | } else { |
npm | ec64cee | 2016-11-04 12:54:51 -0700 | [diff] [blame] | 298 | // If the vector has size level+1, the child is not in yet |
| 299 | if (m_pTreeTraversal.size() == level + 1) |
| 300 | m_pTreeTraversal.push_back(std::make_pair(pKid, 0)); |
| 301 | // Now m_pTreeTraversal[level+1] should exist and be equal to pKid. |
| 302 | CPDF_Dictionary* pageKid = TraversePDFPages(iPage, nPagesToGo, level + 1); |
| 303 | // Check if child was completely processed, i.e. it popped itself out |
| 304 | if (m_pTreeTraversal.size() == level + 1) |
| 305 | m_pTreeTraversal[level].second++; |
Nicolas Pena | 83c5eac | 2017-01-03 14:33:20 -0500 | [diff] [blame] | 306 | // If child did not finish, no pages to go, or max level reached, end |
| 307 | if (m_pTreeTraversal.size() != level + 1 || *nPagesToGo == 0 || |
| 308 | m_bReachedMaxPageLevel) { |
npm | ec64cee | 2016-11-04 12:54:51 -0700 | [diff] [blame] | 309 | page = pageKid; |
| 310 | break; |
| 311 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 312 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 313 | } |
npm | ec64cee | 2016-11-04 12:54:51 -0700 | [diff] [blame] | 314 | if (m_pTreeTraversal[level].second == pKidList->GetCount()) |
| 315 | m_pTreeTraversal.pop_back(); |
| 316 | return page; |
| 317 | } |
| 318 | |
| 319 | void CPDF_Document::ResetTraversal() { |
| 320 | m_iNextPageToTraverse = 0; |
Nicolas Pena | 83c5eac | 2017-01-03 14:33:20 -0500 | [diff] [blame] | 321 | m_bReachedMaxPageLevel = false; |
npm | ec64cee | 2016-11-04 12:54:51 -0700 | [diff] [blame] | 322 | m_pTreeTraversal.clear(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 323 | } |
Lei Zhang | 6de7f2f | 2015-09-03 14:51:53 -0700 | [diff] [blame] | 324 | |
npm | 01b67ed | 2016-09-12 13:38:52 -0700 | [diff] [blame] | 325 | CPDF_Dictionary* CPDF_Document::GetPagesDict() const { |
Lei Zhang | 0158106 | 2017-08-30 14:19:26 -0700 | [diff] [blame] | 326 | const CPDF_Dictionary* pRoot = GetRoot(); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 327 | return pRoot ? pRoot->GetDictFor("Pages") : nullptr; |
npm | 01b67ed | 2016-09-12 13:38:52 -0700 | [diff] [blame] | 328 | } |
| 329 | |
dsinclair | 23e173a | 2016-09-20 12:35:36 -0700 | [diff] [blame] | 330 | bool CPDF_Document::IsPageLoaded(int iPage) const { |
tsepez | 0fa4717 | 2017-01-09 07:01:36 -0800 | [diff] [blame] | 331 | return !!m_PageList[iPage]; |
dsinclair | 23e173a | 2016-09-20 12:35:36 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Tom Sepez | 967aa07 | 2018-05-08 13:40:20 +0000 | [diff] [blame^] | 334 | CPDF_Dictionary* CPDF_Document::GetPageDictionary(int iPage) { |
Tom Sepez | 193e6ca | 2017-03-14 15:53:36 -0700 | [diff] [blame] | 335 | if (!pdfium::IndexInBounds(m_PageList, iPage)) |
Lei Zhang | 6de7f2f | 2015-09-03 14:51:53 -0700 | [diff] [blame] | 336 | return nullptr; |
| 337 | |
Tom Sepez | 193e6ca | 2017-03-14 15:53:36 -0700 | [diff] [blame] | 338 | if (m_bLinearized && iPage == m_iFirstPageNo) { |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 339 | if (CPDF_Dictionary* pDict = |
dsinclair | 03bd7c7 | 2016-08-23 20:13:41 -0700 | [diff] [blame] | 340 | ToDictionary(GetOrParseIndirectObject(m_dwFirstPageObjNum))) { |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 341 | return pDict; |
Tom Sepez | 0f68785 | 2016-01-25 13:37:42 -0800 | [diff] [blame] | 342 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 343 | } |
tsepez | 0fa4717 | 2017-01-09 07:01:36 -0800 | [diff] [blame] | 344 | uint32_t objnum = m_PageList[iPage]; |
| 345 | if (objnum) |
| 346 | return ToDictionary(GetOrParseIndirectObject(objnum)); |
npm | 900f421 | 2016-10-28 14:30:44 -0700 | [diff] [blame] | 347 | |
| 348 | CPDF_Dictionary* pPages = GetPagesDict(); |
| 349 | if (!pPages) |
| 350 | return nullptr; |
| 351 | |
Nicolas Pena | c467d46 | 2017-03-15 16:21:02 -0400 | [diff] [blame] | 352 | if (m_pTreeTraversal.empty()) { |
| 353 | ResetTraversal(); |
npm | ec64cee | 2016-11-04 12:54:51 -0700 | [diff] [blame] | 354 | m_pTreeTraversal.push_back(std::make_pair(pPages, 0)); |
Nicolas Pena | c467d46 | 2017-03-15 16:21:02 -0400 | [diff] [blame] | 355 | } |
| 356 | int nPagesToGo = iPage - m_iNextPageToTraverse + 1; |
npm | ec64cee | 2016-11-04 12:54:51 -0700 | [diff] [blame] | 357 | CPDF_Dictionary* pPage = TraversePDFPages(iPage, &nPagesToGo, 0); |
| 358 | m_iNextPageToTraverse = iPage + 1; |
npm | 900f421 | 2016-10-28 14:30:44 -0700 | [diff] [blame] | 359 | return pPage; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 360 | } |
Lei Zhang | 6de7f2f | 2015-09-03 14:51:53 -0700 | [diff] [blame] | 361 | |
dsinclair | 23e173a | 2016-09-20 12:35:36 -0700 | [diff] [blame] | 362 | void CPDF_Document::SetPageObjNum(int iPage, uint32_t objNum) { |
tsepez | 0fa4717 | 2017-01-09 07:01:36 -0800 | [diff] [blame] | 363 | m_PageList[iPage] = objNum; |
dsinclair | 23e173a | 2016-09-20 12:35:36 -0700 | [diff] [blame] | 364 | } |
| 365 | |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 366 | int CPDF_Document::FindPageIndex(CPDF_Dictionary* pNode, |
tsepez | e507dc5 | 2017-01-18 10:24:35 -0800 | [diff] [blame] | 367 | uint32_t* skip_count, |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 368 | uint32_t objnum, |
tsepez | e507dc5 | 2017-01-18 10:24:35 -0800 | [diff] [blame] | 369 | int* index, |
Lei Zhang | 0158106 | 2017-08-30 14:19:26 -0700 | [diff] [blame] | 370 | int level) const { |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 371 | if (!pNode->KeyExist("Kids")) { |
| 372 | if (objnum == pNode->GetObjNum()) |
tsepez | e507dc5 | 2017-01-18 10:24:35 -0800 | [diff] [blame] | 373 | return *index; |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 374 | |
tsepez | e507dc5 | 2017-01-18 10:24:35 -0800 | [diff] [blame] | 375 | if (*skip_count) |
| 376 | (*skip_count)--; |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 377 | |
tsepez | e507dc5 | 2017-01-18 10:24:35 -0800 | [diff] [blame] | 378 | (*index)++; |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 379 | return -1; |
| 380 | } |
| 381 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 382 | CPDF_Array* pKidList = pNode->GetArrayFor("Kids"); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 383 | if (!pKidList) |
| 384 | return -1; |
| 385 | |
| 386 | if (level >= FX_MAX_PAGE_LEVEL) |
| 387 | return -1; |
| 388 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 389 | size_t count = pNode->GetIntegerFor("Count"); |
tsepez | e507dc5 | 2017-01-18 10:24:35 -0800 | [diff] [blame] | 390 | if (count <= *skip_count) { |
| 391 | (*skip_count) -= count; |
| 392 | (*index) += count; |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 393 | return -1; |
| 394 | } |
| 395 | |
| 396 | if (count && count == pKidList->GetCount()) { |
| 397 | for (size_t i = 0; i < count; i++) { |
tsepez | e507dc5 | 2017-01-18 10:24:35 -0800 | [diff] [blame] | 398 | CPDF_Reference* pKid = ToReference(pKidList->GetObjectAt(i)); |
| 399 | if (pKid && pKid->GetRefObjNum() == objnum) |
| 400 | return static_cast<int>(*index + i); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 401 | } |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | for (size_t i = 0; i < pKidList->GetCount(); i++) { |
| 405 | CPDF_Dictionary* pKid = pKidList->GetDictAt(i); |
| 406 | if (!pKid || pKid == pNode) |
| 407 | continue; |
| 408 | |
| 409 | int found_index = FindPageIndex(pKid, skip_count, objnum, index, level + 1); |
| 410 | if (found_index >= 0) |
| 411 | return found_index; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 412 | } |
| 413 | return -1; |
| 414 | } |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 415 | |
tsepez | b5e8f14 | 2016-03-25 15:18:35 -0700 | [diff] [blame] | 416 | int CPDF_Document::GetPageIndex(uint32_t objnum) { |
tsepez | 0fa4717 | 2017-01-09 07:01:36 -0800 | [diff] [blame] | 417 | uint32_t nPages = m_PageList.size(); |
tsepez | b5e8f14 | 2016-03-25 15:18:35 -0700 | [diff] [blame] | 418 | uint32_t skip_count = 0; |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 419 | bool bSkipped = false; |
tsepez | b5e8f14 | 2016-03-25 15:18:35 -0700 | [diff] [blame] | 420 | for (uint32_t i = 0; i < nPages; i++) { |
tsepez | 0fa4717 | 2017-01-09 07:01:36 -0800 | [diff] [blame] | 421 | if (m_PageList[i] == objnum) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 422 | return i; |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 423 | |
tsepez | 0fa4717 | 2017-01-09 07:01:36 -0800 | [diff] [blame] | 424 | if (!bSkipped && m_PageList[i] == 0) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 425 | skip_count = i; |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 426 | bSkipped = true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 427 | } |
| 428 | } |
npm | 01b67ed | 2016-09-12 13:38:52 -0700 | [diff] [blame] | 429 | CPDF_Dictionary* pPages = GetPagesDict(); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 430 | if (!pPages) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 431 | return -1; |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 432 | |
tsepez | e507dc5 | 2017-01-18 10:24:35 -0800 | [diff] [blame] | 433 | int start_index = 0; |
| 434 | int found_index = FindPageIndex(pPages, &skip_count, objnum, &start_index); |
| 435 | |
| 436 | // Corrupt page tree may yield out-of-range results. |
Tom Sepez | 193e6ca | 2017-03-14 15:53:36 -0700 | [diff] [blame] | 437 | if (!pdfium::IndexInBounds(m_PageList, found_index)) |
tsepez | e507dc5 | 2017-01-18 10:24:35 -0800 | [diff] [blame] | 438 | return -1; |
| 439 | |
| 440 | m_PageList[found_index] = objnum; |
| 441 | return found_index; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 442 | } |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 443 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 444 | int CPDF_Document::GetPageCount() const { |
tsepez | 0fa4717 | 2017-01-09 07:01:36 -0800 | [diff] [blame] | 445 | return pdfium::CollectionSize<int>(m_PageList); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 446 | } |
Wei Li | 215816b | 2016-01-14 12:29:02 -0800 | [diff] [blame] | 447 | |
| 448 | int CPDF_Document::RetrievePageCount() const { |
npm | 01b67ed | 2016-09-12 13:38:52 -0700 | [diff] [blame] | 449 | CPDF_Dictionary* pPages = GetPagesDict(); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 450 | if (!pPages) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 451 | return 0; |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 452 | |
| 453 | if (!pPages->KeyExist("Kids")) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 454 | return 1; |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 455 | |
Wei Li | 215816b | 2016-01-14 12:29:02 -0800 | [diff] [blame] | 456 | std::set<CPDF_Dictionary*> visited_pages; |
| 457 | visited_pages.insert(pPages); |
| 458 | return CountPages(pPages, &visited_pages); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 459 | } |
Wei Li | 215816b | 2016-01-14 12:29:02 -0800 | [diff] [blame] | 460 | |
thestig | 27ddf16 | 2016-05-23 15:06:59 -0700 | [diff] [blame] | 461 | uint32_t CPDF_Document::GetUserPermissions() const { |
| 462 | // https://bugs.chromium.org/p/pdfium/issues/detail?id=499 |
| 463 | if (!m_pParser) { |
| 464 | #ifndef PDF_ENABLE_XFA |
| 465 | return 0; |
| 466 | #else // PDF_ENABLE_XFA |
| 467 | return 0xFFFFFFFF; |
| 468 | #endif |
| 469 | } |
| 470 | return m_pParser->GetPermissions(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 471 | } |
Tom Sepez | 02056fa | 2016-01-25 12:15:28 -0800 | [diff] [blame] | 472 | |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 473 | CPDF_Font* CPDF_Document::LoadFont(CPDF_Dictionary* pFontDict) { |
| 474 | ASSERT(pFontDict); |
npm | e883afc | 2016-10-05 13:43:25 -0700 | [diff] [blame] | 475 | return m_pDocPage->GetFont(pFontDict); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 478 | RetainPtr<CPDF_StreamAcc> CPDF_Document::LoadFontFile(CPDF_Stream* pStream) { |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 479 | return m_pDocPage->GetFontFileStreamAcc(pStream); |
| 480 | } |
| 481 | |
| 482 | CPDF_ColorSpace* CPDF_Document::LoadColorSpace(CPDF_Object* pCSObj, |
| 483 | CPDF_Dictionary* pResources) { |
| 484 | return m_pDocPage->GetColorSpace(pCSObj, pResources); |
| 485 | } |
| 486 | |
| 487 | CPDF_Pattern* CPDF_Document::LoadPattern(CPDF_Object* pPatternObj, |
npm | e883afc | 2016-10-05 13:43:25 -0700 | [diff] [blame] | 488 | bool bShading, |
thestig | 12168d7 | 2016-04-26 22:41:36 -0700 | [diff] [blame] | 489 | const CFX_Matrix& matrix) { |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 490 | return m_pDocPage->GetPattern(pPatternObj, bShading, matrix); |
| 491 | } |
| 492 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 493 | RetainPtr<CPDF_IccProfile> CPDF_Document::LoadIccProfile(CPDF_Stream* pStream) { |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 494 | return m_pDocPage->GetIccProfile(pStream); |
| 495 | } |
| 496 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 497 | RetainPtr<CPDF_Image> CPDF_Document::LoadImageFromPageData( |
Tom Sepez | 7d4f6a8 | 2017-03-31 17:10:34 -0700 | [diff] [blame] | 498 | uint32_t dwStreamObjNum) { |
tsepez | 137a344 | 2016-11-14 15:03:55 -0800 | [diff] [blame] | 499 | ASSERT(dwStreamObjNum); |
| 500 | return m_pDocPage->GetImage(dwStreamObjNum); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 501 | } |
| 502 | |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 503 | void CPDF_Document::CreateNewDoc() { |
Lei Zhang | 0158106 | 2017-08-30 14:19:26 -0700 | [diff] [blame] | 504 | ASSERT(!m_pRootDict); |
| 505 | ASSERT(!m_pInfoDict); |
tsepez | 5913a6c | 2016-11-16 17:31:18 -0800 | [diff] [blame] | 506 | m_pRootDict = NewIndirect<CPDF_Dictionary>(); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 507 | m_pRootDict->SetNewFor<CPDF_Name>("Type", "Catalog"); |
tsepez | 698c571 | 2016-09-28 16:47:07 -0700 | [diff] [blame] | 508 | |
tsepez | 5913a6c | 2016-11-16 17:31:18 -0800 | [diff] [blame] | 509 | CPDF_Dictionary* pPages = NewIndirect<CPDF_Dictionary>(); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 510 | pPages->SetNewFor<CPDF_Name>("Type", "Pages"); |
| 511 | pPages->SetNewFor<CPDF_Number>("Count", 0); |
| 512 | pPages->SetNewFor<CPDF_Array>("Kids"); |
| 513 | m_pRootDict->SetNewFor<CPDF_Reference>("Pages", this, pPages->GetObjNum()); |
tsepez | 5913a6c | 2016-11-16 17:31:18 -0800 | [diff] [blame] | 514 | m_pInfoDict = NewIndirect<CPDF_Dictionary>(); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | CPDF_Dictionary* CPDF_Document::CreateNewPage(int iPage) { |
tsepez | 5913a6c | 2016-11-16 17:31:18 -0800 | [diff] [blame] | 518 | CPDF_Dictionary* pDict = NewIndirect<CPDF_Dictionary>(); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 519 | pDict->SetNewFor<CPDF_Name>("Type", "Page"); |
tsepez | 70c4afd | 2016-11-15 11:33:44 -0800 | [diff] [blame] | 520 | uint32_t dwObjNum = pDict->GetObjNum(); |
npm | 20ef5b9 | 2016-11-03 11:47:43 -0700 | [diff] [blame] | 521 | if (!InsertNewPage(iPage, pDict)) { |
tsepez | 33fdebc | 2016-11-04 11:38:40 -0700 | [diff] [blame] | 522 | DeleteIndirectObject(dwObjNum); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 523 | return nullptr; |
| 524 | } |
| 525 | return pDict; |
| 526 | } |
| 527 | |
npm | 20ef5b9 | 2016-11-03 11:47:43 -0700 | [diff] [blame] | 528 | bool CPDF_Document::InsertDeletePDFPage(CPDF_Dictionary* pPages, |
| 529 | int nPagesToGo, |
| 530 | CPDF_Dictionary* pPageDict, |
| 531 | bool bInsert, |
| 532 | std::set<CPDF_Dictionary*>* pVisited) { |
| 533 | CPDF_Array* pKidList = pPages->GetArrayFor("Kids"); |
| 534 | if (!pKidList) |
| 535 | return false; |
| 536 | |
| 537 | for (size_t i = 0; i < pKidList->GetCount(); i++) { |
| 538 | CPDF_Dictionary* pKid = pKidList->GetDictAt(i); |
| 539 | if (pKid->GetStringFor("Type") == "Page") { |
| 540 | if (nPagesToGo != 0) { |
| 541 | nPagesToGo--; |
| 542 | continue; |
| 543 | } |
| 544 | if (bInsert) { |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 545 | pKidList->InsertNewAt<CPDF_Reference>(i, this, pPageDict->GetObjNum()); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 546 | pPageDict->SetNewFor<CPDF_Reference>("Parent", this, |
| 547 | pPages->GetObjNum()); |
npm | 20ef5b9 | 2016-11-03 11:47:43 -0700 | [diff] [blame] | 548 | } else { |
| 549 | pKidList->RemoveAt(i); |
| 550 | } |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 551 | pPages->SetNewFor<CPDF_Number>( |
npm | 20ef5b9 | 2016-11-03 11:47:43 -0700 | [diff] [blame] | 552 | "Count", pPages->GetIntegerFor("Count") + (bInsert ? 1 : -1)); |
npm | ec64cee | 2016-11-04 12:54:51 -0700 | [diff] [blame] | 553 | ResetTraversal(); |
npm | 20ef5b9 | 2016-11-03 11:47:43 -0700 | [diff] [blame] | 554 | break; |
| 555 | } |
| 556 | int nPages = pKid->GetIntegerFor("Count"); |
| 557 | if (nPagesToGo >= nPages) { |
| 558 | nPagesToGo -= nPages; |
| 559 | continue; |
| 560 | } |
| 561 | if (pdfium::ContainsKey(*pVisited, pKid)) |
| 562 | return false; |
| 563 | |
| 564 | pdfium::ScopedSetInsertion<CPDF_Dictionary*> insertion(pVisited, pKid); |
| 565 | if (!InsertDeletePDFPage(pKid, nPagesToGo, pPageDict, bInsert, pVisited)) |
| 566 | return false; |
| 567 | |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 568 | pPages->SetNewFor<CPDF_Number>( |
| 569 | "Count", pPages->GetIntegerFor("Count") + (bInsert ? 1 : -1)); |
npm | 20ef5b9 | 2016-11-03 11:47:43 -0700 | [diff] [blame] | 570 | break; |
| 571 | } |
| 572 | return true; |
| 573 | } |
| 574 | |
| 575 | bool CPDF_Document::InsertNewPage(int iPage, CPDF_Dictionary* pPageDict) { |
Lei Zhang | 0158106 | 2017-08-30 14:19:26 -0700 | [diff] [blame] | 576 | const CPDF_Dictionary* pRoot = GetRoot(); |
npm | 20ef5b9 | 2016-11-03 11:47:43 -0700 | [diff] [blame] | 577 | CPDF_Dictionary* pPages = pRoot ? pRoot->GetDictFor("Pages") : nullptr; |
| 578 | if (!pPages) |
| 579 | return false; |
| 580 | |
| 581 | int nPages = GetPageCount(); |
| 582 | if (iPage < 0 || iPage > nPages) |
| 583 | return false; |
| 584 | |
| 585 | if (iPage == nPages) { |
| 586 | CPDF_Array* pPagesList = pPages->GetArrayFor("Kids"); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 587 | if (!pPagesList) |
| 588 | pPagesList = pPages->SetNewFor<CPDF_Array>("Kids"); |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 589 | pPagesList->AddNew<CPDF_Reference>(this, pPageDict->GetObjNum()); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 590 | pPages->SetNewFor<CPDF_Number>("Count", nPages + 1); |
| 591 | pPageDict->SetNewFor<CPDF_Reference>("Parent", this, pPages->GetObjNum()); |
npm | ec64cee | 2016-11-04 12:54:51 -0700 | [diff] [blame] | 592 | ResetTraversal(); |
npm | 20ef5b9 | 2016-11-03 11:47:43 -0700 | [diff] [blame] | 593 | } else { |
| 594 | std::set<CPDF_Dictionary*> stack = {pPages}; |
| 595 | if (!InsertDeletePDFPage(pPages, iPage, pPageDict, true, &stack)) |
| 596 | return false; |
| 597 | } |
tsepez | 0fa4717 | 2017-01-09 07:01:36 -0800 | [diff] [blame] | 598 | m_PageList.insert(m_PageList.begin() + iPage, pPageDict->GetObjNum()); |
npm | 20ef5b9 | 2016-11-03 11:47:43 -0700 | [diff] [blame] | 599 | return true; |
| 600 | } |
| 601 | |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 602 | void CPDF_Document::DeletePage(int iPage) { |
npm | 01b67ed | 2016-09-12 13:38:52 -0700 | [diff] [blame] | 603 | CPDF_Dictionary* pPages = GetPagesDict(); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 604 | if (!pPages) |
| 605 | return; |
| 606 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 607 | int nPages = pPages->GetIntegerFor("Count"); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 608 | if (iPage < 0 || iPage >= nPages) |
| 609 | return; |
| 610 | |
| 611 | std::set<CPDF_Dictionary*> stack = {pPages}; |
npm | 20ef5b9 | 2016-11-03 11:47:43 -0700 | [diff] [blame] | 612 | if (!InsertDeletePDFPage(pPages, iPage, nullptr, false, &stack)) |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 613 | return; |
| 614 | |
tsepez | 0fa4717 | 2017-01-09 07:01:36 -0800 | [diff] [blame] | 615 | m_PageList.erase(m_PageList.begin() + iPage); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 616 | } |
| 617 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 618 | CPDF_Font* CPDF_Document::AddStandardFont(const char* font, |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 619 | CPDF_FontEncoding* pEncoding) { |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 620 | ByteString name(font); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 621 | if (PDF_GetStandardFontName(&name) < 0) |
| 622 | return nullptr; |
tsepez | 71a452f | 2016-05-13 17:51:27 -0700 | [diff] [blame] | 623 | return GetPageData()->GetStandardFont(name, pEncoding); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 624 | } |
| 625 | |
npm | 01b67ed | 2016-09-12 13:38:52 -0700 | [diff] [blame] | 626 | size_t CPDF_Document::CalculateEncodingDict(int charset, |
| 627 | CPDF_Dictionary* pBaseDict) { |
| 628 | size_t i; |
| 629 | for (i = 0; i < FX_ArraySize(g_FX_CharsetUnicodes); ++i) { |
| 630 | if (g_FX_CharsetUnicodes[i].m_Charset == charset) |
| 631 | break; |
| 632 | } |
| 633 | if (i == FX_ArraySize(g_FX_CharsetUnicodes)) |
| 634 | return i; |
tsepez | 70c4afd | 2016-11-15 11:33:44 -0800 | [diff] [blame] | 635 | |
tsepez | 5913a6c | 2016-11-16 17:31:18 -0800 | [diff] [blame] | 636 | CPDF_Dictionary* pEncodingDict = NewIndirect<CPDF_Dictionary>(); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 637 | pEncodingDict->SetNewFor<CPDF_Name>("BaseEncoding", "WinAnsiEncoding"); |
tsepez | 70c4afd | 2016-11-15 11:33:44 -0800 | [diff] [blame] | 638 | |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 639 | CPDF_Array* pArray = pEncodingDict->SetNewFor<CPDF_Array>("Differences"); |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 640 | pArray->AddNew<CPDF_Number>(128); |
tsepez | 70c4afd | 2016-11-15 11:33:44 -0800 | [diff] [blame] | 641 | |
npm | 01b67ed | 2016-09-12 13:38:52 -0700 | [diff] [blame] | 642 | const uint16_t* pUnicodes = g_FX_CharsetUnicodes[i].m_pUnicodes; |
| 643 | for (int j = 0; j < 128; j++) { |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 644 | ByteString name = PDF_AdobeNameFromUnicode(pUnicodes[j]); |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 645 | pArray->AddNew<CPDF_Name>(name.IsEmpty() ? ".notdef" : name); |
npm | 01b67ed | 2016-09-12 13:38:52 -0700 | [diff] [blame] | 646 | } |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 647 | pBaseDict->SetNewFor<CPDF_Reference>("Encoding", this, |
| 648 | pEncodingDict->GetObjNum()); |
npm | 01b67ed | 2016-09-12 13:38:52 -0700 | [diff] [blame] | 649 | return i; |
| 650 | } |
| 651 | |
npm | e9988dd | 2016-09-16 13:57:22 -0700 | [diff] [blame] | 652 | CPDF_Dictionary* CPDF_Document::ProcessbCJK( |
| 653 | CPDF_Dictionary* pBaseDict, |
| 654 | int charset, |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 655 | bool bVert, |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 656 | ByteString basefont, |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 657 | std::function<void(wchar_t, wchar_t, CPDF_Array*)> Insert) { |
tsepez | 5913a6c | 2016-11-16 17:31:18 -0800 | [diff] [blame] | 658 | CPDF_Dictionary* pFontDict = NewIndirect<CPDF_Dictionary>(); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 659 | ByteString cmap; |
| 660 | ByteString ordering; |
npm | e9988dd | 2016-09-16 13:57:22 -0700 | [diff] [blame] | 661 | int supplement = 0; |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 662 | CPDF_Array* pWidthArray = pFontDict->SetNewFor<CPDF_Array>("W"); |
npm | e9988dd | 2016-09-16 13:57:22 -0700 | [diff] [blame] | 663 | switch (charset) { |
Dan Sinclair | f51a02a | 2017-04-19 12:46:53 -0400 | [diff] [blame] | 664 | case FX_CHARSET_ChineseTraditional: |
npm | e9988dd | 2016-09-16 13:57:22 -0700 | [diff] [blame] | 665 | cmap = bVert ? "ETenms-B5-V" : "ETenms-B5-H"; |
| 666 | ordering = "CNS1"; |
| 667 | supplement = 4; |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 668 | pWidthArray->AddNew<CPDF_Number>(1); |
npm | e9988dd | 2016-09-16 13:57:22 -0700 | [diff] [blame] | 669 | Insert(0x20, 0x7e, pWidthArray); |
| 670 | break; |
Dan Sinclair | f51a02a | 2017-04-19 12:46:53 -0400 | [diff] [blame] | 671 | case FX_CHARSET_ChineseSimplified: |
npm | e9988dd | 2016-09-16 13:57:22 -0700 | [diff] [blame] | 672 | cmap = bVert ? "GBK-EUC-V" : "GBK-EUC-H"; |
| 673 | ordering = "GB1"; |
| 674 | supplement = 2; |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 675 | pWidthArray->AddNew<CPDF_Number>(7716); |
npm | e9988dd | 2016-09-16 13:57:22 -0700 | [diff] [blame] | 676 | Insert(0x20, 0x20, pWidthArray); |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 677 | pWidthArray->AddNew<CPDF_Number>(814); |
npm | e9988dd | 2016-09-16 13:57:22 -0700 | [diff] [blame] | 678 | Insert(0x21, 0x7e, pWidthArray); |
| 679 | break; |
Dan Sinclair | f51a02a | 2017-04-19 12:46:53 -0400 | [diff] [blame] | 680 | case FX_CHARSET_Hangul: |
npm | e9988dd | 2016-09-16 13:57:22 -0700 | [diff] [blame] | 681 | cmap = bVert ? "KSCms-UHC-V" : "KSCms-UHC-H"; |
| 682 | ordering = "Korea1"; |
| 683 | supplement = 2; |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 684 | pWidthArray->AddNew<CPDF_Number>(1); |
npm | e9988dd | 2016-09-16 13:57:22 -0700 | [diff] [blame] | 685 | Insert(0x20, 0x7e, pWidthArray); |
| 686 | break; |
Dan Sinclair | f51a02a | 2017-04-19 12:46:53 -0400 | [diff] [blame] | 687 | case FX_CHARSET_ShiftJIS: |
npm | e9988dd | 2016-09-16 13:57:22 -0700 | [diff] [blame] | 688 | cmap = bVert ? "90ms-RKSJ-V" : "90ms-RKSJ-H"; |
| 689 | ordering = "Japan1"; |
| 690 | supplement = 5; |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 691 | pWidthArray->AddNew<CPDF_Number>(231); |
npm | e9988dd | 2016-09-16 13:57:22 -0700 | [diff] [blame] | 692 | Insert(0x20, 0x7d, pWidthArray); |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 693 | pWidthArray->AddNew<CPDF_Number>(326); |
npm | e9988dd | 2016-09-16 13:57:22 -0700 | [diff] [blame] | 694 | Insert(0xa0, 0xa0, pWidthArray); |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 695 | pWidthArray->AddNew<CPDF_Number>(327); |
npm | e9988dd | 2016-09-16 13:57:22 -0700 | [diff] [blame] | 696 | Insert(0xa1, 0xdf, pWidthArray); |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 697 | pWidthArray->AddNew<CPDF_Number>(631); |
npm | e9988dd | 2016-09-16 13:57:22 -0700 | [diff] [blame] | 698 | Insert(0x7e, 0x7e, pWidthArray); |
| 699 | break; |
| 700 | } |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 701 | pBaseDict->SetNewFor<CPDF_Name>("Subtype", "Type0"); |
| 702 | pBaseDict->SetNewFor<CPDF_Name>("BaseFont", basefont); |
| 703 | pBaseDict->SetNewFor<CPDF_Name>("Encoding", cmap); |
| 704 | pFontDict->SetNewFor<CPDF_Name>("Type", "Font"); |
| 705 | pFontDict->SetNewFor<CPDF_Name>("Subtype", "CIDFontType2"); |
| 706 | pFontDict->SetNewFor<CPDF_Name>("BaseFont", basefont); |
| 707 | |
| 708 | CPDF_Dictionary* pCIDSysInfo = |
| 709 | pFontDict->SetNewFor<CPDF_Dictionary>("CIDSystemInfo"); |
| 710 | pCIDSysInfo->SetNewFor<CPDF_String>("Registry", "Adobe", false); |
| 711 | pCIDSysInfo->SetNewFor<CPDF_String>("Ordering", ordering, false); |
| 712 | pCIDSysInfo->SetNewFor<CPDF_Number>("Supplement", supplement); |
| 713 | |
| 714 | CPDF_Array* pArray = pBaseDict->SetNewFor<CPDF_Array>("DescendantFonts"); |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 715 | pArray->AddNew<CPDF_Reference>(this, pFontDict->GetObjNum()); |
npm | e9988dd | 2016-09-16 13:57:22 -0700 | [diff] [blame] | 716 | return pFontDict; |
| 717 | } |
| 718 | |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 719 | CPDF_Font* CPDF_Document::AddFont(CFX_Font* pFont, int charset, bool bVert) { |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 720 | if (!pFont) |
| 721 | return nullptr; |
| 722 | |
Dan Sinclair | f51a02a | 2017-04-19 12:46:53 -0400 | [diff] [blame] | 723 | bool bCJK = charset == FX_CHARSET_ChineseTraditional || |
| 724 | charset == FX_CHARSET_ChineseSimplified || |
| 725 | charset == FX_CHARSET_Hangul || charset == FX_CHARSET_ShiftJIS; |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 726 | ByteString basefont = pFont->GetFamilyName(); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 727 | basefont.Replace(" ", ""); |
npm | 01b67ed | 2016-09-12 13:38:52 -0700 | [diff] [blame] | 728 | int flags = |
| 729 | CalculateFlags(pFont->IsBold(), pFont->IsItalic(), pFont->IsFixedWidth(), |
Dan Sinclair | f51a02a | 2017-04-19 12:46:53 -0400 | [diff] [blame] | 730 | false, false, charset == FX_CHARSET_Symbol); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 731 | |
tsepez | 5913a6c | 2016-11-16 17:31:18 -0800 | [diff] [blame] | 732 | CPDF_Dictionary* pBaseDict = NewIndirect<CPDF_Dictionary>(); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 733 | pBaseDict->SetNewFor<CPDF_Name>("Type", "Font"); |
Tom Sepez | 827db14 | 2017-04-26 13:50:33 -0700 | [diff] [blame] | 734 | auto pEncoding = pdfium::MakeUnique<CFX_UnicodeEncoding>(pFont); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 735 | CPDF_Dictionary* pFontDict = pBaseDict; |
| 736 | if (!bCJK) { |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 737 | auto pWidths = pdfium::MakeUnique<CPDF_Array>(); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 738 | for (int charcode = 32; charcode < 128; charcode++) { |
| 739 | int glyph_index = pEncoding->GlyphFromCharCode(charcode); |
| 740 | int char_width = pFont->GetGlyphWidth(glyph_index); |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 741 | pWidths->AddNew<CPDF_Number>(char_width); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 742 | } |
Dan Sinclair | f51a02a | 2017-04-19 12:46:53 -0400 | [diff] [blame] | 743 | if (charset == FX_CHARSET_ANSI || charset == FX_CHARSET_Default || |
| 744 | charset == FX_CHARSET_Symbol) { |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 745 | pBaseDict->SetNewFor<CPDF_Name>("Encoding", "WinAnsiEncoding"); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 746 | for (int charcode = 128; charcode <= 255; charcode++) { |
| 747 | int glyph_index = pEncoding->GlyphFromCharCode(charcode); |
| 748 | int char_width = pFont->GetGlyphWidth(glyph_index); |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 749 | pWidths->AddNew<CPDF_Number>(char_width); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 750 | } |
| 751 | } else { |
npm | 01b67ed | 2016-09-12 13:38:52 -0700 | [diff] [blame] | 752 | size_t i = CalculateEncodingDict(charset, pBaseDict); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 753 | if (i < FX_ArraySize(g_FX_CharsetUnicodes)) { |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 754 | const uint16_t* pUnicodes = g_FX_CharsetUnicodes[i].m_pUnicodes; |
| 755 | for (int j = 0; j < 128; j++) { |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 756 | int glyph_index = pEncoding->GlyphFromCharCode(pUnicodes[j]); |
| 757 | int char_width = pFont->GetGlyphWidth(glyph_index); |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 758 | pWidths->AddNew<CPDF_Number>(char_width); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 759 | } |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 760 | } |
| 761 | } |
npm | 01b67ed | 2016-09-12 13:38:52 -0700 | [diff] [blame] | 762 | ProcessNonbCJK(pBaseDict, pFont->IsBold(), pFont->IsItalic(), basefont, |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 763 | std::move(pWidths)); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 764 | } else { |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 765 | pFontDict = ProcessbCJK( |
| 766 | pBaseDict, charset, bVert, basefont, |
| 767 | [pFont, &pEncoding](wchar_t start, wchar_t end, CPDF_Array* widthArr) { |
| 768 | InsertWidthArray1(pFont, pEncoding.get(), start, end, widthArr); |
| 769 | }); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 770 | } |
npm | e9988dd | 2016-09-16 13:57:22 -0700 | [diff] [blame] | 771 | int italicangle = |
| 772 | pFont->GetSubstFont() ? pFont->GetSubstFont()->m_ItalicAngle : 0; |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 773 | FX_RECT bbox; |
Lei Zhang | 1c23a6d | 2018-04-12 15:37:39 +0000 | [diff] [blame] | 774 | pFont->GetBBox(&bbox); |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 775 | auto pBBox = pdfium::MakeUnique<CPDF_Array>(); |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 776 | pBBox->AddNew<CPDF_Number>(bbox.left); |
| 777 | pBBox->AddNew<CPDF_Number>(bbox.bottom); |
| 778 | pBBox->AddNew<CPDF_Number>(bbox.right); |
| 779 | pBBox->AddNew<CPDF_Number>(bbox.top); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 780 | int32_t nStemV = 0; |
| 781 | if (pFont->GetSubstFont()) { |
| 782 | nStemV = pFont->GetSubstFont()->m_Weight / 5; |
| 783 | } else { |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 784 | static const char stem_chars[] = {'i', 'I', '!', '1'}; |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 785 | const size_t count = FX_ArraySize(stem_chars); |
| 786 | uint32_t glyph = pEncoding->GlyphFromCharCode(stem_chars[0]); |
| 787 | nStemV = pFont->GetGlyphWidth(glyph); |
| 788 | for (size_t i = 1; i < count; i++) { |
| 789 | glyph = pEncoding->GlyphFromCharCode(stem_chars[i]); |
| 790 | int width = pFont->GetGlyphWidth(glyph); |
npm | 01b67ed | 2016-09-12 13:38:52 -0700 | [diff] [blame] | 791 | if (width > 0 && width < nStemV) |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 792 | nStemV = width; |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 793 | } |
| 794 | } |
tsepez | 70c4afd | 2016-11-15 11:33:44 -0800 | [diff] [blame] | 795 | CPDF_Dictionary* pFontDesc = ToDictionary(AddIndirectObject( |
tsepez | 698c571 | 2016-09-28 16:47:07 -0700 | [diff] [blame] | 796 | CalculateFontDesc(this, basefont, flags, italicangle, pFont->GetAscent(), |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 797 | pFont->GetDescent(), std::move(pBBox), nStemV))); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 798 | pFontDict->SetNewFor<CPDF_Reference>("FontDescriptor", this, |
| 799 | pFontDesc->GetObjNum()); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 800 | return LoadFont(pBaseDict); |
| 801 | } |
| 802 | |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 803 | #if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 804 | CPDF_Font* CPDF_Document::AddWindowsFont(LOGFONTW* pLogFont, |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 805 | bool bVert, |
| 806 | bool bTranslateName) { |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 807 | LOGFONTA lfa; |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 808 | memcpy(&lfa, pLogFont, (char*)lfa.lfFaceName - (char*)&lfa); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 809 | ByteString face = ByteString::FromUnicode(pLogFont->lfFaceName); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 810 | if (face.GetLength() >= LF_FACESIZE) |
| 811 | return nullptr; |
| 812 | |
Dan Sinclair | 7e7c649 | 2017-04-03 14:50:05 -0400 | [diff] [blame] | 813 | strncpy(lfa.lfFaceName, face.c_str(), |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 814 | (face.GetLength() + 1) * sizeof(ByteString::CharType)); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 815 | return AddWindowsFont(&lfa, bVert, bTranslateName); |
| 816 | } |
| 817 | |
| 818 | CPDF_Font* CPDF_Document::AddWindowsFont(LOGFONTA* pLogFont, |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 819 | bool bVert, |
| 820 | bool bTranslateName) { |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 821 | pLogFont->lfHeight = -1000; |
| 822 | pLogFont->lfWidth = 0; |
| 823 | HGDIOBJ hFont = CreateFontIndirectA(pLogFont); |
| 824 | HDC hDC = CreateCompatibleDC(nullptr); |
| 825 | hFont = SelectObject(hDC, hFont); |
| 826 | int tm_size = GetOutlineTextMetrics(hDC, 0, nullptr); |
| 827 | if (tm_size == 0) { |
| 828 | hFont = SelectObject(hDC, hFont); |
| 829 | DeleteObject(hFont); |
| 830 | DeleteDC(hDC); |
| 831 | return nullptr; |
| 832 | } |
| 833 | |
| 834 | LPBYTE tm_buf = FX_Alloc(BYTE, tm_size); |
npm | 01b67ed | 2016-09-12 13:38:52 -0700 | [diff] [blame] | 835 | OUTLINETEXTMETRIC* ptm = reinterpret_cast<OUTLINETEXTMETRIC*>(tm_buf); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 836 | GetOutlineTextMetrics(hDC, tm_size, ptm); |
npm | 01b67ed | 2016-09-12 13:38:52 -0700 | [diff] [blame] | 837 | int flags = CalculateFlags(false, pLogFont->lfItalic != 0, |
| 838 | (pLogFont->lfPitchAndFamily & 3) == FIXED_PITCH, |
| 839 | (pLogFont->lfPitchAndFamily & 0xf8) == FF_ROMAN, |
| 840 | (pLogFont->lfPitchAndFamily & 0xf8) == FF_SCRIPT, |
Dan Sinclair | f51a02a | 2017-04-19 12:46:53 -0400 | [diff] [blame] | 841 | pLogFont->lfCharSet == FX_CHARSET_Symbol); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 842 | |
Dan Sinclair | f51a02a | 2017-04-19 12:46:53 -0400 | [diff] [blame] | 843 | bool bCJK = pLogFont->lfCharSet == FX_CHARSET_ChineseTraditional || |
| 844 | pLogFont->lfCharSet == FX_CHARSET_ChineseSimplified || |
| 845 | pLogFont->lfCharSet == FX_CHARSET_Hangul || |
| 846 | pLogFont->lfCharSet == FX_CHARSET_ShiftJIS; |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 847 | ByteString basefont; |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 848 | if (bTranslateName && bCJK) |
| 849 | basefont = FPDF_GetPSNameFromTT(hDC); |
| 850 | |
| 851 | if (basefont.IsEmpty()) |
| 852 | basefont = pLogFont->lfFaceName; |
| 853 | |
npm | 01b67ed | 2016-09-12 13:38:52 -0700 | [diff] [blame] | 854 | int italicangle = ptm->otmItalicAngle / 10; |
| 855 | int ascend = ptm->otmrcFontBox.top; |
| 856 | int descend = ptm->otmrcFontBox.bottom; |
| 857 | int capheight = ptm->otmsCapEmHeight; |
| 858 | int bbox[4] = {ptm->otmrcFontBox.left, ptm->otmrcFontBox.bottom, |
| 859 | ptm->otmrcFontBox.right, ptm->otmrcFontBox.top}; |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 860 | FX_Free(tm_buf); |
| 861 | basefont.Replace(" ", ""); |
tsepez | 5913a6c | 2016-11-16 17:31:18 -0800 | [diff] [blame] | 862 | CPDF_Dictionary* pBaseDict = NewIndirect<CPDF_Dictionary>(); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 863 | pBaseDict->SetNewFor<CPDF_Name>("Type", "Font"); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 864 | CPDF_Dictionary* pFontDict = pBaseDict; |
| 865 | if (!bCJK) { |
Dan Sinclair | f51a02a | 2017-04-19 12:46:53 -0400 | [diff] [blame] | 866 | if (pLogFont->lfCharSet == FX_CHARSET_ANSI || |
| 867 | pLogFont->lfCharSet == FX_CHARSET_Default || |
| 868 | pLogFont->lfCharSet == FX_CHARSET_Symbol) { |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 869 | pBaseDict->SetNewFor<CPDF_Name>("Encoding", "WinAnsiEncoding"); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 870 | } else { |
npm | 01b67ed | 2016-09-12 13:38:52 -0700 | [diff] [blame] | 871 | CalculateEncodingDict(pLogFont->lfCharSet, pBaseDict); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 872 | } |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 873 | int char_widths[224]; |
| 874 | GetCharWidth(hDC, 32, 255, char_widths); |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 875 | auto pWidths = pdfium::MakeUnique<CPDF_Array>(); |
weili | 3cc01f2 | 2016-05-16 13:53:42 -0700 | [diff] [blame] | 876 | for (size_t i = 0; i < 224; i++) |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 877 | pWidths->AddNew<CPDF_Number>(char_widths[i]); |
npm | 01b67ed | 2016-09-12 13:38:52 -0700 | [diff] [blame] | 878 | ProcessNonbCJK(pBaseDict, pLogFont->lfWeight > FW_MEDIUM, |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 879 | pLogFont->lfItalic != 0, basefont, std::move(pWidths)); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 880 | } else { |
npm | e9988dd | 2016-09-16 13:57:22 -0700 | [diff] [blame] | 881 | pFontDict = |
| 882 | ProcessbCJK(pBaseDict, pLogFont->lfCharSet, bVert, basefont, |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 883 | [&hDC](wchar_t start, wchar_t end, CPDF_Array* widthArr) { |
npm | e9988dd | 2016-09-16 13:57:22 -0700 | [diff] [blame] | 884 | InsertWidthArray(hDC, start, end, widthArr); |
| 885 | }); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 886 | } |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 887 | auto pBBox = pdfium::MakeUnique<CPDF_Array>(); |
npm | 01b67ed | 2016-09-12 13:38:52 -0700 | [diff] [blame] | 888 | for (int i = 0; i < 4; i++) |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 889 | pBBox->AddNew<CPDF_Number>(bbox[i]); |
tsepez | 70c4afd | 2016-11-15 11:33:44 -0800 | [diff] [blame] | 890 | std::unique_ptr<CPDF_Dictionary> pFontDesc = |
tsepez | 698c571 | 2016-09-28 16:47:07 -0700 | [diff] [blame] | 891 | CalculateFontDesc(this, basefont, flags, italicangle, ascend, descend, |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 892 | std::move(pBBox), pLogFont->lfWeight / 5); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 893 | pFontDesc->SetNewFor<CPDF_Number>("CapHeight", capheight); |
| 894 | pFontDict->SetNewFor<CPDF_Reference>( |
| 895 | "FontDescriptor", this, |
| 896 | AddIndirectObject(std::move(pFontDesc))->GetObjNum()); |
thestig | 931bf37 | 2016-04-26 22:24:30 -0700 | [diff] [blame] | 897 | hFont = SelectObject(hDC, hFont); |
| 898 | DeleteObject(hFont); |
| 899 | DeleteDC(hDC); |
| 900 | return LoadFont(pBaseDict); |
| 901 | } |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 902 | #endif // _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |