void CPDF_IndirectObjectHolder::DeleteIndirectObject(uint32_t objnum) { CPDF_Object* pObj = GetIndirectObject(objnum); if (!pObj || pObj->GetObjNum() == CPDF_Object::kInvalidObjNum) return; m_IndirectObjs.erase(objnum); }
FX_BOOL CPDF_Document::IsContentUsedElsewhere(FX_DWORD objnum, CPDF_Dictionary* pThisPageDict) { for (int i = 0; i < m_PageList.GetSize(); i++) { CPDF_Dictionary* pPageDict = GetPage(i); if (pPageDict == pThisPageDict) { continue; } CPDF_Object* pContents = pPageDict ? pPageDict->GetElement("Contents") : NULL; if (!pContents) { continue; } if (pContents->GetDirectType() == PDFOBJ_ARRAY) { CPDF_Array* pArray = pContents->GetDirect()->AsArray(); for (FX_DWORD j = 0; j < pArray->GetCount(); j++) { CPDF_Reference* pRef = ToReference(pArray->GetElement(j)); if (pRef && pRef->GetRefObjNum() == objnum) return TRUE; } } else if (pContents->GetObjNum() == objnum) { return TRUE; } } return FALSE; }
FX_BOOL CPDF_StructTreeImpl::AddTopLevelNode(CPDF_Dictionary* pDict, CPDF_StructElementImpl* pElement) { CPDF_Object* pObj = m_pTreeRoot->GetDirectObjectBy("K"); if (!pObj) return FALSE; if (pObj->IsDictionary()) { if (pObj->GetObjNum() != pDict->GetObjNum()) return FALSE; m_Kids[0].Reset(pElement); } if (CPDF_Array* pTopKids = pObj->AsArray()) { bool bSave = false; for (size_t i = 0; i < pTopKids->GetCount(); i++) { CPDF_Reference* pKidRef = ToReference(pTopKids->GetObjectAt(i)); if (pKidRef && pKidRef->GetRefObjNum() == pDict->GetObjNum()) { m_Kids[i].Reset(pElement); bSave = true; } } if (!bSave) return FALSE; } return TRUE; }
int CPDF_Dest::GetPageIndex(CPDF_Document* pDoc) { CPDF_Array* pArray = ToArray(m_pObj); if (!pArray) return 0; CPDF_Object* pPage = pArray->GetElementValue(0); if (!pPage) return 0; if (pPage->IsNumber()) return pPage->GetInteger(); if (!pPage->IsDictionary()) return 0; return pDoc->GetPageIndex(pPage->GetObjNum()); }
FX_DWORD CPDF_Dest::GetPageObjNum() { CPDF_Array* pArray = ToArray(m_pObj); if (!pArray) return 0; CPDF_Object* pPage = pArray->GetElementValue(0); if (!pPage) return 0; if (pPage->IsNumber()) return pPage->GetInteger(); if (pPage->IsDictionary()) return pPage->GetObjNum(); return 0; }
uint32_t CPDF_Dest::GetPageObjNum() { CPDF_Array* pArray = ToArray(m_pObj); if (!pArray) return 0; CPDF_Object* pPage = pArray->GetDirectObjectAt(0); if (!pPage) return 0; if (pPage->IsNumber()) return pPage->GetInteger(); if (pPage->IsDictionary()) return pPage->GetObjNum(); return 0; }
int CPDF_Dest::GetPageIndex(CPDF_Document* pDoc) { if (m_pObj == NULL || m_pObj->GetType() != PDFOBJ_ARRAY) { return 0; } CPDF_Object* pPage = ((CPDF_Array*)m_pObj)->GetElementValue(0); if (pPage == NULL) { return 0; } if (pPage->GetType() == PDFOBJ_NUMBER) { return pPage->GetInteger(); } if (pPage->GetType() != PDFOBJ_DICTIONARY) { return 0; } return pDoc->GetPageIndex(pPage->GetObjNum()); }
CPDF_Object* CPDF_IndirectObjectHolder::GetOrParseIndirectObject( uint32_t objnum) { if (objnum == 0) return nullptr; CPDF_Object* pObj = GetIndirectObject(objnum); if (pObj) return pObj->GetObjNum() != CPDF_Object::kInvalidObjNum ? pObj : nullptr; std::unique_ptr<CPDF_Object> pNewObj = ParseIndirectObject(objnum); if (!pNewObj) return nullptr; pNewObj->m_ObjNum = objnum; m_LastObjNum = std::max(m_LastObjNum, objnum); m_IndirectObjs[objnum] = std::move(pNewObj); return m_IndirectObjs[objnum].get(); }
CPDF_Object* CPDF_IndirectObjectHolder::GetOrParseIndirectObject( uint32_t objnum) { if (objnum == 0) return nullptr; CPDF_Object* pObj = GetIndirectObject(objnum); if (pObj) return pObj->GetObjNum() != CPDF_Object::kInvalidObjNum ? pObj : nullptr; pObj = ParseIndirectObject(objnum); if (!pObj) return nullptr; pObj->m_ObjNum = objnum; m_LastObjNum = std::max(m_LastObjNum, objnum); if (m_IndirectObjs[objnum]) m_IndirectObjs[objnum]->Destroy(); m_IndirectObjs[objnum] = pObj; return pObj; }
FX_BOOL CPDF_StructTreeImpl::AddTopLevelNode(CPDF_Dictionary* pDict, CPDF_StructElementImpl* pElement) { CPDF_Object *pObj = m_pTreeRoot->GetElementValue(FX_BSTRC("K")); if (!pObj) { return FALSE; } if (pObj->GetType() == PDFOBJ_DICTIONARY) { if (pObj->GetObjNum() == pDict->GetObjNum()) { if (m_Kids[0]) { m_Kids[0]->Release(); } m_Kids[0] = pElement->Retain(); } else { return FALSE; } } if (pObj->GetType() == PDFOBJ_ARRAY) { CPDF_Array* pTopKids = (CPDF_Array*)pObj; FX_DWORD i; FX_BOOL bSave = FALSE; for (i = 0; i < pTopKids->GetCount(); i ++) { CPDF_Object* pKidRef = pTopKids->GetElement(i); if (pKidRef == NULL || pKidRef->GetType() != PDFOBJ_REFERENCE) { continue; } if (((CPDF_Reference*) pKidRef)->GetRefObjNum() != pDict->GetObjNum()) { continue; } if (m_Kids[i]) { m_Kids[i]->Release(); } m_Kids[i] = pElement->Retain(); bSave = TRUE; } if (!bSave) { return FALSE; } } return TRUE; }
DLLEXPORT FPDF_BOOL STDCALL FPDFPage_TransFormWithClip(FPDF_PAGE page, FS_MATRIX* matrix, FS_RECTF* clipRect) { CPDF_Page* pPage = CPDFPageFromFPDFPage(page); if (!pPage) return FALSE; CFX_ByteTextBuf textBuf; textBuf << "q "; CFX_FloatRect rect(clipRect->left, clipRect->bottom, clipRect->right, clipRect->top); rect.Normalize(); CFX_ByteString bsClipping; bsClipping.Format("%f %f %f %f re W* n ", rect.left, rect.bottom, rect.Width(), rect.Height()); textBuf << bsClipping; CFX_ByteString bsMatix; bsMatix.Format("%f %f %f %f %f %f cm ", matrix->a, matrix->b, matrix->c, matrix->d, matrix->e, matrix->f); textBuf << bsMatix; CPDF_Dictionary* pPageDic = pPage->m_pFormDict; CPDF_Object* pContentObj = pPageDic ? pPageDic->GetElement("Contents") : nullptr; if (!pContentObj) pContentObj = pPageDic ? pPageDic->GetArrayBy("Contents") : nullptr; if (!pContentObj) return FALSE; CPDF_Dictionary* pDic = new CPDF_Dictionary; CPDF_Stream* pStream = new CPDF_Stream(nullptr, 0, pDic); pStream->SetData(textBuf.GetBuffer(), textBuf.GetSize(), FALSE, FALSE); CPDF_Document* pDoc = pPage->m_pDocument; if (!pDoc) return FALSE; pDoc->AddIndirectObject(pStream); pDic = new CPDF_Dictionary; CPDF_Stream* pEndStream = new CPDF_Stream(nullptr, 0, pDic); pEndStream->SetData((const uint8_t*)" Q", 2, FALSE, FALSE); pDoc->AddIndirectObject(pEndStream); CPDF_Array* pContentArray = nullptr; if (CPDF_Array* pArray = ToArray(pContentObj)) { pContentArray = pArray; CPDF_Reference* pRef = new CPDF_Reference(pDoc, pStream->GetObjNum()); pContentArray->InsertAt(0, pRef); pContentArray->AddReference(pDoc, pEndStream); } else if (CPDF_Reference* pReference = ToReference(pContentObj)) { CPDF_Object* pDirectObj = pReference->GetDirect(); if (pDirectObj) { if (CPDF_Array* pArray = pDirectObj->AsArray()) { pContentArray = pArray; CPDF_Reference* pRef = new CPDF_Reference(pDoc, pStream->GetObjNum()); pContentArray->InsertAt(0, pRef); pContentArray->AddReference(pDoc, pEndStream); } else if (pDirectObj->IsStream()) { pContentArray = new CPDF_Array(); pContentArray->AddReference(pDoc, pStream->GetObjNum()); pContentArray->AddReference(pDoc, pDirectObj->GetObjNum()); pContentArray->AddReference(pDoc, pEndStream); pPageDic->SetAtReference("Contents", pDoc, pDoc->AddIndirectObject(pContentArray)); } } } // Need to transform the patterns as well. CPDF_Dictionary* pRes = pPageDic->GetDictBy("Resources"); if (pRes) { CPDF_Dictionary* pPattenDict = pRes->GetDictBy("Pattern"); if (pPattenDict) { for (const auto& it : *pPattenDict) { CPDF_Object* pObj = it.second; if (pObj->IsReference()) pObj = pObj->GetDirect(); CPDF_Dictionary* pDict = nullptr; if (pObj->IsDictionary()) pDict = pObj->AsDictionary(); else if (CPDF_Stream* pStream = pObj->AsStream()) pDict = pStream->GetDict(); else continue; CFX_Matrix m = pDict->GetMatrixBy("Matrix"); CFX_Matrix t = *(CFX_Matrix*)matrix; m.Concat(t); pDict->SetAtMatrix("Matrix", m); } } } return TRUE; }
DLLEXPORT void STDCALL FPDFPage_InsertClipPath(FPDF_PAGE page, FPDF_CLIPPATH clipPath) { CPDF_Page* pPage = CPDFPageFromFPDFPage(page); if (!pPage) return; CPDF_Dictionary* pPageDic = pPage->m_pFormDict; CPDF_Object* pContentObj = pPageDic ? pPageDic->GetElement("Contents") : nullptr; if (!pContentObj) pContentObj = pPageDic ? pPageDic->GetArrayBy("Contents") : nullptr; if (!pContentObj) return; CFX_ByteTextBuf strClip; CPDF_ClipPath* pClipPath = (CPDF_ClipPath*)clipPath; FX_DWORD i; for (i = 0; i < pClipPath->GetPathCount(); i++) { CPDF_Path path = pClipPath->GetPath(i); int iClipType = pClipPath->GetClipType(i); if (path.GetPointCount() == 0) { // Empty clipping (totally clipped out) strClip << "0 0 m W n "; } else { OutputPath(strClip, path); if (iClipType == FXFILL_WINDING) strClip << "W n\n"; else strClip << "W* n\n"; } } CPDF_Dictionary* pDic = new CPDF_Dictionary; CPDF_Stream* pStream = new CPDF_Stream(nullptr, 0, pDic); pStream->SetData(strClip.GetBuffer(), strClip.GetSize(), FALSE, FALSE); CPDF_Document* pDoc = pPage->m_pDocument; if (!pDoc) return; pDoc->AddIndirectObject(pStream); CPDF_Array* pContentArray = nullptr; if (CPDF_Array* pArray = ToArray(pContentObj)) { pContentArray = pArray; CPDF_Reference* pRef = new CPDF_Reference(pDoc, pStream->GetObjNum()); pContentArray->InsertAt(0, pRef); } else if (CPDF_Reference* pReference = ToReference(pContentObj)) { CPDF_Object* pDirectObj = pReference->GetDirect(); if (pDirectObj) { if (CPDF_Array* pArray = pDirectObj->AsArray()) { pContentArray = pArray; CPDF_Reference* pRef = new CPDF_Reference(pDoc, pStream->GetObjNum()); pContentArray->InsertAt(0, pRef); } else if (pDirectObj->IsStream()) { pContentArray = new CPDF_Array(); pContentArray->AddReference(pDoc, pStream->GetObjNum()); pContentArray->AddReference(pDoc, pDirectObj->GetObjNum()); pPageDic->SetAtReference("Contents", pDoc, pDoc->AddIndirectObject(pContentArray)); } } } }
void CPDF_StreamContentParser::EndImageDict() { if (m_StringBuf.GetSize() != m_LastImageDict.GetSize() || FXSYS_memcmp32(m_StringBuf.GetBuffer(), m_LastImageDict.GetBuffer(), m_StringBuf.GetSize())) { m_WordState = 0; StartDict(); InputData(m_StringBuf.GetBuffer(), m_StringBuf.GetSize()); Finish(); m_bSameLastDict = FALSE; if (m_pLastImageDict && m_bReleaseLastDict) { m_pLastImageDict->Release(); m_pLastImageDict = NULL; } if (!m_ObjectSize) { m_InlineImageState = 0; return; } m_pLastImageDict = (CPDF_Dictionary*)m_pObjectStack[--m_ObjectSize]; m_bReleaseLastDict = !m_pObjectState[m_ObjectSize]; m_pObjectState[m_ObjectSize] = FALSE; _PDF_ReplaceAbbr(m_pLastImageDict); m_LastImageDict.TakeOver(m_StringBuf); if (m_pLastImageDict->KeyExist(FX_BSTRC("ColorSpace"))) { CPDF_Object* pCSObj = m_pLastImageDict->GetElementValue(FX_BSTRC("ColorSpace")); if (pCSObj->GetType() == PDFOBJ_NAME) { CFX_ByteString name = pCSObj->GetString(); if (name != FX_BSTRC("DeviceRGB") && name != FX_BSTRC("DeviceGray") && name != FX_BSTRC("DeviceCMYK")) { pCSObj = FindResourceObj(FX_BSTRC("ColorSpace"), name); if (pCSObj) { if (!pCSObj->GetObjNum()) { pCSObj = pCSObj->Clone(); } m_pLastImageDict->SetAt(FX_BSTRC("ColorSpace"), pCSObj, m_pDocument); } } } } } else { m_bSameLastDict = TRUE; } m_ImageSrcBuf.Clear(); if (m_pLastCloneImageDict) { m_pLastCloneImageDict->Release(); } m_pLastCloneImageDict = (CPDF_Dictionary*)m_pLastImageDict->Clone(); if (m_pLastCloneImageDict->KeyExist(FX_BSTRC("Filter"))) { m_WordState = 10; m_InlineImageState = 0; } else { int width = m_pLastCloneImageDict->GetInteger(FX_BSTRC("Width")); int height = m_pLastCloneImageDict->GetInteger(FX_BSTRC("Height")); int OrigSize = 0; CPDF_Object* pCSObj = m_pLastCloneImageDict->GetElementValue(FX_BSTRC("ColorSpace")); if (pCSObj != NULL) { int bpc = m_pLastCloneImageDict->GetInteger(FX_BSTRC("BitsPerComponent")); int nComponents = 1; CPDF_ColorSpace* pCS = m_pDocument->LoadColorSpace(pCSObj); if (pCS == NULL) { nComponents = 3; } else { nComponents = pCS->CountComponents(); m_pDocument->GetPageData()->ReleaseColorSpace(pCSObj); } int pitch = (width * bpc * nComponents + 7) / 8; OrigSize = pitch * height; } else { OrigSize = ((width + 7) / 8) * height; } m_ImageSrcBuf.AppendBlock(NULL, OrigSize); m_WordState = 11; m_InlineImageState = 0; } }
void CPDF_StreamContentParser::Handle_BeginImage() { FX_FILESIZE savePos = m_pSyntax->GetPos(); CPDF_Dictionary* pDict = CPDF_Dictionary::Create(); while (1) { CPDF_StreamParser::SyntaxType type = m_pSyntax->ParseNextElement(); if (type == CPDF_StreamParser::Keyword) { CFX_ByteString bsKeyword(m_pSyntax->GetWordBuf(), m_pSyntax->GetWordSize()); if (bsKeyword != FX_BSTRC("ID")) { m_pSyntax->SetPos(savePos); pDict->Release(); return; } } if (type != CPDF_StreamParser::Name) { break; } CFX_ByteString key((const FX_CHAR*)m_pSyntax->GetWordBuf() + 1, m_pSyntax->GetWordSize() - 1); CPDF_Object* pObj = m_pSyntax->ReadNextObject(); if (!key.IsEmpty()) { pDict->SetAt(key, pObj, m_pDocument); } else if (pObj) { pObj->Release(); } } _PDF_ReplaceAbbr(pDict); CPDF_Object* pCSObj = NULL; if (pDict->KeyExist(FX_BSTRC("ColorSpace"))) { pCSObj = pDict->GetElementValue(FX_BSTRC("ColorSpace")); if (pCSObj->GetType() == PDFOBJ_NAME) { CFX_ByteString name = pCSObj->GetString(); if (name != FX_BSTRC("DeviceRGB") && name != FX_BSTRC("DeviceGray") && name != FX_BSTRC("DeviceCMYK")) { pCSObj = FindResourceObj(FX_BSTRC("ColorSpace"), name); if (pCSObj && !pCSObj->GetObjNum()) { pCSObj = pCSObj->Clone(); pDict->SetAt(FX_BSTRC("ColorSpace"), pCSObj, m_pDocument); } } } } CPDF_Stream* pStream = m_pSyntax->ReadInlineStream(m_pDocument, pDict, pCSObj, m_Options.m_bDecodeInlineImage); while (1) { CPDF_StreamParser::SyntaxType type = m_pSyntax->ParseNextElement(); if (type == CPDF_StreamParser::EndOfData) { break; } if (type != CPDF_StreamParser::Keyword) { continue; } if (m_pSyntax->GetWordSize() == 2 && m_pSyntax->GetWordBuf()[0] == 'E' && m_pSyntax->GetWordBuf()[1] == 'I') { break; } } if (m_Options.m_bTextOnly) { if (pStream) { pStream->Release(); } else { pDict->Release(); } return; } pDict->SetAtName(FX_BSTRC("Subtype"), FX_BSTRC("Image")); CPDF_ImageObject *pImgObj = AddImage(pStream, NULL, TRUE); if (!pImgObj) { if (pStream) { pStream->Release(); } else { pDict->Release(); } } }
DLLEXPORT FPDF_BOOL STDCALL FPDFPage_TransFormWithClip(FPDF_PAGE page, FS_MATRIX* matrix, FS_RECTF* clipRect) { if(!page) return FALSE; CFX_ByteTextBuf textBuf; textBuf<<"q "; CFX_FloatRect rect(clipRect->left, clipRect->bottom, clipRect->right, clipRect->top); rect.Normalize(); CFX_ByteString bsClipping; bsClipping.Format("%f %f %f %f re W* n ", rect.left, rect.bottom, rect.Width(), rect.Height()); textBuf<<bsClipping; CFX_ByteString bsMatix; bsMatix.Format("%f %f %f %f %f %f cm ", matrix->a, matrix->b,matrix->c,matrix->d,matrix->e,matrix->f); textBuf<<bsMatix; CPDF_Page* pPage = (CPDF_Page*)page; CPDF_Dictionary* pPageDic = pPage->m_pFormDict; CPDF_Object* pContentObj = pPageDic->GetElement("Contents"); if(!pContentObj) pContentObj = pPageDic->GetArray("Contents"); if(!pContentObj) return FALSE; CPDF_Dictionary* pDic = FX_NEW CPDF_Dictionary; CPDF_Stream* pStream = FX_NEW CPDF_Stream(NULL,0, pDic); pStream->SetData(textBuf.GetBuffer(), textBuf.GetSize(), FALSE, FALSE); CPDF_Document* pDoc = pPage->m_pDocument; if(!pDoc) return FALSE; pDoc->AddIndirectObject(pStream); pDic = FX_NEW CPDF_Dictionary; CPDF_Stream* pEndStream = FX_NEW CPDF_Stream(NULL,0, pDic); pEndStream->SetData((FX_LPCBYTE)" Q", 2, FALSE, FALSE); pDoc->AddIndirectObject(pEndStream); CPDF_Array* pContentArray = NULL; if (pContentObj && pContentObj->GetType() == PDFOBJ_ARRAY) { pContentArray = (CPDF_Array*)pContentObj; CPDF_Reference* pRef = FX_NEW CPDF_Reference(pDoc, pStream->GetObjNum()); pContentArray->InsertAt(0, pRef); pContentArray->AddReference(pDoc,pEndStream); } else if(pContentObj && pContentObj->GetType() == PDFOBJ_REFERENCE) { CPDF_Reference* pReference = (CPDF_Reference*)pContentObj; CPDF_Object* pDirectObj = pReference->GetDirect(); if(pDirectObj != NULL) { if(pDirectObj->GetType() == PDFOBJ_ARRAY) { pContentArray = (CPDF_Array*)pDirectObj; CPDF_Reference* pRef = FX_NEW CPDF_Reference(pDoc, pStream->GetObjNum()); pContentArray->InsertAt(0, pRef); pContentArray->AddReference(pDoc,pEndStream); } else if(pDirectObj->GetType() == PDFOBJ_STREAM) { pContentArray = FX_NEW CPDF_Array(); pContentArray->AddReference(pDoc,pStream->GetObjNum()); pContentArray->AddReference(pDoc,pDirectObj->GetObjNum()); pContentArray->AddReference(pDoc, pEndStream); pPageDic->SetAtReference("Contents", pDoc, pDoc->AddIndirectObject(pContentArray)); } } } //Need to transform the patterns as well. CPDF_Dictionary* pRes = pPageDic->GetDict(FX_BSTRC("Resources")); if(pRes) { CPDF_Dictionary* pPattenDict = pRes->GetDict(FX_BSTRC("Pattern")); if(pPattenDict) { FX_POSITION pos = pPattenDict->GetStartPos(); while(pos) { CPDF_Dictionary* pDict = NULL; CFX_ByteString key; CPDF_Object* pObj = pPattenDict->GetNextElement(pos, key); if(pObj->GetType() == PDFOBJ_REFERENCE) pObj = pObj->GetDirect(); if(pObj->GetType() == PDFOBJ_DICTIONARY) { pDict = (CPDF_Dictionary*)pObj; } else if(pObj->GetType() == PDFOBJ_STREAM) { pDict = ((CPDF_Stream*)pObj)->GetDict(); } else continue; CFX_AffineMatrix m = pDict->GetMatrix(FX_BSTRC("Matrix")); CFX_AffineMatrix t = *(CFX_AffineMatrix*)matrix; m.Concat(t); pDict->SetAtMatrix(FX_BSTRC("Matrix"), m); } } } return TRUE; }
DLLEXPORT void STDCALL FPDFPage_InsertClipPath(FPDF_PAGE page,FPDF_CLIPPATH clipPath) { if(!page) return; CPDF_Page* pPage = (CPDF_Page*)page; CPDF_Dictionary* pPageDic = pPage->m_pFormDict; CPDF_Object* pContentObj = pPageDic->GetElement("Contents"); if(!pContentObj) pContentObj = pPageDic->GetArray("Contents"); if(!pContentObj) return; CFX_ByteTextBuf strClip; CPDF_ClipPath* pClipPath = (CPDF_ClipPath*)clipPath; FX_DWORD i; for (i = 0; i < pClipPath->GetPathCount(); i ++) { CPDF_Path path = pClipPath->GetPath(i); int iClipType = pClipPath->GetClipType(i); if (path.GetPointCount() == 0) { // Empty clipping (totally clipped out) strClip << "0 0 m W n "; } else { OutputPath(strClip, path); if (iClipType == FXFILL_WINDING) strClip << "W n\n"; else strClip << "W* n\n"; } } CPDF_Dictionary* pDic = FX_NEW CPDF_Dictionary; CPDF_Stream* pStream = FX_NEW CPDF_Stream(NULL,0, pDic); pStream->SetData(strClip.GetBuffer(), strClip.GetSize(), FALSE, FALSE); CPDF_Document* pDoc = pPage->m_pDocument; if(!pDoc) return; pDoc->AddIndirectObject(pStream); CPDF_Array* pContentArray = NULL; if (pContentObj && pContentObj->GetType() == PDFOBJ_ARRAY) { pContentArray = (CPDF_Array*)pContentObj; CPDF_Reference* pRef = FX_NEW CPDF_Reference(pDoc, pStream->GetObjNum()); pContentArray->InsertAt(0, pRef); } else if(pContentObj && pContentObj->GetType() == PDFOBJ_REFERENCE) { CPDF_Reference* pReference = (CPDF_Reference*)pContentObj; CPDF_Object* pDirectObj = pReference->GetDirect(); if(pDirectObj != NULL) { if(pDirectObj->GetType() == PDFOBJ_ARRAY) { pContentArray = (CPDF_Array*)pDirectObj; CPDF_Reference* pRef = FX_NEW CPDF_Reference(pDoc, pStream->GetObjNum()); pContentArray->InsertAt(0, pRef); } else if(pDirectObj->GetType() == PDFOBJ_STREAM) { pContentArray = FX_NEW CPDF_Array(); pContentArray->AddReference(pDoc,pStream->GetObjNum()); pContentArray->AddReference(pDoc,pDirectObj->GetObjNum()); pPageDic->SetAtReference("Contents", pDoc, pDoc->AddIndirectObject(pContentArray)); } } } }
DLLEXPORT void STDCALL FPDFPage_InsertClipPath(FPDF_PAGE page, FPDF_CLIPPATH clipPath) { CPDF_Page* pPage = CPDFPageFromFPDFPage(page); if (!pPage) return; CPDF_Dictionary* pPageDic = pPage->m_pFormDict; CPDF_Object* pContentObj = pPageDic ? pPageDic->GetObjectFor("Contents") : nullptr; if (!pContentObj) pContentObj = pPageDic ? pPageDic->GetArrayFor("Contents") : nullptr; if (!pContentObj) return; CFX_ByteTextBuf strClip; CPDF_ClipPath* pClipPath = (CPDF_ClipPath*)clipPath; uint32_t i; for (i = 0; i < pClipPath->GetPathCount(); i++) { CPDF_Path path = pClipPath->GetPath(i); int iClipType = pClipPath->GetClipType(i); if (path.GetPoints().empty()) { // Empty clipping (totally clipped out) strClip << "0 0 m W n "; } else { OutputPath(strClip, path); if (iClipType == FXFILL_WINDING) strClip << "W n\n"; else strClip << "W* n\n"; } } CPDF_Document* pDoc = pPage->m_pDocument; if (!pDoc) return; CPDF_Stream* pStream = pDoc->NewIndirect<CPDF_Stream>( nullptr, 0, pdfium::MakeUnique<CPDF_Dictionary>(pDoc->GetByteStringPool())); pStream->SetData(strClip.GetBuffer(), strClip.GetSize()); CPDF_Array* pArray = ToArray(pContentObj); if (pArray) { pArray->InsertNewAt<CPDF_Reference>(0, pDoc, pStream->GetObjNum()); return; } CPDF_Reference* pReference = ToReference(pContentObj); if (!pReference) return; CPDF_Object* pDirectObj = pReference->GetDirect(); if (!pDirectObj) return; CPDF_Array* pObjArray = pDirectObj->AsArray(); if (pObjArray) { pObjArray->InsertNewAt<CPDF_Reference>(0, pDoc, pStream->GetObjNum()); return; } if (pDirectObj->IsStream()) { CPDF_Array* pContentArray = pDoc->NewIndirect<CPDF_Array>(); pContentArray->AddNew<CPDF_Reference>(pDoc, pStream->GetObjNum()); pContentArray->AddNew<CPDF_Reference>(pDoc, pDirectObj->GetObjNum()); pPageDic->SetNewFor<CPDF_Reference>("Contents", pDoc, pContentArray->GetObjNum()); } }
CFX_ByteTextBuf& operator<<(CFX_ByteTextBuf& buf, const CPDF_Object* pObj) { if (!pObj) { buf << " null"; return buf; } switch (pObj->GetType()) { case PDFOBJ_NULL: buf << " null"; break; case PDFOBJ_BOOLEAN: case PDFOBJ_NUMBER: buf << " " << pObj->GetString(); break; case PDFOBJ_STRING: buf << PDF_EncodeString(pObj->GetString(), pObj->AsString()->IsHex()); break; case PDFOBJ_NAME: { CFX_ByteString str = pObj->GetString(); buf << "/" << PDF_NameEncode(str); break; } case PDFOBJ_REFERENCE: { buf << " " << pObj->AsReference()->GetRefObjNum() << " 0 R "; break; } case PDFOBJ_ARRAY: { const CPDF_Array* p = pObj->AsArray(); buf << "["; for (FX_DWORD i = 0; i < p->GetCount(); i++) { CPDF_Object* pElement = p->GetElement(i); if (pElement->GetObjNum()) { buf << " " << pElement->GetObjNum() << " 0 R"; } else { buf << pElement; } } buf << "]"; break; } case PDFOBJ_DICTIONARY: { const CPDF_Dictionary* p = pObj->AsDictionary(); buf << "<<"; for (const auto& it : *p) { const CFX_ByteString& key = it.first; CPDF_Object* pValue = it.second; buf << "/" << PDF_NameEncode(key); if (pValue && pValue->GetObjNum()) { buf << " " << pValue->GetObjNum() << " 0 R "; } else { buf << pValue; } } buf << ">>"; break; } case PDFOBJ_STREAM: { const CPDF_Stream* p = pObj->AsStream(); buf << p->GetDict() << "stream\r\n"; CPDF_StreamAcc acc; acc.LoadAllData(p, TRUE); buf.AppendBlock(acc.GetData(), acc.GetSize()); buf << "\r\nendstream"; break; } default: ASSERT(FALSE); break; } return buf; }
CFX_ByteTextBuf& operator<<(CFX_ByteTextBuf& buf, const CPDF_Object* pObj) { if (pObj == NULL) { buf << FX_BSTRC(" null"); return buf; } switch (pObj->GetType()) { case PDFOBJ_NULL: buf << FX_BSTRC(" null"); break; case PDFOBJ_BOOLEAN: case PDFOBJ_NUMBER: buf << " " << pObj->GetString(); break; case PDFOBJ_STRING: buf << PDF_EncodeString(pObj->GetString(), pObj->AsString()->IsHex()); break; case PDFOBJ_NAME: { CFX_ByteString str = pObj->GetString(); buf << FX_BSTRC("/") << PDF_NameEncode(str); break; } case PDFOBJ_REFERENCE: { buf << " " << pObj->AsReference()->GetRefObjNum() << FX_BSTRC(" 0 R "); break; } case PDFOBJ_ARRAY: { const CPDF_Array* p = pObj->AsArray(); buf << FX_BSTRC("["); for (FX_DWORD i = 0; i < p->GetCount(); i++) { CPDF_Object* pElement = p->GetElement(i); if (pElement->GetObjNum()) { buf << " " << pElement->GetObjNum() << FX_BSTRC(" 0 R"); } else { buf << pElement; } } buf << FX_BSTRC("]"); break; } case PDFOBJ_DICTIONARY: { const CPDF_Dictionary* p = pObj->AsDictionary(); buf << FX_BSTRC("<<"); FX_POSITION pos = p->GetStartPos(); while (pos) { CFX_ByteString key; CPDF_Object* pValue = p->GetNextElement(pos, key); buf << FX_BSTRC("/") << PDF_NameEncode(key); if (pValue && pValue->GetObjNum()) { buf << " " << pValue->GetObjNum() << FX_BSTRC(" 0 R "); } else { buf << pValue; } } buf << FX_BSTRC(">>"); break; } case PDFOBJ_STREAM: { const CPDF_Stream* p = pObj->AsStream(); buf << p->GetDict() << FX_BSTRC("stream\r\n"); CPDF_StreamAcc acc; acc.LoadAllData(p, TRUE); buf.AppendBlock(acc.GetData(), acc.GetSize()); buf << FX_BSTRC("\r\nendstream"); break; } default: ASSERT(FALSE); break; } return buf; }