1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
#include "core/fpdfapi/font/cpdf_cmapparser.h"
#include <vector>
#include "core/fpdfapi/cmaps/cmap_int.h"
#include "core/fpdfapi/cpdf_modulemgr.h"
#include "core/fpdfapi/page/cpdf_pagemodule.h"
#include "core/fpdfapi/parser/cpdf_array.h"
#include "core/fpdfapi/parser/cpdf_dictionary.h"
#include "core/fpdfapi/parser/cpdf_simple_parser.h"
#include "core/fxcrt/fx_extension.h"
#include "core/fxge/fx_freetype.h"
#include "third_party/base/logging.h"
#include "third_party/base/stl_util.h"
namespace {
const char* const g_CharsetNames[CIDSET_NUM_SETS] = {nullptr, "GB1", "CNS1",
"Japan1", "Korea1", "UCS"};
CIDSet CIDSetFromSizeT(size_t index) {
if (index >= CIDSET_NUM_SETS) {
NOTREACHED();
return CIDSET_UNKNOWN;
}
return static_cast<CIDSet>(index);
}
CFX_ByteStringC CMap_GetString(const CFX_ByteStringC& word) {
if (word.GetLength() <= 2)
return CFX_ByteStringC();
return word.Right(word.GetLength() - 2);
}
} // namespace
CPDF_CMapParser::CPDF_CMapParser(CPDF_CMap* pCMap)
: m_pCMap(pCMap), m_Status(0), m_CodeSeq(0) {}
CPDF_CMapParser::~CPDF_CMapParser() {}
void CPDF_CMapParser::ParseWord(const CFX_ByteStringC& word) {
if (word.IsEmpty()) {
return;
}
if (word == "begincidchar") {
m_Status = 1;
m_CodeSeq = 0;
} else if (word == "begincidrange") {
m_Status = 2;
m_CodeSeq = 0;
} else if (word == "endcidrange" || word == "endcidchar") {
m_Status = 0;
} else if (word == "/WMode") {
m_Status = 6;
} else if (word == "/Registry") {
m_Status = 3;
} else if (word == "/Ordering") {
m_Status = 4;
} else if (word == "/Supplement") {
m_Status = 5;
} else if (word == "begincodespacerange") {
m_Status = 7;
m_CodeSeq = 0;
} else if (word == "usecmap") {
} else if (m_Status == 1 || m_Status == 2) {
m_CodePoints[m_CodeSeq] = CMap_GetCode(word);
m_CodeSeq++;
uint32_t StartCode, EndCode;
uint16_t StartCID;
if (m_Status == 1) {
if (m_CodeSeq < 2) {
return;
}
EndCode = StartCode = m_CodePoints[0];
StartCID = (uint16_t)m_CodePoints[1];
} else {
if (m_CodeSeq < 3) {
return;
}
StartCode = m_CodePoints[0];
EndCode = m_CodePoints[1];
StartCID = (uint16_t)m_CodePoints[2];
}
if (EndCode < 0x10000) {
for (uint32_t code = StartCode; code <= EndCode; code++) {
m_pCMap->m_DirectCharcodeToCIDTable[code] =
static_cast<uint16_t>(StartCID + code - StartCode);
}
} else {
m_AdditionalCharcodeToCIDMappings.push_back(
{StartCode, EndCode, StartCID});
}
m_CodeSeq = 0;
} else if (m_Status == 3) {
m_Status = 0;
} else if (m_Status == 4) {
m_pCMap->m_Charset = CharsetFromOrdering(CMap_GetString(word));
m_Status = 0;
} else if (m_Status == 5) {
m_Status = 0;
} else if (m_Status == 6) {
m_pCMap->m_bVertical = CMap_GetCode(word) != 0;
m_Status = 0;
} else if (m_Status == 7) {
if (word == "endcodespacerange") {
uint32_t nSegs = pdfium::CollectionSize<uint32_t>(m_CodeRanges);
if (nSegs > 1) {
m_pCMap->m_CodingScheme = CPDF_CMap::MixedFourBytes;
m_pCMap->m_MixedFourByteLeadingRanges = m_CodeRanges;
} else if (nSegs == 1) {
m_pCMap->m_CodingScheme = (m_CodeRanges[0].m_CharSize == 2)
? CPDF_CMap::TwoBytes
: CPDF_CMap::OneByte;
}
m_Status = 0;
} else {
if (word.GetLength() == 0 || word[0] != '<') {
return;
}
if (m_CodeSeq % 2) {
CPDF_CMap::CodeRange range;
if (CMap_GetCodeRange(range, m_LastWord.AsStringC(), word))
m_CodeRanges.push_back(range);
}
m_CodeSeq++;
}
}
m_LastWord = word;
}
// Static.
uint32_t CPDF_CMapParser::CMap_GetCode(const CFX_ByteStringC& word) {
if (word.IsEmpty())
return 0;
pdfium::base::CheckedNumeric<uint32_t> num = 0;
if (word[0] == '<') {
for (FX_STRSIZE i = 1; i < word.GetLength() && std::isxdigit(word[i]);
++i) {
num = num * 16 + FXSYS_HexCharToInt(word[i]);
if (!num.IsValid())
return 0;
}
return num.ValueOrDie();
}
for (FX_STRSIZE i = 0; i < word.GetLength() && std::isdigit(word[i]); ++i) {
num = num * 10 + FXSYS_DecimalCharToInt(static_cast<wchar_t>(word[i]));
if (!num.IsValid())
return 0;
}
return num.ValueOrDie();
}
// Static.
bool CPDF_CMapParser::CMap_GetCodeRange(CPDF_CMap::CodeRange& range,
const CFX_ByteStringC& first,
const CFX_ByteStringC& second) {
if (first.GetLength() == 0 || first[0] != '<')
return false;
FX_STRSIZE i;
for (i = 1; i < first.GetLength(); ++i) {
if (first[i] == '>') {
break;
}
}
range.m_CharSize = (i - 1) / 2;
if (range.m_CharSize > 4)
return false;
for (i = 0; i < range.m_CharSize; ++i) {
uint8_t digit1 = first[i * 2 + 1];
uint8_t digit2 = first[i * 2 + 2];
range.m_Lower[i] =
FXSYS_HexCharToInt(digit1) * 16 + FXSYS_HexCharToInt(digit2);
}
FX_STRSIZE size = second.GetLength();
for (i = 0; i < range.m_CharSize; ++i) {
uint8_t digit1 = (i * 2 + 1 < size) ? second[i * 2 + 1] : '0';
uint8_t digit2 = (i * 2 + 2 < size) ? second[i * 2 + 2] : '0';
range.m_Upper[i] =
FXSYS_HexCharToInt(digit1) * 16 + FXSYS_HexCharToInt(digit2);
}
return true;
}
// static
CIDSet CPDF_CMapParser::CharsetFromOrdering(const CFX_ByteStringC& ordering) {
for (size_t charset = 1; charset < FX_ArraySize(g_CharsetNames); ++charset) {
if (ordering == g_CharsetNames[charset])
return CIDSetFromSizeT(charset);
}
return CIDSET_UNKNOWN;
}
|