Fix else-after-returns throughout pdfium.
[pdfium.git] / core / src / fpdfapi / fpdf_page / fpdf_page_parser.cpp
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 "../../../include/fpdfapi/fpdf_page.h"
8 #include "../../../include/fpdfapi/fpdf_module.h"
9 #include "../../../include/fpdfapi/fpdf_serial.h"
10 #include "pageint.h"
11
12 #define REQUIRE_PARAMS(count) if (m_ParamCount != count) { m_bAbort = TRUE; return; }
13
14 CPDF_StreamContentParser::CPDF_StreamContentParser(
15     CPDF_Document* pDocument,
16     CPDF_Dictionary* pPageResources,
17     CPDF_Dictionary* pParentResources,
18     CFX_AffineMatrix* pmtContentToUser,
19     CPDF_PageObjects* pObjList,
20     CPDF_Dictionary* pResources,
21     CPDF_Rect* pBBox,
22     CPDF_ParseOptions* pOptions,
23     CPDF_AllStates* pStates,
24     int level)
25     : m_pDocument(pDocument),
26       m_pPageResources(pPageResources),
27       m_pParentResources(pParentResources),
28       m_pResources(pResources),
29       m_pObjectList(pObjList),
30       m_Level(level),
31       m_ParamStartPos(0),
32       m_ParamCount(0),
33       m_bAbort(FALSE),
34       m_pCurStates(new CPDF_AllStates),
35       m_pLastTextObject(nullptr),
36       m_DefFontSize(0),
37       m_pPathPoints(nullptr),
38       m_PathPointCount(0),
39       m_PathAllocSize(0),
40       m_PathCurrentX(0.0f),
41       m_PathCurrentY(0.0f),
42       m_PathClipType(0),
43       m_pLastImage(nullptr),
44       m_pLastImageDict(nullptr),
45       m_pLastCloneImageDict(nullptr),
46       m_bReleaseLastDict(TRUE),
47       m_bColored(FALSE),
48       m_bResourceMissing(FALSE)
49 {
50     if (pmtContentToUser) {
51         m_mtContentToUser = *pmtContentToUser;
52     }
53     if (pOptions) {
54         m_Options = *pOptions;
55     }
56     if (!m_pResources) {
57         m_pResources = m_pParentResources;
58     }
59     if (!m_pResources) {
60         m_pResources = m_pPageResources;
61     }
62     if (pBBox) {
63         m_BBox = *pBBox;
64     }
65     if (pStates) {
66         m_pCurStates->Copy(*pStates);
67     } else {
68         m_pCurStates->m_GeneralState.New();
69         m_pCurStates->m_GraphState.New();
70         m_pCurStates->m_TextState.New();
71         m_pCurStates->m_ColorState.New();
72     }
73     for (int i = 0; i < FX_ArraySize(m_Type3Data); ++i) {
74         m_Type3Data[i] = 0.0;
75     }
76 }
77
78 CPDF_StreamContentParser::~CPDF_StreamContentParser()
79 {
80     ClearAllParams();
81     for (int i = 0; i < m_StateStack.GetSize(); ++i) {
82         delete (CPDF_AllStates*)m_StateStack[i];
83     }
84     if (m_pPathPoints) {
85         FX_Free(m_pPathPoints);
86     }
87     if (m_pLastImageDict) {
88         m_pLastImageDict->Release();
89     }
90     if (m_pLastCloneImageDict) {
91         m_pLastCloneImageDict->Release();
92     }
93 }
94 int CPDF_StreamContentParser::GetNextParamPos()
95 {
96     if (m_ParamCount == PARAM_BUF_SIZE) {
97         m_ParamStartPos ++;
98         if (m_ParamStartPos == PARAM_BUF_SIZE) {
99             m_ParamStartPos = 0;
100         }
101         if (m_ParamBuf1[m_ParamStartPos].m_Type == 0) {
102             if (CPDF_Object* pObject = m_ParamBuf1[m_ParamStartPos].m_pObject)
103                 pObject->Release();
104         }
105         return m_ParamStartPos;
106     }
107     int index = m_ParamStartPos + m_ParamCount;
108     if (index >= PARAM_BUF_SIZE) {
109         index -= PARAM_BUF_SIZE;
110     }
111     m_ParamCount ++;
112     return index;
113 }
114 void CPDF_StreamContentParser::AddNameParam(const FX_CHAR* name, int len)
115 {
116     int index = GetNextParamPos();
117     if (len > 32) {
118         m_ParamBuf1[index].m_Type = 0;
119         m_ParamBuf1[index].m_pObject = CPDF_Name::Create(PDF_NameDecode(CFX_ByteStringC(name, len)));
120     } else {
121         m_ParamBuf1[index].m_Type = PDFOBJ_NAME;
122         if (FXSYS_memchr(name, '#', len) == NULL) {
123             FXSYS_memcpy(m_ParamBuf1[index].m_Name.m_Buffer, name, len);
124             m_ParamBuf1[index].m_Name.m_Len = len;
125         } else {
126             CFX_ByteString str = PDF_NameDecode(CFX_ByteStringC(name, len));
127             FXSYS_memcpy(m_ParamBuf1[index].m_Name.m_Buffer, str.c_str(), str.GetLength());
128             m_ParamBuf1[index].m_Name.m_Len = str.GetLength();
129         }
130     }
131 }
132 void CPDF_StreamContentParser::AddNumberParam(const FX_CHAR* str, int len)
133 {
134     int index = GetNextParamPos();
135     m_ParamBuf1[index].m_Type = PDFOBJ_NUMBER;
136     FX_atonum(CFX_ByteStringC(str, len), m_ParamBuf1[index].m_Number.m_bInteger,
137               &m_ParamBuf1[index].m_Number.m_Integer);
138 }
139 void CPDF_StreamContentParser::AddObjectParam(CPDF_Object* pObj)
140 {
141     int index = GetNextParamPos();
142     m_ParamBuf1[index].m_Type = 0;
143     m_ParamBuf1[index].m_pObject = pObj;
144 }
145 void CPDF_StreamContentParser::ClearAllParams()
146 {
147     FX_DWORD index = m_ParamStartPos;
148     for (FX_DWORD i = 0; i < m_ParamCount; i ++) {
149         if (m_ParamBuf1[index].m_Type == 0) {
150             if (CPDF_Object* pObject = m_ParamBuf1[index].m_pObject)
151                 pObject->Release();
152         }
153         index ++;
154         if (index == PARAM_BUF_SIZE) {
155             index = 0;
156         }
157     }
158     m_ParamStartPos = 0;
159     m_ParamCount = 0;
160 }
161 CPDF_Object* CPDF_StreamContentParser::GetObject(FX_DWORD index)
162 {
163     if (index >= m_ParamCount) {
164         return NULL;
165     }
166     int real_index = m_ParamStartPos + m_ParamCount - index - 1;
167     if (real_index >= PARAM_BUF_SIZE) {
168         real_index -= PARAM_BUF_SIZE;
169     }
170     _ContentParam& param = m_ParamBuf1[real_index];
171     if (param.m_Type == PDFOBJ_NUMBER) {
172         CPDF_Number* pNumber = CPDF_Number::Create(param.m_Number.m_bInteger, &param.m_Number.m_Integer);
173         param.m_Type = 0;
174         param.m_pObject = pNumber;
175         return pNumber;
176     }
177     if (param.m_Type == PDFOBJ_NAME) {
178         CPDF_Name* pName = CPDF_Name::Create(CFX_ByteString(param.m_Name.m_Buffer, param.m_Name.m_Len));
179         param.m_Type = 0;
180         param.m_pObject = pName;
181         return pName;
182     }
183     if (param.m_Type == 0) {
184         return param.m_pObject;
185     }
186     ASSERT(FALSE);
187     return NULL;
188 }
189 CFX_ByteString CPDF_StreamContentParser::GetString(FX_DWORD index)
190 {
191     if (index >= m_ParamCount) {
192         return CFX_ByteString();
193     }
194     int real_index = m_ParamStartPos + m_ParamCount - index - 1;
195     if (real_index >= PARAM_BUF_SIZE) {
196         real_index -= PARAM_BUF_SIZE;
197     }
198     _ContentParam& param = m_ParamBuf1[real_index];
199     if (param.m_Type == PDFOBJ_NAME) {
200         return CFX_ByteString(param.m_Name.m_Buffer, param.m_Name.m_Len);
201     }
202     if (param.m_Type == 0 && param.m_pObject) {
203         return param.m_pObject->GetString();
204     }
205     return CFX_ByteString();
206 }
207 FX_FLOAT CPDF_StreamContentParser::GetNumber(FX_DWORD index)
208 {
209     if (index >= m_ParamCount) {
210         return 0;
211     }
212     int real_index = m_ParamStartPos + m_ParamCount - index - 1;
213     if (real_index >= PARAM_BUF_SIZE) {
214         real_index -= PARAM_BUF_SIZE;
215     }
216     _ContentParam& param = m_ParamBuf1[real_index];
217     if (param.m_Type == PDFOBJ_NUMBER) {
218         return param.m_Number.m_bInteger ? (FX_FLOAT)param.m_Number.m_Integer : param.m_Number.m_Float;
219     }
220     if (param.m_Type == 0 && param.m_pObject) {
221         return param.m_pObject->GetNumber();
222     }
223     return 0;
224 }
225 FX_FLOAT CPDF_StreamContentParser::GetNumber16(FX_DWORD index)
226 {
227     return GetNumber(index);
228 }
229 void CPDF_StreamContentParser::SetGraphicStates(CPDF_PageObject* pObj, FX_BOOL bColor, FX_BOOL bText, FX_BOOL bGraph)
230 {
231     pObj->m_GeneralState = m_pCurStates->m_GeneralState;
232     pObj->m_ClipPath = m_pCurStates->m_ClipPath;
233     pObj->m_ContentMark = m_CurContentMark;
234     if (bColor) {
235         pObj->m_ColorState = m_pCurStates->m_ColorState;
236     }
237     if (bGraph) {
238         pObj->m_GraphState = m_pCurStates->m_GraphState;
239     }
240     if (bText) {
241         pObj->m_TextState = m_pCurStates->m_TextState;
242     }
243 }
244
245 const CPDF_StreamContentParser::OpCode CPDF_StreamContentParser::g_OpCodes[] =
246 {
247     {FXBSTR_ID('"', 0, 0, 0),           &CPDF_StreamContentParser::Handle_NextLineShowText_Space},
248     {FXBSTR_ID('\'', 0, 0, 0),          &CPDF_StreamContentParser::Handle_NextLineShowText},
249     {FXBSTR_ID('B', 0, 0, 0),           &CPDF_StreamContentParser::Handle_FillStrokePath},
250     {FXBSTR_ID('B', '*', 0, 0), &CPDF_StreamContentParser::Handle_EOFillStrokePath},
251     {FXBSTR_ID('B', 'D', 'C', 0),       &CPDF_StreamContentParser::Handle_BeginMarkedContent_Dictionary},
252     {FXBSTR_ID('B', 'I', 0, 0), &CPDF_StreamContentParser::Handle_BeginImage},
253     {FXBSTR_ID('B', 'M', 'C', 0),       &CPDF_StreamContentParser::Handle_BeginMarkedContent},
254     {FXBSTR_ID('B', 'T', 0, 0), &CPDF_StreamContentParser::Handle_BeginText},
255     {FXBSTR_ID('B', 'X', 0, 0), &CPDF_StreamContentParser::Handle_BeginSectionUndefined},
256     {FXBSTR_ID('C', 'S', 0, 0), &CPDF_StreamContentParser::Handle_SetColorSpace_Stroke},
257     {FXBSTR_ID('D', 'P', 0, 0), &CPDF_StreamContentParser::Handle_MarkPlace_Dictionary},
258     {FXBSTR_ID('D', 'o', 0, 0), &CPDF_StreamContentParser::Handle_ExecuteXObject},
259     {FXBSTR_ID('E', 'I', 0, 0), &CPDF_StreamContentParser::Handle_EndImage},
260     {FXBSTR_ID('E', 'M', 'C', 0),       &CPDF_StreamContentParser::Handle_EndMarkedContent},
261     {FXBSTR_ID('E', 'T', 0, 0), &CPDF_StreamContentParser::Handle_EndText},
262     {FXBSTR_ID('E', 'X', 0, 0), &CPDF_StreamContentParser::Handle_EndSectionUndefined},
263     {FXBSTR_ID('F', 0, 0, 0),           &CPDF_StreamContentParser::Handle_FillPathOld},
264     {FXBSTR_ID('G', 0, 0, 0),           &CPDF_StreamContentParser::Handle_SetGray_Stroke},
265     {FXBSTR_ID('I', 'D', 0, 0), &CPDF_StreamContentParser::Handle_BeginImageData},
266     {FXBSTR_ID('J', 0, 0, 0),           &CPDF_StreamContentParser::Handle_SetLineCap},
267     {FXBSTR_ID('K', 0, 0, 0),           &CPDF_StreamContentParser::Handle_SetCMYKColor_Stroke},
268     {FXBSTR_ID('M', 0, 0, 0),           &CPDF_StreamContentParser::Handle_SetMiterLimit},
269     {FXBSTR_ID('M', 'P', 0, 0), &CPDF_StreamContentParser::Handle_MarkPlace},
270     {FXBSTR_ID('Q', 0, 0, 0),           &CPDF_StreamContentParser::Handle_RestoreGraphState},
271     {FXBSTR_ID('R', 'G', 0, 0), &CPDF_StreamContentParser::Handle_SetRGBColor_Stroke},
272     {FXBSTR_ID('S', 0, 0, 0),           &CPDF_StreamContentParser::Handle_StrokePath},
273     {FXBSTR_ID('S', 'C', 0, 0), &CPDF_StreamContentParser::Handle_SetColor_Stroke},
274     {FXBSTR_ID('S', 'C', 'N', 0),       &CPDF_StreamContentParser::Handle_SetColorPS_Stroke},
275     {FXBSTR_ID('T', '*', 0, 0), &CPDF_StreamContentParser::Handle_MoveToNextLine},
276     {FXBSTR_ID('T', 'D', 0, 0), &CPDF_StreamContentParser::Handle_MoveTextPoint_SetLeading},
277     {FXBSTR_ID('T', 'J', 0, 0), &CPDF_StreamContentParser::Handle_ShowText_Positioning},
278     {FXBSTR_ID('T', 'L', 0, 0), &CPDF_StreamContentParser::Handle_SetTextLeading},
279     {FXBSTR_ID('T', 'c', 0, 0), &CPDF_StreamContentParser::Handle_SetCharSpace},
280     {FXBSTR_ID('T', 'd', 0, 0), &CPDF_StreamContentParser::Handle_MoveTextPoint},
281     {FXBSTR_ID('T', 'f', 0, 0), &CPDF_StreamContentParser::Handle_SetFont},
282     {FXBSTR_ID('T', 'j', 0, 0), &CPDF_StreamContentParser::Handle_ShowText},
283     {FXBSTR_ID('T', 'm', 0, 0), &CPDF_StreamContentParser::Handle_SetTextMatrix},
284     {FXBSTR_ID('T', 'r', 0, 0), &CPDF_StreamContentParser::Handle_SetTextRenderMode},
285     {FXBSTR_ID('T', 's', 0, 0), &CPDF_StreamContentParser::Handle_SetTextRise},
286     {FXBSTR_ID('T', 'w', 0, 0), &CPDF_StreamContentParser::Handle_SetWordSpace},
287     {FXBSTR_ID('T', 'z', 0, 0), &CPDF_StreamContentParser::Handle_SetHorzScale},
288     {FXBSTR_ID('W', 0, 0, 0),           &CPDF_StreamContentParser::Handle_Clip},
289     {FXBSTR_ID('W', '*', 0, 0), &CPDF_StreamContentParser::Handle_EOClip},
290     {FXBSTR_ID('b', 0, 0, 0),           &CPDF_StreamContentParser::Handle_CloseFillStrokePath},
291     {FXBSTR_ID('b', '*', 0, 0), &CPDF_StreamContentParser::Handle_CloseEOFillStrokePath},
292     {FXBSTR_ID('c', 0, 0, 0),           &CPDF_StreamContentParser::Handle_CurveTo_123},
293     {FXBSTR_ID('c', 'm', 0, 0), &CPDF_StreamContentParser::Handle_ConcatMatrix},
294     {FXBSTR_ID('c', 's', 0, 0), &CPDF_StreamContentParser::Handle_SetColorSpace_Fill},
295     {FXBSTR_ID('d', 0, 0, 0),           &CPDF_StreamContentParser::Handle_SetDash},
296     {FXBSTR_ID('d', '0', 0, 0), &CPDF_StreamContentParser::Handle_SetCharWidth},
297     {FXBSTR_ID('d', '1', 0, 0), &CPDF_StreamContentParser::Handle_SetCachedDevice},
298     {FXBSTR_ID('f', 0, 0, 0),           &CPDF_StreamContentParser::Handle_FillPath},
299     {FXBSTR_ID('f', '*', 0, 0), &CPDF_StreamContentParser::Handle_EOFillPath},
300     {FXBSTR_ID('g', 0, 0, 0),           &CPDF_StreamContentParser::Handle_SetGray_Fill},
301     {FXBSTR_ID('g', 's', 0, 0), &CPDF_StreamContentParser::Handle_SetExtendGraphState},
302     {FXBSTR_ID('h', 0, 0, 0),           &CPDF_StreamContentParser::Handle_ClosePath},
303     {FXBSTR_ID('i', 0, 0, 0),           &CPDF_StreamContentParser::Handle_SetFlat},
304     {FXBSTR_ID('j', 0, 0, 0),           &CPDF_StreamContentParser::Handle_SetLineJoin},
305     {FXBSTR_ID('k', 0, 0, 0),           &CPDF_StreamContentParser::Handle_SetCMYKColor_Fill},
306     {FXBSTR_ID('l', 0, 0, 0),           &CPDF_StreamContentParser::Handle_LineTo},
307     {FXBSTR_ID('m', 0, 0, 0),           &CPDF_StreamContentParser::Handle_MoveTo},
308     {FXBSTR_ID('n', 0, 0, 0),           &CPDF_StreamContentParser::Handle_EndPath},
309     {FXBSTR_ID('q', 0, 0, 0),           &CPDF_StreamContentParser::Handle_SaveGraphState},
310     {FXBSTR_ID('r', 'e', 0, 0), &CPDF_StreamContentParser::Handle_Rectangle},
311     {FXBSTR_ID('r', 'g', 0, 0), &CPDF_StreamContentParser::Handle_SetRGBColor_Fill},
312     {FXBSTR_ID('r', 'i', 0, 0), &CPDF_StreamContentParser::Handle_SetRenderIntent},
313     {FXBSTR_ID('s', 0, 0, 0),           &CPDF_StreamContentParser::Handle_CloseStrokePath},
314     {FXBSTR_ID('s', 'c', 0, 0), &CPDF_StreamContentParser::Handle_SetColor_Fill},
315     {FXBSTR_ID('s', 'c', 'n', 0),       &CPDF_StreamContentParser::Handle_SetColorPS_Fill},
316     {FXBSTR_ID('s', 'h', 0, 0), &CPDF_StreamContentParser::Handle_ShadeFill},
317     {FXBSTR_ID('v', 0, 0, 0),           &CPDF_StreamContentParser::Handle_CurveTo_23},
318     {FXBSTR_ID('w', 0, 0, 0),           &CPDF_StreamContentParser::Handle_SetLineWidth},
319     {FXBSTR_ID('y', 0, 0, 0),           &CPDF_StreamContentParser::Handle_CurveTo_13},
320 };
321 FX_BOOL CPDF_StreamContentParser::OnOperator(const FX_CHAR* op)
322 {
323     int i = 0;
324     FX_DWORD opid = 0;
325     while (i < 4 && op[i]) {
326         opid = (opid << 8) + op[i];
327         i ++;
328     }
329     while (i < 4) {
330         opid <<= 8;
331         i ++;
332     };
333     int low = 0, high = sizeof g_OpCodes / sizeof(OpCode) - 1;
334     while (low <= high) {
335         int middle = (low + high) / 2;
336         int compare = opid - g_OpCodes[middle].m_OpId;
337         if (compare == 0) {
338             (this->*g_OpCodes[middle].m_OpHandler)();
339             return TRUE;
340         }
341         if (compare < 0) {
342             high = middle - 1;
343         } else {
344             low = middle + 1;
345         }
346     }
347     return m_CompatCount != 0;
348 }
349 void CPDF_StreamContentParser::Handle_CloseFillStrokePath()
350 {
351     if (m_Options.m_bTextOnly) {
352         return;
353     }
354     Handle_ClosePath();
355     AddPathObject(FXFILL_WINDING, TRUE);
356 }
357 void CPDF_StreamContentParser::Handle_FillStrokePath()
358 {
359     if (m_Options.m_bTextOnly) {
360         return;
361     }
362     AddPathObject(FXFILL_WINDING, TRUE);
363 }
364 void CPDF_StreamContentParser::Handle_CloseEOFillStrokePath()
365 {
366     if (m_Options.m_bTextOnly) {
367         return;
368     }
369     AddPathPoint(m_PathStartX, m_PathStartY, FXPT_LINETO | FXPT_CLOSEFIGURE);
370     AddPathObject(FXFILL_ALTERNATE, TRUE);
371 }
372 void CPDF_StreamContentParser::Handle_EOFillStrokePath()
373 {
374     if (m_Options.m_bTextOnly) {
375         return;
376     }
377     AddPathObject(FXFILL_ALTERNATE, TRUE);
378 }
379 void CPDF_StreamContentParser::Handle_BeginMarkedContent_Dictionary()
380 {
381     if (!m_Options.m_bMarkedContent) {
382         return;
383     }
384     CFX_ByteString tag = GetString(1);
385     CPDF_Object* pProperty = GetObject(0);
386     if (pProperty == NULL) {
387         return;
388     }
389     FX_BOOL bDirect = TRUE;
390     if (pProperty->GetType() == PDFOBJ_NAME) {
391         pProperty = FindResourceObj(FX_BSTRC("Properties"), pProperty->GetString());
392         if (pProperty == NULL) {
393             return;
394         }
395         bDirect = FALSE;
396     }
397     if (pProperty->GetType() != PDFOBJ_DICTIONARY) {
398         return;
399     }
400     m_CurContentMark.GetModify()->AddMark(tag, (CPDF_Dictionary*)pProperty, bDirect);
401 }
402 void CPDF_StreamContentParser::Handle_BeginMarkedContent()
403 {
404     if (!m_Options.m_bMarkedContent) {
405         return;
406     }
407     CFX_ByteString tag = GetString(0);
408     m_CurContentMark.GetModify()->AddMark(tag, NULL, FALSE);
409 }
410 struct _FX_BSTR {
411     const FX_CHAR*      m_Ptr;
412     int                 m_Size;
413 };
414 #define _FX_BSTRC(str) {str, sizeof(str)-1}
415 const _FX_BSTR _PDF_InlineKeyAbbr[] = {
416     _FX_BSTRC("BitsPerComponent"), _FX_BSTRC("BPC"),
417     _FX_BSTRC("ColorSpace"), _FX_BSTRC("CS"),
418     _FX_BSTRC("Decode"), _FX_BSTRC("D"),
419     _FX_BSTRC("DecodeParms"), _FX_BSTRC("DP"),
420     _FX_BSTRC("Filter"), _FX_BSTRC("F"),
421     _FX_BSTRC("Height"), _FX_BSTRC("H"),
422     _FX_BSTRC("ImageMask"), _FX_BSTRC("IM"),
423     _FX_BSTRC("Interpolate"), _FX_BSTRC("I"),
424     _FX_BSTRC("Width"), _FX_BSTRC("W"),
425 };
426 const _FX_BSTR _PDF_InlineValueAbbr[] = {
427     _FX_BSTRC("DeviceGray"), _FX_BSTRC("G"),
428     _FX_BSTRC("DeviceRGB"), _FX_BSTRC("RGB"),
429     _FX_BSTRC("DeviceCMYK"), _FX_BSTRC("CMYK"),
430     _FX_BSTRC("Indexed"), _FX_BSTRC("I"),
431     _FX_BSTRC("ASCIIHexDecode"), _FX_BSTRC("AHx"),
432     _FX_BSTRC("ASCII85Decode"), _FX_BSTRC("A85"),
433     _FX_BSTRC("LZWDecode"), _FX_BSTRC("LZW"),
434     _FX_BSTRC("FlateDecode"), _FX_BSTRC("Fl"),
435     _FX_BSTRC("RunLengthDecode"), _FX_BSTRC("RL"),
436     _FX_BSTRC("CCITTFaxDecode"), _FX_BSTRC("CCF"),
437     _FX_BSTRC("DCTDecode"), _FX_BSTRC("DCT"),
438 };
439 static CFX_ByteStringC _PDF_FindFullName(const _FX_BSTR* table, int count, const CFX_ByteStringC& abbr)
440 {
441     int i = 0;
442     while (i < count) {
443         if (abbr.GetLength() == table[i + 1].m_Size && FXSYS_memcmp(abbr.GetPtr(), table[i + 1].m_Ptr, abbr.GetLength()) == 0) {
444             return CFX_ByteStringC(table[i].m_Ptr, table[i].m_Size);
445         }
446         i += 2;
447     }
448     return CFX_ByteStringC();
449 }
450 void _PDF_ReplaceAbbr(CPDF_Object* pObj)
451 {
452     switch (pObj->GetType()) {
453         case PDFOBJ_DICTIONARY: {
454                 CPDF_Dictionary* pDict = (CPDF_Dictionary*)pObj;
455                 FX_POSITION pos = pDict->GetStartPos();
456                 while (pos) {
457                     CFX_ByteString key;
458                     CPDF_Object* value = pDict->GetNextElement(pos, key);
459                     CFX_ByteStringC fullname = _PDF_FindFullName(_PDF_InlineKeyAbbr,
460                                                sizeof _PDF_InlineKeyAbbr / sizeof(_FX_BSTR), key);
461                     if (!fullname.IsEmpty()) {
462                         pDict->ReplaceKey(key, fullname);
463                         key = fullname;
464                     }
465                     if (value->GetType() == PDFOBJ_NAME) {
466                         CFX_ByteString name = value->GetString();
467                         fullname = _PDF_FindFullName(_PDF_InlineValueAbbr,
468                                                      sizeof _PDF_InlineValueAbbr / sizeof(_FX_BSTR), name);
469                         if (!fullname.IsEmpty()) {
470                             pDict->SetAtName(key, fullname);
471                         }
472                     } else {
473                         _PDF_ReplaceAbbr(value);
474                     }
475                 }
476                 break;
477             }
478         case PDFOBJ_ARRAY: {
479                 CPDF_Array* pArray = (CPDF_Array*)pObj;
480                 for (FX_DWORD i = 0; i < pArray->GetCount(); i ++) {
481                     CPDF_Object* pElement = pArray->GetElement(i);
482                     if (pElement->GetType() == PDFOBJ_NAME) {
483                         CFX_ByteString name = pElement->GetString();
484                         CFX_ByteStringC fullname = _PDF_FindFullName(_PDF_InlineValueAbbr,
485                                                    sizeof _PDF_InlineValueAbbr / sizeof(_FX_BSTR), name);
486                         if (!fullname.IsEmpty()) {
487                             pArray->SetAt(i, CPDF_Name::Create(fullname));
488                         }
489                     } else {
490                         _PDF_ReplaceAbbr(pElement);
491                     }
492                 }
493                 break;
494             }
495     }
496 }
497 static CFX_ByteStringC _PDF_FindAbbrName(const _FX_BSTR* table, int count, const CFX_ByteStringC& fullName)
498 {
499     int i = 0;
500     while (i < count) {
501         if (fullName.GetLength() == table[i].m_Size && FXSYS_memcmp(fullName.GetPtr(), table[i].m_Ptr, fullName.GetLength()) == 0) {
502             return CFX_ByteStringC(table[i + 1].m_Ptr, table[i + 1].m_Size);
503         }
504         i += 2;
505     }
506     return CFX_ByteStringC();
507 }
508 void _PDF_ReplaceFull(CPDF_Object* pObj)
509 {
510     switch (pObj->GetType()) {
511         case PDFOBJ_DICTIONARY: {
512                 CPDF_Dictionary* pDict = (CPDF_Dictionary*)pObj;
513                 FX_POSITION pos = pDict->GetStartPos();
514                 while (pos) {
515                     CFX_ByteString key;
516                     CPDF_Object* value = pDict->GetNextElement(pos, key);
517                     CFX_ByteStringC abbrName = _PDF_FindAbbrName(_PDF_InlineKeyAbbr,
518                                                sizeof(_PDF_InlineKeyAbbr) / sizeof(_FX_BSTR), key);
519                     if (!abbrName.IsEmpty()) {
520                         pDict->ReplaceKey(key, abbrName);
521                         key = abbrName;
522                     }
523                     if (value->GetType() == PDFOBJ_NAME) {
524                         CFX_ByteString name = value->GetString();
525                         abbrName = _PDF_FindAbbrName(_PDF_InlineValueAbbr,
526                                                      sizeof(_PDF_InlineValueAbbr) / sizeof(_FX_BSTR), name);
527                         if (!abbrName.IsEmpty()) {
528                             pDict->SetAtName(key, abbrName);
529                         }
530                     } else {
531                         _PDF_ReplaceFull(value);
532                     }
533                 }
534                 break;
535             }
536         case PDFOBJ_ARRAY: {
537                 CPDF_Array* pArray = (CPDF_Array*)pObj;
538                 for (FX_DWORD i = 0; i < pArray->GetCount(); i ++) {
539                     CPDF_Object* pElement = pArray->GetElement(i);
540                     if (pElement->GetType() == PDFOBJ_NAME) {
541                         CFX_ByteString name = pElement->GetString();
542                         CFX_ByteStringC abbrName = _PDF_FindAbbrName(_PDF_InlineValueAbbr,
543                                                    sizeof _PDF_InlineValueAbbr / sizeof(_FX_BSTR), name);
544                         if (!abbrName.IsEmpty()) {
545                             pArray->SetAt(i, CPDF_Name::Create(abbrName));
546                         }
547                     } else {
548                         _PDF_ReplaceFull(pElement);
549                     }
550                 }
551                 break;
552             }
553     }
554 }
555 void CPDF_StreamContentParser::Handle_BeginText()
556 {
557     m_pCurStates->m_TextMatrix.Set(1.0f, 0, 0, 1.0f, 0, 0);
558     OnChangeTextMatrix();
559     m_pCurStates->m_TextX = 0;
560     m_pCurStates->m_TextY = 0;
561     m_pCurStates->m_TextLineX = 0;
562     m_pCurStates->m_TextLineY = 0;
563 }
564 void CPDF_StreamContentParser::Handle_BeginSectionUndefined()
565 {
566     m_CompatCount ++;
567 }
568 void CPDF_StreamContentParser::Handle_CurveTo_123()
569 {
570     if (m_Options.m_bTextOnly) {
571         return;
572     }
573     AddPathPoint(GetNumber(5), GetNumber(4), FXPT_BEZIERTO);
574     AddPathPoint(GetNumber(3), GetNumber(2), FXPT_BEZIERTO);
575     AddPathPoint(GetNumber(1), GetNumber(0), FXPT_BEZIERTO);
576 }
577 void CPDF_StreamContentParser::Handle_ConcatMatrix()
578 {
579     FX_FLOAT a2 = GetNumber16(5), b2 = GetNumber16(4), c2 = GetNumber16(3), d2 = GetNumber16(2);
580     FX_FLOAT e2 = GetNumber(1), f2 = GetNumber(0);
581     CFX_AffineMatrix new_matrix(a2, b2, c2, d2, e2, f2);
582     new_matrix.Concat(m_pCurStates->m_CTM);
583     m_pCurStates->m_CTM = new_matrix;
584     OnChangeTextMatrix();
585 }
586 void CPDF_StreamContentParser::Handle_SetColorSpace_Fill()
587 {
588     if (m_Options.m_bTextOnly) {
589         return;
590     }
591     CFX_ByteString csname = GetString(0);
592     CPDF_ColorSpace* pCS = FindColorSpace(csname);
593     if (pCS == NULL) {
594         return;
595     }
596     m_pCurStates->m_ColorState.GetModify()->m_FillColor.SetColorSpace(pCS);
597 }
598 void CPDF_StreamContentParser::Handle_SetColorSpace_Stroke()
599 {
600     if (m_Options.m_bTextOnly) {
601         return;
602     }
603     CFX_ByteString csname = GetString(0);
604     CPDF_ColorSpace* pCS = FindColorSpace(csname);
605     if (pCS == NULL) {
606         return;
607     }
608     m_pCurStates->m_ColorState.GetModify()->m_StrokeColor.SetColorSpace(pCS);
609 }
610 void CPDF_StreamContentParser::Handle_SetDash()
611 {
612     if (m_Options.m_bTextOnly) {
613         return;
614     }
615     CPDF_Array* pArray = GetObject(1) ? GetObject(1)->GetArray() : NULL;
616     if (pArray == NULL) {
617         return;
618     }
619     m_pCurStates->SetLineDash(pArray, GetNumber(0), 1.0f);
620 }
621 void CPDF_StreamContentParser::Handle_SetCharWidth()
622 {
623     m_Type3Data[0] = GetNumber(1);
624     m_Type3Data[1] = GetNumber(0);
625     m_bColored = TRUE;
626 }
627 void CPDF_StreamContentParser::Handle_SetCachedDevice()
628 {
629     for (int i = 0; i < 6; i ++) {
630         m_Type3Data[i] = GetNumber(5 - i);
631     }
632     m_bColored = FALSE;
633 }
634 void CPDF_StreamContentParser::Handle_ExecuteXObject()
635 {
636     CFX_ByteString name = GetString(0);
637     if (name == m_LastImageName && m_pLastImage && m_pLastImage->GetStream() && m_pLastImage->GetStream()->GetObjNum()) {
638         AddImage(NULL, m_pLastImage, FALSE);
639         return;
640     }
641     if (m_Options.m_bTextOnly) {
642         CPDF_Object* pRes = NULL;
643         if (m_pResources == NULL) {
644             return;
645         }
646         if (m_pResources == m_pPageResources) {
647             CPDF_Dictionary* pList = m_pResources->GetDict(FX_BSTRC("XObject"));
648             if (pList == NULL) {
649                 return;
650             }
651             pRes = pList->GetElement(name);
652             if (pRes == NULL || pRes->GetType() != PDFOBJ_REFERENCE) {
653                 return;
654             }
655         } else {
656             CPDF_Dictionary* pList = m_pResources->GetDict(FX_BSTRC("XObject"));
657             if (pList == NULL) {
658                 if (m_pPageResources == NULL) {
659                     return;
660                 }
661                 CPDF_Dictionary* pList = m_pPageResources->GetDict(FX_BSTRC("XObject"));
662                 if (pList == NULL) {
663                     return;
664                 }
665                 pRes = pList->GetElement(name);
666                 if (pRes == NULL || pRes->GetType() != PDFOBJ_REFERENCE) {
667                     return;
668                 }
669             } else {
670                 pRes = pList->GetElement(name);
671                 if (pRes == NULL || pRes->GetType() != PDFOBJ_REFERENCE) {
672                     return;
673                 }
674             }
675         }
676         FX_BOOL bForm;
677         if (m_pDocument->IsFormStream(((CPDF_Reference*)pRes)->GetRefObjNum(), bForm) && !bForm) {
678             return;
679         }
680     }
681     CPDF_Stream* pXObject = (CPDF_Stream*)FindResourceObj(FX_BSTRC("XObject"), name);
682     if (pXObject == NULL || pXObject->GetType() != PDFOBJ_STREAM) {
683         m_bResourceMissing = TRUE;
684         return;
685     }
686     CFX_ByteStringC type = pXObject->GetDict() ? pXObject->GetDict()->GetConstString(FX_BSTRC("Subtype")) : CFX_ByteStringC();
687     if (type == FX_BSTRC("Image")) {
688         if (m_Options.m_bTextOnly) {
689             return;
690         }
691         CPDF_ImageObject* pObj = AddImage(pXObject, NULL, FALSE);
692         m_LastImageName = name;
693         m_pLastImage = pObj->m_pImage;
694     } else if (type == FX_BSTRC("Form")) {
695         AddForm(pXObject);
696     } else {
697         return;
698     }
699 }
700 void CPDF_StreamContentParser::AddForm(CPDF_Stream* pStream)
701 {
702     if (!m_Options.m_bSeparateForm) {
703         CPDF_Dictionary* pResources = pStream->GetDict()->GetDict(FX_BSTRC("Resources"));
704         CFX_AffineMatrix form_matrix = pStream->GetDict()->GetMatrix(FX_BSTRC("Matrix"));
705         form_matrix.Concat(m_pCurStates->m_CTM);
706         CPDF_Array* pBBox = pStream->GetDict()->GetArray(FX_BSTRC("BBox"));
707         CFX_FloatRect form_bbox;
708         CPDF_Path ClipPath;
709         if (pBBox) {
710             form_bbox = pStream->GetDict()->GetRect(FX_BSTRC("BBox"));
711             ClipPath.New();
712             ClipPath.AppendRect(form_bbox.left, form_bbox.bottom, form_bbox.right, form_bbox.top);
713             ClipPath.Transform(&form_matrix);
714             form_bbox.Transform(&form_matrix);
715         }
716         CPDF_StreamContentParser parser(
717             m_pDocument, m_pPageResources, m_pResources, &m_mtContentToUser,
718             m_pObjectList, pResources, &form_bbox, &m_Options,
719             m_pCurStates.get(), m_Level + 1);
720         parser.m_pCurStates->m_CTM = form_matrix;
721         if (ClipPath.NotNull()) {
722             parser.m_pCurStates->m_ClipPath.AppendPath(ClipPath, FXFILL_WINDING, TRUE);
723         }
724         CPDF_StreamAcc stream;
725         stream.LoadAllData(pStream, FALSE);
726         if (stream.GetSize() == 0) {
727             return;
728         }
729         parser.Parse(stream.GetData(), stream.GetSize(), 0);
730         return;
731     }
732     CPDF_FormObject* pFormObj = new CPDF_FormObject;
733     pFormObj->m_pForm = new CPDF_Form(m_pDocument, m_pPageResources, pStream, m_pResources);
734     pFormObj->m_FormMatrix = m_pCurStates->m_CTM;
735     pFormObj->m_FormMatrix.Concat(m_mtContentToUser);
736     CPDF_AllStates status;
737     status.m_GeneralState = m_pCurStates->m_GeneralState;
738     status.m_GraphState = m_pCurStates->m_GraphState;
739     status.m_ColorState = m_pCurStates->m_ColorState;
740     status.m_TextState = m_pCurStates->m_TextState;
741     pFormObj->m_pForm->ParseContent(&status, NULL, NULL, &m_Options, m_Level + 1);
742     if (!m_pObjectList->m_bBackgroundAlphaNeeded && pFormObj->m_pForm->m_bBackgroundAlphaNeeded) {
743         m_pObjectList->m_bBackgroundAlphaNeeded = TRUE;
744     }
745     pFormObj->CalcBoundingBox();
746     SetGraphicStates(pFormObj, TRUE, TRUE, TRUE);
747     m_pObjectList->m_ObjectList.AddTail(pFormObj);
748 }
749 CPDF_ImageObject* CPDF_StreamContentParser::AddImage(CPDF_Stream* pStream, CPDF_Image* pImage, FX_BOOL bInline)
750 {
751     if (pStream == NULL && pImage == NULL) {
752         return NULL;
753     }
754     CFX_AffineMatrix ImageMatrix;
755     ImageMatrix.Copy(m_pCurStates->m_CTM);
756     ImageMatrix.Concat(m_mtContentToUser);
757     CPDF_ImageObject* pImageObj = new CPDF_ImageObject;
758     if (pImage) {
759         pImageObj->m_pImage = m_pDocument->GetPageData()->GetImage(pImage->GetStream());
760     } else if (pStream->GetObjNum()) {
761         pImageObj->m_pImage = m_pDocument->LoadImageF(pStream);
762     } else {
763         pImageObj->m_pImage = new CPDF_Image(m_pDocument);
764         pImageObj->m_pImage->LoadImageF(pStream, bInline);
765     }
766     SetGraphicStates(pImageObj, pImageObj->m_pImage->IsMask(), FALSE, FALSE);
767     pImageObj->m_Matrix = ImageMatrix;
768     pImageObj->CalcBoundingBox();
769     m_pObjectList->m_ObjectList.AddTail(pImageObj);
770     return pImageObj;
771 }
772 void CPDF_StreamContentParser::Handle_MarkPlace_Dictionary()
773 {
774 }
775 void CPDF_StreamContentParser::Handle_EndImage()
776 {
777 }
778 void CPDF_StreamContentParser::Handle_EndMarkedContent()
779 {
780     if (!m_Options.m_bMarkedContent) {
781         return;
782     }
783     if (m_CurContentMark.IsNull()) {
784         return;
785     }
786     int count = m_CurContentMark.GetObject()->CountItems();
787     if (count == 1) {
788         m_CurContentMark.SetNull();
789         return;
790     }
791     m_CurContentMark.GetModify()->DeleteLastMark();
792 }
793 void CPDF_StreamContentParser::Handle_EndText()
794 {
795     int count = m_ClipTextList.GetSize();
796     if (count == 0) {
797         return;
798     }
799     if (m_pCurStates->m_TextState.GetObject()->m_TextMode < 4) {
800         for (int i = 0; i < count; i ++) {
801             CPDF_TextObject* pText = (CPDF_TextObject*)m_ClipTextList.GetAt(i);
802             delete pText;
803         }
804     } else {
805         m_pCurStates->m_ClipPath.AppendTexts((CPDF_TextObject**)m_ClipTextList.GetData(), count);
806     }
807     m_ClipTextList.RemoveAll();
808 }
809 void CPDF_StreamContentParser::Handle_EndSectionUndefined()
810 {
811     if (m_CompatCount) {
812         m_CompatCount --;
813     }
814 }
815 void CPDF_StreamContentParser::Handle_FillPath()
816 {
817     if (m_Options.m_bTextOnly) {
818         return;
819     }
820     AddPathObject(FXFILL_WINDING, FALSE);
821 }
822 void CPDF_StreamContentParser::Handle_FillPathOld()
823 {
824     if (m_Options.m_bTextOnly) {
825         return;
826     }
827     AddPathObject(FXFILL_WINDING, FALSE);
828 }
829 void CPDF_StreamContentParser::Handle_EOFillPath()
830 {
831     if (m_Options.m_bTextOnly) {
832         return;
833     }
834     AddPathObject(FXFILL_ALTERNATE, FALSE);
835 }
836 void CPDF_StreamContentParser::Handle_SetGray_Fill()
837 {
838     FX_FLOAT value = GetNumber(0);
839     CPDF_ColorSpace* pCS = CPDF_ColorSpace::GetStockCS(PDFCS_DEVICEGRAY);
840     m_pCurStates->m_ColorState.SetFillColor(pCS, &value, 1);
841 }
842 void CPDF_StreamContentParser::Handle_SetGray_Stroke()
843 {
844     FX_FLOAT value = GetNumber(0);
845     CPDF_ColorSpace* pCS = CPDF_ColorSpace::GetStockCS(PDFCS_DEVICEGRAY);
846     m_pCurStates->m_ColorState.SetStrokeColor(pCS, &value, 1);
847 }
848 void CPDF_StreamContentParser::Handle_SetExtendGraphState()
849 {
850     CFX_ByteString name = GetString(0);
851     CPDF_Dictionary* pGS = (CPDF_Dictionary*)FindResourceObj(FX_BSTRC("ExtGState"), name);
852     if (pGS == NULL || pGS->GetType() != PDFOBJ_DICTIONARY) {
853         m_bResourceMissing = TRUE;
854         return;
855     }
856     m_pCurStates->ProcessExtGS(pGS, this);
857 }
858 void CPDF_StreamContentParser::Handle_ClosePath()
859 {
860     if (m_Options.m_bTextOnly) {
861         return;
862     }
863     if (m_PathPointCount == 0) {
864         return;
865     }
866     if (m_PathStartX != m_PathCurrentX || m_PathStartY != m_PathCurrentY) {
867         AddPathPoint(m_PathStartX, m_PathStartY, FXPT_LINETO | FXPT_CLOSEFIGURE);
868     } else if (m_pPathPoints[m_PathPointCount - 1].m_Flag != FXPT_MOVETO) {
869         m_pPathPoints[m_PathPointCount - 1].m_Flag |= FXPT_CLOSEFIGURE;
870     }
871 }
872 void CPDF_StreamContentParser::Handle_SetFlat()
873 {
874     m_pCurStates->m_GeneralState.GetModify()->m_Flatness = GetNumber(0);
875 }
876 void CPDF_StreamContentParser::Handle_BeginImageData()
877 {
878 }
879 void CPDF_StreamContentParser::Handle_SetLineJoin()
880 {
881     m_pCurStates->m_GraphState.GetModify()->m_LineJoin = (CFX_GraphStateData::LineJoin)GetInteger(0);
882 }
883 void CPDF_StreamContentParser::Handle_SetLineCap()
884 {
885     m_pCurStates->m_GraphState.GetModify()->m_LineCap = (CFX_GraphStateData::LineCap)GetInteger(0);
886 }
887 void CPDF_StreamContentParser::Handle_SetCMYKColor_Fill()
888 {
889     REQUIRE_PARAMS(4);
890     FX_FLOAT values[4];
891     for (int i = 0; i < 4; i ++) {
892         values[i] = GetNumber(3 - i);
893     }
894     CPDF_ColorSpace* pCS = CPDF_ColorSpace::GetStockCS(PDFCS_DEVICECMYK);
895     m_pCurStates->m_ColorState.SetFillColor(pCS, values, 4);
896 }
897 void CPDF_StreamContentParser::Handle_SetCMYKColor_Stroke()
898 {
899     REQUIRE_PARAMS(4);
900     FX_FLOAT values[4];
901     for (int i = 0; i < 4; i ++) {
902         values[i] = GetNumber(3 - i);
903     }
904     CPDF_ColorSpace* pCS = CPDF_ColorSpace::GetStockCS(PDFCS_DEVICECMYK);
905     m_pCurStates->m_ColorState.SetStrokeColor(pCS, values, 4);
906 }
907 void CPDF_StreamContentParser::Handle_LineTo()
908 {
909     REQUIRE_PARAMS(2);
910     if (m_Options.m_bTextOnly) {
911         return;
912     }
913     AddPathPoint(GetNumber(1), GetNumber(0), FXPT_LINETO);
914 }
915 void CPDF_StreamContentParser::Handle_MoveTo()
916 {
917     REQUIRE_PARAMS(2);
918     if (m_Options.m_bTextOnly) {
919         m_pSyntax->SkipPathObject();
920         return;
921     }
922     AddPathPoint(GetNumber(1), GetNumber(0), FXPT_MOVETO);
923     ParsePathObject();
924 }
925 void CPDF_StreamContentParser::Handle_SetMiterLimit()
926 {
927     m_pCurStates->m_GraphState.GetModify()->m_MiterLimit = GetNumber(0);
928 }
929 void CPDF_StreamContentParser::Handle_MarkPlace()
930 {
931 }
932 void CPDF_StreamContentParser::Handle_EndPath()
933 {
934     if (m_Options.m_bTextOnly) {
935         return;
936     }
937     AddPathObject(0, FALSE);
938 }
939 void CPDF_StreamContentParser::Handle_SaveGraphState()
940 {
941     CPDF_AllStates* pStates = new CPDF_AllStates;
942     pStates->Copy(*m_pCurStates);
943     m_StateStack.Add(pStates);
944 }
945 void CPDF_StreamContentParser::Handle_RestoreGraphState()
946 {
947     int size = m_StateStack.GetSize();
948     if (size == 0) {
949         return;
950     }
951     CPDF_AllStates* pStates = (CPDF_AllStates*)m_StateStack.GetAt(size - 1);
952     m_pCurStates->Copy(*pStates);
953     delete pStates;
954     m_StateStack.RemoveAt(size - 1);
955 }
956 void CPDF_StreamContentParser::Handle_Rectangle()
957 {
958     if (m_Options.m_bTextOnly) {
959         return;
960     }
961     FX_FLOAT x = GetNumber(3), y = GetNumber(2);
962     FX_FLOAT w = GetNumber(1), h = GetNumber(0);
963     AddPathRect(x, y, w, h);
964 }
965 void CPDF_StreamContentParser::AddPathRect(FX_FLOAT x, FX_FLOAT y, FX_FLOAT w, FX_FLOAT h)
966 {
967     AddPathPoint(x, y, FXPT_MOVETO);
968     AddPathPoint(x + w, y, FXPT_LINETO);
969     AddPathPoint(x + w, y + h, FXPT_LINETO);
970     AddPathPoint(x, y + h, FXPT_LINETO);
971     AddPathPoint(x, y, FXPT_LINETO | FXPT_CLOSEFIGURE);
972 }
973 void CPDF_StreamContentParser::Handle_SetRGBColor_Fill()
974 {
975     REQUIRE_PARAMS(3);
976     FX_FLOAT values[3];
977     for (int i = 0; i < 3; i ++) {
978         values[i] = GetNumber(2 - i);
979     }
980     CPDF_ColorSpace* pCS = CPDF_ColorSpace::GetStockCS(PDFCS_DEVICERGB);
981     m_pCurStates->m_ColorState.SetFillColor(pCS, values, 3);
982 }
983 void CPDF_StreamContentParser::Handle_SetRGBColor_Stroke()
984 {
985     REQUIRE_PARAMS(3);
986     FX_FLOAT values[3];
987     for (int i = 0; i < 3; i ++) {
988         values[i] = GetNumber(2 - i);
989     }
990     CPDF_ColorSpace* pCS = CPDF_ColorSpace::GetStockCS(PDFCS_DEVICERGB);
991     m_pCurStates->m_ColorState.SetStrokeColor(pCS, values, 3);
992 }
993 void CPDF_StreamContentParser::Handle_SetRenderIntent()
994 {
995 }
996 void CPDF_StreamContentParser::Handle_CloseStrokePath()
997 {
998     if (m_Options.m_bTextOnly) {
999         return;
1000     }
1001     Handle_ClosePath();
1002     AddPathObject(0, TRUE);
1003 }
1004 void CPDF_StreamContentParser::Handle_StrokePath()
1005 {
1006     if (m_Options.m_bTextOnly) {
1007         return;
1008     }
1009     AddPathObject(0, TRUE);
1010 }
1011 void CPDF_StreamContentParser::Handle_SetColor_Fill()
1012 {
1013     if (m_Options.m_bTextOnly) {
1014         return;
1015     }
1016     FX_FLOAT values[4];
1017     int nargs = m_ParamCount;
1018     if (nargs > 4) {
1019         nargs = 4;
1020     }
1021     for (int i = 0; i < nargs; i ++) {
1022         values[i] = GetNumber(nargs - i - 1);
1023     }
1024     m_pCurStates->m_ColorState.SetFillColor(NULL, values, nargs);
1025 }
1026 void CPDF_StreamContentParser::Handle_SetColor_Stroke()
1027 {
1028     if (m_Options.m_bTextOnly) {
1029         return;
1030     }
1031     FX_FLOAT values[4];
1032     int nargs = m_ParamCount;
1033     if (nargs > 4) {
1034         nargs = 4;
1035     }
1036     for (int i = 0; i < nargs; i ++) {
1037         values[i] = GetNumber(nargs - i - 1);
1038     }
1039     m_pCurStates->m_ColorState.SetStrokeColor(NULL, values, nargs);
1040 }
1041 void CPDF_StreamContentParser::Handle_SetColorPS_Fill()
1042 {
1043     if (m_Options.m_bTextOnly) {
1044         return;
1045     }
1046     CPDF_Object* pLastParam = GetObject(0);
1047     if (pLastParam == NULL) {
1048         return;
1049     }
1050     int nargs = m_ParamCount;
1051     int nvalues = nargs;
1052     if (pLastParam->GetType() == PDFOBJ_NAME) {
1053         nvalues --;
1054     }
1055     FX_FLOAT* values = NULL;
1056     if (nvalues) {
1057         values = FX_Alloc(FX_FLOAT, nvalues);
1058         for (int i = 0; i < nvalues; i ++) {
1059             values[i] = GetNumber(nargs - i - 1);
1060         }
1061     }
1062     if (nvalues != nargs) {
1063         CPDF_Pattern* pPattern = FindPattern(GetString(0), FALSE);
1064         if (pPattern) {
1065             m_pCurStates->m_ColorState.SetFillPattern(pPattern, values, nvalues);
1066         }
1067     } else {
1068         m_pCurStates->m_ColorState.SetFillColor(NULL, values, nvalues);
1069     }
1070     if (values) {
1071         FX_Free(values);
1072     }
1073 }
1074 void CPDF_StreamContentParser::Handle_SetColorPS_Stroke()
1075 {
1076     if (m_Options.m_bTextOnly) {
1077         return;
1078     }
1079     CPDF_Object* pLastParam = GetObject(0);
1080     if (pLastParam == NULL) {
1081         return;
1082     }
1083     int nargs = m_ParamCount;
1084     int nvalues = nargs;
1085     if (pLastParam->GetType() == PDFOBJ_NAME) {
1086         nvalues --;
1087     }
1088     FX_FLOAT* values = NULL;
1089     if (nvalues) {
1090         values = FX_Alloc(FX_FLOAT, nvalues);
1091         for (int i = 0; i < nvalues; i ++) {
1092             values[i] = GetNumber(nargs - i - 1);
1093         }
1094     }
1095     if (nvalues != nargs) {
1096         CPDF_Pattern* pPattern = FindPattern(GetString(0), FALSE);
1097         if (pPattern) {
1098             m_pCurStates->m_ColorState.SetStrokePattern(pPattern, values, nvalues);
1099         }
1100     } else {
1101         m_pCurStates->m_ColorState.SetStrokeColor(NULL, values, nvalues);
1102     }
1103     if (values) {
1104         FX_Free(values);
1105     }
1106 }
1107 CFX_FloatRect _GetShadingBBox(CPDF_Stream* pStream, int type, const CFX_AffineMatrix* pMatrix,
1108                               CPDF_Function** pFuncs, int nFuncs, CPDF_ColorSpace* pCS);
1109 void CPDF_StreamContentParser::Handle_ShadeFill()
1110 {
1111     if (m_Options.m_bTextOnly) {
1112         return;
1113     }
1114     CPDF_Pattern* pPattern = FindPattern(GetString(0), TRUE);
1115     if (pPattern == NULL) {
1116         return;
1117     }
1118     if (pPattern->m_PatternType != PATTERN_SHADING) {
1119         return;
1120     }
1121     CPDF_ShadingPattern* pShading = (CPDF_ShadingPattern*)pPattern;
1122     if (!pShading->m_bShadingObj) {
1123         return;
1124     }
1125     if (!pShading->Load()) {
1126         return;
1127     }
1128     CPDF_ShadingObject* pObj = new CPDF_ShadingObject;
1129     pObj->m_pShading = pShading;
1130     SetGraphicStates(pObj, FALSE, FALSE, FALSE);
1131     pObj->m_Matrix = m_pCurStates->m_CTM;
1132     pObj->m_Matrix.Concat(m_mtContentToUser);
1133     CFX_FloatRect bbox;
1134     if (!pObj->m_ClipPath.IsNull()) {
1135         bbox = pObj->m_ClipPath.GetClipBox();
1136     } else {
1137         bbox = m_BBox;
1138     }
1139     if (pShading->m_ShadingType >= 4) {
1140         bbox.Intersect(_GetShadingBBox((CPDF_Stream*)pShading->m_pShadingObj, pShading->m_ShadingType, &pObj->m_Matrix,
1141                                        pShading->m_pFunctions, pShading->m_nFuncs, pShading->m_pCS));
1142     }
1143     pObj->m_Left = bbox.left;
1144     pObj->m_Right = bbox.right;
1145     pObj->m_Top = bbox.top;
1146     pObj->m_Bottom = bbox.bottom;
1147     m_pObjectList->m_ObjectList.AddTail(pObj);
1148 }
1149 void CPDF_StreamContentParser::Handle_SetCharSpace()
1150 {
1151     m_pCurStates->m_TextState.GetModify()->m_CharSpace = GetNumber(0);
1152 }
1153 void CPDF_StreamContentParser::Handle_MoveTextPoint()
1154 {
1155     m_pCurStates->m_TextLineX += GetNumber(1);
1156     m_pCurStates->m_TextLineY += GetNumber(0);
1157     m_pCurStates->m_TextX = m_pCurStates->m_TextLineX;
1158     m_pCurStates->m_TextY = m_pCurStates->m_TextLineY;
1159 }
1160 void CPDF_StreamContentParser::Handle_MoveTextPoint_SetLeading()
1161 {
1162     Handle_MoveTextPoint();
1163     m_pCurStates->m_TextLeading = -GetNumber(0);
1164 }
1165 void CPDF_StreamContentParser::Handle_SetFont()
1166 {
1167     FX_FLOAT fs = GetNumber(0);
1168     if (fs == 0) {
1169         fs = m_DefFontSize;
1170     }
1171     m_pCurStates->m_TextState.GetModify()->m_FontSize = fs;
1172     CPDF_Font* pFont = FindFont(GetString(1));
1173     if (pFont) {
1174         m_pCurStates->m_TextState.SetFont(pFont);
1175     }
1176 }
1177 CPDF_Object* CPDF_StreamContentParser::FindResourceObj(const CFX_ByteStringC& type, const CFX_ByteString& name)
1178 {
1179     if (m_pResources == NULL) {
1180         return NULL;
1181     }
1182     if (m_pResources == m_pPageResources) {
1183         CPDF_Dictionary* pList = m_pResources->GetDict(type);
1184         if (pList == NULL) {
1185             return NULL;
1186         }
1187         CPDF_Object* pRes = pList->GetElementValue(name);
1188         return pRes;
1189     }
1190     CPDF_Dictionary* pList = m_pResources->GetDict(type);
1191     if (pList == NULL) {
1192         if (m_pPageResources == NULL) {
1193             return NULL;
1194         }
1195         CPDF_Dictionary* pList = m_pPageResources->GetDict(type);
1196         if (pList == NULL) {
1197             return NULL;
1198         }
1199         CPDF_Object* pRes = pList->GetElementValue(name);
1200         return pRes;
1201     }
1202     CPDF_Object* pRes = pList->GetElementValue(name);
1203     return pRes;
1204 }
1205 CPDF_Font* CPDF_StreamContentParser::FindFont(const CFX_ByteString& name)
1206 {
1207     CPDF_Dictionary* pFontDict = (CPDF_Dictionary*)FindResourceObj(FX_BSTRC("Font"), name);
1208     if (pFontDict == NULL || pFontDict->GetType() != PDFOBJ_DICTIONARY) {
1209         m_bResourceMissing = TRUE;
1210         return CPDF_Font::GetStockFont(m_pDocument, FX_BSTRC("Helvetica"));
1211     }
1212     CPDF_Font* pFont = m_pDocument->LoadFont(pFontDict);
1213     if (pFont && pFont->GetType3Font()) {
1214         pFont->GetType3Font()->SetPageResources(m_pResources);
1215         pFont->GetType3Font()->CheckType3FontMetrics();
1216     }
1217     return pFont;
1218 }
1219 CPDF_ColorSpace* CPDF_StreamContentParser::FindColorSpace(const CFX_ByteString& name)
1220 {
1221     if (name == FX_BSTRC("Pattern")) {
1222         return CPDF_ColorSpace::GetStockCS(PDFCS_PATTERN);
1223     }
1224     if (name == FX_BSTRC("DeviceGray") || name == FX_BSTRC("DeviceCMYK") || name == FX_BSTRC("DeviceRGB")) {
1225         CFX_ByteString defname = "Default";
1226         defname += name.Mid(7);
1227         CPDF_Object* pDefObj = FindResourceObj(FX_BSTRC("ColorSpace"), defname);
1228         if (pDefObj == NULL) {
1229             if (name == FX_BSTRC("DeviceGray")) {
1230                 return CPDF_ColorSpace::GetStockCS(PDFCS_DEVICEGRAY);
1231             }
1232             if (name == FX_BSTRC("DeviceRGB")) {
1233                 return CPDF_ColorSpace::GetStockCS(PDFCS_DEVICERGB);
1234             }
1235             return CPDF_ColorSpace::GetStockCS(PDFCS_DEVICECMYK);
1236         }
1237         return m_pDocument->LoadColorSpace(pDefObj);
1238     }
1239     CPDF_Object* pCSObj = FindResourceObj(FX_BSTRC("ColorSpace"), name);
1240     if (pCSObj == NULL) {
1241         m_bResourceMissing = TRUE;
1242         return NULL;
1243     }
1244     return m_pDocument->LoadColorSpace(pCSObj);
1245 }
1246 CPDF_Pattern* CPDF_StreamContentParser::FindPattern(const CFX_ByteString& name, FX_BOOL bShading)
1247 {
1248     CPDF_Object* pPattern = FindResourceObj(bShading ? FX_BSTRC("Shading") : FX_BSTRC("Pattern"), name);
1249     if (pPattern == NULL || (pPattern->GetType() != PDFOBJ_DICTIONARY &&
1250                              pPattern->GetType() != PDFOBJ_STREAM)) {
1251         m_bResourceMissing = TRUE;
1252         return NULL;
1253     }
1254     return m_pDocument->LoadPattern(pPattern, bShading, &m_pCurStates->m_ParentMatrix);
1255 }
1256 void CPDF_StreamContentParser::ConvertTextSpace(FX_FLOAT& x, FX_FLOAT& y)
1257 {
1258     m_pCurStates->m_TextMatrix.Transform(x, y, x, y);
1259     ConvertUserSpace(x, y);
1260 }
1261 void CPDF_StreamContentParser::ConvertUserSpace(FX_FLOAT& x, FX_FLOAT& y)
1262 {
1263     m_pCurStates->m_CTM.Transform(x, y, x, y);
1264     m_mtContentToUser.Transform(x, y, x, y);
1265 }
1266 void CPDF_StreamContentParser::AddTextObject(CFX_ByteString* pStrs, FX_FLOAT fInitKerning, FX_FLOAT* pKerning, int nsegs)
1267 {
1268     CPDF_Font* pFont = m_pCurStates->m_TextState.GetFont();
1269     if (pFont == NULL) {
1270         return;
1271     }
1272     if (fInitKerning != 0) {
1273         if (!pFont->IsVertWriting()) {
1274             m_pCurStates->m_TextX -= FXSYS_Mul(fInitKerning, m_pCurStates->m_TextState.GetFontSize()) / 1000;
1275         } else {
1276             m_pCurStates->m_TextY -= FXSYS_Mul(fInitKerning, m_pCurStates->m_TextState.GetFontSize()) / 1000;
1277         }
1278     }
1279     if (nsegs == 0) {
1280         return;
1281     }
1282     int textmode;
1283     if (pFont->GetFontType() == PDFFONT_TYPE3) {
1284         textmode = 0;
1285     } else {
1286         textmode = m_pCurStates->m_TextState.GetObject()->m_TextMode;
1287     }
1288     CPDF_TextObject* pText = new CPDF_TextObject;
1289     m_pLastTextObject = pText;
1290     SetGraphicStates(pText, TRUE, TRUE, TRUE);
1291     if (textmode && textmode != 3 && textmode != 4 && textmode != 7) {
1292         FX_FLOAT* pCTM = pText->m_TextState.GetModify()->m_CTM;
1293         pCTM[0] = m_pCurStates->m_CTM.a;
1294         pCTM[1] = m_pCurStates->m_CTM.c;
1295         pCTM[2] = m_pCurStates->m_CTM.b;
1296         pCTM[3] = m_pCurStates->m_CTM.d;
1297     }
1298     pText->SetSegments(pStrs, pKerning, nsegs);
1299     pText->m_PosX = m_pCurStates->m_TextX;
1300     pText->m_PosY = m_pCurStates->m_TextY + m_pCurStates->m_TextRise;
1301     ConvertTextSpace(pText->m_PosX, pText->m_PosY);
1302     FX_FLOAT x_advance, y_advance;
1303     pText->CalcPositionData(&x_advance, &y_advance, m_pCurStates->m_TextHorzScale, m_Level);
1304     m_pCurStates->m_TextX += x_advance;
1305     m_pCurStates->m_TextY += y_advance;
1306     if (textmode > 3) {
1307         CPDF_TextObject* pCopy = new CPDF_TextObject;
1308         pCopy->Copy(pText);
1309         m_ClipTextList.Add(pCopy);
1310     }
1311     m_pObjectList->m_ObjectList.AddTail(pText);
1312     if (pKerning && pKerning[nsegs - 1] != 0) {
1313         if (!pFont->IsVertWriting()) {
1314             m_pCurStates->m_TextX -= FXSYS_Mul(pKerning[nsegs - 1], m_pCurStates->m_TextState.GetFontSize()) / 1000;
1315         } else {
1316             m_pCurStates->m_TextY -= FXSYS_Mul(pKerning[nsegs - 1], m_pCurStates->m_TextState.GetFontSize()) / 1000;
1317         }
1318     }
1319 }
1320 void CPDF_StreamContentParser::Handle_ShowText()
1321 {
1322     CFX_ByteString str = GetString(0);
1323     if (str.IsEmpty()) {
1324         return;
1325     }
1326     AddTextObject(&str, 0, NULL, 1);
1327 }
1328 void CPDF_StreamContentParser::Handle_ShowText_Positioning()
1329 {
1330     CPDF_Array* pArray = GetObject(0) ? GetObject(0)->GetArray() : NULL;
1331     if (pArray == NULL) {
1332         return;
1333     }
1334     int n = pArray->GetCount(), nsegs = 0, i;
1335     for (i = 0; i < n; i ++) {
1336         CPDF_Object* pObj = pArray->GetElementValue(i);
1337         if (pObj->GetType() == PDFOBJ_STRING) {
1338             nsegs ++;
1339         }
1340     }
1341     if (nsegs == 0) {
1342         for (i = 0; i < n; i ++) {
1343             m_pCurStates->m_TextX -= FXSYS_Mul(pArray->GetNumber(i), m_pCurStates->m_TextState.GetFontSize()) / 1000;
1344         };
1345         return;
1346     }
1347     CFX_ByteString* pStrs = new CFX_ByteString[nsegs];
1348     FX_FLOAT* pKerning = FX_Alloc(FX_FLOAT, nsegs);
1349     int iSegment = 0;
1350     FX_FLOAT fInitKerning = 0;
1351     for (i = 0; i < n; i ++) {
1352         CPDF_Object* pObj = pArray->GetElementValue(i);
1353         if (pObj->GetType() == PDFOBJ_STRING) {
1354             CFX_ByteString str = pObj->GetString();
1355             if (str.IsEmpty()) {
1356                 continue;
1357             }
1358             pStrs[iSegment] = str;
1359             pKerning[iSegment ++] = 0;
1360         } else {
1361             FX_FLOAT num = pObj ? pObj->GetNumber() : 0;
1362             if (iSegment == 0) {
1363                 fInitKerning += num;
1364             } else {
1365                 pKerning[iSegment - 1] += num;
1366             }
1367         }
1368     }
1369     AddTextObject(pStrs, fInitKerning, pKerning, iSegment);
1370     delete[] pStrs;
1371     FX_Free(pKerning);
1372 }
1373 void CPDF_StreamContentParser::Handle_SetTextLeading()
1374 {
1375     m_pCurStates->m_TextLeading = GetNumber(0);
1376 }
1377 void CPDF_StreamContentParser::Handle_SetTextMatrix()
1378 {
1379     m_pCurStates->m_TextMatrix.Set(GetNumber16(5), GetNumber16(4), GetNumber16(3),
1380                                    GetNumber16(2), GetNumber(1), GetNumber(0));
1381     OnChangeTextMatrix();
1382     m_pCurStates->m_TextX = 0;
1383     m_pCurStates->m_TextY = 0;
1384     m_pCurStates->m_TextLineX = 0;
1385     m_pCurStates->m_TextLineY = 0;
1386 }
1387 void CPDF_StreamContentParser::OnChangeTextMatrix()
1388 {
1389     CFX_AffineMatrix text_matrix(m_pCurStates->m_TextHorzScale, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
1390     text_matrix.Concat(m_pCurStates->m_TextMatrix);
1391     text_matrix.Concat(m_pCurStates->m_CTM);
1392     text_matrix.Concat(m_mtContentToUser);
1393     FX_FLOAT* pTextMatrix = m_pCurStates->m_TextState.GetModify()->m_Matrix;
1394     pTextMatrix[0] = text_matrix.a;
1395     pTextMatrix[1] = text_matrix.c;
1396     pTextMatrix[2] = text_matrix.b;
1397     pTextMatrix[3] = text_matrix.d;
1398 }
1399 void CPDF_StreamContentParser::Handle_SetTextRenderMode()
1400 {
1401     int mode = GetInteger(0);
1402     if (mode < 0 || mode > 7) {
1403         return;
1404     }
1405     m_pCurStates->m_TextState.GetModify()->m_TextMode = mode;
1406 }
1407 void CPDF_StreamContentParser::Handle_SetTextRise()
1408 {
1409     m_pCurStates->m_TextRise = GetNumber(0);
1410 }
1411 void CPDF_StreamContentParser::Handle_SetWordSpace()
1412 {
1413     m_pCurStates->m_TextState.GetModify()->m_WordSpace = GetNumber(0);
1414 }
1415 void CPDF_StreamContentParser::Handle_SetHorzScale()
1416 {
1417     if (m_ParamCount != 1) {
1418         return;
1419     }
1420     m_pCurStates->m_TextHorzScale = GetNumber(0) / 100;
1421     OnChangeTextMatrix();
1422 }
1423 void CPDF_StreamContentParser::Handle_MoveToNextLine()
1424 {
1425     m_pCurStates->m_TextLineY -= m_pCurStates->m_TextLeading;
1426     m_pCurStates->m_TextX = m_pCurStates->m_TextLineX;
1427     m_pCurStates->m_TextY = m_pCurStates->m_TextLineY;
1428 }
1429 void CPDF_StreamContentParser::Handle_CurveTo_23()
1430 {
1431     if (m_Options.m_bTextOnly) {
1432         return;
1433     }
1434     AddPathPoint(m_PathCurrentX, m_PathCurrentY, FXPT_BEZIERTO);
1435     AddPathPoint(GetNumber(3), GetNumber(2), FXPT_BEZIERTO);
1436     AddPathPoint(GetNumber(1), GetNumber(0), FXPT_BEZIERTO);
1437 }
1438 void CPDF_StreamContentParser::Handle_SetLineWidth()
1439 {
1440     FX_FLOAT width = GetNumber(0);
1441     m_pCurStates->m_GraphState.GetModify()->m_LineWidth = width;
1442 }
1443 void CPDF_StreamContentParser::Handle_Clip()
1444 {
1445     m_PathClipType = FXFILL_WINDING;
1446 }
1447 void CPDF_StreamContentParser::Handle_EOClip()
1448 {
1449     m_PathClipType = FXFILL_ALTERNATE;
1450 }
1451 void CPDF_StreamContentParser::Handle_CurveTo_13()
1452 {
1453     if (m_Options.m_bTextOnly) {
1454         return;
1455     }
1456     AddPathPoint(GetNumber(3), GetNumber(2), FXPT_BEZIERTO);
1457     AddPathPoint(GetNumber(1), GetNumber(0), FXPT_BEZIERTO);
1458     AddPathPoint(GetNumber(1), GetNumber(0), FXPT_BEZIERTO);
1459 }
1460 void CPDF_StreamContentParser::Handle_NextLineShowText()
1461 {
1462     Handle_MoveToNextLine();
1463     Handle_ShowText();
1464 }
1465 void CPDF_StreamContentParser::Handle_NextLineShowText_Space()
1466 {
1467     m_pCurStates->m_TextState.GetModify()->m_WordSpace = GetNumber(2);
1468     m_pCurStates->m_TextState.GetModify()->m_CharSpace = GetNumber(1);
1469     Handle_NextLineShowText();
1470 }
1471 void CPDF_StreamContentParser::Handle_Invalid()
1472 {
1473 }
1474 void CPDF_StreamContentParser::AddPathPoint(FX_FLOAT x, FX_FLOAT y, int flag)
1475 {
1476     m_PathCurrentX = x;
1477     m_PathCurrentY = y;
1478     if (flag == FXPT_MOVETO) {
1479         m_PathStartX = x;
1480         m_PathStartY = y;
1481         if (m_PathPointCount && m_pPathPoints[m_PathPointCount - 1].m_Flag == FXPT_MOVETO) {
1482             m_pPathPoints[m_PathPointCount - 1].m_PointX = x;
1483             m_pPathPoints[m_PathPointCount - 1].m_PointY = y;
1484             return;
1485         }
1486     } else if (m_PathPointCount == 0) {
1487         return;
1488     }
1489     m_PathPointCount ++;
1490     if (m_PathPointCount > m_PathAllocSize) {
1491         int newsize = m_PathPointCount + 256;
1492         FX_PATHPOINT* pNewPoints = FX_Alloc(FX_PATHPOINT, newsize);
1493         if (m_PathAllocSize) {
1494             FXSYS_memcpy(pNewPoints, m_pPathPoints, m_PathAllocSize * sizeof(FX_PATHPOINT));
1495             FX_Free(m_pPathPoints);
1496         }
1497         m_pPathPoints = pNewPoints;
1498         m_PathAllocSize = newsize;
1499     }
1500     m_pPathPoints[m_PathPointCount - 1].m_Flag = flag;
1501     m_pPathPoints[m_PathPointCount - 1].m_PointX = x;
1502     m_pPathPoints[m_PathPointCount - 1].m_PointY = y;
1503 }
1504 void CPDF_StreamContentParser::AddPathObject(int FillType, FX_BOOL bStroke)
1505 {
1506     int PathPointCount = m_PathPointCount, PathClipType = m_PathClipType;
1507     m_PathPointCount = 0;
1508     m_PathClipType = 0;
1509     if (PathPointCount <= 1) {
1510         if (PathPointCount && PathClipType) {
1511             CPDF_Path path;
1512             path.New()->AppendRect(0, 0, 0, 0);
1513             m_pCurStates->m_ClipPath.AppendPath(path, FXFILL_WINDING, TRUE);
1514         }
1515         return;
1516     }
1517     if (PathPointCount && m_pPathPoints[PathPointCount - 1].m_Flag == FXPT_MOVETO) {
1518         PathPointCount --;
1519     }
1520     CPDF_Path Path;
1521     CFX_PathData* pPathData = Path.New();
1522     pPathData->SetPointCount(PathPointCount);
1523     FXSYS_memcpy(pPathData->GetPoints(), m_pPathPoints, sizeof(FX_PATHPOINT) * PathPointCount);
1524     CFX_AffineMatrix matrix = m_pCurStates->m_CTM;
1525     matrix.Concat(m_mtContentToUser);
1526     if (bStroke || FillType) {
1527         CPDF_PathObject* pPathObj = new CPDF_PathObject;
1528         pPathObj->m_bStroke = bStroke;
1529         pPathObj->m_FillType = FillType;
1530         pPathObj->m_Path = Path;
1531         pPathObj->m_Matrix = matrix;
1532         SetGraphicStates(pPathObj, TRUE, FALSE, TRUE);
1533         pPathObj->CalcBoundingBox();
1534         m_pObjectList->m_ObjectList.AddTail(pPathObj);
1535     }
1536     if (PathClipType) {
1537         if (!matrix.IsIdentity()) {
1538             Path.Transform(&matrix);
1539             matrix.SetIdentity();
1540         }
1541         m_pCurStates->m_ClipPath.AppendPath(Path, PathClipType, TRUE);
1542     }
1543 }
1544 CFX_ByteString _FPDF_ByteStringFromHex(CFX_BinaryBuf& src_buf)
1545 {
1546     CFX_ByteTextBuf buf;
1547     FX_BOOL bFirst = TRUE;
1548     int code = 0;
1549     const uint8_t* str = src_buf.GetBuffer();
1550     FX_DWORD size = src_buf.GetSize();
1551     for (FX_DWORD i = 0; i < size; i ++) {
1552         uint8_t ch = str[i];
1553         if (ch >= '0' && ch <= '9') {
1554             if (bFirst) {
1555                 code = (ch - '0') * 16;
1556             } else {
1557                 code += ch - '0';
1558                 buf.AppendChar((char)code);
1559             }
1560             bFirst = !bFirst;
1561         } else if (ch >= 'A' && ch <= 'F') {
1562             if (bFirst) {
1563                 code = (ch - 'A' + 10) * 16;
1564             } else {
1565                 code += ch - 'A' + 10;
1566                 buf.AppendChar((char)code);
1567             }
1568             bFirst = !bFirst;
1569         } else if (ch >= 'a' && ch <= 'f') {
1570             if (bFirst) {
1571                 code = (ch - 'a' + 10) * 16;
1572             } else {
1573                 code += ch - 'a' + 10;
1574                 buf.AppendChar((char)code);
1575             }
1576             bFirst = !bFirst;
1577         }
1578     }
1579     if (!bFirst) {
1580         buf.AppendChar((char)code);
1581     }
1582     return buf.GetByteString();
1583 }