Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(367)

Side by Side Diff: core/fpdfdoc/doc_ocg.cpp

Issue 2006483002: Clean up doc_ocg.cpp. (Closed) Base URL: https://pdfium.googlesource.com/pdfium@master
Patch Set: nit Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | core/fpdfdoc/include/fpdf_doc.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/fpdf_page/cpdf_contentmarkdata.h" 7 #include "core/fpdfapi/fpdf_page/cpdf_contentmarkdata.h"
8 #include "core/fpdfapi/fpdf_page/include/cpdf_pageobject.h" 8 #include "core/fpdfapi/fpdf_page/include/cpdf_pageobject.h"
9 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" 9 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h"
10 #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" 10 #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h"
11 #include "core/fpdfdoc/include/fpdf_doc.h" 11 #include "core/fpdfdoc/include/fpdf_doc.h"
12 12
13 static int32_t FPDFDOC_OCG_FindGroup(const CPDF_Object* pObject, 13 namespace {
14 const CPDF_Dictionary* pGroupDict) { 14
15 if (!pObject || !pGroupDict) 15 int32_t FPDFDOC_OCG_FindGroup(const CPDF_Array* pArray,
16 const CPDF_Dictionary* pGroupDict) {
17 if (!pArray || !pGroupDict)
16 return -1; 18 return -1;
17 19
18 if (const CPDF_Array* pArray = pObject->AsArray()) { 20 for (size_t i = 0; i < pArray->GetCount(); i++) {
19 for (size_t i = 0; i < pArray->GetCount(); i++) { 21 if (pArray->GetDictAt(i) == pGroupDict)
20 if (pArray->GetDictAt(i) == pGroupDict) 22 return i;
21 return i;
22 }
23 return -1;
24 } 23 }
25 return pObject->GetDict() == pGroupDict ? 0 : -1; 24 return -1;
26 } 25 }
27 static FX_BOOL FPDFDOC_OCG_HasIntent(const CPDF_Dictionary* pDict, 26
28 const CFX_ByteStringC& csElement, 27 bool FPDFDOC_OCG_HasIntent(const CPDF_Dictionary* pDict,
29 const CFX_ByteStringC& csDef = "") { 28 const CFX_ByteStringC& csElement,
29 const CFX_ByteStringC& csDef) {
30 CPDF_Object* pIntent = pDict->GetDirectObjectBy("Intent"); 30 CPDF_Object* pIntent = pDict->GetDirectObjectBy("Intent");
31 if (!pIntent) { 31 if (!pIntent)
32 return csElement == csDef; 32 return csElement == csDef;
33 } 33
34 CFX_ByteString bsIntent; 34 CFX_ByteString bsIntent;
35 if (CPDF_Array* pArray = pIntent->AsArray()) { 35 if (CPDF_Array* pArray = pIntent->AsArray()) {
36 for (size_t i = 0; i < pArray->GetCount(); i++) { 36 for (size_t i = 0; i < pArray->GetCount(); i++) {
37 bsIntent = pArray->GetStringAt(i); 37 bsIntent = pArray->GetStringAt(i);
38 if (bsIntent == "All" || bsIntent == csElement) 38 if (bsIntent == "All" || bsIntent == csElement)
39 return TRUE; 39 return true;
40 } 40 }
41 return FALSE; 41 return false;
42 } 42 }
43 bsIntent = pIntent->GetString(); 43 bsIntent = pIntent->GetString();
44 return bsIntent == "All" || bsIntent == csElement; 44 return bsIntent == "All" || bsIntent == csElement;
45 } 45 }
46 46
47 static CPDF_Dictionary* FPDFDOC_OCG_GetConfig(CPDF_Document* pDoc, 47 CPDF_Dictionary* FPDFDOC_OCG_GetConfig(CPDF_Document* pDoc,
48 const CPDF_Dictionary* pOCGDict) { 48 const CPDF_Dictionary* pOCGDict) {
49 ASSERT(pOCGDict); 49 ASSERT(pOCGDict);
50 CPDF_Dictionary* pOCProperties = pDoc->GetRoot()->GetDictBy("OCProperties"); 50 CPDF_Dictionary* pOCProperties = pDoc->GetRoot()->GetDictBy("OCProperties");
51 if (!pOCProperties) 51 if (!pOCProperties)
52 return nullptr; 52 return nullptr;
53 53
54 CPDF_Array* pOCGs = pOCProperties->GetArrayBy("OCGs"); 54 CPDF_Array* pOCGs = pOCProperties->GetArrayBy("OCGs");
55 if (!pOCGs) 55 if (!pOCGs)
56 return nullptr; 56 return nullptr;
57 57
58 if (FPDFDOC_OCG_FindGroup(pOCGs, pOCGDict) < 0) 58 if (FPDFDOC_OCG_FindGroup(pOCGs, pOCGDict) < 0)
59 return nullptr; 59 return nullptr;
60 60
61 CPDF_Dictionary* pConfig = pOCProperties->GetDictBy("D"); 61 CPDF_Dictionary* pConfig = pOCProperties->GetDictBy("D");
62 CPDF_Array* pConfigs = pOCProperties->GetArrayBy("Configs"); 62 CPDF_Array* pConfigs = pOCProperties->GetArrayBy("Configs");
63 if (pConfigs) { 63 if (!pConfigs)
64 CPDF_Dictionary* pFind; 64 return pConfig;
65 for (size_t i = 0; i < pConfigs->GetCount(); i++) { 65
66 pFind = pConfigs->GetDictAt(i); 66 for (size_t i = 0; i < pConfigs->GetCount(); i++) {
67 if (!pFind) { 67 CPDF_Dictionary* pFind = pConfigs->GetDictAt(i);
68 continue; 68 if (pFind && FPDFDOC_OCG_HasIntent(pFind, "View", "View"))
69 } 69 return pFind;
70 if (!FPDFDOC_OCG_HasIntent(pFind, "View", "View")) {
71 continue;
72 }
73 pConfig = pFind;
74 break;
75 }
76 } 70 }
77 return pConfig; 71 return pConfig;
78 } 72 }
79 static CFX_ByteString FPDFDOC_OCG_GetUsageTypeString( 73
80 CPDF_OCContext::UsageType eType) { 74 CFX_ByteString FPDFDOC_OCG_GetUsageTypeString(CPDF_OCContext::UsageType eType) {
81 CFX_ByteString csState = "View"; 75 CFX_ByteString csState;
82 if (eType == CPDF_OCContext::Design) { 76 switch (eType) {
83 csState = "Design"; 77 case CPDF_OCContext::Design:
84 } else if (eType == CPDF_OCContext::Print) { 78 csState = "Design";
85 csState = "Print"; 79 break;
86 } else if (eType == CPDF_OCContext::Export) { 80 case CPDF_OCContext::Print:
87 csState = "Export"; 81 csState = "Print";
82 break;
83 case CPDF_OCContext::Export:
84 csState = "Export";
85 break;
86 default:
87 csState = "View";
88 break;
88 } 89 }
89 return csState; 90 return csState;
90 } 91 }
91 CPDF_OCContext::CPDF_OCContext(CPDF_Document* pDoc, UsageType eUsageType) { 92
93 } // namespace
94
95 CPDF_OCContext::CPDF_OCContext(CPDF_Document* pDoc, UsageType eUsageType)
96 : m_pDocument(pDoc), m_eUsageType(eUsageType) {
92 ASSERT(pDoc); 97 ASSERT(pDoc);
93 m_pDocument = pDoc;
94 m_eUsageType = eUsageType;
95 }
96 CPDF_OCContext::~CPDF_OCContext() {
97 m_OCGStates.clear();
98 } 98 }
99 99
100 FX_BOOL CPDF_OCContext::LoadOCGStateFromConfig(const CFX_ByteString& csConfig, 100 CPDF_OCContext::~CPDF_OCContext() {
101 const CPDF_Dictionary* pOCGDict, 101 }
102 FX_BOOL& bValidConfig) const { 102
103 bool CPDF_OCContext::LoadOCGStateFromConfig(
104 const CFX_ByteString& csConfig,
105 const CPDF_Dictionary* pOCGDict) const {
103 CPDF_Dictionary* pConfig = FPDFDOC_OCG_GetConfig(m_pDocument, pOCGDict); 106 CPDF_Dictionary* pConfig = FPDFDOC_OCG_GetConfig(m_pDocument, pOCGDict);
104 if (!pConfig) 107 if (!pConfig)
105 return TRUE; 108 return true;
106 109
107 bValidConfig = TRUE; 110 bool bState = pConfig->GetStringBy("BaseState", "ON") != "OFF";
108 FX_BOOL bState = pConfig->GetStringBy("BaseState", "ON") != "OFF";
109 CPDF_Array* pArray = pConfig->GetArrayBy("ON"); 111 CPDF_Array* pArray = pConfig->GetArrayBy("ON");
110 if (pArray) { 112 if (pArray) {
111 if (FPDFDOC_OCG_FindGroup(pArray, pOCGDict) >= 0) { 113 if (FPDFDOC_OCG_FindGroup(pArray, pOCGDict) >= 0)
112 bState = TRUE; 114 bState = true;
113 }
114 } 115 }
115 pArray = pConfig->GetArrayBy("OFF"); 116 pArray = pConfig->GetArrayBy("OFF");
116 if (pArray) { 117 if (pArray) {
117 if (FPDFDOC_OCG_FindGroup(pArray, pOCGDict) >= 0) { 118 if (FPDFDOC_OCG_FindGroup(pArray, pOCGDict) >= 0)
118 bState = FALSE; 119 bState = false;
119 }
120 } 120 }
121 pArray = pConfig->GetArrayBy("AS"); 121 pArray = pConfig->GetArrayBy("AS");
122 if (pArray) { 122 if (!pArray)
123 CFX_ByteString csFind = csConfig + "State"; 123 return bState;
124 for (size_t i = 0; i < pArray->GetCount(); i++) { 124
125 CPDF_Dictionary* pUsage = pArray->GetDictAt(i); 125 CFX_ByteString csFind = csConfig + "State";
126 if (!pUsage) { 126 for (size_t i = 0; i < pArray->GetCount(); i++) {
127 continue; 127 CPDF_Dictionary* pUsage = pArray->GetDictAt(i);
128 } 128 if (!pUsage)
129 if (pUsage->GetStringBy("Event", "View") != csConfig) { 129 continue;
130 continue; 130
131 } 131 if (pUsage->GetStringBy("Event", "View") != csConfig)
132 CPDF_Array* pOCGs = pUsage->GetArrayBy("OCGs"); 132 continue;
133 if (!pOCGs) { 133
134 continue; 134 CPDF_Array* pOCGs = pUsage->GetArrayBy("OCGs");
135 } 135 if (!pOCGs)
136 if (FPDFDOC_OCG_FindGroup(pOCGs, pOCGDict) < 0) { 136 continue;
137 continue; 137
138 } 138 if (FPDFDOC_OCG_FindGroup(pOCGs, pOCGDict) < 0)
139 CPDF_Dictionary* pState = pUsage->GetDictBy(csConfig); 139 continue;
140 if (!pState) { 140
141 continue; 141 CPDF_Dictionary* pState = pUsage->GetDictBy(csConfig);
142 } 142 if (!pState)
143 bState = pState->GetStringBy(csFind) != "OFF"; 143 continue;
144 } 144
145 bState = pState->GetStringBy(csFind) != "OFF";
145 } 146 }
146 return bState; 147 return bState;
147 } 148 }
148 FX_BOOL CPDF_OCContext::LoadOCGState(const CPDF_Dictionary* pOCGDict) const { 149
149 if (!FPDFDOC_OCG_HasIntent(pOCGDict, "View", "View")) { 150 bool CPDF_OCContext::LoadOCGState(const CPDF_Dictionary* pOCGDict) const {
150 return TRUE; 151 if (!FPDFDOC_OCG_HasIntent(pOCGDict, "View", "View"))
151 } 152 return true;
153
152 CFX_ByteString csState = FPDFDOC_OCG_GetUsageTypeString(m_eUsageType); 154 CFX_ByteString csState = FPDFDOC_OCG_GetUsageTypeString(m_eUsageType);
153 CPDF_Dictionary* pUsage = pOCGDict->GetDictBy("Usage"); 155 CPDF_Dictionary* pUsage = pOCGDict->GetDictBy("Usage");
154 if (pUsage) { 156 if (pUsage) {
155 CPDF_Dictionary* pState = pUsage->GetDictBy(csState); 157 CPDF_Dictionary* pState = pUsage->GetDictBy(csState);
156 if (pState) { 158 if (pState) {
157 CFX_ByteString csFind = csState + "State"; 159 CFX_ByteString csFind = csState + "State";
158 if (pState->KeyExist(csFind)) { 160 if (pState->KeyExist(csFind)) {
159 return pState->GetStringBy(csFind) != "OFF"; 161 return pState->GetStringBy(csFind) != "OFF";
160 } 162 }
161 } 163 }
162 if (csState != "View") { 164 if (csState != "View") {
163 pState = pUsage->GetDictBy("View"); 165 pState = pUsage->GetDictBy("View");
164 if (pState && pState->KeyExist("ViewState")) { 166 if (pState && pState->KeyExist("ViewState")) {
165 return pState->GetStringBy("ViewState") != "OFF"; 167 return pState->GetStringBy("ViewState") != "OFF";
166 } 168 }
167 } 169 }
168 } 170 }
169 FX_BOOL bDefValid = FALSE; 171 return LoadOCGStateFromConfig(csState, pOCGDict);
170 return LoadOCGStateFromConfig(csState, pOCGDict, bDefValid);
171 } 172 }
172 173
173 FX_BOOL CPDF_OCContext::GetOCGVisible(const CPDF_Dictionary* pOCGDict) { 174 bool CPDF_OCContext::GetOCGVisible(const CPDF_Dictionary* pOCGDict) {
174 if (!pOCGDict) 175 if (!pOCGDict)
175 return FALSE; 176 return false;
176 177
177 const auto it = m_OCGStates.find(pOCGDict); 178 const auto it = m_OCGStates.find(pOCGDict);
178 if (it != m_OCGStates.end()) 179 if (it != m_OCGStates.end())
179 return it->second; 180 return it->second;
180 181
181 FX_BOOL bState = LoadOCGState(pOCGDict); 182 bool bState = LoadOCGState(pOCGDict);
182 m_OCGStates[pOCGDict] = bState; 183 m_OCGStates[pOCGDict] = bState;
183 return bState; 184 return bState;
184 } 185 }
185 186
186 FX_BOOL CPDF_OCContext::CheckObjectVisible(const CPDF_PageObject* pObj) { 187 bool CPDF_OCContext::CheckObjectVisible(const CPDF_PageObject* pObj) {
187 const CPDF_ContentMarkData* pData = pObj->m_ContentMark; 188 const CPDF_ContentMarkData* pData = pObj->m_ContentMark;
188 for (int i = 0; i < pData->CountItems(); i++) { 189 for (int i = 0; i < pData->CountItems(); i++) {
189 const CPDF_ContentMarkItem& item = pData->GetItem(i); 190 const CPDF_ContentMarkItem& item = pData->GetItem(i);
190 if (item.GetName() == "OC" && 191 if (item.GetName() == "OC" &&
191 item.GetParamType() == CPDF_ContentMarkItem::PropertiesDict && 192 item.GetParamType() == CPDF_ContentMarkItem::PropertiesDict &&
192 !CheckOCGVisible(item.GetParam())) { 193 !CheckOCGVisible(item.GetParam())) {
193 return FALSE; 194 return false;
194 } 195 }
195 } 196 }
196 return TRUE; 197 return true;
197 } 198 }
198 199
199 FX_BOOL CPDF_OCContext::GetOCGVE(CPDF_Array* pExpression, 200 bool CPDF_OCContext::GetOCGVE(CPDF_Array* pExpression, int nLevel) {
200 FX_BOOL bFromConfig, 201 if (nLevel > 32 || !pExpression)
201 int nLevel) { 202 return false;
202 if (nLevel > 32) { 203
203 return FALSE;
204 }
205 if (!pExpression) {
206 return FALSE;
207 }
208 CPDF_Object* pOCGObj;
209 CFX_ByteString csOperator = pExpression->GetStringAt(0); 204 CFX_ByteString csOperator = pExpression->GetStringAt(0);
210 if (csOperator == "Not") { 205 if (csOperator == "Not") {
211 pOCGObj = pExpression->GetDirectObjectAt(1); 206 CPDF_Object* pOCGObj = pExpression->GetDirectObjectAt(1);
212 if (!pOCGObj) 207 if (!pOCGObj)
213 return FALSE; 208 return false;
214 if (CPDF_Dictionary* pDict = pOCGObj->AsDictionary()) 209 if (CPDF_Dictionary* pDict = pOCGObj->AsDictionary())
215 return !(bFromConfig ? LoadOCGState(pDict) : GetOCGVisible(pDict)); 210 return !GetOCGVisible(pDict);
216 if (CPDF_Array* pArray = pOCGObj->AsArray()) 211 if (CPDF_Array* pArray = pOCGObj->AsArray())
217 return !GetOCGVE(pArray, bFromConfig, nLevel + 1); 212 return !GetOCGVE(pArray, nLevel + 1);
218 return FALSE; 213 return false;
219 } 214 }
220 if (csOperator == "Or" || csOperator == "And") {
221 FX_BOOL bValue = FALSE;
222 for (size_t i = 1; i < pExpression->GetCount(); i++) {
223 pOCGObj = pExpression->GetDirectObjectAt(1);
224 if (!pOCGObj) {
225 continue;
226 }
227 FX_BOOL bItem = FALSE;
228 if (CPDF_Dictionary* pDict = pOCGObj->AsDictionary())
229 bItem = bFromConfig ? LoadOCGState(pDict) : GetOCGVisible(pDict);
230 else if (CPDF_Array* pArray = pOCGObj->AsArray())
231 bItem = GetOCGVE(pArray, bFromConfig, nLevel + 1);
232 215
233 if (i == 1) { 216 if (csOperator != "Or" && csOperator != "And")
234 bValue = bItem; 217 return false;
218
219 bool bValue = false;
220 for (size_t i = 1; i < pExpression->GetCount(); i++) {
221 CPDF_Object* pOCGObj = pExpression->GetDirectObjectAt(1);
222 if (!pOCGObj)
223 continue;
224
225 bool bItem = false;
226 if (CPDF_Dictionary* pDict = pOCGObj->AsDictionary())
227 bItem = GetOCGVisible(pDict);
228 else if (CPDF_Array* pArray = pOCGObj->AsArray())
229 bItem = GetOCGVE(pArray, nLevel + 1);
230
231 if (i == 1) {
232 bValue = bItem;
233 } else {
234 if (csOperator == "Or") {
235 bValue = bValue || bItem;
235 } else { 236 } else {
236 if (csOperator == "Or") { 237 bValue = bValue && bItem;
237 bValue = bValue || bItem;
238 } else {
239 bValue = bValue && bItem;
240 }
241 } 238 }
242 } 239 }
243 return bValue;
244 } 240 }
245 return FALSE; 241 return bValue;
246 } 242 }
247 FX_BOOL CPDF_OCContext::LoadOCMDState(const CPDF_Dictionary* pOCMDDict, 243
248 FX_BOOL bFromConfig) { 244 bool CPDF_OCContext::LoadOCMDState(const CPDF_Dictionary* pOCMDDict) {
249 CPDF_Array* pVE = pOCMDDict->GetArrayBy("VE"); 245 CPDF_Array* pVE = pOCMDDict->GetArrayBy("VE");
250 if (pVE) { 246 if (pVE)
251 return GetOCGVE(pVE, bFromConfig); 247 return GetOCGVE(pVE, 0);
252 } 248
253 CFX_ByteString csP = pOCMDDict->GetStringBy("P", "AnyOn"); 249 CFX_ByteString csP = pOCMDDict->GetStringBy("P", "AnyOn");
254 CPDF_Object* pOCGObj = pOCMDDict->GetDirectObjectBy("OCGs"); 250 CPDF_Object* pOCGObj = pOCMDDict->GetDirectObjectBy("OCGs");
255 if (!pOCGObj) 251 if (!pOCGObj)
256 return TRUE; 252 return true;
253
257 if (const CPDF_Dictionary* pDict = pOCGObj->AsDictionary()) 254 if (const CPDF_Dictionary* pDict = pOCGObj->AsDictionary())
258 return bFromConfig ? LoadOCGState(pDict) : GetOCGVisible(pDict); 255 return GetOCGVisible(pDict);
259 256
260 CPDF_Array* pArray = pOCGObj->AsArray(); 257 CPDF_Array* pArray = pOCGObj->AsArray();
261 if (!pArray) 258 if (!pArray)
262 return TRUE; 259 return true;
263 260
264 FX_BOOL bState = FALSE; 261 bool bState = (csP == "AllOn" || csP == "AllOff");
265 if (csP == "AllOn" || csP == "AllOff") {
266 bState = TRUE;
267 }
268 for (size_t i = 0; i < pArray->GetCount(); i++) { 262 for (size_t i = 0; i < pArray->GetCount(); i++) {
269 FX_BOOL bItem = TRUE; 263 bool bItem = true;
270 CPDF_Dictionary* pItemDict = pArray->GetDictAt(i); 264 CPDF_Dictionary* pItemDict = pArray->GetDictAt(i);
271 if (pItemDict) 265 if (pItemDict)
272 bItem = bFromConfig ? LoadOCGState(pItemDict) : GetOCGVisible(pItemDict); 266 bItem = GetOCGVisible(pItemDict);
273 267
274 if ((csP == "AnyOn" && bItem) || (csP == "AnyOff" && !bItem)) 268 if ((csP == "AnyOn" && bItem) || (csP == "AnyOff" && !bItem))
275 return TRUE; 269 return true;
276 if ((csP == "AllOn" && !bItem) || (csP == "AllOff" && bItem)) 270 if ((csP == "AllOn" && !bItem) || (csP == "AllOff" && bItem))
277 return FALSE; 271 return false;
278 } 272 }
279 return bState; 273 return bState;
280 } 274 }
281 FX_BOOL CPDF_OCContext::CheckOCGVisible(const CPDF_Dictionary* pOCGDict) { 275
282 if (!pOCGDict) { 276 bool CPDF_OCContext::CheckOCGVisible(const CPDF_Dictionary* pOCGDict) {
283 return TRUE; 277 if (!pOCGDict)
284 } 278 return true;
279
285 CFX_ByteString csType = pOCGDict->GetStringBy("Type", "OCG"); 280 CFX_ByteString csType = pOCGDict->GetStringBy("Type", "OCG");
286 if (csType == "OCG") { 281 if (csType == "OCG")
287 return GetOCGVisible(pOCGDict); 282 return GetOCGVisible(pOCGDict);
288 } 283 return LoadOCMDState(pOCGDict);
289 return LoadOCMDState(pOCGDict, FALSE);
290 } 284 }
291 void CPDF_OCContext::ResetOCContext() {
292 m_OCGStates.clear();
293 }
OLDNEW
« no previous file with comments | « no previous file | core/fpdfdoc/include/fpdf_doc.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698