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.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
7 #include "../../../include/fpdfapi/fpdf_page.h"
8 #include "../../../include/fpdfapi/fpdf_module.h"
9 #include "../../../include/fxcodec/fx_codec.h"
12 extern const FX_LPCSTR _PDF_OpCharType =
13 "IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII"
14 "IIVIIIIVIIVIIIIIVVIIIIIIIIIIIIII"
15 "IIVVVVVVIVVVVVVIVVVVVIIVVIIIIIII"
16 "IIVVVVVVVVVVVVVVIVVVIIVVIVVIIIII"
17 "IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII"
18 "IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII"
19 "IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII"
20 "IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII";
21 FX_BOOL _PDF_HasInvalidOpChar(FX_LPCSTR op)
28 if(_PDF_OpCharType[ch] == 'I') {
34 FX_DWORD CPDF_StreamContentParser::Parse(FX_LPCBYTE pData, FX_DWORD dwSize, FX_DWORD max_cost)
36 if (m_Level > _FPDF_MAX_FORM_LEVEL_) {
39 FX_DWORD InitObjCount = m_pObjectList->CountObjects();
40 CPDF_StreamParser syntax(pData, dwSize);
44 FX_DWORD cost = m_pObjectList->CountObjects() - InitObjCount;
45 if (max_cost && cost >= max_cost) {
48 switch (syntax.ParseNextElement()) {
49 case CPDF_StreamParser::EndOfData:
50 return m_pSyntax->GetPos();
51 case CPDF_StreamParser::Keyword:
52 if(!OnOperator((char*)syntax.GetWordBuf()) && _PDF_HasInvalidOpChar((char*)syntax.GetWordBuf())) {
56 return m_pSyntax->GetPos();
60 case CPDF_StreamParser::Number:
61 AddNumberParam((char*)syntax.GetWordBuf(), syntax.GetWordSize());
63 case CPDF_StreamParser::Name:
64 AddNameParam((FX_LPCSTR)syntax.GetWordBuf() + 1, syntax.GetWordSize() - 1);
67 AddObjectParam(syntax.GetObject());
70 return m_pSyntax->GetPos();
72 void _PDF_ReplaceAbbr(CPDF_Object* pObj);
73 void CPDF_StreamContentParser::Handle_BeginImage()
75 FX_FILESIZE savePos = m_pSyntax->GetPos();
76 CPDF_Dictionary* pDict = CPDF_Dictionary::Create();
78 CPDF_StreamParser::SyntaxType type = m_pSyntax->ParseNextElement();
79 if (type == CPDF_StreamParser::Keyword) {
80 CFX_ByteString bsKeyword(m_pSyntax->GetWordBuf(), m_pSyntax->GetWordSize());
81 if (bsKeyword != FX_BSTRC("ID")) {
82 m_pSyntax->SetPos(savePos);
87 if (type != CPDF_StreamParser::Name) {
90 CFX_ByteString key((FX_LPCSTR)m_pSyntax->GetWordBuf() + 1, m_pSyntax->GetWordSize() - 1);
91 CPDF_Object* pObj = m_pSyntax->ReadNextObject();
93 pDict->SetAt(key, pObj, m_pDocument);
98 _PDF_ReplaceAbbr(pDict);
99 CPDF_Object* pCSObj = NULL;
100 if (pDict->KeyExist(FX_BSTRC("ColorSpace"))) {
101 pCSObj = pDict->GetElementValue(FX_BSTRC("ColorSpace"));
102 if (pCSObj->GetType() == PDFOBJ_NAME) {
103 CFX_ByteString name = pCSObj->GetString();
104 if (name != FX_BSTRC("DeviceRGB") && name != FX_BSTRC("DeviceGray") && name != FX_BSTRC("DeviceCMYK")) {
105 pCSObj = FindResourceObj(FX_BSTRC("ColorSpace"), name);
106 if (pCSObj && !pCSObj->GetObjNum()) {
107 pCSObj = pCSObj->Clone();
108 pDict->SetAt(FX_BSTRC("ColorSpace"), pCSObj, m_pDocument);
113 CPDF_Stream* pStream = m_pSyntax->ReadInlineStream(m_pDocument, pDict, pCSObj, m_Options.m_bDecodeInlineImage);
115 CPDF_StreamParser::SyntaxType type = m_pSyntax->ParseNextElement();
116 if (type == CPDF_StreamParser::EndOfData) {
119 if (type != CPDF_StreamParser::Keyword) {
122 if (m_pSyntax->GetWordSize() == 2 && m_pSyntax->GetWordBuf()[0] == 'E' &&
123 m_pSyntax->GetWordBuf()[1] == 'I') {
127 if (m_Options.m_bTextOnly) {
135 pDict->SetAtName(FX_BSTRC("Subtype"), FX_BSTRC("Image"));
136 CPDF_ImageObject *pImgObj = AddImage(pStream, NULL, TRUE);
145 void CPDF_StreamContentParser::ParsePathObject()
147 FX_FLOAT params[6] = {0};
149 int last_pos = m_pSyntax->GetPos();
151 CPDF_StreamParser::SyntaxType type = m_pSyntax->ParseNextElement();
152 FX_BOOL bProcessed = TRUE;
154 case CPDF_StreamParser::EndOfData:
156 case CPDF_StreamParser::Keyword: {
157 int len = m_pSyntax->GetWordSize();
159 switch (m_pSyntax->GetWordBuf()[0]) {
161 AddPathPoint(params[0], params[1], FXPT_MOVETO);
165 AddPathPoint(params[0], params[1], FXPT_LINETO);
169 AddPathPoint(params[0], params[1], FXPT_BEZIERTO);
170 AddPathPoint(params[2], params[3], FXPT_BEZIERTO);
171 AddPathPoint(params[4], params[5], FXPT_BEZIERTO);
175 AddPathPoint(m_PathCurrentX, m_PathCurrentY, FXPT_BEZIERTO);
176 AddPathPoint(params[0], params[1], FXPT_BEZIERTO);
177 AddPathPoint(params[2], params[3], FXPT_BEZIERTO);
181 AddPathPoint(params[0], params[1], FXPT_BEZIERTO);
182 AddPathPoint(params[2], params[3], FXPT_BEZIERTO);
183 AddPathPoint(params[2], params[3], FXPT_BEZIERTO);
194 } else if (len == 2) {
195 if (m_pSyntax->GetWordBuf()[0] == 'r' && m_pSyntax->GetWordBuf()[1] == 'e') {
196 AddPathRect(params[0], params[1], params[2], params[3]);
205 last_pos = m_pSyntax->GetPos();
209 case CPDF_StreamParser::Number: {
215 FX_atonum(CFX_ByteStringC(m_pSyntax->GetWordBuf(), m_pSyntax->GetWordSize()), bInteger, &value);
216 params[nParams++] = bInteger ? (FX_FLOAT)value : *(FX_FLOAT*)&value;
223 m_pSyntax->SetPos(last_pos);
228 CPDF_StreamParser::CPDF_StreamParser(const FX_BYTE* pData, FX_DWORD dwSize)
235 CPDF_StreamParser::~CPDF_StreamParser()
238 m_pLastObj->Release();
241 FX_DWORD _DecodeAllScanlines(ICodec_ScanlineDecoder* pDecoder, FX_LPBYTE& dest_buf, FX_DWORD& dest_size)
243 if (pDecoder == NULL) {
244 return (FX_DWORD) - 1;
246 int ncomps = pDecoder->CountComps();
247 int bpc = pDecoder->GetBPC();
248 int width = pDecoder->GetWidth();
249 int height = pDecoder->GetHeight();
250 int pitch = (width * ncomps * bpc + 7) / 8;
251 if (height == 0 || pitch > (1 << 30) / height) {
255 dest_size = pitch * height;
256 dest_buf = FX_Alloc( FX_BYTE, dest_size);
257 for (int row = 0; row < height; row ++) {
258 FX_LPBYTE pLine = pDecoder->GetScanline(row);
262 FXSYS_memcpy32(dest_buf + row * pitch, pLine, pitch);
264 FX_DWORD srcoff = pDecoder->GetSrcOffset();
268 ICodec_ScanlineDecoder* FPDFAPI_CreateFaxDecoder(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height,
269 const CPDF_Dictionary* pParams);
270 FX_DWORD _A85Decode(const FX_BYTE* src_buf, FX_DWORD src_size, FX_LPBYTE& dest_buf, FX_DWORD& dest_size);
271 FX_DWORD _HexDecode(const FX_BYTE* src_buf, FX_DWORD src_size, FX_LPBYTE& dest_buf, FX_DWORD& dest_size);
272 FX_DWORD FPDFAPI_FlateOrLZWDecode(FX_BOOL bLZW, const FX_BYTE* src_buf, FX_DWORD src_size, CPDF_Dictionary* pParams,
273 FX_DWORD estimated_size, FX_LPBYTE& dest_buf, FX_DWORD& dest_size);
274 FX_DWORD PDF_DecodeInlineStream(const FX_BYTE* src_buf, FX_DWORD limit,
275 int width, int height, CFX_ByteString& decoder,
276 CPDF_Dictionary* pParam, FX_LPBYTE& dest_buf, FX_DWORD& dest_size)
278 if (decoder == FX_BSTRC("CCITTFaxDecode") || decoder == FX_BSTRC("CCF")) {
279 ICodec_ScanlineDecoder* pDecoder = FPDFAPI_CreateFaxDecoder(src_buf, limit, width, height, pParam);
280 return _DecodeAllScanlines(pDecoder, dest_buf, dest_size);
281 } else if (decoder == FX_BSTRC("ASCII85Decode") || decoder == FX_BSTRC("A85")) {
282 return _A85Decode(src_buf, limit, dest_buf, dest_size);
283 } else if (decoder == FX_BSTRC("ASCIIHexDecode") || decoder == FX_BSTRC("AHx")) {
284 return _HexDecode(src_buf, limit, dest_buf, dest_size);
285 } else if (decoder == FX_BSTRC("FlateDecode") || decoder == FX_BSTRC("Fl")) {
286 return FPDFAPI_FlateOrLZWDecode(FALSE, src_buf, limit, pParam, dest_size, dest_buf, dest_size);
287 } else if (decoder == FX_BSTRC("LZWDecode") || decoder == FX_BSTRC("LZW")) {
288 return FPDFAPI_FlateOrLZWDecode(TRUE, src_buf, limit, pParam, 0, dest_buf, dest_size);
289 } else if (decoder == FX_BSTRC("DCTDecode") || decoder == FX_BSTRC("DCT")) {
290 ICodec_ScanlineDecoder* pDecoder = CPDF_ModuleMgr::Get()->GetJpegModule()->CreateDecoder(
291 src_buf, limit, width, height, 0, pParam ? pParam->GetInteger(FX_BSTRC("ColorTransform"), 1) : 1);
292 return _DecodeAllScanlines(pDecoder, dest_buf, dest_size);
293 } else if (decoder == FX_BSTRC("RunLengthDecode") || decoder == FX_BSTRC("RL")) {
294 return RunLengthDecode(src_buf, limit, dest_buf, dest_size);
298 return (FX_DWORD) - 1;
300 extern const FX_LPCSTR _PDF_CharType;
301 CPDF_Stream* CPDF_StreamParser::ReadInlineStream(CPDF_Document* pDoc, CPDF_Dictionary* pDict, CPDF_Object* pCSObj, FX_BOOL bDecode)
303 if (m_Pos == m_Size) {
306 if (_PDF_CharType[m_pBuf[m_Pos]] == 'W') {
309 CFX_ByteString Decoder;
310 CPDF_Dictionary* pParam = NULL;
311 CPDF_Object* pFilter = pDict->GetElementValue(FX_BSTRC("Filter"));
312 if (pFilter == NULL) {
313 } else if (pFilter->GetType() == PDFOBJ_ARRAY) {
314 Decoder = ((CPDF_Array*)pFilter)->GetString(0);
315 CPDF_Array* pParams = pDict->GetArray(FX_BSTRC("DecodeParms"));
317 pParam = pParams->GetDict(0);
320 Decoder = pFilter->GetString();
321 pParam = pDict->GetDict(FX_BSTRC("DecodeParms"));
323 FX_DWORD width = pDict->GetInteger(FX_BSTRC("Width"));
324 FX_DWORD height = pDict->GetInteger(FX_BSTRC("Height"));
325 FX_DWORD OrigSize = 0;
326 if (pCSObj != NULL) {
327 FX_DWORD bpc = pDict->GetInteger(FX_BSTRC("BitsPerComponent"));
328 FX_DWORD nComponents = 1;
329 CPDF_ColorSpace* pCS = pDoc->LoadColorSpace(pCSObj);
333 nComponents = pCS->CountComponents();
334 pDoc->GetPageData()->ReleaseColorSpace(pCSObj);
336 FX_DWORD pitch = width;
337 if (bpc && pitch > INT_MAX / bpc) {
341 if (nComponents && pitch > INT_MAX / nComponents) {
344 pitch *= nComponents;
345 if (pitch > INT_MAX - 7) {
352 if (width > INT_MAX - 7) {
355 OrigSize = ((width + 7) / 8);
357 if (height && OrigSize > INT_MAX / height) {
361 FX_LPBYTE pData = NULL;
362 FX_DWORD dwStreamSize;
363 if (Decoder.IsEmpty()) {
364 if (OrigSize > m_Size - m_Pos) {
365 OrigSize = m_Size - m_Pos;
367 pData = FX_Alloc(FX_BYTE, OrigSize);
368 FXSYS_memcpy32(pData, m_pBuf + m_Pos, OrigSize);
369 dwStreamSize = OrigSize;
372 FX_DWORD dwDestSize = OrigSize;
373 dwStreamSize = PDF_DecodeInlineStream(m_pBuf + m_Pos, m_Size - m_Pos, width, height, Decoder, pParam,
375 if ((int)dwStreamSize < 0) {
379 m_Pos += dwStreamSize;
380 dwStreamSize = dwDestSize;
381 if (pFilter->GetType() == PDFOBJ_ARRAY) {
382 ((CPDF_Array*)pFilter)->RemoveAt(0);
383 CPDF_Array* pParams = pDict->GetArray(FX_BSTRC("DecodeParms"));
385 pParams->RemoveAt(0);
388 pDict->RemoveAt(FX_BSTRC("Filter"));
389 pDict->RemoveAt(FX_BSTRC("DecodeParms"));
395 FX_DWORD dwSavePos = m_Pos;
396 m_Pos += dwStreamSize;
398 FX_DWORD dwPrevPos = m_Pos;
399 CPDF_StreamParser::SyntaxType type = ParseNextElement();
400 if (type == CPDF_StreamParser::EndOfData) {
403 if (type != CPDF_StreamParser::Keyword) {
404 dwStreamSize += m_Pos - dwPrevPos;
407 if (GetWordSize() == 2 && GetWordBuf()[0] == 'E' &&
408 GetWordBuf()[1] == 'I') {
412 dwStreamSize += m_Pos - dwPrevPos;
415 pData = FX_Alloc(FX_BYTE, dwStreamSize);
416 FXSYS_memcpy32(pData, m_pBuf + m_Pos, dwStreamSize);
417 m_Pos += dwStreamSize;
420 pDict->SetAtInteger(FX_BSTRC("Length"), (int)dwStreamSize);
421 return CPDF_Stream::Create(pData, dwStreamSize, pDict);
423 #define MAX_WORD_BUFFER 256
424 #define MAX_STRING_LENGTH 32767
425 #define FXDWORD_TRUE FXDWORD_FROM_LSBFIRST(0x65757274)
426 #define FXDWORD_NULL FXDWORD_FROM_LSBFIRST(0x6c6c756e)
427 #define FXDWORD_FALS FXDWORD_FROM_LSBFIRST(0x736c6166)
428 CPDF_StreamParser::SyntaxType CPDF_StreamParser::ParseNextElement()
431 m_pLastObj->Release();
435 FX_BOOL bIsNumber = TRUE;
436 if (m_Pos >= m_Size) {
439 int ch = m_pBuf[m_Pos++];
440 int type = _PDF_CharType[ch];
442 while (type == 'W') {
443 if (m_Size <= m_Pos) {
446 ch = m_pBuf[m_Pos++];
447 type = _PDF_CharType[ch];
453 if (m_Size <= m_Pos) {
456 ch = m_pBuf[m_Pos++];
457 if (ch == '\r' || ch == '\n') {
461 type = _PDF_CharType[ch];
463 if (type == 'D' && ch != '/') {
465 m_pLastObj = ReadNextObject();
469 if (m_WordSize < MAX_WORD_BUFFER) {
470 m_WordBuffer[m_WordSize++] = ch;
475 if (m_Size <= m_Pos) {
478 ch = m_pBuf[m_Pos++];
479 type = _PDF_CharType[ch];
480 if (type == 'D' || type == 'W') {
485 m_WordBuffer[m_WordSize] = 0;
489 if (m_WordBuffer[0] == '/') {
492 if (m_WordSize == 4) {
493 if (*(FX_DWORD*)m_WordBuffer == FXDWORD_TRUE) {
494 m_pLastObj = CPDF_Boolean::Create(TRUE);
497 if (*(FX_DWORD*)m_WordBuffer == FXDWORD_NULL) {
498 m_pLastObj = CPDF_Null::Create();
501 } else if (m_WordSize == 5) {
502 if (*(FX_DWORD*)m_WordBuffer == FXDWORD_FALS && m_WordBuffer[4] == 'e') {
503 m_pLastObj = CPDF_Boolean::Create(FALSE);
509 void CPDF_StreamParser::SkipPathObject()
511 FX_DWORD command_startpos = m_Pos;
512 if (m_Pos >= m_Size) {
515 int ch = m_pBuf[m_Pos++];
516 int type = _PDF_CharType[ch];
518 while (type == 'W') {
519 if (m_Pos >= m_Size) {
522 ch = m_pBuf[m_Pos++];
523 type = _PDF_CharType[ch];
526 m_Pos = command_startpos;
530 while (type != 'W') {
531 if (m_Pos >= m_Size) {
534 ch = m_pBuf[m_Pos++];
535 type = _PDF_CharType[ch];
537 while (type == 'W') {
538 if (m_Pos >= m_Size) {
541 ch = m_pBuf[m_Pos++];
542 type = _PDF_CharType[ch];
547 FX_DWORD op_startpos = m_Pos - 1;
548 while (type != 'W' && type != 'D') {
549 if (m_Pos >= m_Size) {
552 ch = m_pBuf[m_Pos++];
553 type = _PDF_CharType[ch];
555 if (m_Pos - op_startpos == 2) {
556 int op = m_pBuf[op_startpos];
557 if (op == 'm' || op == 'l' || op == 'c' || op == 'v' || op == 'y') {
558 command_startpos = m_Pos;
561 } else if (m_Pos - op_startpos == 3) {
562 if (m_pBuf[op_startpos] == 'r' && m_pBuf[op_startpos + 1] == 'e') {
563 command_startpos = m_Pos;
567 m_Pos = command_startpos;
572 CPDF_Object* CPDF_StreamParser::ReadNextObject(FX_BOOL bAllowNestedArray, FX_BOOL bInArray)
575 GetNextWord(bIsNumber);
576 if (m_WordSize == 0) {
580 m_WordBuffer[m_WordSize] = 0;
581 return CPDF_Number::Create(CFX_ByteStringC(m_WordBuffer, m_WordSize));
583 int first_char = m_WordBuffer[0];
584 if (first_char == '/') {
585 return CPDF_Name::Create(PDF_NameDecode(CFX_ByteStringC(m_WordBuffer + 1, m_WordSize - 1)));
587 if (first_char == '(') {
588 return CPDF_String::Create(ReadString());
590 if (first_char == '<') {
591 if (m_WordSize == 1) {
592 return CPDF_String::Create(ReadHexString(), TRUE);
594 CPDF_Dictionary* pDict = CPDF_Dictionary::Create();
596 GetNextWord(bIsNumber);
597 if (m_WordSize == 0) {
601 if (m_WordSize == 2 && m_WordBuffer[0] == '>') {
604 if (m_WordBuffer[0] != '/') {
608 CFX_ByteString key = PDF_NameDecode(CFX_ByteStringC(m_WordBuffer + 1, m_WordSize - 1));
609 CPDF_Object* pObj = ReadNextObject(TRUE);
616 if (!key.IsEmpty()) {
617 pDict->SetAt(key, pObj);
624 if (first_char == '[') {
625 if (!bAllowNestedArray && bInArray) {
628 CPDF_Array* pArray = CPDF_Array::Create();
630 CPDF_Object* pObj = ReadNextObject(bAllowNestedArray, TRUE);
632 if (m_WordSize == 0 || m_WordBuffer[0] == ']') {
635 if (m_WordBuffer[0] == '[') {
643 if (m_WordSize == 4) {
644 if (*(FX_DWORD*)m_WordBuffer == FXDWORD_TRUE) {
645 return CPDF_Boolean::Create(TRUE);
647 if (*(FX_DWORD*)m_WordBuffer == FXDWORD_NULL) {
648 return CPDF_Null::Create();
650 } else if (m_WordSize == 5) {
651 if (*(FX_DWORD*)m_WordBuffer == FXDWORD_FALS && m_WordBuffer[4] == 'e') {
652 return CPDF_Boolean::Create(FALSE);
657 void CPDF_StreamParser::GetNextWord(FX_BOOL& bIsNumber)
661 if (m_Size <= m_Pos) {
664 int ch = m_pBuf[m_Pos++];
665 int type = _PDF_CharType[ch];
667 while (type == 'W') {
668 if (m_Size <= m_Pos) {
671 ch = m_pBuf[m_Pos++];
672 type = _PDF_CharType[ch];
678 if (m_Size <= m_Pos) {
681 ch = m_pBuf[m_Pos++];
682 if (ch == '\r' || ch == '\n') {
686 type = _PDF_CharType[ch];
690 m_WordBuffer[m_WordSize++] = ch;
693 if (m_Size <= m_Pos) {
696 ch = m_pBuf[m_Pos++];
697 type = _PDF_CharType[ch];
698 if (type != 'R' && type != 'N') {
702 if (m_WordSize < MAX_WORD_BUFFER) {
703 m_WordBuffer[m_WordSize++] = ch;
706 } else if (ch == '<') {
707 if (m_Size <= m_Pos) {
710 ch = m_pBuf[m_Pos++];
712 m_WordBuffer[m_WordSize++] = ch;
716 } else if (ch == '>') {
717 if (m_Size <= m_Pos) {
720 ch = m_pBuf[m_Pos++];
722 m_WordBuffer[m_WordSize++] = ch;
730 if (m_WordSize < MAX_WORD_BUFFER) {
731 m_WordBuffer[m_WordSize++] = ch;
736 if (m_Size <= m_Pos) {
739 ch = m_pBuf[m_Pos++];
740 type = _PDF_CharType[ch];
741 if (type == 'D' || type == 'W') {
747 CFX_ByteString CPDF_StreamParser::ReadString()
749 if (m_Size <= m_Pos) {
750 return CFX_ByteString();
752 int ch = m_pBuf[m_Pos++];
755 int status = 0, iEscCode = 0;
761 if (buf.GetLength() > MAX_STRING_LENGTH) {
762 return CFX_ByteString(buf.GetBuffer(), MAX_STRING_LENGTH);
764 return buf.GetByteString();
768 } else if (ch == '(') {
771 } else if (ch == '\\') {
774 buf.AppendChar((char)ch);
778 if (ch >= '0' && ch <= '7') {
784 buf.AppendChar('\n');
785 } else if (ch == 'r') {
786 buf.AppendChar('\r');
787 } else if (ch == 't') {
788 buf.AppendChar('\t');
789 } else if (ch == 'b') {
790 buf.AppendChar('\b');
791 } else if (ch == 'f') {
792 buf.AppendChar('\f');
793 } else if (ch == '\r') {
796 } else if (ch == '\n') {
803 if (ch >= '0' && ch <= '7') {
804 iEscCode = iEscCode * 8 + ch - '0';
807 buf.AppendChar(iEscCode);
813 if (ch >= '0' && ch <= '7') {
814 iEscCode = iEscCode * 8 + ch - '0';
815 buf.AppendChar(iEscCode);
818 buf.AppendChar(iEscCode);
830 if (m_Size <= m_Pos) {
833 ch = m_pBuf[m_Pos++];
835 if (m_Size > m_Pos) {
836 ch = m_pBuf[m_Pos++];
838 if (buf.GetLength() > MAX_STRING_LENGTH) {
839 return CFX_ByteString(buf.GetBuffer(), MAX_STRING_LENGTH);
841 return buf.GetByteString();
843 CFX_ByteString CPDF_StreamParser::ReadHexString()
845 if (m_Size <= m_Pos) {
846 return CFX_ByteString();
848 int ch = m_pBuf[m_Pos++];
850 FX_BOOL bFirst = TRUE;
856 if (ch >= '0' && ch <= '9') {
858 code = (ch - '0') * 16;
861 buf.AppendChar((char)code);
864 } else if (ch >= 'A' && ch <= 'F') {
866 code = (ch - 'A' + 10) * 16;
868 code += ch - 'A' + 10;
869 buf.AppendChar((char)code);
872 } else if (ch >= 'a' && ch <= 'f') {
874 code = (ch - 'a' + 10) * 16;
876 code += ch - 'a' + 10;
877 buf.AppendChar((char)code);
881 if (m_Size <= m_Pos) {
884 ch = m_pBuf[m_Pos++];
887 buf.AppendChar((char)code);
889 if (buf.GetLength() > MAX_STRING_LENGTH) {
890 return CFX_ByteString(buf.GetBuffer(), MAX_STRING_LENGTH);
892 return buf.GetByteString();
894 #define PAGEPARSE_STAGE_GETCONTENT 1
895 #define PAGEPARSE_STAGE_PARSE 2
896 #define PAGEPARSE_STAGE_CHECKCLIP 3
897 CPDF_ContentParser::CPDF_ContentParser()
900 m_pStreamArray = NULL;
901 m_pSingleStream = NULL;
906 CPDF_ContentParser::~CPDF_ContentParser()
910 void CPDF_ContentParser::Clear()
915 if (m_pSingleStream) {
916 delete m_pSingleStream;
918 if (m_pStreamArray) {
919 for (FX_DWORD i = 0; i < m_nStreams; i ++)
920 if (m_pStreamArray[i]) {
921 delete m_pStreamArray[i];
923 FX_Free(m_pStreamArray);
925 if (m_pData && m_pSingleStream == NULL) {
926 FX_Free((void*)m_pData);
929 m_pStreamArray = NULL;
930 m_pSingleStream = NULL;
934 void CPDF_ContentParser::Start(CPDF_Page* pPage, CPDF_ParseOptions* pOptions)
936 if (m_Status != Ready || pPage == NULL || pPage->m_pDocument == NULL || pPage->m_pFormDict == NULL) {
943 m_Options = *pOptions;
945 m_Status = ToBeContinued;
946 m_InternalStage = PAGEPARSE_STAGE_GETCONTENT;
948 CPDF_Object* pContent = pPage->m_pFormDict->GetElementValue(FX_BSTRC("Contents"));
949 if (pContent == NULL) {
953 if (pContent->GetType() == PDFOBJ_STREAM) {
955 m_pSingleStream = FX_NEW CPDF_StreamAcc;
956 m_pSingleStream->LoadAllData((CPDF_Stream*)pContent, FALSE);
957 } else if (pContent->GetType() == PDFOBJ_ARRAY) {
958 CPDF_Array* pArray = (CPDF_Array*)pContent;
959 m_nStreams = pArray->GetCount();
960 if (m_nStreams == 0) {
964 m_pStreamArray = FX_Alloc(CPDF_StreamAcc*, m_nStreams);
970 void CPDF_ContentParser::Start(CPDF_Form* pForm, CPDF_AllStates* pGraphicStates,
971 CFX_AffineMatrix* pParentMatrix, CPDF_Type3Char* pType3Char, CPDF_ParseOptions* pOptions, int level)
973 m_pType3Char = pType3Char;
976 CFX_AffineMatrix form_matrix = pForm->m_pFormDict->GetMatrix(FX_BSTRC("Matrix"));
977 if (pGraphicStates) {
978 form_matrix.Concat(pGraphicStates->m_CTM);
980 CPDF_Array* pBBox = pForm->m_pFormDict->GetArray(FX_BSTRC("BBox"));
981 CFX_FloatRect form_bbox;
984 form_bbox = pBBox->GetRect();
986 ClipPath.AppendRect(form_bbox.left, form_bbox.bottom, form_bbox.right, form_bbox.top);
987 ClipPath.Transform(&form_matrix);
989 ClipPath.Transform(pParentMatrix);
991 form_bbox.Transform(&form_matrix);
993 form_bbox.Transform(pParentMatrix);
996 CPDF_Dictionary* pResources = pForm->m_pFormDict->GetDict(FX_BSTRC("Resources"));
997 m_pParser = FX_NEW CPDF_StreamContentParser;
998 m_pParser->Initialize();
999 m_pParser->PrepareParse(pForm->m_pDocument, pForm->m_pPageResources, pForm->m_pResources, pParentMatrix, pForm,
1000 pResources, &form_bbox, pOptions, pGraphicStates, level);
1001 m_pParser->m_pCurStates->m_CTM = form_matrix;
1002 m_pParser->m_pCurStates->m_ParentMatrix = form_matrix;
1003 if (ClipPath.NotNull()) {
1004 m_pParser->m_pCurStates->m_ClipPath.AppendPath(ClipPath, FXFILL_WINDING, TRUE);
1006 if (pForm->m_Transparency & PDFTRANS_GROUP) {
1007 CPDF_GeneralStateData* pData = m_pParser->m_pCurStates->m_GeneralState.GetModify();
1008 pData->m_BlendType = FXDIB_BLEND_NORMAL;
1009 pData->m_StrokeAlpha = 1.0f;
1010 pData->m_FillAlpha = 1.0f;
1011 pData->m_pSoftMask = NULL;
1014 m_pSingleStream = FX_NEW CPDF_StreamAcc;
1015 if (pForm->m_pDocument) {
1016 m_pSingleStream->LoadAllData(pForm->m_pFormStream, FALSE);
1018 m_pSingleStream->LoadAllData(pForm->m_pFormStream, FALSE);
1020 m_pData = (FX_LPBYTE)m_pSingleStream->GetData();
1021 m_Size = m_pSingleStream->GetSize();
1022 m_Status = ToBeContinued;
1023 m_InternalStage = PAGEPARSE_STAGE_PARSE;
1024 m_CurrentOffset = 0;
1026 void CPDF_ContentParser::Continue(IFX_Pause* pPause)
1029 while (m_Status == ToBeContinued) {
1030 if (m_InternalStage == PAGEPARSE_STAGE_GETCONTENT) {
1031 if (m_CurrentOffset == m_nStreams) {
1032 if (m_pStreamArray) {
1035 for (i = 0; i < m_nStreams; i ++) {
1036 FX_DWORD size = m_pStreamArray[i]->GetSize();
1037 if (m_Size + size + 1 <= m_Size) {
1043 m_pData = FX_Alloc(FX_BYTE, m_Size);
1049 for (i = 0; i < m_nStreams; i ++) {
1050 FXSYS_memcpy32(m_pData + pos, m_pStreamArray[i]->GetData(), m_pStreamArray[i]->GetSize());
1051 pos += m_pStreamArray[i]->GetSize() + 1;
1052 m_pData[pos - 1] = ' ';
1053 delete m_pStreamArray[i];
1055 FX_Free(m_pStreamArray);
1056 m_pStreamArray = NULL;
1058 m_pData = (FX_LPBYTE)m_pSingleStream->GetData();
1059 m_Size = m_pSingleStream->GetSize();
1061 m_InternalStage = PAGEPARSE_STAGE_PARSE;
1062 m_CurrentOffset = 0;
1064 CPDF_Array* pContent = m_pObjects->m_pFormDict->GetArray(FX_BSTRC("Contents"));
1065 m_pStreamArray[m_CurrentOffset] = FX_NEW CPDF_StreamAcc;
1066 CPDF_Stream* pStreamObj = (CPDF_Stream*)(pContent ? pContent->GetElementValue(m_CurrentOffset) : NULL);
1067 m_pStreamArray[m_CurrentOffset]->LoadAllData(pStreamObj, FALSE);
1071 if (m_InternalStage == PAGEPARSE_STAGE_PARSE) {
1072 if (m_pParser == NULL) {
1073 m_pParser = FX_NEW CPDF_StreamContentParser;
1074 m_pParser->Initialize();
1075 m_pParser->PrepareParse(m_pObjects->m_pDocument, m_pObjects->m_pPageResources, NULL, NULL, m_pObjects,
1076 m_pObjects->m_pResources, &m_pObjects->m_BBox, &m_Options, NULL, 0);
1077 m_pParser->m_pCurStates->m_ColorState.GetModify()->Default();
1079 if (m_CurrentOffset >= m_Size) {
1080 m_InternalStage = PAGEPARSE_STAGE_CHECKCLIP;
1082 m_CurrentOffset += m_pParser->Parse(m_pData + m_CurrentOffset, m_Size - m_CurrentOffset, PARSE_STEP_LIMIT);
1083 if (m_pParser->m_bAbort) {
1084 m_InternalStage = PAGEPARSE_STAGE_CHECKCLIP;
1089 if (m_InternalStage == PAGEPARSE_STAGE_CHECKCLIP) {
1091 m_pType3Char->m_bColored = m_pParser->m_bColored;
1092 m_pType3Char->m_Width = FXSYS_round(m_pParser->m_Type3Data[0] * 1000);
1093 m_pType3Char->m_BBox.left = FXSYS_round(m_pParser->m_Type3Data[2] * 1000);
1094 m_pType3Char->m_BBox.bottom = FXSYS_round(m_pParser->m_Type3Data[3] * 1000);
1095 m_pType3Char->m_BBox.right = FXSYS_round(m_pParser->m_Type3Data[4] * 1000);
1096 m_pType3Char->m_BBox.top = FXSYS_round(m_pParser->m_Type3Data[5] * 1000);
1098 FX_POSITION pos = m_pObjects->m_ObjectList.GetHeadPosition();
1100 CPDF_PageObject* pObj = (CPDF_PageObject*)m_pObjects->m_ObjectList.GetNext(pos);
1101 if (pObj->m_ClipPath.IsNull()) {
1104 if (pObj->m_ClipPath.GetPathCount() != 1) {
1107 if (pObj->m_ClipPath.GetTextCount()) {
1110 CPDF_Path ClipPath = pObj->m_ClipPath.GetPath(0);
1111 if (!ClipPath.IsRect() || pObj->m_Type == PDFPAGE_SHADING) {
1114 CFX_FloatRect old_rect(ClipPath.GetPointX(0), ClipPath.GetPointY(0),
1115 ClipPath.GetPointX(2), ClipPath.GetPointY(2));
1116 CFX_FloatRect obj_rect(pObj->m_Left, pObj->m_Bottom, pObj->m_Right, pObj->m_Top);
1117 if (old_rect.Contains(obj_rect)) {
1118 pObj->m_ClipPath.SetNull();
1125 if (pPause && pPause->NeedToPauseNow()) {
1130 int CPDF_ContentParser::EstimateProgress()
1132 if (m_Status == Ready) {
1135 if (m_Status == Done) {
1138 if (m_InternalStage == PAGEPARSE_STAGE_GETCONTENT) {
1141 if (m_InternalStage == PAGEPARSE_STAGE_CHECKCLIP) {
1144 return 10 + 80 * m_CurrentOffset / m_Size;