diff options
author | Dan Sinclair <dsinclair@chromium.org> | 2016-03-14 13:35:12 -0400 |
---|---|---|
committer | Dan Sinclair <dsinclair@chromium.org> | 2016-03-14 13:35:12 -0400 |
commit | 764ec513eecbebd12781bcc96ce81ed5e736ee92 (patch) | |
tree | 12763fde4be1f10ea1183d92185917b2b587e00b /core/src/fpdfapi/fpdf_edit | |
parent | 97da97662417085774f75c26e535c6fbe70266ae (diff) | |
download | pdfium-764ec513eecbebd12781bcc96ce81ed5e736ee92.tar.xz |
Move core/src/ up to core/.
This CL moves the core/src/ files up to core/ and fixes up the include guards,
includes and build files.
R=tsepez@chromium.org
Review URL: https://codereview.chromium.org/1800523005 .
Diffstat (limited to 'core/src/fpdfapi/fpdf_edit')
-rw-r--r-- | core/src/fpdfapi/fpdf_edit/editint.h | 74 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_edit/fpdf_edit_content.cpp | 162 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp | 2076 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_edit/fpdf_edit_doc.cpp | 1156 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_edit/fpdf_edit_image.cpp | 391 |
5 files changed, 0 insertions, 3859 deletions
diff --git a/core/src/fpdfapi/fpdf_edit/editint.h b/core/src/fpdfapi/fpdf_edit/editint.h deleted file mode 100644 index 411de0f0fb..0000000000 --- a/core/src/fpdfapi/fpdf_edit/editint.h +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright 2014 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#ifndef CORE_SRC_FPDFAPI_FPDF_EDIT_EDITINT_H_ -#define CORE_SRC_FPDFAPI_FPDF_EDIT_EDITINT_H_ - -#include <vector> - -#include "core/include/fxcrt/fx_basic.h" -#include "core/include/fxcrt/fx_stream.h" -#include "core/include/fxcrt/fx_system.h" - -class CPDF_Creator; -class CPDF_Object; - -class CPDF_ObjectStream { - public: - CPDF_ObjectStream(); - - FX_BOOL Start(); - - int32_t CompressIndirectObject(FX_DWORD dwObjNum, const CPDF_Object* pObj); - int32_t CompressIndirectObject(FX_DWORD dwObjNum, - const uint8_t* pBuffer, - FX_DWORD dwSize); - - FX_FILESIZE End(CPDF_Creator* pCreator); - - CFX_DWordArray m_ObjNumArray; - - CFX_ByteTextBuf m_Buffer; - FX_DWORD m_dwObjNum; - int32_t m_index; - - protected: - CFX_DWordArray m_OffsetArray; -}; -class CPDF_XRefStream { - public: - struct Index { - FX_DWORD objnum; - FX_DWORD count; - }; - - CPDF_XRefStream(); - - FX_BOOL Start(); - int32_t CompressIndirectObject(FX_DWORD dwObjNum, - const CPDF_Object* pObj, - CPDF_Creator* pCreator); - int32_t CompressIndirectObject(FX_DWORD dwObjNum, - const uint8_t* pBuffer, - FX_DWORD dwSize, - CPDF_Creator* pCreator); - FX_BOOL End(CPDF_Creator* pCreator, FX_BOOL bEOF = FALSE); - void AddObjectNumberToIndexArray(FX_DWORD objnum); - FX_BOOL EndXRefStream(CPDF_Creator* pCreator); - - std::vector<Index> m_IndexArray; - FX_FILESIZE m_PrevOffset; - FX_DWORD m_dwTempObjNum; - - protected: - int32_t EndObjectStream(CPDF_Creator* pCreator, FX_BOOL bEOF = TRUE); - FX_BOOL GenerateXRefStream(CPDF_Creator* pCreator, FX_BOOL bEOF); - size_t m_iSeg; - CPDF_ObjectStream m_ObjStream; - CFX_ByteTextBuf m_Buffer; -}; - -#endif // CORE_SRC_FPDFAPI_FPDF_EDIT_EDITINT_H_ diff --git a/core/src/fpdfapi/fpdf_edit/fpdf_edit_content.cpp b/core/src/fpdfapi/fpdf_edit/fpdf_edit_content.cpp deleted file mode 100644 index a8fddf6cfc..0000000000 --- a/core/src/fpdfapi/fpdf_edit/fpdf_edit_content.cpp +++ /dev/null @@ -1,162 +0,0 @@ -// Copyright 2014 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#include "core/include/fpdfapi/cpdf_array.h" -#include "core/include/fpdfapi/cpdf_dictionary.h" -#include "core/include/fpdfapi/cpdf_document.h" -#include "core/include/fpdfapi/fpdf_module.h" -#include "core/include/fpdfapi/fpdf_page.h" -#include "core/include/fpdfapi/fpdf_parser_decode.h" -#include "core/include/fpdfapi/fpdf_serial.h" -#include "core/src/fpdfapi/fpdf_page/pageint.h" - -CFX_ByteTextBuf& operator<<(CFX_ByteTextBuf& ar, CFX_Matrix& matrix) { - ar << matrix.a << " " << matrix.b << " " << matrix.c << " " << matrix.d << " " - << matrix.e << " " << matrix.f; - return ar; -} - -CPDF_PageContentGenerator::CPDF_PageContentGenerator(CPDF_Page* pPage) - : m_pPage(pPage), m_pDocument(m_pPage->m_pDocument) { - for (const auto& pObj : *pPage->GetPageObjectList()) - InsertPageObject(pObj.get()); -} - -FX_BOOL CPDF_PageContentGenerator::InsertPageObject( - CPDF_PageObject* pPageObject) { - return pPageObject && m_pageObjects.Add(pPageObject); -} - -void CPDF_PageContentGenerator::GenerateContent() { - CFX_ByteTextBuf buf; - CPDF_Dictionary* pPageDict = m_pPage->m_pFormDict; - for (int i = 0; i < m_pageObjects.GetSize(); ++i) { - CPDF_PageObject* pPageObj = m_pageObjects[i]; - if (!pPageObj || !pPageObj->IsImage()) { - continue; - } - ProcessImage(buf, pPageObj->AsImage()); - } - CPDF_Object* pContent = - pPageDict ? pPageDict->GetElementValue("Contents") : NULL; - if (pContent) { - pPageDict->RemoveAt("Contents"); - } - CPDF_Stream* pStream = new CPDF_Stream(NULL, 0, NULL); - pStream->SetData(buf.GetBuffer(), buf.GetLength(), FALSE, FALSE); - m_pDocument->AddIndirectObject(pStream); - pPageDict->SetAtReference("Contents", m_pDocument, pStream->GetObjNum()); -} -CFX_ByteString CPDF_PageContentGenerator::RealizeResource( - CPDF_Object* pResourceObj, - const FX_CHAR* szType) { - if (!m_pPage->m_pResources) { - m_pPage->m_pResources = new CPDF_Dictionary; - int objnum = m_pDocument->AddIndirectObject(m_pPage->m_pResources); - m_pPage->m_pFormDict->SetAtReference("Resources", m_pDocument, objnum); - } - CPDF_Dictionary* pResList = m_pPage->m_pResources->GetDictBy(szType); - if (!pResList) { - pResList = new CPDF_Dictionary; - m_pPage->m_pResources->SetAt(szType, pResList); - } - m_pDocument->AddIndirectObject(pResourceObj); - CFX_ByteString name; - int idnum = 1; - while (1) { - name.Format("FX%c%d", szType[0], idnum); - if (!pResList->KeyExist(name)) { - break; - } - idnum++; - } - pResList->AddReference(name, m_pDocument, pResourceObj->GetObjNum()); - return name; -} -void CPDF_PageContentGenerator::ProcessImage(CFX_ByteTextBuf& buf, - CPDF_ImageObject* pImageObj) { - if ((pImageObj->m_Matrix.a == 0 && pImageObj->m_Matrix.b == 0) || - (pImageObj->m_Matrix.c == 0 && pImageObj->m_Matrix.d == 0)) { - return; - } - buf << "q " << pImageObj->m_Matrix << " cm "; - if (!pImageObj->m_pImage->IsInline()) { - CPDF_Stream* pStream = pImageObj->m_pImage->GetStream(); - FX_DWORD dwSavedObjNum = pStream->GetObjNum(); - CFX_ByteString name = RealizeResource(pStream, "XObject"); - if (dwSavedObjNum == 0) { - if (pImageObj->m_pImage) - pImageObj->m_pImage->Release(); - pImageObj->m_pImage = m_pDocument->GetPageData()->GetImage(pStream); - } - buf << "/" << PDF_NameEncode(name) << " Do Q\n"; - } -} -void CPDF_PageContentGenerator::ProcessForm(CFX_ByteTextBuf& buf, - const uint8_t* data, - FX_DWORD size, - CFX_Matrix& matrix) { - if (!data || !size) { - return; - } - CPDF_Stream* pStream = new CPDF_Stream(NULL, 0, NULL); - CPDF_Dictionary* pFormDict = new CPDF_Dictionary; - pFormDict->SetAtName("Type", "XObject"); - pFormDict->SetAtName("Subtype", "Form"); - CFX_FloatRect bbox = m_pPage->GetPageBBox(); - matrix.TransformRect(bbox); - pFormDict->SetAtRect("BBox", bbox); - pStream->InitStream((uint8_t*)data, size, pFormDict); - buf << "q " << matrix << " cm "; - CFX_ByteString name = RealizeResource(pStream, "XObject"); - buf << "/" << PDF_NameEncode(name) << " Do Q\n"; -} -void CPDF_PageContentGenerator::TransformContent(CFX_Matrix& matrix) { - CPDF_Dictionary* pDict = m_pPage->m_pFormDict; - CPDF_Object* pContent = pDict ? pDict->GetElementValue("Contents") : NULL; - if (!pContent) - return; - - CFX_ByteTextBuf buf; - if (CPDF_Array* pArray = pContent->AsArray()) { - int iCount = pArray->GetCount(); - CPDF_StreamAcc** pContentArray = FX_Alloc(CPDF_StreamAcc*, iCount); - int size = 0; - int i = 0; - for (i = 0; i < iCount; ++i) { - pContent = pArray->GetElement(i); - CPDF_Stream* pStream = ToStream(pContent); - if (!pStream) - continue; - - CPDF_StreamAcc* pStreamAcc = new CPDF_StreamAcc(); - pStreamAcc->LoadAllData(pStream); - pContentArray[i] = pStreamAcc; - size += pContentArray[i]->GetSize() + 1; - } - int pos = 0; - uint8_t* pBuf = FX_Alloc(uint8_t, size); - for (i = 0; i < iCount; ++i) { - FXSYS_memcpy(pBuf + pos, pContentArray[i]->GetData(), - pContentArray[i]->GetSize()); - pos += pContentArray[i]->GetSize() + 1; - pBuf[pos - 1] = ' '; - delete pContentArray[i]; - } - ProcessForm(buf, pBuf, size, matrix); - FX_Free(pBuf); - FX_Free(pContentArray); - } else if (CPDF_Stream* pStream = pContent->AsStream()) { - CPDF_StreamAcc contentStream; - contentStream.LoadAllData(pStream); - ProcessForm(buf, contentStream.GetData(), contentStream.GetSize(), matrix); - } - CPDF_Stream* pStream = new CPDF_Stream(NULL, 0, NULL); - pStream->SetData(buf.GetBuffer(), buf.GetLength(), FALSE, FALSE); - m_pDocument->AddIndirectObject(pStream); - m_pPage->m_pFormDict->SetAtReference("Contents", m_pDocument, - pStream->GetObjNum()); -} diff --git a/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp b/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp deleted file mode 100644 index f3797c2532..0000000000 --- a/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp +++ /dev/null @@ -1,2076 +0,0 @@ -// Copyright 2014 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#include "core/src/fpdfapi/fpdf_edit/editint.h" - -#include <vector> - -#include "core/include/fpdfapi/cpdf_array.h" -#include "core/include/fpdfapi/cpdf_dictionary.h" -#include "core/include/fpdfapi/cpdf_document.h" -#include "core/include/fpdfapi/cpdf_parser.h" -#include "core/include/fpdfapi/cpdf_string.h" -#include "core/include/fpdfapi/cpdf_reference.h" -#include "core/include/fpdfapi/fpdf_parser_decode.h" -#include "core/include/fpdfapi/fpdf_serial.h" -#include "core/include/fpdfapi/ipdf_crypto_handler.h" -#include "core/include/fxcrt/fx_ext.h" -#include "core/src/fpdfapi/fpdf_parser/cpdf_standard_crypto_handler.h" -#include "core/src/fpdfapi/fpdf_parser/cpdf_standard_security_handler.h" -#include "third_party/base/stl_util.h" - -#define PDF_OBJECTSTREAM_MAXLENGTH (256 * 1024) -#define PDF_XREFSTREAM_MAXSIZE 10000 - -// TODO(ochang): Make helper for appending "objnum 0 R ". - -namespace { - -int32_t PDF_CreatorAppendObject(const CPDF_Object* pObj, - CFX_FileBufferArchive* pFile, - FX_FILESIZE& offset) { - int32_t len = 0; - if (!pObj) { - if (pFile->AppendString(" null") < 0) { - return -1; - } - offset += 5; - return 1; - } - switch (pObj->GetType()) { - case CPDF_Object::NULLOBJ: - if (pFile->AppendString(" null") < 0) { - return -1; - } - offset += 5; - break; - case CPDF_Object::BOOLEAN: - case CPDF_Object::NUMBER: - if (pFile->AppendString(" ") < 0) { - return -1; - } - if ((len = pFile->AppendString(pObj->GetString())) < 0) { - return -1; - } - offset += len + 1; - break; - case CPDF_Object::STRING: { - CFX_ByteString str = pObj->GetString(); - FX_BOOL bHex = pObj->AsString()->IsHex(); - if ((len = pFile->AppendString(PDF_EncodeString(str, bHex))) < 0) { - return -1; - } - offset += len; - break; - } - case CPDF_Object::NAME: { - if (pFile->AppendString("/") < 0) { - return -1; - } - CFX_ByteString str = pObj->GetString(); - if ((len = pFile->AppendString(PDF_NameEncode(str))) < 0) { - return -1; - } - offset += len + 1; - break; - } - case CPDF_Object::REFERENCE: { - if (pFile->AppendString(" ") < 0) - return -1; - if ((len = pFile->AppendDWord(pObj->AsReference()->GetRefObjNum())) < 0) - return -1; - if (pFile->AppendString(" 0 R ") < 0) - return -1; - offset += len + 6; - break; - } - case CPDF_Object::ARRAY: { - if (pFile->AppendString("[") < 0) { - return -1; - } - offset += 1; - const CPDF_Array* p = pObj->AsArray(); - for (FX_DWORD i = 0; i < p->GetCount(); i++) { - CPDF_Object* pElement = p->GetElement(i); - if (pElement->GetObjNum()) { - if (pFile->AppendString(" ") < 0) { - return -1; - } - if ((len = pFile->AppendDWord(pElement->GetObjNum())) < 0) { - return -1; - } - if (pFile->AppendString(" 0 R") < 0) { - return -1; - } - offset += len + 5; - } else { - if (PDF_CreatorAppendObject(pElement, pFile, offset) < 0) { - return -1; - } - } - } - if (pFile->AppendString("]") < 0) { - return -1; - } - offset += 1; - break; - } - case CPDF_Object::DICTIONARY: { - if (pFile->AppendString("<<") < 0) { - return -1; - } - offset += 2; - const CPDF_Dictionary* p = pObj->AsDictionary(); - for (const auto& it : *p) { - const CFX_ByteString& key = it.first; - CPDF_Object* pValue = it.second; - if (pFile->AppendString("/") < 0) { - return -1; - } - if ((len = pFile->AppendString(PDF_NameEncode(key))) < 0) { - return -1; - } - offset += len + 1; - if (pValue->GetObjNum()) { - if (pFile->AppendString(" ") < 0) { - return -1; - } - if ((len = pFile->AppendDWord(pValue->GetObjNum())) < 0) { - return -1; - } - if (pFile->AppendString(" 0 R") < 0) { - return -1; - } - offset += len + 5; - } else { - if (PDF_CreatorAppendObject(pValue, pFile, offset) < 0) { - return -1; - } - } - } - if (pFile->AppendString(">>") < 0) { - return -1; - } - offset += 2; - break; - } - case CPDF_Object::STREAM: { - const CPDF_Stream* p = pObj->AsStream(); - if (PDF_CreatorAppendObject(p->GetDict(), pFile, offset) < 0) { - return -1; - } - if (pFile->AppendString("stream\r\n") < 0) { - return -1; - } - offset += 8; - CPDF_StreamAcc acc; - acc.LoadAllData(p, TRUE); - if (pFile->AppendBlock(acc.GetData(), acc.GetSize()) < 0) { - return -1; - } - offset += acc.GetSize(); - if ((len = pFile->AppendString("\r\nendstream")) < 0) { - return -1; - } - offset += len; - break; - } - default: - ASSERT(FALSE); - break; - } - return 1; -} - -int32_t PDF_CreatorWriteTrailer(CPDF_Document* pDocument, - CFX_FileBufferArchive* pFile, - CPDF_Array* pIDArray, - FX_BOOL bCompress) { - FX_FILESIZE offset = 0; - int32_t len = 0; - FXSYS_assert(pDocument && pFile); - CPDF_Parser* pParser = (CPDF_Parser*)pDocument->GetParser(); - if (pParser) { - CPDF_Dictionary* p = pParser->GetTrailer(); - for (const auto& it : *p) { - const CFX_ByteString& key = it.first; - CPDF_Object* pValue = it.second; - if (key == "Encrypt" || key == "Size" || key == "Filter" || - key == "Index" || key == "Length" || key == "Prev" || key == "W" || - key == "XRefStm" || key == "Type" || key == "ID") { - continue; - } - if (bCompress && key == "DecodeParms") { - continue; - } - if (pFile->AppendString(("/")) < 0) { - return -1; - } - if ((len = pFile->AppendString(PDF_NameEncode(key))) < 0) { - return -1; - } - offset += len + 1; - if (pValue->GetObjNum()) { - if (pFile->AppendString(" ") < 0) { - return -1; - } - if ((len = pFile->AppendDWord(pValue->GetObjNum())) < 0) { - return -1; - } - if (pFile->AppendString(" 0 R ") < 0) { - return -1; - } - offset += len + 6; - } else { - if (PDF_CreatorAppendObject(pValue, pFile, offset) < 0) { - return -1; - } - } - } - if (pIDArray) { - if (pFile->AppendString(("/ID")) < 0) { - return -1; - } - offset += 3; - if (PDF_CreatorAppendObject(pIDArray, pFile, offset) < 0) { - return -1; - } - } - return offset; - } - if (pFile->AppendString("\r\n/Root ") < 0) { - return -1; - } - if ((len = pFile->AppendDWord(pDocument->GetRoot()->GetObjNum())) < 0) { - return -1; - } - if (pFile->AppendString(" 0 R\r\n") < 0) { - return -1; - } - offset += len + 14; - if (pDocument->GetInfo()) { - if (pFile->AppendString("/Info ") < 0) { - return -1; - } - if ((len = pFile->AppendDWord(pDocument->GetInfo()->GetObjNum())) < 0) { - return -1; - } - if (pFile->AppendString(" 0 R\r\n") < 0) { - return -1; - } - offset += len + 12; - } - if (pIDArray) { - if (pFile->AppendString(("/ID")) < 0) { - return -1; - } - offset += 3; - if (PDF_CreatorAppendObject(pIDArray, pFile, offset) < 0) { - return -1; - } - } - return offset; -} - -int32_t PDF_CreatorWriteEncrypt(const CPDF_Dictionary* pEncryptDict, - FX_DWORD dwObjNum, - CFX_FileBufferArchive* pFile) { - if (!pEncryptDict) { - return 0; - } - FXSYS_assert(pFile); - FX_FILESIZE offset = 0; - int32_t len = 0; - if (pFile->AppendString("/Encrypt") < 0) { - return -1; - } - offset += 8; - if (pFile->AppendString(" ") < 0) { - return -1; - } - if ((len = pFile->AppendDWord(dwObjNum)) < 0) { - return -1; - } - if (pFile->AppendString(" 0 R ") < 0) { - return -1; - } - offset += len + 6; - return offset; -} - -std::vector<uint8_t> PDF_GenerateFileID(FX_DWORD dwSeed1, FX_DWORD dwSeed2) { - std::vector<uint8_t> buffer(sizeof(FX_DWORD) * 4); - FX_DWORD* pBuffer = reinterpret_cast<FX_DWORD*>(buffer.data()); - void* pContext = FX_Random_MT_Start(dwSeed1); - for (int i = 0; i < 2; ++i) - *pBuffer++ = FX_Random_MT_Generate(pContext); - FX_Random_MT_Close(pContext); - pContext = FX_Random_MT_Start(dwSeed2); - for (int i = 0; i < 2; ++i) - *pBuffer++ = FX_Random_MT_Generate(pContext); - FX_Random_MT_Close(pContext); - return buffer; -} - -void AppendIndex0(CFX_ByteTextBuf& buffer, bool bFirstObject) { - buffer.AppendByte(0); - buffer.AppendByte(0); - buffer.AppendByte(0); - buffer.AppendByte(0); - buffer.AppendByte(0); - const uint8_t byte = bFirstObject ? 0xFF : 0; - buffer.AppendByte(byte); - buffer.AppendByte(byte); -} - -void AppendIndex1(CFX_ByteTextBuf& buffer, FX_FILESIZE offset) { - buffer.AppendByte(1); - buffer.AppendByte(FX_GETBYTEOFFSET24(offset)); - buffer.AppendByte(FX_GETBYTEOFFSET16(offset)); - buffer.AppendByte(FX_GETBYTEOFFSET8(offset)); - buffer.AppendByte(FX_GETBYTEOFFSET0(offset)); - buffer.AppendByte(0); - buffer.AppendByte(0); -} - -void AppendIndex2(CFX_ByteTextBuf& buffer, FX_DWORD objnum, int32_t index) { - buffer.AppendByte(2); - buffer.AppendByte(FX_GETBYTEOFFSET24(objnum)); - buffer.AppendByte(FX_GETBYTEOFFSET16(objnum)); - buffer.AppendByte(FX_GETBYTEOFFSET8(objnum)); - buffer.AppendByte(FX_GETBYTEOFFSET0(objnum)); - buffer.AppendByte(FX_GETBYTEOFFSET8(index)); - buffer.AppendByte(FX_GETBYTEOFFSET0(index)); -} - -bool IsXRefNeedEnd(CPDF_XRefStream* pXRef, FX_DWORD flag) { - if (!(flag & FPDFCREATE_INCREMENTAL)) - return false; - - FX_DWORD iCount = 0; - for (const auto& pair : pXRef->m_IndexArray) - iCount += pair.count; - - return iCount >= PDF_XREFSTREAM_MAXSIZE; -} - -int32_t OutputIndex(CFX_FileBufferArchive* pFile, FX_FILESIZE offset) { - if (sizeof(offset) > 4) { - if (FX_GETBYTEOFFSET32(offset)) { - if (pFile->AppendByte(FX_GETBYTEOFFSET56(offset)) < 0) - return -1; - if (pFile->AppendByte(FX_GETBYTEOFFSET48(offset)) < 0) - return -1; - if (pFile->AppendByte(FX_GETBYTEOFFSET40(offset)) < 0) - return -1; - if (pFile->AppendByte(FX_GETBYTEOFFSET32(offset)) < 0) - return -1; - } - } - if (pFile->AppendByte(FX_GETBYTEOFFSET24(offset)) < 0) - return -1; - if (pFile->AppendByte(FX_GETBYTEOFFSET16(offset)) < 0) - return -1; - if (pFile->AppendByte(FX_GETBYTEOFFSET8(offset)) < 0) - return -1; - if (pFile->AppendByte(FX_GETBYTEOFFSET0(offset)) < 0) - return -1; - if (pFile->AppendByte(0) < 0) - return -1; - return 0; -} - -class CPDF_FlateEncoder { - public: - CPDF_FlateEncoder(); - ~CPDF_FlateEncoder(); - FX_BOOL Initialize(CPDF_Stream* pStream, FX_BOOL bFlateEncode); - FX_BOOL Initialize(const uint8_t* pBuffer, - FX_DWORD size, - FX_BOOL bFlateEncode, - FX_BOOL bXRefStream = FALSE); - void CloneDict(); - uint8_t* m_pData; - FX_DWORD m_dwSize; - CPDF_Dictionary* m_pDict; - FX_BOOL m_bCloned; - FX_BOOL m_bNewData; - CPDF_StreamAcc m_Acc; -}; -CPDF_FlateEncoder::CPDF_FlateEncoder() { - m_pData = NULL; - m_dwSize = 0; - m_pDict = NULL; - m_bCloned = FALSE; - m_bNewData = FALSE; -} -void CPDF_FlateEncoder::CloneDict() { - if (!m_bCloned) { - m_pDict = ToDictionary(m_pDict->Clone()); - ASSERT(m_pDict); - m_bCloned = TRUE; - } -} -FX_BOOL CPDF_FlateEncoder::Initialize(CPDF_Stream* pStream, - FX_BOOL bFlateEncode) { - m_Acc.LoadAllData(pStream, TRUE); - if ((pStream && pStream->GetDict() && - pStream->GetDict()->KeyExist("Filter")) || - !bFlateEncode) { - if (pStream->GetDict()->KeyExist("Filter") && !bFlateEncode) { - CPDF_StreamAcc destAcc; - destAcc.LoadAllData(pStream); - m_dwSize = destAcc.GetSize(); - m_pData = (uint8_t*)destAcc.DetachData(); - m_pDict = ToDictionary(pStream->GetDict()->Clone()); - m_pDict->RemoveAt("Filter"); - m_bNewData = TRUE; - m_bCloned = TRUE; - } else { - m_pData = (uint8_t*)m_Acc.GetData(); - m_dwSize = m_Acc.GetSize(); - m_pDict = pStream->GetDict(); - } - return TRUE; - } - m_pData = NULL; - m_dwSize = 0; - m_bNewData = TRUE; - m_bCloned = TRUE; - ::FlateEncode(m_Acc.GetData(), m_Acc.GetSize(), m_pData, m_dwSize); - m_pDict = ToDictionary(pStream->GetDict()->Clone()); - m_pDict->SetAtInteger("Length", m_dwSize); - m_pDict->SetAtName("Filter", "FlateDecode"); - m_pDict->RemoveAt("DecodeParms"); - return TRUE; -} -FX_BOOL CPDF_FlateEncoder::Initialize(const uint8_t* pBuffer, - FX_DWORD size, - FX_BOOL bFlateEncode, - FX_BOOL bXRefStream) { - if (!bFlateEncode) { - m_pData = (uint8_t*)pBuffer; - m_dwSize = size; - return TRUE; - } - m_bNewData = TRUE; - if (bXRefStream) { - ::FlateEncode(pBuffer, size, 12, 1, 8, 7, m_pData, m_dwSize); - } else { - ::FlateEncode(pBuffer, size, m_pData, m_dwSize); - } - return TRUE; -} -CPDF_FlateEncoder::~CPDF_FlateEncoder() { - if (m_bCloned && m_pDict) { - m_pDict->Release(); - } - if (m_bNewData) { - FX_Free(m_pData); - } -} -class CPDF_Encryptor { - public: - CPDF_Encryptor(); - ~CPDF_Encryptor(); - FX_BOOL Initialize(IPDF_CryptoHandler* pHandler, - int objnum, - uint8_t* src_data, - FX_DWORD src_size); - uint8_t* m_pData; - FX_DWORD m_dwSize; - FX_BOOL m_bNewBuf; -}; -CPDF_Encryptor::CPDF_Encryptor() { - m_pData = NULL; - m_dwSize = 0; - m_bNewBuf = FALSE; -} -FX_BOOL CPDF_Encryptor::Initialize(IPDF_CryptoHandler* pHandler, - int objnum, - uint8_t* src_data, - FX_DWORD src_size) { - if (src_size == 0) { - return TRUE; - } - if (!pHandler) { - m_pData = (uint8_t*)src_data; - m_dwSize = src_size; - m_bNewBuf = FALSE; - return TRUE; - } - m_dwSize = pHandler->EncryptGetSize(objnum, 0, src_data, src_size); - m_pData = FX_Alloc(uint8_t, m_dwSize); - pHandler->EncryptContent(objnum, 0, src_data, src_size, m_pData, m_dwSize); - m_bNewBuf = TRUE; - return TRUE; -} -CPDF_Encryptor::~CPDF_Encryptor() { - if (m_bNewBuf) { - FX_Free(m_pData); - } -} - -} // namespace - -CPDF_ObjectStream::CPDF_ObjectStream() : m_dwObjNum(0), m_index(0) {} -FX_BOOL CPDF_ObjectStream::Start() { - m_ObjNumArray.RemoveAll(); - m_OffsetArray.RemoveAll(); - m_Buffer.Clear(); - m_dwObjNum = 0; - m_index = 0; - return TRUE; -} -int32_t CPDF_ObjectStream::CompressIndirectObject(FX_DWORD dwObjNum, - const CPDF_Object* pObj) { - m_ObjNumArray.Add(dwObjNum); - m_OffsetArray.Add(m_Buffer.GetLength()); - m_Buffer << pObj; - return 1; -} -int32_t CPDF_ObjectStream::CompressIndirectObject(FX_DWORD dwObjNum, - const uint8_t* pBuffer, - FX_DWORD dwSize) { - m_ObjNumArray.Add(dwObjNum); - m_OffsetArray.Add(m_Buffer.GetLength()); - m_Buffer.AppendBlock(pBuffer, dwSize); - return 1; -} -FX_FILESIZE CPDF_ObjectStream::End(CPDF_Creator* pCreator) { - FXSYS_assert(pCreator); - if (m_ObjNumArray.GetSize() == 0) { - return 0; - } - CFX_FileBufferArchive* pFile = &pCreator->m_File; - IPDF_CryptoHandler* pHandler = pCreator->m_pCryptoHandler; - FX_FILESIZE ObjOffset = pCreator->m_Offset; - if (!m_dwObjNum) { - m_dwObjNum = ++pCreator->m_dwLastObjNum; - } - CFX_ByteTextBuf tempBuffer; - int32_t iCount = m_ObjNumArray.GetSize(); - for (int32_t i = 0; i < iCount; i++) { - tempBuffer << m_ObjNumArray.ElementAt(i) << " " - << m_OffsetArray.ElementAt(i) << " "; - } - FX_FILESIZE& offset = pCreator->m_Offset; - int32_t len = pFile->AppendDWord(m_dwObjNum); - if (len < 0) { - return -1; - } - offset += len; - if ((len = pFile->AppendString(" 0 obj\r\n<</Type /ObjStm /N ")) < 0) { - return -1; - } - offset += len; - if ((len = pFile->AppendDWord((FX_DWORD)iCount)) < 0) { - return -1; - } - offset += len; - if (pFile->AppendString("/First ") < 0) { - return -1; - } - if ((len = pFile->AppendDWord((FX_DWORD)tempBuffer.GetLength())) < 0) { - return -1; - } - if (pFile->AppendString("/Length ") < 0) { - return -1; - } - offset += len + 15; - if (!pCreator->m_bCompress && !pHandler) { - if ((len = pFile->AppendDWord( - (FX_DWORD)(tempBuffer.GetLength() + m_Buffer.GetLength()))) < 0) { - return -1; - } - offset += len; - if ((len = pFile->AppendString(">>stream\r\n")) < 0) { - return -1; - } - if (pFile->AppendBlock(tempBuffer.GetBuffer(), tempBuffer.GetLength()) < - 0) { - return -1; - } - if (pFile->AppendBlock(m_Buffer.GetBuffer(), m_Buffer.GetLength()) < 0) { - return -1; - } - offset += len + tempBuffer.GetLength() + m_Buffer.GetLength(); - } else { - tempBuffer << m_Buffer; - CPDF_FlateEncoder encoder; - encoder.Initialize(tempBuffer.GetBuffer(), tempBuffer.GetLength(), - pCreator->m_bCompress); - CPDF_Encryptor encryptor; - encryptor.Initialize(pHandler, m_dwObjNum, encoder.m_pData, - encoder.m_dwSize); - if ((len = pFile->AppendDWord(encryptor.m_dwSize)) < 0) { - return -1; - } - offset += len; - if (pCreator->m_bCompress) { - if (pFile->AppendString("/Filter /FlateDecode") < 0) { - return -1; - } - offset += 20; - } - if ((len = pFile->AppendString(">>stream\r\n")) < 0) { - return -1; - } - if (pFile->AppendBlock(encryptor.m_pData, encryptor.m_dwSize) < 0) { - return -1; - } - offset += len + encryptor.m_dwSize; - } - if ((len = pFile->AppendString("\r\nendstream\r\nendobj\r\n")) < 0) { - return -1; - } - offset += len; - return ObjOffset; -} -CPDF_XRefStream::CPDF_XRefStream() - : m_PrevOffset(0), m_dwTempObjNum(0), m_iSeg(0) {} -FX_BOOL CPDF_XRefStream::Start() { - m_IndexArray.clear(); - m_Buffer.Clear(); - m_iSeg = 0; - return TRUE; -} -int32_t CPDF_XRefStream::CompressIndirectObject(FX_DWORD dwObjNum, - const CPDF_Object* pObj, - CPDF_Creator* pCreator) { - if (!pCreator) { - return 0; - } - m_ObjStream.CompressIndirectObject(dwObjNum, pObj); - if (m_ObjStream.m_ObjNumArray.GetSize() < pCreator->m_ObjectStreamSize && - m_ObjStream.m_Buffer.GetLength() < PDF_OBJECTSTREAM_MAXLENGTH) { - return 1; - } - return EndObjectStream(pCreator); -} -int32_t CPDF_XRefStream::CompressIndirectObject(FX_DWORD dwObjNum, - const uint8_t* pBuffer, - FX_DWORD dwSize, - CPDF_Creator* pCreator) { - if (!pCreator) { - return 0; - } - m_ObjStream.CompressIndirectObject(dwObjNum, pBuffer, dwSize); - if (m_ObjStream.m_ObjNumArray.GetSize() < pCreator->m_ObjectStreamSize && - m_ObjStream.m_Buffer.GetLength() < PDF_OBJECTSTREAM_MAXLENGTH) { - return 1; - } - return EndObjectStream(pCreator); -} - -int32_t CPDF_XRefStream::EndObjectStream(CPDF_Creator* pCreator, FX_BOOL bEOF) { - FX_FILESIZE objOffset = 0; - if (bEOF) { - objOffset = m_ObjStream.End(pCreator); - if (objOffset < 0) { - return -1; - } - } - FX_DWORD& dwObjStmNum = m_ObjStream.m_dwObjNum; - if (!dwObjStmNum) { - dwObjStmNum = ++pCreator->m_dwLastObjNum; - } - int32_t iSize = m_ObjStream.m_ObjNumArray.GetSize(); - size_t iSeg = m_IndexArray.size(); - if (!(pCreator->m_dwFlags & FPDFCREATE_INCREMENTAL)) { - if (m_dwTempObjNum == 0) { - AppendIndex0(m_Buffer, true); - m_dwTempObjNum++; - } - FX_DWORD end_num = m_IndexArray.back().objnum + m_IndexArray.back().count; - int index = 0; - for (; m_dwTempObjNum < end_num; m_dwTempObjNum++) { - FX_FILESIZE* offset = pCreator->m_ObjectOffset.GetPtrAt(m_dwTempObjNum); - if (offset) { - if (index >= iSize || - m_dwTempObjNum != m_ObjStream.m_ObjNumArray[index]) { - AppendIndex1(m_Buffer, *offset); - } else { - AppendIndex2(m_Buffer, dwObjStmNum, index++); - } - } else { - AppendIndex0(m_Buffer, false); - } - } - if (iSize > 0 && bEOF) { - pCreator->m_ObjectOffset.Add(dwObjStmNum, 1); - pCreator->m_ObjectOffset[dwObjStmNum] = objOffset; - } - m_iSeg = iSeg; - if (bEOF) { - m_ObjStream.Start(); - } - return 1; - } - for (auto it = m_IndexArray.begin() + m_iSeg; it != m_IndexArray.end(); - ++it) { - for (FX_DWORD m = it->objnum; m < it->objnum + it->count; ++m) { - if (m_ObjStream.m_index >= iSize || - m != m_ObjStream.m_ObjNumArray.ElementAt(it - m_IndexArray.begin())) { - AppendIndex1(m_Buffer, pCreator->m_ObjectOffset[m]); - } else { - AppendIndex2(m_Buffer, dwObjStmNum, m_ObjStream.m_index++); - } - } - } - if (iSize > 0 && bEOF) { - AppendIndex1(m_Buffer, objOffset); - m_IndexArray.push_back({dwObjStmNum, 1}); - iSeg += 1; - } - m_iSeg = iSeg; - if (bEOF) { - m_ObjStream.Start(); - } - return 1; -} -FX_BOOL CPDF_XRefStream::GenerateXRefStream(CPDF_Creator* pCreator, - FX_BOOL bEOF) { - FX_FILESIZE offset_tmp = pCreator->m_Offset; - FX_DWORD objnum = ++pCreator->m_dwLastObjNum; - CFX_FileBufferArchive* pFile = &pCreator->m_File; - FX_BOOL bIncremental = (pCreator->m_dwFlags & FPDFCREATE_INCREMENTAL) != 0; - if (bIncremental) { - AddObjectNumberToIndexArray(objnum); - } else { - for (; m_dwTempObjNum < pCreator->m_dwLastObjNum; m_dwTempObjNum++) { - FX_FILESIZE* offset = pCreator->m_ObjectOffset.GetPtrAt(m_dwTempObjNum); - if (offset) { - AppendIndex1(m_Buffer, *offset); - } else { - AppendIndex0(m_Buffer, false); - } - } - } - AppendIndex1(m_Buffer, offset_tmp); - FX_FILESIZE& offset = pCreator->m_Offset; - int32_t len = pFile->AppendDWord(objnum); - if (len < 0) { - return FALSE; - } - offset += len; - if ((len = pFile->AppendString(" 0 obj\r\n<</Type /XRef/W[1 4 2]/Index[")) < - 0) { - return FALSE; - } - offset += len; - if (!bIncremental) { - if ((len = pFile->AppendDWord(0)) < 0) { - return FALSE; - } - if ((len = pFile->AppendString(" ")) < 0) { - return FALSE; - } - offset += len + 1; - if ((len = pFile->AppendDWord(objnum + 1)) < 0) { - return FALSE; - } - offset += len; - } else { - for (const auto& pair : m_IndexArray) { - if ((len = pFile->AppendDWord(pair.objnum)) < 0) { - return FALSE; - } - if (pFile->AppendString(" ") < 0) { - return FALSE; - } - offset += len + 1; - if ((len = pFile->AppendDWord(pair.count)) < 0) { - return FALSE; - } - if (pFile->AppendString(" ") < 0) { - return FALSE; - } - offset += len + 1; - } - } - if (pFile->AppendString("]/Size ") < 0) { - return FALSE; - } - if ((len = pFile->AppendDWord(objnum + 1)) < 0) { - return FALSE; - } - offset += len + 7; - if (m_PrevOffset > 0) { - if (pFile->AppendString("/Prev ") < 0) { - return FALSE; - } - FX_CHAR offset_buf[20]; - FXSYS_memset(offset_buf, 0, sizeof(offset_buf)); - FXSYS_i64toa(m_PrevOffset, offset_buf, 10); - int32_t len = (int32_t)FXSYS_strlen(offset_buf); - if (pFile->AppendBlock(offset_buf, len) < 0) { - return FALSE; - } - offset += len + 6; - } - FX_BOOL bPredictor = TRUE; - CPDF_FlateEncoder encoder; - encoder.Initialize(m_Buffer.GetBuffer(), m_Buffer.GetLength(), - pCreator->m_bCompress, bPredictor); - if (pCreator->m_bCompress) { - if (pFile->AppendString("/Filter /FlateDecode") < 0) { - return FALSE; - } - offset += 20; - if (bPredictor) { - if ((len = pFile->AppendString( - "/DecodeParms<</Columns 7/Predictor 12>>")) < 0) { - return FALSE; - } - offset += len; - } - } - if (pFile->AppendString("/Length ") < 0) { - return FALSE; - } - if ((len = pFile->AppendDWord(encoder.m_dwSize)) < 0) { - return FALSE; - } - offset += len + 8; - if (bEOF) { - if ((len = PDF_CreatorWriteTrailer(pCreator->m_pDocument, pFile, - pCreator->m_pIDArray, - pCreator->m_bCompress)) < 0) { - return FALSE; - } - offset += len; - if (pCreator->m_pEncryptDict) { - FX_DWORD dwEncryptObjNum = pCreator->m_pEncryptDict->GetObjNum(); - if (dwEncryptObjNum == 0) { - dwEncryptObjNum = pCreator->m_dwEnryptObjNum; - } - if ((len = PDF_CreatorWriteEncrypt(pCreator->m_pEncryptDict, - dwEncryptObjNum, pFile)) < 0) { - return FALSE; - } - offset += len; - } - } - if ((len = pFile->AppendString(">>stream\r\n")) < 0) { - return FALSE; - } - offset += len; - if (pFile->AppendBlock(encoder.m_pData, encoder.m_dwSize) < 0) { - return FALSE; - } - if ((len = pFile->AppendString("\r\nendstream\r\nendobj\r\n")) < 0) { - return FALSE; - } - offset += encoder.m_dwSize + len; - m_PrevOffset = offset_tmp; - return TRUE; -} -FX_BOOL CPDF_XRefStream::End(CPDF_Creator* pCreator, FX_BOOL bEOF) { - if (EndObjectStream(pCreator, bEOF) < 0) { - return FALSE; - } - return GenerateXRefStream(pCreator, bEOF); -} -FX_BOOL CPDF_XRefStream::EndXRefStream(CPDF_Creator* pCreator) { - if (!(pCreator->m_dwFlags & FPDFCREATE_INCREMENTAL)) { - AppendIndex0(m_Buffer, true); - for (FX_DWORD i = 1; i < pCreator->m_dwLastObjNum + 1; i++) { - FX_FILESIZE* offset = pCreator->m_ObjectOffset.GetPtrAt(i); - if (offset) { - AppendIndex1(m_Buffer, *offset); - } else { - AppendIndex0(m_Buffer, false); - } - } - } else { - for (const auto& pair : m_IndexArray) { - for (FX_DWORD j = pair.objnum; j < pair.objnum + pair.count; ++j) - AppendIndex1(m_Buffer, pCreator->m_ObjectOffset[j]); - } - } - return GenerateXRefStream(pCreator, FALSE); -} -void CPDF_XRefStream::AddObjectNumberToIndexArray(FX_DWORD objnum) { - if (m_IndexArray.empty()) { - m_IndexArray.push_back({objnum, 1}); - return; - } - FX_DWORD next_objnum = m_IndexArray.back().objnum + m_IndexArray.back().count; - if (objnum == next_objnum) - m_IndexArray.back().count += 1; - else - m_IndexArray.push_back({objnum, 1}); -} -CPDF_Creator::CPDF_Creator(CPDF_Document* pDoc) { - m_pDocument = pDoc; - m_pParser = (CPDF_Parser*)pDoc->m_pParser; - m_bCompress = TRUE; - if (m_pParser) { - m_pEncryptDict = m_pParser->GetEncryptDict(); - m_pCryptoHandler = m_pParser->GetCryptoHandler(); - } else { - m_pEncryptDict = NULL; - m_pCryptoHandler = NULL; - } - m_bSecurityChanged = FALSE; - m_bStandardSecurity = FALSE; - m_pMetadata = NULL; - m_bEncryptCloned = FALSE; - m_bEncryptMetadata = FALSE; - m_Offset = 0; - m_iStage = -1; - m_dwFlags = 0; - m_Pos = NULL; - m_XrefStart = 0; - m_pXRefStream = NULL; - m_ObjectStreamSize = 200; - m_dwLastObjNum = m_pDocument->GetLastObjNum(); - m_pIDArray = NULL; - m_FileVersion = 0; - m_dwEnryptObjNum = 0; - m_bNewCrypto = FALSE; -} -CPDF_Creator::~CPDF_Creator() { - ResetStandardSecurity(); - if (m_bEncryptCloned && m_pEncryptDict) { - m_pEncryptDict->Release(); - m_pEncryptDict = NULL; - } - Clear(); -} - -int32_t CPDF_Creator::WriteIndirectObjectToStream(const CPDF_Object* pObj) { - if (!m_pXRefStream) - return 1; - - FX_DWORD objnum = pObj->GetObjNum(); - if (m_pParser && m_pParser->GetObjectGenNum(objnum) > 0) - return 1; - - if (pObj->IsNumber()) - return 1; - - CPDF_Dictionary* pDict = pObj->GetDict(); - if (pObj->IsStream()) { - if (pDict && pDict->GetStringBy("Type") == "XRef") - return 0; - return 1; - } - - if (pDict) { - if (pDict == m_pDocument->m_pRootDict || pDict == m_pEncryptDict) - return 1; - if (pDict->IsSignatureDict()) - return 1; - if (pDict->GetStringBy("Type") == "Page") - return 1; - } - - m_pXRefStream->AddObjectNumberToIndexArray(objnum); - if (m_pXRefStream->CompressIndirectObject(objnum, pObj, this) < 0) - return -1; - if (!IsXRefNeedEnd(m_pXRefStream, m_dwFlags)) - return 0; - if (!m_pXRefStream->End(this)) - return -1; - if (!m_pXRefStream->Start()) - return -1; - return 0; -} -int32_t CPDF_Creator::WriteIndirectObjectToStream(FX_DWORD objnum, - const uint8_t* pBuffer, - FX_DWORD dwSize) { - if (!m_pXRefStream) { - return 1; - } - m_pXRefStream->AddObjectNumberToIndexArray(objnum); - int32_t iRet = - m_pXRefStream->CompressIndirectObject(objnum, pBuffer, dwSize, this); - if (iRet < 1) { - return iRet; - } - if (!IsXRefNeedEnd(m_pXRefStream, m_dwFlags)) { - return 0; - } - if (!m_pXRefStream->End(this)) { - return -1; - } - if (!m_pXRefStream->Start()) { - return -1; - } - return 0; -} -int32_t CPDF_Creator::AppendObjectNumberToXRef(FX_DWORD objnum) { - if (!m_pXRefStream) { - return 1; - } - m_pXRefStream->AddObjectNumberToIndexArray(objnum); - if (!IsXRefNeedEnd(m_pXRefStream, m_dwFlags)) { - return 0; - } - if (!m_pXRefStream->End(this)) { - return -1; - } - if (!m_pXRefStream->Start()) { - return -1; - } - return 0; -} -int32_t CPDF_Creator::WriteStream(const CPDF_Object* pStream, - FX_DWORD objnum, - IPDF_CryptoHandler* pCrypto) { - CPDF_FlateEncoder encoder; - encoder.Initialize(const_cast<CPDF_Stream*>(pStream->AsStream()), - pStream == m_pMetadata ? FALSE : m_bCompress); - CPDF_Encryptor encryptor; - if (!encryptor.Initialize(pCrypto, objnum, encoder.m_pData, - encoder.m_dwSize)) { - return -1; - } - if ((FX_DWORD)encoder.m_pDict->GetIntegerBy("Length") != encryptor.m_dwSize) { - encoder.CloneDict(); - encoder.m_pDict->SetAtInteger("Length", encryptor.m_dwSize); - } - if (WriteDirectObj(objnum, encoder.m_pDict) < 0) { - return -1; - } - int len = m_File.AppendString("stream\r\n"); - if (len < 0) { - return -1; - } - m_Offset += len; - if (m_File.AppendBlock(encryptor.m_pData, encryptor.m_dwSize) < 0) { - return -1; - } - m_Offset += encryptor.m_dwSize; - if ((len = m_File.AppendString("\r\nendstream")) < 0) { - return -1; - } - m_Offset += len; - return 1; -} -int32_t CPDF_Creator::WriteIndirectObj(FX_DWORD objnum, - const CPDF_Object* pObj) { - int32_t len = m_File.AppendDWord(objnum); - if (len < 0) - return -1; - - m_Offset += len; - if ((len = m_File.AppendString(" 0 obj\r\n")) < 0) - return -1; - - m_Offset += len; - if (pObj->IsStream()) { - IPDF_CryptoHandler* pHandler = nullptr; - pHandler = - (pObj == m_pMetadata && !m_bEncryptMetadata) ? NULL : m_pCryptoHandler; - if (WriteStream(pObj, objnum, pHandler) < 0) - return -1; - } else { - if (WriteDirectObj(objnum, pObj) < 0) - return -1; - } - if ((len = m_File.AppendString("\r\nendobj\r\n")) < 0) - return -1; - - m_Offset += len; - if (AppendObjectNumberToXRef(objnum) < 0) - return -1; - return 0; -} -int32_t CPDF_Creator::WriteIndirectObj(const CPDF_Object* pObj) { - int32_t iRet = WriteIndirectObjectToStream(pObj); - if (iRet < 1) { - return iRet; - } - return WriteIndirectObj(pObj->GetObjNum(), pObj); -} -int32_t CPDF_Creator::WriteDirectObj(FX_DWORD objnum, - const CPDF_Object* pObj, - FX_BOOL bEncrypt) { - int32_t len = 0; - if (!pObj) { - if (m_File.AppendString(" null") < 0) { - return -1; - } - m_Offset += 5; - return 1; - } - switch (pObj->GetType()) { - case CPDF_Object::NULLOBJ: - if (m_File.AppendString(" null") < 0) { - return -1; - } - m_Offset += 5; - break; - case CPDF_Object::BOOLEAN: - case CPDF_Object::NUMBER: - if (m_File.AppendString(" ") < 0) { - return -1; - } - if ((len = m_File.AppendString(pObj->GetString())) < 0) { - return -1; - } - m_Offset += len + 1; - break; - case CPDF_Object::STRING: { - CFX_ByteString str = pObj->GetString(); - FX_BOOL bHex = pObj->AsString()->IsHex(); - if (!m_pCryptoHandler || !bEncrypt) { - CFX_ByteString content = PDF_EncodeString(str, bHex); - if ((len = m_File.AppendString(content)) < 0) { - return -1; - } - m_Offset += len; - break; - } - CPDF_Encryptor encryptor; - encryptor.Initialize(m_pCryptoHandler, objnum, (uint8_t*)str.c_str(), - str.GetLength()); - CFX_ByteString content = PDF_EncodeString( - CFX_ByteString((const FX_CHAR*)encryptor.m_pData, encryptor.m_dwSize), - bHex); - if ((len = m_File.AppendString(content)) < 0) { - return -1; - } - m_Offset += len; - break; - } - case CPDF_Object::STREAM: { - CPDF_FlateEncoder encoder; - encoder.Initialize(const_cast<CPDF_Stream*>(pObj->AsStream()), - m_bCompress); - CPDF_Encryptor encryptor; - IPDF_CryptoHandler* pHandler = m_pCryptoHandler; - encryptor.Initialize(pHandler, objnum, encoder.m_pData, encoder.m_dwSize); - if ((FX_DWORD)encoder.m_pDict->GetIntegerBy("Length") != - encryptor.m_dwSize) { - encoder.CloneDict(); - encoder.m_pDict->SetAtInteger("Length", encryptor.m_dwSize); - } - if (WriteDirectObj(objnum, encoder.m_pDict) < 0) { - return -1; - } - if ((len = m_File.AppendString("stream\r\n")) < 0) { - return -1; - } - m_Offset += len; - if (m_File.AppendBlock(encryptor.m_pData, encryptor.m_dwSize) < 0) { - return -1; - } - m_Offset += encryptor.m_dwSize; - if ((len = m_File.AppendString("\r\nendstream")) < 0) { - return -1; - } - m_Offset += len; - break; - } - case CPDF_Object::NAME: { - if (m_File.AppendString("/") < 0) { - return -1; - } - CFX_ByteString str = pObj->GetString(); - if ((len = m_File.AppendString(PDF_NameEncode(str))) < 0) { - return -1; - } - m_Offset += len + 1; - break; - } - case CPDF_Object::REFERENCE: { - if (m_File.AppendString(" ") < 0) - return -1; - if ((len = m_File.AppendDWord(pObj->AsReference()->GetRefObjNum())) < 0) - return -1; - if (m_File.AppendString(" 0 R") < 0) - return -1; - m_Offset += len + 5; - break; - } - case CPDF_Object::ARRAY: { - if (m_File.AppendString("[") < 0) { - return -1; - } - m_Offset += 1; - const CPDF_Array* p = pObj->AsArray(); - for (FX_DWORD i = 0; i < p->GetCount(); i++) { - CPDF_Object* pElement = p->GetElement(i); - if (pElement->GetObjNum()) { - if (m_File.AppendString(" ") < 0) { - return -1; - } - if ((len = m_File.AppendDWord(pElement->GetObjNum())) < 0) { - return -1; - } - if (m_File.AppendString(" 0 R") < 0) { - return -1; - } - m_Offset += len + 5; - } else { - if (WriteDirectObj(objnum, pElement) < 0) { - return -1; - } - } - } - if (m_File.AppendString("]") < 0) { - return -1; - } - m_Offset += 1; - break; - } - case CPDF_Object::DICTIONARY: { - if (!m_pCryptoHandler || pObj == m_pEncryptDict) { - return PDF_CreatorAppendObject(pObj, &m_File, m_Offset); - } - if (m_File.AppendString("<<") < 0) { - return -1; - } - m_Offset += 2; - const CPDF_Dictionary* p = pObj->AsDictionary(); - bool bSignDict = p->IsSignatureDict(); - for (const auto& it : *p) { - FX_BOOL bSignValue = FALSE; - const CFX_ByteString& key = it.first; - CPDF_Object* pValue = it.second; - if (m_File.AppendString("/") < 0) { - return -1; - } - if ((len = m_File.AppendString(PDF_NameEncode(key))) < 0) { - return -1; - } - m_Offset += len + 1; - if (bSignDict && key == "Contents") { - bSignValue = TRUE; - } - if (pValue->GetObjNum()) { - if (m_File.AppendString(" ") < 0) { - return -1; - } - if ((len = m_File.AppendDWord(pValue->GetObjNum())) < 0) { - return -1; - } - if (m_File.AppendString(" 0 R ") < 0) { - return -1; - } - m_Offset += len + 6; - } else { - if (WriteDirectObj(objnum, pValue, !bSignValue) < 0) { - return -1; - } - } - } - if (m_File.AppendString(">>") < 0) { - return -1; - } - m_Offset += 2; - break; - } - } - return 1; -} -int32_t CPDF_Creator::WriteOldIndirectObject(FX_DWORD objnum) { - if (m_pParser->IsObjectFreeOrNull(objnum)) - return 0; - - m_ObjectOffset[objnum] = m_Offset; - FX_BOOL bExistInMap = - pdfium::ContainsKey(m_pDocument->m_IndirectObjs, objnum); - const uint8_t object_type = m_pParser->GetObjectType(objnum); - bool bObjStm = (object_type == 2) && m_pEncryptDict && !m_pXRefStream; - if (m_pParser->IsVersionUpdated() || m_bSecurityChanged || bExistInMap || - bObjStm) { - CPDF_Object* pObj = m_pDocument->GetIndirectObject(objnum); - if (!pObj) { - m_ObjectOffset[objnum] = 0; - return 0; - } - if (WriteIndirectObj(pObj)) { - return -1; - } - if (!bExistInMap) { - m_pDocument->ReleaseIndirectObject(objnum); - } - } else { - uint8_t* pBuffer; - FX_DWORD size; - m_pParser->GetIndirectBinary(objnum, pBuffer, size); - if (!pBuffer) { - return 0; - } - if (object_type == 2) { - if (m_pXRefStream) { - if (WriteIndirectObjectToStream(objnum, pBuffer, size) < 0) { - FX_Free(pBuffer); - return -1; - } - } else { - int32_t len = m_File.AppendDWord(objnum); - if (len < 0) { - return -1; - } - if (m_File.AppendString(" 0 obj ") < 0) { - return -1; - } - m_Offset += len + 7; - if (m_File.AppendBlock(pBuffer, size) < 0) { - return -1; - } - m_Offset += size; - if (m_File.AppendString("\r\nendobj\r\n") < 0) { - return -1; - } - m_Offset += 10; - } - } else { - if (m_File.AppendBlock(pBuffer, size) < 0) { - return -1; - } - m_Offset += size; - if (AppendObjectNumberToXRef(objnum) < 0) { - return -1; - } - } - FX_Free(pBuffer); - } - return 1; -} -int32_t CPDF_Creator::WriteOldObjs(IFX_Pause* pPause) { - FX_DWORD nLastObjNum = m_pParser->GetLastObjNum(); - if (!m_pParser->IsValidObjectNumber(nLastObjNum)) - return 0; - - FX_DWORD objnum = (FX_DWORD)(uintptr_t)m_Pos; - for (; objnum <= nLastObjNum; ++objnum) { - int32_t iRet = WriteOldIndirectObject(objnum); - if (iRet < 0) - return iRet; - - if (!iRet) - continue; - - if (pPause && pPause->NeedToPauseNow()) { - m_Pos = (void*)(uintptr_t)(objnum + 1); - return 1; - } - } - return 0; -} -int32_t CPDF_Creator::WriteNewObjs(FX_BOOL bIncremental, IFX_Pause* pPause) { - int32_t iCount = m_NewObjNumArray.GetSize(); - int32_t index = (int32_t)(uintptr_t)m_Pos; - while (index < iCount) { - FX_DWORD objnum = m_NewObjNumArray.ElementAt(index); - auto it = m_pDocument->m_IndirectObjs.find(objnum); - if (it == m_pDocument->m_IndirectObjs.end()) { - ++index; - continue; - } - m_ObjectOffset[objnum] = m_Offset; - if (WriteIndirectObj(it->second)) { - return -1; - } - index++; - if (pPause && pPause->NeedToPauseNow()) { - m_Pos = (FX_POSITION)(uintptr_t)index; - return 1; - } - } - return 0; -} -void CPDF_Creator::InitOldObjNumOffsets() { - if (!m_pParser) { - return; - } - FX_DWORD j = 0; - FX_DWORD dwStart = 0; - FX_DWORD dwEnd = m_pParser->GetLastObjNum(); - while (dwStart <= dwEnd) { - while (dwStart <= dwEnd && m_pParser->IsObjectFreeOrNull(dwStart)) - dwStart++; - - if (dwStart > dwEnd) - break; - - j = dwStart; - while (j <= dwEnd && !m_pParser->IsObjectFreeOrNull(j)) - j++; - - m_ObjectOffset.Add(dwStart, j - dwStart); - dwStart = j; - } -} -void CPDF_Creator::InitNewObjNumOffsets() { - FX_BOOL bIncremental = (m_dwFlags & FPDFCREATE_INCREMENTAL) != 0; - FX_BOOL bNoOriginal = (m_dwFlags & FPDFCREATE_NO_ORIGINAL) != 0; - for (const auto& pair : m_pDocument->m_IndirectObjs) { - const FX_DWORD objnum = pair.first; - const CPDF_Object* pObj = pair.second; - if (pObj->GetObjNum() == -1) - continue; - if (bIncremental) { - if (!pObj->IsModified()) - continue; - } else if (m_pParser && m_pParser->IsValidObjectNumber(objnum) && - m_pParser->GetObjectType(objnum)) { - continue; - } - AppendNewObjNum(objnum); - } - - int32_t iCount = m_NewObjNumArray.GetSize(); - if (iCount == 0) { - return; - } - int32_t i = 0; - FX_DWORD dwStartObjNum = 0; - FX_BOOL bCrossRefValid = m_pParser && m_pParser->GetLastXRefOffset() > 0; - while (i < iCount) { - dwStartObjNum = m_NewObjNumArray.ElementAt(i); - if ((bIncremental && (bNoOriginal || bCrossRefValid)) || - !m_ObjectOffset.GetPtrAt(dwStartObjNum)) { - break; - } - i++; - } - if (i >= iCount) { - return; - } - FX_DWORD dwLastObjNum = dwStartObjNum; - i++; - FX_BOOL bNewStart = FALSE; - for (; i < iCount; i++) { - FX_DWORD dwCurObjNum = m_NewObjNumArray.ElementAt(i); - bool bExist = m_pParser && m_pParser->IsValidObjectNumber(dwCurObjNum) && - m_ObjectOffset.GetPtrAt(dwCurObjNum); - if (bExist || dwCurObjNum - dwLastObjNum > 1) { - if (!bNewStart) - m_ObjectOffset.Add(dwStartObjNum, dwLastObjNum - dwStartObjNum + 1); - dwStartObjNum = dwCurObjNum; - } - if (bNewStart) { - dwStartObjNum = dwCurObjNum; - } - bNewStart = bExist; - dwLastObjNum = dwCurObjNum; - } - m_ObjectOffset.Add(dwStartObjNum, dwLastObjNum - dwStartObjNum + 1); -} -void CPDF_Creator::AppendNewObjNum(FX_DWORD objbum) { - int32_t iStart = 0, iFind = 0; - int32_t iEnd = m_NewObjNumArray.GetUpperBound(); - while (iStart <= iEnd) { - int32_t iMid = (iStart + iEnd) / 2; - FX_DWORD dwMid = m_NewObjNumArray.ElementAt(iMid); - if (objbum < dwMid) { - iEnd = iMid - 1; - } else { - if (iMid == iEnd) { - iFind = iMid + 1; - break; - } - FX_DWORD dwNext = m_NewObjNumArray.ElementAt(iMid + 1); - if (objbum < dwNext) { - iFind = iMid + 1; - break; - } - iStart = iMid + 1; - } - } - m_NewObjNumArray.InsertAt(iFind, objbum); -} -int32_t CPDF_Creator::WriteDoc_Stage1(IFX_Pause* pPause) { - FXSYS_assert(m_iStage > -1 || m_iStage < 20); - if (m_iStage == 0) { - if (!m_pParser) { - m_dwFlags &= ~FPDFCREATE_INCREMENTAL; - } - if (m_bSecurityChanged && (m_dwFlags & FPDFCREATE_NO_ORIGINAL) == 0) { - m_dwFlags &= ~FPDFCREATE_INCREMENTAL; - } - CPDF_Dictionary* pDict = m_pDocument->GetRoot(); - m_pMetadata = pDict ? pDict->GetElementValue("Metadata") : NULL; - if (m_dwFlags & FPDFCREATE_OBJECTSTREAM) { - m_pXRefStream = new CPDF_XRefStream; - m_pXRefStream->Start(); - if ((m_dwFlags & FPDFCREATE_INCREMENTAL) != 0 && m_pParser) { - FX_FILESIZE prev = m_pParser->GetLastXRefOffset(); - m_pXRefStream->m_PrevOffset = prev; - } - } - m_iStage = 10; - } - if (m_iStage == 10) { - if ((m_dwFlags & FPDFCREATE_INCREMENTAL) == 0) { - if (m_File.AppendString("%PDF-1.") < 0) { - return -1; - } - m_Offset += 7; - int32_t version = 7; - if (m_FileVersion) { - version = m_FileVersion; - } else if (m_pParser) { - version = m_pParser->GetFileVersion(); - } - int32_t len = m_File.AppendDWord(version % 10); - if (len < 0) { - return -1; - } - m_Offset += len; - if ((len = m_File.AppendString("\r\n%\xA1\xB3\xC5\xD7\r\n")) < 0) { - return -1; - } - m_Offset += len; - InitOldObjNumOffsets(); - m_iStage = 20; - } else { - IFX_FileRead* pSrcFile = m_pParser->GetFileAccess(); - m_Offset = pSrcFile->GetSize(); - m_Pos = (void*)(uintptr_t)m_Offset; - m_iStage = 15; - } - } - if (m_iStage == 15) { - if ((m_dwFlags & FPDFCREATE_NO_ORIGINAL) == 0 && m_Pos) { - IFX_FileRead* pSrcFile = m_pParser->GetFileAccess(); - uint8_t buffer[4096]; - FX_DWORD src_size = (FX_DWORD)(uintptr_t)m_Pos; - while (src_size) { - FX_DWORD block_size = src_size > 4096 ? 4096 : src_size; - if (!pSrcFile->ReadBlock(buffer, m_Offset - src_size, block_size)) { - return -1; - } - if (m_File.AppendBlock(buffer, block_size) < 0) { - return -1; - } - src_size -= block_size; - if (pPause && pPause->NeedToPauseNow()) { - m_Pos = (void*)(uintptr_t)src_size; - return 1; - } - } - } - if ((m_dwFlags & FPDFCREATE_NO_ORIGINAL) == 0 && - m_pParser->GetLastXRefOffset() == 0) { - InitOldObjNumOffsets(); - FX_DWORD dwEnd = m_pParser->GetLastObjNum(); - bool bObjStm = (m_dwFlags & FPDFCREATE_OBJECTSTREAM) != 0; - for (FX_DWORD objnum = 0; objnum <= dwEnd; objnum++) { - if (m_pParser->IsObjectFreeOrNull(objnum)) - continue; - - m_ObjectOffset[objnum] = m_pParser->GetObjectPositionOrZero(objnum); - if (bObjStm) { - m_pXRefStream->AddObjectNumberToIndexArray(objnum); - } - } - if (bObjStm) { - m_pXRefStream->EndXRefStream(this); - m_pXRefStream->Start(); - } - } - m_iStage = 20; - } - InitNewObjNumOffsets(); - return m_iStage; -} -int32_t CPDF_Creator::WriteDoc_Stage2(IFX_Pause* pPause) { - FXSYS_assert(m_iStage >= 20 || m_iStage < 30); - if (m_iStage == 20) { - if ((m_dwFlags & FPDFCREATE_INCREMENTAL) == 0 && m_pParser) { - m_Pos = (void*)(uintptr_t)0; - m_iStage = 21; - } else { - m_iStage = 25; - } - } - if (m_iStage == 21) { - int32_t iRet = WriteOldObjs(pPause); - if (iRet) { - return iRet; - } - m_iStage = 25; - } - if (m_iStage == 25) { - m_Pos = (void*)(uintptr_t)0; - m_iStage = 26; - } - if (m_iStage == 26) { - int32_t iRet = - WriteNewObjs((m_dwFlags & FPDFCREATE_INCREMENTAL) != 0, pPause); - if (iRet) { - return iRet; - } - m_iStage = 27; - } - if (m_iStage == 27) { - if (NULL != m_pEncryptDict && 0 == m_pEncryptDict->GetObjNum()) { - m_dwLastObjNum += 1; - FX_FILESIZE saveOffset = m_Offset; - if (WriteIndirectObj(m_dwLastObjNum, m_pEncryptDict) < 0) { - return -1; - } - m_ObjectOffset.Add(m_dwLastObjNum, 1); - m_ObjectOffset[m_dwLastObjNum] = saveOffset; - m_dwEnryptObjNum = m_dwLastObjNum; - if (m_dwFlags & FPDFCREATE_INCREMENTAL) { - m_NewObjNumArray.Add(m_dwLastObjNum); - } - } - m_iStage = 80; - } - return m_iStage; -} -int32_t CPDF_Creator::WriteDoc_Stage3(IFX_Pause* pPause) { - FXSYS_assert(m_iStage >= 80 || m_iStage < 90); - FX_DWORD dwLastObjNum = m_dwLastObjNum; - if (m_iStage == 80) { - m_XrefStart = m_Offset; - if (m_dwFlags & FPDFCREATE_OBJECTSTREAM) { - m_pXRefStream->End(this, TRUE); - m_XrefStart = m_pXRefStream->m_PrevOffset; - m_iStage = 90; - } else if ((m_dwFlags & FPDFCREATE_INCREMENTAL) == 0 || - !m_pParser->IsXRefStream()) { - if ((m_dwFlags & FPDFCREATE_INCREMENTAL) == 0 || - m_pParser->GetLastXRefOffset() == 0) { - CFX_ByteString str; - str = m_ObjectOffset.GetPtrAt(1) - ? "xref\r\n" - : "xref\r\n0 1\r\n0000000000 65535 f\r\n"; - if (m_File.AppendString(str) < 0) { - return -1; - } - m_Pos = (void*)(uintptr_t)1; - m_iStage = 81; - } else { - if (m_File.AppendString("xref\r\n") < 0) { - return -1; - } - m_Pos = (void*)(uintptr_t)0; - m_iStage = 82; - } - } else { - m_iStage = 90; - } - } - if (m_iStage == 81) { - CFX_ByteString str; - FX_DWORD i = (FX_DWORD)(uintptr_t)m_Pos, j; - while (i <= dwLastObjNum) { - while (i <= dwLastObjNum && !m_ObjectOffset.GetPtrAt(i)) { - i++; - } - if (i > dwLastObjNum) { - break; - } - j = i; - while (j <= dwLastObjNum && m_ObjectOffset.GetPtrAt(j)) { - j++; - } - if (i == 1) { - str.Format("0 %d\r\n0000000000 65535 f\r\n", j); - } else { - str.Format("%d %d\r\n", i, j - i); - } - if (m_File.AppendBlock(str.c_str(), str.GetLength()) < 0) { - return -1; - } - while (i < j) { - str.Format("%010d 00000 n\r\n", m_ObjectOffset[i++]); - if (m_File.AppendBlock(str.c_str(), str.GetLength()) < 0) { - return -1; - } - } - if (i > dwLastObjNum) { - break; - } - if (pPause && pPause->NeedToPauseNow()) { - m_Pos = (void*)(uintptr_t)i; - return 1; - } - } - m_iStage = 90; - } - if (m_iStage == 82) { - CFX_ByteString str; - int32_t iCount = m_NewObjNumArray.GetSize(); - int32_t i = (int32_t)(uintptr_t)m_Pos; - while (i < iCount) { - int32_t j = i; - FX_DWORD objnum = m_NewObjNumArray.ElementAt(i); - while (j < iCount) { - if (++j == iCount) { - break; - } - FX_DWORD dwCurrent = m_NewObjNumArray.ElementAt(j); - if (dwCurrent - objnum > 1) { - break; - } - objnum = dwCurrent; - } - objnum = m_NewObjNumArray.ElementAt(i); - if (objnum == 1) { - str.Format("0 %d\r\n0000000000 65535 f\r\n", j - i + 1); - } else { - str.Format("%d %d\r\n", objnum, j - i); - } - if (m_File.AppendBlock(str.c_str(), str.GetLength()) < 0) { - return -1; - } - while (i < j) { - objnum = m_NewObjNumArray.ElementAt(i++); - str.Format("%010d 00000 n\r\n", m_ObjectOffset[objnum]); - if (m_File.AppendBlock(str.c_str(), str.GetLength()) < 0) { - return -1; - } - } - if (pPause && (i % 100) == 0 && pPause->NeedToPauseNow()) { - m_Pos = (void*)(uintptr_t)i; - return 1; - } - } - m_iStage = 90; - } - return m_iStage; -} - -int32_t CPDF_Creator::WriteDoc_Stage4(IFX_Pause* pPause) { - FXSYS_assert(m_iStage >= 90); - if ((m_dwFlags & FPDFCREATE_OBJECTSTREAM) == 0) { - FX_BOOL bXRefStream = - (m_dwFlags & FPDFCREATE_INCREMENTAL) != 0 && m_pParser->IsXRefStream(); - if (!bXRefStream) { - if (m_File.AppendString("trailer\r\n<<") < 0) { - return -1; - } - } else { - if (m_File.AppendDWord(m_pDocument->m_LastObjNum + 1) < 0) { - return -1; - } - if (m_File.AppendString(" 0 obj <<") < 0) { - return -1; - } - } - if (m_pParser) { - CPDF_Dictionary* p = m_pParser->GetTrailer(); - for (const auto& it : *p) { - const CFX_ByteString& key = it.first; - CPDF_Object* pValue = it.second; - // TODO(ochang): Consolidate with similar check in - // PDF_CreatorWriteTrailer. - if (key == "Encrypt" || key == "Size" || key == "Filter" || - key == "Index" || key == "Length" || key == "Prev" || key == "W" || - key == "XRefStm" || key == "ID") { - continue; - } - if (m_File.AppendString(("/")) < 0) { - return -1; - } - if (m_File.AppendString(PDF_NameEncode(key)) < 0) { - return -1; - } - if (pValue->GetObjNum()) { - if (m_File.AppendString(" ") < 0) { - return -1; - } - if (m_File.AppendDWord(pValue->GetObjNum()) < 0) { - return -1; - } - if (m_File.AppendString(" 0 R ") < 0) { - return -1; - } - } else { - FX_FILESIZE offset = 0; - if (PDF_CreatorAppendObject(pValue, &m_File, offset) < 0) { - return -1; - } - } - } - } else { - if (m_File.AppendString("\r\n/Root ") < 0) { - return -1; - } - if (m_File.AppendDWord(m_pDocument->m_pRootDict->GetObjNum()) < 0) { - return -1; - } - if (m_File.AppendString(" 0 R\r\n") < 0) { - return -1; - } - if (m_pDocument->m_pInfoDict) { - if (m_File.AppendString("/Info ") < 0) { - return -1; - } - if (m_File.AppendDWord(m_pDocument->m_pInfoDict->GetObjNum()) < 0) { - return -1; - } - if (m_File.AppendString(" 0 R\r\n") < 0) { - return -1; - } - } - } - if (m_pEncryptDict) { - if (m_File.AppendString("/Encrypt") < 0) { - return -1; - } - FX_DWORD dwObjNum = m_pEncryptDict->GetObjNum(); - if (dwObjNum == 0) { - dwObjNum = m_pDocument->GetLastObjNum() + 1; - } - if (m_File.AppendString(" ") < 0) { - return -1; - } - if (m_File.AppendDWord(dwObjNum) < 0) { - return -1; - } - if (m_File.AppendString(" 0 R ") < 0) { - return -1; - } - } - if (m_File.AppendString("/Size ") < 0) { - return -1; - } - if (m_File.AppendDWord(m_dwLastObjNum + (bXRefStream ? 2 : 1)) < 0) { - return -1; - } - if ((m_dwFlags & FPDFCREATE_INCREMENTAL) != 0) { - FX_FILESIZE prev = m_pParser->GetLastXRefOffset(); - if (prev) { - if (m_File.AppendString("/Prev ") < 0) { - return -1; - } - FX_CHAR offset_buf[20]; - FXSYS_memset(offset_buf, 0, sizeof(offset_buf)); - FXSYS_i64toa(prev, offset_buf, 10); - if (m_File.AppendBlock(offset_buf, FXSYS_strlen(offset_buf)) < 0) { - return -1; - } - } - } - if (m_pIDArray) { - if (m_File.AppendString(("/ID")) < 0) { - return -1; - } - FX_FILESIZE offset = 0; - if (PDF_CreatorAppendObject(m_pIDArray, &m_File, offset) < 0) { - return -1; - } - } - if (!bXRefStream) { - if (m_File.AppendString(">>") < 0) { - return -1; - } - } else { - if (m_File.AppendString("/W[0 4 1]/Index[") < 0) { - return -1; - } - if ((m_dwFlags & FPDFCREATE_INCREMENTAL) != 0 && m_pParser && - m_pParser->GetLastXRefOffset() == 0) { - FX_DWORD i = 0; - for (i = 0; i < m_dwLastObjNum; i++) { - if (!m_ObjectOffset.GetPtrAt(i)) { - continue; - } - if (m_File.AppendDWord(i) < 0) { - return -1; - } - if (m_File.AppendString(" 1 ") < 0) { - return -1; - } - } - if (m_File.AppendString("]/Length ") < 0) { - return -1; - } - if (m_File.AppendDWord(m_dwLastObjNum * 5) < 0) { - return -1; - } - if (m_File.AppendString(">>stream\r\n") < 0) { - return -1; - } - for (i = 0; i < m_dwLastObjNum; i++) { - FX_FILESIZE* offset = m_ObjectOffset.GetPtrAt(i); - if (!offset) { - continue; - } - OutputIndex(&m_File, *offset); - } - } else { - int count = m_NewObjNumArray.GetSize(); - int32_t i = 0; - for (i = 0; i < count; i++) { - FX_DWORD objnum = m_NewObjNumArray.ElementAt(i); - if (m_File.AppendDWord(objnum) < 0) { - return -1; - } - if (m_File.AppendString(" 1 ") < 0) { - return -1; - } - } - if (m_File.AppendString("]/Length ") < 0) { - return -1; - } - if (m_File.AppendDWord(count * 5) < 0) { - return -1; - } - if (m_File.AppendString(">>stream\r\n") < 0) { - return -1; - } - for (i = 0; i < count; i++) { - FX_DWORD objnum = m_NewObjNumArray.ElementAt(i); - FX_FILESIZE offset = m_ObjectOffset[objnum]; - OutputIndex(&m_File, offset); - } - } - if (m_File.AppendString("\r\nendstream") < 0) { - return -1; - } - } - } - if (m_File.AppendString("\r\nstartxref\r\n") < 0) { - return -1; - } - FX_CHAR offset_buf[20]; - FXSYS_memset(offset_buf, 0, sizeof(offset_buf)); - FXSYS_i64toa(m_XrefStart, offset_buf, 10); - if (m_File.AppendBlock(offset_buf, FXSYS_strlen(offset_buf)) < 0) { - return -1; - } - if (m_File.AppendString("\r\n%%EOF\r\n") < 0) { - return -1; - } - m_File.Flush(); - return m_iStage = 100; -} -void CPDF_Creator::Clear() { - delete m_pXRefStream; - m_pXRefStream = NULL; - m_File.Clear(); - m_NewObjNumArray.RemoveAll(); - if (m_pIDArray) { - m_pIDArray->Release(); - m_pIDArray = NULL; - } -} - -bool CPDF_Creator::Create(IFX_StreamWrite* pFile, FX_DWORD flags) { - m_File.AttachFile(pFile); - return Create(flags); -} - -bool CPDF_Creator::Create(FX_DWORD flags) { - m_dwFlags = flags; - m_iStage = 0; - m_Offset = 0; - m_dwLastObjNum = m_pDocument->GetLastObjNum(); - m_ObjectOffset.Clear(); - m_NewObjNumArray.RemoveAll(); - InitID(); - if (flags & FPDFCREATE_PROGRESSIVE) { - return true; - } - return Continue(NULL) > -1; -} -void CPDF_Creator::InitID(FX_BOOL bDefault) { - CPDF_Array* pOldIDArray = m_pParser ? m_pParser->GetIDArray() : NULL; - FX_BOOL bNewId = !m_pIDArray; - if (!m_pIDArray) { - m_pIDArray = new CPDF_Array; - CPDF_Object* pID1 = pOldIDArray ? pOldIDArray->GetElement(0) : NULL; - if (pID1) { - m_pIDArray->Add(pID1->Clone()); - } else { - std::vector<uint8_t> buffer = - PDF_GenerateFileID((FX_DWORD)(uintptr_t) this, m_dwLastObjNum); - CFX_ByteStringC bsBuffer(buffer.data(), buffer.size()); - m_pIDArray->Add(new CPDF_String(bsBuffer, TRUE), m_pDocument); - } - } - if (!bDefault) { - return; - } - if (pOldIDArray) { - CPDF_Object* pID2 = pOldIDArray->GetElement(1); - if ((m_dwFlags & FPDFCREATE_INCREMENTAL) && m_pEncryptDict && pID2) { - m_pIDArray->Add(pID2->Clone()); - return; - } - std::vector<uint8_t> buffer = - PDF_GenerateFileID((FX_DWORD)(uintptr_t) this, m_dwLastObjNum); - CFX_ByteStringC bsBuffer(buffer.data(), buffer.size()); - m_pIDArray->Add(new CPDF_String(bsBuffer, TRUE), m_pDocument); - return; - } - m_pIDArray->Add(m_pIDArray->GetElement(0)->Clone()); - if (m_pEncryptDict && !pOldIDArray && m_pParser && bNewId) { - if (m_pEncryptDict->GetStringBy("Filter") == "Standard") { - CPDF_StandardSecurityHandler handler; - CFX_ByteString user_pass = m_pParser->GetPassword(); - FX_DWORD flag = PDF_ENCRYPT_CONTENT; - handler.OnCreate(m_pEncryptDict, m_pIDArray, (const uint8_t*)user_pass, - user_pass.GetLength(), flag); - if (m_bNewCrypto) { - delete m_pCryptoHandler; - } - m_pCryptoHandler = new CPDF_StandardCryptoHandler; - m_pCryptoHandler->Init(m_pEncryptDict, &handler); - m_bNewCrypto = TRUE; - m_bSecurityChanged = TRUE; - } - } -} -int32_t CPDF_Creator::Continue(IFX_Pause* pPause) { - if (m_iStage < 0) { - return m_iStage; - } - int32_t iRet = 0; - while (m_iStage < 100) { - if (m_iStage < 20) { - iRet = WriteDoc_Stage1(pPause); - } else if (m_iStage < 30) { - iRet = WriteDoc_Stage2(pPause); - } else if (m_iStage < 90) { - iRet = WriteDoc_Stage3(pPause); - } else { - iRet = WriteDoc_Stage4(pPause); - } - if (iRet < m_iStage) { - break; - } - } - if (iRet < 1 || m_iStage == 100) { - m_iStage = -1; - Clear(); - return iRet > 99 ? 0 : (iRet < 1 ? -1 : iRet); - } - return m_iStage; -} -FX_BOOL CPDF_Creator::SetFileVersion(int32_t fileVersion) { - if (fileVersion < 10 || fileVersion > 17) { - return FALSE; - } - m_FileVersion = fileVersion; - return TRUE; -} -void CPDF_Creator::RemoveSecurity() { - ResetStandardSecurity(); - m_bSecurityChanged = TRUE; - m_pEncryptDict = NULL; - m_pCryptoHandler = NULL; -} -void CPDF_Creator::ResetStandardSecurity() { - if (m_bStandardSecurity || m_bNewCrypto) { - delete m_pCryptoHandler; - m_pCryptoHandler = NULL; - } - m_bNewCrypto = FALSE; - if (!m_bStandardSecurity) { - return; - } - if (m_pEncryptDict) { - m_pEncryptDict->Release(); - m_pEncryptDict = NULL; - } - m_bStandardSecurity = FALSE; -} diff --git a/core/src/fpdfapi/fpdf_edit/fpdf_edit_doc.cpp b/core/src/fpdfapi/fpdf_edit/fpdf_edit_doc.cpp deleted file mode 100644 index 1d21727f72..0000000000 --- a/core/src/fpdfapi/fpdf_edit/fpdf_edit_doc.cpp +++ /dev/null @@ -1,1156 +0,0 @@ -// Copyright 2014 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#include <limits.h> - -#include "core/include/fpdfapi/cpdf_array.h" -#include "core/include/fpdfapi/cpdf_dictionary.h" -#include "core/include/fpdfapi/cpdf_document.h" -#include "core/include/fpdfapi/cpdf_reference.h" -#include "core/include/fpdfapi/fpdf_module.h" -#include "core/include/fpdfapi/fpdf_page.h" -#include "core/src/fpdfapi/fpdf_page/pageint.h" - -CPDF_Document::CPDF_Document() : CPDF_IndirectObjectHolder(NULL) { - m_pRootDict = NULL; - m_pInfoDict = NULL; - m_bLinearized = FALSE; - m_dwFirstPageNo = 0; - m_dwFirstPageObjNum = 0; - m_pDocPage = CPDF_ModuleMgr::Get()->GetPageModule()->CreateDocData(this); - m_pDocRender = CPDF_ModuleMgr::Get()->GetRenderModule()->CreateDocData(this); -} -void CPDF_Document::CreateNewDoc() { - ASSERT(!m_pRootDict && !m_pInfoDict); - m_pRootDict = new CPDF_Dictionary; - m_pRootDict->SetAtName("Type", "Catalog"); - int objnum = AddIndirectObject(m_pRootDict); - CPDF_Dictionary* pPages = new CPDF_Dictionary; - pPages->SetAtName("Type", "Pages"); - pPages->SetAtNumber("Count", 0); - pPages->SetAt("Kids", new CPDF_Array); - objnum = AddIndirectObject(pPages); - m_pRootDict->SetAtReference("Pages", this, objnum); - m_pInfoDict = new CPDF_Dictionary; - AddIndirectObject(m_pInfoDict); -} -static const FX_WCHAR g_FX_CP874Unicodes[128] = { - 0x20AC, 0x0000, 0x0000, 0x0000, 0x0000, 0x2026, 0x0000, 0x0000, 0x0000, - 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x2018, - 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x0000, 0x0000, 0x0000, - 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x00A0, 0x0E01, 0x0E02, 0x0E03, - 0x0E04, 0x0E05, 0x0E06, 0x0E07, 0x0E08, 0x0E09, 0x0E0A, 0x0E0B, 0x0E0C, - 0x0E0D, 0x0E0E, 0x0E0F, 0x0E10, 0x0E11, 0x0E12, 0x0E13, 0x0E14, 0x0E15, - 0x0E16, 0x0E17, 0x0E18, 0x0E19, 0x0E1A, 0x0E1B, 0x0E1C, 0x0E1D, 0x0E1E, - 0x0E1F, 0x0E20, 0x0E21, 0x0E22, 0x0E23, 0x0E24, 0x0E25, 0x0E26, 0x0E27, - 0x0E28, 0x0E29, 0x0E2A, 0x0E2B, 0x0E2C, 0x0E2D, 0x0E2E, 0x0E2F, 0x0E30, - 0x0E31, 0x0E32, 0x0E33, 0x0E34, 0x0E35, 0x0E36, 0x0E37, 0x0E38, 0x0E39, - 0x0E3A, 0x0000, 0x0000, 0x0000, 0x0000, 0x0E3F, 0x0E40, 0x0E41, 0x0E42, - 0x0E43, 0x0E44, 0x0E45, 0x0E46, 0x0E47, 0x0E48, 0x0E49, 0x0E4A, 0x0E4B, - 0x0E4C, 0x0E4D, 0x0E4E, 0x0E4F, 0x0E50, 0x0E51, 0x0E52, 0x0E53, 0x0E54, - 0x0E55, 0x0E56, 0x0E57, 0x0E58, 0x0E59, 0x0E5A, 0x0E5B, 0x0000, 0x0000, - 0x0000, 0x0000, -}; -static const FX_WCHAR g_FX_CP1250Unicodes[128] = { - 0x20AC, 0x0000, 0x201A, 0x0000, 0x201E, 0x2026, 0x2020, 0x2021, 0x0000, - 0x2030, 0x0160, 0x2039, 0x015A, 0x0164, 0x017D, 0x0179, 0x0000, 0x2018, - 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x0000, 0x2122, 0x0161, - 0x203A, 0x015B, 0x0165, 0x017E, 0x017A, 0x00A0, 0x02C7, 0x02D8, 0x0141, - 0x00A4, 0x0104, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0x015E, 0x00AB, 0x00AC, - 0x00AD, 0x00AE, 0x017B, 0x00B0, 0x00B1, 0x02DB, 0x0142, 0x00B4, 0x00B5, - 0x00B6, 0x00B7, 0x00B8, 0x0105, 0x015F, 0x00BB, 0x013D, 0x02DD, 0x013E, - 0x017C, 0x0154, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x0139, 0x0106, 0x00C7, - 0x010C, 0x00C9, 0x0118, 0x00CB, 0x011A, 0x00CD, 0x00CE, 0x010E, 0x0110, - 0x0143, 0x0147, 0x00D3, 0x00D4, 0x0150, 0x00D6, 0x00D7, 0x0158, 0x016E, - 0x00DA, 0x0170, 0x00DC, 0x00DD, 0x0162, 0x00DF, 0x0155, 0x00E1, 0x00E2, - 0x0103, 0x00E4, 0x013A, 0x0107, 0x00E7, 0x010D, 0x00E9, 0x0119, 0x00EB, - 0x011B, 0x00ED, 0x00EE, 0x010F, 0x0111, 0x0144, 0x0148, 0x00F3, 0x00F4, - 0x0151, 0x00F6, 0x00F7, 0x0159, 0x016F, 0x00FA, 0x0171, 0x00FC, 0x00FD, - 0x0163, 0x02D9, -}; -static const FX_WCHAR g_FX_CP1251Unicodes[128] = { - 0x0402, 0x0403, 0x201A, 0x0453, 0x201E, 0x2026, 0x2020, 0x2021, 0x20AC, - 0x2030, 0x0409, 0x2039, 0x040A, 0x040C, 0x040B, 0x040F, 0x0452, 0x2018, - 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x0000, 0x2122, 0x0459, - 0x203A, 0x045A, 0x045C, 0x045B, 0x045F, 0x00A0, 0x040E, 0x045E, 0x0408, - 0x00A4, 0x0490, 0x00A6, 0x00A7, 0x0401, 0x00A9, 0x0404, 0x00AB, 0x00AC, - 0x00AD, 0x00AE, 0x0407, 0x00B0, 0x00B1, 0x0406, 0x0456, 0x0491, 0x00B5, - 0x00B6, 0x00B7, 0x0451, 0x2116, 0x0454, 0x00BB, 0x0458, 0x0405, 0x0455, - 0x0457, 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, - 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F, 0x0420, - 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, 0x0428, 0x0429, - 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F, 0x0430, 0x0431, 0x0432, - 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, 0x0438, 0x0439, 0x043A, 0x043B, - 0x043C, 0x043D, 0x043E, 0x043F, 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, - 0x0445, 0x0446, 0x0447, 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, - 0x044E, 0x044F, -}; -static const FX_WCHAR g_FX_CP1253Unicodes[128] = { - 0x20AC, 0x0000, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, 0x0000, - 0x2030, 0x0000, 0x2039, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x2018, - 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x0000, 0x2122, 0x0000, - 0x203A, 0x0000, 0x0000, 0x0000, 0x0000, 0x00A0, 0x0385, 0x0386, 0x00A3, - 0x00A4, 0x00A5, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0x0000, 0x00AB, 0x00AC, - 0x00AD, 0x00AE, 0x2015, 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x0384, 0x00B5, - 0x00B6, 0x00B7, 0x0388, 0x0389, 0x038A, 0x00BB, 0x038C, 0x00BD, 0x038E, - 0x038F, 0x0390, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397, - 0x0398, 0x0399, 0x039A, 0x039B, 0x039C, 0x039D, 0x039E, 0x039F, 0x03A0, - 0x03A1, 0x0000, 0x03A3, 0x03A4, 0x03A5, 0x03A6, 0x03A7, 0x03A8, 0x03A9, - 0x03AA, 0x03AB, 0x03AC, 0x03AD, 0x03AE, 0x03AF, 0x03B0, 0x03B1, 0x03B2, - 0x03B3, 0x03B4, 0x03B5, 0x03B6, 0x03B7, 0x03B8, 0x03B9, 0x03BA, 0x03BB, - 0x03BC, 0x03BD, 0x03BE, 0x03BF, 0x03C0, 0x03C1, 0x03C2, 0x03C3, 0x03C4, - 0x03C5, 0x03C6, 0x03C7, 0x03C8, 0x03C9, 0x03CA, 0x03CB, 0x03CC, 0x03CD, - 0x03CE, 0x0000, -}; -static const FX_WCHAR g_FX_CP1254Unicodes[128] = { - 0x20AC, 0x0000, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, 0x02C6, - 0x2030, 0x0160, 0x2039, 0x0152, 0x0000, 0x0000, 0x0000, 0x0000, 0x2018, - 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x02DC, 0x2122, 0x0161, - 0x203A, 0x0153, 0x0000, 0x0000, 0x0178, 0x00A0, 0x00A1, 0x00A2, 0x00A3, - 0x00A4, 0x00A5, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, - 0x00AD, 0x00AE, 0x00AF, 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, - 0x00B6, 0x00B7, 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, - 0x00BF, 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x00C7, - 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF, 0x011E, - 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x00D7, 0x00D8, 0x00D9, - 0x00DA, 0x00DB, 0x00DC, 0x0130, 0x015E, 0x00DF, 0x00E0, 0x00E1, 0x00E2, - 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7, 0x00E8, 0x00E9, 0x00EA, 0x00EB, - 0x00EC, 0x00ED, 0x00EE, 0x00EF, 0x011F, 0x00F1, 0x00F2, 0x00F3, 0x00F4, - 0x00F5, 0x00F6, 0x00F7, 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x0131, - 0x015F, 0x00FF, -}; -static const FX_WCHAR g_FX_CP1255Unicodes[128] = { - 0x20AC, 0x0000, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, 0x02C6, - 0x2030, 0x0000, 0x2039, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x2018, - 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x02DC, 0x2122, 0x0000, - 0x203A, 0x0000, 0x0000, 0x0000, 0x0000, 0x00A0, 0x00A1, 0x00A2, 0x00A3, - 0x20AA, 0x00A5, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0x00D7, 0x00AB, 0x00AC, - 0x00AD, 0x00AE, 0x00AF, 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, - 0x00B6, 0x00B7, 0x00B8, 0x00B9, 0x00F7, 0x00BB, 0x00BC, 0x00BD, 0x00BE, - 0x00BF, 0x05B0, 0x05B1, 0x05B2, 0x05B3, 0x05B4, 0x05B5, 0x05B6, 0x05B7, - 0x05B8, 0x05B9, 0x0000, 0x05BB, 0x05BC, 0x05BD, 0x05BE, 0x05BF, 0x05C0, - 0x05C1, 0x05C2, 0x05C3, 0x05F0, 0x05F1, 0x05F2, 0x05F3, 0x05F4, 0x0000, - 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x05D0, 0x05D1, 0x05D2, - 0x05D3, 0x05D4, 0x05D5, 0x05D6, 0x05D7, 0x05D8, 0x05D9, 0x05DA, 0x05DB, - 0x05DC, 0x05DD, 0x05DE, 0x05DF, 0x05E0, 0x05E1, 0x05E2, 0x05E3, 0x05E4, - 0x05E5, 0x05E6, 0x05E7, 0x05E8, 0x05E9, 0x05EA, 0x0000, 0x0000, 0x200E, - 0x200F, 0x0000, -}; -static const FX_WCHAR g_FX_CP1256Unicodes[128] = { - 0x20AC, 0x067E, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, 0x02C6, - 0x2030, 0x0679, 0x2039, 0x0152, 0x0686, 0x0698, 0x0688, 0x06AF, 0x2018, - 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x06A9, 0x2122, 0x0691, - 0x203A, 0x0153, 0x200C, 0x200D, 0x06BA, 0x00A0, 0x060C, 0x00A2, 0x00A3, - 0x00A4, 0x00A5, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0x06BE, 0x00AB, 0x00AC, - 0x00AD, 0x00AE, 0x00AF, 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, - 0x00B6, 0x00B7, 0x00B8, 0x00B9, 0x061B, 0x00BB, 0x00BC, 0x00BD, 0x00BE, - 0x061F, 0x06C1, 0x0621, 0x0622, 0x0623, 0x0624, 0x0625, 0x0626, 0x0627, - 0x0628, 0x0629, 0x062A, 0x062B, 0x062C, 0x062D, 0x062E, 0x062F, 0x0630, - 0x0631, 0x0632, 0x0633, 0x0634, 0x0635, 0x0636, 0x00D7, 0x0637, 0x0638, - 0x0639, 0x063A, 0x0640, 0x0641, 0x0642, 0x0643, 0x00E0, 0x0644, 0x00E2, - 0x0645, 0x0646, 0x0647, 0x0648, 0x00E7, 0x00E8, 0x00E9, 0x00EA, 0x00EB, - 0x0649, 0x064A, 0x00EE, 0x00EF, 0x064B, 0x064C, 0x064D, 0x064E, 0x00F4, - 0x064F, 0x0650, 0x00F7, 0x0651, 0x00F9, 0x0652, 0x00FB, 0x00FC, 0x200E, - 0x200F, 0x06D2, -}; -static const FX_WCHAR g_FX_CP1257Unicodes[128] = { - 0x20AC, 0x0000, 0x201A, 0x0000, 0x201E, 0x2026, 0x2020, 0x2021, 0x0000, - 0x2030, 0x0000, 0x2039, 0x0000, 0x00A8, 0x02C7, 0x00B8, 0x0000, 0x2018, - 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x0000, 0x2122, 0x0000, - 0x203A, 0x0000, 0x00AF, 0x02DB, 0x0000, 0x00A0, 0x0000, 0x00A2, 0x00A3, - 0x00A4, 0x0000, 0x00A6, 0x00A7, 0x00D8, 0x00A9, 0x0156, 0x00AB, 0x00AC, - 0x00AD, 0x00AE, 0x00C6, 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, - 0x00B6, 0x00B7, 0x00F8, 0x00B9, 0x0157, 0x00BB, 0x00BC, 0x00BD, 0x00BE, - 0x00E6, 0x0104, 0x012E, 0x0100, 0x0106, 0x00C4, 0x00C5, 0x0118, 0x0112, - 0x010C, 0x00C9, 0x0179, 0x0116, 0x0122, 0x0136, 0x012A, 0x013B, 0x0160, - 0x0143, 0x0145, 0x00D3, 0x014C, 0x00D5, 0x00D6, 0x00D7, 0x0172, 0x0141, - 0x015A, 0x016A, 0x00DC, 0x017B, 0x017D, 0x00DF, 0x0105, 0x012F, 0x0101, - 0x0107, 0x00E4, 0x00E5, 0x0119, 0x0113, 0x010D, 0x00E9, 0x017A, 0x0117, - 0x0123, 0x0137, 0x012B, 0x013C, 0x0161, 0x0144, 0x0146, 0x00F3, 0x014D, - 0x00F5, 0x00F6, 0x00F7, 0x0173, 0x0142, 0x015B, 0x016B, 0x00FC, 0x017C, - 0x017E, 0x02D9, -}; -typedef struct { - uint8_t m_Charset; - const FX_WCHAR* m_pUnicodes; -} FX_CharsetUnicodes; -const FX_CharsetUnicodes g_FX_CharsetUnicodes[] = { - {FXFONT_THAI_CHARSET, g_FX_CP874Unicodes}, - {FXFONT_EASTEUROPE_CHARSET, g_FX_CP1250Unicodes}, - {FXFONT_RUSSIAN_CHARSET, g_FX_CP1251Unicodes}, - {FXFONT_GREEK_CHARSET, g_FX_CP1253Unicodes}, - {FXFONT_TURKISH_CHARSET, g_FX_CP1254Unicodes}, - {FXFONT_HEBREW_CHARSET, g_FX_CP1255Unicodes}, - {FXFONT_ARABIC_CHARSET, g_FX_CP1256Unicodes}, - {FXFONT_BALTIC_CHARSET, g_FX_CP1257Unicodes}, -}; -#if (_FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN64_DESKTOP_) -static void _InsertWidthArray(HDC hDC, - int start, - int end, - CPDF_Array* pWidthArray) { - int size = end - start + 1; - int* widths = FX_Alloc(int, size); - GetCharWidth(hDC, start, end, widths); - int i; - for (i = 1; i < size; i++) - if (widths[i] != *widths) { - break; - } - if (i == size) { - int first = pWidthArray->GetIntegerAt(pWidthArray->GetCount() - 1); - pWidthArray->AddInteger(first + size - 1); - pWidthArray->AddInteger(*widths); - } else { - CPDF_Array* pWidthArray1 = new CPDF_Array; - pWidthArray->Add(pWidthArray1); - for (i = 0; i < size; i++) { - pWidthArray1->AddInteger(widths[i]); - } - } - FX_Free(widths); -} -CPDF_Font* CPDF_Document::AddWindowsFont(LOGFONTW* pLogFont, - FX_BOOL bVert, - FX_BOOL bTranslateName) { - LOGFONTA lfa; - FXSYS_memcpy(&lfa, pLogFont, (char*)lfa.lfFaceName - (char*)&lfa); - CFX_ByteString face = CFX_ByteString::FromUnicode(pLogFont->lfFaceName); - if (face.GetLength() >= LF_FACESIZE) { - return NULL; - } - FXSYS_strcpy(lfa.lfFaceName, face.c_str()); - return AddWindowsFont(&lfa, bVert, bTranslateName); -} -CFX_ByteString _FPDF_GetPSNameFromTT(HDC hDC) { - CFX_ByteString result; - DWORD size = ::GetFontData(hDC, 'eman', 0, NULL, 0); - if (size != GDI_ERROR) { - LPBYTE buffer = FX_Alloc(BYTE, size); - ::GetFontData(hDC, 'eman', 0, buffer, size); - result = GetNameFromTT(buffer, 6); - FX_Free(buffer); - } - return result; -} -CPDF_Font* CPDF_Document::AddWindowsFont(LOGFONTA* pLogFont, - FX_BOOL bVert, - FX_BOOL bTranslateName) { - pLogFont->lfHeight = -1000; - pLogFont->lfWidth = 0; - HGDIOBJ hFont = CreateFontIndirectA(pLogFont); - HDC hDC = CreateCompatibleDC(NULL); - hFont = SelectObject(hDC, hFont); - int tm_size = GetOutlineTextMetrics(hDC, 0, NULL); - if (tm_size == 0) { - hFont = SelectObject(hDC, hFont); - DeleteObject(hFont); - DeleteDC(hDC); - return NULL; - } - LPBYTE tm_buf = FX_Alloc(BYTE, tm_size); - OUTLINETEXTMETRIC* ptm = (OUTLINETEXTMETRIC*)tm_buf; - GetOutlineTextMetrics(hDC, tm_size, ptm); - int flags = 0, italicangle, ascend, descend, capheight, bbox[4]; - if (pLogFont->lfItalic) { - flags |= PDFFONT_ITALIC; - } - if ((pLogFont->lfPitchAndFamily & 3) == FIXED_PITCH) { - flags |= PDFFONT_FIXEDPITCH; - } - if ((pLogFont->lfPitchAndFamily & 0xf8) == FF_ROMAN) { - flags |= PDFFONT_SERIF; - } - if ((pLogFont->lfPitchAndFamily & 0xf8) == FF_SCRIPT) { - flags |= PDFFONT_SCRIPT; - } - bool bCJK = pLogFont->lfCharSet == CHINESEBIG5_CHARSET || - pLogFont->lfCharSet == GB2312_CHARSET || - pLogFont->lfCharSet == HANGEUL_CHARSET || - pLogFont->lfCharSet == SHIFTJIS_CHARSET; - CFX_ByteString basefont; - if (bTranslateName && bCJK) { - basefont = _FPDF_GetPSNameFromTT(hDC); - } - if (basefont.IsEmpty()) { - basefont = pLogFont->lfFaceName; - } - italicangle = ptm->otmItalicAngle / 10; - ascend = ptm->otmrcFontBox.top; - descend = ptm->otmrcFontBox.bottom; - capheight = ptm->otmsCapEmHeight; - bbox[0] = ptm->otmrcFontBox.left; - bbox[1] = ptm->otmrcFontBox.bottom; - bbox[2] = ptm->otmrcFontBox.right; - bbox[3] = ptm->otmrcFontBox.top; - FX_Free(tm_buf); - basefont.Replace(" ", ""); - CPDF_Dictionary* pBaseDict = new CPDF_Dictionary; - pBaseDict->SetAtName("Type", "Font"); - CPDF_Dictionary* pFontDict = pBaseDict; - if (!bCJK) { - if (pLogFont->lfCharSet == ANSI_CHARSET || - pLogFont->lfCharSet == DEFAULT_CHARSET || - pLogFont->lfCharSet == SYMBOL_CHARSET) { - if (pLogFont->lfCharSet == SYMBOL_CHARSET) { - flags |= PDFFONT_SYMBOLIC; - } else { - flags |= PDFFONT_NONSYMBOLIC; - } - pBaseDict->SetAtName("Encoding", "WinAnsiEncoding"); - } else { - flags |= PDFFONT_NONSYMBOLIC; - int i; - for (i = 0; i < sizeof g_FX_CharsetUnicodes / sizeof(FX_CharsetUnicodes); - i++) - if (g_FX_CharsetUnicodes[i].m_Charset == pLogFont->lfCharSet) { - break; - } - if (i < sizeof g_FX_CharsetUnicodes / sizeof(FX_CharsetUnicodes)) { - CPDF_Dictionary* pEncoding = new CPDF_Dictionary; - pEncoding->SetAtName("BaseEncoding", "WinAnsiEncoding"); - CPDF_Array* pArray = new CPDF_Array; - pArray->AddInteger(128); - const FX_WCHAR* pUnicodes = g_FX_CharsetUnicodes[i].m_pUnicodes; - for (int j = 0; j < 128; j++) { - CFX_ByteString name = PDF_AdobeNameFromUnicode(pUnicodes[j]); - if (name.IsEmpty()) { - pArray->AddName(".notdef"); - } else { - pArray->AddName(name); - } - } - pEncoding->SetAt("Differences", pArray); - AddIndirectObject(pEncoding); - pBaseDict->SetAtReference("Encoding", this, pEncoding); - } - } - if (pLogFont->lfWeight > FW_MEDIUM && pLogFont->lfItalic) { - basefont += ",BoldItalic"; - } else if (pLogFont->lfWeight > FW_MEDIUM) { - basefont += ",Bold"; - } else if (pLogFont->lfItalic) { - basefont += ",Italic"; - } - pBaseDict->SetAtName("Subtype", "TrueType"); - pBaseDict->SetAtName("BaseFont", basefont); - pBaseDict->SetAtNumber("FirstChar", 32); - pBaseDict->SetAtNumber("LastChar", 255); - int char_widths[224]; - GetCharWidth(hDC, 32, 255, char_widths); - CPDF_Array* pWidths = new CPDF_Array; - for (int i = 0; i < 224; i++) { - pWidths->AddInteger(char_widths[i]); - } - pBaseDict->SetAt("Widths", pWidths); - } else { - flags |= PDFFONT_NONSYMBOLIC; - pFontDict = new CPDF_Dictionary; - CFX_ByteString cmap; - CFX_ByteString ordering; - int supplement; - CPDF_Array* pWidthArray = new CPDF_Array; - switch (pLogFont->lfCharSet) { - case CHINESEBIG5_CHARSET: - cmap = bVert ? "ETenms-B5-V" : "ETenms-B5-H"; - ordering = "CNS1"; - supplement = 4; - pWidthArray->AddInteger(1); - _InsertWidthArray(hDC, 0x20, 0x7e, pWidthArray); - break; - case GB2312_CHARSET: - cmap = bVert ? "GBK-EUC-V" : "GBK-EUC-H"; - ordering = "GB1", supplement = 2; - pWidthArray->AddInteger(7716); - _InsertWidthArray(hDC, 0x20, 0x20, pWidthArray); - pWidthArray->AddInteger(814); - _InsertWidthArray(hDC, 0x21, 0x7e, pWidthArray); - break; - case HANGEUL_CHARSET: - cmap = bVert ? "KSCms-UHC-V" : "KSCms-UHC-H"; - ordering = "Korea1"; - supplement = 2; - pWidthArray->AddInteger(1); - _InsertWidthArray(hDC, 0x20, 0x7e, pWidthArray); - break; - case SHIFTJIS_CHARSET: - cmap = bVert ? "90ms-RKSJ-V" : "90ms-RKSJ-H"; - ordering = "Japan1"; - supplement = 5; - pWidthArray->AddInteger(231); - _InsertWidthArray(hDC, 0x20, 0x7d, pWidthArray); - pWidthArray->AddInteger(326); - _InsertWidthArray(hDC, 0xa0, 0xa0, pWidthArray); - pWidthArray->AddInteger(327); - _InsertWidthArray(hDC, 0xa1, 0xdf, pWidthArray); - pWidthArray->AddInteger(631); - _InsertWidthArray(hDC, 0x7e, 0x7e, pWidthArray); - break; - } - pBaseDict->SetAtName("Subtype", "Type0"); - pBaseDict->SetAtName("BaseFont", basefont); - pBaseDict->SetAtName("Encoding", cmap); - pFontDict->SetAt("W", pWidthArray); - pFontDict->SetAtName("Type", "Font"); - pFontDict->SetAtName("Subtype", "CIDFontType2"); - pFontDict->SetAtName("BaseFont", basefont); - CPDF_Dictionary* pCIDSysInfo = new CPDF_Dictionary; - pCIDSysInfo->SetAtString("Registry", "Adobe"); - pCIDSysInfo->SetAtString("Ordering", ordering); - pCIDSysInfo->SetAtInteger("Supplement", supplement); - pFontDict->SetAt("CIDSystemInfo", pCIDSysInfo); - CPDF_Array* pArray = new CPDF_Array; - pBaseDict->SetAt("DescendantFonts", pArray); - AddIndirectObject(pFontDict); - pArray->AddReference(this, pFontDict); - } - AddIndirectObject(pBaseDict); - CPDF_Dictionary* pFontDesc = new CPDF_Dictionary; - pFontDesc->SetAtName("Type", "FontDescriptor"); - pFontDesc->SetAtName("FontName", basefont); - pFontDesc->SetAtInteger("Flags", flags); - CPDF_Array* pBBox = new CPDF_Array; - for (int i = 0; i < 4; i++) { - pBBox->AddInteger(bbox[i]); - } - pFontDesc->SetAt("FontBBox", pBBox); - pFontDesc->SetAtInteger("ItalicAngle", italicangle); - pFontDesc->SetAtInteger("Ascent", ascend); - pFontDesc->SetAtInteger("Descent", descend); - pFontDesc->SetAtInteger("CapHeight", capheight); - pFontDesc->SetAtInteger("StemV", pLogFont->lfWeight / 5); - AddIndirectObject(pFontDesc); - pFontDict->SetAtReference("FontDescriptor", this, pFontDesc); - hFont = SelectObject(hDC, hFont); - DeleteObject(hFont); - DeleteDC(hDC); - return LoadFont(pBaseDict); -} -#endif - -#if (_FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_) -uint32_t FX_GetLangHashCode(const FX_CHAR* pStr) { - FXSYS_assert(pStr); - int32_t iLength = FXSYS_strlen(pStr); - const FX_CHAR* pStrEnd = pStr + iLength; - uint32_t uHashCode = 0; - while (pStr < pStrEnd) { - uHashCode = 31 * uHashCode + tolower(*pStr++); - } - return uHashCode; -} -struct FX_LANG2CS { - FX_DWORD uLang; - int uCharset; -} * FX_LPLANG2CS; -static const FX_LANG2CS gs_FXLang2CharsetTable[] = { - {3109, 0}, {3121, 178}, {3129, 162}, {3139, 204}, {3141, 204}, - {3166, 0}, {3184, 238}, {3197, 0}, {3201, 0}, {3239, 161}, - {3241, 0}, {3246, 0}, {3247, 186}, {3248, 0}, {3259, 178}, - {3267, 0}, {3273, 0}, {3276, 0}, {3301, 0}, {3310, 1}, - {3325, 177}, {3329, 1}, {3338, 238}, {3341, 238}, {3345, 1}, - {3355, 0}, {3370, 0}, {3371, 0}, {3383, 128}, {3424, 204}, - {3427, 1}, {3428, 129}, {3436, 178}, {3464, 186}, {3466, 186}, - {3486, 204}, {3487, 0}, {3493, 1}, {3494, 0}, {3508, 0}, - {3518, 0}, {3520, 0}, {3569, 1}, {3580, 238}, {3588, 0}, - {3645, 238}, {3651, 204}, {3672, 238}, {3673, 238}, {3678, 238}, - {3679, 238}, {3683, 0}, {3684, 0}, {3693, 1}, {3697, 1}, - {3700, 222}, {3710, 162}, {3734, 204}, {3741, 178}, {3749, 162}, - {3763, 163}, {3886, 134}, {105943, 0}, {106375, 1}, {3923451837, 134}, - {3923451838, 136}, -}; -static FX_WORD FX_GetCsFromLangCode(uint32_t uCode) { - int32_t iStart = 0; - int32_t iEnd = sizeof(gs_FXLang2CharsetTable) / sizeof(FX_LANG2CS) - 1; - while (iStart <= iEnd) { - int32_t iMid = (iStart + iEnd) / 2; - const FX_LANG2CS& charset = gs_FXLang2CharsetTable[iMid]; - if (uCode == charset.uLang) { - return charset.uCharset; - } - if (uCode < charset.uLang) { - iEnd = iMid - 1; - } else { - iStart = iMid + 1; - } - } - return 0; -} -static FX_WORD FX_GetCharsetFromLang(const FX_CHAR* pLang, int32_t iLength) { - FXSYS_assert(pLang); - if (iLength < 0) { - iLength = FXSYS_strlen(pLang); - } - uint32_t uHash = FX_GetLangHashCode(pLang); - return FX_GetCsFromLangCode(uHash); -} -static void _CFString2CFXByteString(CFStringRef src, CFX_ByteString& dest) { - SInt32 len = CFStringGetLength(src); - CFRange range = CFRangeMake(0, len); - CFIndex used = 0; - UInt8* pBuffer = (UInt8*)calloc(len + 1, sizeof(UInt8)); - CFStringGetBytes(src, range, kCFStringEncodingASCII, 0, false, pBuffer, len, - &used); - dest = (FX_CHAR*)pBuffer; - free(pBuffer); -} -FX_BOOL IsHasCharSet(CFArrayRef languages, const CFX_DWordArray& charSets) { - int iCount = charSets.GetSize(); - for (int i = 0; i < CFArrayGetCount(languages); ++i) { - CFStringRef language = (CFStringRef)CFArrayGetValueAtIndex(languages, i); - FX_DWORD CharSet = FX_GetCharsetFromLang( - CFStringGetCStringPtr(language, kCFStringEncodingMacRoman), -1); - for (int j = 0; j < iCount; ++j) { - if (CharSet == charSets[j]) { - return TRUE; - } - } - } - return FALSE; -} -void FX_GetCharWidth(CTFontRef font, UniChar start, UniChar end, int* width) { - CGFloat size = CTFontGetSize(font); - for (; start <= end; ++start) { - CGGlyph pGlyph = 0; - CFIndex count = 1; - CTFontGetGlyphsForCharacters(font, &start, &pGlyph, count); - CGSize advances; - CTFontGetAdvancesForGlyphs(font, kCTFontDefaultOrientation, &pGlyph, - &advances, 1); - *width = (int)(advances.width / size * 1000); - width++; - } -} -static void _InsertWidthArray(CTFontRef font, - int start, - int end, - CPDF_Array* pWidthArray) { - int size = end - start + 1; - int* widths = FX_Alloc(int, size); - FX_GetCharWidth(font, start, end, widths); - int i; - for (i = 1; i < size; i++) - if (widths[i] != *widths) { - break; - } - if (i == size) { - int first = pWidthArray->GetIntegerAt(pWidthArray->GetCount() - 1); - pWidthArray->AddInteger(first + size - 1); - pWidthArray->AddInteger(*widths); - } else { - CPDF_Array* pWidthArray1 = new CPDF_Array; - pWidthArray->Add(pWidthArray1); - for (i = 0; i < size; i++) { - pWidthArray1->AddInteger(widths[i]); - } - } - FX_Free(widths); -} -CPDF_Font* CPDF_Document::AddMacFont(CTFontRef pFont, - FX_BOOL bVert, - FX_BOOL bTranslateName) { - CTFontRef font = (CTFontRef)pFont; - CTFontDescriptorRef descriptor = CTFontCopyFontDescriptor(font); - if (!descriptor) { - return NULL; - } - CFX_ByteString basefont; - FX_BOOL bCJK = FALSE; - int flags = 0, italicangle = 0, ascend = 0, descend = 0, capheight = 0, - bbox[4]; - FXSYS_memset(bbox, 0, sizeof(int) * 4); - CFArrayRef languages = (CFArrayRef)CTFontDescriptorCopyAttribute( - descriptor, kCTFontLanguagesAttribute); - if (!languages) { - CFRelease(descriptor); - return NULL; - } - CFX_DWordArray charSets; - charSets.Add(FXFONT_CHINESEBIG5_CHARSET); - charSets.Add(FXFONT_GB2312_CHARSET); - charSets.Add(FXFONT_HANGEUL_CHARSET); - charSets.Add(FXFONT_SHIFTJIS_CHARSET); - if (IsHasCharSet(languages, charSets)) { - bCJK = TRUE; - } - CFRelease(descriptor); - CFDictionaryRef traits = (CFDictionaryRef)CTFontCopyTraits(font); - if (!traits) { - CFRelease(languages); - return NULL; - } - CFNumberRef sybolicTrait = - (CFNumberRef)CFDictionaryGetValue(traits, kCTFontSymbolicTrait); - CTFontSymbolicTraits trait = 0; - CFNumberGetValue(sybolicTrait, kCFNumberSInt32Type, &trait); - if (trait & kCTFontItalicTrait) { - flags |= PDFFONT_ITALIC; - } - if (trait & kCTFontMonoSpaceTrait) { - flags |= PDFFONT_FIXEDPITCH; - } - if (trait & kCTFontModernSerifsClass) { - flags |= PDFFONT_SERIF; - } - if (trait & kCTFontScriptsClass) { - flags |= PDFFONT_SCRIPT; - } - CFNumberRef weightTrait = - (CFNumberRef)CFDictionaryGetValue(traits, kCTFontWeightTrait); - Float32 weight = 0; - CFNumberGetValue(weightTrait, kCFNumberFloat32Type, &weight); - italicangle = CTFontGetSlantAngle(font); - ascend = CTFontGetAscent(font); - descend = CTFontGetDescent(font); - capheight = CTFontGetCapHeight(font); - CGRect box = CTFontGetBoundingBox(font); - bbox[0] = box.origin.x; - bbox[1] = box.origin.y; - bbox[2] = box.origin.x + box.size.width; - bbox[3] = box.origin.y + box.size.height; - if (bTranslateName && bCJK) { - CFStringRef postName = CTFontCopyPostScriptName(font); - _CFString2CFXByteString(postName, basefont); - CFRelease(postName); - } - if (basefont.IsEmpty()) { - CFStringRef fullName = CTFontCopyFullName(font); - _CFString2CFXByteString(fullName, basefont); - CFRelease(fullName); - } - basefont.Replace(" ", ""); - CPDF_Dictionary* pBaseDict = new CPDF_Dictionary; - CPDF_Dictionary* pFontDict = pBaseDict; - if (!bCJK) { - charSets.RemoveAll(); - charSets.Add(FXFONT_ANSI_CHARSET); - charSets.Add(FXFONT_DEFAULT_CHARSET); - charSets.Add(FXFONT_SYMBOL_CHARSET); - if (IsHasCharSet(languages, charSets)) { - charSets.RemoveAll(); - charSets.Add(FXFONT_SYMBOL_CHARSET); - if (IsHasCharSet(languages, charSets)) { - flags |= PDFFONT_SYMBOLIC; - } else { - flags |= PDFFONT_NONSYMBOLIC; - } - pBaseDict->SetAtName("Encoding", "WinAnsiEncoding"); - } else { - flags |= PDFFONT_NONSYMBOLIC; - size_t i; - for (i = 0; i < sizeof g_FX_CharsetUnicodes / sizeof(FX_CharsetUnicodes); - i++) { - charSets.RemoveAll(); - charSets.Add(g_FX_CharsetUnicodes[i].m_Charset); - if (IsHasCharSet(languages, charSets)) { - break; - } - } - if (i < sizeof g_FX_CharsetUnicodes / sizeof(FX_CharsetUnicodes)) { - CPDF_Dictionary* pEncoding = new CPDF_Dictionary; - pEncoding->SetAtName("BaseEncoding", "WinAnsiEncoding"); - CPDF_Array* pArray = new CPDF_Array; - pArray->AddInteger(128); - const FX_WCHAR* pUnicodes = g_FX_CharsetUnicodes[i].m_pUnicodes; - for (int j = 0; j < 128; j++) { - CFX_ByteString name = PDF_AdobeNameFromUnicode(pUnicodes[j]); - if (name.IsEmpty()) { - pArray->AddName(".notdef"); - } else { - pArray->AddName(name); - } - } - pEncoding->SetAt("Differences", pArray); - AddIndirectObject(pEncoding); - pBaseDict->SetAtReference("Encoding", this, pEncoding); - } - } - if (weight > 0.0 && trait & kCTFontItalicTrait) { - basefont += ",BoldItalic"; - } else if (weight > 0.0) { - basefont += ",Bold"; - } else if (trait & kCTFontItalicTrait) { - basefont += ",Italic"; - } - pBaseDict->SetAtName("Subtype", "TrueType"); - pBaseDict->SetAtName("BaseFont", basefont); - pBaseDict->SetAtNumber("FirstChar", 32); - pBaseDict->SetAtNumber("LastChar", 255); - int char_widths[224]; - FX_GetCharWidth(font, 32, 255, char_widths); - CPDF_Array* pWidths = new CPDF_Array; - for (int i = 0; i < 224; i++) { - pWidths->AddInteger(char_widths[i]); - } - pBaseDict->SetAt("Widths", pWidths); - } else { - flags |= PDFFONT_NONSYMBOLIC; - CPDF_Array* pArray = NULL; - pFontDict = new CPDF_Dictionary; - CFX_ByteString cmap; - CFX_ByteString ordering; - int supplement; - FX_BOOL bFound = FALSE; - CPDF_Array* pWidthArray = new CPDF_Array; - charSets.RemoveAll(); - charSets.Add(FXFONT_CHINESEBIG5_CHARSET); - if (IsHasCharSet(languages, charSets)) { - cmap = bVert ? "ETenms-B5-V" : "ETenms-B5-H"; - ordering = "CNS1"; - supplement = 4; - pWidthArray->AddInteger(1); - _InsertWidthArray(font, 0x20, 0x7e, pWidthArray); - bFound = TRUE; - } - charSets.RemoveAll(); - charSets.Add(FXFONT_GB2312_CHARSET); - if (!bFound && IsHasCharSet(languages, charSets)) { - cmap = bVert ? "GBK-EUC-V" : "GBK-EUC-H"; - ordering = "GB1", supplement = 2; - pWidthArray->AddInteger(7716); - _InsertWidthArray(font, 0x20, 0x20, pWidthArray); - pWidthArray->AddInteger(814); - _InsertWidthArray(font, 0x21, 0x7e, pWidthArray); - bFound = TRUE; - } - charSets.RemoveAll(); - charSets.Add(FXFONT_HANGEUL_CHARSET); - if (!bFound && IsHasCharSet(languages, charSets)) { - cmap = bVert ? "KSCms-UHC-V" : "KSCms-UHC-H"; - ordering = "Korea1"; - supplement = 2; - pWidthArray->AddInteger(1); - _InsertWidthArray(font, 0x20, 0x7e, pWidthArray); - bFound = TRUE; - } - charSets.RemoveAll(); - charSets.Add(FXFONT_SHIFTJIS_CHARSET); - if (!bFound && IsHasCharSet(languages, charSets)) { - cmap = bVert ? "90ms-RKSJ-V" : "90ms-RKSJ-H"; - ordering = "Japan1"; - supplement = 5; - pWidthArray->AddInteger(231); - _InsertWidthArray(font, 0x20, 0x7d, pWidthArray); - pWidthArray->AddInteger(326); - _InsertWidthArray(font, 0xa0, 0xa0, pWidthArray); - pWidthArray->AddInteger(327); - _InsertWidthArray(font, 0xa1, 0xdf, pWidthArray); - pWidthArray->AddInteger(631); - _InsertWidthArray(font, 0x7e, 0x7e, pWidthArray); - } - pBaseDict->SetAtName("Subtype", "Type0"); - pBaseDict->SetAtName("BaseFont", basefont); - pBaseDict->SetAtName("Encoding", cmap); - pFontDict->SetAt("W", pWidthArray); - pFontDict->SetAtName("Type", "Font"); - pFontDict->SetAtName("Subtype", "CIDFontType2"); - pFontDict->SetAtName("BaseFont", basefont); - CPDF_Dictionary* pCIDSysInfo = new CPDF_Dictionary; - pCIDSysInfo->SetAtString("Registry", "Adobe"); - pCIDSysInfo->SetAtString("Ordering", ordering); - pCIDSysInfo->SetAtInteger("Supplement", supplement); - pFontDict->SetAt("CIDSystemInfo", pCIDSysInfo); - pArray = new CPDF_Array; - pBaseDict->SetAt("DescendantFonts", pArray); - AddIndirectObject(pFontDict); - pArray->AddReference(this, pFontDict); - } - AddIndirectObject(pBaseDict); - CPDF_Dictionary* pFontDesc = new CPDF_Dictionary; - pFontDesc->SetAtName("Type", "FontDescriptor"); - pFontDesc->SetAtName("FontName", basefont); - pFontDesc->SetAtInteger("Flags", flags); - CPDF_Array* pBBox = new CPDF_Array; - for (int i = 0; i < 4; i++) { - pBBox->AddInteger(bbox[i]); - } - pFontDesc->SetAt("FontBBox", pBBox); - pFontDesc->SetAtInteger("ItalicAngle", italicangle); - pFontDesc->SetAtInteger("Ascent", ascend); - pFontDesc->SetAtInteger("Descent", descend); - pFontDesc->SetAtInteger("CapHeight", capheight); - CGFloat fStemV = 0; - int16_t min_width = SHRT_MAX; - - static const UniChar stem_chars[] = {'i', 'I', '!', '1'}; - CGGlyph glyphs[FX_ArraySize(stem_chars)]; - CGRect boundingRects[FX_ArraySize(stem_chars)]; - - const size_t count = FX_ArraySize(stem_chars); - if (CTFontGetGlyphsForCharacters(font, stem_chars, glyphs, count)) { - CTFontGetBoundingRectsForGlyphs(font, kCTFontHorizontalOrientation, glyphs, - boundingRects, count); - for (size_t i = 0; i < count; i++) { - int16_t width = boundingRects[i].size.width; - if (width > 0 && width < min_width) { - min_width = width; - fStemV = min_width; - } - } - } - pFontDesc->SetAtInteger("StemV", fStemV); - AddIndirectObject(pFontDesc); - pFontDict->SetAtReference("FontDescriptor", this, pFontDesc); - CFRelease(traits); - CFRelease(languages); - return LoadFont(pBaseDict); -} -#endif // (_FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_) - -static void _InsertWidthArray1(CFX_Font* pFont, - CFX_UnicodeEncoding* pEncoding, - FX_WCHAR start, - FX_WCHAR end, - CPDF_Array* pWidthArray) { - int size = end - start + 1; - int* widths = FX_Alloc(int, size); - int i; - for (i = 0; i < size; i++) { - int glyph_index = pEncoding->GlyphFromCharCode(start + i); - widths[i] = pFont->GetGlyphWidth(glyph_index); - } - for (i = 1; i < size; i++) - if (widths[i] != *widths) { - break; - } - if (i == size) { - int first = pWidthArray->GetIntegerAt(pWidthArray->GetCount() - 1); - pWidthArray->AddInteger(first + size - 1); - pWidthArray->AddInteger(*widths); - } else { - CPDF_Array* pWidthArray1 = new CPDF_Array; - pWidthArray->Add(pWidthArray1); - for (i = 0; i < size; i++) { - pWidthArray1->AddInteger(widths[i]); - } - } - FX_Free(widths); -} - -CPDF_Font* CPDF_Document::AddFont(CFX_Font* pFont, int charset, FX_BOOL bVert) { - if (!pFont) { - return NULL; - } - bool bCJK = charset == FXFONT_CHINESEBIG5_CHARSET || - charset == FXFONT_GB2312_CHARSET || - charset == FXFONT_HANGEUL_CHARSET || - charset == FXFONT_SHIFTJIS_CHARSET; - CFX_ByteString basefont = pFont->GetFamilyName(); - basefont.Replace(" ", ""); - int flags = 0; - if (pFont->IsBold()) { - flags |= PDFFONT_FORCEBOLD; - } - if (pFont->IsItalic()) { - flags |= PDFFONT_ITALIC; - } - if (pFont->IsFixedWidth()) { - flags |= PDFFONT_FIXEDPITCH; - } - CPDF_Dictionary* pBaseDict = new CPDF_Dictionary; - pBaseDict->SetAtName("Type", "Font"); - std::unique_ptr<CFX_UnicodeEncoding> pEncoding( - new CFX_UnicodeEncoding(pFont)); - CPDF_Dictionary* pFontDict = pBaseDict; - if (!bCJK) { - CPDF_Array* pWidths = new CPDF_Array; - int charcode; - for (charcode = 32; charcode < 128; charcode++) { - int glyph_index = pEncoding->GlyphFromCharCode(charcode); - int char_width = pFont->GetGlyphWidth(glyph_index); - pWidths->AddInteger(char_width); - } - if (charset == FXFONT_ANSI_CHARSET || charset == FXFONT_DEFAULT_CHARSET || - charset == FXFONT_SYMBOL_CHARSET) { - if (charset == FXFONT_SYMBOL_CHARSET) { - flags |= PDFFONT_SYMBOLIC; - } else { - flags |= PDFFONT_NONSYMBOLIC; - } - pBaseDict->SetAtName("Encoding", "WinAnsiEncoding"); - for (charcode = 128; charcode <= 255; charcode++) { - int glyph_index = pEncoding->GlyphFromCharCode(charcode); - int char_width = pFont->GetGlyphWidth(glyph_index); - pWidths->AddInteger(char_width); - } - } else { - flags |= PDFFONT_NONSYMBOLIC; - size_t i; - for (i = 0; i < sizeof g_FX_CharsetUnicodes / sizeof(FX_CharsetUnicodes); - i++) - if (g_FX_CharsetUnicodes[i].m_Charset == charset) { - break; - } - if (i < sizeof g_FX_CharsetUnicodes / sizeof(FX_CharsetUnicodes)) { - CPDF_Dictionary* pEncodingDict = new CPDF_Dictionary; - pEncodingDict->SetAtName("BaseEncoding", "WinAnsiEncoding"); - CPDF_Array* pArray = new CPDF_Array; - pArray->AddInteger(128); - const FX_WCHAR* pUnicodes = g_FX_CharsetUnicodes[i].m_pUnicodes; - for (int j = 0; j < 128; j++) { - CFX_ByteString name = PDF_AdobeNameFromUnicode(pUnicodes[j]); - if (name.IsEmpty()) { - pArray->AddName(".notdef"); - } else { - pArray->AddName(name); - } - int glyph_index = pEncoding->GlyphFromCharCode(pUnicodes[j]); - int char_width = pFont->GetGlyphWidth(glyph_index); - pWidths->AddInteger(char_width); - } - pEncodingDict->SetAt("Differences", pArray); - AddIndirectObject(pEncodingDict); - pBaseDict->SetAtReference("Encoding", this, pEncodingDict); - } - } - if (pFont->IsBold() && pFont->IsItalic()) { - basefont += ",BoldItalic"; - } else if (pFont->IsBold()) { - basefont += ",Bold"; - } else if (pFont->IsItalic()) { - basefont += ",Italic"; - } - pBaseDict->SetAtName("Subtype", "TrueType"); - pBaseDict->SetAtName("BaseFont", basefont); - pBaseDict->SetAtNumber("FirstChar", 32); - pBaseDict->SetAtNumber("LastChar", 255); - pBaseDict->SetAt("Widths", pWidths); - } else { - flags |= PDFFONT_NONSYMBOLIC; - pFontDict = new CPDF_Dictionary; - CFX_ByteString cmap; - CFX_ByteString ordering; - int supplement; - CPDF_Array* pWidthArray = new CPDF_Array; - switch (charset) { - case FXFONT_CHINESEBIG5_CHARSET: - cmap = bVert ? "ETenms-B5-V" : "ETenms-B5-H"; - ordering = "CNS1"; - supplement = 4; - pWidthArray->AddInteger(1); - _InsertWidthArray1(pFont, pEncoding.get(), 0x20, 0x7e, pWidthArray); - break; - case FXFONT_GB2312_CHARSET: - cmap = bVert ? "GBK-EUC-V" : "GBK-EUC-H"; - ordering = "GB1", supplement = 2; - pWidthArray->AddInteger(7716); - _InsertWidthArray1(pFont, pEncoding.get(), 0x20, 0x20, pWidthArray); - pWidthArray->AddInteger(814); - _InsertWidthArray1(pFont, pEncoding.get(), 0x21, 0x7e, pWidthArray); - break; - case FXFONT_HANGEUL_CHARSET: - cmap = bVert ? "KSCms-UHC-V" : "KSCms-UHC-H"; - ordering = "Korea1"; - supplement = 2; - pWidthArray->AddInteger(1); - _InsertWidthArray1(pFont, pEncoding.get(), 0x20, 0x7e, pWidthArray); - break; - case FXFONT_SHIFTJIS_CHARSET: - cmap = bVert ? "90ms-RKSJ-V" : "90ms-RKSJ-H"; - ordering = "Japan1"; - supplement = 5; - pWidthArray->AddInteger(231); - _InsertWidthArray1(pFont, pEncoding.get(), 0x20, 0x7d, pWidthArray); - pWidthArray->AddInteger(326); - _InsertWidthArray1(pFont, pEncoding.get(), 0xa0, 0xa0, pWidthArray); - pWidthArray->AddInteger(327); - _InsertWidthArray1(pFont, pEncoding.get(), 0xa1, 0xdf, pWidthArray); - pWidthArray->AddInteger(631); - _InsertWidthArray1(pFont, pEncoding.get(), 0x7e, 0x7e, pWidthArray); - break; - } - pBaseDict->SetAtName("Subtype", "Type0"); - pBaseDict->SetAtName("BaseFont", basefont); - pBaseDict->SetAtName("Encoding", cmap); - pFontDict->SetAt("W", pWidthArray); - pFontDict->SetAtName("Type", "Font"); - pFontDict->SetAtName("Subtype", "CIDFontType2"); - pFontDict->SetAtName("BaseFont", basefont); - CPDF_Dictionary* pCIDSysInfo = new CPDF_Dictionary; - pCIDSysInfo->SetAtString("Registry", "Adobe"); - pCIDSysInfo->SetAtString("Ordering", ordering); - pCIDSysInfo->SetAtInteger("Supplement", supplement); - pFontDict->SetAt("CIDSystemInfo", pCIDSysInfo); - CPDF_Array* pArray = new CPDF_Array; - pBaseDict->SetAt("DescendantFonts", pArray); - AddIndirectObject(pFontDict); - pArray->AddReference(this, pFontDict); - } - AddIndirectObject(pBaseDict); - CPDF_Dictionary* pFontDesc = new CPDF_Dictionary; - pFontDesc->SetAtName("Type", "FontDescriptor"); - pFontDesc->SetAtName("FontName", basefont); - pFontDesc->SetAtInteger("Flags", flags); - pFontDesc->SetAtInteger( - "ItalicAngle", - pFont->GetSubstFont() ? pFont->GetSubstFont()->m_ItalicAngle : 0); - pFontDesc->SetAtInteger("Ascent", pFont->GetAscent()); - pFontDesc->SetAtInteger("Descent", pFont->GetDescent()); - FX_RECT bbox; - pFont->GetBBox(bbox); - CPDF_Array* pBBox = new CPDF_Array; - pBBox->AddInteger(bbox.left); - pBBox->AddInteger(bbox.bottom); - pBBox->AddInteger(bbox.right); - pBBox->AddInteger(bbox.top); - pFontDesc->SetAt("FontBBox", pBBox); - int32_t nStemV = 0; - if (pFont->GetSubstFont()) { - nStemV = pFont->GetSubstFont()->m_Weight / 5; - } else { - static const FX_CHAR stem_chars[] = {'i', 'I', '!', '1'}; - const size_t count = sizeof(stem_chars) / sizeof(stem_chars[0]); - FX_DWORD glyph = pEncoding->GlyphFromCharCode(stem_chars[0]); - nStemV = pFont->GetGlyphWidth(glyph); - for (size_t i = 1; i < count; i++) { - glyph = pEncoding->GlyphFromCharCode(stem_chars[i]); - int width = pFont->GetGlyphWidth(glyph); - if (width > 0 && width < nStemV) { - nStemV = width; - } - } - } - pFontDesc->SetAtInteger("StemV", nStemV); - AddIndirectObject(pFontDesc); - pFontDict->SetAtReference("FontDescriptor", this, pFontDesc); - return LoadFont(pBaseDict); -} -static int InsertDeletePDFPage(CPDF_Document* pDoc, - CPDF_Dictionary* pPages, - int nPagesToGo, - CPDF_Dictionary* pPage, - FX_BOOL bInsert, - CFX_ArrayTemplate<CPDF_Dictionary*>& stackList) { - CPDF_Array* pKidList = pPages->GetArrayBy("Kids"); - if (!pKidList) { - return -1; - } - int nKids = pKidList->GetCount(); - for (int i = 0; i < nKids; i++) { - CPDF_Dictionary* pKid = pKidList->GetDictAt(i); - if (pKid->GetStringBy("Type") == "Page") { - if (nPagesToGo == 0) { - if (bInsert) { - pKidList->InsertAt(i, new CPDF_Reference(pDoc, pPage->GetObjNum())); - pPage->SetAtReference("Parent", pDoc, pPages->GetObjNum()); - } else { - pKidList->RemoveAt(i); - } - pPages->SetAtInteger( - "Count", pPages->GetIntegerBy("Count") + (bInsert ? 1 : -1)); - return 1; - } - nPagesToGo--; - } else { - int nPages = pKid->GetIntegerBy("Count"); - if (nPagesToGo < nPages) { - int stackCount = stackList.GetSize(); - for (int j = 0; j < stackCount; ++j) { - if (pKid == stackList[j]) { - return -1; - } - } - stackList.Add(pKid); - if (InsertDeletePDFPage(pDoc, pKid, nPagesToGo, pPage, bInsert, - stackList) < 0) { - return -1; - } - stackList.RemoveAt(stackCount); - pPages->SetAtInteger( - "Count", pPages->GetIntegerBy("Count") + (bInsert ? 1 : -1)); - return 1; - } - nPagesToGo -= nPages; - } - } - return 0; -} -static int InsertNewPage(CPDF_Document* pDoc, - int iPage, - CPDF_Dictionary* pPageDict, - CFX_DWordArray& pageList) { - CPDF_Dictionary* pRoot = pDoc->GetRoot(); - if (!pRoot) { - return -1; - } - CPDF_Dictionary* pPages = pRoot->GetDictBy("Pages"); - if (!pPages) { - return -1; - } - int nPages = pDoc->GetPageCount(); - if (iPage < 0 || iPage > nPages) { - return -1; - } - if (iPage == nPages) { - CPDF_Array* pPagesList = pPages->GetArrayBy("Kids"); - if (!pPagesList) { - pPagesList = new CPDF_Array; - pPages->SetAt("Kids", pPagesList); - } - pPagesList->Add(pPageDict, pDoc); - pPages->SetAtInteger("Count", nPages + 1); - pPageDict->SetAtReference("Parent", pDoc, pPages->GetObjNum()); - } else { - CFX_ArrayTemplate<CPDF_Dictionary*> stack; - stack.Add(pPages); - if (InsertDeletePDFPage(pDoc, pPages, iPage, pPageDict, TRUE, stack) < 0) { - return -1; - } - } - pageList.InsertAt(iPage, pPageDict->GetObjNum()); - return iPage; -} -CPDF_Dictionary* CPDF_Document::CreateNewPage(int iPage) { - CPDF_Dictionary* pDict = new CPDF_Dictionary; - pDict->SetAtName("Type", "Page"); - FX_DWORD dwObjNum = AddIndirectObject(pDict); - if (InsertNewPage(this, iPage, pDict, m_PageList) < 0) { - ReleaseIndirectObject(dwObjNum); - return NULL; - } - return pDict; -} - -CPDF_Font* CPDF_Document::AddStandardFont(const FX_CHAR* font, - CPDF_FontEncoding* pEncoding) { - CFX_ByteString name(font); - if (PDF_GetStandardFontName(&name) < 0) - return nullptr; - return GetPageData()->GetStandardFont(name, pEncoding); -} - -void CPDF_Document::DeletePage(int iPage) { - CPDF_Dictionary* pRoot = GetRoot(); - if (!pRoot) { - return; - } - CPDF_Dictionary* pPages = pRoot->GetDictBy("Pages"); - if (!pPages) { - return; - } - int nPages = pPages->GetIntegerBy("Count"); - if (iPage < 0 || iPage >= nPages) { - return; - } - CFX_ArrayTemplate<CPDF_Dictionary*> stack; - stack.Add(pPages); - if (InsertDeletePDFPage(this, pPages, iPage, NULL, FALSE, stack) < 0) { - return; - } - m_PageList.RemoveAt(iPage); -} -CPDF_Object* FPDFAPI_GetPageAttr(CPDF_Dictionary* pPageDict, - const CFX_ByteStringC& name); -void FPDFAPI_FlatPageAttr(CPDF_Dictionary* pPageDict, - const CFX_ByteStringC& name) { - if (pPageDict->KeyExist(name)) { - return; - } - CPDF_Object* pObj = FPDFAPI_GetPageAttr(pPageDict, name); - if (pObj) { - pPageDict->SetAt(name, pObj->Clone()); - } -} diff --git a/core/src/fpdfapi/fpdf_edit/fpdf_edit_image.cpp b/core/src/fpdfapi/fpdf_edit/fpdf_edit_image.cpp deleted file mode 100644 index 47e8367420..0000000000 --- a/core/src/fpdfapi/fpdf_edit/fpdf_edit_image.cpp +++ /dev/null @@ -1,391 +0,0 @@ -// Copyright 2014 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#include "core/include/fpdfapi/cpdf_array.h" -#include "core/include/fpdfapi/cpdf_boolean.h" -#include "core/include/fpdfapi/cpdf_dictionary.h" -#include "core/include/fpdfapi/cpdf_document.h" -#include "core/include/fpdfapi/cpdf_string.h" -#include "core/include/fpdfapi/fpdf_module.h" -#include "core/include/fpdfapi/fpdf_page.h" -#include "core/include/fpdfapi/fpdf_render.h" -#include "core/include/fxcodec/fx_codec.h" -#include "core/src/fpdfapi/fpdf_page/pageint.h" -#include "core/src/fpdfapi/fpdf_render/render_int.h" - -CPDF_Dictionary* CPDF_Image::InitJPEG(uint8_t* pData, FX_DWORD size) { - int32_t width; - int32_t height; - int32_t num_comps; - int32_t bits; - FX_BOOL color_trans; - if (!CPDF_ModuleMgr::Get()->GetJpegModule()->LoadInfo( - pData, size, width, height, num_comps, bits, color_trans)) { - return NULL; - } - CPDF_Dictionary* pDict = new CPDF_Dictionary; - pDict->SetAtName("Type", "XObject"); - pDict->SetAtName("Subtype", "Image"); - pDict->SetAtInteger("Width", width); - pDict->SetAtInteger("Height", height); - const FX_CHAR* csname = NULL; - if (num_comps == 1) { - csname = "DeviceGray"; - } else if (num_comps == 3) { - csname = "DeviceRGB"; - } else if (num_comps == 4) { - csname = "DeviceCMYK"; - CPDF_Array* pDecode = new CPDF_Array; - for (int n = 0; n < 4; n++) { - pDecode->AddInteger(1); - pDecode->AddInteger(0); - } - pDict->SetAt("Decode", pDecode); - } - pDict->SetAtName("ColorSpace", csname); - pDict->SetAtInteger("BitsPerComponent", bits); - pDict->SetAtName("Filter", "DCTDecode"); - if (!color_trans) { - CPDF_Dictionary* pParms = new CPDF_Dictionary; - pDict->SetAt("DecodeParms", pParms); - pParms->SetAtInteger("ColorTransform", 0); - } - m_bIsMask = FALSE; - m_Width = width; - m_Height = height; - if (!m_pStream) { - m_pStream = new CPDF_Stream(NULL, 0, NULL); - } - return pDict; -} -void CPDF_Image::SetJpegImage(uint8_t* pData, FX_DWORD size) { - CPDF_Dictionary* pDict = InitJPEG(pData, size); - if (!pDict) { - return; - } - m_pStream->InitStream(pData, size, pDict); -} -void CPDF_Image::SetJpegImage(IFX_FileRead* pFile) { - FX_DWORD size = (FX_DWORD)pFile->GetSize(); - if (!size) { - return; - } - FX_DWORD dwEstimateSize = size; - if (dwEstimateSize > 8192) { - dwEstimateSize = 8192; - } - uint8_t* pData = FX_Alloc(uint8_t, dwEstimateSize); - pFile->ReadBlock(pData, 0, dwEstimateSize); - CPDF_Dictionary* pDict = InitJPEG(pData, dwEstimateSize); - FX_Free(pData); - if (!pDict && size > dwEstimateSize) { - pData = FX_Alloc(uint8_t, size); - pFile->ReadBlock(pData, 0, size); - pDict = InitJPEG(pData, size); - FX_Free(pData); - } - if (!pDict) { - return; - } - m_pStream->InitStreamFromFile(pFile, pDict); -} -void _DCTEncodeBitmap(CPDF_Dictionary* pBitmapDict, - const CFX_DIBitmap* pBitmap, - int quality, - uint8_t*& buf, - FX_STRSIZE& size) {} -void _JBIG2EncodeBitmap(CPDF_Dictionary* pBitmapDict, - const CFX_DIBitmap* pBitmap, - CPDF_Document* pDoc, - uint8_t*& buf, - FX_STRSIZE& size, - FX_BOOL bLossLess) {} -void CPDF_Image::SetImage(const CFX_DIBitmap* pBitmap, - int32_t iCompress, - IFX_FileWrite* pFileWrite, - IFX_FileRead* pFileRead, - const CFX_DIBitmap* pMask, - const CPDF_ImageSetParam* pParam) { - int32_t BitmapWidth = pBitmap->GetWidth(); - int32_t BitmapHeight = pBitmap->GetHeight(); - if (BitmapWidth < 1 || BitmapHeight < 1) { - return; - } - uint8_t* src_buf = pBitmap->GetBuffer(); - int32_t src_pitch = pBitmap->GetPitch(); - int32_t bpp = pBitmap->GetBPP(); - FX_BOOL bUseMatte = - pParam && pParam->pMatteColor && (pBitmap->GetFormat() == FXDIB_Argb); - CPDF_Dictionary* pDict = new CPDF_Dictionary; - pDict->SetAtName("Type", "XObject"); - pDict->SetAtName("Subtype", "Image"); - pDict->SetAtInteger("Width", BitmapWidth); - pDict->SetAtInteger("Height", BitmapHeight); - uint8_t* dest_buf = NULL; - FX_STRSIZE dest_pitch = 0, dest_size = 0, opType = -1; - if (bpp == 1) { - int32_t reset_a = 0, reset_r = 0, reset_g = 0, reset_b = 0; - int32_t set_a = 0, set_r = 0, set_g = 0, set_b = 0; - if (!pBitmap->IsAlphaMask()) { - ArgbDecode(pBitmap->GetPaletteArgb(0), reset_a, reset_r, reset_g, - reset_b); - ArgbDecode(pBitmap->GetPaletteArgb(1), set_a, set_r, set_g, set_b); - } - if (set_a == 0 || reset_a == 0) { - pDict->SetAt("ImageMask", new CPDF_Boolean(TRUE)); - if (reset_a == 0) { - CPDF_Array* pArray = new CPDF_Array; - pArray->AddInteger(1); - pArray->AddInteger(0); - pDict->SetAt("Decode", pArray); - } - } else { - CPDF_Array* pCS = new CPDF_Array; - pCS->AddName("Indexed"); - pCS->AddName("DeviceRGB"); - pCS->AddInteger(1); - CFX_ByteString ct; - FX_CHAR* pBuf = ct.GetBuffer(6); - pBuf[0] = (FX_CHAR)reset_r; - pBuf[1] = (FX_CHAR)reset_g; - pBuf[2] = (FX_CHAR)reset_b; - pBuf[3] = (FX_CHAR)set_r; - pBuf[4] = (FX_CHAR)set_g; - pBuf[5] = (FX_CHAR)set_b; - ct.ReleaseBuffer(6); - pCS->Add(new CPDF_String(ct, TRUE)); - pDict->SetAt("ColorSpace", pCS); - } - pDict->SetAtInteger("BitsPerComponent", 1); - dest_pitch = (BitmapWidth + 7) / 8; - if ((iCompress & 0x03) == PDF_IMAGE_NO_COMPRESS) { - opType = 1; - } else { - opType = 0; - } - } else if (bpp == 8) { - int32_t iPalette = pBitmap->GetPaletteSize(); - if (iPalette > 0) { - CPDF_Array* pCS = new CPDF_Array; - m_pDocument->AddIndirectObject(pCS); - pCS->AddName("Indexed"); - pCS->AddName("DeviceRGB"); - pCS->AddInteger(iPalette - 1); - uint8_t* pColorTable = FX_Alloc2D(uint8_t, iPalette, 3); - uint8_t* ptr = pColorTable; - for (int32_t i = 0; i < iPalette; i++) { - FX_DWORD argb = pBitmap->GetPaletteArgb(i); - ptr[0] = (uint8_t)(argb >> 16); - ptr[1] = (uint8_t)(argb >> 8); - ptr[2] = (uint8_t)argb; - ptr += 3; - } - CPDF_Stream* pCTS = - new CPDF_Stream(pColorTable, iPalette * 3, new CPDF_Dictionary); - m_pDocument->AddIndirectObject(pCTS); - pCS->AddReference(m_pDocument, pCTS); - pDict->SetAtReference("ColorSpace", m_pDocument, pCS); - } else { - pDict->SetAtName("ColorSpace", "DeviceGray"); - } - pDict->SetAtInteger("BitsPerComponent", 8); - if ((iCompress & 0x03) == PDF_IMAGE_NO_COMPRESS) { - dest_pitch = BitmapWidth; - opType = 1; - } else { - opType = 0; - } - } else { - pDict->SetAtName("ColorSpace", "DeviceRGB"); - pDict->SetAtInteger("BitsPerComponent", 8); - if ((iCompress & 0x03) == PDF_IMAGE_NO_COMPRESS) { - dest_pitch = BitmapWidth * 3; - opType = 2; - } else { - opType = 0; - } - } - const CFX_DIBitmap* pMaskBitmap = NULL; - FX_BOOL bDeleteMask = FALSE; - if (pBitmap->HasAlpha()) { - pMaskBitmap = pBitmap->GetAlphaMask(); - bDeleteMask = TRUE; - } - if (!pMaskBitmap && pMask) { - FXDIB_Format maskFormat = pMask->GetFormat(); - if (maskFormat == FXDIB_1bppMask || maskFormat == FXDIB_8bppMask) { - pMaskBitmap = pMask; - } - } - if (pMaskBitmap) { - int32_t maskWidth = pMaskBitmap->GetWidth(); - int32_t maskHeight = pMaskBitmap->GetHeight(); - uint8_t* mask_buf = NULL; - FX_STRSIZE mask_size = 0; - CPDF_Dictionary* pMaskDict = new CPDF_Dictionary; - pMaskDict->SetAtName("Type", "XObject"); - pMaskDict->SetAtName("Subtype", "Image"); - pMaskDict->SetAtInteger("Width", maskWidth); - pMaskDict->SetAtInteger("Height", maskHeight); - pMaskDict->SetAtName("ColorSpace", "DeviceGray"); - pMaskDict->SetAtInteger("BitsPerComponent", 8); - if (pMaskBitmap->GetBPP() == 8 && - (iCompress & PDF_IMAGE_MASK_LOSSY_COMPRESS) != 0) { - _DCTEncodeBitmap(pMaskDict, pMaskBitmap, pParam ? pParam->nQuality : 75, - mask_buf, mask_size); - } else if (pMaskBitmap->GetFormat() == FXDIB_1bppMask) { - _JBIG2EncodeBitmap(pMaskDict, pMaskBitmap, m_pDocument, mask_buf, - mask_size, TRUE); - } else { - mask_buf = FX_Alloc2D(uint8_t, maskHeight, maskWidth); - mask_size = maskHeight * maskWidth; // Safe since checked alloc returned. - for (int32_t a = 0; a < maskHeight; a++) { - FXSYS_memcpy(mask_buf + a * maskWidth, pMaskBitmap->GetScanline(a), - maskWidth); - } - } - pMaskDict->SetAtInteger("Length", mask_size); - if (bUseMatte) { - int a, r, g, b; - ArgbDecode(*(pParam->pMatteColor), a, r, g, b); - CPDF_Array* pMatte = new CPDF_Array; - pMatte->AddInteger(r); - pMatte->AddInteger(g); - pMatte->AddInteger(b); - pMaskDict->SetAt("Matte", pMatte); - } - CPDF_Stream* pMaskStream = new CPDF_Stream(mask_buf, mask_size, pMaskDict); - m_pDocument->AddIndirectObject(pMaskStream); - pDict->SetAtReference("SMask", m_pDocument, pMaskStream); - if (bDeleteMask) { - delete pMaskBitmap; - } - } - FX_BOOL bStream = pFileWrite && pFileRead; - if (opType == 0) { - if (iCompress & PDF_IMAGE_LOSSLESS_COMPRESS) { - if (pBitmap->GetBPP() == 1) { - _JBIG2EncodeBitmap(pDict, pBitmap, m_pDocument, dest_buf, dest_size, - TRUE); - } - } else { - if (pBitmap->GetBPP() == 1) { - _JBIG2EncodeBitmap(pDict, pBitmap, m_pDocument, dest_buf, dest_size, - FALSE); - } else if (pBitmap->GetBPP() >= 8 && pBitmap->GetPalette()) { - CFX_DIBitmap* pNewBitmap = new CFX_DIBitmap(); - pNewBitmap->Copy(pBitmap); - pNewBitmap->ConvertFormat(FXDIB_Rgb); - SetImage(pNewBitmap, iCompress, pFileWrite, pFileRead); - if (pDict) { - pDict->Release(); - pDict = NULL; - } - FX_Free(dest_buf); - dest_buf = NULL; - dest_size = 0; - delete pNewBitmap; - return; - } else { - if (bUseMatte) { - CFX_DIBitmap* pNewBitmap = new CFX_DIBitmap(); - pNewBitmap->Create(BitmapWidth, BitmapHeight, FXDIB_Argb); - uint8_t* dst_buf = pNewBitmap->GetBuffer(); - int32_t src_offset = 0; - for (int32_t row = 0; row < BitmapHeight; row++) { - src_offset = row * src_pitch; - for (int32_t column = 0; column < BitmapWidth; column++) { - FX_FLOAT alpha = src_buf[src_offset + 3] / 255.0f; - dst_buf[src_offset] = (uint8_t)(src_buf[src_offset] * alpha); - dst_buf[src_offset + 1] = - (uint8_t)(src_buf[src_offset + 1] * alpha); - dst_buf[src_offset + 2] = - (uint8_t)(src_buf[src_offset + 2] * alpha); - dst_buf[src_offset + 3] = (uint8_t)(src_buf[src_offset + 3]); - src_offset += 4; - } - } - _DCTEncodeBitmap(pDict, pNewBitmap, pParam ? pParam->nQuality : 75, - dest_buf, dest_size); - delete pNewBitmap; - } else { - _DCTEncodeBitmap(pDict, pBitmap, pParam ? pParam->nQuality : 75, - dest_buf, dest_size); - } - } - } - if (bStream) { - pFileWrite->WriteBlock(dest_buf, dest_size); - FX_Free(dest_buf); - dest_buf = NULL; - } - } else if (opType == 1) { - if (!bStream) { - dest_buf = FX_Alloc2D(uint8_t, dest_pitch, BitmapHeight); - dest_size = - dest_pitch * BitmapHeight; // Safe since checked alloc returned. - } - uint8_t* pDest = dest_buf; - for (int32_t i = 0; i < BitmapHeight; i++) { - if (!bStream) { - FXSYS_memcpy(pDest, src_buf, dest_pitch); - pDest += dest_pitch; - } else { - pFileWrite->WriteBlock(src_buf, dest_pitch); - } - src_buf += src_pitch; - } - } else if (opType == 2) { - if (!bStream) { - dest_buf = FX_Alloc2D(uint8_t, dest_pitch, BitmapHeight); - dest_size = - dest_pitch * BitmapHeight; // Safe since checked alloc returned. - } else { - dest_buf = FX_Alloc(uint8_t, dest_pitch); - } - uint8_t* pDest = dest_buf; - int32_t src_offset = 0; - int32_t dest_offset = 0; - for (int32_t row = 0; row < BitmapHeight; row++) { - src_offset = row * src_pitch; - for (int32_t column = 0; column < BitmapWidth; column++) { - FX_FLOAT alpha = bUseMatte ? src_buf[src_offset + 3] / 255.0f : 1; - pDest[dest_offset] = (uint8_t)(src_buf[src_offset + 2] * alpha); - pDest[dest_offset + 1] = (uint8_t)(src_buf[src_offset + 1] * alpha); - pDest[dest_offset + 2] = (uint8_t)(src_buf[src_offset] * alpha); - dest_offset += 3; - src_offset += bpp == 24 ? 3 : 4; - } - if (bStream) { - pFileWrite->WriteBlock(pDest, dest_pitch); - pDest = dest_buf; - } else { - pDest += dest_pitch; - } - dest_offset = 0; - } - if (bStream) { - FX_Free(dest_buf); - dest_buf = NULL; - } - } - if (!m_pStream) { - m_pStream = new CPDF_Stream(NULL, 0, NULL); - } - if (!bStream) { - m_pStream->InitStream(dest_buf, dest_size, pDict); - } else { - pFileWrite->Flush(); - m_pStream->InitStreamFromFile(pFileRead, pDict); - } - m_bIsMask = pBitmap->IsAlphaMask(); - m_Width = BitmapWidth; - m_Height = BitmapHeight; - FX_Free(dest_buf); -} -void CPDF_Image::ResetCache(CPDF_Page* pPage, const CFX_DIBitmap* pBitmap) { - pPage->GetRenderCache()->ResetBitmap(m_pStream, pBitmap); -} |