| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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 | |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | |
| 6 | |
| 7 #include "core/fpdfapi/fpdf_page/cpdf_page.h" | |
| 8 | |
| 9 #include <set> | |
| 10 | |
| 11 #include "core/fpdfapi/cpdf_pagerendercontext.h" | |
| 12 #include "core/fpdfapi/fpdf_page/cpdf_pageobject.h" | |
| 13 #include "core/fpdfapi/fpdf_page/pageint.h" | |
| 14 #include "core/fpdfapi/fpdf_parser/cpdf_array.h" | |
| 15 #include "core/fpdfapi/fpdf_parser/cpdf_dictionary.h" | |
| 16 #include "core/fpdfapi/fpdf_parser/cpdf_object.h" | |
| 17 #include "core/fpdfapi/fpdf_render/cpdf_pagerendercache.h" | |
| 18 #include "third_party/base/stl_util.h" | |
| 19 | |
| 20 CPDF_Page::CPDF_Page(CPDF_Document* pDocument, | |
| 21 CPDF_Dictionary* pPageDict, | |
| 22 bool bPageCache) | |
| 23 : m_PageWidth(100), | |
| 24 m_PageHeight(100), | |
| 25 m_pView(nullptr), | |
| 26 m_pPageRender(bPageCache ? new CPDF_PageRenderCache(this) : nullptr) { | |
| 27 m_pFormDict = pPageDict; | |
| 28 m_pDocument = pDocument; | |
| 29 if (!pPageDict) | |
| 30 return; | |
| 31 | |
| 32 CPDF_Object* pageAttr = GetPageAttr("Resources"); | |
| 33 m_pResources = pageAttr ? pageAttr->GetDict() : nullptr; | |
| 34 m_pPageResources = m_pResources; | |
| 35 CPDF_Object* pRotate = GetPageAttr("Rotate"); | |
| 36 int rotate = pRotate ? pRotate->GetInteger() / 90 % 4 : 0; | |
| 37 if (rotate < 0) | |
| 38 rotate += 4; | |
| 39 | |
| 40 CPDF_Array* pMediaBox = ToArray(GetPageAttr("MediaBox")); | |
| 41 CFX_FloatRect mediabox; | |
| 42 if (pMediaBox) { | |
| 43 mediabox = pMediaBox->GetRect(); | |
| 44 mediabox.Normalize(); | |
| 45 } | |
| 46 if (mediabox.IsEmpty()) | |
| 47 mediabox = CFX_FloatRect(0, 0, 612, 792); | |
| 48 | |
| 49 CPDF_Array* pCropBox = ToArray(GetPageAttr("CropBox")); | |
| 50 if (pCropBox) { | |
| 51 m_BBox = pCropBox->GetRect(); | |
| 52 m_BBox.Normalize(); | |
| 53 } | |
| 54 if (m_BBox.IsEmpty()) | |
| 55 m_BBox = mediabox; | |
| 56 else | |
| 57 m_BBox.Intersect(mediabox); | |
| 58 | |
| 59 m_PageWidth = m_BBox.right - m_BBox.left; | |
| 60 m_PageHeight = m_BBox.top - m_BBox.bottom; | |
| 61 if (rotate % 2) | |
| 62 std::swap(m_PageWidth, m_PageHeight); | |
| 63 | |
| 64 switch (rotate) { | |
| 65 case 0: | |
| 66 m_PageMatrix.Set(1.0f, 0, 0, 1.0f, -m_BBox.left, -m_BBox.bottom); | |
| 67 break; | |
| 68 case 1: | |
| 69 m_PageMatrix.Set(0, -1.0f, 1.0f, 0, -m_BBox.bottom, m_BBox.right); | |
| 70 break; | |
| 71 case 2: | |
| 72 m_PageMatrix.Set(-1.0f, 0, 0, -1.0f, m_BBox.right, m_BBox.top); | |
| 73 break; | |
| 74 case 3: | |
| 75 m_PageMatrix.Set(0, 1.0f, -1.0f, 0, m_BBox.top, -m_BBox.left); | |
| 76 break; | |
| 77 } | |
| 78 | |
| 79 m_Transparency = PDFTRANS_ISOLATED; | |
| 80 LoadTransInfo(); | |
| 81 } | |
| 82 | |
| 83 CPDF_Page::~CPDF_Page() {} | |
| 84 | |
| 85 void CPDF_Page::StartParse() { | |
| 86 if (m_ParseState == CONTENT_PARSED || m_ParseState == CONTENT_PARSING) | |
| 87 return; | |
| 88 | |
| 89 m_pParser.reset(new CPDF_ContentParser); | |
| 90 m_pParser->Start(this); | |
| 91 m_ParseState = CONTENT_PARSING; | |
| 92 } | |
| 93 | |
| 94 void CPDF_Page::ParseContent() { | |
| 95 StartParse(); | |
| 96 ContinueParse(nullptr); | |
| 97 } | |
| 98 | |
| 99 void CPDF_Page::SetRenderContext( | |
| 100 std::unique_ptr<CPDF_PageRenderContext> pContext) { | |
| 101 m_pRenderContext = std::move(pContext); | |
| 102 } | |
| 103 | |
| 104 CPDF_Object* CPDF_Page::GetPageAttr(const CFX_ByteString& name) const { | |
| 105 CPDF_Dictionary* pPageDict = m_pFormDict; | |
| 106 std::set<CPDF_Dictionary*> visited; | |
| 107 while (1) { | |
| 108 visited.insert(pPageDict); | |
| 109 if (CPDF_Object* pObj = pPageDict->GetDirectObjectFor(name)) | |
| 110 return pObj; | |
| 111 | |
| 112 pPageDict = pPageDict->GetDictFor("Parent"); | |
| 113 if (!pPageDict || pdfium::ContainsKey(visited, pPageDict)) | |
| 114 break; | |
| 115 } | |
| 116 return nullptr; | |
| 117 } | |
| 118 | |
| 119 void CPDF_Page::GetDisplayMatrix(CFX_Matrix& matrix, | |
| 120 int xPos, | |
| 121 int yPos, | |
| 122 int xSize, | |
| 123 int ySize, | |
| 124 int iRotate) const { | |
| 125 if (m_PageWidth == 0 || m_PageHeight == 0) { | |
| 126 return; | |
| 127 } | |
| 128 CFX_Matrix display_matrix; | |
| 129 int x0 = 0; | |
| 130 int y0 = 0; | |
| 131 int x1 = 0; | |
| 132 int y1 = 0; | |
| 133 int x2 = 0; | |
| 134 int y2 = 0; | |
| 135 iRotate %= 4; | |
| 136 switch (iRotate) { | |
| 137 case 0: | |
| 138 x0 = xPos; | |
| 139 y0 = yPos + ySize; | |
| 140 x1 = xPos; | |
| 141 y1 = yPos; | |
| 142 x2 = xPos + xSize; | |
| 143 y2 = yPos + ySize; | |
| 144 break; | |
| 145 case 1: | |
| 146 x0 = xPos; | |
| 147 y0 = yPos; | |
| 148 x1 = xPos + xSize; | |
| 149 y1 = yPos; | |
| 150 x2 = xPos; | |
| 151 y2 = yPos + ySize; | |
| 152 break; | |
| 153 case 2: | |
| 154 x0 = xPos + xSize; | |
| 155 y0 = yPos; | |
| 156 x1 = xPos + xSize; | |
| 157 y1 = yPos + ySize; | |
| 158 x2 = xPos; | |
| 159 y2 = yPos; | |
| 160 break; | |
| 161 case 3: | |
| 162 x0 = xPos + xSize; | |
| 163 y0 = yPos + ySize; | |
| 164 x1 = xPos; | |
| 165 y1 = yPos + ySize; | |
| 166 x2 = xPos + xSize; | |
| 167 y2 = yPos; | |
| 168 break; | |
| 169 } | |
| 170 display_matrix.Set( | |
| 171 ((FX_FLOAT)(x2 - x0)) / m_PageWidth, ((FX_FLOAT)(y2 - y0)) / m_PageWidth, | |
| 172 ((FX_FLOAT)(x1 - x0)) / m_PageHeight, | |
| 173 ((FX_FLOAT)(y1 - y0)) / m_PageHeight, (FX_FLOAT)x0, (FX_FLOAT)y0); | |
| 174 matrix = m_PageMatrix; | |
| 175 matrix.Concat(display_matrix); | |
| 176 } | |
| OLD | NEW |