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

Side by Side Diff: core/src/fpdfapi/fpdf_page/fpdf_page_doc.cpp

Issue 1265503005: clang-format all pdfium code. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: sigh Created 5 years, 4 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
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 "../../../include/fpdfapi/fpdf_page.h" 7 #include "../../../include/fpdfapi/fpdf_page.h"
8 #include "../../../include/fpdfapi/fpdf_module.h" 8 #include "../../../include/fpdfapi/fpdf_module.h"
9 #include "../../../include/fdrm/fx_crypt.h" 9 #include "../../../include/fdrm/fx_crypt.h"
10 #include "../fpdf_font/font_int.h" 10 #include "../fpdf_font/font_int.h"
11 #include "pageint.h" 11 #include "pageint.h"
12 12
13 class CPDF_PageModule : public IPDF_PageModule 13 class CPDF_PageModule : public IPDF_PageModule {
14 { 14 public:
15 public: 15 CPDF_PageModule()
16 CPDF_PageModule() 16 : m_StockGrayCS(nullptr, PDFCS_DEVICEGRAY),
17 : m_StockGrayCS(nullptr, PDFCS_DEVICEGRAY), 17 m_StockRGBCS(nullptr, PDFCS_DEVICERGB),
18 m_StockRGBCS(nullptr, PDFCS_DEVICERGB), 18 m_StockCMYKCS(nullptr, PDFCS_DEVICECMYK),
19 m_StockCMYKCS(nullptr, PDFCS_DEVICECMYK), 19 m_StockPatternCS(nullptr) {}
20 m_StockPatternCS(nullptr) {} 20
21 21 private:
22 private: 22 ~CPDF_PageModule() override {}
23 ~CPDF_PageModule() override {} 23
24 24 CPDF_DocPageData* CreateDocData(CPDF_Document* pDoc) override {
25 CPDF_DocPageData* CreateDocData(CPDF_Document* pDoc) override 25 return new CPDF_DocPageData(pDoc);
26 { 26 }
27 return new CPDF_DocPageData(pDoc); 27
28 } 28 void ReleaseDoc(CPDF_Document* pDoc) override;
29 29 void ClearDoc(CPDF_Document* pDoc) override;
30 void ReleaseDoc(CPDF_Document* pDoc) override; 30
31 void ClearDoc(CPDF_Document* pDoc) override; 31 CPDF_FontGlobals* GetFontGlobals() override { return &m_FontGlobals; }
32 32
33 CPDF_FontGlobals* GetFontGlobals() override 33 void ClearStockFont(CPDF_Document* pDoc) override {
34 { 34 m_FontGlobals.Clear(pDoc);
35 return &m_FontGlobals; 35 }
36 } 36
37 37 CPDF_ColorSpace* GetStockCS(int family) override;
38 void ClearStockFont(CPDF_Document* pDoc) override 38 void NotifyCJKAvailable() override;
39 { 39
40 m_FontGlobals.Clear(pDoc); 40 CPDF_FontGlobals m_FontGlobals;
41 } 41 CPDF_DeviceCS m_StockGrayCS;
42 42 CPDF_DeviceCS m_StockRGBCS;
43 CPDF_ColorSpace* GetStockCS(int family) override; 43 CPDF_DeviceCS m_StockCMYKCS;
44 void NotifyCJKAvailable() override; 44 CPDF_PatternCS m_StockPatternCS;
45
46 CPDF_FontGlobals m_FontGlobals;
47 CPDF_DeviceCS m_StockGrayCS;
48 CPDF_DeviceCS m_StockRGBCS;
49 CPDF_DeviceCS m_StockCMYKCS;
50 CPDF_PatternCS m_StockPatternCS;
51 }; 45 };
52 46
53 CPDF_ColorSpace* CPDF_PageModule::GetStockCS(int family) 47 CPDF_ColorSpace* CPDF_PageModule::GetStockCS(int family) {
54 { 48 if (family == PDFCS_DEVICEGRAY) {
55 if (family == PDFCS_DEVICEGRAY) { 49 return &m_StockGrayCS;
56 return &m_StockGrayCS; 50 }
57 } 51 if (family == PDFCS_DEVICERGB) {
58 if (family == PDFCS_DEVICERGB) { 52 return &m_StockRGBCS;
59 return &m_StockRGBCS; 53 }
60 } 54 if (family == PDFCS_DEVICECMYK) {
61 if (family == PDFCS_DEVICECMYK) { 55 return &m_StockCMYKCS;
62 return &m_StockCMYKCS; 56 }
63 } 57 if (family == PDFCS_PATTERN) {
64 if (family == PDFCS_PATTERN) { 58 return &m_StockPatternCS;
65 return &m_StockPatternCS; 59 }
66 } 60 return NULL;
61 }
62
63 void CPDF_ModuleMgr::InitPageModule() {
64 m_pPageModule.reset(new CPDF_PageModule);
65 }
66
67 void CPDF_PageModule::ReleaseDoc(CPDF_Document* pDoc) {
68 delete pDoc->GetPageData();
69 }
70 void CPDF_PageModule::ClearDoc(CPDF_Document* pDoc) {
71 pDoc->GetPageData()->Clear(FALSE);
72 }
73 void CPDF_PageModule::NotifyCJKAvailable() {
74 m_FontGlobals.m_CMapManager.ReloadAll();
75 }
76
77 CPDF_Font* CPDF_Document::LoadFont(CPDF_Dictionary* pFontDict) {
78 ASSERT(pFontDict);
79 return GetValidatePageData()->GetFont(pFontDict, FALSE);
80 }
81
82 CPDF_StreamAcc* CPDF_Document::LoadFontFile(CPDF_Stream* pStream) {
83 return GetValidatePageData()->GetFontFileStreamAcc(pStream);
84 }
85
86 CPDF_ColorSpace* _CSFromName(const CFX_ByteString& name);
87 CPDF_ColorSpace* CPDF_Document::LoadColorSpace(CPDF_Object* pCSObj,
88 CPDF_Dictionary* pResources) {
89 return GetValidatePageData()->GetColorSpace(pCSObj, pResources);
90 }
91 CPDF_Pattern* CPDF_Document::LoadPattern(CPDF_Object* pPatternObj,
92 FX_BOOL bShading,
93 const CFX_AffineMatrix* matrix) {
94 return GetValidatePageData()->GetPattern(pPatternObj, bShading, matrix);
95 }
96 CPDF_IccProfile* CPDF_Document::LoadIccProfile(CPDF_Stream* pStream) {
97 return GetValidatePageData()->GetIccProfile(pStream);
98 }
99 CPDF_Image* CPDF_Document::LoadImageF(CPDF_Object* pObj) {
100 if (!pObj) {
67 return NULL; 101 return NULL;
68 } 102 }
69 103 FXSYS_assert(pObj->GetObjNum());
70 void CPDF_ModuleMgr::InitPageModule() 104 return GetValidatePageData()->GetImage(pObj);
71 { 105 }
72 m_pPageModule.reset(new CPDF_PageModule); 106 void CPDF_Document::RemoveColorSpaceFromPageData(CPDF_Object* pCSObj) {
73 } 107 if (!pCSObj) {
74 108 return;
75 void CPDF_PageModule::ReleaseDoc(CPDF_Document* pDoc) 109 }
76 { 110 GetPageData()->ReleaseColorSpace(pCSObj);
77 delete pDoc->GetPageData();
78 }
79 void CPDF_PageModule::ClearDoc(CPDF_Document* pDoc)
80 {
81 pDoc->GetPageData()->Clear(FALSE);
82 }
83 void CPDF_PageModule::NotifyCJKAvailable()
84 {
85 m_FontGlobals.m_CMapManager.ReloadAll();
86 }
87
88 CPDF_Font* CPDF_Document::LoadFont(CPDF_Dictionary* pFontDict)
89 {
90 ASSERT(pFontDict);
91 return GetValidatePageData()->GetFont(pFontDict, FALSE);
92 }
93
94 CPDF_StreamAcc* CPDF_Document::LoadFontFile(CPDF_Stream* pStream)
95 {
96 return GetValidatePageData()->GetFontFileStreamAcc(pStream);
97 }
98
99 CPDF_ColorSpace* _CSFromName(const CFX_ByteString& name);
100 CPDF_ColorSpace* CPDF_Document::LoadColorSpace(CPDF_Object* pCSObj, CPDF_Diction ary* pResources)
101 {
102 return GetValidatePageData()->GetColorSpace(pCSObj, pResources);
103 }
104 CPDF_Pattern* CPDF_Document::LoadPattern(CPDF_Object* pPatternObj, FX_BOOL bShad ing, const CFX_AffineMatrix* matrix)
105 {
106 return GetValidatePageData()->GetPattern(pPatternObj, bShading, matrix);
107 }
108 CPDF_IccProfile* CPDF_Document::LoadIccProfile(CPDF_Stream* pStream)
109 {
110 return GetValidatePageData()->GetIccProfile(pStream);
111 }
112 CPDF_Image* CPDF_Document::LoadImageF(CPDF_Object* pObj)
113 {
114 if (!pObj) {
115 return NULL;
116 }
117 FXSYS_assert(pObj->GetObjNum());
118 return GetValidatePageData()->GetImage(pObj);
119 }
120 void CPDF_Document::RemoveColorSpaceFromPageData(CPDF_Object* pCSObj)
121 {
122 if (!pCSObj) {
123 return;
124 }
125 GetPageData()->ReleaseColorSpace(pCSObj);
126 } 111 }
127 CPDF_DocPageData::CPDF_DocPageData(CPDF_Document* pPDFDoc) 112 CPDF_DocPageData::CPDF_DocPageData(CPDF_Document* pPDFDoc)
128 : m_pPDFDoc(pPDFDoc), 113 : m_pPDFDoc(pPDFDoc), m_bForceClear(FALSE) {}
129 m_bForceClear(FALSE) 114
130 { 115 CPDF_DocPageData::~CPDF_DocPageData() {
131 } 116 Clear(FALSE);
132 117 Clear(TRUE);
133 CPDF_DocPageData::~CPDF_DocPageData() 118
134 { 119 for (auto& it : m_PatternMap)
135 Clear(FALSE); 120 delete it.second;
136 Clear(TRUE); 121 m_PatternMap.clear();
137 122
138 for (auto& it : m_PatternMap) 123 for (auto& it : m_FontMap)
139 delete it.second; 124 delete it.second;
140 m_PatternMap.clear(); 125 m_FontMap.clear();
141 126
142 for (auto& it : m_FontMap) 127 for (auto& it : m_ColorSpaceMap)
143 delete it.second; 128 delete it.second;
144 m_FontMap.clear(); 129 m_ColorSpaceMap.clear();
145 130 }
146 for (auto& it : m_ColorSpaceMap) 131
147 delete it.second; 132 void CPDF_DocPageData::Clear(FX_BOOL bForceRelease) {
148 m_ColorSpaceMap.clear(); 133 m_bForceClear = bForceRelease;
149 } 134
150 135 for (auto& it : m_PatternMap) {
151 void CPDF_DocPageData::Clear(FX_BOOL bForceRelease) 136 CPDF_CountedPattern* ptData = it.second;
152 { 137 if (!ptData->get())
153 m_bForceClear = bForceRelease; 138 continue;
154 139
155 for (auto& it : m_PatternMap) { 140 if (bForceRelease || ptData->use_count() < 2) {
156 CPDF_CountedPattern* ptData = it.second; 141 ptData->get()->SetForceClear(bForceRelease);
157 if (!ptData->get()) 142 ptData->clear();
158 continue; 143 }
159 144 }
160 if (bForceRelease || ptData->use_count() < 2) { 145
161 ptData->get()->SetForceClear(bForceRelease); 146 for (auto& it : m_FontMap) {
162 ptData->clear(); 147 CPDF_CountedFont* fontData = it.second;
148 if (!fontData->get())
149 continue;
150
151 if (bForceRelease || fontData->use_count() < 2) {
152 fontData->clear();
153 }
154 }
155
156 for (auto& it : m_ColorSpaceMap) {
157 CPDF_CountedColorSpace* csData = it.second;
158 if (!csData->get())
159 continue;
160
161 if (bForceRelease || csData->use_count() < 2) {
162 csData->get()->ReleaseCS();
163 csData->reset(nullptr);
164 }
165 }
166
167 for (auto it = m_IccProfileMap.begin(); it != m_IccProfileMap.end();) {
168 auto curr_it = it++;
169 CPDF_CountedIccProfile* ipData = curr_it->second;
170 if (!ipData->get())
171 continue;
172
173 if (bForceRelease || ipData->use_count() < 2) {
174 CPDF_Stream* ipKey = curr_it->first;
175 FX_POSITION pos2 = m_HashProfileMap.GetStartPosition();
176 while (pos2) {
177 CFX_ByteString bsKey;
178 CPDF_Stream* pFindStream = nullptr;
179 m_HashProfileMap.GetNextAssoc(pos2, bsKey, (void*&)pFindStream);
180 if (ipKey == pFindStream) {
181 m_HashProfileMap.RemoveKey(bsKey);
182 break;
163 } 183 }
164 } 184 }
165 185 delete ipData->get();
166 for (auto& it : m_FontMap) { 186 delete ipData;
167 CPDF_CountedFont* fontData = it.second; 187 m_IccProfileMap.erase(curr_it);
168 if (!fontData->get()) 188 }
169 continue; 189 }
170 190
171 if (bForceRelease || fontData->use_count() < 2) { 191 for (auto it = m_FontFileMap.begin(); it != m_FontFileMap.end();) {
172 fontData->clear(); 192 auto curr_it = it++;
173 } 193 CPDF_CountedStreamAcc* ftData = curr_it->second;
174 } 194 if (!ftData->get())
175 195 continue;
176 for (auto& it : m_ColorSpaceMap) { 196
177 CPDF_CountedColorSpace* csData = it.second; 197 if (bForceRelease || ftData->use_count() < 2) {
178 if (!csData->get()) 198 delete ftData->get();
179 continue; 199 delete ftData;
180 200 m_FontFileMap.erase(curr_it);
181 if (bForceRelease || csData->use_count() < 2) { 201 }
182 csData->get()->ReleaseCS(); 202 }
183 csData->reset(nullptr); 203
184 } 204 for (auto it = m_ImageMap.begin(); it != m_ImageMap.end();) {
185 } 205 auto curr_it = it++;
186 206 CPDF_CountedImage* imageData = curr_it->second;
187 for (auto it = m_IccProfileMap.begin(); it != m_IccProfileMap.end();) { 207 if (!imageData->get())
188 auto curr_it = it++; 208 continue;
189 CPDF_CountedIccProfile* ipData = curr_it->second; 209
190 if (!ipData->get()) 210 if (bForceRelease || imageData->use_count() < 2) {
191 continue; 211 delete imageData->get();
192 212 delete imageData;
193 if (bForceRelease || ipData->use_count() < 2) { 213 m_ImageMap.erase(curr_it);
194 CPDF_Stream* ipKey = curr_it->first; 214 }
195 FX_POSITION pos2 = m_HashProfileMap.GetStartPosition(); 215 }
196 while (pos2) { 216 }
197 CFX_ByteString bsKey; 217
198 CPDF_Stream* pFindStream = nullptr; 218 CPDF_Font* CPDF_DocPageData::GetFont(CPDF_Dictionary* pFontDict,
199 m_HashProfileMap.GetNextAssoc(pos2, bsKey, (void*&)pFindStream); 219 FX_BOOL findOnly) {
200 if (ipKey == pFindStream) { 220 if (!pFontDict) {
201 m_HashProfileMap.RemoveKey(bsKey); 221 return NULL;
202 break; 222 }
203 } 223 if (findOnly) {
204 }
205 delete ipData->get();
206 delete ipData;
207 m_IccProfileMap.erase(curr_it);
208 }
209 }
210
211 for (auto it = m_FontFileMap.begin(); it != m_FontFileMap.end();) {
212 auto curr_it = it++;
213 CPDF_CountedStreamAcc* ftData = curr_it->second;
214 if (!ftData->get())
215 continue;
216
217 if (bForceRelease || ftData->use_count() < 2) {
218 delete ftData->get();
219 delete ftData;
220 m_FontFileMap.erase(curr_it);
221 }
222 }
223
224 for (auto it = m_ImageMap.begin(); it != m_ImageMap.end();) {
225 auto curr_it = it++;
226 CPDF_CountedImage* imageData = curr_it->second;
227 if (!imageData->get())
228 continue;
229
230 if (bForceRelease || imageData->use_count() < 2) {
231 delete imageData->get();
232 delete imageData;
233 m_ImageMap.erase(curr_it);
234 }
235 }
236 }
237
238 CPDF_Font* CPDF_DocPageData::GetFont(CPDF_Dictionary* pFontDict, FX_BOOL findOnl y)
239 {
240 if (!pFontDict) {
241 return NULL;
242 }
243 if (findOnly) {
244 auto it = m_FontMap.find(pFontDict);
245 if (it != m_FontMap.end() && it->second->get()) {
246 return it->second->AddRef();
247 }
248 return nullptr;
249 }
250
251 CPDF_CountedFont* fontData = nullptr;
252 auto it = m_FontMap.find(pFontDict); 224 auto it = m_FontMap.find(pFontDict);
253 if (it != m_FontMap.end()) { 225 if (it != m_FontMap.end() && it->second->get()) {
254 fontData = it->second; 226 return it->second->AddRef();
255 if (fontData->get()) { 227 }
256 return fontData->AddRef(); 228 return nullptr;
257 } 229 }
258 } 230
259 231 CPDF_CountedFont* fontData = nullptr;
260 CPDF_Font* pFont = CPDF_Font::CreateFontF(m_pPDFDoc, pFontDict); 232 auto it = m_FontMap.find(pFontDict);
261 if (!pFont) { 233 if (it != m_FontMap.end()) {
262 return nullptr; 234 fontData = it->second;
263 } 235 if (fontData->get()) {
264 if (!fontData) { 236 return fontData->AddRef();
265 fontData = new CPDF_CountedFont(pFont); 237 }
266 m_FontMap[pFontDict] = fontData; 238 }
267 } else { 239
268 fontData->reset(pFont); 240 CPDF_Font* pFont = CPDF_Font::CreateFontF(m_pPDFDoc, pFontDict);
269 } 241 if (!pFont) {
242 return nullptr;
243 }
244 if (!fontData) {
245 fontData = new CPDF_CountedFont(pFont);
246 m_FontMap[pFontDict] = fontData;
247 } else {
248 fontData->reset(pFont);
249 }
250 return fontData->AddRef();
251 }
252
253 CPDF_Font* CPDF_DocPageData::GetStandardFont(const CFX_ByteStringC& fontName,
254 CPDF_FontEncoding* pEncoding) {
255 if (fontName.IsEmpty())
256 return nullptr;
257
258 for (auto& it : m_FontMap) {
259 CPDF_CountedFont* fontData = it.second;
260 CPDF_Font* pFont = fontData->get();
261 if (!pFont)
262 continue;
263 if (pFont->GetBaseFont() != fontName)
264 continue;
265 if (pFont->IsEmbedded())
266 continue;
267 if (pFont->GetFontType() != PDFFONT_TYPE1)
268 continue;
269 if (pFont->GetFontDict()->KeyExist(FX_BSTRC("Widths")))
270 continue;
271
272 CPDF_Type1Font* pT1Font = pFont->GetType1Font();
273 if (pEncoding && !pT1Font->GetEncoding()->IsIdentical(pEncoding))
274 continue;
275
270 return fontData->AddRef(); 276 return fontData->AddRef();
271 } 277 }
272 278
273 CPDF_Font* CPDF_DocPageData::GetStandardFont(const CFX_ByteStringC& fontName, CP DF_FontEncoding* pEncoding) 279 CPDF_Dictionary* pDict = new CPDF_Dictionary;
274 { 280 pDict->SetAtName(FX_BSTRC("Type"), FX_BSTRC("Font"));
275 if (fontName.IsEmpty()) 281 pDict->SetAtName(FX_BSTRC("Subtype"), FX_BSTRC("Type1"));
276 return nullptr; 282 pDict->SetAtName(FX_BSTRC("BaseFont"), fontName);
277 283 if (pEncoding) {
278 for (auto& it : m_FontMap) { 284 pDict->SetAt(FX_BSTRC("Encoding"), pEncoding->Realize());
279 CPDF_CountedFont* fontData = it.second; 285 }
280 CPDF_Font* pFont = fontData->get(); 286 m_pPDFDoc->AddIndirectObject(pDict);
281 if (!pFont) 287 CPDF_Font* pFont = CPDF_Font::CreateFontF(m_pPDFDoc, pDict);
282 continue; 288 if (!pFont) {
283 if (pFont->GetBaseFont() != fontName) 289 return nullptr;
284 continue; 290 }
285 if (pFont->IsEmbedded()) 291 CPDF_CountedFont* fontData = new CPDF_CountedFont(pFont);
286 continue; 292 m_FontMap[pDict] = fontData;
287 if (pFont->GetFontType() != PDFFONT_TYPE1) 293 return fontData->AddRef();
288 continue; 294 }
289 if (pFont->GetFontDict()->KeyExist(FX_BSTRC("Widths"))) 295
290 continue; 296 void CPDF_DocPageData::ReleaseFont(CPDF_Dictionary* pFontDict) {
291 297 if (!pFontDict)
292 CPDF_Type1Font* pT1Font = pFont->GetType1Font(); 298 return;
293 if (pEncoding && !pT1Font->GetEncoding()->IsIdentical(pEncoding)) 299
294 continue; 300 auto it = m_FontMap.find(pFontDict);
295 301 if (it == m_FontMap.end())
296 return fontData->AddRef(); 302 return;
297 } 303
298 304 CPDF_CountedFont* fontData = it->second;
299 CPDF_Dictionary* pDict = new CPDF_Dictionary; 305 if (fontData->get()) {
300 pDict->SetAtName(FX_BSTRC("Type"), FX_BSTRC("Font")); 306 fontData->RemoveRef();
301 pDict->SetAtName(FX_BSTRC("Subtype"), FX_BSTRC("Type1")); 307 if (fontData->use_count() == 0) {
302 pDict->SetAtName(FX_BSTRC("BaseFont"), fontName); 308 fontData->clear();
303 if (pEncoding) { 309 }
304 pDict->SetAt(FX_BSTRC("Encoding"), pEncoding->Realize()); 310 }
305 } 311 }
306 m_pPDFDoc->AddIndirectObject(pDict); 312
307 CPDF_Font* pFont = CPDF_Font::CreateFontF(m_pPDFDoc, pDict); 313 CPDF_ColorSpace* CPDF_DocPageData::GetColorSpace(CPDF_Object* pCSObj,
308 if (!pFont) { 314 CPDF_Dictionary* pResources) {
309 return nullptr; 315 if (!pCSObj) {
310 } 316 return NULL;
311 CPDF_CountedFont* fontData = new CPDF_CountedFont(pFont); 317 }
312 m_FontMap[pDict] = fontData; 318 if (pCSObj->GetType() == PDFOBJ_NAME) {
313 return fontData->AddRef(); 319 CFX_ByteString name = pCSObj->GetConstString();
314 } 320 CPDF_ColorSpace* pCS = _CSFromName(name);
315 321 if (!pCS && pResources) {
316 void CPDF_DocPageData::ReleaseFont(CPDF_Dictionary* pFontDict) 322 CPDF_Dictionary* pList = pResources->GetDict(FX_BSTRC("ColorSpace"));
317 { 323 if (pList) {
318 if (!pFontDict) 324 pCSObj = pList->GetElementValue(name);
319 return; 325 return GetColorSpace(pCSObj, NULL);
320 326 }
321 auto it = m_FontMap.find(pFontDict); 327 }
322 if (it == m_FontMap.end()) 328 if (pCS == NULL || pResources == NULL) {
323 return; 329 return pCS;
324 330 }
325 CPDF_CountedFont* fontData = it->second; 331 CPDF_Dictionary* pColorSpaces = pResources->GetDict(FX_BSTRC("ColorSpace"));
326 if (fontData->get()) { 332 if (pColorSpaces == NULL) {
327 fontData->RemoveRef(); 333 return pCS;
328 if (fontData->use_count() == 0) { 334 }
329 fontData->clear(); 335 CPDF_Object* pDefaultCS = NULL;
330 } 336 switch (pCS->GetFamily()) {
331 } 337 case PDFCS_DEVICERGB:
332 } 338 pDefaultCS = pColorSpaces->GetElementValue(FX_BSTRC("DefaultRGB"));
333 339 break;
334 CPDF_ColorSpace* CPDF_DocPageData::GetColorSpace(CPDF_Object* pCSObj, CPDF_Dicti onary* pResources) 340 case PDFCS_DEVICEGRAY:
335 { 341 pDefaultCS = pColorSpaces->GetElementValue(FX_BSTRC("DefaultGray"));
336 if (!pCSObj) { 342 break;
337 return NULL; 343 case PDFCS_DEVICECMYK:
338 } 344 pDefaultCS = pColorSpaces->GetElementValue(FX_BSTRC("DefaultCMYK"));
339 if (pCSObj->GetType() == PDFOBJ_NAME) { 345 break;
340 CFX_ByteString name = pCSObj->GetConstString(); 346 }
341 CPDF_ColorSpace* pCS = _CSFromName(name); 347 if (pDefaultCS == NULL) {
342 if (!pCS && pResources) { 348 return pCS;
343 CPDF_Dictionary* pList = pResources->GetDict(FX_BSTRC("ColorSpace")) ; 349 }
344 if (pList) { 350 return GetColorSpace(pDefaultCS, NULL);
345 pCSObj = pList->GetElementValue(name); 351 }
346 return GetColorSpace(pCSObj, NULL); 352
347 } 353 if (pCSObj->GetType() != PDFOBJ_ARRAY)
348 } 354 return nullptr;
349 if (pCS == NULL || pResources == NULL) { 355 CPDF_Array* pArray = (CPDF_Array*)pCSObj;
350 return pCS; 356 if (pArray->GetCount() == 0)
351 } 357 return nullptr;
352 CPDF_Dictionary* pColorSpaces = pResources->GetDict(FX_BSTRC("ColorSpace ")); 358 if (pArray->GetCount() == 1)
353 if (pColorSpaces == NULL) { 359 return GetColorSpace(pArray->GetElementValue(0), pResources);
354 return pCS; 360
355 } 361 CPDF_CountedColorSpace* csData = nullptr;
356 CPDF_Object* pDefaultCS = NULL; 362 auto it = m_ColorSpaceMap.find(pCSObj);
357 switch (pCS->GetFamily()) { 363 if (it != m_ColorSpaceMap.end()) {
358 case PDFCS_DEVICERGB: 364 csData = it->second;
359 pDefaultCS = pColorSpaces->GetElementValue(FX_BSTRC("DefaultRGB" ));
360 break;
361 case PDFCS_DEVICEGRAY:
362 pDefaultCS = pColorSpaces->GetElementValue(FX_BSTRC("DefaultGray "));
363 break;
364 case PDFCS_DEVICECMYK:
365 pDefaultCS = pColorSpaces->GetElementValue(FX_BSTRC("DefaultCMYK "));
366 break;
367 }
368 if (pDefaultCS == NULL) {
369 return pCS;
370 }
371 return GetColorSpace(pDefaultCS, NULL);
372 }
373
374 if (pCSObj->GetType() != PDFOBJ_ARRAY)
375 return nullptr;
376 CPDF_Array* pArray = (CPDF_Array*)pCSObj;
377 if (pArray->GetCount() == 0)
378 return nullptr;
379 if (pArray->GetCount() == 1)
380 return GetColorSpace(pArray->GetElementValue(0), pResources);
381
382 CPDF_CountedColorSpace* csData = nullptr;
383 auto it = m_ColorSpaceMap.find(pCSObj);
384 if (it != m_ColorSpaceMap.end()) {
385 csData = it->second;
386 if (csData->get()) {
387 return csData->AddRef();
388 }
389 }
390
391 CPDF_ColorSpace* pCS = CPDF_ColorSpace::Load(m_pPDFDoc, pArray);
392 if (!pCS)
393 return nullptr;
394
395 if (!csData) {
396 csData = new CPDF_CountedColorSpace(pCS);
397 m_ColorSpaceMap[pCSObj] = csData;
398 } else {
399 csData->reset(pCS);
400 }
401 return csData->AddRef();
402 }
403
404 CPDF_ColorSpace* CPDF_DocPageData::GetCopiedColorSpace(CPDF_Object* pCSObj)
405 {
406 if (!pCSObj)
407 return nullptr;
408
409 auto it = m_ColorSpaceMap.find(pCSObj);
410 if (it != m_ColorSpaceMap.end())
411 return it->second->AddRef();
412
413 return nullptr;
414 }
415
416 void CPDF_DocPageData::ReleaseColorSpace(CPDF_Object* pColorSpace)
417 {
418 if (!pColorSpace)
419 return;
420
421 auto it = m_ColorSpaceMap.find(pColorSpace);
422 if (it == m_ColorSpaceMap.end())
423 return;
424
425 CPDF_CountedColorSpace* csData = it->second;
426 if (csData->get()) { 365 if (csData->get()) {
427 csData->RemoveRef(); 366 return csData->AddRef();
428 if (csData->use_count() == 0) { 367 }
429 csData->get()->ReleaseCS(); 368 }
430 csData->reset(nullptr); 369
431 } 370 CPDF_ColorSpace* pCS = CPDF_ColorSpace::Load(m_pPDFDoc, pArray);
432 } 371 if (!pCS)
433 } 372 return nullptr;
434 373
435 CPDF_Pattern* CPDF_DocPageData::GetPattern(CPDF_Object* pPatternObj, FX_BOOL bSh ading, const CFX_AffineMatrix* matrix) 374 if (!csData) {
436 { 375 csData = new CPDF_CountedColorSpace(pCS);
437 if (!pPatternObj) 376 m_ColorSpaceMap[pCSObj] = csData;
438 return nullptr; 377 } else {
439 378 csData->reset(pCS);
440 CPDF_CountedPattern* ptData = nullptr; 379 }
441 auto it = m_PatternMap.find(pPatternObj); 380 return csData->AddRef();
442 if (it != m_PatternMap.end()) { 381 }
443 ptData = it->second; 382
444 if (ptData->get()) { 383 CPDF_ColorSpace* CPDF_DocPageData::GetCopiedColorSpace(CPDF_Object* pCSObj) {
445 return ptData->AddRef(); 384 if (!pCSObj)
446 } 385 return nullptr;
447 } 386
448 CPDF_Pattern* pPattern = nullptr; 387 auto it = m_ColorSpaceMap.find(pCSObj);
449 if (bShading) { 388 if (it != m_ColorSpaceMap.end())
450 pPattern = new CPDF_ShadingPattern(m_pPDFDoc, pPatternObj, bShading, mat rix); 389 return it->second->AddRef();
451 } else { 390
452 CPDF_Dictionary* pDict = pPatternObj ? pPatternObj->GetDict() : nullptr; 391 return nullptr;
453 if (pDict) { 392 }
454 int type = pDict->GetInteger(FX_BSTRC("PatternType")); 393
455 if (type == 1) { 394 void CPDF_DocPageData::ReleaseColorSpace(CPDF_Object* pColorSpace) {
456 pPattern = new CPDF_TilingPattern(m_pPDFDoc, pPatternObj, matrix ); 395 if (!pColorSpace)
457 } else if (type == 2) { 396 return;
458 pPattern = new CPDF_ShadingPattern(m_pPDFDoc, pPatternObj, FALSE , matrix); 397
459 } 398 auto it = m_ColorSpaceMap.find(pColorSpace);
460 } 399 if (it == m_ColorSpaceMap.end())
461 } 400 return;
462 if (!pPattern) 401
463 return nullptr; 402 CPDF_CountedColorSpace* csData = it->second;
464 403 if (csData->get()) {
465 if (!ptData) { 404 csData->RemoveRef();
466 ptData = new CPDF_CountedPattern(pPattern); 405 if (csData->use_count() == 0) {
467 m_PatternMap[pPatternObj] = ptData; 406 csData->get()->ReleaseCS();
468 } else { 407 csData->reset(nullptr);
469 ptData->reset(pPattern); 408 }
470 } 409 }
471 return ptData->AddRef(); 410 }
472 } 411
473 412 CPDF_Pattern* CPDF_DocPageData::GetPattern(CPDF_Object* pPatternObj,
474 void CPDF_DocPageData::ReleasePattern(CPDF_Object* pPatternObj) 413 FX_BOOL bShading,
475 { 414 const CFX_AffineMatrix* matrix) {
476 if (!pPatternObj) 415 if (!pPatternObj)
477 return; 416 return nullptr;
478 417
479 auto it = m_PatternMap.find(pPatternObj); 418 CPDF_CountedPattern* ptData = nullptr;
480 if (it == m_PatternMap.end()) 419 auto it = m_PatternMap.find(pPatternObj);
481 return; 420 if (it != m_PatternMap.end()) {
482 421 ptData = it->second;
483 CPDF_CountedPattern* ptData = it->second;
484 if (ptData->get()) { 422 if (ptData->get()) {
485 ptData->RemoveRef(); 423 return ptData->AddRef();
486 if (ptData->use_count() == 0) { 424 }
487 ptData->clear(); 425 }
488 } 426 CPDF_Pattern* pPattern = nullptr;
489 } 427 if (bShading) {
490 } 428 pPattern =
491 429 new CPDF_ShadingPattern(m_pPDFDoc, pPatternObj, bShading, matrix);
492 CPDF_Image* CPDF_DocPageData::GetImage(CPDF_Object* pImageStream) 430 } else {
493 { 431 CPDF_Dictionary* pDict = pPatternObj ? pPatternObj->GetDict() : nullptr;
494 if (!pImageStream) 432 if (pDict) {
495 return nullptr; 433 int type = pDict->GetInteger(FX_BSTRC("PatternType"));
496 434 if (type == 1) {
497 const FX_DWORD dwImageObjNum = pImageStream->GetObjNum(); 435 pPattern = new CPDF_TilingPattern(m_pPDFDoc, pPatternObj, matrix);
498 auto it = m_ImageMap.find(dwImageObjNum); 436 } else if (type == 2) {
499 if (it != m_ImageMap.end()) { 437 pPattern =
500 return it->second->AddRef(); 438 new CPDF_ShadingPattern(m_pPDFDoc, pPatternObj, FALSE, matrix);
501 } 439 }
502 440 }
503 CPDF_Image* pImage = new CPDF_Image(m_pPDFDoc); 441 }
504 pImage->LoadImageF((CPDF_Stream*)pImageStream, FALSE); 442 if (!pPattern)
505 443 return nullptr;
506 CPDF_CountedImage* imageData = new CPDF_CountedImage(pImage); 444
507 m_ImageMap[dwImageObjNum] = imageData; 445 if (!ptData) {
508 return imageData->AddRef(); 446 ptData = new CPDF_CountedPattern(pPattern);
509 } 447 m_PatternMap[pPatternObj] = ptData;
510 448 } else {
511 void CPDF_DocPageData::ReleaseImage(CPDF_Object* pImageStream) 449 ptData->reset(pPattern);
512 { 450 }
513 if (!pImageStream || !pImageStream->GetObjNum()) 451 return ptData->AddRef();
514 return; 452 }
515 453
516 auto it = m_ImageMap.find(pImageStream->GetObjNum()); 454 void CPDF_DocPageData::ReleasePattern(CPDF_Object* pPatternObj) {
517 if (it == m_ImageMap.end()) 455 if (!pPatternObj)
518 return; 456 return;
519 457
520 CPDF_CountedImage* image = it->second; 458 auto it = m_PatternMap.find(pPatternObj);
521 if (!image) 459 if (it == m_PatternMap.end())
522 return; 460 return;
523 461
524 image->RemoveRef(); 462 CPDF_CountedPattern* ptData = it->second;
525 if (image->use_count() == 0) { 463 if (ptData->get()) {
526 delete image->get(); 464 ptData->RemoveRef();
527 delete image; 465 if (ptData->use_count() == 0) {
528 m_ImageMap.erase(it); 466 ptData->clear();
529 } 467 }
530 } 468 }
531 469 }
532 CPDF_IccProfile* CPDF_DocPageData::GetIccProfile(CPDF_Stream* pIccProfileStream) 470
533 { 471 CPDF_Image* CPDF_DocPageData::GetImage(CPDF_Object* pImageStream) {
534 if (!pIccProfileStream) 472 if (!pImageStream)
535 return NULL; 473 return nullptr;
536 474
537 auto it = m_IccProfileMap.find(pIccProfileStream); 475 const FX_DWORD dwImageObjNum = pImageStream->GetObjNum();
538 if (it != m_IccProfileMap.end()) { 476 auto it = m_ImageMap.find(dwImageObjNum);
539 return it->second->AddRef(); 477 if (it != m_ImageMap.end()) {
540 } 478 return it->second->AddRef();
541 479 }
542 CPDF_StreamAcc stream; 480
543 stream.LoadAllData(pIccProfileStream, FALSE); 481 CPDF_Image* pImage = new CPDF_Image(m_pPDFDoc);
544 uint8_t digest[20]; 482 pImage->LoadImageF((CPDF_Stream*)pImageStream, FALSE);
545 CPDF_Stream* pCopiedStream = nullptr; 483
546 CRYPT_SHA1Generate(stream.GetData(), stream.GetSize(), digest); 484 CPDF_CountedImage* imageData = new CPDF_CountedImage(pImage);
547 if (m_HashProfileMap.Lookup(CFX_ByteStringC(digest, 20), (void*&)pCopiedStre am)) { 485 m_ImageMap[dwImageObjNum] = imageData;
548 auto it_copied_stream = m_IccProfileMap.find(pCopiedStream); 486 return imageData->AddRef();
549 return it_copied_stream->second->AddRef(); 487 }
550 } 488
551 CPDF_IccProfile* pProfile = new CPDF_IccProfile(stream.GetData(), stream.Get Size()); 489 void CPDF_DocPageData::ReleaseImage(CPDF_Object* pImageStream) {
552 CPDF_CountedIccProfile* ipData = new CPDF_CountedIccProfile(pProfile); 490 if (!pImageStream || !pImageStream->GetObjNum())
553 m_IccProfileMap[pIccProfileStream] = ipData; 491 return;
554 m_HashProfileMap.SetAt(CFX_ByteStringC(digest, 20), pIccProfileStream); 492
555 return ipData->AddRef(); 493 auto it = m_ImageMap.find(pImageStream->GetObjNum());
556 } 494 if (it == m_ImageMap.end())
557 495 return;
558 void CPDF_DocPageData::ReleaseIccProfile(CPDF_IccProfile* pIccProfile) 496
559 { 497 CPDF_CountedImage* image = it->second;
560 ASSERT(pIccProfile); 498 if (!image)
561 499 return;
562 for (auto it = m_IccProfileMap.begin(); it != m_IccProfileMap.end(); ++it) { 500
563 CPDF_CountedIccProfile* profile = it->second; 501 image->RemoveRef();
564 if (profile->get() != pIccProfile) 502 if (image->use_count() == 0) {
565 continue; 503 delete image->get();
566 504 delete image;
567 profile->RemoveRef(); 505 m_ImageMap.erase(it);
568 if (profile->use_count() == 0) { 506 }
569 delete profile->get(); 507 }
570 delete profile; 508
571 m_IccProfileMap.erase(it); 509 CPDF_IccProfile* CPDF_DocPageData::GetIccProfile(
572 return; 510 CPDF_Stream* pIccProfileStream) {
573 } 511 if (!pIccProfileStream)
574 } 512 return NULL;
575 } 513
576 514 auto it = m_IccProfileMap.find(pIccProfileStream);
577 CPDF_StreamAcc* CPDF_DocPageData::GetFontFileStreamAcc(CPDF_Stream* pFontStream) 515 if (it != m_IccProfileMap.end()) {
578 { 516 return it->second->AddRef();
579 ASSERT(pFontStream); 517 }
580 518
581 auto it = m_FontFileMap.find(pFontStream); 519 CPDF_StreamAcc stream;
582 if (it != m_FontFileMap.end()) 520 stream.LoadAllData(pIccProfileStream, FALSE);
583 return it->second->AddRef(); 521 uint8_t digest[20];
584 522 CPDF_Stream* pCopiedStream = nullptr;
585 CPDF_Dictionary* pFontDict = pFontStream->GetDict(); 523 CRYPT_SHA1Generate(stream.GetData(), stream.GetSize(), digest);
586 int32_t org_size = pFontDict->GetInteger(FX_BSTRC("Length1")) + 524 if (m_HashProfileMap.Lookup(CFX_ByteStringC(digest, 20),
587 pFontDict->GetInteger(FX_BSTRC("Length2")) + 525 (void*&)pCopiedStream)) {
588 pFontDict->GetInteger(FX_BSTRC("Length3")); 526 auto it_copied_stream = m_IccProfileMap.find(pCopiedStream);
589 if (org_size < 0) 527 return it_copied_stream->second->AddRef();
590 org_size = 0; 528 }
591 529 CPDF_IccProfile* pProfile =
592 CPDF_StreamAcc* pFontFile = new CPDF_StreamAcc; 530 new CPDF_IccProfile(stream.GetData(), stream.GetSize());
593 pFontFile->LoadAllData(pFontStream, FALSE, org_size); 531 CPDF_CountedIccProfile* ipData = new CPDF_CountedIccProfile(pProfile);
594 532 m_IccProfileMap[pIccProfileStream] = ipData;
595 CPDF_CountedStreamAcc* ftData = new CPDF_CountedStreamAcc(pFontFile); 533 m_HashProfileMap.SetAt(CFX_ByteStringC(digest, 20), pIccProfileStream);
596 m_FontFileMap[pFontStream] = ftData; 534 return ipData->AddRef();
597 return ftData->AddRef(); 535 }
598 } 536
599 537 void CPDF_DocPageData::ReleaseIccProfile(CPDF_IccProfile* pIccProfile) {
600 void CPDF_DocPageData::ReleaseFontFileStreamAcc(CPDF_Stream* pFontStream, FX_BOO L bForce) 538 ASSERT(pIccProfile);
601 { 539
602 if (!pFontStream) 540 for (auto it = m_IccProfileMap.begin(); it != m_IccProfileMap.end(); ++it) {
603 return; 541 CPDF_CountedIccProfile* profile = it->second;
604 542 if (profile->get() != pIccProfile)
605 auto it = m_FontFileMap.find(pFontStream); 543 continue;
606 if (it == m_FontFileMap.end()) 544
607 return; 545 profile->RemoveRef();
608 546 if (profile->use_count() == 0) {
609 CPDF_CountedStreamAcc* findData = it->second; 547 delete profile->get();
610 if (!findData) 548 delete profile;
611 return; 549 m_IccProfileMap.erase(it);
612 550 return;
613 findData->RemoveRef(); 551 }
614 if (findData->use_count() == 0 || bForce) { 552 }
615 delete findData->get(); 553 }
616 delete findData; 554
617 m_FontFileMap.erase(it); 555 CPDF_StreamAcc* CPDF_DocPageData::GetFontFileStreamAcc(
618 } 556 CPDF_Stream* pFontStream) {
619 } 557 ASSERT(pFontStream);
620 558
621 CPDF_CountedColorSpace* CPDF_DocPageData::FindColorSpacePtr(CPDF_Object* pCSObj) const 559 auto it = m_FontFileMap.find(pFontStream);
622 { 560 if (it != m_FontFileMap.end())
623 if (!pCSObj) 561 return it->second->AddRef();
624 return nullptr; 562
625 563 CPDF_Dictionary* pFontDict = pFontStream->GetDict();
626 auto it = m_ColorSpaceMap.find(pCSObj); 564 int32_t org_size = pFontDict->GetInteger(FX_BSTRC("Length1")) +
627 return it != m_ColorSpaceMap.end() ? it->second : nullptr; 565 pFontDict->GetInteger(FX_BSTRC("Length2")) +
628 } 566 pFontDict->GetInteger(FX_BSTRC("Length3"));
629 567 if (org_size < 0)
630 CPDF_CountedPattern* CPDF_DocPageData::FindPatternPtr(CPDF_Object* pPatternObj) const 568 org_size = 0;
631 { 569
632 if (!pPatternObj) 570 CPDF_StreamAcc* pFontFile = new CPDF_StreamAcc;
633 return nullptr; 571 pFontFile->LoadAllData(pFontStream, FALSE, org_size);
634 572
635 auto it = m_PatternMap.find(pPatternObj); 573 CPDF_CountedStreamAcc* ftData = new CPDF_CountedStreamAcc(pFontFile);
636 return it != m_PatternMap.end() ? it->second : nullptr; 574 m_FontFileMap[pFontStream] = ftData;
637 } 575 return ftData->AddRef();
576 }
577
578 void CPDF_DocPageData::ReleaseFontFileStreamAcc(CPDF_Stream* pFontStream,
579 FX_BOOL bForce) {
580 if (!pFontStream)
581 return;
582
583 auto it = m_FontFileMap.find(pFontStream);
584 if (it == m_FontFileMap.end())
585 return;
586
587 CPDF_CountedStreamAcc* findData = it->second;
588 if (!findData)
589 return;
590
591 findData->RemoveRef();
592 if (findData->use_count() == 0 || bForce) {
593 delete findData->get();
594 delete findData;
595 m_FontFileMap.erase(it);
596 }
597 }
598
599 CPDF_CountedColorSpace* CPDF_DocPageData::FindColorSpacePtr(
600 CPDF_Object* pCSObj) const {
601 if (!pCSObj)
602 return nullptr;
603
604 auto it = m_ColorSpaceMap.find(pCSObj);
605 return it != m_ColorSpaceMap.end() ? it->second : nullptr;
606 }
607
608 CPDF_CountedPattern* CPDF_DocPageData::FindPatternPtr(
609 CPDF_Object* pPatternObj) const {
610 if (!pPatternObj)
611 return nullptr;
612
613 auto it = m_PatternMap.find(pPatternObj);
614 return it != m_PatternMap.end() ? it->second : nullptr;
615 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698