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

Side by Side Diff: core/fpdfapi/page/fpdf_page_parser.cpp

Issue 2474303003: Clean up fpdf_page_parsers (Closed)
Patch Set: Remove PDF_ from namespace stuff Created 4 years, 1 month 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
(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/page/pageint.h"
8
9 #include <memory>
10 #include <utility>
11 #include <vector>
12
13 #include "core/fpdfapi/edit/cpdf_creator.h"
14 #include "core/fpdfapi/font/cpdf_font.h"
15 #include "core/fpdfapi/font/cpdf_type3font.h"
16 #include "core/fpdfapi/page/cpdf_allstates.h"
17 #include "core/fpdfapi/page/cpdf_docpagedata.h"
18 #include "core/fpdfapi/page/cpdf_form.h"
19 #include "core/fpdfapi/page/cpdf_formobject.h"
20 #include "core/fpdfapi/page/cpdf_image.h"
21 #include "core/fpdfapi/page/cpdf_imageobject.h"
22 #include "core/fpdfapi/page/cpdf_meshstream.h"
23 #include "core/fpdfapi/page/cpdf_pageobject.h"
24 #include "core/fpdfapi/page/cpdf_pathobject.h"
25 #include "core/fpdfapi/page/cpdf_shadingobject.h"
26 #include "core/fpdfapi/page/cpdf_shadingpattern.h"
27 #include "core/fpdfapi/page/cpdf_textobject.h"
28 #include "core/fpdfapi/parser/cpdf_array.h"
29 #include "core/fpdfapi/parser/cpdf_dictionary.h"
30 #include "core/fpdfapi/parser/cpdf_document.h"
31 #include "core/fpdfapi/parser/cpdf_name.h"
32 #include "core/fpdfapi/parser/cpdf_number.h"
33 #include "core/fpdfapi/parser/cpdf_reference.h"
34 #include "core/fpdfapi/parser/cpdf_stream.h"
35 #include "core/fpdfapi/parser/cpdf_stream_acc.h"
36 #include "core/fpdfapi/parser/fpdf_parser_decode.h"
37 #include "core/fxcrt/fx_safe_types.h"
38 #include "core/fxge/cfx_graphstatedata.h"
39 #include "core/fxge/cfx_pathdata.h"
40 #include "third_party/base/ptr_util.h"
41
42 namespace {
43
44 struct PDF_AbbrPair {
45 const FX_CHAR* abbr;
46 const FX_CHAR* full_name;
47 };
48
49 const PDF_AbbrPair PDF_InlineKeyAbbr[] = {
50 {"BPC", "BitsPerComponent"}, {"CS", "ColorSpace"}, {"D", "Decode"},
51 {"DP", "DecodeParms"}, {"F", "Filter"}, {"H", "Height"},
52 {"IM", "ImageMask"}, {"I", "Interpolate"}, {"W", "Width"},
53 };
54
55 const PDF_AbbrPair PDF_InlineValueAbbr[] = {
56 {"G", "DeviceGray"}, {"RGB", "DeviceRGB"},
57 {"CMYK", "DeviceCMYK"}, {"I", "Indexed"},
58 {"AHx", "ASCIIHexDecode"}, {"A85", "ASCII85Decode"},
59 {"LZW", "LZWDecode"}, {"Fl", "FlateDecode"},
60 {"RL", "RunLengthDecode"}, {"CCF", "CCITTFaxDecode"},
61 {"DCT", "DCTDecode"},
62 };
63
64 struct AbbrReplacementOp {
65 bool is_replace_key;
66 CFX_ByteString key;
67 CFX_ByteStringC replacement;
68 };
69
70 CFX_ByteStringC PDF_FindFullName(const PDF_AbbrPair* table,
71 size_t count,
72 const CFX_ByteStringC& abbr) {
73 auto it = std::find_if(
74 table, table + count,
75 [abbr](const PDF_AbbrPair& pair) { return pair.abbr == abbr; });
76 return it != table + count ? CFX_ByteStringC(it->full_name)
77 : CFX_ByteStringC();
78 }
79
80 } // namespace
81
82 CFX_ByteStringC PDF_FindKeyAbbreviationForTesting(const CFX_ByteStringC& abbr) {
83 return PDF_FindFullName(PDF_InlineKeyAbbr, FX_ArraySize(PDF_InlineKeyAbbr),
84 abbr);
85 }
86
87 CFX_ByteStringC PDF_FindValueAbbreviationForTesting(
88 const CFX_ByteStringC& abbr) {
89 return PDF_FindFullName(PDF_InlineValueAbbr,
90 FX_ArraySize(PDF_InlineValueAbbr), abbr);
91 }
92
93 void PDF_ReplaceAbbr(CPDF_Object* pObj) {
94 switch (pObj->GetType()) {
95 case CPDF_Object::DICTIONARY: {
96 CPDF_Dictionary* pDict = pObj->AsDictionary();
97 std::vector<AbbrReplacementOp> replacements;
98 for (const auto& it : *pDict) {
99 CFX_ByteString key = it.first;
100 CPDF_Object* value = it.second;
101 CFX_ByteStringC fullname =
102 PDF_FindFullName(PDF_InlineKeyAbbr, FX_ArraySize(PDF_InlineKeyAbbr),
103 key.AsStringC());
104 if (!fullname.IsEmpty()) {
105 AbbrReplacementOp op;
106 op.is_replace_key = true;
107 op.key = key;
108 op.replacement = fullname;
109 replacements.push_back(op);
110 key = fullname;
111 }
112
113 if (value->IsName()) {
114 CFX_ByteString name = value->GetString();
115 fullname = PDF_FindFullName(PDF_InlineValueAbbr,
116 FX_ArraySize(PDF_InlineValueAbbr),
117 name.AsStringC());
118 if (!fullname.IsEmpty()) {
119 AbbrReplacementOp op;
120 op.is_replace_key = false;
121 op.key = key;
122 op.replacement = fullname;
123 replacements.push_back(op);
124 }
125 } else {
126 PDF_ReplaceAbbr(value);
127 }
128 }
129 for (const auto& op : replacements) {
130 if (op.is_replace_key)
131 pDict->ReplaceKey(op.key, CFX_ByteString(op.replacement));
132 else
133 pDict->SetNameFor(op.key, CFX_ByteString(op.replacement));
134 }
135 break;
136 }
137 case CPDF_Object::ARRAY: {
138 CPDF_Array* pArray = pObj->AsArray();
139 for (size_t i = 0; i < pArray->GetCount(); i++) {
140 CPDF_Object* pElement = pArray->GetObjectAt(i);
141 if (pElement->IsName()) {
142 CFX_ByteString name = pElement->GetString();
143 CFX_ByteStringC fullname = PDF_FindFullName(
144 PDF_InlineValueAbbr, FX_ArraySize(PDF_InlineValueAbbr),
145 name.AsStringC());
146 if (!fullname.IsEmpty()) {
147 pArray->SetAt(i, new CPDF_Name(CFX_ByteString(fullname)));
148 }
149 } else {
150 PDF_ReplaceAbbr(pElement);
151 }
152 }
153 break;
154 }
155 default:
156 break;
157 }
158 }
OLDNEW
« no previous file with comments | « core/fpdfapi/page/cpdf_streamparser_unittest.cpp ('k') | core/fpdfapi/page/fpdf_page_parser_old.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698