Fix a few more warnings in chromium_code mode.
[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     CFX_AffineMatrix new_matrix(a2, b2, c2, d2, e2, f2);
623     new_matrix.Concat(m_pCurStates->m_CTM);
624     m_pCurStates->m_CTM = new_matrix;
625     OnChangeTextMatrix();
626 }
627 void CPDF_StreamContentParser::Handle_SetColorSpace_Fill()
628 {
629     if (m_Options.m_bTextOnly) {
630         return;
631     }
632     CFX_ByteString csname = GetString(0);
633     CPDF_ColorSpace* pCS = FindColorSpace(csname);
634     if (pCS == NULL) {
635         return;
636     }
637     m_pCurStates->m_ColorState.GetModify()->m_FillColor.SetColorSpace(pCS);
638 }
639 void CPDF_StreamContentParser::Handle_SetColorSpace_Stroke()
640 {
641     if (m_Options.m_bTextOnly) {
642         return;
643     }
644     CFX_ByteString csname = GetString(0);
645     CPDF_ColorSpace* pCS = FindColorSpace(csname);
646     if (pCS == NULL) {
647         return;
648     }
649     m_pCurStates->m_ColorState.GetModify()->m_StrokeColor.SetColorSpace(pCS);
650 }
651 void CPDF_StreamContentParser::Handle_SetDash()
652 {
653     if (m_Options.m_bTextOnly) {
654         return;
655     }
656     CPDF_Array* pArray = GetObject(1) ? GetObject(1)->GetArray() : NULL;
657     if (pArray == NULL) {
658         return;
659     }
660     m_pCurStates->SetLineDash(pArray, GetNumber(0), 1.0f);
661 }
662 void CPDF_StreamContentParser::Handle_SetCharWidth()
663 {
664     m_Type3Data[0] = GetNumber(1);
665     m_Type3Data[1] = GetNumber(0);
666     m_bColored = TRUE;
667 }
668 void CPDF_StreamContentParser::Handle_SetCachedDevice()
669 {
670     for (int i = 0; i < 6; i ++) {
671         m_Type3Data[i] = GetNumber(5 - i);
672     }
673     m_bColored = FALSE;
674 }
675 void CPDF_StreamContentParser::Handle_ExecuteXObject()
676 {
677     CFX_ByteString name = GetString(0);
678     if (name == m_LastImageName && m_pLastImage && m_pLastImage->GetStream() && m_pLastImage->GetStream()->GetObjNum()) {
679 #if defined(_FPDFAPI_MINI_) && !defined(_FXCORE_FEATURE_ALL_)
680         AddDuplicateImage();
681 #else
682         AddImage(NULL, m_pLastImage, FALSE);
683 #endif
684         return;
685     }
686     if (m_Options.m_bTextOnly) {
687         CPDF_Object* pRes = NULL;
688         if (m_pResources == NULL) {
689             return;
690         }
691         if (m_pResources == m_pPageResources) {
692             CPDF_Dictionary* pList = m_pResources->GetDict(FX_BSTRC("XObject"));
693             if (pList == NULL) {
694                 return;
695             }
696             pRes = pList->GetElement(name);
697             if (pRes == NULL || pRes->GetType() != PDFOBJ_REFERENCE) {
698                 return;
699             }
700         } else {
701             CPDF_Dictionary* pList = m_pResources->GetDict(FX_BSTRC("XObject"));
702             if (pList == NULL) {
703                 if (m_pPageResources == NULL) {
704                     return;
705                 }
706                 CPDF_Dictionary* pList = m_pPageResources->GetDict(FX_BSTRC("XObject"));
707                 if (pList == NULL) {
708                     return;
709                 }
710                 pRes = pList->GetElement(name);
711                 if (pRes == NULL || pRes->GetType() != PDFOBJ_REFERENCE) {
712                     return;
713                 }
714             } else {
715                 pRes = pList->GetElement(name);
716                 if (pRes == NULL || pRes->GetType() != PDFOBJ_REFERENCE) {
717                     return;
718                 }
719             }
720         }
721         FX_BOOL bForm;
722         if (m_pDocument->IsFormStream(((CPDF_Reference*)pRes)->GetRefObjNum(), bForm) && !bForm) {
723             return;
724         }
725     }
726     CPDF_Stream* pXObject = (CPDF_Stream*)FindResourceObj(FX_BSTRC("XObject"), name);
727     if (pXObject == NULL || pXObject->GetType() != PDFOBJ_STREAM) {
728         m_bResourceMissing = TRUE;
729         return;
730     }
731     CFX_ByteStringC type = pXObject->GetDict() ? pXObject->GetDict()->GetConstString(FX_BSTRC("Subtype")) : CFX_ByteStringC();
732     if (type == FX_BSTRC("Image")) {
733         if (m_Options.m_bTextOnly) {
734             return;
735         }
736         CPDF_ImageObject* pObj = AddImage(pXObject, NULL, FALSE);
737         m_LastImageName = name;
738         m_pLastImage = pObj->m_pImage;
739     } else if (type == FX_BSTRC("Form")) {
740         AddForm(pXObject);
741     } else {
742         return;
743     }
744 }
745 void CPDF_StreamContentParser::AddForm(CPDF_Stream* pStream)
746 {
747 #if !defined(_FPDFAPI_MINI_) || defined(_FXCORE_FEATURE_ALL_)
748     if (!m_Options.m_bSeparateForm) {
749         CPDF_Dictionary* pResources = pStream->GetDict()->GetDict(FX_BSTRC("Resources"));
750         CFX_AffineMatrix form_matrix = pStream->GetDict()->GetMatrix(FX_BSTRC("Matrix"));
751         form_matrix.Concat(m_pCurStates->m_CTM);
752         CPDF_Array* pBBox = pStream->GetDict()->GetArray(FX_BSTRC("BBox"));
753         CFX_FloatRect form_bbox;
754         CPDF_Path ClipPath;
755         if (pBBox) {
756             form_bbox = pStream->GetDict()->GetRect(FX_BSTRC("BBox"));
757             ClipPath.New();
758             ClipPath.AppendRect(form_bbox.left, form_bbox.bottom, form_bbox.right, form_bbox.top);
759             ClipPath.Transform(&form_matrix);
760             form_bbox.Transform(&form_matrix);
761         }
762         CPDF_StreamContentParser parser;
763         parser.Initialize();
764         parser.PrepareParse(m_pDocument, m_pPageResources, m_pResources, &m_mtContentToUser,
765                             m_pObjectList, pResources, &form_bbox, &m_Options, m_pCurStates, m_Level + 1);
766         parser.m_pCurStates->m_CTM = form_matrix;
767         if (ClipPath.NotNull()) {
768             parser.m_pCurStates->m_ClipPath.AppendPath(ClipPath, FXFILL_WINDING, TRUE);
769         }
770         CPDF_StreamAcc stream;
771         stream.LoadAllData(pStream, FALSE);
772         if (stream.GetSize() == 0) {
773             return;
774         }
775 #ifdef _FPDFAPI_MINI_
776         parser.InputData(stream.GetData(), stream.GetSize());
777         parser.Finish();
778 #else
779         parser.Parse(stream.GetData(), stream.GetSize(), 0);
780 #endif
781         return;
782     }
783 #endif
784     CPDF_FormObject* pFormObj = FX_NEW CPDF_FormObject;
785     pFormObj->m_pForm = FX_NEW CPDF_Form(m_pDocument, m_pPageResources, pStream, m_pResources);
786     pFormObj->m_FormMatrix = m_pCurStates->m_CTM;
787     pFormObj->m_FormMatrix.Concat(m_mtContentToUser);
788     CPDF_AllStates status;
789     status.m_GeneralState = m_pCurStates->m_GeneralState;
790     status.m_GraphState = m_pCurStates->m_GraphState;
791     status.m_ColorState = m_pCurStates->m_ColorState;
792     status.m_TextState = m_pCurStates->m_TextState;
793     pFormObj->m_pForm->ParseContent(&status, NULL, NULL, &m_Options, m_Level + 1);
794     if (!m_pObjectList->m_bBackgroundAlphaNeeded && pFormObj->m_pForm->m_bBackgroundAlphaNeeded) {
795         m_pObjectList->m_bBackgroundAlphaNeeded = TRUE;
796     }
797     pFormObj->CalcBoundingBox();
798     SetGraphicStates(pFormObj, TRUE, TRUE, TRUE);
799     m_pObjectList->m_ObjectList.AddTail(pFormObj);
800 }
801 #if defined(_FPDFAPI_MINI_) && !defined(_FXCORE_FEATURE_ALL_)
802 void CPDF_StreamContentParser::AddDuplicateImage()
803 {
804     FX_POSITION tailpos = m_pObjectList->m_ObjectList.GetTailPosition();
805     CPDF_PageObject* pLastObj = (CPDF_PageObject*)m_pObjectList->m_ObjectList.GetAt(tailpos);
806     if (pLastObj == NULL || (pLastObj->m_Type != PDFPAGE_INLINES && pLastObj->m_Type != PDFPAGE_IMAGE)) {
807         AddImage(NULL, m_pLastImage, FALSE);
808         return;
809     }
810     if (pLastObj->m_GeneralState != m_pCurStates->m_GeneralState ||
811             pLastObj->m_ClipPath != m_pCurStates->m_ClipPath ||
812             pLastObj->m_ColorState != m_pCurStates->m_ColorState) {
813         AddImage(NULL, m_pLastImage, FALSE);
814         return;
815     }
816     CFX_AffineMatrix ImageMatrix;
817     ImageMatrix.Copy(m_pCurStates->m_CTM);
818     ImageMatrix.Concat(m_mtContentToUser);
819     if (pLastObj->m_Type == PDFPAGE_INLINES) {
820         CPDF_InlineImages* pInlines = (CPDF_InlineImages*)pLastObj;
821         if (pInlines->m_pStream != m_pLastImage->GetStream()) {
822             AddImage(NULL, m_pLastImage, FALSE);
823             return;
824         }
825         pInlines->AddMatrix(ImageMatrix);
826     } else {
827         CPDF_ImageObject* pImageObj = (CPDF_ImageObject*)pLastObj;
828         CPDF_InlineImages* pInlines = FX_NEW CPDF_InlineImages;
829         pInlines->m_pStream = m_pLastImage->GetStream();
830         SetGraphicStates(pInlines, !pInlines->m_pStream->GetDict()->KeyExist(FX_BSTRC("ColorSpace")), FALSE, FALSE);
831         pInlines->AddMatrix(pImageObj->m_Matrix);
832         pInlines->AddMatrix(ImageMatrix);
833         m_pObjectList->m_ObjectList.RemoveAt(tailpos);
834         m_pObjectList->m_ObjectList.AddTail(pInlines);
835         pLastObj->Release();
836     }
837 }
838 #endif
839 CPDF_ImageObject* CPDF_StreamContentParser::AddImage(CPDF_Stream* pStream, CPDF_Image* pImage, FX_BOOL bInline)
840 {
841     if (pStream == NULL && pImage == NULL) {
842         return NULL;
843     }
844     CFX_AffineMatrix ImageMatrix;
845     ImageMatrix.Copy(m_pCurStates->m_CTM);
846     ImageMatrix.Concat(m_mtContentToUser);
847     CPDF_ImageObject* pImageObj = FX_NEW CPDF_ImageObject;
848     if (pImage) {
849         pImageObj->m_pImage = m_pDocument->GetPageData()->GetImage(pImage->GetStream());
850     } else if (pStream->GetObjNum()) {
851         pImageObj->m_pImage = m_pDocument->LoadImageF(pStream);
852     } else {
853         pImageObj->m_pImage = FX_NEW CPDF_Image(m_pDocument);
854         pImageObj->m_pImage->LoadImageF(pStream, bInline);
855     }
856     SetGraphicStates(pImageObj, pImageObj->m_pImage->IsMask(), FALSE, FALSE);
857     pImageObj->m_Matrix = ImageMatrix;
858     pImageObj->CalcBoundingBox();
859     m_pObjectList->m_ObjectList.AddTail(pImageObj);
860     return pImageObj;
861 }
862 void CPDF_StreamContentParser::Handle_MarkPlace_Dictionary()
863 {
864 }
865 void CPDF_StreamContentParser::Handle_EndImage()
866 {
867 }
868 void CPDF_StreamContentParser::Handle_EndMarkedContent()
869 {
870     if (!m_Options.m_bMarkedContent) {
871         return;
872     }
873     if (m_CurContentMark.IsNull()) {
874         return;
875     }
876     int count = m_CurContentMark.GetObject()->CountItems();
877     if (count == 1) {
878         m_CurContentMark.SetNull();
879         return;
880     }
881     m_CurContentMark.GetModify()->DeleteLastMark();
882 }
883 void CPDF_StreamContentParser::Handle_EndText()
884 {
885     int count = m_ClipTextList.GetSize();
886     if (count == 0) {
887         return;
888     }
889     if (m_pCurStates->m_TextState.GetObject()->m_TextMode < 4) {
890         for (int i = 0; i < count; i ++) {
891             CPDF_TextObject* pText = (CPDF_TextObject*)m_ClipTextList.GetAt(i);
892             if (pText) {
893                 delete pText;
894             }
895         }
896     } else {
897         m_pCurStates->m_ClipPath.AppendTexts((CPDF_TextObject**)m_ClipTextList.GetData(), count);
898     }
899     m_ClipTextList.RemoveAll();
900 }
901 void CPDF_StreamContentParser::Handle_EndSectionUndefined()
902 {
903     if (m_CompatCount) {
904         m_CompatCount --;
905     }
906 }
907 void CPDF_StreamContentParser::Handle_FillPath()
908 {
909     if (m_Options.m_bTextOnly) {
910         return;
911     }
912     AddPathObject(FXFILL_WINDING, FALSE);
913 }
914 void CPDF_StreamContentParser::Handle_FillPathOld()
915 {
916     if (m_Options.m_bTextOnly) {
917         return;
918     }
919     AddPathObject(FXFILL_WINDING, FALSE);
920 }
921 void CPDF_StreamContentParser::Handle_EOFillPath()
922 {
923     if (m_Options.m_bTextOnly) {
924         return;
925     }
926     AddPathObject(FXFILL_ALTERNATE, FALSE);
927 }
928 void CPDF_StreamContentParser::Handle_SetGray_Fill()
929 {
930     FX_FLOAT value = GetNumber(0);
931     CPDF_ColorSpace* pCS = CPDF_ColorSpace::GetStockCS(PDFCS_DEVICEGRAY);
932     m_pCurStates->m_ColorState.SetFillColor(pCS, &value, 1);
933 }
934 void CPDF_StreamContentParser::Handle_SetGray_Stroke()
935 {
936     FX_FLOAT value = GetNumber(0);
937     CPDF_ColorSpace* pCS = CPDF_ColorSpace::GetStockCS(PDFCS_DEVICEGRAY);
938     m_pCurStates->m_ColorState.SetStrokeColor(pCS, &value, 1);
939 }
940 void CPDF_StreamContentParser::Handle_SetExtendGraphState()
941 {
942     CFX_ByteString name = GetString(0);
943     CPDF_Dictionary* pGS = (CPDF_Dictionary*)FindResourceObj(FX_BSTRC("ExtGState"), name);
944     if (pGS == NULL || pGS->GetType() != PDFOBJ_DICTIONARY) {
945         m_bResourceMissing = TRUE;
946         return;
947     }
948     m_pCurStates->ProcessExtGS(pGS, this);
949 }
950 void CPDF_StreamContentParser::Handle_ClosePath()
951 {
952     if (m_Options.m_bTextOnly) {
953         return;
954     }
955     if (m_PathPointCount == 0) {
956         return;
957     }
958     if (m_PathStartX != m_PathCurrentX || m_PathStartY != m_PathCurrentY) {
959         AddPathPoint(m_PathStartX, m_PathStartY, FXPT_LINETO | FXPT_CLOSEFIGURE);
960     } else if (m_pPathPoints[m_PathPointCount - 1].m_Flag != FXPT_MOVETO) {
961         m_pPathPoints[m_PathPointCount - 1].m_Flag |= FXPT_CLOSEFIGURE;
962     }
963 }
964 void CPDF_StreamContentParser::Handle_SetFlat()
965 {
966 #if !defined(_FPDFAPI_MINI_) || defined(_FXCORE_FEATURE_ALL_)
967     m_pCurStates->m_GeneralState.GetModify()->m_Flatness = GetNumber(0);
968 #endif
969 }
970 void CPDF_StreamContentParser::Handle_BeginImageData()
971 {
972 }
973 void CPDF_StreamContentParser::Handle_SetLineJoin()
974 {
975     m_pCurStates->m_GraphState.GetModify()->m_LineJoin = (CFX_GraphStateData::LineJoin)GetInteger(0);
976 }
977 void CPDF_StreamContentParser::Handle_SetLineCap()
978 {
979     m_pCurStates->m_GraphState.GetModify()->m_LineCap = (CFX_GraphStateData::LineCap)GetInteger(0);
980 }
981 void CPDF_StreamContentParser::Handle_SetCMYKColor_Fill()
982 {
983     REQUIRE_PARAMS(4);
984     FX_FLOAT values[4];
985     for (int i = 0; i < 4; i ++) {
986         values[i] = GetNumber(3 - i);
987     }
988     CPDF_ColorSpace* pCS = CPDF_ColorSpace::GetStockCS(PDFCS_DEVICECMYK);
989     m_pCurStates->m_ColorState.SetFillColor(pCS, values, 4);
990 }
991 void CPDF_StreamContentParser::Handle_SetCMYKColor_Stroke()
992 {
993     REQUIRE_PARAMS(4);
994     FX_FLOAT values[4];
995     for (int i = 0; i < 4; i ++) {
996         values[i] = GetNumber(3 - i);
997     }
998     CPDF_ColorSpace* pCS = CPDF_ColorSpace::GetStockCS(PDFCS_DEVICECMYK);
999     m_pCurStates->m_ColorState.SetStrokeColor(pCS, values, 4);
1000 }
1001 void CPDF_StreamContentParser::Handle_LineTo()
1002 {
1003     REQUIRE_PARAMS(2);
1004     if (m_Options.m_bTextOnly) {
1005         return;
1006     }
1007     AddPathPoint(GetNumber(1), GetNumber(0), FXPT_LINETO);
1008 }
1009 void CPDF_StreamContentParser::Handle_MoveTo()
1010 {
1011     REQUIRE_PARAMS(2);
1012     if (m_Options.m_bTextOnly) {
1013 #ifndef _FPDFAPI_MINI_
1014         m_pSyntax->SkipPathObject();
1015 #endif
1016         return;
1017     }
1018     AddPathPoint(GetNumber(1), GetNumber(0), FXPT_MOVETO);
1019 #ifndef _FPDFAPI_MINI_
1020     ParsePathObject();
1021 #endif
1022 }
1023 void CPDF_StreamContentParser::Handle_SetMiterLimit()
1024 {
1025     m_pCurStates->m_GraphState.GetModify()->m_MiterLimit = GetNumber(0);
1026 }
1027 void CPDF_StreamContentParser::Handle_MarkPlace()
1028 {
1029 }
1030 void CPDF_StreamContentParser::Handle_EndPath()
1031 {
1032     if (m_Options.m_bTextOnly) {
1033         return;
1034     }
1035     AddPathObject(0, FALSE);
1036 }
1037 void CPDF_StreamContentParser::Handle_SaveGraphState()
1038 {
1039     CPDF_AllStates* pStates = FX_NEW CPDF_AllStates;
1040     pStates->Copy(*m_pCurStates);
1041     m_StateStack.Add(pStates);
1042 }
1043 void CPDF_StreamContentParser::Handle_RestoreGraphState()
1044 {
1045     int size = m_StateStack.GetSize();
1046     if (size == 0) {
1047         return;
1048     }
1049     CPDF_AllStates* pStates = (CPDF_AllStates*)m_StateStack.GetAt(size - 1);
1050     m_pCurStates->Copy(*pStates);
1051     delete pStates;
1052     m_StateStack.RemoveAt(size - 1);
1053 }
1054 void CPDF_StreamContentParser::Handle_Rectangle()
1055 {
1056     if (m_Options.m_bTextOnly) {
1057         return;
1058     }
1059     FX_FLOAT x = GetNumber(3), y = GetNumber(2);
1060     FX_FLOAT w = GetNumber(1), h = GetNumber(0);
1061     AddPathRect(x, y, w, h);
1062 }
1063 void CPDF_StreamContentParser::AddPathRect(FX_FLOAT x, FX_FLOAT y, FX_FLOAT w, FX_FLOAT h)
1064 {
1065     AddPathPoint(x, y, FXPT_MOVETO);
1066     AddPathPoint(x + w, y, FXPT_LINETO);
1067     AddPathPoint(x + w, y + h, FXPT_LINETO);
1068     AddPathPoint(x, y + h, FXPT_LINETO);
1069     AddPathPoint(x, y, FXPT_LINETO | FXPT_CLOSEFIGURE);
1070 }
1071 void CPDF_StreamContentParser::Handle_SetRGBColor_Fill()
1072 {
1073     REQUIRE_PARAMS(3);
1074     FX_FLOAT values[3];
1075     for (int i = 0; i < 3; i ++) {
1076         values[i] = GetNumber(2 - i);
1077     }
1078     CPDF_ColorSpace* pCS = CPDF_ColorSpace::GetStockCS(PDFCS_DEVICERGB);
1079     m_pCurStates->m_ColorState.SetFillColor(pCS, values, 3);
1080 }
1081 void CPDF_StreamContentParser::Handle_SetRGBColor_Stroke()
1082 {
1083     REQUIRE_PARAMS(3);
1084     FX_FLOAT values[3];
1085     for (int i = 0; i < 3; i ++) {
1086         values[i] = GetNumber(2 - i);
1087     }
1088     CPDF_ColorSpace* pCS = CPDF_ColorSpace::GetStockCS(PDFCS_DEVICERGB);
1089     m_pCurStates->m_ColorState.SetStrokeColor(pCS, values, 3);
1090 }
1091 void CPDF_StreamContentParser::Handle_SetRenderIntent()
1092 {
1093 }
1094 void CPDF_StreamContentParser::Handle_CloseStrokePath()
1095 {
1096     if (m_Options.m_bTextOnly) {
1097         return;
1098     }
1099     Handle_ClosePath();
1100     AddPathObject(0, TRUE);
1101 }
1102 void CPDF_StreamContentParser::Handle_StrokePath()
1103 {
1104     if (m_Options.m_bTextOnly) {
1105         return;
1106     }
1107     AddPathObject(0, TRUE);
1108 }
1109 void CPDF_StreamContentParser::Handle_SetColor_Fill()
1110 {
1111     if (m_Options.m_bTextOnly) {
1112         return;
1113     }
1114     FX_FLOAT values[4];
1115     int nargs = m_ParamCount;
1116     if (nargs > 4) {
1117         nargs = 4;
1118     }
1119     for (int i = 0; i < nargs; i ++) {
1120         values[i] = GetNumber(nargs - i - 1);
1121     }
1122     m_pCurStates->m_ColorState.SetFillColor(NULL, values, nargs);
1123 }
1124 void CPDF_StreamContentParser::Handle_SetColor_Stroke()
1125 {
1126     if (m_Options.m_bTextOnly) {
1127         return;
1128     }
1129     FX_FLOAT values[4];
1130     int nargs = m_ParamCount;
1131     if (nargs > 4) {
1132         nargs = 4;
1133     }
1134     for (int i = 0; i < nargs; i ++) {
1135         values[i] = GetNumber(nargs - i - 1);
1136     }
1137     m_pCurStates->m_ColorState.SetStrokeColor(NULL, values, nargs);
1138 }
1139 void CPDF_StreamContentParser::Handle_SetColorPS_Fill()
1140 {
1141     if (m_Options.m_bTextOnly) {
1142         return;
1143     }
1144     CPDF_Object* pLastParam = GetObject(0);
1145     if (pLastParam == NULL) {
1146         return;
1147     }
1148     int nargs = m_ParamCount;
1149     int nvalues = nargs;
1150     if (pLastParam->GetType() == PDFOBJ_NAME) {
1151         nvalues --;
1152     }
1153     FX_FLOAT* values = NULL;
1154     if (nvalues) {
1155         values = FX_Alloc(FX_FLOAT, nvalues);
1156         for (int i = 0; i < nvalues; i ++) {
1157             values[i] = GetNumber(nargs - i - 1);
1158         }
1159     }
1160     if (nvalues != nargs) {
1161         CPDF_Pattern* pPattern = FindPattern(GetString(0), FALSE);
1162         if (pPattern) {
1163             m_pCurStates->m_ColorState.SetFillPattern(pPattern, values, nvalues);
1164         }
1165     } else {
1166         m_pCurStates->m_ColorState.SetFillColor(NULL, values, nvalues);
1167     }
1168     if (values) {
1169         FX_Free(values);
1170     }
1171 }
1172 void CPDF_StreamContentParser::Handle_SetColorPS_Stroke()
1173 {
1174     if (m_Options.m_bTextOnly) {
1175         return;
1176     }
1177     CPDF_Object* pLastParam = GetObject(0);
1178     if (pLastParam == NULL) {
1179         return;
1180     }
1181     int nargs = m_ParamCount;
1182     int nvalues = nargs;
1183     if (pLastParam->GetType() == PDFOBJ_NAME) {
1184         nvalues --;
1185     }
1186     FX_FLOAT* values = NULL;
1187     if (nvalues) {
1188         values = FX_Alloc(FX_FLOAT, nvalues);
1189         for (int i = 0; i < nvalues; i ++) {
1190             values[i] = GetNumber(nargs - i - 1);
1191         }
1192     }
1193     if (nvalues != nargs) {
1194         CPDF_Pattern* pPattern = FindPattern(GetString(0), FALSE);
1195         if (pPattern) {
1196             m_pCurStates->m_ColorState.SetStrokePattern(pPattern, values, nvalues);
1197         }
1198     } else {
1199         m_pCurStates->m_ColorState.SetStrokeColor(NULL, values, nvalues);
1200     }
1201     if (values) {
1202         FX_Free(values);
1203     }
1204 }
1205 CFX_FloatRect _GetShadingBBox(CPDF_Stream* pStream, int type, const CFX_AffineMatrix* pMatrix,
1206                               CPDF_Function** pFuncs, int nFuncs, CPDF_ColorSpace* pCS);
1207 void CPDF_StreamContentParser::Handle_ShadeFill()
1208 {
1209     if (m_Options.m_bTextOnly) {
1210         return;
1211     }
1212     CPDF_Pattern* pPattern = FindPattern(GetString(0), TRUE);
1213     if (pPattern == NULL) {
1214         return;
1215     }
1216     if (pPattern->m_PatternType != PATTERN_SHADING) {
1217         return;
1218     }
1219     CPDF_ShadingPattern* pShading = (CPDF_ShadingPattern*)pPattern;
1220     if (!pShading->m_bShadingObj) {
1221         return;
1222     }
1223     if (!pShading->Load()) {
1224         return;
1225     }
1226     CPDF_ShadingObject* pObj = FX_NEW CPDF_ShadingObject;
1227     pObj->m_pShading = pShading;
1228     SetGraphicStates(pObj, FALSE, FALSE, FALSE);
1229     pObj->m_Matrix = m_pCurStates->m_CTM;
1230     pObj->m_Matrix.Concat(m_mtContentToUser);
1231     CFX_FloatRect bbox;
1232     if (!pObj->m_ClipPath.IsNull()) {
1233         bbox = pObj->m_ClipPath.GetClipBox();
1234     } else {
1235         bbox = m_BBox;
1236     }
1237     if (pShading->m_ShadingType >= 4) {
1238         bbox.Intersect(_GetShadingBBox((CPDF_Stream*)pShading->m_pShadingObj, pShading->m_ShadingType, &pObj->m_Matrix,
1239                                        pShading->m_pFunctions, pShading->m_nFuncs, pShading->m_pCS));
1240     }
1241     pObj->m_Left = bbox.left;
1242     pObj->m_Right = bbox.right;
1243     pObj->m_Top = bbox.top;
1244     pObj->m_Bottom = bbox.bottom;
1245     m_pObjectList->m_ObjectList.AddTail(pObj);
1246 }
1247 void CPDF_StreamContentParser::Handle_SetCharSpace()
1248 {
1249     m_pCurStates->m_TextState.GetModify()->m_CharSpace = GetNumber(0);
1250 }
1251 void CPDF_StreamContentParser::Handle_MoveTextPoint()
1252 {
1253     m_pCurStates->m_TextLineX += GetNumber(1);
1254     m_pCurStates->m_TextLineY += GetNumber(0);
1255     m_pCurStates->m_TextX = m_pCurStates->m_TextLineX;
1256     m_pCurStates->m_TextY = m_pCurStates->m_TextLineY;
1257 }
1258 void CPDF_StreamContentParser::Handle_MoveTextPoint_SetLeading()
1259 {
1260     Handle_MoveTextPoint();
1261     m_pCurStates->m_TextLeading = -GetNumber(0);
1262 }
1263 void CPDF_StreamContentParser::Handle_SetFont()
1264 {
1265     FX_FLOAT fs = GetNumber(0);
1266     if (fs == 0) {
1267         fs = m_DefFontSize;
1268     }
1269     m_pCurStates->m_TextState.GetModify()->m_FontSize = fs;
1270     CPDF_Font* pFont = FindFont(GetString(1));
1271     if (pFont) {
1272         m_pCurStates->m_TextState.SetFont(pFont);
1273     }
1274 }
1275 CPDF_Object* CPDF_StreamContentParser::FindResourceObj(FX_BSTR type, const CFX_ByteString& name)
1276 {
1277     if (m_pResources == NULL) {
1278         return NULL;
1279     }
1280     if (m_pResources == m_pPageResources) {
1281         CPDF_Dictionary* pList = m_pResources->GetDict(type);
1282         if (pList == NULL) {
1283             return NULL;
1284         }
1285         CPDF_Object* pRes = pList->GetElementValue(name);
1286         return pRes;
1287     }
1288     CPDF_Dictionary* pList = m_pResources->GetDict(type);
1289     if (pList == NULL) {
1290         if (m_pPageResources == NULL) {
1291             return NULL;
1292         }
1293         CPDF_Dictionary* pList = m_pPageResources->GetDict(type);
1294         if (pList == NULL) {
1295             return NULL;
1296         }
1297         CPDF_Object* pRes = pList->GetElementValue(name);
1298         return pRes;
1299     }
1300     CPDF_Object* pRes = pList->GetElementValue(name);
1301     return pRes;
1302 }
1303 CPDF_Font* CPDF_StreamContentParser::FindFont(const CFX_ByteString& name)
1304 {
1305     CPDF_Dictionary* pFontDict = (CPDF_Dictionary*)FindResourceObj(FX_BSTRC("Font"), name);
1306     if (pFontDict == NULL || pFontDict->GetType() != PDFOBJ_DICTIONARY) {
1307         m_bResourceMissing = TRUE;
1308         return CPDF_Font::GetStockFont(m_pDocument, FX_BSTRC("Helvetica"));
1309     }
1310     CPDF_Font* pFont = m_pDocument->LoadFont(pFontDict);
1311     if (pFont && pFont->GetType3Font()) {
1312         pFont->GetType3Font()->SetPageResources(m_pResources);
1313         pFont->GetType3Font()->CheckType3FontMetrics();
1314     }
1315     return pFont;
1316 }
1317 CPDF_ColorSpace* CPDF_StreamContentParser::FindColorSpace(const CFX_ByteString& name)
1318 {
1319     if (name == FX_BSTRC("Pattern")) {
1320         return CPDF_ColorSpace::GetStockCS(PDFCS_PATTERN);
1321     }
1322     if (name == FX_BSTRC("DeviceGray") || name == FX_BSTRC("DeviceCMYK") || name == FX_BSTRC("DeviceRGB")) {
1323         CFX_ByteString defname = "Default";
1324         defname += name.Mid(7);
1325         CPDF_Object* pDefObj = FindResourceObj(FX_BSTRC("ColorSpace"), defname);
1326         if (pDefObj == NULL) {
1327             if (name == FX_BSTRC("DeviceGray")) {
1328                 return CPDF_ColorSpace::GetStockCS(PDFCS_DEVICEGRAY);
1329             }
1330             if (name == FX_BSTRC("DeviceRGB")) {
1331                 return CPDF_ColorSpace::GetStockCS(PDFCS_DEVICERGB);
1332             }
1333             return CPDF_ColorSpace::GetStockCS(PDFCS_DEVICECMYK);
1334         }
1335         return m_pDocument->LoadColorSpace(pDefObj);
1336     }
1337     CPDF_Object* pCSObj = FindResourceObj(FX_BSTRC("ColorSpace"), name);
1338     if (pCSObj == NULL) {
1339         m_bResourceMissing = TRUE;
1340         return NULL;
1341     }
1342     return m_pDocument->LoadColorSpace(pCSObj);
1343 }
1344 CPDF_Pattern* CPDF_StreamContentParser::FindPattern(const CFX_ByteString& name, FX_BOOL bShading)
1345 {
1346     CPDF_Object* pPattern = FindResourceObj(bShading ? FX_BSTRC("Shading") : FX_BSTRC("Pattern"), name);
1347     if (pPattern == NULL || (pPattern->GetType() != PDFOBJ_DICTIONARY &&
1348                              pPattern->GetType() != PDFOBJ_STREAM)) {
1349         m_bResourceMissing = TRUE;
1350         return NULL;
1351     }
1352     return m_pDocument->LoadPattern(pPattern, bShading, &m_pCurStates->m_ParentMatrix);
1353 }
1354 void CPDF_StreamContentParser::ConvertTextSpace(FX_FLOAT& x, FX_FLOAT& y)
1355 {
1356     m_pCurStates->m_TextMatrix.Transform(x, y, x, y);
1357     ConvertUserSpace(x, y);
1358 }
1359 void CPDF_StreamContentParser::ConvertUserSpace(FX_FLOAT& x, FX_FLOAT& y)
1360 {
1361     m_pCurStates->m_CTM.Transform(x, y, x, y);
1362     m_mtContentToUser.Transform(x, y, x, y);
1363 }
1364 void CPDF_StreamContentParser::AddTextObject(CFX_ByteString* pStrs, FX_FLOAT fInitKerning, FX_FLOAT* pKerning, int nsegs)
1365 {
1366     CPDF_Font* pFont = m_pCurStates->m_TextState.GetFont();
1367     if (pFont == NULL) {
1368         return;
1369     }
1370     if (fInitKerning != 0) {
1371         if (!pFont->IsVertWriting()) {
1372             m_pCurStates->m_TextX -= FXSYS_Mul(fInitKerning, m_pCurStates->m_TextState.GetFontSize()) / 1000;
1373         } else {
1374             m_pCurStates->m_TextY -= FXSYS_Mul(fInitKerning, m_pCurStates->m_TextState.GetFontSize()) / 1000;
1375         }
1376     }
1377     if (nsegs == 0) {
1378         return;
1379     }
1380     int textmode;
1381     if (pFont->GetFontType() == PDFFONT_TYPE3) {
1382         textmode = 0;
1383     } else {
1384         textmode = m_pCurStates->m_TextState.GetObject()->m_TextMode;
1385     }
1386     CPDF_TextObject* pText = FX_NEW CPDF_TextObject;
1387     m_pLastTextObject = pText;
1388     SetGraphicStates(pText, TRUE, TRUE, TRUE);
1389     if (textmode && textmode != 3 && textmode != 4 && textmode != 7) {
1390         FX_FLOAT* pCTM = pText->m_TextState.GetModify()->m_CTM;
1391         pCTM[0] = m_pCurStates->m_CTM.a;
1392         pCTM[1] = m_pCurStates->m_CTM.c;
1393         pCTM[2] = m_pCurStates->m_CTM.b;
1394         pCTM[3] = m_pCurStates->m_CTM.d;
1395     }
1396     pText->SetSegments(pStrs, pKerning, nsegs);
1397     pText->m_PosX = m_pCurStates->m_TextX;
1398     pText->m_PosY = m_pCurStates->m_TextY + m_pCurStates->m_TextRise;
1399     ConvertTextSpace(pText->m_PosX, pText->m_PosY);
1400     FX_FLOAT x_advance, y_advance;
1401     pText->CalcPositionData(&x_advance, &y_advance, m_pCurStates->m_TextHorzScale, m_Level);
1402     m_pCurStates->m_TextX += x_advance;
1403     m_pCurStates->m_TextY += y_advance;
1404     if (textmode > 3) {
1405         CPDF_TextObject* pCopy = FX_NEW CPDF_TextObject;
1406         pCopy->Copy(pText);
1407         m_ClipTextList.Add(pCopy);
1408     }
1409     m_pObjectList->m_ObjectList.AddTail(pText);
1410     if (pKerning && pKerning[nsegs - 1] != 0) {
1411         if (!pFont->IsVertWriting()) {
1412             m_pCurStates->m_TextX -= FXSYS_Mul(pKerning[nsegs - 1], m_pCurStates->m_TextState.GetFontSize()) / 1000;
1413         } else {
1414             m_pCurStates->m_TextY -= FXSYS_Mul(pKerning[nsegs - 1], m_pCurStates->m_TextState.GetFontSize()) / 1000;
1415         }
1416     }
1417 }
1418 void CPDF_StreamContentParser::Handle_ShowText()
1419 {
1420     CFX_ByteString str = GetString(0);
1421     if (str.IsEmpty()) {
1422         return;
1423     }
1424     AddTextObject(&str, 0, NULL, 1);
1425 }
1426 void CPDF_StreamContentParser::Handle_ShowText_Positioning()
1427 {
1428     CPDF_Array* pArray = GetObject(0) ? GetObject(0)->GetArray() : NULL;
1429     if (pArray == NULL) {
1430         return;
1431     }
1432     int n = pArray->GetCount(), nsegs = 0, i;
1433     for (i = 0; i < n; i ++) {
1434         CPDF_Object* pObj = pArray->GetElementValue(i);
1435         if (pObj->GetType() == PDFOBJ_STRING) {
1436             nsegs ++;
1437         }
1438     }
1439     if (nsegs == 0) {
1440         for (i = 0; i < n; i ++) {
1441             m_pCurStates->m_TextX -= FXSYS_Mul(pArray->GetNumber(i), m_pCurStates->m_TextState.GetFontSize()) / 1000;
1442         };
1443         return;
1444     }
1445     CFX_ByteString* pStrs;
1446     FX_NEW_VECTOR(pStrs, CFX_ByteString, nsegs);
1447     FX_FLOAT* pKerning = FX_Alloc(FX_FLOAT, nsegs);
1448     int iSegment = 0;
1449     FX_FLOAT fInitKerning = 0;
1450     for (i = 0; i < n; i ++) {
1451         CPDF_Object* pObj = pArray->GetElementValue(i);
1452         if (pObj->GetType() == PDFOBJ_STRING) {
1453             CFX_ByteString str = pObj->GetString();
1454             if (str.IsEmpty()) {
1455                 continue;
1456             }
1457             pStrs[iSegment] = str;
1458             pKerning[iSegment ++] = 0;
1459         } else {
1460             FX_FLOAT num = pObj ? pObj->GetNumber() : 0;
1461             if (iSegment == 0) {
1462                 fInitKerning += num;
1463             } else {
1464                 pKerning[iSegment - 1] += num;
1465             }
1466         }
1467     }
1468     AddTextObject(pStrs, fInitKerning, pKerning, iSegment);
1469     FX_DELETE_VECTOR(pStrs, CFX_ByteString, nsegs);
1470     FX_Free(pKerning);
1471 }
1472 void CPDF_StreamContentParser::Handle_SetTextLeading()
1473 {
1474     m_pCurStates->m_TextLeading = GetNumber(0);
1475 }
1476 void CPDF_StreamContentParser::Handle_SetTextMatrix()
1477 {
1478     m_pCurStates->m_TextMatrix.Set(GetNumber16(5), GetNumber16(4), GetNumber16(3),
1479                                    GetNumber16(2), GetNumber(1), GetNumber(0));
1480     OnChangeTextMatrix();
1481     m_pCurStates->m_TextX = 0;
1482     m_pCurStates->m_TextY = 0;
1483     m_pCurStates->m_TextLineX = 0;
1484     m_pCurStates->m_TextLineY = 0;
1485 }
1486 void CPDF_StreamContentParser::OnChangeTextMatrix()
1487 {
1488     CFX_AffineMatrix text_matrix(m_pCurStates->m_TextHorzScale, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
1489     text_matrix.Concat(m_pCurStates->m_TextMatrix);
1490     text_matrix.Concat(m_pCurStates->m_CTM);
1491     text_matrix.Concat(m_mtContentToUser);
1492     FX_FLOAT* pTextMatrix = m_pCurStates->m_TextState.GetModify()->m_Matrix;
1493     pTextMatrix[0] = text_matrix.a;
1494     pTextMatrix[1] = text_matrix.c;
1495     pTextMatrix[2] = text_matrix.b;
1496     pTextMatrix[3] = text_matrix.d;
1497 }
1498 void CPDF_StreamContentParser::Handle_SetTextRenderMode()
1499 {
1500     int mode = GetInteger(0);
1501     if (mode < 0 || mode > 7) {
1502         return;
1503     }
1504     m_pCurStates->m_TextState.GetModify()->m_TextMode = mode;
1505 }
1506 void CPDF_StreamContentParser::Handle_SetTextRise()
1507 {
1508     m_pCurStates->m_TextRise = GetNumber(0);
1509 }
1510 void CPDF_StreamContentParser::Handle_SetWordSpace()
1511 {
1512     m_pCurStates->m_TextState.GetModify()->m_WordSpace = GetNumber(0);
1513 }
1514 void CPDF_StreamContentParser::Handle_SetHorzScale()
1515 {
1516     if (m_ParamCount != 1) {
1517         return;
1518     }
1519     m_pCurStates->m_TextHorzScale = GetNumber(0) / 100;
1520     OnChangeTextMatrix();
1521 }
1522 void CPDF_StreamContentParser::Handle_MoveToNextLine()
1523 {
1524     m_pCurStates->m_TextLineY -= m_pCurStates->m_TextLeading;
1525     m_pCurStates->m_TextX = m_pCurStates->m_TextLineX;
1526     m_pCurStates->m_TextY = m_pCurStates->m_TextLineY;
1527 }
1528 void CPDF_StreamContentParser::Handle_CurveTo_23()
1529 {
1530     if (m_Options.m_bTextOnly) {
1531         return;
1532     }
1533     AddPathPoint(m_PathCurrentX, m_PathCurrentY, FXPT_BEZIERTO);
1534     AddPathPoint(GetNumber(3), GetNumber(2), FXPT_BEZIERTO);
1535     AddPathPoint(GetNumber(1), GetNumber(0), FXPT_BEZIERTO);
1536 }
1537 void CPDF_StreamContentParser::Handle_SetLineWidth()
1538 {
1539     FX_FLOAT width = GetNumber(0);
1540     m_pCurStates->m_GraphState.GetModify()->m_LineWidth = width;
1541 }
1542 void CPDF_StreamContentParser::Handle_Clip()
1543 {
1544     m_PathClipType = FXFILL_WINDING;
1545 }
1546 void CPDF_StreamContentParser::Handle_EOClip()
1547 {
1548     m_PathClipType = FXFILL_ALTERNATE;
1549 }
1550 void CPDF_StreamContentParser::Handle_CurveTo_13()
1551 {
1552     if (m_Options.m_bTextOnly) {
1553         return;
1554     }
1555     AddPathPoint(GetNumber(3), GetNumber(2), FXPT_BEZIERTO);
1556     AddPathPoint(GetNumber(1), GetNumber(0), FXPT_BEZIERTO);
1557     AddPathPoint(GetNumber(1), GetNumber(0), FXPT_BEZIERTO);
1558 }
1559 void CPDF_StreamContentParser::Handle_NextLineShowText()
1560 {
1561     Handle_MoveToNextLine();
1562     Handle_ShowText();
1563 }
1564 void CPDF_StreamContentParser::Handle_NextLineShowText_Space()
1565 {
1566     m_pCurStates->m_TextState.GetModify()->m_WordSpace = GetNumber(2);
1567     m_pCurStates->m_TextState.GetModify()->m_CharSpace = GetNumber(1);
1568     Handle_NextLineShowText();
1569 }
1570 void CPDF_StreamContentParser::Handle_Invalid()
1571 {
1572 }
1573 void CPDF_StreamContentParser::AddPathPoint(FX_FLOAT x, FX_FLOAT y, int flag)
1574 {
1575     m_PathCurrentX = x;
1576     m_PathCurrentY = y;
1577     if (flag == FXPT_MOVETO) {
1578         m_PathStartX = x;
1579         m_PathStartY = y;
1580         if (m_PathPointCount && m_pPathPoints[m_PathPointCount - 1].m_Flag == FXPT_MOVETO) {
1581             m_pPathPoints[m_PathPointCount - 1].m_PointX = x;
1582             m_pPathPoints[m_PathPointCount - 1].m_PointY = y;
1583             return;
1584         }
1585     } else if (m_PathPointCount == 0) {
1586         return;
1587     }
1588     m_PathPointCount ++;
1589     if (m_PathPointCount > m_PathAllocSize) {
1590         int newsize = m_PathPointCount + 256;
1591         FX_PATHPOINT* pNewPoints = FX_Alloc(FX_PATHPOINT, newsize);
1592         if (m_PathAllocSize) {
1593             FXSYS_memcpy32(pNewPoints, m_pPathPoints, m_PathAllocSize * sizeof(FX_PATHPOINT));
1594             FX_Free(m_pPathPoints);
1595         }
1596         m_pPathPoints = pNewPoints;
1597         m_PathAllocSize = newsize;
1598     }
1599     m_pPathPoints[m_PathPointCount - 1].m_Flag = flag;
1600     m_pPathPoints[m_PathPointCount - 1].m_PointX = x;
1601     m_pPathPoints[m_PathPointCount - 1].m_PointY = y;
1602 }
1603 void CPDF_StreamContentParser::AddPathObject(int FillType, FX_BOOL bStroke)
1604 {
1605     int PathPointCount = m_PathPointCount, PathClipType = m_PathClipType;
1606     m_PathPointCount = 0;
1607     m_PathClipType = 0;
1608     if (PathPointCount <= 1) {
1609         if (PathPointCount && PathClipType) {
1610             CPDF_Path path;
1611             path.New()->AppendRect(0, 0, 0, 0);
1612             m_pCurStates->m_ClipPath.AppendPath(path, FXFILL_WINDING, TRUE);
1613         }
1614         return;
1615     }
1616     if (PathPointCount && m_pPathPoints[PathPointCount - 1].m_Flag == FXPT_MOVETO) {
1617         PathPointCount --;
1618     }
1619     CPDF_Path Path;
1620     CFX_PathData* pPathData = Path.New();
1621     pPathData->SetPointCount(PathPointCount);
1622     FXSYS_memcpy32(pPathData->GetPoints(), m_pPathPoints, sizeof(FX_PATHPOINT) * PathPointCount);
1623     CFX_AffineMatrix matrix = m_pCurStates->m_CTM;
1624     matrix.Concat(m_mtContentToUser);
1625     if (bStroke || FillType) {
1626         CPDF_PathObject* pPathObj = FX_NEW CPDF_PathObject;
1627         pPathObj->m_bStroke = bStroke;
1628         pPathObj->m_FillType = FillType;
1629         pPathObj->m_Path = Path;
1630         pPathObj->m_Matrix = matrix;
1631         SetGraphicStates(pPathObj, TRUE, FALSE, TRUE);
1632         pPathObj->CalcBoundingBox();
1633         m_pObjectList->m_ObjectList.AddTail(pPathObj);
1634     }
1635     if (PathClipType) {
1636         if (!matrix.IsIdentity()) {
1637             Path.Transform(&matrix);
1638             matrix.SetIdentity();
1639         }
1640         m_pCurStates->m_ClipPath.AppendPath(Path, PathClipType, TRUE);
1641     }
1642 }
1643 CFX_ByteString _FPDF_ByteStringFromHex(CFX_BinaryBuf& src_buf)
1644 {
1645     CFX_ByteTextBuf buf;
1646     FX_BOOL bFirst = TRUE;
1647     int code = 0;
1648     FX_LPCBYTE str = src_buf.GetBuffer();
1649     FX_DWORD size = src_buf.GetSize();
1650     for (FX_DWORD i = 0; i < size; i ++) {
1651         FX_BYTE ch = str[i];
1652         if (ch >= '0' && ch <= '9') {
1653             if (bFirst) {
1654                 code = (ch - '0') * 16;
1655             } else {
1656                 code += ch - '0';
1657                 buf.AppendChar((char)code);
1658             }
1659             bFirst = !bFirst;
1660         } else if (ch >= 'A' && ch <= 'F') {
1661             if (bFirst) {
1662                 code = (ch - 'A' + 10) * 16;
1663             } else {
1664                 code += ch - 'A' + 10;
1665                 buf.AppendChar((char)code);
1666             }
1667             bFirst = !bFirst;
1668         } else if (ch >= 'a' && ch <= 'f') {
1669             if (bFirst) {
1670                 code = (ch - 'a' + 10) * 16;
1671             } else {
1672                 code += ch - 'a' + 10;
1673                 buf.AppendChar((char)code);
1674             }
1675             bFirst = !bFirst;
1676         }
1677     }
1678     if (!bFirst) {
1679         buf.AppendChar((char)code);
1680     }
1681     return buf.GetByteString();
1682 }