Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cpdf_page.cpp
Go to the documentation of this file.
1// Copyright 2016 The PDFium Authors
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/page/cpdf_page.h"
8
9#include <set>
10#include <utility>
11
12#include "constants/page_object.h"
13#include "core/fpdfapi/page/cpdf_contentparser.h"
14#include "core/fpdfapi/page/cpdf_pageimagecache.h"
15#include "core/fpdfapi/page/cpdf_pageobject.h"
16#include "core/fpdfapi/parser/cpdf_array.h"
17#include "core/fpdfapi/parser/cpdf_dictionary.h"
18#include "core/fpdfapi/parser/cpdf_object.h"
19#include "third_party/base/check.h"
20#include "third_party/base/check_op.h"
21#include "third_party/base/containers/contains.h"
22
23CPDF_Page::CPDF_Page(CPDF_Document* pDocument,
24 RetainPtr<CPDF_Dictionary> pPageDict)
25 : CPDF_PageObjectHolder(pDocument, std::move(pPageDict), nullptr, nullptr),
26 m_PageSize(100, 100),
27 m_pPDFDocument(pDocument) {
28 // Cannot initialize |m_pResources| and |m_pPageResources| via the
29 // CPDF_PageObjectHolder ctor because GetPageAttr() requires
30 // CPDF_PageObjectHolder to finish initializing first.
31 RetainPtr<CPDF_Object> pPageAttr =
32 GetMutablePageAttr(pdfium::page_object::kResources);
33 m_pResources = pPageAttr ? pPageAttr->GetMutableDict() : nullptr;
34 m_pPageResources = m_pResources;
35
39}
40
41CPDF_Page::~CPDF_Page() = default;
42
43CPDF_Page* CPDF_Page::AsPDFPage() {
44 return this;
45}
46
47CPDFXFA_Page* CPDF_Page::AsXFAPage() {
48 return nullptr;
49}
50
51CPDF_Document* CPDF_Page::GetDocument() const {
52 return m_pPDFDocument;
53}
54
55float CPDF_Page::GetPageWidth() const {
56 return m_PageSize.width;
57}
58
59float CPDF_Page::GetPageHeight() const {
60 return m_PageSize.height;
61}
62
63bool CPDF_Page::IsPage() const {
64 return true;
65}
66
67void CPDF_Page::ParseContent() {
69 return;
70
71 if (GetParseState() == ParseState::kNotParsed)
72 StartParse(std::make_unique<CPDF_ContentParser>(this));
73
75 ContinueParse(nullptr);
76}
77
78RetainPtr<CPDF_Object> CPDF_Page::GetMutablePageAttr(const ByteString& name) {
79 return pdfium::WrapRetain(const_cast<CPDF_Object*>(GetPageAttr(name).Get()));
80}
81
82RetainPtr<const CPDF_Object> CPDF_Page::GetPageAttr(
83 const ByteString& name) const {
84 std::set<RetainPtr<const CPDF_Dictionary>> visited;
85 RetainPtr<const CPDF_Dictionary> pPageDict = GetDict();
86 while (pPageDict && !pdfium::Contains(visited, pPageDict)) {
87 RetainPtr<const CPDF_Object> pObj = pPageDict->GetDirectObjectFor(name);
88 if (pObj)
89 return pObj;
90
91 visited.insert(pPageDict);
92 pPageDict = pPageDict->GetDictFor(pdfium::page_object::kParent);
93 }
94 return nullptr;
95}
96
97CFX_FloatRect CPDF_Page::GetBox(const ByteString& name) const {
98 CFX_FloatRect box;
99 RetainPtr<const CPDF_Array> pBox = ToArray(GetPageAttr(name));
100 if (pBox) {
101 box = pBox->GetRect();
102 box.Normalize();
103 }
104 return box;
105}
106
108 const FX_RECT& rect,
109 int rotate,
110 const CFX_PointF& device_point) const {
111 CFX_Matrix page2device = GetDisplayMatrix(rect, rotate);
112 return page2device.GetInverse().Transform(device_point);
113}
114
116 const FX_RECT& rect,
117 int rotate,
118 const CFX_PointF& page_point) const {
119 CFX_Matrix page2device = GetDisplayMatrix(rect, rotate);
120 return page2device.Transform(page_point);
121}
122
123CFX_Matrix CPDF_Page::GetDisplayMatrix(const FX_RECT& rect, int iRotate) const {
124 if (m_PageSize.width == 0 || m_PageSize.height == 0)
125 return CFX_Matrix();
126
127 float x0 = 0;
128 float y0 = 0;
129 float x1 = 0;
130 float y1 = 0;
131 float x2 = 0;
132 float y2 = 0;
133 iRotate %= 4;
134 // This code implicitly inverts the y-axis to account for page coordinates
135 // pointing up and bitmap coordinates pointing down. (x0, y0) is the base
136 // point, (x1, y1) is that point translated on y and (x2, y2) is the point
137 // translated on x. On iRotate = 0, y0 is rect.bottom and the translation
138 // to get y1 is performed as negative. This results in the desired
139 // transformation.
140 switch (iRotate) {
141 case 0:
142 x0 = rect.left;
143 y0 = rect.bottom;
144 x1 = rect.left;
145 y1 = rect.top;
146 x2 = rect.right;
147 y2 = rect.bottom;
148 break;
149 case 1:
150 x0 = rect.left;
151 y0 = rect.top;
152 x1 = rect.right;
153 y1 = rect.top;
154 x2 = rect.left;
155 y2 = rect.bottom;
156 break;
157 case 2:
158 x0 = rect.right;
159 y0 = rect.top;
160 x1 = rect.right;
161 y1 = rect.bottom;
162 x2 = rect.left;
163 y2 = rect.top;
164 break;
165 case 3:
166 x0 = rect.right;
167 y0 = rect.bottom;
168 x1 = rect.left;
169 y1 = rect.bottom;
170 x2 = rect.right;
171 y2 = rect.top;
172 break;
173 }
174 CFX_Matrix matrix((x2 - x0) / m_PageSize.width, (y2 - y0) / m_PageSize.width,
175 (x1 - x0) / m_PageSize.height,
176 (y1 - y0) / m_PageSize.height, x0, y0);
177 return m_PageMatrix * matrix;
178}
179
180int CPDF_Page::GetPageRotation() const {
181 RetainPtr<const CPDF_Object> pRotate =
182 GetPageAttr(pdfium::page_object::kRotate);
183 int rotate = pRotate ? (pRotate->GetInteger() / 90) % 4 : 0;
184 return (rotate < 0) ? (rotate + 4) : rotate;
185}
186
187RetainPtr<CPDF_Array> CPDF_Page::GetOrCreateAnnotsArray() {
188 return GetMutableDict()->GetOrCreateArrayFor("Annots");
189}
190
191RetainPtr<CPDF_Array> CPDF_Page::GetMutableAnnotsArray() {
192 return GetMutableDict()->GetMutableArrayFor("Annots");
193}
194
195RetainPtr<const CPDF_Array> CPDF_Page::GetAnnotsArray() const {
196 return GetDict()->GetArrayFor("Annots");
197}
198
199void CPDF_Page::AddPageImageCache() {
200 m_pPageImageCache = std::make_unique<CPDF_PageImageCache>(this);
201}
202
203void CPDF_Page::SetRenderContext(std::unique_ptr<RenderContextIface> pContext) {
204 DCHECK(!m_pRenderContext);
205 DCHECK(pContext);
206 m_pRenderContext = std::move(pContext);
207}
208
209void CPDF_Page::ClearRenderContext() {
210 m_pRenderContext.reset();
211}
212
213void CPDF_Page::ClearView() {
214 if (m_pView)
215 m_pView->ClearPage(this);
216}
217
218void CPDF_Page::UpdateDimensions() {
219 CFX_FloatRect mediabox = GetBox(pdfium::page_object::kMediaBox);
220 if (mediabox.IsEmpty())
221 mediabox = CFX_FloatRect(0, 0, 612, 792);
222
223 m_BBox = GetBox(pdfium::page_object::kCropBox);
225 m_BBox = mediabox;
226 else
227 m_BBox.Intersect(mediabox);
228
229 m_PageSize.width = m_BBox.Width();
230 m_PageSize.height = m_BBox.Height();
231
232 switch (GetPageRotation()) {
233 case 0:
234 m_PageMatrix = CFX_Matrix(1.0f, 0, 0, 1.0f, -m_BBox.left, -m_BBox.bottom);
235 break;
236 case 1:
237 std::swap(m_PageSize.width, m_PageSize.height);
238 m_PageMatrix =
240 break;
241 case 2:
242 m_PageMatrix = CFX_Matrix(-1.0f, 0, 0, -1.0f, m_BBox.right, m_BBox.top);
243 break;
244 case 3:
245 std::swap(m_PageSize.width, m_PageSize.height);
246 m_PageMatrix = CFX_Matrix(0, 1.0f, -1.0f, 0, m_BBox.top, -m_BBox.left);
247 break;
248 }
249}
250
251CPDF_Page::RenderContextClearer::RenderContextClearer(CPDF_Page* pPage)
252 : m_pPage(pPage) {}
253
255 if (m_pPage)
256 m_pPage->ClearRenderContext();
257}
constexpr CFX_FloatRect(float l, float b, float r, float t)
bool IsEmpty() const
void Intersect(const CFX_FloatRect &other_rect)
CFX_FloatRect & operator=(const CFX_FloatRect &that)=default
CFX_Matrix & operator=(const CFX_Matrix &other)=default
CFX_Matrix(float a1, float b1, float c1, float d1, float e1, float f1)
CFX_Matrix operator*(const CFX_Matrix &right) const
CFX_PointF Transform(const CFX_PointF &point) const
CFX_Matrix GetInverse() const
CFX_Matrix()=default
ParseState GetParseState() const
void ContinueParse(PauseIndicatorIface *pPause)
CPDF_Transparency m_Transparency
RenderContextClearer(CPDF_Page *pPage)
RetainPtr< const CPDF_Array > GetAnnotsArray() const
RetainPtr< CPDF_Array > GetOrCreateAnnotsArray()
absl::optional< CFX_PointF > DeviceToPage(const FX_RECT &rect, int rotate, const CFX_PointF &device_point) const override
CPDF_Document * GetDocument() const override
Definition cpdf_page.cpp:51
float GetPageHeight() const override
Definition cpdf_page.cpp:59
void UpdateDimensions()
~CPDF_Page() override
bool IsPage() const override
Definition cpdf_page.cpp:63
float GetPageWidth() const override
Definition cpdf_page.cpp:55
CPDFXFA_Page * AsXFAPage() override
Definition cpdf_page.cpp:47
void ParseContent()
Definition cpdf_page.cpp:67
RetainPtr< CPDF_Array > GetMutableAnnotsArray()
void ClearView()
absl::optional< CFX_PointF > PageToDevice(const FX_RECT &rect, int rotate, const CFX_PointF &page_point) const override
void SetRenderContext(std::unique_ptr< RenderContextIface > pContext)
CPDF_Page * AsPDFPage() override
Definition cpdf_page.cpp:43
void AddPageImageCache()
CFX_Matrix GetDisplayMatrix(const FX_RECT &rect, int iRotate) const override
int GetPageRotation() const
void ClearRenderContext()
const char kMediaBox[]
const char kParent[]
const char kCropBox[]
int32_t bottom
int32_t right
int32_t top
int32_t left