summaryrefslogtreecommitdiff
path: root/core/src/fpdfapi/fpdf_edit
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/fpdfapi/fpdf_edit')
-rw-r--r--core/src/fpdfapi/fpdf_edit/editint.h118
-rw-r--r--core/src/fpdfapi/fpdf_edit/fpdf_edit_content.cpp328
-rw-r--r--core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp4262
-rw-r--r--core/src/fpdfapi/fpdf_edit/fpdf_edit_doc.cpp2410
-rw-r--r--core/src/fpdfapi/fpdf_edit/fpdf_edit_image.cpp748
5 files changed, 3933 insertions, 3933 deletions
diff --git a/core/src/fpdfapi/fpdf_edit/editint.h b/core/src/fpdfapi/fpdf_edit/editint.h
index bceba1a745..6346f06136 100644
--- a/core/src/fpdfapi/fpdf_edit/editint.h
+++ b/core/src/fpdfapi/fpdf_edit/editint.h
@@ -1,59 +1,59 @@
-// 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 _FPDF_EDITINT_
-#define _FPDF_EDITINT_
-class CPDF_ObjectStream : public CFX_Object
-{
-public:
- CPDF_ObjectStream();
-
- FX_BOOL Start();
-
- FX_INT32 CompressIndirectObject(FX_DWORD dwObjNum, const CPDF_Object *pObj);
- FX_INT32 CompressIndirectObject(FX_DWORD dwObjNum, FX_LPCBYTE pBuffer, FX_DWORD dwSize);
-
- FX_FILESIZE End(CPDF_Creator* pCreator);
-
- CFX_DWordArray m_ObjNumArray;
-
- CFX_ByteTextBuf m_Buffer;
- FX_DWORD m_dwObjNum;
- FX_INT32 m_index;
-protected:
-
- CFX_DWordArray m_OffsetArray;
-};
-class CPDF_XRefStream : public CFX_Object
-{
-public:
-
- CPDF_XRefStream();
-
- FX_BOOL Start();
-
- FX_INT32 CompressIndirectObject(FX_DWORD dwObjNum, const CPDF_Object *pObj, CPDF_Creator *pCreator);
-
- FX_INT32 CompressIndirectObject(FX_DWORD dwObjNum, FX_LPCBYTE pBuffer, FX_DWORD dwSize, CPDF_Creator *pCreator);
-
- FX_BOOL End(CPDF_Creator *pCreator, FX_BOOL bEOF = FALSE);
- FX_BOOL AddObjectNumberToIndexArray(FX_DWORD objnum);
- FX_BOOL EndXRefStream(CPDF_Creator* pCreator);
-
-
- CFX_DWordArray m_IndexArray;
-
- FX_FILESIZE m_PrevOffset;
- FX_DWORD m_dwTempObjNum;
-
-protected:
- FX_INT32 EndObjectStream(CPDF_Creator *pCreator, FX_BOOL bEOF = TRUE);
- FX_BOOL GenerateXRefStream(CPDF_Creator* pCreator, FX_BOOL bEOF);
- FX_INT32 m_iSeg;
- CPDF_ObjectStream m_ObjStream;
- CFX_ByteTextBuf m_Buffer;
-};
-#endif
+// 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 _FPDF_EDITINT_
+#define _FPDF_EDITINT_
+class CPDF_ObjectStream : public CFX_Object
+{
+public:
+ CPDF_ObjectStream();
+
+ FX_BOOL Start();
+
+ FX_INT32 CompressIndirectObject(FX_DWORD dwObjNum, const CPDF_Object *pObj);
+ FX_INT32 CompressIndirectObject(FX_DWORD dwObjNum, FX_LPCBYTE pBuffer, FX_DWORD dwSize);
+
+ FX_FILESIZE End(CPDF_Creator* pCreator);
+
+ CFX_DWordArray m_ObjNumArray;
+
+ CFX_ByteTextBuf m_Buffer;
+ FX_DWORD m_dwObjNum;
+ FX_INT32 m_index;
+protected:
+
+ CFX_DWordArray m_OffsetArray;
+};
+class CPDF_XRefStream : public CFX_Object
+{
+public:
+
+ CPDF_XRefStream();
+
+ FX_BOOL Start();
+
+ FX_INT32 CompressIndirectObject(FX_DWORD dwObjNum, const CPDF_Object *pObj, CPDF_Creator *pCreator);
+
+ FX_INT32 CompressIndirectObject(FX_DWORD dwObjNum, FX_LPCBYTE pBuffer, FX_DWORD dwSize, CPDF_Creator *pCreator);
+
+ FX_BOOL End(CPDF_Creator *pCreator, FX_BOOL bEOF = FALSE);
+ FX_BOOL AddObjectNumberToIndexArray(FX_DWORD objnum);
+ FX_BOOL EndXRefStream(CPDF_Creator* pCreator);
+
+
+ CFX_DWordArray m_IndexArray;
+
+ FX_FILESIZE m_PrevOffset;
+ FX_DWORD m_dwTempObjNum;
+
+protected:
+ FX_INT32 EndObjectStream(CPDF_Creator *pCreator, FX_BOOL bEOF = TRUE);
+ FX_BOOL GenerateXRefStream(CPDF_Creator* pCreator, FX_BOOL bEOF);
+ FX_INT32 m_iSeg;
+ CPDF_ObjectStream m_ObjStream;
+ CFX_ByteTextBuf m_Buffer;
+};
+#endif
diff --git a/core/src/fpdfapi/fpdf_edit/fpdf_edit_content.cpp b/core/src/fpdfapi/fpdf_edit/fpdf_edit_content.cpp
index 798c77261b..243c23fabf 100644
--- a/core/src/fpdfapi/fpdf_edit/fpdf_edit_content.cpp
+++ b/core/src/fpdfapi/fpdf_edit/fpdf_edit_content.cpp
@@ -1,164 +1,164 @@
-// 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 "../../../include/fpdfapi/fpdf_page.h"
-#include "../../../include/fpdfapi/fpdf_serial.h"
-#include "../../../include/fpdfapi/fpdf_module.h"
-#include "../../../include/fxcodec/fx_codec.h"
-#include "../fpdf_page/pageint.h"
-CFX_ByteTextBuf& operator << (CFX_ByteTextBuf& ar, CFX_AffineMatrix& matrix)
-{
- ar << matrix.a << " " << matrix.b << " " << matrix.c << " " << matrix.d << " " << matrix.e << " " << matrix.f;
- return ar;
-}
-CPDF_PageContentGenerate::CPDF_PageContentGenerate(CPDF_Page* pPage) : m_pPage(pPage)
-{
- m_pDocument = NULL;
- if (m_pPage) {
- m_pDocument = m_pPage->m_pDocument;
- }
-}
-CPDF_PageContentGenerate::~CPDF_PageContentGenerate()
-{
- for (int i = 0; i < m_pageObjects.GetSize(); ++i) {
- CPDF_PageObject* pPageObj = (CPDF_PageObject*)m_pageObjects[i];
- if (pPageObj) {
- pPageObj->Release();
- }
- }
-}
-FX_BOOL CPDF_PageContentGenerate::InsertPageObject(CPDF_PageObject* pPageObject)
-{
- if (!pPageObject) {
- return FALSE;
- }
- return m_pageObjects.Add(pPageObject);
-}
-void CPDF_PageContentGenerate::GenerateContent()
-{
- CFX_ByteTextBuf buf;
- CPDF_Dictionary* pPageDict = m_pPage->m_pFormDict;
- for (int i = 0; i < m_pageObjects.GetSize(); ++i) {
- CPDF_PageObject* pPageObj = (CPDF_PageObject*)m_pageObjects[i];
- if (!pPageObj || pPageObj->m_Type != PDFPAGE_IMAGE) {
- continue;
- }
- ProcessImage(buf, (CPDF_ImageObject*)pPageObj);
- }
- CPDF_Object* pContent = pPageDict->GetElementValue("Contents");
- if (pContent != NULL) {
- pPageDict->RemoveAt("Contents");
- }
- CPDF_Stream* pStream = FX_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_PageContentGenerate::RealizeResource(CPDF_Object* pResourceObj, const FX_CHAR* szType)
-{
- if (m_pPage->m_pResources == NULL) {
- m_pPage->m_pResources = FX_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->GetDict(szType);
- if (pResList == NULL) {
- pResList = FX_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_PageContentGenerate::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) {
- pImageObj->m_pImage->Release();
- pImageObj->m_pImage = m_pDocument->GetPageData()->GetImage(pStream);
- }
- buf << "/" << PDF_NameEncode(name) << " Do Q\n";
- }
-}
-void CPDF_PageContentGenerate::ProcessForm(CFX_ByteTextBuf& buf, FX_LPCBYTE data, FX_DWORD size, CFX_Matrix& matrix)
-{
- if (!data || !size) {
- return;
- }
- CPDF_Stream* pStream = FX_NEW CPDF_Stream(NULL, 0, NULL);
- CPDF_Dictionary* pFormDict = CPDF_Dictionary::Create();
- pFormDict->SetAtName(FX_BSTR("Type"), FX_BSTR("XObject"));
- pFormDict->SetAtName(FX_BSTR("Subtype"), FX_BSTR("Form"));
- CFX_FloatRect bbox = m_pPage->GetPageBBox();
- matrix.TransformRect(bbox);
- pFormDict->SetAtRect(FX_BSTR("BBox"), bbox);
- pStream->InitStream((FX_LPBYTE)data, size, pFormDict);
- buf << "q " << matrix << " cm ";
- CFX_ByteString name = RealizeResource(pStream, "XObject");
- buf << "/" << PDF_NameEncode(name) << " Do Q\n";
-}
-void CPDF_PageContentGenerate::TransformContent(CFX_Matrix& matrix)
-{
- CPDF_Object* pContent = m_pPage->m_pFormDict->GetElementValue("Contents");
- if (!pContent) {
- return;
- }
- CFX_ByteTextBuf buf;
- int type = pContent->GetType();
- if (type == PDFOBJ_ARRAY) {
- CPDF_Array* pArray = (CPDF_Array*)pContent;
- int iCount = pArray->GetCount();
- CPDF_StreamAcc** pContentArray = (CPDF_StreamAcc**)FX_Alloc(CPDF_StreamAcc*, iCount);
- int size = 0;
- int i = 0;
- for (i = 0; i < iCount; ++i) {
- pContent = pArray->GetElement(i);
- if (!pContent || pContent->GetType() != PDFOBJ_STREAM) {
- continue;
- }
- CPDF_StreamAcc* pStream = FX_NEW CPDF_StreamAcc();
- pStream->LoadAllData((CPDF_Stream*)pContent);
- pContentArray[i] = pStream;
- size += pContentArray[i]->GetSize() + 1;
- }
- int pos = 0;
- FX_LPBYTE pBuf = FX_Alloc(FX_BYTE, size);
- for (i = 0; i < iCount; ++i) {
- FXSYS_memcpy32(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 (type == PDFOBJ_STREAM) {
- CPDF_StreamAcc contentStream;
- contentStream.LoadAllData((CPDF_Stream*)pContent);
- ProcessForm(buf, contentStream.GetData(), contentStream.GetSize(), matrix);
- }
- CPDF_Stream* pStream = FX_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());
-}
+// 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 "../../../include/fpdfapi/fpdf_page.h"
+#include "../../../include/fpdfapi/fpdf_serial.h"
+#include "../../../include/fpdfapi/fpdf_module.h"
+#include "../../../include/fxcodec/fx_codec.h"
+#include "../fpdf_page/pageint.h"
+CFX_ByteTextBuf& operator << (CFX_ByteTextBuf& ar, CFX_AffineMatrix& matrix)
+{
+ ar << matrix.a << " " << matrix.b << " " << matrix.c << " " << matrix.d << " " << matrix.e << " " << matrix.f;
+ return ar;
+}
+CPDF_PageContentGenerate::CPDF_PageContentGenerate(CPDF_Page* pPage) : m_pPage(pPage)
+{
+ m_pDocument = NULL;
+ if (m_pPage) {
+ m_pDocument = m_pPage->m_pDocument;
+ }
+}
+CPDF_PageContentGenerate::~CPDF_PageContentGenerate()
+{
+ for (int i = 0; i < m_pageObjects.GetSize(); ++i) {
+ CPDF_PageObject* pPageObj = (CPDF_PageObject*)m_pageObjects[i];
+ if (pPageObj) {
+ pPageObj->Release();
+ }
+ }
+}
+FX_BOOL CPDF_PageContentGenerate::InsertPageObject(CPDF_PageObject* pPageObject)
+{
+ if (!pPageObject) {
+ return FALSE;
+ }
+ return m_pageObjects.Add(pPageObject);
+}
+void CPDF_PageContentGenerate::GenerateContent()
+{
+ CFX_ByteTextBuf buf;
+ CPDF_Dictionary* pPageDict = m_pPage->m_pFormDict;
+ for (int i = 0; i < m_pageObjects.GetSize(); ++i) {
+ CPDF_PageObject* pPageObj = (CPDF_PageObject*)m_pageObjects[i];
+ if (!pPageObj || pPageObj->m_Type != PDFPAGE_IMAGE) {
+ continue;
+ }
+ ProcessImage(buf, (CPDF_ImageObject*)pPageObj);
+ }
+ CPDF_Object* pContent = pPageDict->GetElementValue("Contents");
+ if (pContent != NULL) {
+ pPageDict->RemoveAt("Contents");
+ }
+ CPDF_Stream* pStream = FX_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_PageContentGenerate::RealizeResource(CPDF_Object* pResourceObj, const FX_CHAR* szType)
+{
+ if (m_pPage->m_pResources == NULL) {
+ m_pPage->m_pResources = FX_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->GetDict(szType);
+ if (pResList == NULL) {
+ pResList = FX_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_PageContentGenerate::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) {
+ pImageObj->m_pImage->Release();
+ pImageObj->m_pImage = m_pDocument->GetPageData()->GetImage(pStream);
+ }
+ buf << "/" << PDF_NameEncode(name) << " Do Q\n";
+ }
+}
+void CPDF_PageContentGenerate::ProcessForm(CFX_ByteTextBuf& buf, FX_LPCBYTE data, FX_DWORD size, CFX_Matrix& matrix)
+{
+ if (!data || !size) {
+ return;
+ }
+ CPDF_Stream* pStream = FX_NEW CPDF_Stream(NULL, 0, NULL);
+ CPDF_Dictionary* pFormDict = CPDF_Dictionary::Create();
+ pFormDict->SetAtName(FX_BSTR("Type"), FX_BSTR("XObject"));
+ pFormDict->SetAtName(FX_BSTR("Subtype"), FX_BSTR("Form"));
+ CFX_FloatRect bbox = m_pPage->GetPageBBox();
+ matrix.TransformRect(bbox);
+ pFormDict->SetAtRect(FX_BSTR("BBox"), bbox);
+ pStream->InitStream((FX_LPBYTE)data, size, pFormDict);
+ buf << "q " << matrix << " cm ";
+ CFX_ByteString name = RealizeResource(pStream, "XObject");
+ buf << "/" << PDF_NameEncode(name) << " Do Q\n";
+}
+void CPDF_PageContentGenerate::TransformContent(CFX_Matrix& matrix)
+{
+ CPDF_Object* pContent = m_pPage->m_pFormDict->GetElementValue("Contents");
+ if (!pContent) {
+ return;
+ }
+ CFX_ByteTextBuf buf;
+ int type = pContent->GetType();
+ if (type == PDFOBJ_ARRAY) {
+ CPDF_Array* pArray = (CPDF_Array*)pContent;
+ int iCount = pArray->GetCount();
+ CPDF_StreamAcc** pContentArray = (CPDF_StreamAcc**)FX_Alloc(CPDF_StreamAcc*, iCount);
+ int size = 0;
+ int i = 0;
+ for (i = 0; i < iCount; ++i) {
+ pContent = pArray->GetElement(i);
+ if (!pContent || pContent->GetType() != PDFOBJ_STREAM) {
+ continue;
+ }
+ CPDF_StreamAcc* pStream = FX_NEW CPDF_StreamAcc();
+ pStream->LoadAllData((CPDF_Stream*)pContent);
+ pContentArray[i] = pStream;
+ size += pContentArray[i]->GetSize() + 1;
+ }
+ int pos = 0;
+ FX_LPBYTE pBuf = FX_Alloc(FX_BYTE, size);
+ for (i = 0; i < iCount; ++i) {
+ FXSYS_memcpy32(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 (type == PDFOBJ_STREAM) {
+ CPDF_StreamAcc contentStream;
+ contentStream.LoadAllData((CPDF_Stream*)pContent);
+ ProcessForm(buf, contentStream.GetData(), contentStream.GetSize(), matrix);
+ }
+ CPDF_Stream* pStream = FX_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
index 4ec3aa6e5e..e5bb1cf985 100644
--- a/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp
+++ b/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp
@@ -1,2131 +1,2131 @@
-// 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 "../../../include/fpdfapi/fpdf_serial.h"
-#include "editint.h"
-#define PDF_OBJECTSTREAM_MAXLENGTH (256 * 1024)
-#define PDF_XREFSTREAM_MAXSIZE 10000
-extern void FlateEncode(const FX_BYTE* src_buf, FX_DWORD src_data, FX_LPBYTE& dest_buf, FX_DWORD& dest_size);
-extern void FlateEncode(FX_LPCBYTE src_buf, FX_DWORD src_size, int predictor, int Colors, int BitsPerComponent, int Columns,
- FX_LPBYTE& dest_buf, FX_DWORD& dest_size);
-extern FX_BOOL IsSignatureDict(const CPDF_Dictionary* pDict);
-FX_INT32 PDF_CreatorAppendObject(const CPDF_Object* pObj, CFX_FileBufferArchive *pFile, FX_FILESIZE& offset)
-{
- FX_INT32 len = 0;
- if (pObj == NULL) {
- if (pFile->AppendString(FX_BSTRC(" null")) < 0) {
- return -1;
- }
- offset += 5;
- return 1;
- }
- switch (pObj->GetType()) {
- case PDFOBJ_NULL:
- if (pFile->AppendString(FX_BSTRC(" null")) < 0) {
- return -1;
- }
- offset += 5;
- break;
- case PDFOBJ_BOOLEAN:
- case PDFOBJ_NUMBER:
- if (pFile->AppendString(FX_BSTRC(" ")) < 0) {
- return -1;
- }
- if ((len = pFile->AppendString(pObj->GetString())) < 0) {
- return -1;
- }
- offset += len + 1;
- break;
- case PDFOBJ_STRING: {
- CFX_ByteString str = pObj->GetString();
- FX_BOOL bHex = ((CPDF_String*)pObj)->IsHex();
- if ((len = pFile->AppendString(PDF_EncodeString(str, bHex))) < 0) {
- return -1;
- }
- offset += len;
- break;
- }
- case PDFOBJ_NAME: {
- if (pFile->AppendString(FX_BSTRC("/")) < 0) {
- return -1;
- }
- CFX_ByteString str = pObj->GetString();
- if ((len = pFile->AppendString(PDF_NameEncode(str))) < 0) {
- return -1;
- }
- offset += len + 1;
- break;
- }
- case PDFOBJ_REFERENCE: {
- if (pFile->AppendString(FX_BSTRC(" ")) < 0) {
- return -1;
- }
- CPDF_Reference* p = (CPDF_Reference*)pObj;
- if ((len = pFile->AppendDWord(p->GetRefObjNum())) < 0) {
- return -1;
- }
- if (pFile->AppendString(FX_BSTRC(" 0 R ")) < 0) {
- return -1;
- }
- offset += len + 6;
- break;
- }
- case PDFOBJ_ARRAY: {
- if (pFile->AppendString(FX_BSTRC("[")) < 0) {
- return -1;
- }
- offset += 1;
- CPDF_Array* p = (CPDF_Array*)pObj;
- for (FX_DWORD i = 0; i < p->GetCount(); i ++) {
- CPDF_Object* pElement = p->GetElement(i);
- if (pElement->GetObjNum()) {
- if (pFile->AppendString(FX_BSTRC(" ")) < 0) {
- return -1;
- }
- if ((len = pFile->AppendDWord(pElement->GetObjNum())) < 0) {
- return -1;
- }
- if (pFile->AppendString(FX_BSTRC(" 0 R")) < 0) {
- return -1;
- }
- offset += len + 5;
- } else {
- if (PDF_CreatorAppendObject(pElement, pFile, offset) < 0) {
- return -1;
- }
- }
- }
- if (pFile->AppendString(FX_BSTRC("]")) < 0) {
- return -1;
- }
- offset += 1;
- break;
- }
- case PDFOBJ_DICTIONARY: {
- if (pFile->AppendString(FX_BSTRC("<<")) < 0) {
- return -1;
- }
- offset += 2;
- CPDF_Dictionary* p = (CPDF_Dictionary*)pObj;
- FX_POSITION pos = p->GetStartPos();
- while (pos) {
- CFX_ByteString key;
- CPDF_Object* pValue = p->GetNextElement(pos, key);
- if (pFile->AppendString(FX_BSTRC("/")) < 0) {
- return -1;
- }
- if ((len = pFile->AppendString(PDF_NameEncode(key))) < 0) {
- return -1;
- }
- offset += len + 1;
- if (pValue->GetObjNum()) {
- if (pFile->AppendString(FX_BSTRC(" ")) < 0) {
- return -1;
- }
- if ((len = pFile->AppendDWord(pValue->GetObjNum())) < 0) {
- return -1;
- }
- if (pFile->AppendString(FX_BSTRC(" 0 R")) < 0) {
- return -1;
- }
- offset += len + 5;
- } else {
- if (PDF_CreatorAppendObject(pValue, pFile, offset) < 0) {
- return -1;
- }
- }
- }
- if (pFile->AppendString(FX_BSTRC(">>")) < 0) {
- return -1;
- }
- offset += 2;
- break;
- }
- case PDFOBJ_STREAM: {
- CPDF_Stream* p = (CPDF_Stream*)pObj;
- if (PDF_CreatorAppendObject(p->GetDict(), pFile, offset) < 0) {
- return -1;
- }
- if (pFile->AppendString(FX_BSTRC("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(FX_BSTRC("\r\nendstream"))) < 0) {
- return -1;
- }
- offset += len;
- break;
- }
- default:
- ASSERT(FALSE);
- break;
- }
- return 1;
-}
-FX_INT32 PDF_CreatorWriteTrailer(CPDF_Document* pDocument, CFX_FileBufferArchive* pFile, CPDF_Array* pIDArray, FX_BOOL bCompress)
-{
- FX_FILESIZE offset = 0;
- FX_INT32 len = 0;
- FXSYS_assert(pDocument && pFile);
- CPDF_Parser *pParser = (CPDF_Parser*)pDocument->GetParser();
- if (pParser) {
- CPDF_Dictionary* p = pParser->GetTrailer();
- FX_POSITION pos = p->GetStartPos();
- while (pos) {
- CFX_ByteString key;
- CPDF_Object* pValue = p->GetNextElement(pos, key);
- if (key == FX_BSTRC("Encrypt") || key == FX_BSTRC("Size") || key == FX_BSTRC("Filter") ||
- key == FX_BSTRC("Index") || key == FX_BSTRC("Length") || key == FX_BSTRC("Prev") ||
- key == FX_BSTRC("W") || key == FX_BSTRC("XRefStm") || key == FX_BSTRC("Type") || key == FX_BSTRC("ID")) {
- continue;
- }
- if (bCompress && key == FX_BSTRC("DecodeParms")) {
- continue;
- }
- if (pFile->AppendString((FX_BSTRC("/"))) < 0) {
- return -1;
- }
- if ((len = pFile->AppendString(PDF_NameEncode(key))) < 0) {
- return -1;
- }
- offset += len + 1;
- if (pValue->GetObjNum()) {
- if (pFile->AppendString(FX_BSTRC(" ")) < 0) {
- return -1;
- }
- if ((len = pFile->AppendDWord(pValue->GetObjNum())) < 0) {
- return -1;
- }
- if (pFile->AppendString(FX_BSTRC(" 0 R ")) < 0) {
- return -1;
- }
- offset += len + 6;
- } else {
- if (PDF_CreatorAppendObject(pValue, pFile, offset) < 0) {
- return -1;
- }
- }
- }
- if (pIDArray) {
- if (pFile->AppendString((FX_BSTRC("/ID"))) < 0) {
- return -1;
- }
- offset += 3;
- if (PDF_CreatorAppendObject(pIDArray, pFile, offset) < 0) {
- return -1;
- }
- }
- return offset;
- }
- if (pFile->AppendString(FX_BSTRC("\r\n/Root ")) < 0) {
- return -1;
- }
- if ((len = pFile->AppendDWord(pDocument->GetRoot()->GetObjNum())) < 0) {
- return -1;
- }
- if (pFile->AppendString(FX_BSTRC(" 0 R\r\n")) < 0) {
- return -1;
- }
- offset += len + 14;
- if (pDocument->GetInfo()) {
- if (pFile->AppendString(FX_BSTRC("/Info ")) < 0) {
- return -1;
- }
- if ((len = pFile->AppendDWord(pDocument->GetInfo()->GetObjNum())) < 0) {
- return -1;
- }
- if (pFile->AppendString(FX_BSTRC(" 0 R\r\n")) < 0) {
- return -1;
- }
- offset += len + 12;
- }
- if (pIDArray) {
- if (pFile->AppendString((FX_BSTRC("/ID"))) < 0) {
- return -1;
- }
- offset += 3;
- if (PDF_CreatorAppendObject(pIDArray, pFile, offset) < 0) {
- return -1;
- }
- }
- return offset;
-}
-FX_INT32 PDF_CreatorWriteEncrypt(const CPDF_Dictionary* pEncryptDict, FX_DWORD dwObjNum, CFX_FileBufferArchive *pFile)
-{
- if (!pEncryptDict) {
- return 0;
- }
- FXSYS_assert(pFile);
- FX_FILESIZE offset = 0;
- FX_INT32 len = 0;
- if (pFile->AppendString(FX_BSTRC("/Encrypt")) < 0) {
- return -1;
- }
- offset += 8;
- if (pFile->AppendString(FX_BSTRC(" ")) < 0) {
- return -1;
- }
- if ((len = pFile->AppendDWord(dwObjNum)) < 0) {
- return -1;
- }
- if (pFile->AppendString(FX_BSTRC(" 0 R ")) < 0) {
- return -1;
- }
- offset += len + 6;
- return offset;
-}
-FX_BOOL PDF_GenerateFileID(FX_DWORD dwSeed1, FX_DWORD dwSeed2, FX_LPDWORD pBuffer)
-{
- if (!pBuffer) {
- return FALSE;
- }
- FX_LPVOID pContext = FX_Random_MT_Start(dwSeed1);
- FX_INT32 i = 0;
- for (i = 0; i < 2; i++) {
- *pBuffer++ = FX_Random_MT_Generate(pContext);
- }
- FX_Random_MT_Close(pContext);
- pContext = FX_Random_MT_Start(dwSeed2);
- for (i = 0; i < 2; i++) {
- *pBuffer++ = FX_Random_MT_Generate(pContext);
- }
- FX_Random_MT_Close(pContext);
- return TRUE;
-}
-class CPDF_FlateEncoder
-{
-public:
- CPDF_FlateEncoder();
- ~CPDF_FlateEncoder();
- FX_BOOL Initialize(CPDF_Stream* pStream, FX_BOOL bFlateEncode);
- FX_BOOL Initialize(FX_LPCBYTE pBuffer, FX_DWORD size, FX_BOOL bFlateEncode, FX_BOOL bXRefStream = FALSE);
- void CloneDict();
- FX_LPBYTE 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 = (CPDF_Dictionary*)m_pDict->Clone();
- m_bCloned = TRUE;
- }
-}
-FX_BOOL CPDF_FlateEncoder::Initialize(CPDF_Stream* pStream, FX_BOOL bFlateEncode)
-{
- m_Acc.LoadAllData(pStream, TRUE);
- if (pStream->GetDict()->KeyExist("Filter") || !bFlateEncode) {
- if (pStream->GetDict()->KeyExist("Filter") && !bFlateEncode) {
- CPDF_StreamAcc destAcc;
- destAcc.LoadAllData(pStream);
- m_dwSize = destAcc.GetSize();
- m_pData = (FX_LPBYTE)destAcc.DetachData();
- m_pDict = (CPDF_Dictionary*)pStream->GetDict()->Clone();
- m_pDict->RemoveAt(FX_BSTRC("Filter"));
- m_bNewData = TRUE;
- m_bCloned = TRUE;
- } else {
- m_pData = (FX_LPBYTE)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 = (CPDF_Dictionary*)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(FX_LPCBYTE pBuffer, FX_DWORD size, FX_BOOL bFlateEncode, FX_BOOL bXRefStream)
-{
- if (!bFlateEncode) {
- m_pData = (FX_LPBYTE)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 && m_pData) {
- FX_Free(m_pData);
- }
-}
-class CPDF_Encryptor
-{
-public:
- CPDF_Encryptor();
- ~CPDF_Encryptor();
- FX_BOOL Initialize(CPDF_CryptoHandler* pHandler, int objnum, FX_LPBYTE src_data, FX_DWORD src_size);
- FX_LPBYTE 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(CPDF_CryptoHandler* pHandler, int objnum, FX_LPBYTE src_data, FX_DWORD src_size)
-{
- if (src_size == 0) {
- return TRUE;
- }
- if (pHandler == NULL) {
- m_pData = (FX_LPBYTE)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(FX_BYTE, m_dwSize);
- if(!m_pData) {
- return FALSE;
- }
- 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);
- }
-}
-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;
-}
-FX_INT32 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;
-}
-FX_INT32 CPDF_ObjectStream::CompressIndirectObject(FX_DWORD dwObjNum, FX_LPCBYTE 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;
- CPDF_CryptoHandler *pHandler = pCreator->m_pCryptoHandler;
- FX_FILESIZE ObjOffset = pCreator->m_Offset;
- if (!m_dwObjNum) {
- m_dwObjNum = ++pCreator->m_dwLastObjNum;
- }
- CFX_ByteTextBuf tempBuffer;
- FX_INT32 iCount = m_ObjNumArray.GetSize();
- for (FX_INT32 i = 0; i < iCount; i++) {
- tempBuffer << m_ObjNumArray.ElementAt(i) << FX_BSTRC(" ") << m_OffsetArray.ElementAt(i) << FX_BSTRC(" ");
- }
- FX_FILESIZE &offset = pCreator->m_Offset;
- FX_INT32 len = pFile->AppendDWord(m_dwObjNum);
- if (len < 0) {
- return -1;
- }
- offset += len;
- if ((len = pFile->AppendString(FX_BSTRC(" 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(FX_BSTRC("/First ")) < 0) {
- return -1;
- }
- if ((len = pFile->AppendDWord((FX_DWORD)tempBuffer.GetLength())) < 0) {
- return -1;
- }
- if (pFile->AppendString(FX_BSTRC("/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(FX_BSTRC(">>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(FX_BSTRC("/Filter /FlateDecode")) < 0) {
- return -1;
- }
- offset += 20;
- }
- if ((len = pFile->AppendString(FX_BSTRC(">>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(FX_BSTRC("\r\nendstream\r\nendobj\r\n"))) < 0) {
- return -1;
- }
- offset += len;
- return ObjOffset;
-}
-CPDF_XRefStream::CPDF_XRefStream()
- : m_PrevOffset(0)
- , m_iSeg(0)
- , m_dwTempObjNum(0)
-{
-}
-FX_BOOL CPDF_XRefStream::Start()
-{
- m_IndexArray.RemoveAll();
- m_Buffer.Clear();
- m_iSeg = 0;
- return TRUE;
-}
-FX_INT32 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);
-}
-FX_INT32 CPDF_XRefStream::CompressIndirectObject(FX_DWORD dwObjNum, FX_LPCBYTE 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);
-}
-static void _AppendIndex0(CFX_ByteTextBuf& buffer, FX_BOOL bFirstObject = TRUE)
-{
- buffer.AppendByte(0);
- buffer.AppendByte(0);
- buffer.AppendByte(0);
- buffer.AppendByte(0);
- buffer.AppendByte(0);
- if (bFirstObject) {
- buffer.AppendByte(0xFF);
- buffer.AppendByte(0xFF);
- } else {
- buffer.AppendByte(0);
- buffer.AppendByte(0);
- }
-}
-static 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);
-}
-static void _AppendIndex2(CFX_ByteTextBuf& buffer, FX_DWORD objnum, FX_INT32 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));
-}
-FX_INT32 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;
- }
- FX_INT32 iSize = m_ObjStream.m_ObjNumArray.GetSize();
- FX_INT32 iSeg = m_IndexArray.GetSize() / 2;
- if (!(pCreator->m_dwFlags & FPDFCREATE_INCREMENTAL)) {
- if (m_dwTempObjNum == 0) {
- _AppendIndex0(m_Buffer);
- m_dwTempObjNum++;
- }
- FX_DWORD end_num = m_IndexArray.GetAt((iSeg - 1) * 2) + m_IndexArray.GetAt((iSeg - 1) * 2 + 1);
- 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_ObjectSize.Add(dwObjStmNum, 1);
- pCreator->m_ObjectOffset[dwObjStmNum] = objOffset;
- }
- m_iSeg = iSeg;
- if (bEOF) {
- m_ObjStream.Start();
- }
- return 1;
- }
- FX_INT32 &j = m_ObjStream.m_index;
- for (int i = m_iSeg; i < iSeg; i++) {
- FX_DWORD start = m_IndexArray.ElementAt(i * 2);
- FX_DWORD end = m_IndexArray.ElementAt(i * 2 + 1) + start;
- for (FX_DWORD m = start; m < end; m++) {
- if (j >= iSize || m != m_ObjStream.m_ObjNumArray.ElementAt(j)) {
- _AppendIndex1(m_Buffer, pCreator->m_ObjectOffset[m]);
- } else {
- _AppendIndex2(m_Buffer, dwObjStmNum, j++);
- }
- }
- }
- if (iSize > 0 && bEOF) {
- _AppendIndex1(m_Buffer, objOffset);
- m_IndexArray.Add(dwObjStmNum);
- m_IndexArray.Add(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;
- FX_INT32 len = pFile->AppendDWord(objnum);
- if (len < 0) {
- return FALSE;
- }
- offset += len;
- if ((len = pFile->AppendString(FX_BSTRC(" 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(FX_BSTRC(" "))) < 0) {
- return FALSE;
- }
- offset += len + 1;
- if ((len = pFile->AppendDWord(objnum + 1)) < 0) {
- return FALSE;
- }
- offset += len;
- } else {
- FX_INT32 iSeg = m_IndexArray.GetSize() / 2;
- for (FX_INT32 i = 0; i < iSeg; i++) {
- if ((len = pFile->AppendDWord(m_IndexArray.ElementAt(i * 2))) < 0) {
- return FALSE;
- }
- if (pFile->AppendString(FX_BSTRC(" ")) < 0) {
- return FALSE;
- }
- offset += len + 1;
- if ((len = pFile->AppendDWord(m_IndexArray.ElementAt(i * 2 + 1))) < 0) {
- return FALSE;
- }
- if (pFile->AppendString(FX_BSTRC(" ")) < 0) {
- return FALSE;
- }
- offset += len + 1;
- }
- }
- if (pFile->AppendString(FX_BSTRC("]/Size ")) < 0) {
- return FALSE;
- }
- if ((len = pFile->AppendDWord(objnum + 1)) < 0) {
- return FALSE;
- }
- offset += len + 7;
- if (m_PrevOffset > 0) {
- if (pFile->AppendString(FX_BSTRC("/Prev ")) < 0) {
- return -1;
- }
- FX_CHAR offset_buf[20];
- FXSYS_memset32(offset_buf, 0, sizeof(offset_buf));
- FXSYS_i64toa(m_PrevOffset, offset_buf, 10);
- FX_INT32 len = (FX_INT32)FXSYS_strlen(offset_buf);
- if (pFile->AppendBlock(offset_buf, len) < 0) {
- return -1;
- }
- 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(FX_BSTRC("/Filter /FlateDecode")) < 0) {
- return FALSE;
- }
- offset += 20;
- if (bPredictor) {
- if ((len = pFile->AppendString(FX_BSTRC("/DecodeParms<</Columns 7/Predictor 12>>"))) < 0) {
- return FALSE;
- }
- offset += len;
- }
- }
- if (pFile->AppendString(FX_BSTRC("/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 -1;
- }
- 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 -1;
- }
- offset += len;
- }
- }
- if ((len = pFile->AppendString(FX_BSTRC(">>stream\r\n"))) < 0) {
- return FALSE;
- }
- offset += len;
- if (pFile->AppendBlock(encoder.m_pData, encoder.m_dwSize) < 0) {
- return FALSE;
- }
- if ((len = pFile->AppendString(FX_BSTRC("\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);
- 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 {
- FX_INT32 iSeg = m_IndexArray.GetSize() / 2;
- for (int i = 0; i < iSeg; i++) {
- FX_DWORD start = m_IndexArray.ElementAt(i * 2);
- FX_DWORD end = m_IndexArray.ElementAt(i * 2 + 1) + start;
- for (FX_DWORD j = start; j < end; j++) {
- _AppendIndex1(m_Buffer, pCreator->m_ObjectOffset[j]);
- }
- }
- }
- return GenerateXRefStream(pCreator, FALSE);
-}
-FX_BOOL CPDF_XRefStream::AddObjectNumberToIndexArray(FX_DWORD objnum)
-{
- FX_INT32 iSize = m_IndexArray.GetSize();
- if (iSize == 0) {
- m_IndexArray.Add(objnum);
- m_IndexArray.Add(1);
- } else {
- FXSYS_assert(iSize > 1);
- FX_DWORD startobjnum = m_IndexArray.ElementAt(iSize - 2);
- FX_INT32 iCount = m_IndexArray.ElementAt(iSize - 1);
- if (objnum == startobjnum + iCount) {
- m_IndexArray[iSize - 1] = iCount + 1;
- } else {
- m_IndexArray.Add(objnum);
- m_IndexArray.Add(1);
- }
- }
- return TRUE;
-}
-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();
-}
-static FX_BOOL _IsXRefNeedEnd(CPDF_XRefStream* pXRef, FX_DWORD flag)
-{
- if (!(flag & FPDFCREATE_INCREMENTAL)) {
- return FALSE;
- }
- FX_INT32 iSize = pXRef->m_IndexArray.GetSize() / 2;
- FX_INT32 iCount = 0;
- for (FX_INT32 i = 0; i < iSize; i++) {
- iCount += pXRef->m_IndexArray.ElementAt(i * 2 + 1);
- }
- return (iCount >= PDF_XREFSTREAM_MAXSIZE);
-}
-FX_INT32 CPDF_Creator::WriteIndirectObjectToStream(const CPDF_Object* pObj)
-{
- if (!m_pXRefStream) {
- return 1;
- }
- FX_DWORD objnum = pObj->GetObjNum();
- if (m_pParser && m_pParser->m_ObjVersion.GetSize() > (FX_INT32)objnum && m_pParser->m_ObjVersion[objnum] > 0) {
- return 1;
- }
- if (pObj->GetType() == PDFOBJ_NUMBER) {
- return 1;
- }
- CPDF_Dictionary *pDict = pObj->GetDict();
- if (pObj->GetType() == PDFOBJ_STREAM) {
- if (pDict && pDict->GetString(FX_BSTRC("Type")) == FX_BSTRC("XRef")) {
- return 0;
- }
- return 1;
- }
- if (pDict) {
- if (pDict == m_pDocument->m_pRootDict || pDict == m_pEncryptDict) {
- return 1;
- }
- if (IsSignatureDict(pDict)) {
- return 1;
- }
- if (pDict->GetString(FX_BSTRC("Type")) == FX_BSTRC("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;
-}
-FX_INT32 CPDF_Creator::WriteIndirectObjectToStream(FX_DWORD objnum, FX_LPCBYTE pBuffer, FX_DWORD dwSize)
-{
- if (!m_pXRefStream) {
- return 1;
- }
- m_pXRefStream->AddObjectNumberToIndexArray(objnum);
- FX_INT32 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;
-}
-FX_INT32 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;
-}
-FX_INT32 CPDF_Creator::WriteStream(const CPDF_Object* pStream, FX_DWORD objnum, CPDF_CryptoHandler* pCrypto)
-{
- CPDF_FlateEncoder encoder;
- encoder.Initialize((CPDF_Stream*)pStream, 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->GetInteger(FX_BSTRC("Length")) != encryptor.m_dwSize) {
- encoder.CloneDict();
- encoder.m_pDict->SetAtInteger(FX_BSTRC("Length"), encryptor.m_dwSize);
- }
- if (WriteDirectObj(objnum, encoder.m_pDict) < 0) {
- return -1;
- }
- int len = m_File.AppendString(FX_BSTRC("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(FX_BSTRC("\r\nendstream"))) < 0) {
- return -1;
- }
- m_Offset += len;
- return 1;
-}
-FX_INT32 CPDF_Creator::WriteIndirectObj(FX_DWORD objnum, const CPDF_Object* pObj)
-{
- FX_INT32 len = m_File.AppendDWord(objnum);
- if (len < 0) {
- return -1;
- }
- m_Offset += len;
- if ((len = m_File.AppendString(FX_BSTRC(" 0 obj\r\n"))) < 0) {
- return -1;
- }
- m_Offset += len;
- if (pObj->GetType() == PDFOBJ_STREAM) {
- CPDF_CryptoHandler *pHandler = NULL;
- 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(FX_BSTRC("\r\nendobj\r\n"))) < 0) {
- return -1;
- }
- m_Offset += len;
- if (AppendObjectNumberToXRef(objnum) < 0) {
- return -1;
- }
- return 0;
-}
-FX_INT32 CPDF_Creator::WriteIndirectObj(const CPDF_Object* pObj)
-{
- FX_INT32 iRet = WriteIndirectObjectToStream(pObj);
- if (iRet < 1) {
- return iRet;
- }
- return WriteIndirectObj(pObj->GetObjNum(), pObj);
-}
-FX_INT32 CPDF_Creator::WriteDirectObj(FX_DWORD objnum, const CPDF_Object* pObj, FX_BOOL bEncrypt)
-{
- FX_INT32 len = 0;
- if (pObj == NULL) {
- if (m_File.AppendString(FX_BSTRC(" null")) < 0) {
- return -1;
- }
- m_Offset += 5;
- return 1;
- }
- switch (pObj->GetType()) {
- case PDFOBJ_NULL:
- if (m_File.AppendString(FX_BSTRC(" null")) < 0) {
- return -1;
- }
- m_Offset += 5;
- break;
- case PDFOBJ_BOOLEAN:
- case PDFOBJ_NUMBER:
- if (m_File.AppendString(FX_BSTRC(" ")) < 0) {
- return -1;
- }
- if ((len = m_File.AppendString(pObj->GetString())) < 0) {
- return -1;
- }
- m_Offset += len + 1;
- break;
- case PDFOBJ_STRING: {
- CFX_ByteString str = pObj->GetString();
- FX_BOOL bHex = ((CPDF_String*)pObj)->IsHex();
- if (m_pCryptoHandler == NULL || !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, (FX_LPBYTE)(FX_LPCSTR)str, str.GetLength());
- CFX_ByteString content = PDF_EncodeString(CFX_ByteString((FX_LPCSTR)encryptor.m_pData, encryptor.m_dwSize), bHex);
- if ((len = m_File.AppendString(content)) < 0) {
- return -1;
- }
- m_Offset += len;
- break;
- }
- case PDFOBJ_STREAM: {
- CPDF_FlateEncoder encoder;
- encoder.Initialize((CPDF_Stream*)pObj, m_bCompress);
- CPDF_Encryptor encryptor;
- CPDF_CryptoHandler* pHandler = m_pCryptoHandler;
- encryptor.Initialize(pHandler, objnum, encoder.m_pData, encoder.m_dwSize);
- if ((FX_DWORD)encoder.m_pDict->GetInteger(FX_BSTRC("Length")) != encryptor.m_dwSize) {
- encoder.CloneDict();
- encoder.m_pDict->SetAtInteger(FX_BSTRC("Length"), encryptor.m_dwSize);
- }
- if (WriteDirectObj(objnum, encoder.m_pDict) < 0) {
- return -1;
- }
- if ((len = m_File.AppendString(FX_BSTRC("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(FX_BSTRC("\r\nendstream"))) < 0) {
- return -1;
- }
- m_Offset += len;
- break;
- }
- case PDFOBJ_NAME: {
- if (m_File.AppendString(FX_BSTRC("/")) < 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 PDFOBJ_REFERENCE: {
- if (m_File.AppendString(FX_BSTRC(" ")) < 0) {
- return -1;
- }
- CPDF_Reference* p = (CPDF_Reference*)pObj;
- if ((len = m_File.AppendDWord(p->GetRefObjNum())) < 0) {
- return -1;
- }
- if (m_File.AppendString(FX_BSTRC(" 0 R")) < 0) {
- return -1;
- }
- m_Offset += len + 5;
- break;
- }
- case PDFOBJ_ARRAY: {
- if (m_File.AppendString(FX_BSTRC("[")) < 0) {
- return -1;
- }
- m_Offset += 1;
- CPDF_Array* p = (CPDF_Array*)pObj;
- for (FX_DWORD i = 0; i < p->GetCount(); i ++) {
- CPDF_Object* pElement = p->GetElement(i);
- if (pElement->GetObjNum()) {
- if (m_File.AppendString(FX_BSTRC(" ")) < 0) {
- return -1;
- }
- if ((len = m_File.AppendDWord(pElement->GetObjNum())) < 0) {
- return -1;
- }
- if (m_File.AppendString(FX_BSTRC(" 0 R")) < 0) {
- return -1;
- }
- m_Offset += len + 5;
- } else {
- if (WriteDirectObj(objnum, pElement) < 0) {
- return -1;
- }
- }
- }
- if (m_File.AppendString(FX_BSTRC("]")) < 0) {
- return -1;
- }
- m_Offset += 1;
- break;
- }
- case PDFOBJ_DICTIONARY: {
- if (m_pCryptoHandler == NULL || pObj == m_pEncryptDict) {
- return PDF_CreatorAppendObject(pObj, &m_File, m_Offset);
- }
- if (m_File.AppendString(FX_BSTRC("<<")) < 0) {
- return -1;
- }
- m_Offset += 2;
- CPDF_Dictionary* p = (CPDF_Dictionary*)pObj;
- FX_BOOL bSignDict = IsSignatureDict(p);
- FX_POSITION pos = p->GetStartPos();
- while (pos) {
- FX_BOOL bSignValue = FALSE;
- CFX_ByteString key;
- CPDF_Object* pValue = p->GetNextElement(pos, key);
- if (m_File.AppendString(FX_BSTRC("/")) < 0) {
- return -1;
- }
- if ((len = m_File.AppendString(PDF_NameEncode(key))) < 0) {
- return -1;
- }
- m_Offset += len + 1;
- if (bSignDict && key == FX_BSTRC("Contents")) {
- bSignValue = TRUE;
- }
- if (pValue->GetObjNum()) {
- if (m_File.AppendString(FX_BSTRC(" ")) < 0) {
- return -1;
- }
- if ((len = m_File.AppendDWord(pValue->GetObjNum())) < 0) {
- return -1;
- }
- if (m_File.AppendString(FX_BSTRC(" 0 R ")) < 0) {
- return -1;
- }
- m_Offset += len + 6;
- } else {
- if (WriteDirectObj(objnum, pValue, !bSignValue) < 0) {
- return -1;
- }
- }
- }
- if (m_File.AppendString(FX_BSTRC(">>")) < 0) {
- return -1;
- }
- m_Offset += 2;
- break;
- }
- }
- return 1;
-}
-FX_INT32 CPDF_Creator::WriteOldIndirectObject(FX_DWORD objnum)
-{
- if(m_pParser->m_V5Type[objnum] == 0 || m_pParser->m_V5Type[objnum] == 255) {
- return 0;
- }
- m_ObjectOffset[objnum] = m_Offset;
- FX_LPVOID valuetemp = NULL;
- FX_BOOL bExistInMap = m_pDocument->m_IndirectObjs.Lookup((FX_LPVOID)(FX_UINTPTR)objnum, valuetemp);
- FX_BOOL bObjStm = (m_pParser->m_V5Type[objnum] == 2) && m_pEncryptDict && !m_pXRefStream;
- if(m_pParser->m_bVersionUpdated || m_bSecurityChanged || bExistInMap || bObjStm) {
- CPDF_Object* pObj = m_pDocument->GetIndirectObject(objnum);
- if (pObj == NULL) {
- m_ObjectOffset[objnum] = 0;
- m_ObjectSize[objnum] = 0;
- return 0;
- }
- if (WriteIndirectObj(pObj)) {
- return -1;
- }
- if (!bExistInMap) {
- m_pDocument->ReleaseIndirectObject(objnum);
- }
- } else {
- FX_BYTE* pBuffer;
- FX_DWORD size;
- m_pParser->GetIndirectBinary(objnum, pBuffer, size);
- if (pBuffer == NULL) {
- return 0;
- }
- if (m_pParser->m_V5Type[objnum] == 2) {
- if (m_pXRefStream) {
- if (WriteIndirectObjectToStream(objnum, pBuffer, size) < 0) {
- FX_Free(pBuffer);
- return -1;
- }
- } else {
- FX_INT32 len = m_File.AppendDWord(objnum);
- if (len < 0) {
- return -1;
- }
- if (m_File.AppendString(FX_BSTRC(" 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(FX_BSTRC("\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;
-}
-FX_INT32 CPDF_Creator::WriteOldObjs(IFX_Pause *pPause)
-{
- FX_DWORD nOldSize = m_pParser->m_CrossRef.GetSize();
- FX_DWORD objnum = (FX_DWORD)(FX_UINTPTR)m_Pos;
- for(; objnum < nOldSize; objnum ++) {
- FX_INT32 iRet = WriteOldIndirectObject(objnum);
- if (!iRet) {
- continue;
- }
- if (iRet < 0) {
- return iRet;
- }
- m_ObjectSize[objnum] = (FX_DWORD)(m_Offset - m_ObjectOffset[objnum]);
- if (pPause && pPause->NeedToPauseNow()) {
- m_Pos = (FX_LPVOID)(FX_UINTPTR)(objnum + 1);
- return 1;
- }
- }
- return 0;
-}
-FX_INT32 CPDF_Creator::WriteNewObjs(FX_BOOL bIncremental, IFX_Pause *pPause)
-{
- FX_INT32 iCount = m_NewObjNumArray.GetSize();
- FX_INT32 index = (FX_INT32)(FX_UINTPTR)m_Pos;
- while (index < iCount) {
- FX_DWORD objnum = m_NewObjNumArray.ElementAt(index);
- CPDF_Object *pObj = NULL;
- m_pDocument->m_IndirectObjs.Lookup((FX_LPVOID)(FX_UINTPTR)objnum, (FX_LPVOID&)pObj);
- if (NULL == pObj) {
- ++index;
- continue;
- }
- m_ObjectOffset[objnum] = m_Offset;
- if (WriteIndirectObj(pObj)) {
- return -1;
- }
- m_ObjectSize[objnum] = (FX_DWORD)(m_Offset - m_ObjectOffset[objnum]);
- index++;
- if (pPause && pPause->NeedToPauseNow()) {
- m_Pos = (FX_POSITION)(FX_UINTPTR)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->m_V5Type[dwStart] == 0 || m_pParser->m_V5Type[dwStart] == 255)) {
- dwStart++;
- }
- if (dwStart > dwEnd) {
- break;
- }
- j = dwStart;
- while (j <= dwEnd && m_pParser->m_V5Type[j] != 0 && m_pParser->m_V5Type[j] != 255) {
- j++;
- }
- m_ObjectOffset.Add(dwStart, j - dwStart);
- m_ObjectSize.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;
- FX_DWORD nOldSize = m_pParser ? m_pParser->m_CrossRef.GetSize() : 0;
- FX_POSITION pos = m_pDocument->m_IndirectObjs.GetStartPosition();
- while (pos) {
- size_t key = 0;
- CPDF_Object* pObj;
- m_pDocument->m_IndirectObjs.GetNextAssoc(pos, (FX_LPVOID&)key, (FX_LPVOID&)pObj);
- FX_DWORD objnum = (FX_DWORD)key;
- if (pObj->GetObjNum() == -1) {
- continue;
- }
- if (bIncremental) {
- if (!pObj->IsModified()) {
- continue;
- }
- } else {
- if (objnum < nOldSize && m_pParser->m_V5Type[objnum] != 0) {
- continue;
- }
- }
- AppendNewObjNum(objnum);
- }
- FX_INT32 iCount = m_NewObjNumArray.GetSize();
- if (iCount == 0) {
- return;
- }
- FX_INT32 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);
- FX_BOOL bExist = (dwCurObjNum < nOldSize && m_ObjectOffset.GetPtrAt(dwCurObjNum) != NULL);
- if (bExist || dwCurObjNum - dwLastObjNum > 1) {
- if (!bNewStart) {
- m_ObjectOffset.Add(dwStartObjNum, dwLastObjNum - dwStartObjNum + 1);
- m_ObjectSize.Add(dwStartObjNum, dwLastObjNum - dwStartObjNum + 1);
- }
- dwStartObjNum = dwCurObjNum;
- }
- if (bNewStart) {
- dwStartObjNum = dwCurObjNum;
- }
- bNewStart = bExist;
- dwLastObjNum = dwCurObjNum;
- }
- m_ObjectOffset.Add(dwStartObjNum, dwLastObjNum - dwStartObjNum + 1);
- m_ObjectSize.Add(dwStartObjNum, dwLastObjNum - dwStartObjNum + 1);
-}
-void CPDF_Creator::AppendNewObjNum(FX_DWORD objbum)
-{
- FX_INT32 iStart = 0, iFind = 0;
- FX_INT32 iEnd = m_NewObjNumArray.GetUpperBound();
- while (iStart <= iEnd) {
- FX_INT32 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);
-}
-FX_INT32 CPDF_Creator::WriteDoc_Stage1(IFX_Pause *pPause)
-{
- FXSYS_assert(m_iStage > -1 || m_iStage < 20);
- if (m_iStage == 0) {
- if (m_pParser == NULL) {
- m_dwFlags &= ~FPDFCREATE_INCREMENTAL;
- }
- if (m_bSecurityChanged && (m_dwFlags & FPDFCREATE_NO_ORIGINAL) == 0) {
- m_dwFlags &= ~FPDFCREATE_INCREMENTAL;
- }
- m_pMetadata = m_pDocument->GetRoot()->GetElementValue(FX_BSTRC("Metadata"));
- if (m_dwFlags & FPDFCREATE_OBJECTSTREAM) {
- m_pXRefStream = FX_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(FX_BSTRC("%PDF-1.")) < 0) {
- return -1;
- }
- m_Offset += 7;
- FX_INT32 version = 7;
- if (m_FileVersion) {
- version = m_FileVersion;
- } else if (m_pParser) {
- version = m_pParser->GetFileVersion();
- }
- FX_INT32 len = m_File.AppendDWord(version % 10);
- if (len < 0) {
- return -1;
- }
- m_Offset += len;
- if ((len = m_File.AppendString(FX_BSTRC("\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 = (FX_LPVOID)(FX_UINTPTR)m_Offset;
- m_iStage = 15;
- }
- }
- if (m_iStage == 15) {
- if ((m_dwFlags & FPDFCREATE_NO_ORIGINAL) == 0 && m_Pos) {
- IFX_FileRead* pSrcFile = m_pParser->GetFileAccess();
- FX_BYTE buffer[4096];
- FX_DWORD src_size = (FX_DWORD)(FX_UINTPTR)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 = (FX_LPVOID)(FX_UINTPTR)src_size;
- return 1;
- }
- }
- }
- if ((m_dwFlags & FPDFCREATE_NO_ORIGINAL) == 0 && m_pParser->GetLastXRefOffset() == 0) {
- InitOldObjNumOffsets();
- FX_DWORD dwEnd = m_pParser->GetLastObjNum();
- FX_BOOL bObjStm = (m_dwFlags & FPDFCREATE_OBJECTSTREAM) != 0;
- for (FX_DWORD objnum = 0; objnum <= dwEnd; objnum++) {
- if (m_pParser->m_V5Type[objnum] == 0 || m_pParser->m_V5Type[objnum] == 255) {
- continue;
- }
- m_ObjectOffset[objnum] = m_pParser->m_CrossRef[objnum];
- if (bObjStm) {
- m_pXRefStream->AddObjectNumberToIndexArray(objnum);
- }
- }
- if (bObjStm) {
- m_pXRefStream->EndXRefStream(this);
- m_pXRefStream->Start();
- }
- }
- m_iStage = 20;
- }
- InitNewObjNumOffsets();
- return m_iStage;
-}
-FX_INT32 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 = (FX_LPVOID)(FX_UINTPTR)0;
- m_iStage = 21;
- } else {
- m_iStage = 25;
- }
- }
- if (m_iStage == 21) {
- FX_INT32 iRet = WriteOldObjs(pPause);
- if (iRet) {
- return iRet;
- }
- m_iStage = 25;
- }
- if (m_iStage == 25) {
- m_Pos = (FX_LPVOID)(FX_UINTPTR)0;
- m_iStage = 26;
- }
- if (m_iStage == 26) {
- FX_INT32 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_ObjectSize.Add(m_dwLastObjNum, 1);
- m_ObjectSize[m_dwLastObjNum] = m_Offset - saveOffset;
- m_dwEnryptObjNum = m_dwLastObjNum;
- if (m_dwFlags & FPDFCREATE_INCREMENTAL) {
- m_NewObjNumArray.Add(m_dwLastObjNum);
- }
- }
- m_iStage = 80;
- }
- return m_iStage;
-}
-FX_INT32 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) ? FX_BSTRC("xref\r\n") : FX_BSTRC("xref\r\n0 1\r\n0000000000 65536 f\r\n");
- if (m_File.AppendString(str) < 0) {
- return -1;
- }
- m_Pos = (FX_LPVOID)(FX_UINTPTR)1;
- m_iStage = 81;
- } else {
- if (m_File.AppendString(FX_BSTRC("xref\r\n")) < 0) {
- return -1;
- }
- m_Pos = (FX_LPVOID)(FX_UINTPTR)0;
- m_iStage = 82;
- }
- } else {
- m_iStage = 90;
- }
- }
- if (m_iStage == 81) {
- CFX_ByteString str;
- FX_DWORD i = (FX_DWORD)(FX_UINTPTR)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 65536 f\r\n", j);
- } else {
- str.Format("%d %d\r\n", i, j - i);
- }
- if (m_File.AppendBlock((FX_LPCSTR)str, str.GetLength()) < 0) {
- return -1;
- }
- while (i < j) {
- str.Format("%010d 00000 n\r\n", m_ObjectOffset[i ++]);
- if (m_File.AppendBlock((FX_LPCSTR)str, str.GetLength()) < 0) {
- return -1;
- }
- }
- if (i > dwLastObjNum) {
- break;
- }
- if (pPause && pPause->NeedToPauseNow()) {
- m_Pos = (FX_LPVOID)(FX_UINTPTR)i;
- return 1;
- }
- }
- m_iStage = 90;
- }
- if (m_iStage == 82) {
- CFX_ByteString str;
- FX_INT32 iCount = m_NewObjNumArray.GetSize();
- FX_INT32 i = (FX_INT32)(FX_UINTPTR)m_Pos;
- while (i < iCount) {
- FX_INT32 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 65536 f\r\n", j - i + 1);
- } else {
- str.Format("%d %d\r\n", objnum, j - i);
- }
- if (m_File.AppendBlock((FX_LPCSTR)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((FX_LPCSTR)str, str.GetLength()) < 0) {
- return -1;
- }
- }
- if (pPause && (i % 100) == 0 && pPause->NeedToPauseNow()) {
- m_Pos = (FX_LPVOID)(FX_UINTPTR)i;
- return 1;
- }
- }
- m_iStage = 90;
- }
- return m_iStage;
-}
-static FX_INT32 _OutPutIndex(CFX_FileBufferArchive* pFile, FX_FILESIZE offset)
-{
- FXSYS_assert(pFile);
- 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;
-}
-FX_INT32 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(FX_BSTRC("trailer\r\n<<")) < 0) {
- return -1;
- }
- } else {
- if (m_File.AppendDWord(m_pDocument->m_LastObjNum + 1) < 0) {
- return -1;
- }
- if (m_File.AppendString(FX_BSTRC(" 0 obj <<")) < 0) {
- return -1;
- }
- }
- if (m_pParser) {
- CPDF_Dictionary* p = m_pParser->m_pTrailer;
- FX_POSITION pos = p->GetStartPos();
- while (pos) {
- CFX_ByteString key;
- CPDF_Object* pValue = p->GetNextElement(pos, key);
- if (key == FX_BSTRC("Encrypt") || key == FX_BSTRC("Size") || key == FX_BSTRC("Filter") ||
- key == FX_BSTRC("Index") || key == FX_BSTRC("Length") || key == FX_BSTRC("Prev") ||
- key == FX_BSTRC("W") || key == FX_BSTRC("XRefStm") || key == FX_BSTRC("ID")) {
- continue;
- }
- if (m_File.AppendString((FX_BSTRC("/"))) < 0) {
- return -1;
- }
- if (m_File.AppendString(PDF_NameEncode(key)) < 0) {
- return -1;
- }
- if (pValue->GetObjNum()) {
- if (m_File.AppendString(FX_BSTRC(" ")) < 0) {
- return -1;
- }
- if (m_File.AppendDWord(pValue->GetObjNum()) < 0) {
- return -1;
- }
- if (m_File.AppendString(FX_BSTRC(" 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(FX_BSTRC("\r\n/Root ")) < 0) {
- return -1;
- }
- if (m_File.AppendDWord(m_pDocument->m_pRootDict->GetObjNum()) < 0) {
- return -1;
- }
- if (m_File.AppendString(FX_BSTRC(" 0 R\r\n")) < 0) {
- return -1;
- }
- if (m_pDocument->m_pInfoDict) {
- if (m_File.AppendString(FX_BSTRC("/Info ")) < 0) {
- return -1;
- }
- if (m_File.AppendDWord(m_pDocument->m_pInfoDict->GetObjNum()) < 0) {
- return -1;
- }
- if (m_File.AppendString(FX_BSTRC(" 0 R\r\n")) < 0) {
- return -1;
- }
- }
- }
- if (m_pEncryptDict) {
- if (m_File.AppendString(FX_BSTRC("/Encrypt")) < 0) {
- return -1;
- }
- FX_DWORD dwObjNum = m_pEncryptDict->GetObjNum();
- if (dwObjNum == 0) {
- dwObjNum = m_pDocument->GetLastObjNum() + 1;
- }
- if (m_File.AppendString(FX_BSTRC(" ")) < 0) {
- return -1;
- }
- if (m_File.AppendDWord(dwObjNum) < 0) {
- return -1;
- }
- if (m_File.AppendString(FX_BSTRC(" 0 R ")) < 0) {
- return -1;
- }
- }
- if (m_File.AppendString(FX_BSTRC("/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(FX_BSTRC("/Prev ")) < 0) {
- return -1;
- }
- FX_CHAR offset_buf[20];
- FXSYS_memset32(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((FX_BSTRC("/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(FX_BSTRC(">>")) < 0) {
- return -1;
- }
- } else {
- if (m_File.AppendString(FX_BSTRC("/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(FX_BSTRC(" 1 ")) < 0) {
- return -1;
- }
- }
- if (m_File.AppendString(FX_BSTRC("]/Length ")) < 0) {
- return -1;
- }
- if (m_File.AppendDWord(m_dwLastObjNum * 5) < 0) {
- return -1;
- }
- if (m_File.AppendString(FX_BSTRC(">>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();
- FX_INT32 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(FX_BSTRC(" 1 ")) < 0) {
- return -1;
- }
- }
- if (m_File.AppendString(FX_BSTRC("]/Length ")) < 0) {
- return -1;
- }
- if (m_File.AppendDWord(count * 5) < 0) {
- return -1;
- }
- if (m_File.AppendString(FX_BSTRC(">>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(FX_BSTRC("\r\nendstream")) < 0) {
- return -1;
- }
- }
- }
- if (m_File.AppendString(FX_BSTRC("\r\nstartxref\r\n")) < 0) {
- return -1;
- }
- FX_CHAR offset_buf[20];
- FXSYS_memset32(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(FX_BSTRC("\r\n%%EOF\r\n")) < 0) {
- return -1;
- }
- m_File.Flush();
- return m_iStage = 100;
-}
-void CPDF_Creator::Clear()
-{
- if (m_pXRefStream) {
- delete m_pXRefStream;
- m_pXRefStream = NULL;
- }
- m_File.Clear();
- m_NewObjNumArray.RemoveAll();
- if (m_pIDArray) {
- m_pIDArray->Release();
- m_pIDArray = NULL;
- }
-}
-FX_BOOL CPDF_Creator::Create(FX_LPCSTR filename, FX_DWORD flags)
-{
- if (!m_File.AttachFile(filename)) {
- return FALSE;
- }
- FX_BOOL bRet = Create(flags);
- if (!bRet || !(flags & FPDFCREATE_PROGRESSIVE)) {
- Clear();
- }
- return bRet;
-}
-FX_BOOL CPDF_Creator::Create(FX_LPCWSTR filename, FX_DWORD flags)
-{
- if (!m_File.AttachFile(filename)) {
- return FALSE;
- }
- FX_BOOL bRet = Create(flags);
- if (!bRet || !(flags & FPDFCREATE_PROGRESSIVE)) {
- Clear();
- }
- return bRet;
-}
-FX_BOOL CPDF_Creator::Create(IFX_StreamWrite* pFile, FX_DWORD flags)
-{
- if (!pFile) {
- return FALSE;
- }
- if (!m_File.AttachFile(pFile, FALSE)) {
- return FALSE;
- }
- return Create(flags);
-}
-FX_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_ObjectSize.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) {
- FX_LPDWORD pBuffer = NULL;
- m_pIDArray = CPDF_Array::Create();
- CPDF_Object* pID1 = pOldIDArray->GetElement(0);
- if (pID1) {
- m_pIDArray->Add(pID1->Clone());
- } else {
- pBuffer = FX_Alloc(FX_DWORD, 4);
- PDF_GenerateFileID((FX_DWORD)(FX_UINTPTR)this, m_dwLastObjNum, pBuffer);
- CFX_ByteStringC bsBuffer((FX_LPCBYTE)pBuffer, 4 * sizeof(FX_DWORD));
- m_pIDArray->Add(CPDF_String::Create(bsBuffer, TRUE), m_pDocument);
- }
- if (pBuffer) {
- FX_Free(pBuffer);
- }
- }
- 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;
- }
- FX_LPDWORD pBuffer = FX_Alloc(FX_DWORD, 4);
- PDF_GenerateFileID((FX_DWORD)(FX_UINTPTR)this, m_dwLastObjNum, pBuffer);
- CFX_ByteStringC bsBuffer((FX_LPCBYTE)pBuffer, 4 * sizeof(FX_DWORD));
- m_pIDArray->Add(CPDF_String::Create(bsBuffer, TRUE), m_pDocument);
- FX_Free(pBuffer);
- return;
- }
- m_pIDArray->Add(m_pIDArray->GetElement(0)->Clone());
- if (m_pEncryptDict && !pOldIDArray && m_pParser && bNewId) {
- if (m_pEncryptDict->GetString(FX_BSTRC("Filter")) == FX_BSTRC("Standard")) {
- CPDF_StandardSecurityHandler handler;
- CFX_ByteString user_pass = m_pParser->GetPassword();
- FX_DWORD flag = PDF_ENCRYPT_CONTENT;
- handler.OnCreate(m_pEncryptDict, m_pIDArray, (FX_LPCBYTE)user_pass, user_pass.GetLength(), flag);
- if (m_pCryptoHandler && m_bNewCrypto) {
- delete m_pCryptoHandler;
- }
- m_pCryptoHandler = FX_NEW CPDF_StandardCryptoHandler;
- m_pCryptoHandler->Init(m_pEncryptDict, &handler);
- m_bNewCrypto = TRUE;
- m_bSecurityChanged = TRUE;
- }
- }
-}
-FX_INT32 CPDF_Creator::Continue(IFX_Pause *pPause)
-{
- if (m_iStage < 0) {
- return m_iStage;
- }
- FX_INT32 iRet;
- 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(FX_INT32 fileVersion )
-{
- if (fileVersion < 10 || fileVersion > 17) {
- return FALSE;
- }
- m_FileVersion = fileVersion;
- return TRUE;
-}
-void CPDF_Creator::ResetStandardSecurity()
-{
- if ((m_bStandardSecurity || m_bNewCrypto) && m_pCryptoHandler) {
- 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;
-}
+// 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 "../../../include/fpdfapi/fpdf_serial.h"
+#include "editint.h"
+#define PDF_OBJECTSTREAM_MAXLENGTH (256 * 1024)
+#define PDF_XREFSTREAM_MAXSIZE 10000
+extern void FlateEncode(const FX_BYTE* src_buf, FX_DWORD src_data, FX_LPBYTE& dest_buf, FX_DWORD& dest_size);
+extern void FlateEncode(FX_LPCBYTE src_buf, FX_DWORD src_size, int predictor, int Colors, int BitsPerComponent, int Columns,
+ FX_LPBYTE& dest_buf, FX_DWORD& dest_size);
+extern FX_BOOL IsSignatureDict(const CPDF_Dictionary* pDict);
+FX_INT32 PDF_CreatorAppendObject(const CPDF_Object* pObj, CFX_FileBufferArchive *pFile, FX_FILESIZE& offset)
+{
+ FX_INT32 len = 0;
+ if (pObj == NULL) {
+ if (pFile->AppendString(FX_BSTRC(" null")) < 0) {
+ return -1;
+ }
+ offset += 5;
+ return 1;
+ }
+ switch (pObj->GetType()) {
+ case PDFOBJ_NULL:
+ if (pFile->AppendString(FX_BSTRC(" null")) < 0) {
+ return -1;
+ }
+ offset += 5;
+ break;
+ case PDFOBJ_BOOLEAN:
+ case PDFOBJ_NUMBER:
+ if (pFile->AppendString(FX_BSTRC(" ")) < 0) {
+ return -1;
+ }
+ if ((len = pFile->AppendString(pObj->GetString())) < 0) {
+ return -1;
+ }
+ offset += len + 1;
+ break;
+ case PDFOBJ_STRING: {
+ CFX_ByteString str = pObj->GetString();
+ FX_BOOL bHex = ((CPDF_String*)pObj)->IsHex();
+ if ((len = pFile->AppendString(PDF_EncodeString(str, bHex))) < 0) {
+ return -1;
+ }
+ offset += len;
+ break;
+ }
+ case PDFOBJ_NAME: {
+ if (pFile->AppendString(FX_BSTRC("/")) < 0) {
+ return -1;
+ }
+ CFX_ByteString str = pObj->GetString();
+ if ((len = pFile->AppendString(PDF_NameEncode(str))) < 0) {
+ return -1;
+ }
+ offset += len + 1;
+ break;
+ }
+ case PDFOBJ_REFERENCE: {
+ if (pFile->AppendString(FX_BSTRC(" ")) < 0) {
+ return -1;
+ }
+ CPDF_Reference* p = (CPDF_Reference*)pObj;
+ if ((len = pFile->AppendDWord(p->GetRefObjNum())) < 0) {
+ return -1;
+ }
+ if (pFile->AppendString(FX_BSTRC(" 0 R ")) < 0) {
+ return -1;
+ }
+ offset += len + 6;
+ break;
+ }
+ case PDFOBJ_ARRAY: {
+ if (pFile->AppendString(FX_BSTRC("[")) < 0) {
+ return -1;
+ }
+ offset += 1;
+ CPDF_Array* p = (CPDF_Array*)pObj;
+ for (FX_DWORD i = 0; i < p->GetCount(); i ++) {
+ CPDF_Object* pElement = p->GetElement(i);
+ if (pElement->GetObjNum()) {
+ if (pFile->AppendString(FX_BSTRC(" ")) < 0) {
+ return -1;
+ }
+ if ((len = pFile->AppendDWord(pElement->GetObjNum())) < 0) {
+ return -1;
+ }
+ if (pFile->AppendString(FX_BSTRC(" 0 R")) < 0) {
+ return -1;
+ }
+ offset += len + 5;
+ } else {
+ if (PDF_CreatorAppendObject(pElement, pFile, offset) < 0) {
+ return -1;
+ }
+ }
+ }
+ if (pFile->AppendString(FX_BSTRC("]")) < 0) {
+ return -1;
+ }
+ offset += 1;
+ break;
+ }
+ case PDFOBJ_DICTIONARY: {
+ if (pFile->AppendString(FX_BSTRC("<<")) < 0) {
+ return -1;
+ }
+ offset += 2;
+ CPDF_Dictionary* p = (CPDF_Dictionary*)pObj;
+ FX_POSITION pos = p->GetStartPos();
+ while (pos) {
+ CFX_ByteString key;
+ CPDF_Object* pValue = p->GetNextElement(pos, key);
+ if (pFile->AppendString(FX_BSTRC("/")) < 0) {
+ return -1;
+ }
+ if ((len = pFile->AppendString(PDF_NameEncode(key))) < 0) {
+ return -1;
+ }
+ offset += len + 1;
+ if (pValue->GetObjNum()) {
+ if (pFile->AppendString(FX_BSTRC(" ")) < 0) {
+ return -1;
+ }
+ if ((len = pFile->AppendDWord(pValue->GetObjNum())) < 0) {
+ return -1;
+ }
+ if (pFile->AppendString(FX_BSTRC(" 0 R")) < 0) {
+ return -1;
+ }
+ offset += len + 5;
+ } else {
+ if (PDF_CreatorAppendObject(pValue, pFile, offset) < 0) {
+ return -1;
+ }
+ }
+ }
+ if (pFile->AppendString(FX_BSTRC(">>")) < 0) {
+ return -1;
+ }
+ offset += 2;
+ break;
+ }
+ case PDFOBJ_STREAM: {
+ CPDF_Stream* p = (CPDF_Stream*)pObj;
+ if (PDF_CreatorAppendObject(p->GetDict(), pFile, offset) < 0) {
+ return -1;
+ }
+ if (pFile->AppendString(FX_BSTRC("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(FX_BSTRC("\r\nendstream"))) < 0) {
+ return -1;
+ }
+ offset += len;
+ break;
+ }
+ default:
+ ASSERT(FALSE);
+ break;
+ }
+ return 1;
+}
+FX_INT32 PDF_CreatorWriteTrailer(CPDF_Document* pDocument, CFX_FileBufferArchive* pFile, CPDF_Array* pIDArray, FX_BOOL bCompress)
+{
+ FX_FILESIZE offset = 0;
+ FX_INT32 len = 0;
+ FXSYS_assert(pDocument && pFile);
+ CPDF_Parser *pParser = (CPDF_Parser*)pDocument->GetParser();
+ if (pParser) {
+ CPDF_Dictionary* p = pParser->GetTrailer();
+ FX_POSITION pos = p->GetStartPos();
+ while (pos) {
+ CFX_ByteString key;
+ CPDF_Object* pValue = p->GetNextElement(pos, key);
+ if (key == FX_BSTRC("Encrypt") || key == FX_BSTRC("Size") || key == FX_BSTRC("Filter") ||
+ key == FX_BSTRC("Index") || key == FX_BSTRC("Length") || key == FX_BSTRC("Prev") ||
+ key == FX_BSTRC("W") || key == FX_BSTRC("XRefStm") || key == FX_BSTRC("Type") || key == FX_BSTRC("ID")) {
+ continue;
+ }
+ if (bCompress && key == FX_BSTRC("DecodeParms")) {
+ continue;
+ }
+ if (pFile->AppendString((FX_BSTRC("/"))) < 0) {
+ return -1;
+ }
+ if ((len = pFile->AppendString(PDF_NameEncode(key))) < 0) {
+ return -1;
+ }
+ offset += len + 1;
+ if (pValue->GetObjNum()) {
+ if (pFile->AppendString(FX_BSTRC(" ")) < 0) {
+ return -1;
+ }
+ if ((len = pFile->AppendDWord(pValue->GetObjNum())) < 0) {
+ return -1;
+ }
+ if (pFile->AppendString(FX_BSTRC(" 0 R ")) < 0) {
+ return -1;
+ }
+ offset += len + 6;
+ } else {
+ if (PDF_CreatorAppendObject(pValue, pFile, offset) < 0) {
+ return -1;
+ }
+ }
+ }
+ if (pIDArray) {
+ if (pFile->AppendString((FX_BSTRC("/ID"))) < 0) {
+ return -1;
+ }
+ offset += 3;
+ if (PDF_CreatorAppendObject(pIDArray, pFile, offset) < 0) {
+ return -1;
+ }
+ }
+ return offset;
+ }
+ if (pFile->AppendString(FX_BSTRC("\r\n/Root ")) < 0) {
+ return -1;
+ }
+ if ((len = pFile->AppendDWord(pDocument->GetRoot()->GetObjNum())) < 0) {
+ return -1;
+ }
+ if (pFile->AppendString(FX_BSTRC(" 0 R\r\n")) < 0) {
+ return -1;
+ }
+ offset += len + 14;
+ if (pDocument->GetInfo()) {
+ if (pFile->AppendString(FX_BSTRC("/Info ")) < 0) {
+ return -1;
+ }
+ if ((len = pFile->AppendDWord(pDocument->GetInfo()->GetObjNum())) < 0) {
+ return -1;
+ }
+ if (pFile->AppendString(FX_BSTRC(" 0 R\r\n")) < 0) {
+ return -1;
+ }
+ offset += len + 12;
+ }
+ if (pIDArray) {
+ if (pFile->AppendString((FX_BSTRC("/ID"))) < 0) {
+ return -1;
+ }
+ offset += 3;
+ if (PDF_CreatorAppendObject(pIDArray, pFile, offset) < 0) {
+ return -1;
+ }
+ }
+ return offset;
+}
+FX_INT32 PDF_CreatorWriteEncrypt(const CPDF_Dictionary* pEncryptDict, FX_DWORD dwObjNum, CFX_FileBufferArchive *pFile)
+{
+ if (!pEncryptDict) {
+ return 0;
+ }
+ FXSYS_assert(pFile);
+ FX_FILESIZE offset = 0;
+ FX_INT32 len = 0;
+ if (pFile->AppendString(FX_BSTRC("/Encrypt")) < 0) {
+ return -1;
+ }
+ offset += 8;
+ if (pFile->AppendString(FX_BSTRC(" ")) < 0) {
+ return -1;
+ }
+ if ((len = pFile->AppendDWord(dwObjNum)) < 0) {
+ return -1;
+ }
+ if (pFile->AppendString(FX_BSTRC(" 0 R ")) < 0) {
+ return -1;
+ }
+ offset += len + 6;
+ return offset;
+}
+FX_BOOL PDF_GenerateFileID(FX_DWORD dwSeed1, FX_DWORD dwSeed2, FX_LPDWORD pBuffer)
+{
+ if (!pBuffer) {
+ return FALSE;
+ }
+ FX_LPVOID pContext = FX_Random_MT_Start(dwSeed1);
+ FX_INT32 i = 0;
+ for (i = 0; i < 2; i++) {
+ *pBuffer++ = FX_Random_MT_Generate(pContext);
+ }
+ FX_Random_MT_Close(pContext);
+ pContext = FX_Random_MT_Start(dwSeed2);
+ for (i = 0; i < 2; i++) {
+ *pBuffer++ = FX_Random_MT_Generate(pContext);
+ }
+ FX_Random_MT_Close(pContext);
+ return TRUE;
+}
+class CPDF_FlateEncoder
+{
+public:
+ CPDF_FlateEncoder();
+ ~CPDF_FlateEncoder();
+ FX_BOOL Initialize(CPDF_Stream* pStream, FX_BOOL bFlateEncode);
+ FX_BOOL Initialize(FX_LPCBYTE pBuffer, FX_DWORD size, FX_BOOL bFlateEncode, FX_BOOL bXRefStream = FALSE);
+ void CloneDict();
+ FX_LPBYTE 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 = (CPDF_Dictionary*)m_pDict->Clone();
+ m_bCloned = TRUE;
+ }
+}
+FX_BOOL CPDF_FlateEncoder::Initialize(CPDF_Stream* pStream, FX_BOOL bFlateEncode)
+{
+ m_Acc.LoadAllData(pStream, TRUE);
+ if (pStream->GetDict()->KeyExist("Filter") || !bFlateEncode) {
+ if (pStream->GetDict()->KeyExist("Filter") && !bFlateEncode) {
+ CPDF_StreamAcc destAcc;
+ destAcc.LoadAllData(pStream);
+ m_dwSize = destAcc.GetSize();
+ m_pData = (FX_LPBYTE)destAcc.DetachData();
+ m_pDict = (CPDF_Dictionary*)pStream->GetDict()->Clone();
+ m_pDict->RemoveAt(FX_BSTRC("Filter"));
+ m_bNewData = TRUE;
+ m_bCloned = TRUE;
+ } else {
+ m_pData = (FX_LPBYTE)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 = (CPDF_Dictionary*)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(FX_LPCBYTE pBuffer, FX_DWORD size, FX_BOOL bFlateEncode, FX_BOOL bXRefStream)
+{
+ if (!bFlateEncode) {
+ m_pData = (FX_LPBYTE)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 && m_pData) {
+ FX_Free(m_pData);
+ }
+}
+class CPDF_Encryptor
+{
+public:
+ CPDF_Encryptor();
+ ~CPDF_Encryptor();
+ FX_BOOL Initialize(CPDF_CryptoHandler* pHandler, int objnum, FX_LPBYTE src_data, FX_DWORD src_size);
+ FX_LPBYTE 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(CPDF_CryptoHandler* pHandler, int objnum, FX_LPBYTE src_data, FX_DWORD src_size)
+{
+ if (src_size == 0) {
+ return TRUE;
+ }
+ if (pHandler == NULL) {
+ m_pData = (FX_LPBYTE)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(FX_BYTE, m_dwSize);
+ if(!m_pData) {
+ return FALSE;
+ }
+ 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);
+ }
+}
+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;
+}
+FX_INT32 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;
+}
+FX_INT32 CPDF_ObjectStream::CompressIndirectObject(FX_DWORD dwObjNum, FX_LPCBYTE 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;
+ CPDF_CryptoHandler *pHandler = pCreator->m_pCryptoHandler;
+ FX_FILESIZE ObjOffset = pCreator->m_Offset;
+ if (!m_dwObjNum) {
+ m_dwObjNum = ++pCreator->m_dwLastObjNum;
+ }
+ CFX_ByteTextBuf tempBuffer;
+ FX_INT32 iCount = m_ObjNumArray.GetSize();
+ for (FX_INT32 i = 0; i < iCount; i++) {
+ tempBuffer << m_ObjNumArray.ElementAt(i) << FX_BSTRC(" ") << m_OffsetArray.ElementAt(i) << FX_BSTRC(" ");
+ }
+ FX_FILESIZE &offset = pCreator->m_Offset;
+ FX_INT32 len = pFile->AppendDWord(m_dwObjNum);
+ if (len < 0) {
+ return -1;
+ }
+ offset += len;
+ if ((len = pFile->AppendString(FX_BSTRC(" 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(FX_BSTRC("/First ")) < 0) {
+ return -1;
+ }
+ if ((len = pFile->AppendDWord((FX_DWORD)tempBuffer.GetLength())) < 0) {
+ return -1;
+ }
+ if (pFile->AppendString(FX_BSTRC("/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(FX_BSTRC(">>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(FX_BSTRC("/Filter /FlateDecode")) < 0) {
+ return -1;
+ }
+ offset += 20;
+ }
+ if ((len = pFile->AppendString(FX_BSTRC(">>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(FX_BSTRC("\r\nendstream\r\nendobj\r\n"))) < 0) {
+ return -1;
+ }
+ offset += len;
+ return ObjOffset;
+}
+CPDF_XRefStream::CPDF_XRefStream()
+ : m_PrevOffset(0)
+ , m_iSeg(0)
+ , m_dwTempObjNum(0)
+{
+}
+FX_BOOL CPDF_XRefStream::Start()
+{
+ m_IndexArray.RemoveAll();
+ m_Buffer.Clear();
+ m_iSeg = 0;
+ return TRUE;
+}
+FX_INT32 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);
+}
+FX_INT32 CPDF_XRefStream::CompressIndirectObject(FX_DWORD dwObjNum, FX_LPCBYTE 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);
+}
+static void _AppendIndex0(CFX_ByteTextBuf& buffer, FX_BOOL bFirstObject = TRUE)
+{
+ buffer.AppendByte(0);
+ buffer.AppendByte(0);
+ buffer.AppendByte(0);
+ buffer.AppendByte(0);
+ buffer.AppendByte(0);
+ if (bFirstObject) {
+ buffer.AppendByte(0xFF);
+ buffer.AppendByte(0xFF);
+ } else {
+ buffer.AppendByte(0);
+ buffer.AppendByte(0);
+ }
+}
+static 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);
+}
+static void _AppendIndex2(CFX_ByteTextBuf& buffer, FX_DWORD objnum, FX_INT32 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));
+}
+FX_INT32 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;
+ }
+ FX_INT32 iSize = m_ObjStream.m_ObjNumArray.GetSize();
+ FX_INT32 iSeg = m_IndexArray.GetSize() / 2;
+ if (!(pCreator->m_dwFlags & FPDFCREATE_INCREMENTAL)) {
+ if (m_dwTempObjNum == 0) {
+ _AppendIndex0(m_Buffer);
+ m_dwTempObjNum++;
+ }
+ FX_DWORD end_num = m_IndexArray.GetAt((iSeg - 1) * 2) + m_IndexArray.GetAt((iSeg - 1) * 2 + 1);
+ 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_ObjectSize.Add(dwObjStmNum, 1);
+ pCreator->m_ObjectOffset[dwObjStmNum] = objOffset;
+ }
+ m_iSeg = iSeg;
+ if (bEOF) {
+ m_ObjStream.Start();
+ }
+ return 1;
+ }
+ FX_INT32 &j = m_ObjStream.m_index;
+ for (int i = m_iSeg; i < iSeg; i++) {
+ FX_DWORD start = m_IndexArray.ElementAt(i * 2);
+ FX_DWORD end = m_IndexArray.ElementAt(i * 2 + 1) + start;
+ for (FX_DWORD m = start; m < end; m++) {
+ if (j >= iSize || m != m_ObjStream.m_ObjNumArray.ElementAt(j)) {
+ _AppendIndex1(m_Buffer, pCreator->m_ObjectOffset[m]);
+ } else {
+ _AppendIndex2(m_Buffer, dwObjStmNum, j++);
+ }
+ }
+ }
+ if (iSize > 0 && bEOF) {
+ _AppendIndex1(m_Buffer, objOffset);
+ m_IndexArray.Add(dwObjStmNum);
+ m_IndexArray.Add(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;
+ FX_INT32 len = pFile->AppendDWord(objnum);
+ if (len < 0) {
+ return FALSE;
+ }
+ offset += len;
+ if ((len = pFile->AppendString(FX_BSTRC(" 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(FX_BSTRC(" "))) < 0) {
+ return FALSE;
+ }
+ offset += len + 1;
+ if ((len = pFile->AppendDWord(objnum + 1)) < 0) {
+ return FALSE;
+ }
+ offset += len;
+ } else {
+ FX_INT32 iSeg = m_IndexArray.GetSize() / 2;
+ for (FX_INT32 i = 0; i < iSeg; i++) {
+ if ((len = pFile->AppendDWord(m_IndexArray.ElementAt(i * 2))) < 0) {
+ return FALSE;
+ }
+ if (pFile->AppendString(FX_BSTRC(" ")) < 0) {
+ return FALSE;
+ }
+ offset += len + 1;
+ if ((len = pFile->AppendDWord(m_IndexArray.ElementAt(i * 2 + 1))) < 0) {
+ return FALSE;
+ }
+ if (pFile->AppendString(FX_BSTRC(" ")) < 0) {
+ return FALSE;
+ }
+ offset += len + 1;
+ }
+ }
+ if (pFile->AppendString(FX_BSTRC("]/Size ")) < 0) {
+ return FALSE;
+ }
+ if ((len = pFile->AppendDWord(objnum + 1)) < 0) {
+ return FALSE;
+ }
+ offset += len + 7;
+ if (m_PrevOffset > 0) {
+ if (pFile->AppendString(FX_BSTRC("/Prev ")) < 0) {
+ return -1;
+ }
+ FX_CHAR offset_buf[20];
+ FXSYS_memset32(offset_buf, 0, sizeof(offset_buf));
+ FXSYS_i64toa(m_PrevOffset, offset_buf, 10);
+ FX_INT32 len = (FX_INT32)FXSYS_strlen(offset_buf);
+ if (pFile->AppendBlock(offset_buf, len) < 0) {
+ return -1;
+ }
+ 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(FX_BSTRC("/Filter /FlateDecode")) < 0) {
+ return FALSE;
+ }
+ offset += 20;
+ if (bPredictor) {
+ if ((len = pFile->AppendString(FX_BSTRC("/DecodeParms<</Columns 7/Predictor 12>>"))) < 0) {
+ return FALSE;
+ }
+ offset += len;
+ }
+ }
+ if (pFile->AppendString(FX_BSTRC("/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 -1;
+ }
+ 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 -1;
+ }
+ offset += len;
+ }
+ }
+ if ((len = pFile->AppendString(FX_BSTRC(">>stream\r\n"))) < 0) {
+ return FALSE;
+ }
+ offset += len;
+ if (pFile->AppendBlock(encoder.m_pData, encoder.m_dwSize) < 0) {
+ return FALSE;
+ }
+ if ((len = pFile->AppendString(FX_BSTRC("\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);
+ 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 {
+ FX_INT32 iSeg = m_IndexArray.GetSize() / 2;
+ for (int i = 0; i < iSeg; i++) {
+ FX_DWORD start = m_IndexArray.ElementAt(i * 2);
+ FX_DWORD end = m_IndexArray.ElementAt(i * 2 + 1) + start;
+ for (FX_DWORD j = start; j < end; j++) {
+ _AppendIndex1(m_Buffer, pCreator->m_ObjectOffset[j]);
+ }
+ }
+ }
+ return GenerateXRefStream(pCreator, FALSE);
+}
+FX_BOOL CPDF_XRefStream::AddObjectNumberToIndexArray(FX_DWORD objnum)
+{
+ FX_INT32 iSize = m_IndexArray.GetSize();
+ if (iSize == 0) {
+ m_IndexArray.Add(objnum);
+ m_IndexArray.Add(1);
+ } else {
+ FXSYS_assert(iSize > 1);
+ FX_DWORD startobjnum = m_IndexArray.ElementAt(iSize - 2);
+ FX_INT32 iCount = m_IndexArray.ElementAt(iSize - 1);
+ if (objnum == startobjnum + iCount) {
+ m_IndexArray[iSize - 1] = iCount + 1;
+ } else {
+ m_IndexArray.Add(objnum);
+ m_IndexArray.Add(1);
+ }
+ }
+ return TRUE;
+}
+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();
+}
+static FX_BOOL _IsXRefNeedEnd(CPDF_XRefStream* pXRef, FX_DWORD flag)
+{
+ if (!(flag & FPDFCREATE_INCREMENTAL)) {
+ return FALSE;
+ }
+ FX_INT32 iSize = pXRef->m_IndexArray.GetSize() / 2;
+ FX_INT32 iCount = 0;
+ for (FX_INT32 i = 0; i < iSize; i++) {
+ iCount += pXRef->m_IndexArray.ElementAt(i * 2 + 1);
+ }
+ return (iCount >= PDF_XREFSTREAM_MAXSIZE);
+}
+FX_INT32 CPDF_Creator::WriteIndirectObjectToStream(const CPDF_Object* pObj)
+{
+ if (!m_pXRefStream) {
+ return 1;
+ }
+ FX_DWORD objnum = pObj->GetObjNum();
+ if (m_pParser && m_pParser->m_ObjVersion.GetSize() > (FX_INT32)objnum && m_pParser->m_ObjVersion[objnum] > 0) {
+ return 1;
+ }
+ if (pObj->GetType() == PDFOBJ_NUMBER) {
+ return 1;
+ }
+ CPDF_Dictionary *pDict = pObj->GetDict();
+ if (pObj->GetType() == PDFOBJ_STREAM) {
+ if (pDict && pDict->GetString(FX_BSTRC("Type")) == FX_BSTRC("XRef")) {
+ return 0;
+ }
+ return 1;
+ }
+ if (pDict) {
+ if (pDict == m_pDocument->m_pRootDict || pDict == m_pEncryptDict) {
+ return 1;
+ }
+ if (IsSignatureDict(pDict)) {
+ return 1;
+ }
+ if (pDict->GetString(FX_BSTRC("Type")) == FX_BSTRC("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;
+}
+FX_INT32 CPDF_Creator::WriteIndirectObjectToStream(FX_DWORD objnum, FX_LPCBYTE pBuffer, FX_DWORD dwSize)
+{
+ if (!m_pXRefStream) {
+ return 1;
+ }
+ m_pXRefStream->AddObjectNumberToIndexArray(objnum);
+ FX_INT32 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;
+}
+FX_INT32 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;
+}
+FX_INT32 CPDF_Creator::WriteStream(const CPDF_Object* pStream, FX_DWORD objnum, CPDF_CryptoHandler* pCrypto)
+{
+ CPDF_FlateEncoder encoder;
+ encoder.Initialize((CPDF_Stream*)pStream, 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->GetInteger(FX_BSTRC("Length")) != encryptor.m_dwSize) {
+ encoder.CloneDict();
+ encoder.m_pDict->SetAtInteger(FX_BSTRC("Length"), encryptor.m_dwSize);
+ }
+ if (WriteDirectObj(objnum, encoder.m_pDict) < 0) {
+ return -1;
+ }
+ int len = m_File.AppendString(FX_BSTRC("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(FX_BSTRC("\r\nendstream"))) < 0) {
+ return -1;
+ }
+ m_Offset += len;
+ return 1;
+}
+FX_INT32 CPDF_Creator::WriteIndirectObj(FX_DWORD objnum, const CPDF_Object* pObj)
+{
+ FX_INT32 len = m_File.AppendDWord(objnum);
+ if (len < 0) {
+ return -1;
+ }
+ m_Offset += len;
+ if ((len = m_File.AppendString(FX_BSTRC(" 0 obj\r\n"))) < 0) {
+ return -1;
+ }
+ m_Offset += len;
+ if (pObj->GetType() == PDFOBJ_STREAM) {
+ CPDF_CryptoHandler *pHandler = NULL;
+ 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(FX_BSTRC("\r\nendobj\r\n"))) < 0) {
+ return -1;
+ }
+ m_Offset += len;
+ if (AppendObjectNumberToXRef(objnum) < 0) {
+ return -1;
+ }
+ return 0;
+}
+FX_INT32 CPDF_Creator::WriteIndirectObj(const CPDF_Object* pObj)
+{
+ FX_INT32 iRet = WriteIndirectObjectToStream(pObj);
+ if (iRet < 1) {
+ return iRet;
+ }
+ return WriteIndirectObj(pObj->GetObjNum(), pObj);
+}
+FX_INT32 CPDF_Creator::WriteDirectObj(FX_DWORD objnum, const CPDF_Object* pObj, FX_BOOL bEncrypt)
+{
+ FX_INT32 len = 0;
+ if (pObj == NULL) {
+ if (m_File.AppendString(FX_BSTRC(" null")) < 0) {
+ return -1;
+ }
+ m_Offset += 5;
+ return 1;
+ }
+ switch (pObj->GetType()) {
+ case PDFOBJ_NULL:
+ if (m_File.AppendString(FX_BSTRC(" null")) < 0) {
+ return -1;
+ }
+ m_Offset += 5;
+ break;
+ case PDFOBJ_BOOLEAN:
+ case PDFOBJ_NUMBER:
+ if (m_File.AppendString(FX_BSTRC(" ")) < 0) {
+ return -1;
+ }
+ if ((len = m_File.AppendString(pObj->GetString())) < 0) {
+ return -1;
+ }
+ m_Offset += len + 1;
+ break;
+ case PDFOBJ_STRING: {
+ CFX_ByteString str = pObj->GetString();
+ FX_BOOL bHex = ((CPDF_String*)pObj)->IsHex();
+ if (m_pCryptoHandler == NULL || !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, (FX_LPBYTE)(FX_LPCSTR)str, str.GetLength());
+ CFX_ByteString content = PDF_EncodeString(CFX_ByteString((FX_LPCSTR)encryptor.m_pData, encryptor.m_dwSize), bHex);
+ if ((len = m_File.AppendString(content)) < 0) {
+ return -1;
+ }
+ m_Offset += len;
+ break;
+ }
+ case PDFOBJ_STREAM: {
+ CPDF_FlateEncoder encoder;
+ encoder.Initialize((CPDF_Stream*)pObj, m_bCompress);
+ CPDF_Encryptor encryptor;
+ CPDF_CryptoHandler* pHandler = m_pCryptoHandler;
+ encryptor.Initialize(pHandler, objnum, encoder.m_pData, encoder.m_dwSize);
+ if ((FX_DWORD)encoder.m_pDict->GetInteger(FX_BSTRC("Length")) != encryptor.m_dwSize) {
+ encoder.CloneDict();
+ encoder.m_pDict->SetAtInteger(FX_BSTRC("Length"), encryptor.m_dwSize);
+ }
+ if (WriteDirectObj(objnum, encoder.m_pDict) < 0) {
+ return -1;
+ }
+ if ((len = m_File.AppendString(FX_BSTRC("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(FX_BSTRC("\r\nendstream"))) < 0) {
+ return -1;
+ }
+ m_Offset += len;
+ break;
+ }
+ case PDFOBJ_NAME: {
+ if (m_File.AppendString(FX_BSTRC("/")) < 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 PDFOBJ_REFERENCE: {
+ if (m_File.AppendString(FX_BSTRC(" ")) < 0) {
+ return -1;
+ }
+ CPDF_Reference* p = (CPDF_Reference*)pObj;
+ if ((len = m_File.AppendDWord(p->GetRefObjNum())) < 0) {
+ return -1;
+ }
+ if (m_File.AppendString(FX_BSTRC(" 0 R")) < 0) {
+ return -1;
+ }
+ m_Offset += len + 5;
+ break;
+ }
+ case PDFOBJ_ARRAY: {
+ if (m_File.AppendString(FX_BSTRC("[")) < 0) {
+ return -1;
+ }
+ m_Offset += 1;
+ CPDF_Array* p = (CPDF_Array*)pObj;
+ for (FX_DWORD i = 0; i < p->GetCount(); i ++) {
+ CPDF_Object* pElement = p->GetElement(i);
+ if (pElement->GetObjNum()) {
+ if (m_File.AppendString(FX_BSTRC(" ")) < 0) {
+ return -1;
+ }
+ if ((len = m_File.AppendDWord(pElement->GetObjNum())) < 0) {
+ return -1;
+ }
+ if (m_File.AppendString(FX_BSTRC(" 0 R")) < 0) {
+ return -1;
+ }
+ m_Offset += len + 5;
+ } else {
+ if (WriteDirectObj(objnum, pElement) < 0) {
+ return -1;
+ }
+ }
+ }
+ if (m_File.AppendString(FX_BSTRC("]")) < 0) {
+ return -1;
+ }
+ m_Offset += 1;
+ break;
+ }
+ case PDFOBJ_DICTIONARY: {
+ if (m_pCryptoHandler == NULL || pObj == m_pEncryptDict) {
+ return PDF_CreatorAppendObject(pObj, &m_File, m_Offset);
+ }
+ if (m_File.AppendString(FX_BSTRC("<<")) < 0) {
+ return -1;
+ }
+ m_Offset += 2;
+ CPDF_Dictionary* p = (CPDF_Dictionary*)pObj;
+ FX_BOOL bSignDict = IsSignatureDict(p);
+ FX_POSITION pos = p->GetStartPos();
+ while (pos) {
+ FX_BOOL bSignValue = FALSE;
+ CFX_ByteString key;
+ CPDF_Object* pValue = p->GetNextElement(pos, key);
+ if (m_File.AppendString(FX_BSTRC("/")) < 0) {
+ return -1;
+ }
+ if ((len = m_File.AppendString(PDF_NameEncode(key))) < 0) {
+ return -1;
+ }
+ m_Offset += len + 1;
+ if (bSignDict && key == FX_BSTRC("Contents")) {
+ bSignValue = TRUE;
+ }
+ if (pValue->GetObjNum()) {
+ if (m_File.AppendString(FX_BSTRC(" ")) < 0) {
+ return -1;
+ }
+ if ((len = m_File.AppendDWord(pValue->GetObjNum())) < 0) {
+ return -1;
+ }
+ if (m_File.AppendString(FX_BSTRC(" 0 R ")) < 0) {
+ return -1;
+ }
+ m_Offset += len + 6;
+ } else {
+ if (WriteDirectObj(objnum, pValue, !bSignValue) < 0) {
+ return -1;
+ }
+ }
+ }
+ if (m_File.AppendString(FX_BSTRC(">>")) < 0) {
+ return -1;
+ }
+ m_Offset += 2;
+ break;
+ }
+ }
+ return 1;
+}
+FX_INT32 CPDF_Creator::WriteOldIndirectObject(FX_DWORD objnum)
+{
+ if(m_pParser->m_V5Type[objnum] == 0 || m_pParser->m_V5Type[objnum] == 255) {
+ return 0;
+ }
+ m_ObjectOffset[objnum] = m_Offset;
+ FX_LPVOID valuetemp = NULL;
+ FX_BOOL bExistInMap = m_pDocument->m_IndirectObjs.Lookup((FX_LPVOID)(FX_UINTPTR)objnum, valuetemp);
+ FX_BOOL bObjStm = (m_pParser->m_V5Type[objnum] == 2) && m_pEncryptDict && !m_pXRefStream;
+ if(m_pParser->m_bVersionUpdated || m_bSecurityChanged || bExistInMap || bObjStm) {
+ CPDF_Object* pObj = m_pDocument->GetIndirectObject(objnum);
+ if (pObj == NULL) {
+ m_ObjectOffset[objnum] = 0;
+ m_ObjectSize[objnum] = 0;
+ return 0;
+ }
+ if (WriteIndirectObj(pObj)) {
+ return -1;
+ }
+ if (!bExistInMap) {
+ m_pDocument->ReleaseIndirectObject(objnum);
+ }
+ } else {
+ FX_BYTE* pBuffer;
+ FX_DWORD size;
+ m_pParser->GetIndirectBinary(objnum, pBuffer, size);
+ if (pBuffer == NULL) {
+ return 0;
+ }
+ if (m_pParser->m_V5Type[objnum] == 2) {
+ if (m_pXRefStream) {
+ if (WriteIndirectObjectToStream(objnum, pBuffer, size) < 0) {
+ FX_Free(pBuffer);
+ return -1;
+ }
+ } else {
+ FX_INT32 len = m_File.AppendDWord(objnum);
+ if (len < 0) {
+ return -1;
+ }
+ if (m_File.AppendString(FX_BSTRC(" 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(FX_BSTRC("\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;
+}
+FX_INT32 CPDF_Creator::WriteOldObjs(IFX_Pause *pPause)
+{
+ FX_DWORD nOldSize = m_pParser->m_CrossRef.GetSize();
+ FX_DWORD objnum = (FX_DWORD)(FX_UINTPTR)m_Pos;
+ for(; objnum < nOldSize; objnum ++) {
+ FX_INT32 iRet = WriteOldIndirectObject(objnum);
+ if (!iRet) {
+ continue;
+ }
+ if (iRet < 0) {
+ return iRet;
+ }
+ m_ObjectSize[objnum] = (FX_DWORD)(m_Offset - m_ObjectOffset[objnum]);
+ if (pPause && pPause->NeedToPauseNow()) {
+ m_Pos = (FX_LPVOID)(FX_UINTPTR)(objnum + 1);
+ return 1;
+ }
+ }
+ return 0;
+}
+FX_INT32 CPDF_Creator::WriteNewObjs(FX_BOOL bIncremental, IFX_Pause *pPause)
+{
+ FX_INT32 iCount = m_NewObjNumArray.GetSize();
+ FX_INT32 index = (FX_INT32)(FX_UINTPTR)m_Pos;
+ while (index < iCount) {
+ FX_DWORD objnum = m_NewObjNumArray.ElementAt(index);
+ CPDF_Object *pObj = NULL;
+ m_pDocument->m_IndirectObjs.Lookup((FX_LPVOID)(FX_UINTPTR)objnum, (FX_LPVOID&)pObj);
+ if (NULL == pObj) {
+ ++index;
+ continue;
+ }
+ m_ObjectOffset[objnum] = m_Offset;
+ if (WriteIndirectObj(pObj)) {
+ return -1;
+ }
+ m_ObjectSize[objnum] = (FX_DWORD)(m_Offset - m_ObjectOffset[objnum]);
+ index++;
+ if (pPause && pPause->NeedToPauseNow()) {
+ m_Pos = (FX_POSITION)(FX_UINTPTR)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->m_V5Type[dwStart] == 0 || m_pParser->m_V5Type[dwStart] == 255)) {
+ dwStart++;
+ }
+ if (dwStart > dwEnd) {
+ break;
+ }
+ j = dwStart;
+ while (j <= dwEnd && m_pParser->m_V5Type[j] != 0 && m_pParser->m_V5Type[j] != 255) {
+ j++;
+ }
+ m_ObjectOffset.Add(dwStart, j - dwStart);
+ m_ObjectSize.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;
+ FX_DWORD nOldSize = m_pParser ? m_pParser->m_CrossRef.GetSize() : 0;
+ FX_POSITION pos = m_pDocument->m_IndirectObjs.GetStartPosition();
+ while (pos) {
+ size_t key = 0;
+ CPDF_Object* pObj;
+ m_pDocument->m_IndirectObjs.GetNextAssoc(pos, (FX_LPVOID&)key, (FX_LPVOID&)pObj);
+ FX_DWORD objnum = (FX_DWORD)key;
+ if (pObj->GetObjNum() == -1) {
+ continue;
+ }
+ if (bIncremental) {
+ if (!pObj->IsModified()) {
+ continue;
+ }
+ } else {
+ if (objnum < nOldSize && m_pParser->m_V5Type[objnum] != 0) {
+ continue;
+ }
+ }
+ AppendNewObjNum(objnum);
+ }
+ FX_INT32 iCount = m_NewObjNumArray.GetSize();
+ if (iCount == 0) {
+ return;
+ }
+ FX_INT32 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);
+ FX_BOOL bExist = (dwCurObjNum < nOldSize && m_ObjectOffset.GetPtrAt(dwCurObjNum) != NULL);
+ if (bExist || dwCurObjNum - dwLastObjNum > 1) {
+ if (!bNewStart) {
+ m_ObjectOffset.Add(dwStartObjNum, dwLastObjNum - dwStartObjNum + 1);
+ m_ObjectSize.Add(dwStartObjNum, dwLastObjNum - dwStartObjNum + 1);
+ }
+ dwStartObjNum = dwCurObjNum;
+ }
+ if (bNewStart) {
+ dwStartObjNum = dwCurObjNum;
+ }
+ bNewStart = bExist;
+ dwLastObjNum = dwCurObjNum;
+ }
+ m_ObjectOffset.Add(dwStartObjNum, dwLastObjNum - dwStartObjNum + 1);
+ m_ObjectSize.Add(dwStartObjNum, dwLastObjNum - dwStartObjNum + 1);
+}
+void CPDF_Creator::AppendNewObjNum(FX_DWORD objbum)
+{
+ FX_INT32 iStart = 0, iFind = 0;
+ FX_INT32 iEnd = m_NewObjNumArray.GetUpperBound();
+ while (iStart <= iEnd) {
+ FX_INT32 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);
+}
+FX_INT32 CPDF_Creator::WriteDoc_Stage1(IFX_Pause *pPause)
+{
+ FXSYS_assert(m_iStage > -1 || m_iStage < 20);
+ if (m_iStage == 0) {
+ if (m_pParser == NULL) {
+ m_dwFlags &= ~FPDFCREATE_INCREMENTAL;
+ }
+ if (m_bSecurityChanged && (m_dwFlags & FPDFCREATE_NO_ORIGINAL) == 0) {
+ m_dwFlags &= ~FPDFCREATE_INCREMENTAL;
+ }
+ m_pMetadata = m_pDocument->GetRoot()->GetElementValue(FX_BSTRC("Metadata"));
+ if (m_dwFlags & FPDFCREATE_OBJECTSTREAM) {
+ m_pXRefStream = FX_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(FX_BSTRC("%PDF-1.")) < 0) {
+ return -1;
+ }
+ m_Offset += 7;
+ FX_INT32 version = 7;
+ if (m_FileVersion) {
+ version = m_FileVersion;
+ } else if (m_pParser) {
+ version = m_pParser->GetFileVersion();
+ }
+ FX_INT32 len = m_File.AppendDWord(version % 10);
+ if (len < 0) {
+ return -1;
+ }
+ m_Offset += len;
+ if ((len = m_File.AppendString(FX_BSTRC("\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 = (FX_LPVOID)(FX_UINTPTR)m_Offset;
+ m_iStage = 15;
+ }
+ }
+ if (m_iStage == 15) {
+ if ((m_dwFlags & FPDFCREATE_NO_ORIGINAL) == 0 && m_Pos) {
+ IFX_FileRead* pSrcFile = m_pParser->GetFileAccess();
+ FX_BYTE buffer[4096];
+ FX_DWORD src_size = (FX_DWORD)(FX_UINTPTR)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 = (FX_LPVOID)(FX_UINTPTR)src_size;
+ return 1;
+ }
+ }
+ }
+ if ((m_dwFlags & FPDFCREATE_NO_ORIGINAL) == 0 && m_pParser->GetLastXRefOffset() == 0) {
+ InitOldObjNumOffsets();
+ FX_DWORD dwEnd = m_pParser->GetLastObjNum();
+ FX_BOOL bObjStm = (m_dwFlags & FPDFCREATE_OBJECTSTREAM) != 0;
+ for (FX_DWORD objnum = 0; objnum <= dwEnd; objnum++) {
+ if (m_pParser->m_V5Type[objnum] == 0 || m_pParser->m_V5Type[objnum] == 255) {
+ continue;
+ }
+ m_ObjectOffset[objnum] = m_pParser->m_CrossRef[objnum];
+ if (bObjStm) {
+ m_pXRefStream->AddObjectNumberToIndexArray(objnum);
+ }
+ }
+ if (bObjStm) {
+ m_pXRefStream->EndXRefStream(this);
+ m_pXRefStream->Start();
+ }
+ }
+ m_iStage = 20;
+ }
+ InitNewObjNumOffsets();
+ return m_iStage;
+}
+FX_INT32 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 = (FX_LPVOID)(FX_UINTPTR)0;
+ m_iStage = 21;
+ } else {
+ m_iStage = 25;
+ }
+ }
+ if (m_iStage == 21) {
+ FX_INT32 iRet = WriteOldObjs(pPause);
+ if (iRet) {
+ return iRet;
+ }
+ m_iStage = 25;
+ }
+ if (m_iStage == 25) {
+ m_Pos = (FX_LPVOID)(FX_UINTPTR)0;
+ m_iStage = 26;
+ }
+ if (m_iStage == 26) {
+ FX_INT32 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_ObjectSize.Add(m_dwLastObjNum, 1);
+ m_ObjectSize[m_dwLastObjNum] = m_Offset - saveOffset;
+ m_dwEnryptObjNum = m_dwLastObjNum;
+ if (m_dwFlags & FPDFCREATE_INCREMENTAL) {
+ m_NewObjNumArray.Add(m_dwLastObjNum);
+ }
+ }
+ m_iStage = 80;
+ }
+ return m_iStage;
+}
+FX_INT32 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) ? FX_BSTRC("xref\r\n") : FX_BSTRC("xref\r\n0 1\r\n0000000000 65536 f\r\n");
+ if (m_File.AppendString(str) < 0) {
+ return -1;
+ }
+ m_Pos = (FX_LPVOID)(FX_UINTPTR)1;
+ m_iStage = 81;
+ } else {
+ if (m_File.AppendString(FX_BSTRC("xref\r\n")) < 0) {
+ return -1;
+ }
+ m_Pos = (FX_LPVOID)(FX_UINTPTR)0;
+ m_iStage = 82;
+ }
+ } else {
+ m_iStage = 90;
+ }
+ }
+ if (m_iStage == 81) {
+ CFX_ByteString str;
+ FX_DWORD i = (FX_DWORD)(FX_UINTPTR)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 65536 f\r\n", j);
+ } else {
+ str.Format("%d %d\r\n", i, j - i);
+ }
+ if (m_File.AppendBlock((FX_LPCSTR)str, str.GetLength()) < 0) {
+ return -1;
+ }
+ while (i < j) {
+ str.Format("%010d 00000 n\r\n", m_ObjectOffset[i ++]);
+ if (m_File.AppendBlock((FX_LPCSTR)str, str.GetLength()) < 0) {
+ return -1;
+ }
+ }
+ if (i > dwLastObjNum) {
+ break;
+ }
+ if (pPause && pPause->NeedToPauseNow()) {
+ m_Pos = (FX_LPVOID)(FX_UINTPTR)i;
+ return 1;
+ }
+ }
+ m_iStage = 90;
+ }
+ if (m_iStage == 82) {
+ CFX_ByteString str;
+ FX_INT32 iCount = m_NewObjNumArray.GetSize();
+ FX_INT32 i = (FX_INT32)(FX_UINTPTR)m_Pos;
+ while (i < iCount) {
+ FX_INT32 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 65536 f\r\n", j - i + 1);
+ } else {
+ str.Format("%d %d\r\n", objnum, j - i);
+ }
+ if (m_File.AppendBlock((FX_LPCSTR)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((FX_LPCSTR)str, str.GetLength()) < 0) {
+ return -1;
+ }
+ }
+ if (pPause && (i % 100) == 0 && pPause->NeedToPauseNow()) {
+ m_Pos = (FX_LPVOID)(FX_UINTPTR)i;
+ return 1;
+ }
+ }
+ m_iStage = 90;
+ }
+ return m_iStage;
+}
+static FX_INT32 _OutPutIndex(CFX_FileBufferArchive* pFile, FX_FILESIZE offset)
+{
+ FXSYS_assert(pFile);
+ 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;
+}
+FX_INT32 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(FX_BSTRC("trailer\r\n<<")) < 0) {
+ return -1;
+ }
+ } else {
+ if (m_File.AppendDWord(m_pDocument->m_LastObjNum + 1) < 0) {
+ return -1;
+ }
+ if (m_File.AppendString(FX_BSTRC(" 0 obj <<")) < 0) {
+ return -1;
+ }
+ }
+ if (m_pParser) {
+ CPDF_Dictionary* p = m_pParser->m_pTrailer;
+ FX_POSITION pos = p->GetStartPos();
+ while (pos) {
+ CFX_ByteString key;
+ CPDF_Object* pValue = p->GetNextElement(pos, key);
+ if (key == FX_BSTRC("Encrypt") || key == FX_BSTRC("Size") || key == FX_BSTRC("Filter") ||
+ key == FX_BSTRC("Index") || key == FX_BSTRC("Length") || key == FX_BSTRC("Prev") ||
+ key == FX_BSTRC("W") || key == FX_BSTRC("XRefStm") || key == FX_BSTRC("ID")) {
+ continue;
+ }
+ if (m_File.AppendString((FX_BSTRC("/"))) < 0) {
+ return -1;
+ }
+ if (m_File.AppendString(PDF_NameEncode(key)) < 0) {
+ return -1;
+ }
+ if (pValue->GetObjNum()) {
+ if (m_File.AppendString(FX_BSTRC(" ")) < 0) {
+ return -1;
+ }
+ if (m_File.AppendDWord(pValue->GetObjNum()) < 0) {
+ return -1;
+ }
+ if (m_File.AppendString(FX_BSTRC(" 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(FX_BSTRC("\r\n/Root ")) < 0) {
+ return -1;
+ }
+ if (m_File.AppendDWord(m_pDocument->m_pRootDict->GetObjNum()) < 0) {
+ return -1;
+ }
+ if (m_File.AppendString(FX_BSTRC(" 0 R\r\n")) < 0) {
+ return -1;
+ }
+ if (m_pDocument->m_pInfoDict) {
+ if (m_File.AppendString(FX_BSTRC("/Info ")) < 0) {
+ return -1;
+ }
+ if (m_File.AppendDWord(m_pDocument->m_pInfoDict->GetObjNum()) < 0) {
+ return -1;
+ }
+ if (m_File.AppendString(FX_BSTRC(" 0 R\r\n")) < 0) {
+ return -1;
+ }
+ }
+ }
+ if (m_pEncryptDict) {
+ if (m_File.AppendString(FX_BSTRC("/Encrypt")) < 0) {
+ return -1;
+ }
+ FX_DWORD dwObjNum = m_pEncryptDict->GetObjNum();
+ if (dwObjNum == 0) {
+ dwObjNum = m_pDocument->GetLastObjNum() + 1;
+ }
+ if (m_File.AppendString(FX_BSTRC(" ")) < 0) {
+ return -1;
+ }
+ if (m_File.AppendDWord(dwObjNum) < 0) {
+ return -1;
+ }
+ if (m_File.AppendString(FX_BSTRC(" 0 R ")) < 0) {
+ return -1;
+ }
+ }
+ if (m_File.AppendString(FX_BSTRC("/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(FX_BSTRC("/Prev ")) < 0) {
+ return -1;
+ }
+ FX_CHAR offset_buf[20];
+ FXSYS_memset32(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((FX_BSTRC("/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(FX_BSTRC(">>")) < 0) {
+ return -1;
+ }
+ } else {
+ if (m_File.AppendString(FX_BSTRC("/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(FX_BSTRC(" 1 ")) < 0) {
+ return -1;
+ }
+ }
+ if (m_File.AppendString(FX_BSTRC("]/Length ")) < 0) {
+ return -1;
+ }
+ if (m_File.AppendDWord(m_dwLastObjNum * 5) < 0) {
+ return -1;
+ }
+ if (m_File.AppendString(FX_BSTRC(">>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();
+ FX_INT32 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(FX_BSTRC(" 1 ")) < 0) {
+ return -1;
+ }
+ }
+ if (m_File.AppendString(FX_BSTRC("]/Length ")) < 0) {
+ return -1;
+ }
+ if (m_File.AppendDWord(count * 5) < 0) {
+ return -1;
+ }
+ if (m_File.AppendString(FX_BSTRC(">>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(FX_BSTRC("\r\nendstream")) < 0) {
+ return -1;
+ }
+ }
+ }
+ if (m_File.AppendString(FX_BSTRC("\r\nstartxref\r\n")) < 0) {
+ return -1;
+ }
+ FX_CHAR offset_buf[20];
+ FXSYS_memset32(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(FX_BSTRC("\r\n%%EOF\r\n")) < 0) {
+ return -1;
+ }
+ m_File.Flush();
+ return m_iStage = 100;
+}
+void CPDF_Creator::Clear()
+{
+ if (m_pXRefStream) {
+ delete m_pXRefStream;
+ m_pXRefStream = NULL;
+ }
+ m_File.Clear();
+ m_NewObjNumArray.RemoveAll();
+ if (m_pIDArray) {
+ m_pIDArray->Release();
+ m_pIDArray = NULL;
+ }
+}
+FX_BOOL CPDF_Creator::Create(FX_LPCSTR filename, FX_DWORD flags)
+{
+ if (!m_File.AttachFile(filename)) {
+ return FALSE;
+ }
+ FX_BOOL bRet = Create(flags);
+ if (!bRet || !(flags & FPDFCREATE_PROGRESSIVE)) {
+ Clear();
+ }
+ return bRet;
+}
+FX_BOOL CPDF_Creator::Create(FX_LPCWSTR filename, FX_DWORD flags)
+{
+ if (!m_File.AttachFile(filename)) {
+ return FALSE;
+ }
+ FX_BOOL bRet = Create(flags);
+ if (!bRet || !(flags & FPDFCREATE_PROGRESSIVE)) {
+ Clear();
+ }
+ return bRet;
+}
+FX_BOOL CPDF_Creator::Create(IFX_StreamWrite* pFile, FX_DWORD flags)
+{
+ if (!pFile) {
+ return FALSE;
+ }
+ if (!m_File.AttachFile(pFile, FALSE)) {
+ return FALSE;
+ }
+ return Create(flags);
+}
+FX_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_ObjectSize.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) {
+ FX_LPDWORD pBuffer = NULL;
+ m_pIDArray = CPDF_Array::Create();
+ CPDF_Object* pID1 = pOldIDArray->GetElement(0);
+ if (pID1) {
+ m_pIDArray->Add(pID1->Clone());
+ } else {
+ pBuffer = FX_Alloc(FX_DWORD, 4);
+ PDF_GenerateFileID((FX_DWORD)(FX_UINTPTR)this, m_dwLastObjNum, pBuffer);
+ CFX_ByteStringC bsBuffer((FX_LPCBYTE)pBuffer, 4 * sizeof(FX_DWORD));
+ m_pIDArray->Add(CPDF_String::Create(bsBuffer, TRUE), m_pDocument);
+ }
+ if (pBuffer) {
+ FX_Free(pBuffer);
+ }
+ }
+ 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;
+ }
+ FX_LPDWORD pBuffer = FX_Alloc(FX_DWORD, 4);
+ PDF_GenerateFileID((FX_DWORD)(FX_UINTPTR)this, m_dwLastObjNum, pBuffer);
+ CFX_ByteStringC bsBuffer((FX_LPCBYTE)pBuffer, 4 * sizeof(FX_DWORD));
+ m_pIDArray->Add(CPDF_String::Create(bsBuffer, TRUE), m_pDocument);
+ FX_Free(pBuffer);
+ return;
+ }
+ m_pIDArray->Add(m_pIDArray->GetElement(0)->Clone());
+ if (m_pEncryptDict && !pOldIDArray && m_pParser && bNewId) {
+ if (m_pEncryptDict->GetString(FX_BSTRC("Filter")) == FX_BSTRC("Standard")) {
+ CPDF_StandardSecurityHandler handler;
+ CFX_ByteString user_pass = m_pParser->GetPassword();
+ FX_DWORD flag = PDF_ENCRYPT_CONTENT;
+ handler.OnCreate(m_pEncryptDict, m_pIDArray, (FX_LPCBYTE)user_pass, user_pass.GetLength(), flag);
+ if (m_pCryptoHandler && m_bNewCrypto) {
+ delete m_pCryptoHandler;
+ }
+ m_pCryptoHandler = FX_NEW CPDF_StandardCryptoHandler;
+ m_pCryptoHandler->Init(m_pEncryptDict, &handler);
+ m_bNewCrypto = TRUE;
+ m_bSecurityChanged = TRUE;
+ }
+ }
+}
+FX_INT32 CPDF_Creator::Continue(IFX_Pause *pPause)
+{
+ if (m_iStage < 0) {
+ return m_iStage;
+ }
+ FX_INT32 iRet;
+ 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(FX_INT32 fileVersion )
+{
+ if (fileVersion < 10 || fileVersion > 17) {
+ return FALSE;
+ }
+ m_FileVersion = fileVersion;
+ return TRUE;
+}
+void CPDF_Creator::ResetStandardSecurity()
+{
+ if ((m_bStandardSecurity || m_bNewCrypto) && m_pCryptoHandler) {
+ 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
index e11d93380a..2672116e4f 100644
--- a/core/src/fpdfapi/fpdf_edit/fpdf_edit_doc.cpp
+++ b/core/src/fpdfapi/fpdf_edit/fpdf_edit_doc.cpp
@@ -1,1205 +1,1205 @@
-// 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 "../../../include/fpdfapi/fpdf_page.h"
-#include "../../../include/fpdfapi/fpdf_module.h"
-#include "../fpdf_page/pageint.h"
-#include <limits.h>
-CPDF_Document::CPDF_Document() : CPDF_IndirectObjects(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 == NULL && m_pInfoDict == NULL);
- m_pRootDict = FX_NEW CPDF_Dictionary;
- m_pRootDict->SetAtName("Type", "Catalog");
- int objnum = AddIndirectObject(m_pRootDict);
- CPDF_Dictionary* pPages = FX_NEW CPDF_Dictionary;
- pPages->SetAtName("Type", "Pages");
- pPages->SetAtNumber("Count", 0);
- pPages->SetAt("Kids", FX_NEW CPDF_Array);
- objnum = AddIndirectObject(pPages);
- m_pRootDict->SetAtReference("Pages", this, objnum);
- m_pInfoDict = FX_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 {
- FX_BYTE 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_WIN32_MOBILE_ || _FX_OS_ == _FX_WIN64_)
-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->GetInteger(pWidthArray->GetCount() - 1);
- pWidthArray->AddInteger(first + size - 1);
- pWidthArray->AddInteger(*widths);
- } else {
- CPDF_Array* pWidthArray1 = FX_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_memcpy32(&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, (FX_LPCSTR)face);
- return AddWindowsFont(&lfa, bVert, bTranslateName);
-}
-extern CFX_ByteString _FPDF_GetNameFromTT(FX_LPCBYTE name_table, FX_DWORD name);
-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 = _FPDF_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;
- }
- FX_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 = FX_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(FX_BSTRC("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 = FX_NEW CPDF_Dictionary;
- pEncoding->SetAtName(FX_BSTRC("BaseEncoding"), "WinAnsiEncoding");
- CPDF_Array* pArray = FX_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(FX_BSTRC(".notdef"));
- } else {
- pArray->AddName(name);
- }
- }
- pEncoding->SetAt(FX_BSTRC("Differences"), pArray);
- AddIndirectObject(pEncoding);
- pBaseDict->SetAtReference(FX_BSTRC("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 = FX_NEW CPDF_Array;
- for (int i = 0; i < 224; i ++) {
- pWidths->AddInteger(char_widths[i]);
- }
- pBaseDict->SetAt("Widths", pWidths);
- } else {
- flags |= PDFFONT_NONSYMBOLIC;
- pFontDict = FX_NEW CPDF_Dictionary;
- CFX_ByteString cmap;
- CFX_ByteString ordering;
- int supplement;
- CPDF_Array* pWidthArray = FX_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 = FX_NEW CPDF_Dictionary;
- pCIDSysInfo->SetAtString("Registry", "Adobe");
- pCIDSysInfo->SetAtString("Ordering", ordering);
- pCIDSysInfo->SetAtInteger("Supplement", supplement);
- pFontDict->SetAt("CIDSystemInfo", pCIDSysInfo);
- CPDF_Array* pArray = FX_NEW CPDF_Array;
- pBaseDict->SetAt("DescendantFonts", pArray);
- AddIndirectObject(pFontDict);
- pArray->AddReference(this, pFontDict);
- }
- AddIndirectObject(pBaseDict);
- CPDF_Dictionary* pFontDesc = FX_NEW CPDF_Dictionary;
- pFontDesc->SetAtName("Type", "FontDescriptor");
- pFontDesc->SetAtName("FontName", basefont);
- pFontDesc->SetAtInteger("Flags", flags);
- CPDF_Array* pBBox = FX_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_)
-FX_UINT32 FX_GetLangHashCode( FX_LPCSTR pStr)
-{
- FXSYS_assert( pStr != NULL);
- FX_INT32 iLength = FXSYS_strlen(pStr);
- FX_LPCSTR pStrEnd = pStr + iLength;
- FX_UINT32 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(FX_UINT32 uCode)
-{
- FX_INT32 iStart = 0;
- FX_INT32 iEnd = sizeof(gs_FXLang2CharsetTable) / sizeof(FX_LANG2CS) - 1;
- while (iStart <= iEnd) {
- FX_INT32 iMid = (iStart + iEnd) / 2;
- const FX_LANG2CS &charset = gs_FXLang2CharsetTable[iMid];
- if (uCode == charset.uLang) {
- return charset.uCharset;
- } else if (uCode < charset.uLang) {
- iEnd = iMid - 1;
- } else {
- iStart = iMid + 1;
- }
- };
- return 0;
-}
-static FX_WORD FX_GetCharsetFromLang(FX_LPCSTR pLang, FX_INT32 iLength)
-{
- FXSYS_assert(pLang);
- if (iLength < 0) {
- iLength = FXSYS_strlen(pLang);
- }
- FX_UINT32 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*)malloc(sizeof(UInt8) * (len + 1));
- FXSYS_memset32(pBuffer, 0, sizeof(UInt8) * (len + 1));
- CFStringGetBytes(src, range, kCFStringEncodingASCII, 0, false, pBuffer, len, &used);
- dest = (FX_LPSTR)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->GetInteger(pWidthArray->GetCount() - 1);
- pWidthArray->AddInteger(first + size - 1);
- pWidthArray->AddInteger(*widths);
- } else {
- CPDF_Array* pWidthArray1 = FX_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 == NULL) {
- return NULL;
- }
- CFX_ByteString basefont;
- FX_BOOL bCJK = FALSE;
- int flags = 0, italicangle = 0, ascend = 0, descend = 0, capheight = 0, bbox[4];
- FXSYS_memset32(bbox, 0, sizeof(int) * 4);
- CFArrayRef languages = (CFArrayRef)CTFontDescriptorCopyAttribute(descriptor, kCTFontLanguagesAttribute);
- if (languages == NULL) {
- 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 == NULL) {
- 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* pFontDict = NULL;
- CPDF_Dictionary* pBaseDict = FX_NEW 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(FX_BSTRC("Encoding"), "WinAnsiEncoding");
- } else {
- flags |= PDFFONT_NONSYMBOLIC;
- int 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 = FX_NEW CPDF_Dictionary;
- pEncoding->SetAtName(FX_BSTRC("BaseEncoding"), "WinAnsiEncoding");
- CPDF_Array* pArray = FX_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(FX_BSTRC(".notdef"));
- } else {
- pArray->AddName(name);
- }
- }
- pEncoding->SetAt(FX_BSTRC("Differences"), pArray);
- AddIndirectObject(pEncoding);
- pBaseDict->SetAtReference(FX_BSTRC("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 = FX_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 = FX_NEW CPDF_Dictionary;
- CFX_ByteString cmap;
- CFX_ByteString ordering;
- int supplement;
- FX_BOOL bFound = FALSE;
- CPDF_Array* pWidthArray = FX_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 = FX_NEW CPDF_Dictionary;
- pCIDSysInfo->SetAtString("Registry", "Adobe");
- pCIDSysInfo->SetAtString("Ordering", ordering);
- pCIDSysInfo->SetAtInteger("Supplement", supplement);
- pFontDict->SetAt("CIDSystemInfo", pCIDSysInfo);
- pArray = FX_NEW CPDF_Array;
- pBaseDict->SetAt("DescendantFonts", pArray);
- AddIndirectObject(pFontDict);
- pArray->AddReference(this, pFontDict);
- }
- AddIndirectObject(pBaseDict);
- CPDF_Dictionary* pFontDesc = FX_NEW CPDF_Dictionary;
- pFontDesc->SetAtName("Type", "FontDescriptor");
- pFontDesc->SetAtName("FontName", basefont);
- pFontDesc->SetAtInteger("Flags", flags);
- CPDF_Array* pBBox = FX_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'};
- const size_t count = sizeof(stem_chars) / sizeof(stem_chars[0]);
- CGGlyph glyphs[count];
- CGRect boundingRects[count];
- 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
-static void _InsertWidthArray1(CFX_Font* pFont, IFX_FontEncoding* 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->GetInteger(pWidthArray->GetCount() - 1);
- pWidthArray->AddInteger(first + size - 1);
- pWidthArray->AddInteger(*widths);
- } else {
- CPDF_Array* pWidthArray1 = FX_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 == NULL) {
- return NULL;
- }
- FX_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 = FX_NEW CPDF_Dictionary;
- pBaseDict->SetAtName("Type", "Font");
- IFX_FontEncoding* pEncoding = FXGE_CreateUnicodeEncoding(pFont);
- CPDF_Dictionary* pFontDict = pBaseDict;
- if (!bCJK) {
- CPDF_Array* pWidths = FX_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(FX_BSTRC("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;
- int 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 = FX_NEW CPDF_Dictionary;
- pEncodingDict->SetAtName(FX_BSTRC("BaseEncoding"), "WinAnsiEncoding");
- CPDF_Array* pArray = FX_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(FX_BSTRC(".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(FX_BSTRC("Differences"), pArray);
- AddIndirectObject(pEncodingDict);
- pBaseDict->SetAtReference(FX_BSTRC("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 = FX_NEW CPDF_Dictionary;
- CFX_ByteString cmap;
- CFX_ByteString ordering;
- int supplement;
- CPDF_Array* pWidthArray = FX_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, 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, 0x20, 0x20, pWidthArray);
- pWidthArray->AddInteger(814);
- _InsertWidthArray1(pFont, pEncoding, 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, 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, 0x20, 0x7d, pWidthArray);
- pWidthArray->AddInteger(326);
- _InsertWidthArray1(pFont, pEncoding, 0xa0, 0xa0, pWidthArray);
- pWidthArray->AddInteger(327);
- _InsertWidthArray1(pFont, pEncoding, 0xa1, 0xdf, pWidthArray);
- pWidthArray->AddInteger(631);
- _InsertWidthArray1(pFont, pEncoding, 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 = FX_NEW CPDF_Dictionary;
- pCIDSysInfo->SetAtString("Registry", "Adobe");
- pCIDSysInfo->SetAtString("Ordering", ordering);
- pCIDSysInfo->SetAtInteger("Supplement", supplement);
- pFontDict->SetAt("CIDSystemInfo", pCIDSysInfo);
- CPDF_Array* pArray = FX_NEW CPDF_Array;
- pBaseDict->SetAt("DescendantFonts", pArray);
- AddIndirectObject(pFontDict);
- pArray->AddReference(this, pFontDict);
- }
- AddIndirectObject(pBaseDict);
- CPDF_Dictionary* pFontDesc = FX_NEW CPDF_Dictionary;
- pFontDesc->SetAtName("Type", "FontDescriptor");
- pFontDesc->SetAtName("FontName", basefont);
- pFontDesc->SetAtInteger("Flags", flags);
- pFontDesc->SetAtInteger("ItalicAngle", pFont->m_pSubstFont ? pFont->m_pSubstFont->m_ItalicAngle : 0);
- pFontDesc->SetAtInteger("Ascent", pFont->GetAscent());
- pFontDesc->SetAtInteger("Descent", pFont->GetDescent());
- FX_RECT bbox;
- pFont->GetBBox(bbox);
- CPDF_Array* pBBox = FX_NEW CPDF_Array;
- pBBox->AddInteger(bbox.left);
- pBBox->AddInteger(bbox.bottom);
- pBBox->AddInteger(bbox.right);
- pBBox->AddInteger(bbox.top);
- pFontDesc->SetAt("FontBBox", pBBox);
- FX_INT32 nStemV = 0;
- if (pFont->m_pSubstFont) {
- nStemV = pFont->m_pSubstFont->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;
- }
- }
- }
- if (pEncoding) {
- delete pEncoding;
- }
- pFontDesc->SetAtInteger("StemV", nStemV);
- AddIndirectObject(pFontDesc);
- pFontDict->SetAtReference("FontDescriptor", this, pFontDesc);
- return LoadFont(pBaseDict);
-}
-static CPDF_Stream* GetFormStream(CPDF_Document* pDoc, CPDF_Object* pResObj)
-{
- if (pResObj->GetType() != PDFOBJ_REFERENCE) {
- return NULL;
- }
- CPDF_Reference* pRef = (CPDF_Reference*)pResObj;
- FX_BOOL bForm;
- if (pDoc->IsFormStream(pRef->GetRefObjNum(), bForm) && !bForm) {
- return NULL;
- }
- pResObj = pRef->GetDirect();
- if (pResObj->GetType() != PDFOBJ_STREAM) {
- return NULL;
- }
- CPDF_Stream* pXObject = (CPDF_Stream*)pResObj;
- if (pXObject->GetDict()->GetString(FX_BSTRC("Subtype")) != FX_BSTRC("Form")) {
- return NULL;
- }
- return pXObject;
-}
-static int InsertDeletePDFPage(CPDF_Document* pDoc, CPDF_Dictionary* pPages,
- int nPagesToGo, CPDF_Dictionary* pPage, FX_BOOL bInsert, CFX_PtrArray& stackList)
-{
- CPDF_Array* pKidList = pPages->GetArray("Kids");
- if (!pKidList) {
- return -1;
- }
- int nKids = pKidList->GetCount();
- for (int i = 0; i < nKids; i ++) {
- CPDF_Dictionary* pKid = pKidList->GetDict(i);
- if (pKid->GetString("Type") == FX_BSTRC("Page")) {
- if (nPagesToGo == 0) {
- if (bInsert) {
- pKidList->InsertAt(i, CPDF_Reference::Create(pDoc, pPage->GetObjNum()));
- pPage->SetAtReference("Parent", pDoc, pPages->GetObjNum());
- } else {
- pKidList->RemoveAt(i);
- }
- pPages->SetAtInteger("Count", pPages->GetInteger("Count") + (bInsert ? 1 : -1));
- return 1;
- }
- nPagesToGo --;
- } else {
- int nPages = pKid->GetInteger("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->GetInteger("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->GetDict(FX_BSTRC("Pages"));
- if (!pPages) {
- return -1;
- }
- int nPages = pDoc->GetPageCount();
- if (iPage < 0 || iPage > nPages) {
- return -1;
- }
- if (iPage == nPages) {
- CPDF_Array* pPagesList = pPages->GetArray(FX_BSTRC("Kids"));
- if (!pPagesList) {
- pPagesList = FX_NEW CPDF_Array;
- pPages->SetAt(FX_BSTRC("Kids"), pPagesList);
- }
- pPagesList->Add(pPageDict, pDoc);
- pPages->SetAtInteger(FX_BSTRC("Count"), nPages + 1);
- pPageDict->SetAtReference(FX_BSTRC("Parent"), pDoc, pPages->GetObjNum());
- } else {
- CFX_PtrArray 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 = FX_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;
-}
-int _PDF_GetStandardFontName(CFX_ByteString& name);
-CPDF_Font* CPDF_Document::AddStandardFont(FX_LPCSTR font, CPDF_FontEncoding* pEncoding)
-{
- CFX_ByteString name(font, -1);
- if (_PDF_GetStandardFontName(name) < 0) {
- return NULL;
- }
- return GetPageData()->GetStandardFont(name, pEncoding);
-}
-void CPDF_Document::DeletePage(int iPage)
-{
- CPDF_Dictionary* pRoot = GetRoot();
- if (pRoot == NULL) {
- return;
- }
- CPDF_Dictionary* pPages = pRoot->GetDict("Pages");
- if (pPages == NULL) {
- return;
- }
- int nPages = pPages->GetInteger("Count");
- if (iPage < 0 || iPage >= nPages) {
- return;
- }
- CFX_PtrArray 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, FX_BSTR name);
-void FPDFAPI_FlatPageAttr(CPDF_Dictionary* pPageDict, FX_BSTR name)
-{
- if (pPageDict->KeyExist(name)) {
- return;
- }
- CPDF_Object* pObj = FPDFAPI_GetPageAttr(pPageDict, name);
- if (pObj) {
- pPageDict->SetAt(name, pObj->Clone());
- }
-}
+// 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 "../../../include/fpdfapi/fpdf_page.h"
+#include "../../../include/fpdfapi/fpdf_module.h"
+#include "../fpdf_page/pageint.h"
+#include <limits.h>
+CPDF_Document::CPDF_Document() : CPDF_IndirectObjects(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 == NULL && m_pInfoDict == NULL);
+ m_pRootDict = FX_NEW CPDF_Dictionary;
+ m_pRootDict->SetAtName("Type", "Catalog");
+ int objnum = AddIndirectObject(m_pRootDict);
+ CPDF_Dictionary* pPages = FX_NEW CPDF_Dictionary;
+ pPages->SetAtName("Type", "Pages");
+ pPages->SetAtNumber("Count", 0);
+ pPages->SetAt("Kids", FX_NEW CPDF_Array);
+ objnum = AddIndirectObject(pPages);
+ m_pRootDict->SetAtReference("Pages", this, objnum);
+ m_pInfoDict = FX_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 {
+ FX_BYTE 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_WIN32_MOBILE_ || _FX_OS_ == _FX_WIN64_)
+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->GetInteger(pWidthArray->GetCount() - 1);
+ pWidthArray->AddInteger(first + size - 1);
+ pWidthArray->AddInteger(*widths);
+ } else {
+ CPDF_Array* pWidthArray1 = FX_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_memcpy32(&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, (FX_LPCSTR)face);
+ return AddWindowsFont(&lfa, bVert, bTranslateName);
+}
+extern CFX_ByteString _FPDF_GetNameFromTT(FX_LPCBYTE name_table, FX_DWORD name);
+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 = _FPDF_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;
+ }
+ FX_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 = FX_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(FX_BSTRC("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 = FX_NEW CPDF_Dictionary;
+ pEncoding->SetAtName(FX_BSTRC("BaseEncoding"), "WinAnsiEncoding");
+ CPDF_Array* pArray = FX_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(FX_BSTRC(".notdef"));
+ } else {
+ pArray->AddName(name);
+ }
+ }
+ pEncoding->SetAt(FX_BSTRC("Differences"), pArray);
+ AddIndirectObject(pEncoding);
+ pBaseDict->SetAtReference(FX_BSTRC("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 = FX_NEW CPDF_Array;
+ for (int i = 0; i < 224; i ++) {
+ pWidths->AddInteger(char_widths[i]);
+ }
+ pBaseDict->SetAt("Widths", pWidths);
+ } else {
+ flags |= PDFFONT_NONSYMBOLIC;
+ pFontDict = FX_NEW CPDF_Dictionary;
+ CFX_ByteString cmap;
+ CFX_ByteString ordering;
+ int supplement;
+ CPDF_Array* pWidthArray = FX_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 = FX_NEW CPDF_Dictionary;
+ pCIDSysInfo->SetAtString("Registry", "Adobe");
+ pCIDSysInfo->SetAtString("Ordering", ordering);
+ pCIDSysInfo->SetAtInteger("Supplement", supplement);
+ pFontDict->SetAt("CIDSystemInfo", pCIDSysInfo);
+ CPDF_Array* pArray = FX_NEW CPDF_Array;
+ pBaseDict->SetAt("DescendantFonts", pArray);
+ AddIndirectObject(pFontDict);
+ pArray->AddReference(this, pFontDict);
+ }
+ AddIndirectObject(pBaseDict);
+ CPDF_Dictionary* pFontDesc = FX_NEW CPDF_Dictionary;
+ pFontDesc->SetAtName("Type", "FontDescriptor");
+ pFontDesc->SetAtName("FontName", basefont);
+ pFontDesc->SetAtInteger("Flags", flags);
+ CPDF_Array* pBBox = FX_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_)
+FX_UINT32 FX_GetLangHashCode( FX_LPCSTR pStr)
+{
+ FXSYS_assert( pStr != NULL);
+ FX_INT32 iLength = FXSYS_strlen(pStr);
+ FX_LPCSTR pStrEnd = pStr + iLength;
+ FX_UINT32 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(FX_UINT32 uCode)
+{
+ FX_INT32 iStart = 0;
+ FX_INT32 iEnd = sizeof(gs_FXLang2CharsetTable) / sizeof(FX_LANG2CS) - 1;
+ while (iStart <= iEnd) {
+ FX_INT32 iMid = (iStart + iEnd) / 2;
+ const FX_LANG2CS &charset = gs_FXLang2CharsetTable[iMid];
+ if (uCode == charset.uLang) {
+ return charset.uCharset;
+ } else if (uCode < charset.uLang) {
+ iEnd = iMid - 1;
+ } else {
+ iStart = iMid + 1;
+ }
+ };
+ return 0;
+}
+static FX_WORD FX_GetCharsetFromLang(FX_LPCSTR pLang, FX_INT32 iLength)
+{
+ FXSYS_assert(pLang);
+ if (iLength < 0) {
+ iLength = FXSYS_strlen(pLang);
+ }
+ FX_UINT32 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*)malloc(sizeof(UInt8) * (len + 1));
+ FXSYS_memset32(pBuffer, 0, sizeof(UInt8) * (len + 1));
+ CFStringGetBytes(src, range, kCFStringEncodingASCII, 0, false, pBuffer, len, &used);
+ dest = (FX_LPSTR)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->GetInteger(pWidthArray->GetCount() - 1);
+ pWidthArray->AddInteger(first + size - 1);
+ pWidthArray->AddInteger(*widths);
+ } else {
+ CPDF_Array* pWidthArray1 = FX_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 == NULL) {
+ return NULL;
+ }
+ CFX_ByteString basefont;
+ FX_BOOL bCJK = FALSE;
+ int flags = 0, italicangle = 0, ascend = 0, descend = 0, capheight = 0, bbox[4];
+ FXSYS_memset32(bbox, 0, sizeof(int) * 4);
+ CFArrayRef languages = (CFArrayRef)CTFontDescriptorCopyAttribute(descriptor, kCTFontLanguagesAttribute);
+ if (languages == NULL) {
+ 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 == NULL) {
+ 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* pFontDict = NULL;
+ CPDF_Dictionary* pBaseDict = FX_NEW 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(FX_BSTRC("Encoding"), "WinAnsiEncoding");
+ } else {
+ flags |= PDFFONT_NONSYMBOLIC;
+ int 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 = FX_NEW CPDF_Dictionary;
+ pEncoding->SetAtName(FX_BSTRC("BaseEncoding"), "WinAnsiEncoding");
+ CPDF_Array* pArray = FX_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(FX_BSTRC(".notdef"));
+ } else {
+ pArray->AddName(name);
+ }
+ }
+ pEncoding->SetAt(FX_BSTRC("Differences"), pArray);
+ AddIndirectObject(pEncoding);
+ pBaseDict->SetAtReference(FX_BSTRC("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 = FX_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 = FX_NEW CPDF_Dictionary;
+ CFX_ByteString cmap;
+ CFX_ByteString ordering;
+ int supplement;
+ FX_BOOL bFound = FALSE;
+ CPDF_Array* pWidthArray = FX_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 = FX_NEW CPDF_Dictionary;
+ pCIDSysInfo->SetAtString("Registry", "Adobe");
+ pCIDSysInfo->SetAtString("Ordering", ordering);
+ pCIDSysInfo->SetAtInteger("Supplement", supplement);
+ pFontDict->SetAt("CIDSystemInfo", pCIDSysInfo);
+ pArray = FX_NEW CPDF_Array;
+ pBaseDict->SetAt("DescendantFonts", pArray);
+ AddIndirectObject(pFontDict);
+ pArray->AddReference(this, pFontDict);
+ }
+ AddIndirectObject(pBaseDict);
+ CPDF_Dictionary* pFontDesc = FX_NEW CPDF_Dictionary;
+ pFontDesc->SetAtName("Type", "FontDescriptor");
+ pFontDesc->SetAtName("FontName", basefont);
+ pFontDesc->SetAtInteger("Flags", flags);
+ CPDF_Array* pBBox = FX_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'};
+ const size_t count = sizeof(stem_chars) / sizeof(stem_chars[0]);
+ CGGlyph glyphs[count];
+ CGRect boundingRects[count];
+ 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
+static void _InsertWidthArray1(CFX_Font* pFont, IFX_FontEncoding* 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->GetInteger(pWidthArray->GetCount() - 1);
+ pWidthArray->AddInteger(first + size - 1);
+ pWidthArray->AddInteger(*widths);
+ } else {
+ CPDF_Array* pWidthArray1 = FX_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 == NULL) {
+ return NULL;
+ }
+ FX_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 = FX_NEW CPDF_Dictionary;
+ pBaseDict->SetAtName("Type", "Font");
+ IFX_FontEncoding* pEncoding = FXGE_CreateUnicodeEncoding(pFont);
+ CPDF_Dictionary* pFontDict = pBaseDict;
+ if (!bCJK) {
+ CPDF_Array* pWidths = FX_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(FX_BSTRC("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;
+ int 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 = FX_NEW CPDF_Dictionary;
+ pEncodingDict->SetAtName(FX_BSTRC("BaseEncoding"), "WinAnsiEncoding");
+ CPDF_Array* pArray = FX_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(FX_BSTRC(".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(FX_BSTRC("Differences"), pArray);
+ AddIndirectObject(pEncodingDict);
+ pBaseDict->SetAtReference(FX_BSTRC("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 = FX_NEW CPDF_Dictionary;
+ CFX_ByteString cmap;
+ CFX_ByteString ordering;
+ int supplement;
+ CPDF_Array* pWidthArray = FX_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, 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, 0x20, 0x20, pWidthArray);
+ pWidthArray->AddInteger(814);
+ _InsertWidthArray1(pFont, pEncoding, 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, 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, 0x20, 0x7d, pWidthArray);
+ pWidthArray->AddInteger(326);
+ _InsertWidthArray1(pFont, pEncoding, 0xa0, 0xa0, pWidthArray);
+ pWidthArray->AddInteger(327);
+ _InsertWidthArray1(pFont, pEncoding, 0xa1, 0xdf, pWidthArray);
+ pWidthArray->AddInteger(631);
+ _InsertWidthArray1(pFont, pEncoding, 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 = FX_NEW CPDF_Dictionary;
+ pCIDSysInfo->SetAtString("Registry", "Adobe");
+ pCIDSysInfo->SetAtString("Ordering", ordering);
+ pCIDSysInfo->SetAtInteger("Supplement", supplement);
+ pFontDict->SetAt("CIDSystemInfo", pCIDSysInfo);
+ CPDF_Array* pArray = FX_NEW CPDF_Array;
+ pBaseDict->SetAt("DescendantFonts", pArray);
+ AddIndirectObject(pFontDict);
+ pArray->AddReference(this, pFontDict);
+ }
+ AddIndirectObject(pBaseDict);
+ CPDF_Dictionary* pFontDesc = FX_NEW CPDF_Dictionary;
+ pFontDesc->SetAtName("Type", "FontDescriptor");
+ pFontDesc->SetAtName("FontName", basefont);
+ pFontDesc->SetAtInteger("Flags", flags);
+ pFontDesc->SetAtInteger("ItalicAngle", pFont->m_pSubstFont ? pFont->m_pSubstFont->m_ItalicAngle : 0);
+ pFontDesc->SetAtInteger("Ascent", pFont->GetAscent());
+ pFontDesc->SetAtInteger("Descent", pFont->GetDescent());
+ FX_RECT bbox;
+ pFont->GetBBox(bbox);
+ CPDF_Array* pBBox = FX_NEW CPDF_Array;
+ pBBox->AddInteger(bbox.left);
+ pBBox->AddInteger(bbox.bottom);
+ pBBox->AddInteger(bbox.right);
+ pBBox->AddInteger(bbox.top);
+ pFontDesc->SetAt("FontBBox", pBBox);
+ FX_INT32 nStemV = 0;
+ if (pFont->m_pSubstFont) {
+ nStemV = pFont->m_pSubstFont->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;
+ }
+ }
+ }
+ if (pEncoding) {
+ delete pEncoding;
+ }
+ pFontDesc->SetAtInteger("StemV", nStemV);
+ AddIndirectObject(pFontDesc);
+ pFontDict->SetAtReference("FontDescriptor", this, pFontDesc);
+ return LoadFont(pBaseDict);
+}
+static CPDF_Stream* GetFormStream(CPDF_Document* pDoc, CPDF_Object* pResObj)
+{
+ if (pResObj->GetType() != PDFOBJ_REFERENCE) {
+ return NULL;
+ }
+ CPDF_Reference* pRef = (CPDF_Reference*)pResObj;
+ FX_BOOL bForm;
+ if (pDoc->IsFormStream(pRef->GetRefObjNum(), bForm) && !bForm) {
+ return NULL;
+ }
+ pResObj = pRef->GetDirect();
+ if (pResObj->GetType() != PDFOBJ_STREAM) {
+ return NULL;
+ }
+ CPDF_Stream* pXObject = (CPDF_Stream*)pResObj;
+ if (pXObject->GetDict()->GetString(FX_BSTRC("Subtype")) != FX_BSTRC("Form")) {
+ return NULL;
+ }
+ return pXObject;
+}
+static int InsertDeletePDFPage(CPDF_Document* pDoc, CPDF_Dictionary* pPages,
+ int nPagesToGo, CPDF_Dictionary* pPage, FX_BOOL bInsert, CFX_PtrArray& stackList)
+{
+ CPDF_Array* pKidList = pPages->GetArray("Kids");
+ if (!pKidList) {
+ return -1;
+ }
+ int nKids = pKidList->GetCount();
+ for (int i = 0; i < nKids; i ++) {
+ CPDF_Dictionary* pKid = pKidList->GetDict(i);
+ if (pKid->GetString("Type") == FX_BSTRC("Page")) {
+ if (nPagesToGo == 0) {
+ if (bInsert) {
+ pKidList->InsertAt(i, CPDF_Reference::Create(pDoc, pPage->GetObjNum()));
+ pPage->SetAtReference("Parent", pDoc, pPages->GetObjNum());
+ } else {
+ pKidList->RemoveAt(i);
+ }
+ pPages->SetAtInteger("Count", pPages->GetInteger("Count") + (bInsert ? 1 : -1));
+ return 1;
+ }
+ nPagesToGo --;
+ } else {
+ int nPages = pKid->GetInteger("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->GetInteger("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->GetDict(FX_BSTRC("Pages"));
+ if (!pPages) {
+ return -1;
+ }
+ int nPages = pDoc->GetPageCount();
+ if (iPage < 0 || iPage > nPages) {
+ return -1;
+ }
+ if (iPage == nPages) {
+ CPDF_Array* pPagesList = pPages->GetArray(FX_BSTRC("Kids"));
+ if (!pPagesList) {
+ pPagesList = FX_NEW CPDF_Array;
+ pPages->SetAt(FX_BSTRC("Kids"), pPagesList);
+ }
+ pPagesList->Add(pPageDict, pDoc);
+ pPages->SetAtInteger(FX_BSTRC("Count"), nPages + 1);
+ pPageDict->SetAtReference(FX_BSTRC("Parent"), pDoc, pPages->GetObjNum());
+ } else {
+ CFX_PtrArray 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 = FX_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;
+}
+int _PDF_GetStandardFontName(CFX_ByteString& name);
+CPDF_Font* CPDF_Document::AddStandardFont(FX_LPCSTR font, CPDF_FontEncoding* pEncoding)
+{
+ CFX_ByteString name(font, -1);
+ if (_PDF_GetStandardFontName(name) < 0) {
+ return NULL;
+ }
+ return GetPageData()->GetStandardFont(name, pEncoding);
+}
+void CPDF_Document::DeletePage(int iPage)
+{
+ CPDF_Dictionary* pRoot = GetRoot();
+ if (pRoot == NULL) {
+ return;
+ }
+ CPDF_Dictionary* pPages = pRoot->GetDict("Pages");
+ if (pPages == NULL) {
+ return;
+ }
+ int nPages = pPages->GetInteger("Count");
+ if (iPage < 0 || iPage >= nPages) {
+ return;
+ }
+ CFX_PtrArray 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, FX_BSTR name);
+void FPDFAPI_FlatPageAttr(CPDF_Dictionary* pPageDict, FX_BSTR 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
index cb50601226..d73d7614da 100644
--- a/core/src/fpdfapi/fpdf_edit/fpdf_edit_image.cpp
+++ b/core/src/fpdfapi/fpdf_edit/fpdf_edit_image.cpp
@@ -1,374 +1,374 @@
-// 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 "../../../include/fpdfapi/fpdf_module.h"
-#include "../../../include/fpdfapi/fpdf_page.h"
-#include "../../../include/fxcodec/fx_codec.h"
-#include "../../../include/fpdfapi/fpdf_render.h"
-#include "../fpdf_page/pageint.h"
-#include "../fpdf_render/render_int.h"
-CPDF_Dictionary* CPDF_Image::InitJPEG(FX_LPBYTE pData, FX_DWORD size)
-{
- FX_INT32 width, height, color_trans, num_comps, bits;
- if (!CPDF_ModuleMgr::Get()->GetJpegModule()->
- LoadInfo(pData, size, width, height, num_comps, bits, color_trans)) {
- return NULL;
- }
- CPDF_Dictionary* pDict = FX_NEW CPDF_Dictionary;
- pDict->SetAtName("Type", "XObject");
- pDict->SetAtName("Subtype", "Image");
- pDict->SetAtInteger("Width", width);
- pDict->SetAtInteger("Height", height);
- FX_LPCSTR 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 = CPDF_Array::Create();
- for (int n = 0; n < 4; n ++) {
- pDecode->AddInteger(1);
- pDecode->AddInteger(0);
- }
- pDict->SetAt(FX_BSTRC("Decode"), pDecode);
- }
- pDict->SetAtName("ColorSpace", csname);
- pDict->SetAtInteger("BitsPerComponent", bits);
- pDict->SetAtName("Filter", "DCTDecode");
- if (!color_trans) {
- CPDF_Dictionary* pParms = FX_NEW CPDF_Dictionary;
- pDict->SetAt("DecodeParms", pParms);
- pParms->SetAtInteger("ColorTransform", 0);
- }
- m_bIsMask = FALSE;
- m_Width = width;
- m_Height = height;
- if (m_pStream == NULL) {
- m_pStream = FX_NEW CPDF_Stream(NULL, 0, NULL);
- }
- return pDict;
-}
-void CPDF_Image::SetJpegImage(FX_LPBYTE 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;
- }
- FX_LPBYTE pData = FX_Alloc(FX_BYTE, dwEstimateSize);
- if (!pData) {
- return;
- }
- pFile->ReadBlock(pData, 0, dwEstimateSize);
- CPDF_Dictionary *pDict = InitJPEG(pData, dwEstimateSize);
- FX_Free(pData);
- if (!pDict && size > dwEstimateSize) {
- pData = FX_Alloc(FX_BYTE, size);
- if (!pData) {
- return;
- }
- pFile->ReadBlock(pData, 0, size);
- pDict = InitJPEG(pData, size);
- FX_Free(pData);
- }
- if (!pDict) {
- return;
- }
- m_pStream->InitStream(pFile, pDict);
-}
-void _DCTEncodeBitmap(CPDF_Dictionary *pBitmapDict, const CFX_DIBitmap* pBitmap, int quality, FX_LPBYTE &buf, FX_STRSIZE &size)
-{
-}
-void _JBIG2EncodeBitmap(CPDF_Dictionary *pBitmapDict, const CFX_DIBitmap *pBitmap, CPDF_Document *pDoc, FX_LPBYTE &buf, FX_STRSIZE &size, FX_BOOL bLossLess)
-{
-}
-void CPDF_Image::SetImage(const CFX_DIBitmap* pBitmap, FX_INT32 iCompress, IFX_FileWrite *pFileWrite, IFX_FileRead *pFileRead, const CFX_DIBitmap* pMask, const CPDF_ImageSetParam* pParam)
-{
- FX_INT32 BitmapWidth = pBitmap->GetWidth();
- FX_INT32 BitmapHeight = pBitmap->GetHeight();
- if (BitmapWidth < 1 || BitmapHeight < 1) {
- return;
- }
- FX_LPBYTE src_buf = pBitmap->GetBuffer();
- FX_INT32 src_pitch = pBitmap->GetPitch();
- FX_INT32 bpp = pBitmap->GetBPP();
- FX_BOOL bUseMatte = pParam && pParam->pMatteColor && (pBitmap->GetFormat() == FXDIB_Argb);
- CPDF_Dictionary* pDict = FX_NEW CPDF_Dictionary;
- pDict->SetAtName(FX_BSTRC("Type"), FX_BSTRC("XObject"));
- pDict->SetAtName(FX_BSTRC("Subtype"), FX_BSTRC("Image"));
- pDict->SetAtInteger(FX_BSTRC("Width"), BitmapWidth);
- pDict->SetAtInteger(FX_BSTRC("Height"), BitmapHeight);
- FX_LPBYTE dest_buf = NULL;
- FX_STRSIZE dest_pitch = 0, dest_size = 0, opType = -1;
- if (bpp == 1) {
- FX_INT32 reset_a = 0, reset_r = 0, reset_g = 0, reset_b = 0;
- FX_INT32 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(FX_BSTRC("ImageMask"), FX_NEW CPDF_Boolean(TRUE));
- if (reset_a == 0) {
- CPDF_Array* pArray = FX_NEW CPDF_Array;
- pArray->AddInteger(1);
- pArray->AddInteger(0);
- pDict->SetAt(FX_BSTRC("Decode"), pArray);
- }
- } else {
- CPDF_Array* pCS = FX_NEW CPDF_Array;
- pCS->AddName(FX_BSTRC("Indexed"));
- pCS->AddName(FX_BSTRC("DeviceRGB"));
- pCS->AddInteger(1);
- CFX_ByteString ct;
- FX_LPSTR 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(CPDF_String::Create(ct, TRUE));
- pDict->SetAt(FX_BSTRC("ColorSpace"), pCS);
- }
- pDict->SetAtInteger(FX_BSTRC("BitsPerComponent"), 1);
- dest_pitch = (BitmapWidth + 7) / 8;
- if ((iCompress & 0x03) == PDF_IMAGE_NO_COMPRESS) {
- opType = 1;
- } else {
- opType = 0;
- }
- } else if (bpp == 8) {
- FX_INT32 iPalette = pBitmap->GetPaletteSize();
- if (iPalette > 0) {
- CPDF_Array* pCS = FX_NEW CPDF_Array;
- m_pDocument->AddIndirectObject(pCS);
- pCS->AddName(FX_BSTRC("Indexed"));
- pCS->AddName(FX_BSTRC("DeviceRGB"));
- pCS->AddInteger(iPalette - 1);
- FX_LPBYTE pColorTable = FX_Alloc(FX_BYTE, iPalette * 3);
- FX_LPBYTE ptr = pColorTable;
- for (FX_INT32 i = 0; i < iPalette; i ++) {
- FX_DWORD argb = pBitmap->GetPaletteArgb(i);
- ptr[0] = (FX_BYTE)(argb >> 16);
- ptr[1] = (FX_BYTE)(argb >> 8);
- ptr[2] = (FX_BYTE)argb;
- ptr += 3;
- }
- CPDF_Stream *pCTS = CPDF_Stream::Create(pColorTable, iPalette * 3, CPDF_Dictionary::Create());
- m_pDocument->AddIndirectObject(pCTS);
- pCS->AddReference(m_pDocument, pCTS);
- pDict->SetAtReference(FX_BSTRC("ColorSpace"), m_pDocument, pCS);
- } else {
- pDict->SetAtName(FX_BSTRC("ColorSpace"), FX_BSTRC("DeviceGray"));
- }
- pDict->SetAtInteger(FX_BSTRC("BitsPerComponent"), 8);
- if ((iCompress & 0x03) == PDF_IMAGE_NO_COMPRESS) {
- dest_pitch = BitmapWidth;
- opType = 1;
- } else {
- opType = 0;
- }
- } else {
- pDict->SetAtName(FX_BSTRC("ColorSpace"), FX_BSTRC("DeviceRGB"));
- pDict->SetAtInteger(FX_BSTRC("BitsPerComponent"), 8);
- if ((iCompress & 0x03) == PDF_IMAGE_NO_COMPRESS) {
- dest_pitch = BitmapWidth * 3;
- opType = 2;
- } else {
- opType = 0;
- }
- }
- const CFX_DIBitmap* pMaskBitmap = NULL;
- if (pBitmap->HasAlpha()) {
- pMaskBitmap = pBitmap->GetAlphaMask();
- }
- if (!pMaskBitmap && pMask) {
- FXDIB_Format maskFormat = pMask->GetFormat();
- if (maskFormat == FXDIB_1bppMask || maskFormat == FXDIB_8bppMask) {
- pMaskBitmap = pMask;
- }
- }
- if (pMaskBitmap) {
- FX_INT32 maskWidth = pMaskBitmap->GetWidth();
- FX_INT32 maskHeight = pMaskBitmap->GetHeight();
- FX_LPBYTE mask_buf = NULL;
- FX_STRSIZE mask_size;
- FX_BOOL bDeleteMask = TRUE;
- CPDF_Dictionary* pMaskDict = FX_NEW CPDF_Dictionary;
- pMaskDict->SetAtName(FX_BSTRC("Type"), FX_BSTRC("XObject"));
- pMaskDict->SetAtName(FX_BSTRC("Subtype"), FX_BSTRC("Image"));
- pMaskDict->SetAtInteger(FX_BSTRC("Width"), maskWidth);
- pMaskDict->SetAtInteger(FX_BSTRC("Height"), maskHeight);
- pMaskDict->SetAtName(FX_BSTRC("ColorSpace"), FX_BSTRC("DeviceGray"));
- pMaskDict->SetAtInteger(FX_BSTRC("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_size = maskHeight * maskWidth;
- mask_buf = FX_Alloc(FX_BYTE, mask_size);
- for (FX_INT32 a = 0; a < maskHeight; a ++) {
- FXSYS_memcpy32(mask_buf + a * maskWidth, pMaskBitmap->GetScanline(a), maskWidth);
- }
- }
- if (pMaskDict) {
- pMaskDict->SetAtInteger(FX_BSTRC("Length"), mask_size);
- CPDF_Stream* pMaskStream = NULL;
- if (bUseMatte) {
- int a, r, g, b;
- ArgbDecode(*(pParam->pMatteColor), a, r, g, b);
- CPDF_Array* pMatte = FX_NEW CPDF_Array;
- pMatte->AddInteger(r);
- pMatte->AddInteger(g);
- pMatte->AddInteger(b);
- pMaskDict->SetAt(FX_BSTRC("Matte"), pMatte);
- }
- pMaskStream = FX_NEW CPDF_Stream(mask_buf, mask_size, pMaskDict);
- m_pDocument->AddIndirectObject(pMaskStream);
- bDeleteMask = FALSE;
- pDict->SetAtReference(FX_BSTRC("SMask"), m_pDocument, pMaskStream);
- }
- if (pBitmap->HasAlpha()) {
- delete pMaskBitmap;
- }
- }
- FX_BOOL bStream = pFileWrite != NULL && pFileRead != NULL;
- 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() != NULL) {
- CFX_DIBitmap *pNewBitmap = FX_NEW CFX_DIBitmap();
- pNewBitmap->Copy(pBitmap);
- pNewBitmap->ConvertFormat(FXDIB_Rgb);
- SetImage(pNewBitmap, iCompress, pFileWrite, pFileRead);
- if (pDict) {
- pDict->Release();
- pDict = NULL;
- }
- if (dest_buf) {
- FX_Free(dest_buf);
- dest_buf = NULL;
- }
- dest_size = 0;
- delete pNewBitmap;
- return;
- } else {
- if (bUseMatte) {
- CFX_DIBitmap *pNewBitmap = FX_NEW CFX_DIBitmap();
- pNewBitmap->Create(BitmapWidth, BitmapHeight, FXDIB_Argb);
- FX_LPBYTE dst_buf = pNewBitmap->GetBuffer();
- FX_INT32 src_offset = 0;
- for (FX_INT32 row = 0; row < BitmapHeight; row ++) {
- src_offset = row * src_pitch;
- for (FX_INT32 column = 0; column < BitmapWidth; column ++) {
- FX_FLOAT alpha = src_buf[src_offset + 3] / 255.0f;
- dst_buf[src_offset] = (FX_BYTE)(src_buf[src_offset] * alpha);
- dst_buf[src_offset + 1] = (FX_BYTE)(src_buf[src_offset + 1] * alpha);
- dst_buf[src_offset + 2] = (FX_BYTE)(src_buf[src_offset + 2] * alpha);
- dst_buf[src_offset + 3] = (FX_BYTE)(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_size = dest_pitch * BitmapHeight;
- dest_buf = FX_Alloc(FX_BYTE, dest_size);
- }
- FX_LPBYTE pDest = dest_buf;
- for (FX_INT32 i = 0; i < BitmapHeight; i ++) {
- if (!bStream) {
- FXSYS_memcpy32(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_size = dest_pitch * BitmapHeight;
- dest_buf = FX_Alloc(FX_BYTE, dest_size);
- } else {
- dest_buf = FX_Alloc(FX_BYTE, dest_pitch);
- }
- FX_LPBYTE pDest = dest_buf;
- FX_INT32 src_offset = 0;
- FX_INT32 dest_offset = 0;
- for (FX_INT32 row = 0; row < BitmapHeight; row ++) {
- src_offset = row * src_pitch;
- for (FX_INT32 column = 0; column < BitmapWidth; column ++) {
- FX_FLOAT alpha = bUseMatte ? src_buf[src_offset + 3] / 255.0f : 1;
- pDest[dest_offset] = (FX_BYTE)(src_buf[src_offset + 2] * alpha);
- pDest[dest_offset + 1] = (FX_BYTE)(src_buf[src_offset + 1] * alpha);
- pDest[dest_offset + 2] = (FX_BYTE)(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 == NULL) {
- m_pStream = FX_NEW CPDF_Stream(NULL, 0, NULL);
- }
- if (!bStream) {
- m_pStream->InitStream(dest_buf, dest_size, pDict);
- } else {
- pFileWrite->Flush();
- m_pStream->InitStream(pFileRead, pDict);
- }
- m_bIsMask = pBitmap->IsAlphaMask();
- m_Width = BitmapWidth;
- m_Height = BitmapHeight;
- if (dest_buf) {
- FX_Free(dest_buf);
- }
-}
-void CPDF_Image::ResetCache(CPDF_Page* pPage, const CFX_DIBitmap* pBitmap)
-{
- pPage->GetRenderCache()->ResetBitmap(m_pStream, pBitmap);
-}
+// 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 "../../../include/fpdfapi/fpdf_module.h"
+#include "../../../include/fpdfapi/fpdf_page.h"
+#include "../../../include/fxcodec/fx_codec.h"
+#include "../../../include/fpdfapi/fpdf_render.h"
+#include "../fpdf_page/pageint.h"
+#include "../fpdf_render/render_int.h"
+CPDF_Dictionary* CPDF_Image::InitJPEG(FX_LPBYTE pData, FX_DWORD size)
+{
+ FX_INT32 width, height, color_trans, num_comps, bits;
+ if (!CPDF_ModuleMgr::Get()->GetJpegModule()->
+ LoadInfo(pData, size, width, height, num_comps, bits, color_trans)) {
+ return NULL;
+ }
+ CPDF_Dictionary* pDict = FX_NEW CPDF_Dictionary;
+ pDict->SetAtName("Type", "XObject");
+ pDict->SetAtName("Subtype", "Image");
+ pDict->SetAtInteger("Width", width);
+ pDict->SetAtInteger("Height", height);
+ FX_LPCSTR 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 = CPDF_Array::Create();
+ for (int n = 0; n < 4; n ++) {
+ pDecode->AddInteger(1);
+ pDecode->AddInteger(0);
+ }
+ pDict->SetAt(FX_BSTRC("Decode"), pDecode);
+ }
+ pDict->SetAtName("ColorSpace", csname);
+ pDict->SetAtInteger("BitsPerComponent", bits);
+ pDict->SetAtName("Filter", "DCTDecode");
+ if (!color_trans) {
+ CPDF_Dictionary* pParms = FX_NEW CPDF_Dictionary;
+ pDict->SetAt("DecodeParms", pParms);
+ pParms->SetAtInteger("ColorTransform", 0);
+ }
+ m_bIsMask = FALSE;
+ m_Width = width;
+ m_Height = height;
+ if (m_pStream == NULL) {
+ m_pStream = FX_NEW CPDF_Stream(NULL, 0, NULL);
+ }
+ return pDict;
+}
+void CPDF_Image::SetJpegImage(FX_LPBYTE 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;
+ }
+ FX_LPBYTE pData = FX_Alloc(FX_BYTE, dwEstimateSize);
+ if (!pData) {
+ return;
+ }
+ pFile->ReadBlock(pData, 0, dwEstimateSize);
+ CPDF_Dictionary *pDict = InitJPEG(pData, dwEstimateSize);
+ FX_Free(pData);
+ if (!pDict && size > dwEstimateSize) {
+ pData = FX_Alloc(FX_BYTE, size);
+ if (!pData) {
+ return;
+ }
+ pFile->ReadBlock(pData, 0, size);
+ pDict = InitJPEG(pData, size);
+ FX_Free(pData);
+ }
+ if (!pDict) {
+ return;
+ }
+ m_pStream->InitStream(pFile, pDict);
+}
+void _DCTEncodeBitmap(CPDF_Dictionary *pBitmapDict, const CFX_DIBitmap* pBitmap, int quality, FX_LPBYTE &buf, FX_STRSIZE &size)
+{
+}
+void _JBIG2EncodeBitmap(CPDF_Dictionary *pBitmapDict, const CFX_DIBitmap *pBitmap, CPDF_Document *pDoc, FX_LPBYTE &buf, FX_STRSIZE &size, FX_BOOL bLossLess)
+{
+}
+void CPDF_Image::SetImage(const CFX_DIBitmap* pBitmap, FX_INT32 iCompress, IFX_FileWrite *pFileWrite, IFX_FileRead *pFileRead, const CFX_DIBitmap* pMask, const CPDF_ImageSetParam* pParam)
+{
+ FX_INT32 BitmapWidth = pBitmap->GetWidth();
+ FX_INT32 BitmapHeight = pBitmap->GetHeight();
+ if (BitmapWidth < 1 || BitmapHeight < 1) {
+ return;
+ }
+ FX_LPBYTE src_buf = pBitmap->GetBuffer();
+ FX_INT32 src_pitch = pBitmap->GetPitch();
+ FX_INT32 bpp = pBitmap->GetBPP();
+ FX_BOOL bUseMatte = pParam && pParam->pMatteColor && (pBitmap->GetFormat() == FXDIB_Argb);
+ CPDF_Dictionary* pDict = FX_NEW CPDF_Dictionary;
+ pDict->SetAtName(FX_BSTRC("Type"), FX_BSTRC("XObject"));
+ pDict->SetAtName(FX_BSTRC("Subtype"), FX_BSTRC("Image"));
+ pDict->SetAtInteger(FX_BSTRC("Width"), BitmapWidth);
+ pDict->SetAtInteger(FX_BSTRC("Height"), BitmapHeight);
+ FX_LPBYTE dest_buf = NULL;
+ FX_STRSIZE dest_pitch = 0, dest_size = 0, opType = -1;
+ if (bpp == 1) {
+ FX_INT32 reset_a = 0, reset_r = 0, reset_g = 0, reset_b = 0;
+ FX_INT32 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(FX_BSTRC("ImageMask"), FX_NEW CPDF_Boolean(TRUE));
+ if (reset_a == 0) {
+ CPDF_Array* pArray = FX_NEW CPDF_Array;
+ pArray->AddInteger(1);
+ pArray->AddInteger(0);
+ pDict->SetAt(FX_BSTRC("Decode"), pArray);
+ }
+ } else {
+ CPDF_Array* pCS = FX_NEW CPDF_Array;
+ pCS->AddName(FX_BSTRC("Indexed"));
+ pCS->AddName(FX_BSTRC("DeviceRGB"));
+ pCS->AddInteger(1);
+ CFX_ByteString ct;
+ FX_LPSTR 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(CPDF_String::Create(ct, TRUE));
+ pDict->SetAt(FX_BSTRC("ColorSpace"), pCS);
+ }
+ pDict->SetAtInteger(FX_BSTRC("BitsPerComponent"), 1);
+ dest_pitch = (BitmapWidth + 7) / 8;
+ if ((iCompress & 0x03) == PDF_IMAGE_NO_COMPRESS) {
+ opType = 1;
+ } else {
+ opType = 0;
+ }
+ } else if (bpp == 8) {
+ FX_INT32 iPalette = pBitmap->GetPaletteSize();
+ if (iPalette > 0) {
+ CPDF_Array* pCS = FX_NEW CPDF_Array;
+ m_pDocument->AddIndirectObject(pCS);
+ pCS->AddName(FX_BSTRC("Indexed"));
+ pCS->AddName(FX_BSTRC("DeviceRGB"));
+ pCS->AddInteger(iPalette - 1);
+ FX_LPBYTE pColorTable = FX_Alloc(FX_BYTE, iPalette * 3);
+ FX_LPBYTE ptr = pColorTable;
+ for (FX_INT32 i = 0; i < iPalette; i ++) {
+ FX_DWORD argb = pBitmap->GetPaletteArgb(i);
+ ptr[0] = (FX_BYTE)(argb >> 16);
+ ptr[1] = (FX_BYTE)(argb >> 8);
+ ptr[2] = (FX_BYTE)argb;
+ ptr += 3;
+ }
+ CPDF_Stream *pCTS = CPDF_Stream::Create(pColorTable, iPalette * 3, CPDF_Dictionary::Create());
+ m_pDocument->AddIndirectObject(pCTS);
+ pCS->AddReference(m_pDocument, pCTS);
+ pDict->SetAtReference(FX_BSTRC("ColorSpace"), m_pDocument, pCS);
+ } else {
+ pDict->SetAtName(FX_BSTRC("ColorSpace"), FX_BSTRC("DeviceGray"));
+ }
+ pDict->SetAtInteger(FX_BSTRC("BitsPerComponent"), 8);
+ if ((iCompress & 0x03) == PDF_IMAGE_NO_COMPRESS) {
+ dest_pitch = BitmapWidth;
+ opType = 1;
+ } else {
+ opType = 0;
+ }
+ } else {
+ pDict->SetAtName(FX_BSTRC("ColorSpace"), FX_BSTRC("DeviceRGB"));
+ pDict->SetAtInteger(FX_BSTRC("BitsPerComponent"), 8);
+ if ((iCompress & 0x03) == PDF_IMAGE_NO_COMPRESS) {
+ dest_pitch = BitmapWidth * 3;
+ opType = 2;
+ } else {
+ opType = 0;
+ }
+ }
+ const CFX_DIBitmap* pMaskBitmap = NULL;
+ if (pBitmap->HasAlpha()) {
+ pMaskBitmap = pBitmap->GetAlphaMask();
+ }
+ if (!pMaskBitmap && pMask) {
+ FXDIB_Format maskFormat = pMask->GetFormat();
+ if (maskFormat == FXDIB_1bppMask || maskFormat == FXDIB_8bppMask) {
+ pMaskBitmap = pMask;
+ }
+ }
+ if (pMaskBitmap) {
+ FX_INT32 maskWidth = pMaskBitmap->GetWidth();
+ FX_INT32 maskHeight = pMaskBitmap->GetHeight();
+ FX_LPBYTE mask_buf = NULL;
+ FX_STRSIZE mask_size;
+ FX_BOOL bDeleteMask = TRUE;
+ CPDF_Dictionary* pMaskDict = FX_NEW CPDF_Dictionary;
+ pMaskDict->SetAtName(FX_BSTRC("Type"), FX_BSTRC("XObject"));
+ pMaskDict->SetAtName(FX_BSTRC("Subtype"), FX_BSTRC("Image"));
+ pMaskDict->SetAtInteger(FX_BSTRC("Width"), maskWidth);
+ pMaskDict->SetAtInteger(FX_BSTRC("Height"), maskHeight);
+ pMaskDict->SetAtName(FX_BSTRC("ColorSpace"), FX_BSTRC("DeviceGray"));
+ pMaskDict->SetAtInteger(FX_BSTRC("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_size = maskHeight * maskWidth;
+ mask_buf = FX_Alloc(FX_BYTE, mask_size);
+ for (FX_INT32 a = 0; a < maskHeight; a ++) {
+ FXSYS_memcpy32(mask_buf + a * maskWidth, pMaskBitmap->GetScanline(a), maskWidth);
+ }
+ }
+ if (pMaskDict) {
+ pMaskDict->SetAtInteger(FX_BSTRC("Length"), mask_size);
+ CPDF_Stream* pMaskStream = NULL;
+ if (bUseMatte) {
+ int a, r, g, b;
+ ArgbDecode(*(pParam->pMatteColor), a, r, g, b);
+ CPDF_Array* pMatte = FX_NEW CPDF_Array;
+ pMatte->AddInteger(r);
+ pMatte->AddInteger(g);
+ pMatte->AddInteger(b);
+ pMaskDict->SetAt(FX_BSTRC("Matte"), pMatte);
+ }
+ pMaskStream = FX_NEW CPDF_Stream(mask_buf, mask_size, pMaskDict);
+ m_pDocument->AddIndirectObject(pMaskStream);
+ bDeleteMask = FALSE;
+ pDict->SetAtReference(FX_BSTRC("SMask"), m_pDocument, pMaskStream);
+ }
+ if (pBitmap->HasAlpha()) {
+ delete pMaskBitmap;
+ }
+ }
+ FX_BOOL bStream = pFileWrite != NULL && pFileRead != NULL;
+ 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() != NULL) {
+ CFX_DIBitmap *pNewBitmap = FX_NEW CFX_DIBitmap();
+ pNewBitmap->Copy(pBitmap);
+ pNewBitmap->ConvertFormat(FXDIB_Rgb);
+ SetImage(pNewBitmap, iCompress, pFileWrite, pFileRead);
+ if (pDict) {
+ pDict->Release();
+ pDict = NULL;
+ }
+ if (dest_buf) {
+ FX_Free(dest_buf);
+ dest_buf = NULL;
+ }
+ dest_size = 0;
+ delete pNewBitmap;
+ return;
+ } else {
+ if (bUseMatte) {
+ CFX_DIBitmap *pNewBitmap = FX_NEW CFX_DIBitmap();
+ pNewBitmap->Create(BitmapWidth, BitmapHeight, FXDIB_Argb);
+ FX_LPBYTE dst_buf = pNewBitmap->GetBuffer();
+ FX_INT32 src_offset = 0;
+ for (FX_INT32 row = 0; row < BitmapHeight; row ++) {
+ src_offset = row * src_pitch;
+ for (FX_INT32 column = 0; column < BitmapWidth; column ++) {
+ FX_FLOAT alpha = src_buf[src_offset + 3] / 255.0f;
+ dst_buf[src_offset] = (FX_BYTE)(src_buf[src_offset] * alpha);
+ dst_buf[src_offset + 1] = (FX_BYTE)(src_buf[src_offset + 1] * alpha);
+ dst_buf[src_offset + 2] = (FX_BYTE)(src_buf[src_offset + 2] * alpha);
+ dst_buf[src_offset + 3] = (FX_BYTE)(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_size = dest_pitch * BitmapHeight;
+ dest_buf = FX_Alloc(FX_BYTE, dest_size);
+ }
+ FX_LPBYTE pDest = dest_buf;
+ for (FX_INT32 i = 0; i < BitmapHeight; i ++) {
+ if (!bStream) {
+ FXSYS_memcpy32(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_size = dest_pitch * BitmapHeight;
+ dest_buf = FX_Alloc(FX_BYTE, dest_size);
+ } else {
+ dest_buf = FX_Alloc(FX_BYTE, dest_pitch);
+ }
+ FX_LPBYTE pDest = dest_buf;
+ FX_INT32 src_offset = 0;
+ FX_INT32 dest_offset = 0;
+ for (FX_INT32 row = 0; row < BitmapHeight; row ++) {
+ src_offset = row * src_pitch;
+ for (FX_INT32 column = 0; column < BitmapWidth; column ++) {
+ FX_FLOAT alpha = bUseMatte ? src_buf[src_offset + 3] / 255.0f : 1;
+ pDest[dest_offset] = (FX_BYTE)(src_buf[src_offset + 2] * alpha);
+ pDest[dest_offset + 1] = (FX_BYTE)(src_buf[src_offset + 1] * alpha);
+ pDest[dest_offset + 2] = (FX_BYTE)(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 == NULL) {
+ m_pStream = FX_NEW CPDF_Stream(NULL, 0, NULL);
+ }
+ if (!bStream) {
+ m_pStream->InitStream(dest_buf, dest_size, pDict);
+ } else {
+ pFileWrite->Flush();
+ m_pStream->InitStream(pFileRead, pDict);
+ }
+ m_bIsMask = pBitmap->IsAlphaMask();
+ m_Width = BitmapWidth;
+ m_Height = BitmapHeight;
+ if (dest_buf) {
+ FX_Free(dest_buf);
+ }
+}
+void CPDF_Image::ResetCache(CPDF_Page* pPage, const CFX_DIBitmap* pBitmap)
+{
+ pPage->GetRenderCache()->ResetBitmap(m_pStream, pBitmap);
+}