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