OLD | NEW |
| (Empty) |
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. | |
4 | |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | |
6 | |
7 #include "core/fpdfapi/fpdf_page/include/cpdf_form.h" | |
8 #include "core/fpdfapi/fpdf_page/include/cpdf_page.h" | |
9 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" | |
10 #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" | |
11 #include "core/fpdfapi/fpdf_parser/include/cpdf_reference.h" | |
12 #include "core/fpdfapi/fpdf_render/include/cpdf_rendercontext.h" | |
13 #include "core/fpdfapi/fpdf_render/include/cpdf_renderoptions.h" | |
14 #include "core/fpdfdoc/cpvt_generateap.h" | |
15 #include "core/fpdfdoc/include/fpdf_doc.h" | |
16 #include "core/fxge/include/fx_ge.h" | |
17 | |
18 CPDF_AnnotList::CPDF_AnnotList(CPDF_Page* pPage) | |
19 : m_pDocument(pPage->m_pDocument) { | |
20 if (!pPage->m_pFormDict) | |
21 return; | |
22 | |
23 CPDF_Array* pAnnots = pPage->m_pFormDict->GetArrayBy("Annots"); | |
24 if (!pAnnots) | |
25 return; | |
26 | |
27 CPDF_Dictionary* pRoot = m_pDocument->GetRoot(); | |
28 CPDF_Dictionary* pAcroForm = pRoot->GetDictBy("AcroForm"); | |
29 FX_BOOL bRegenerateAP = | |
30 pAcroForm && pAcroForm->GetBooleanBy("NeedAppearances"); | |
31 for (size_t i = 0; i < pAnnots->GetCount(); ++i) { | |
32 CPDF_Dictionary* pDict = ToDictionary(pAnnots->GetDirectObjectAt(i)); | |
33 if (!pDict) | |
34 continue; | |
35 | |
36 uint32_t dwObjNum = pDict->GetObjNum(); | |
37 if (dwObjNum == 0) { | |
38 dwObjNum = m_pDocument->AddIndirectObject(pDict); | |
39 CPDF_Reference* pAction = new CPDF_Reference(m_pDocument, dwObjNum); | |
40 pAnnots->InsertAt(i, pAction); | |
41 pAnnots->RemoveAt(i + 1); | |
42 pDict = pAnnots->GetDictAt(i); | |
43 } | |
44 m_AnnotList.push_back( | |
45 std::unique_ptr<CPDF_Annot>(new CPDF_Annot(pDict, m_pDocument))); | |
46 if (bRegenerateAP && pDict->GetStringBy("Subtype") == "Widget" && | |
47 CPDF_InterForm::IsUpdateAPEnabled()) { | |
48 FPDF_GenerateAP(m_pDocument, pDict); | |
49 } | |
50 } | |
51 } | |
52 | |
53 CPDF_AnnotList::~CPDF_AnnotList() {} | |
54 | |
55 void CPDF_AnnotList::DisplayPass(CPDF_Page* pPage, | |
56 CFX_RenderDevice* pDevice, | |
57 CPDF_RenderContext* pContext, | |
58 FX_BOOL bPrinting, | |
59 CFX_Matrix* pMatrix, | |
60 FX_BOOL bWidgetPass, | |
61 CPDF_RenderOptions* pOptions, | |
62 FX_RECT* clip_rect) { | |
63 for (const auto& pAnnot : m_AnnotList) { | |
64 bool bWidget = pAnnot->GetSubType() == "Widget"; | |
65 if ((bWidgetPass && !bWidget) || (!bWidgetPass && bWidget)) | |
66 continue; | |
67 | |
68 uint32_t annot_flags = pAnnot->GetFlags(); | |
69 if (annot_flags & ANNOTFLAG_HIDDEN) | |
70 continue; | |
71 | |
72 if (bPrinting && (annot_flags & ANNOTFLAG_PRINT) == 0) | |
73 continue; | |
74 | |
75 if (!bPrinting && (annot_flags & ANNOTFLAG_NOVIEW)) | |
76 continue; | |
77 | |
78 if (pOptions) { | |
79 CPDF_OCContext* pOCContext = pOptions->m_pOCContext; | |
80 CPDF_Dictionary* pAnnotDict = pAnnot->GetAnnotDict(); | |
81 if (pOCContext && pAnnotDict && | |
82 !pOCContext->CheckOCGVisible(pAnnotDict->GetDictBy("OC"))) { | |
83 continue; | |
84 } | |
85 } | |
86 CFX_FloatRect annot_rect_f; | |
87 pAnnot->GetRect(annot_rect_f); | |
88 CFX_Matrix matrix = *pMatrix; | |
89 if (clip_rect) { | |
90 annot_rect_f.Transform(&matrix); | |
91 FX_RECT annot_rect = annot_rect_f.GetOutterRect(); | |
92 annot_rect.Intersect(*clip_rect); | |
93 if (annot_rect.IsEmpty()) { | |
94 continue; | |
95 } | |
96 } | |
97 if (pContext) { | |
98 pAnnot->DrawInContext(pPage, pContext, &matrix, CPDF_Annot::Normal); | |
99 } else if (!pAnnot->DrawAppearance(pPage, pDevice, &matrix, | |
100 CPDF_Annot::Normal, pOptions)) { | |
101 pAnnot->DrawBorder(pDevice, &matrix, pOptions); | |
102 } | |
103 } | |
104 } | |
105 | |
106 void CPDF_AnnotList::DisplayAnnots(CPDF_Page* pPage, | |
107 CFX_RenderDevice* pDevice, | |
108 CPDF_RenderContext* pContext, | |
109 FX_BOOL bPrinting, | |
110 CFX_Matrix* pUser2Device, | |
111 uint32_t dwAnnotFlags, | |
112 CPDF_RenderOptions* pOptions, | |
113 FX_RECT* pClipRect) { | |
114 if (dwAnnotFlags & 0x01) { | |
115 DisplayPass(pPage, pDevice, pContext, bPrinting, pUser2Device, FALSE, | |
116 pOptions, pClipRect); | |
117 } | |
118 if (dwAnnotFlags & 0x02) { | |
119 DisplayPass(pPage, pDevice, pContext, bPrinting, pUser2Device, TRUE, | |
120 pOptions, pClipRect); | |
121 } | |
122 } | |
123 | |
124 CPDF_Annot::CPDF_Annot(CPDF_Dictionary* pDict, CPDF_Document* pDocument) | |
125 : m_pAnnotDict(pDict), | |
126 m_pDocument(pDocument), | |
127 m_sSubtype(m_pAnnotDict->GetStringBy("Subtype")) {} | |
128 | |
129 CPDF_Annot::~CPDF_Annot() { | |
130 ClearCachedAP(); | |
131 } | |
132 | |
133 void CPDF_Annot::ClearCachedAP() { | |
134 for (const auto& pair : m_APMap) { | |
135 delete pair.second; | |
136 } | |
137 m_APMap.clear(); | |
138 } | |
139 CFX_ByteString CPDF_Annot::GetSubType() const { | |
140 return m_sSubtype; | |
141 } | |
142 | |
143 void CPDF_Annot::GetRect(CFX_FloatRect& rect) const { | |
144 if (!m_pAnnotDict) { | |
145 return; | |
146 } | |
147 rect = m_pAnnotDict->GetRectBy("Rect"); | |
148 rect.Normalize(); | |
149 } | |
150 | |
151 uint32_t CPDF_Annot::GetFlags() const { | |
152 return m_pAnnotDict->GetIntegerBy("F"); | |
153 } | |
154 | |
155 CPDF_Stream* FPDFDOC_GetAnnotAP(CPDF_Dictionary* pAnnotDict, | |
156 CPDF_Annot::AppearanceMode mode) { | |
157 CPDF_Dictionary* pAP = pAnnotDict->GetDictBy("AP"); | |
158 if (!pAP) { | |
159 return nullptr; | |
160 } | |
161 const FX_CHAR* ap_entry = "N"; | |
162 if (mode == CPDF_Annot::Down) | |
163 ap_entry = "D"; | |
164 else if (mode == CPDF_Annot::Rollover) | |
165 ap_entry = "R"; | |
166 if (!pAP->KeyExist(ap_entry)) | |
167 ap_entry = "N"; | |
168 | |
169 CPDF_Object* psub = pAP->GetDirectObjectBy(ap_entry); | |
170 if (!psub) | |
171 return nullptr; | |
172 if (CPDF_Stream* pStream = psub->AsStream()) | |
173 return pStream; | |
174 | |
175 if (CPDF_Dictionary* pDict = psub->AsDictionary()) { | |
176 CFX_ByteString as = pAnnotDict->GetStringBy("AS"); | |
177 if (as.IsEmpty()) { | |
178 CFX_ByteString value = pAnnotDict->GetStringBy("V"); | |
179 if (value.IsEmpty()) { | |
180 CPDF_Dictionary* pParentDict = pAnnotDict->GetDictBy("Parent"); | |
181 value = pParentDict ? pParentDict->GetStringBy("V") : CFX_ByteString(); | |
182 } | |
183 if (value.IsEmpty() || !pDict->KeyExist(value)) | |
184 as = "Off"; | |
185 else | |
186 as = value; | |
187 } | |
188 return pDict->GetStreamBy(as); | |
189 } | |
190 return nullptr; | |
191 } | |
192 | |
193 CPDF_Form* CPDF_Annot::GetAPForm(const CPDF_Page* pPage, AppearanceMode mode) { | |
194 CPDF_Stream* pStream = FPDFDOC_GetAnnotAP(m_pAnnotDict, mode); | |
195 if (!pStream) | |
196 return nullptr; | |
197 | |
198 auto it = m_APMap.find(pStream); | |
199 if (it != m_APMap.end()) | |
200 return it->second; | |
201 | |
202 CPDF_Form* pNewForm = | |
203 new CPDF_Form(m_pDocument, pPage->m_pResources, pStream); | |
204 pNewForm->ParseContent(nullptr, nullptr, nullptr); | |
205 m_APMap[pStream] = pNewForm; | |
206 return pNewForm; | |
207 } | |
208 | |
209 static CPDF_Form* FPDFDOC_Annot_GetMatrix(const CPDF_Page* pPage, | |
210 CPDF_Annot* pAnnot, | |
211 CPDF_Annot::AppearanceMode mode, | |
212 const CFX_Matrix* pUser2Device, | |
213 CFX_Matrix& matrix) { | |
214 CPDF_Form* pForm = pAnnot->GetAPForm(pPage, mode); | |
215 if (!pForm) { | |
216 return nullptr; | |
217 } | |
218 CFX_FloatRect form_bbox = pForm->m_pFormDict->GetRectBy("BBox"); | |
219 CFX_Matrix form_matrix = pForm->m_pFormDict->GetMatrixBy("Matrix"); | |
220 form_matrix.TransformRect(form_bbox); | |
221 CFX_FloatRect arect; | |
222 pAnnot->GetRect(arect); | |
223 matrix.MatchRect(arect, form_bbox); | |
224 matrix.Concat(*pUser2Device); | |
225 return pForm; | |
226 } | |
227 FX_BOOL CPDF_Annot::DrawAppearance(CPDF_Page* pPage, | |
228 CFX_RenderDevice* pDevice, | |
229 const CFX_Matrix* pUser2Device, | |
230 AppearanceMode mode, | |
231 const CPDF_RenderOptions* pOptions) { | |
232 CFX_Matrix matrix; | |
233 CPDF_Form* pForm = | |
234 FPDFDOC_Annot_GetMatrix(pPage, this, mode, pUser2Device, matrix); | |
235 if (!pForm) { | |
236 return FALSE; | |
237 } | |
238 CPDF_RenderContext context(pPage); | |
239 context.AppendLayer(pForm, &matrix); | |
240 context.Render(pDevice, pOptions, nullptr); | |
241 return TRUE; | |
242 } | |
243 FX_BOOL CPDF_Annot::DrawInContext(const CPDF_Page* pPage, | |
244 CPDF_RenderContext* pContext, | |
245 const CFX_Matrix* pUser2Device, | |
246 AppearanceMode mode) { | |
247 CFX_Matrix matrix; | |
248 CPDF_Form* pForm = | |
249 FPDFDOC_Annot_GetMatrix(pPage, this, mode, pUser2Device, matrix); | |
250 if (!pForm) { | |
251 return FALSE; | |
252 } | |
253 pContext->AppendLayer(pForm, &matrix); | |
254 return TRUE; | |
255 } | |
256 void CPDF_Annot::DrawBorder(CFX_RenderDevice* pDevice, | |
257 const CFX_Matrix* pUser2Device, | |
258 const CPDF_RenderOptions* pOptions) { | |
259 if (GetSubType() == "Popup") { | |
260 return; | |
261 } | |
262 uint32_t annot_flags = GetFlags(); | |
263 if (annot_flags & ANNOTFLAG_HIDDEN) { | |
264 return; | |
265 } | |
266 bool bPrinting = pDevice->GetDeviceClass() == FXDC_PRINTER || | |
267 (pOptions && (pOptions->m_Flags & RENDER_PRINTPREVIEW)); | |
268 if (bPrinting && (annot_flags & ANNOTFLAG_PRINT) == 0) { | |
269 return; | |
270 } | |
271 if (!bPrinting && (annot_flags & ANNOTFLAG_NOVIEW)) { | |
272 return; | |
273 } | |
274 CPDF_Dictionary* pBS = m_pAnnotDict->GetDictBy("BS"); | |
275 char style_char; | |
276 FX_FLOAT width; | |
277 CPDF_Array* pDashArray = nullptr; | |
278 if (!pBS) { | |
279 CPDF_Array* pBorderArray = m_pAnnotDict->GetArrayBy("Border"); | |
280 style_char = 'S'; | |
281 if (pBorderArray) { | |
282 width = pBorderArray->GetNumberAt(2); | |
283 if (pBorderArray->GetCount() == 4) { | |
284 pDashArray = pBorderArray->GetArrayAt(3); | |
285 if (!pDashArray) { | |
286 return; | |
287 } | |
288 size_t nLen = pDashArray->GetCount(); | |
289 size_t i = 0; | |
290 for (; i < nLen; ++i) { | |
291 CPDF_Object* pObj = pDashArray->GetDirectObjectAt(i); | |
292 if (pObj && pObj->GetInteger()) { | |
293 break; | |
294 } | |
295 } | |
296 if (i == nLen) { | |
297 return; | |
298 } | |
299 style_char = 'D'; | |
300 } | |
301 } else { | |
302 width = 1; | |
303 } | |
304 } else { | |
305 CFX_ByteString style = pBS->GetStringBy("S"); | |
306 pDashArray = pBS->GetArrayBy("D"); | |
307 style_char = style[1]; | |
308 width = pBS->GetNumberBy("W"); | |
309 } | |
310 if (width <= 0) { | |
311 return; | |
312 } | |
313 CPDF_Array* pColor = m_pAnnotDict->GetArrayBy("C"); | |
314 uint32_t argb = 0xff000000; | |
315 if (pColor) { | |
316 int R = (int32_t)(pColor->GetNumberAt(0) * 255); | |
317 int G = (int32_t)(pColor->GetNumberAt(1) * 255); | |
318 int B = (int32_t)(pColor->GetNumberAt(2) * 255); | |
319 argb = ArgbEncode(0xff, R, G, B); | |
320 } | |
321 CFX_GraphStateData graph_state; | |
322 graph_state.m_LineWidth = width; | |
323 if (style_char == 'D') { | |
324 if (pDashArray) { | |
325 size_t dash_count = pDashArray->GetCount(); | |
326 if (dash_count % 2) { | |
327 dash_count++; | |
328 } | |
329 graph_state.m_DashArray = FX_Alloc(FX_FLOAT, dash_count); | |
330 graph_state.m_DashCount = dash_count; | |
331 size_t i; | |
332 for (i = 0; i < pDashArray->GetCount(); ++i) { | |
333 graph_state.m_DashArray[i] = pDashArray->GetNumberAt(i); | |
334 } | |
335 if (i < dash_count) { | |
336 graph_state.m_DashArray[i] = graph_state.m_DashArray[i - 1]; | |
337 } | |
338 } else { | |
339 graph_state.m_DashArray = FX_Alloc(FX_FLOAT, 2); | |
340 graph_state.m_DashCount = 2; | |
341 graph_state.m_DashArray[0] = graph_state.m_DashArray[1] = 3 * 1.0f; | |
342 } | |
343 } | |
344 CFX_FloatRect rect; | |
345 GetRect(rect); | |
346 CFX_PathData path; | |
347 width /= 2; | |
348 path.AppendRect(rect.left + width, rect.bottom + width, rect.right - width, | |
349 rect.top - width); | |
350 int fill_type = 0; | |
351 if (pOptions && (pOptions->m_Flags & RENDER_NOPATHSMOOTH)) { | |
352 fill_type |= FXFILL_NOPATHSMOOTH; | |
353 } | |
354 pDevice->DrawPath(&path, pUser2Device, &graph_state, argb, argb, fill_type); | |
355 } | |
OLD | NEW |