Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #include "core/fpdfapi/parser/cpdf_document.h" | 7 #include "core/fpdfapi/parser/cpdf_document.h" |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <set> | 10 #include <set> |
| (...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 233 int size = end - start + 1; | 233 int size = end - start + 1; |
| 234 int* widths = FX_Alloc(int, size); | 234 int* widths = FX_Alloc(int, size); |
| 235 int i; | 235 int i; |
| 236 for (i = 0; i < size; i++) { | 236 for (i = 0; i < size; i++) { |
| 237 int glyph_index = pEncoding->GlyphFromCharCode(start + i); | 237 int glyph_index = pEncoding->GlyphFromCharCode(start + i); |
| 238 widths[i] = pFont->GetGlyphWidth(glyph_index); | 238 widths[i] = pFont->GetGlyphWidth(glyph_index); |
| 239 } | 239 } |
| 240 InsertWidthArrayImpl(widths, size, pWidthArray); | 240 InsertWidthArrayImpl(widths, size, pWidthArray); |
| 241 } | 241 } |
| 242 | 242 |
| 243 int InsertDeletePDFPage(CPDF_Document* pDoc, | |
| 244 CPDF_Dictionary* pPages, | |
| 245 int nPagesToGo, | |
| 246 CPDF_Dictionary* pPage, | |
| 247 bool bInsert, | |
| 248 std::set<CPDF_Dictionary*>* pVisited) { | |
| 249 CPDF_Array* pKidList = pPages->GetArrayFor("Kids"); | |
| 250 if (!pKidList) | |
| 251 return -1; | |
| 252 | |
| 253 for (size_t i = 0; i < pKidList->GetCount(); i++) { | |
| 254 CPDF_Dictionary* pKid = pKidList->GetDictAt(i); | |
| 255 if (pKid->GetStringFor("Type") == "Page") { | |
| 256 if (nPagesToGo == 0) { | |
| 257 if (bInsert) { | |
| 258 pKidList->InsertAt(i, new CPDF_Reference(pDoc, pPage->GetObjNum())); | |
| 259 pPage->SetReferenceFor("Parent", pDoc, pPages->GetObjNum()); | |
| 260 } else { | |
| 261 pKidList->RemoveAt(i); | |
| 262 } | |
| 263 pPages->SetIntegerFor( | |
| 264 "Count", pPages->GetIntegerFor("Count") + (bInsert ? 1 : -1)); | |
| 265 return 1; | |
| 266 } | |
| 267 nPagesToGo--; | |
| 268 } else { | |
| 269 int nPages = pKid->GetIntegerFor("Count"); | |
| 270 if (nPagesToGo < nPages) { | |
| 271 if (pdfium::ContainsKey(*pVisited, pKid)) | |
| 272 return -1; | |
| 273 | |
| 274 pdfium::ScopedSetInsertion<CPDF_Dictionary*> insertion(pVisited, pKid); | |
| 275 if (InsertDeletePDFPage(pDoc, pKid, nPagesToGo, pPage, bInsert, | |
| 276 pVisited) < 0) { | |
| 277 return -1; | |
| 278 } | |
| 279 pPages->SetIntegerFor( | |
| 280 "Count", pPages->GetIntegerFor("Count") + (bInsert ? 1 : -1)); | |
| 281 return 1; | |
| 282 } | |
| 283 nPagesToGo -= nPages; | |
| 284 } | |
| 285 } | |
| 286 return 0; | |
| 287 } | |
| 288 | |
| 289 int InsertNewPage(CPDF_Document* pDoc, | |
| 290 int iPage, | |
| 291 CPDF_Dictionary* pPageDict, | |
| 292 CFX_ArrayTemplate<uint32_t>& pageList) { | |
| 293 CPDF_Dictionary* pRoot = pDoc->GetRoot(); | |
| 294 CPDF_Dictionary* pPages = pRoot ? pRoot->GetDictFor("Pages") : nullptr; | |
| 295 if (!pPages) | |
| 296 return -1; | |
| 297 | |
| 298 int nPages = pDoc->GetPageCount(); | |
| 299 if (iPage < 0 || iPage > nPages) | |
| 300 return -1; | |
| 301 | |
| 302 if (iPage == nPages) { | |
| 303 CPDF_Array* pPagesList = pPages->GetArrayFor("Kids"); | |
| 304 if (!pPagesList) { | |
| 305 pPagesList = new CPDF_Array; | |
| 306 pPages->SetFor("Kids", pPagesList); | |
| 307 } | |
| 308 pPagesList->Add(new CPDF_Reference(pDoc, pPageDict->GetObjNum())); | |
| 309 pPages->SetIntegerFor("Count", nPages + 1); | |
| 310 pPageDict->SetReferenceFor("Parent", pDoc, pPages->GetObjNum()); | |
| 311 } else { | |
| 312 std::set<CPDF_Dictionary*> stack = {pPages}; | |
| 313 if (InsertDeletePDFPage(pDoc, pPages, iPage, pPageDict, true, &stack) < 0) | |
| 314 return -1; | |
| 315 } | |
| 316 pageList.InsertAt(iPage, pPageDict->GetObjNum()); | |
| 317 return iPage; | |
| 318 } | |
| 319 | |
| 320 int CountPages(CPDF_Dictionary* pPages, | 243 int CountPages(CPDF_Dictionary* pPages, |
| 321 std::set<CPDF_Dictionary*>* visited_pages) { | 244 std::set<CPDF_Dictionary*>* visited_pages) { |
| 322 int count = pPages->GetIntegerFor("Count"); | 245 int count = pPages->GetIntegerFor("Count"); |
| 323 if (count > 0 && count < FPDF_PAGE_MAX_NUM) | 246 if (count > 0 && count < FPDF_PAGE_MAX_NUM) |
| 324 return count; | 247 return count; |
| 325 CPDF_Array* pKidList = pPages->GetArrayFor("Kids"); | 248 CPDF_Array* pKidList = pPages->GetArrayFor("Kids"); |
| 326 if (!pKidList) | 249 if (!pKidList) |
| 327 return 0; | 250 return 0; |
| 328 count = 0; | 251 count = 0; |
| 329 for (size_t i = 0; i < pKidList->GetCount(); i++) { | 252 for (size_t i = 0; i < pKidList->GetCount(); i++) { |
| (...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 703 pPages->SetFor("Kids", new CPDF_Array); | 626 pPages->SetFor("Kids", new CPDF_Array); |
| 704 m_pRootDict->SetReferenceFor("Pages", this, AddIndirectObject(pPages)); | 627 m_pRootDict->SetReferenceFor("Pages", this, AddIndirectObject(pPages)); |
| 705 m_pInfoDict = new CPDF_Dictionary(m_pByteStringPool); | 628 m_pInfoDict = new CPDF_Dictionary(m_pByteStringPool); |
| 706 AddIndirectObject(m_pInfoDict); | 629 AddIndirectObject(m_pInfoDict); |
| 707 } | 630 } |
| 708 | 631 |
| 709 CPDF_Dictionary* CPDF_Document::CreateNewPage(int iPage) { | 632 CPDF_Dictionary* CPDF_Document::CreateNewPage(int iPage) { |
| 710 CPDF_Dictionary* pDict = new CPDF_Dictionary(m_pByteStringPool); | 633 CPDF_Dictionary* pDict = new CPDF_Dictionary(m_pByteStringPool); |
| 711 pDict->SetNameFor("Type", "Page"); | 634 pDict->SetNameFor("Type", "Page"); |
| 712 uint32_t dwObjNum = AddIndirectObject(pDict); | 635 uint32_t dwObjNum = AddIndirectObject(pDict); |
| 713 if (InsertNewPage(this, iPage, pDict, m_PageList) < 0) { | 636 if (InsertNewPage(iPage, pDict, m_PageList) < 0) { |
| 714 ReleaseIndirectObject(dwObjNum); | 637 ReleaseIndirectObject(dwObjNum); |
| 715 return nullptr; | 638 return nullptr; |
| 716 } | 639 } |
| 717 return pDict; | 640 return pDict; |
| 718 } | 641 } |
| 719 | 642 |
| 643 int CPDF_Document::InsertDeletePDFPage(CPDF_Dictionary* pPages, | |
|
Lei Zhang
2016/11/03 07:01:57
This method can just return a bool. Nobody cares w
npm
2016/11/03 18:34:32
Done.
| |
| 644 int nPagesToGo, | |
| 645 CPDF_Dictionary* pPage, | |
|
Lei Zhang
2016/11/03 07:01:57
Name this "pPageDict" like InsertNewPage's paramet
npm
2016/11/03 18:34:32
Done.
| |
| 646 bool bInsert, | |
| 647 std::set<CPDF_Dictionary*>* pVisited) { | |
| 648 CPDF_Array* pKidList = pPages->GetArrayFor("Kids"); | |
| 649 if (!pKidList) | |
| 650 return -1; | |
| 651 | |
| 652 for (size_t i = 0; i < pKidList->GetCount(); i++) { | |
| 653 CPDF_Dictionary* pKid = pKidList->GetDictAt(i); | |
| 654 if (pKid->GetStringFor("Type") == "Page") { | |
| 655 if (nPagesToGo == 0) { | |
| 656 if (bInsert) { | |
| 657 pKidList->InsertAt(i, new CPDF_Reference(this, pPage->GetObjNum())); | |
| 658 pPage->SetReferenceFor("Parent", this, pPages->GetObjNum()); | |
| 659 } else { | |
| 660 pKidList->RemoveAt(i); | |
| 661 } | |
| 662 pPages->SetIntegerFor( | |
| 663 "Count", pPages->GetIntegerFor("Count") + (bInsert ? 1 : -1)); | |
| 664 return 1; | |
| 665 } | |
| 666 nPagesToGo--; | |
|
Lei Zhang
2016/11/03 07:01:57
Some early continues can make this easier to follo
npm
2016/11/03 18:34:32
Done.
| |
| 667 } else { | |
| 668 int nPages = pKid->GetIntegerFor("Count"); | |
| 669 if (nPagesToGo < nPages) { | |
| 670 if (pdfium::ContainsKey(*pVisited, pKid)) | |
| 671 return -1; | |
| 672 | |
| 673 pdfium::ScopedSetInsertion<CPDF_Dictionary*> insertion(pVisited, pKid); | |
| 674 if (InsertDeletePDFPage(pKid, nPagesToGo, pPage, bInsert, pVisited) < | |
| 675 0) { | |
| 676 return -1; | |
| 677 } | |
| 678 pPages->SetIntegerFor( | |
| 679 "Count", pPages->GetIntegerFor("Count") + (bInsert ? 1 : -1)); | |
| 680 return 1; | |
| 681 } | |
| 682 nPagesToGo -= nPages; | |
| 683 } | |
| 684 } | |
| 685 return 0; | |
| 686 } | |
| 687 | |
| 688 int CPDF_Document::InsertNewPage(int iPage, | |
|
Lei Zhang
2016/11/03 07:01:57
This can also return a bool.
npm
2016/11/03 18:34:32
Done.
| |
| 689 CPDF_Dictionary* pPageDict, | |
| 690 CFX_ArrayTemplate<uint32_t>& pageList) { | |
|
Lei Zhang
2016/11/03 07:01:57
Don't pass by non-const ref. It's easy to avoid he
npm
2016/11/03 18:34:32
Well, InsertAt is not const. But the param can be
| |
| 691 CPDF_Dictionary* pRoot = GetRoot(); | |
| 692 CPDF_Dictionary* pPages = pRoot ? pRoot->GetDictFor("Pages") : nullptr; | |
| 693 if (!pPages) | |
| 694 return -1; | |
| 695 | |
| 696 int nPages = GetPageCount(); | |
| 697 if (iPage < 0 || iPage > nPages) | |
| 698 return -1; | |
| 699 | |
| 700 if (iPage == nPages) { | |
| 701 CPDF_Array* pPagesList = pPages->GetArrayFor("Kids"); | |
| 702 if (!pPagesList) { | |
| 703 pPagesList = new CPDF_Array; | |
| 704 pPages->SetFor("Kids", pPagesList); | |
| 705 } | |
| 706 pPagesList->Add(new CPDF_Reference(this, pPageDict->GetObjNum())); | |
| 707 pPages->SetIntegerFor("Count", nPages + 1); | |
| 708 pPageDict->SetReferenceFor("Parent", this, pPages->GetObjNum()); | |
| 709 } else { | |
| 710 std::set<CPDF_Dictionary*> stack = {pPages}; | |
| 711 if (InsertDeletePDFPage(pPages, iPage, pPageDict, true, &stack) < 0) | |
| 712 return -1; | |
| 713 } | |
| 714 pageList.InsertAt(iPage, pPageDict->GetObjNum()); | |
| 715 return iPage; | |
| 716 } | |
| 717 | |
| 720 void CPDF_Document::DeletePage(int iPage) { | 718 void CPDF_Document::DeletePage(int iPage) { |
| 721 CPDF_Dictionary* pPages = GetPagesDict(); | 719 CPDF_Dictionary* pPages = GetPagesDict(); |
| 722 if (!pPages) | 720 if (!pPages) |
| 723 return; | 721 return; |
| 724 | 722 |
| 725 int nPages = pPages->GetIntegerFor("Count"); | 723 int nPages = pPages->GetIntegerFor("Count"); |
| 726 if (iPage < 0 || iPage >= nPages) | 724 if (iPage < 0 || iPage >= nPages) |
| 727 return; | 725 return; |
| 728 | 726 |
| 729 std::set<CPDF_Dictionary*> stack = {pPages}; | 727 std::set<CPDF_Dictionary*> stack = {pPages}; |
| 730 if (InsertDeletePDFPage(this, pPages, iPage, nullptr, false, &stack) < 0) | 728 if (InsertDeletePDFPage(pPages, iPage, nullptr, false, &stack) < 0) |
| 731 return; | 729 return; |
| 732 | 730 |
| 733 m_PageList.RemoveAt(iPage); | 731 m_PageList.RemoveAt(iPage); |
| 734 } | 732 } |
| 735 | 733 |
| 736 CPDF_Font* CPDF_Document::AddStandardFont(const FX_CHAR* font, | 734 CPDF_Font* CPDF_Document::AddStandardFont(const FX_CHAR* font, |
| 737 CPDF_FontEncoding* pEncoding) { | 735 CPDF_FontEncoding* pEncoding) { |
| 738 CFX_ByteString name(font); | 736 CFX_ByteString name(font); |
| 739 if (PDF_GetStandardFontName(&name) < 0) | 737 if (PDF_GetStandardFontName(&name) < 0) |
| 740 return nullptr; | 738 return nullptr; |
| (...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1013 pBBox, pLogFont->lfWeight / 5); | 1011 pBBox, pLogFont->lfWeight / 5); |
| 1014 pFontDesc->SetIntegerFor("CapHeight", capheight); | 1012 pFontDesc->SetIntegerFor("CapHeight", capheight); |
| 1015 pFontDict->SetReferenceFor("FontDescriptor", this, | 1013 pFontDict->SetReferenceFor("FontDescriptor", this, |
| 1016 AddIndirectObject(pFontDesc)); | 1014 AddIndirectObject(pFontDesc)); |
| 1017 hFont = SelectObject(hDC, hFont); | 1015 hFont = SelectObject(hDC, hFont); |
| 1018 DeleteObject(hFont); | 1016 DeleteObject(hFont); |
| 1019 DeleteDC(hDC); | 1017 DeleteDC(hDC); |
| 1020 return LoadFont(pBaseDict); | 1018 return LoadFont(pBaseDict); |
| 1021 } | 1019 } |
| 1022 #endif // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 1020 #endif // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
| OLD | NEW |