From b0fcfad1412c2ddaf33c74fa4b854c3c1390a509 Mon Sep 17 00:00:00 2001 From: thestig Date: Mon, 6 Jun 2016 17:54:29 -0700 Subject: Remove unused PS generation code. CFX_PSRenderer, IFX_PSOutput, CPSOutput, CPSPrinterDriver. Also reland commit 2d63eaa. Review-Url: https://codereview.chromium.org/2019603002 --- BUILD.gn | 1 - core/fpdfapi/fpdf_edit/fpdf_edit_create.cpp | 63 +- core/fpdfapi/fpdf_page/cpdf_image.cpp | 14 +- core/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp | 41 +- .../fpdf_parser_decode_embeddertest.cpp | 12 +- .../fpdf_parser/include/fpdf_parser_decode.h | 23 +- core/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp | 18 +- core/fxcodec/codec/ccodec_basicmodule.h | 8 - core/fxcodec/codec/ccodec_flatemodule.h | 18 +- core/fxcodec/codec/ccodec_jpegmodule.h | 23 +- core/fxcodec/codec/fx_codec.cpp | 14 - core/fxcodec/codec/fx_codec_fax.cpp | 254 +------- core/fxcodec/codec/fx_codec_flate.cpp | 248 ++------ core/fxcodec/codec/fx_codec_jpeg.cpp | 325 +++------- core/fxge/apple/apple_int.h | 16 - core/fxge/ge/fx_ge_ps.cpp | 706 --------------------- core/fxge/ge/fx_ge_text.cpp | 2 +- core/fxge/include/fx_ge.h | 96 --- core/fxge/include/fx_ge_win32.h | 17 +- core/fxge/win32/fx_win32_device.cpp | 24 +- core/fxge/win32/fx_win32_print.cpp | 195 ------ core/fxge/win32/win32_int.h | 92 --- pdfium.gyp | 1 - 23 files changed, 271 insertions(+), 1940 deletions(-) delete mode 100644 core/fxge/ge/fx_ge_ps.cpp diff --git a/BUILD.gn b/BUILD.gn index 6757316fa4..48bf0930ab 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -690,7 +690,6 @@ static_library("fxge") { "core/fxge/ge/fx_ge_fontmap.cpp", "core/fxge/ge/fx_ge_linux.cpp", "core/fxge/ge/fx_ge_path.cpp", - "core/fxge/ge/fx_ge_ps.cpp", "core/fxge/ge/fx_ge_text.cpp", "core/fxge/ge/fx_text_int.h", "core/fxge/include/fx_dib.h", diff --git a/core/fpdfapi/fpdf_edit/fpdf_edit_create.cpp b/core/fpdfapi/fpdf_edit/fpdf_edit_create.cpp index 0a833a4835..09833d82cc 100644 --- a/core/fpdfapi/fpdf_edit/fpdf_edit_create.cpp +++ b/core/fpdfapi/fpdf_edit/fpdf_edit_create.cpp @@ -388,8 +388,8 @@ class CPDF_FlateEncoder { CPDF_FlateEncoder(CPDF_Stream* pStream, FX_BOOL bFlateEncode); CPDF_FlateEncoder(const uint8_t* pBuffer, uint32_t size, - FX_BOOL bFlateEncode, - FX_BOOL bXRefStream = FALSE); + bool bFlateEncode, + bool bXRefStream); ~CPDF_FlateEncoder(); void CloneDict(); @@ -439,7 +439,8 @@ CPDF_FlateEncoder::CPDF_FlateEncoder(CPDF_Stream* pStream, FX_BOOL bFlateEncode) m_bNewData = TRUE; m_bCloned = TRUE; - ::FlateEncode(m_Acc.GetData(), m_Acc.GetSize(), m_pData, m_dwSize); + // TODO(thestig): Move to Init() and check return value. + ::FlateEncode(m_Acc.GetData(), m_Acc.GetSize(), &m_pData, &m_dwSize); m_pDict = ToDictionary(pStream->GetDict()->Clone()); m_pDict->SetAtInteger("Length", m_dwSize); m_pDict->SetAtName("Filter", "FlateDecode"); @@ -448,8 +449,8 @@ CPDF_FlateEncoder::CPDF_FlateEncoder(CPDF_Stream* pStream, FX_BOOL bFlateEncode) CPDF_FlateEncoder::CPDF_FlateEncoder(const uint8_t* pBuffer, uint32_t size, - FX_BOOL bFlateEncode, - FX_BOOL bXRefStream) + bool bFlateEncode, + bool bXRefStream) : m_pData(nullptr), m_dwSize(0), m_pDict(nullptr), @@ -461,10 +462,11 @@ CPDF_FlateEncoder::CPDF_FlateEncoder(const uint8_t* pBuffer, return; } m_bNewData = TRUE; + // TODO(thestig): Move to Init() and check return value. if (bXRefStream) - ::FlateEncode(pBuffer, size, 12, 1, 8, 7, m_pData, m_dwSize); + ::PngEncode(pBuffer, size, &m_pData, &m_dwSize); else - ::FlateEncode(pBuffer, size, m_pData, m_dwSize); + ::FlateEncode(pBuffer, size, &m_pData, &m_dwSize); } CPDF_FlateEncoder::~CPDF_FlateEncoder() { @@ -577,7 +579,7 @@ FX_FILESIZE CPDF_ObjectStream::End(CPDF_Creator* pCreator) { tempBuffer << m_Buffer; CPDF_FlateEncoder encoder(tempBuffer.GetBuffer(), tempBuffer.GetLength(), - TRUE); + true, false); CPDF_Encryptor encryptor(pCreator->m_pCryptoHandler, m_dwObjNum, encoder.m_pData, encoder.m_dwSize); if ((len = pFile->AppendDWord(encryptor.m_dwSize)) < 0) { @@ -785,26 +787,24 @@ FX_BOOL CPDF_XRefStream::GenerateXRefStream(CPDF_Creator* pCreator, } offset += offset_len + 6; } - FX_BOOL bPredictor = TRUE; CPDF_FlateEncoder encoder(m_Buffer.GetBuffer(), m_Buffer.GetLength(), TRUE, - bPredictor); - if (pFile->AppendString("/Filter /FlateDecode") < 0) { + TRUE); + if (pFile->AppendString("/Filter /FlateDecode") < 0) return FALSE; - } + offset += 20; - if (bPredictor) { - if ((len = pFile->AppendString("/DecodeParms<>")) < - 0) { - return FALSE; - } - offset += len; - } - if (pFile->AppendString("/Length ") < 0) { + if ((len = pFile->AppendString("/DecodeParms<>")) < + 0) { return FALSE; } - if ((len = pFile->AppendDWord(encoder.m_dwSize)) < 0) { + + offset += len; + if (pFile->AppendString("/Length ") < 0) return FALSE; - } + + if ((len = pFile->AppendDWord(encoder.m_dwSize)) < 0) + return FALSE; + offset += len + 8; if (bEOF) { if ((len = PDF_CreatorWriteTrailer(pCreator->m_pDocument, pFile, @@ -899,7 +899,7 @@ CPDF_Creator::~CPDF_Creator() { ResetStandardSecurity(); if (m_bEncryptCloned && m_pEncryptDict) { m_pEncryptDict->Release(); - m_pEncryptDict = NULL; + m_pEncryptDict = nullptr; } Clear(); } @@ -1441,7 +1441,7 @@ int32_t CPDF_Creator::WriteDoc_Stage1(IFX_Pause* pPause) { m_dwFlags &= ~FPDFCREATE_INCREMENTAL; } CPDF_Dictionary* pDict = m_pDocument->GetRoot(); - m_pMetadata = pDict ? pDict->GetDirectObjectBy("Metadata") : NULL; + m_pMetadata = pDict ? pDict->GetDirectObjectBy("Metadata") : nullptr; if (m_dwFlags & FPDFCREATE_OBJECTSTREAM) { m_pXRefStream.reset(new CPDF_XRefStream); m_pXRefStream->Start(); @@ -1556,7 +1556,7 @@ int32_t CPDF_Creator::WriteDoc_Stage2(IFX_Pause* pPause) { m_iStage = 27; } if (m_iStage == 27) { - if (NULL != m_pEncryptDict && 0 == m_pEncryptDict->GetObjNum()) { + if (m_pEncryptDict && !m_pEncryptDict->GetObjNum()) { m_dwLastObjNum += 1; FX_FILESIZE saveOffset = m_Offset; if (WriteIndirectObj(m_dwLastObjNum, m_pEncryptDict) < 0) { @@ -1893,13 +1893,14 @@ int32_t CPDF_Creator::WriteDoc_Stage4(IFX_Pause* pPause) { m_File.Flush(); return m_iStage = 100; } + void CPDF_Creator::Clear() { m_pXRefStream.reset(); m_File.Clear(); m_NewObjNumArray.RemoveAll(); if (m_pIDArray) { m_pIDArray->Release(); - m_pIDArray = NULL; + m_pIDArray = nullptr; } } @@ -1916,17 +1917,17 @@ bool CPDF_Creator::Create(uint32_t flags) { m_ObjectOffset.Clear(); m_NewObjNumArray.RemoveAll(); InitID(); - if (flags & FPDFCREATE_PROGRESSIVE) { + if (flags & FPDFCREATE_PROGRESSIVE) return true; - } - return Continue(NULL) > -1; + return Continue(nullptr) > -1; } + void CPDF_Creator::InitID(FX_BOOL bDefault) { - CPDF_Array* pOldIDArray = m_pParser ? m_pParser->GetIDArray() : NULL; + CPDF_Array* pOldIDArray = m_pParser ? m_pParser->GetIDArray() : nullptr; FX_BOOL bNewId = !m_pIDArray; if (!m_pIDArray) { m_pIDArray = new CPDF_Array; - CPDF_Object* pID1 = pOldIDArray ? pOldIDArray->GetObjectAt(0) : NULL; + CPDF_Object* pID1 = pOldIDArray ? pOldIDArray->GetObjectAt(0) : nullptr; if (pID1) { m_pIDArray->Add(pID1->Clone()); } else { diff --git a/core/fpdfapi/fpdf_page/cpdf_image.cpp b/core/fpdfapi/fpdf_page/cpdf_image.cpp index 84f6853b58..d7e50d8291 100644 --- a/core/fpdfapi/fpdf_page/cpdf_image.cpp +++ b/core/fpdfapi/fpdf_page/cpdf_image.cpp @@ -79,17 +79,18 @@ CPDF_Dictionary* CPDF_Image::InitJPEG(uint8_t* pData, uint32_t size) { int32_t height; int32_t num_comps; int32_t bits; - FX_BOOL color_trans; + bool color_trans; if (!CPDF_ModuleMgr::Get()->GetJpegModule()->LoadInfo( - pData, size, width, height, num_comps, bits, color_trans)) { - return NULL; + pData, size, &width, &height, &num_comps, &bits, &color_trans)) { + return nullptr; } + CPDF_Dictionary* pDict = new CPDF_Dictionary; pDict->SetAtName("Type", "XObject"); pDict->SetAtName("Subtype", "Image"); pDict->SetAtInteger("Width", width); pDict->SetAtInteger("Height", height); - const FX_CHAR* csname = NULL; + const FX_CHAR* csname = nullptr; if (num_comps == 1) { csname = "DeviceGray"; } else if (num_comps == 3) { @@ -114,9 +115,8 @@ CPDF_Dictionary* CPDF_Image::InitJPEG(uint8_t* pData, uint32_t size) { m_bIsMask = FALSE; m_Width = width; m_Height = height; - if (!m_pStream) { - m_pStream = new CPDF_Stream(NULL, 0, NULL); - } + if (!m_pStream) + m_pStream = new CPDF_Stream(nullptr, 0, nullptr); return pDict; } diff --git a/core/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp b/core/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp index 7c489a35dc..40ba362ad6 100644 --- a/core/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp +++ b/core/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp @@ -532,45 +532,40 @@ CFX_ByteString PDF_EncodeString(const CFX_ByteString& src, FX_BOOL bHex) { result.AppendChar('('); for (int i = 0; i < srclen; i++) { uint8_t ch = src[i]; - if (ch == ')' || ch == '\\' || ch == '(') { - result.AppendChar('\\'); - } else if (ch == 0x0a) { + if (ch == 0x0a) { result << "\\n"; continue; - } else if (ch == 0x0d) { + } + if (ch == 0x0d) { result << "\\r"; continue; } + if (ch == ')' || ch == '\\' || ch == '(') + result.AppendChar('\\'); result.AppendChar(ch); } result.AppendChar(')'); return result.MakeString(); } -void FlateEncode(const uint8_t* src_buf, +bool FlateEncode(const uint8_t* src_buf, uint32_t src_size, - uint8_t*& dest_buf, - uint32_t& dest_size) { + uint8_t** dest_buf, + uint32_t* dest_size) { CCodec_ModuleMgr* pEncoders = CPDF_ModuleMgr::Get()->GetCodecModule(); - if (pEncoders) { - pEncoders->GetFlateModule()->Encode(src_buf, src_size, dest_buf, dest_size); - } + return pEncoders && + pEncoders->GetFlateModule()->Encode(src_buf, src_size, dest_buf, + dest_size); } -void FlateEncode(const uint8_t* src_buf, - uint32_t src_size, - int predictor, - int Colors, - int BitsPerComponent, - int Columns, - uint8_t*& dest_buf, - uint32_t& dest_size) { +bool PngEncode(const uint8_t* src_buf, + uint32_t src_size, + uint8_t** dest_buf, + uint32_t* dest_size) { CCodec_ModuleMgr* pEncoders = CPDF_ModuleMgr::Get()->GetCodecModule(); - if (pEncoders) { - pEncoders->GetFlateModule()->Encode(src_buf, src_size, predictor, Colors, - BitsPerComponent, Columns, dest_buf, - dest_size); - } + return pEncoders && + pEncoders->GetFlateModule()->PngEncode(src_buf, src_size, dest_buf, + dest_size); } uint32_t FlateDecode(const uint8_t* src_buf, diff --git a/core/fpdfapi/fpdf_parser/fpdf_parser_decode_embeddertest.cpp b/core/fpdfapi/fpdf_parser/fpdf_parser_decode_embeddertest.cpp index 308c9f11c9..e9ca958411 100644 --- a/core/fpdfapi/fpdf_parser/fpdf_parser_decode_embeddertest.cpp +++ b/core/fpdfapi/fpdf_parser/fpdf_parser_decode_embeddertest.cpp @@ -38,14 +38,14 @@ TEST_F(FPDFParserDecodeEmbeddertest, FlateEncode) { for (size_t i = 0; i < FX_ArraySize(flate_encode_cases); ++i) { const pdfium::StrFuncTestData& data = flate_encode_cases[i]; - unsigned char* result; - unsigned int result_size; - FlateEncode(data.input, data.input_size, result, result_size); - ASSERT_TRUE(result); + unsigned char* buf = nullptr; + unsigned int buf_size; + EXPECT_TRUE(FlateEncode(data.input, data.input_size, &buf, &buf_size)); + ASSERT_TRUE(buf); EXPECT_EQ(std::string((const char*)data.expected, data.expected_size), - std::string((const char*)result, result_size)) + std::string((const char*)buf, buf_size)) << " for case " << i; - FX_Free(result); + FX_Free(buf); } } diff --git a/core/fpdfapi/fpdf_parser/include/fpdf_parser_decode.h b/core/fpdfapi/fpdf_parser/include/fpdf_parser_decode.h index 1f1095b396..5ae4011c76 100644 --- a/core/fpdfapi/fpdf_parser/include/fpdf_parser_decode.h +++ b/core/fpdfapi/fpdf_parser/include/fpdf_parser_decode.h @@ -24,18 +24,19 @@ CFX_WideString PDF_DecodeText(const CFX_ByteString& bstr); CFX_ByteString PDF_EncodeText(const FX_WCHAR* pString, int len = -1); CFX_ByteString PDF_EncodeText(const CFX_WideString& str); -void FlateEncode(const uint8_t* src_buf, +bool FlateEncode(const uint8_t* src_buf, uint32_t src_size, - uint8_t*& dest_buf, - uint32_t& dest_size); -void FlateEncode(const uint8_t* src_buf, - uint32_t src_size, - int predictor, - int Colors, - int BitsPerComponent, - int Columns, - uint8_t*& dest_buf, - uint32_t& dest_size); + uint8_t** dest_buf, + uint32_t* dest_size); + +// This used to have more parameters like the predictor and bpc, but there was +// only one caller, so the interface has been simplified, the values are hard +// coded, and dead code has been removed. +bool PngEncode(const uint8_t* src_buf, + uint32_t src_size, + uint8_t** dest_buf, + uint32_t* dest_size); + uint32_t FlateDecode(const uint8_t* src_buf, uint32_t src_size, uint8_t*& dest_buf, diff --git a/core/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp b/core/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp index 4071a115df..2b829d0c56 100644 --- a/core/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp +++ b/core/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp @@ -561,19 +561,20 @@ int CPDF_DIBSource::CreateDecoder() { src_data, src_size, m_Width, m_Height, m_nComponents, pParams ? pParams->GetIntegerBy("ColorTransform", 1) : 1)); if (!m_pDecoder) { - FX_BOOL bTransform = FALSE; + bool bTransform = false; int comps; int bpc; CCodec_JpegModule* pJpegModule = CPDF_ModuleMgr::Get()->GetJpegModule(); - if (pJpegModule->LoadInfo(src_data, src_size, m_Width, m_Height, comps, - bpc, bTransform)) { + if (pJpegModule->LoadInfo(src_data, src_size, &m_Width, &m_Height, &comps, + &bpc, &bTransform)) { if (m_nComponents != static_cast(comps)) { FX_Free(m_pCompData); m_pCompData = nullptr; m_nComponents = static_cast(comps); if (m_pColorSpace && - m_pColorSpace->CountComponents() != m_nComponents) + m_pColorSpace->CountComponents() != m_nComponents) { return 0; + } if (m_Family == PDFCS_LAB && m_nComponents != 3) return 0; m_pCompData = GetDecodeAndMaskArray(m_bDefaultDecode, m_bColorKey); @@ -613,17 +614,14 @@ int CPDF_DIBSource::CreateDecoder() { FX_SAFE_UINT32 requested_pitch = CalculatePitch8(m_bpc, m_nComponents, m_Width); - if (!requested_pitch.IsValid()) { + if (!requested_pitch.IsValid()) return 0; - } FX_SAFE_UINT32 provided_pitch = CalculatePitch8( m_pDecoder->GetBPC(), m_pDecoder->CountComps(), m_pDecoder->GetWidth()); - if (!provided_pitch.IsValid()) { + if (!provided_pitch.IsValid()) return 0; - } - if (provided_pitch.ValueOrDie() < requested_pitch.ValueOrDie()) { + if (provided_pitch.ValueOrDie() < requested_pitch.ValueOrDie()) return 0; - } return 1; } diff --git a/core/fxcodec/codec/ccodec_basicmodule.h b/core/fxcodec/codec/ccodec_basicmodule.h index 4151b4f0f1..641cd00cf4 100644 --- a/core/fxcodec/codec/ccodec_basicmodule.h +++ b/core/fxcodec/codec/ccodec_basicmodule.h @@ -13,14 +13,6 @@ class CCodec_ScanlineDecoder; class CCodec_BasicModule { public: - FX_BOOL RunLengthEncode(const uint8_t* src_buf, - uint32_t src_size, - uint8_t*& dest_buf, - uint32_t& dest_size); - FX_BOOL A85Encode(const uint8_t* src_buf, - uint32_t src_size, - uint8_t*& dest_buf, - uint32_t& dest_size); CCodec_ScanlineDecoder* CreateRunLengthDecoder(const uint8_t* src_buf, uint32_t src_size, int width, diff --git a/core/fxcodec/codec/ccodec_flatemodule.h b/core/fxcodec/codec/ccodec_flatemodule.h index aee03719de..01797e7e68 100644 --- a/core/fxcodec/codec/ccodec_flatemodule.h +++ b/core/fxcodec/codec/ccodec_flatemodule.h @@ -34,18 +34,14 @@ class CCodec_FlateModule { uint32_t estimated_size, uint8_t*& dest_buf, uint32_t& dest_size); - FX_BOOL Encode(const uint8_t* src_buf, + bool Encode(const uint8_t* src_buf, + uint32_t src_size, + uint8_t** dest_buf, + uint32_t* dest_size); + bool PngEncode(const uint8_t* src_buf, uint32_t src_size, - int predictor, - int Colors, - int BitsPerComponent, - int Columns, - uint8_t*& dest_buf, - uint32_t& dest_size); - FX_BOOL Encode(const uint8_t* src_buf, - uint32_t src_size, - uint8_t*& dest_buf, - uint32_t& dest_size); + uint8_t** dest_buf, + uint32_t* dest_size); }; #endif // CORE_FXCODEC_CODEC_CCODEC_FLATEMODULE_H_ diff --git a/core/fxcodec/codec/ccodec_jpegmodule.h b/core/fxcodec/codec/ccodec_jpegmodule.h index 98554fea1c..52fc4d8e81 100644 --- a/core/fxcodec/codec/ccodec_jpegmodule.h +++ b/core/fxcodec/codec/ccodec_jpegmodule.h @@ -20,27 +20,20 @@ class CFX_DIBAttribute; class CCodec_JpegModule { public: CCodec_JpegModule() {} + CCodec_ScanlineDecoder* CreateDecoder(const uint8_t* src_buf, uint32_t src_size, int width, int height, int nComps, FX_BOOL ColorTransform); - FX_BOOL LoadInfo(const uint8_t* src_buf, - uint32_t src_size, - int& width, - int& height, - int& num_components, - int& bits_per_components, - FX_BOOL& color_transform, - uint8_t** icc_buf_ptr = nullptr, - uint32_t* icc_length = nullptr); - FX_BOOL Encode(const CFX_DIBSource* pSource, - uint8_t*& dest_buf, - FX_STRSIZE& dest_size, - int quality = 75, - const uint8_t* icc_buf = nullptr, - uint32_t icc_length = 0); + bool LoadInfo(const uint8_t* src_buf, + uint32_t src_size, + int* width, + int* height, + int* num_components, + int* bits_per_components, + bool* color_transform); FXJPEG_Context* Start(); void Finish(FXJPEG_Context* pContext); void Input(FXJPEG_Context* pContext, diff --git a/core/fxcodec/codec/fx_codec.cpp b/core/fxcodec/codec/fx_codec.cpp index 997d61f25b..6a45737b10 100644 --- a/core/fxcodec/codec/fx_codec.cpp +++ b/core/fxcodec/codec/fx_codec.cpp @@ -77,20 +77,6 @@ uint8_t* CCodec_ScanlineDecoder::ReadNextLine() { return v_GetNextLine(); } -FX_BOOL CCodec_BasicModule::RunLengthEncode(const uint8_t* src_buf, - uint32_t src_size, - uint8_t*& dest_buf, - uint32_t& dest_size) { - return FALSE; -} - -FX_BOOL CCodec_BasicModule::A85Encode(const uint8_t* src_buf, - uint32_t src_size, - uint8_t*& dest_buf, - uint32_t& dest_size) { - return FALSE; -} - #ifdef PDF_ENABLE_XFA CFX_DIBAttribute::CFX_DIBAttribute() : m_nXDPI(-1), diff --git a/core/fxcodec/codec/fx_codec_fax.cpp b/core/fxcodec/codec/fx_codec_fax.cpp index 59010391c5..90d3b24cab 100644 --- a/core/fxcodec/codec/fx_codec_fax.cpp +++ b/core/fxcodec/codec/fx_codec_fax.cpp @@ -461,151 +461,22 @@ FX_BOOL FaxGet1DLine(const uint8_t* src_buf, return TRUE; } -const uint8_t BlackRunTerminator[128] = { - 0x37, 10, 0x02, 3, 0x03, 2, 0x02, 2, 0x03, 3, 0x03, 4, 0x02, 4, - 0x03, 5, 0x05, 6, 0x04, 6, 0x04, 7, 0x05, 7, 0x07, 7, 0x04, 8, - 0x07, 8, 0x18, 9, 0x17, 10, 0x18, 10, 0x08, 10, 0x67, 11, 0x68, 11, - 0x6c, 11, 0x37, 11, 0x28, 11, 0x17, 11, 0x18, 11, 0xca, 12, 0xcb, 12, - 0xcc, 12, 0xcd, 12, 0x68, 12, 0x69, 12, 0x6a, 12, 0x6b, 12, 0xd2, 12, - 0xd3, 12, 0xd4, 12, 0xd5, 12, 0xd6, 12, 0xd7, 12, 0x6c, 12, 0x6d, 12, - 0xda, 12, 0xdb, 12, 0x54, 12, 0x55, 12, 0x56, 12, 0x57, 12, 0x64, 12, - 0x65, 12, 0x52, 12, 0x53, 12, 0x24, 12, 0x37, 12, 0x38, 12, 0x27, 12, - 0x28, 12, 0x58, 12, 0x59, 12, 0x2b, 12, 0x2c, 12, 0x5a, 12, 0x66, 12, - 0x67, 12, -}; - -const uint8_t BlackRunMarkup[80] = { - 0x0f, 10, 0xc8, 12, 0xc9, 12, 0x5b, 12, 0x33, 12, 0x34, 12, 0x35, 12, - 0x6c, 13, 0x6d, 13, 0x4a, 13, 0x4b, 13, 0x4c, 13, 0x4d, 13, 0x72, 13, - 0x73, 13, 0x74, 13, 0x75, 13, 0x76, 13, 0x77, 13, 0x52, 13, 0x53, 13, - 0x54, 13, 0x55, 13, 0x5a, 13, 0x5b, 13, 0x64, 13, 0x65, 13, 0x08, 11, - 0x0c, 11, 0x0d, 11, 0x12, 12, 0x13, 12, 0x14, 12, 0x15, 12, 0x16, 12, - 0x17, 12, 0x1c, 12, 0x1d, 12, 0x1e, 12, 0x1f, 12, -}; - -const uint8_t WhiteRunTerminator[128] = { - 0x35, 8, 0x07, 6, 0x07, 4, 0x08, 4, 0x0B, 4, 0x0C, 4, 0x0E, 4, 0x0F, 4, - 0x13, 5, 0x14, 5, 0x07, 5, 0x08, 5, 0x08, 6, 0x03, 6, 0x34, 6, 0x35, 6, - 0x2a, 6, 0x2B, 6, 0x27, 7, 0x0c, 7, 0x08, 7, 0x17, 7, 0x03, 7, 0x04, 7, - 0x28, 7, 0x2B, 7, 0x13, 7, 0x24, 7, 0x18, 7, 0x02, 8, 0x03, 8, 0x1a, 8, - 0x1b, 8, 0x12, 8, 0x13, 8, 0x14, 8, 0x15, 8, 0x16, 8, 0x17, 8, 0x28, 8, - 0x29, 8, 0x2a, 8, 0x2b, 8, 0x2c, 8, 0x2d, 8, 0x04, 8, 0x05, 8, 0x0a, 8, - 0x0b, 8, 0x52, 8, 0x53, 8, 0x54, 8, 0x55, 8, 0x24, 8, 0x25, 8, 0x58, 8, - 0x59, 8, 0x5a, 8, 0x5b, 8, 0x4a, 8, 0x4b, 8, 0x32, 8, 0x33, 8, 0x34, 8, -}; - -const uint8_t WhiteRunMarkup[80] = { - 0x1b, 5, 0x12, 5, 0x17, 6, 0x37, 7, 0x36, 8, 0x37, 8, 0x64, 8, - 0x65, 8, 0x68, 8, 0x67, 8, 0xcc, 9, 0xcd, 9, 0xd2, 9, 0xd3, 9, - 0xd4, 9, 0xd5, 9, 0xd6, 9, 0xd7, 9, 0xd8, 9, 0xd9, 9, 0xda, 9, - 0xdb, 9, 0x98, 9, 0x99, 9, 0x9a, 9, 0x18, 6, 0x9b, 9, 0x08, 11, - 0x0c, 11, 0x0d, 11, 0x12, 12, 0x13, 12, 0x14, 12, 0x15, 12, 0x16, 12, - 0x17, 12, 0x1c, 12, 0x1d, 12, 0x1e, 12, 0x1f, 12, -}; - -void AddBitStream(uint8_t* dest_buf, int& dest_bitpos, int data, int bitlen) { - for (int i = bitlen - 1; i >= 0; i--) { - if (data & (1 << i)) { - dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8); - } - dest_bitpos++; - } -} - -void FaxEncodeRun(uint8_t* dest_buf, int& dest_bitpos, int run, bool bWhite) { - while (run >= 2560) { - AddBitStream(dest_buf, dest_bitpos, 0x1f, 12); - run -= 2560; - } - if (run >= 64) { - int markup = run - run % 64; - const uint8_t* p = bWhite ? WhiteRunMarkup : BlackRunMarkup; - p += (markup / 64 - 1) * 2; - AddBitStream(dest_buf, dest_bitpos, *p, p[1]); - } - run %= 64; - const uint8_t* p = bWhite ? WhiteRunTerminator : BlackRunTerminator; - p += run * 2; - AddBitStream(dest_buf, dest_bitpos, *p, p[1]); -} - -void FaxEncode2DLine(uint8_t* dest_buf, - int& dest_bitpos, - const uint8_t* src_buf, - const uint8_t* ref_buf, - int cols) { - int a0 = -1; - bool a0color = true; - while (1) { - int a1 = FindBit(src_buf, cols, a0 + 1, !a0color); - int b1, b2; - FaxG4FindB1B2(ref_buf, cols, a0, a0color, b1, b2); - if (b2 < a1) { - dest_bitpos += 3; - dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8); - dest_bitpos++; - a0 = b2; - } else if (a1 - b1 <= 3 && b1 - a1 <= 3) { - int delta = a1 - b1; - switch (delta) { - case 0: - dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8); - break; - case 1: - case 2: - case 3: - dest_bitpos += delta == 1 ? 1 : delta + 2; - dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8); - dest_bitpos++; - dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8); - break; - case -1: - case -2: - case -3: - dest_bitpos += delta == -1 ? 1 : -delta + 2; - dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8); - dest_bitpos++; - break; - } - dest_bitpos++; - a0 = a1; - a0color = !a0color; - } else { - int a2 = FindBit(src_buf, cols, a1 + 1, a0color); - dest_bitpos++; - dest_bitpos++; - dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8); - dest_bitpos++; - if (a0 < 0) { - a0 = 0; - } - FaxEncodeRun(dest_buf, dest_bitpos, a1 - a0, a0color); - FaxEncodeRun(dest_buf, dest_bitpos, a2 - a1, !a0color); - a0 = a2; - } - if (a0 >= cols) { - return; - } - } -} - } // namespace class CCodec_FaxDecoder : public CCodec_ScanlineDecoder { public: - CCodec_FaxDecoder(); + CCodec_FaxDecoder(const uint8_t* src_buf, + uint32_t src_size, + int width, + int height, + int K, + FX_BOOL EndOfLine, + FX_BOOL EncodedByteAlign, + FX_BOOL BlackIs1, + int Columns, + int Rows); ~CCodec_FaxDecoder() override; - FX_BOOL Create(const uint8_t* src_buf, - uint32_t src_size, - int width, - int height, - int K, - FX_BOOL EndOfLine, - FX_BOOL EncodedByteAlign, - FX_BOOL BlackIs1, - int Columns, - int Rows); // CCodec_ScanlineDecoder FX_BOOL v_Rewind() override; @@ -620,36 +491,26 @@ class CCodec_FaxDecoder : public CCodec_ScanlineDecoder { uint8_t* m_pRefBuf; }; -CCodec_FaxDecoder::CCodec_FaxDecoder() { - m_pScanlineBuf = NULL; - m_pRefBuf = NULL; -} -CCodec_FaxDecoder::~CCodec_FaxDecoder() { - FX_Free(m_pScanlineBuf); - FX_Free(m_pRefBuf); -} -FX_BOOL CCodec_FaxDecoder::Create(const uint8_t* src_buf, - uint32_t src_size, - int width, - int height, - int K, - FX_BOOL EndOfLine, - FX_BOOL EncodedByteAlign, - FX_BOOL BlackIs1, - int Columns, - int Rows) { +CCodec_FaxDecoder::CCodec_FaxDecoder(const uint8_t* src_buf, + uint32_t src_size, + int width, + int height, + int K, + FX_BOOL EndOfLine, + FX_BOOL EncodedByteAlign, + FX_BOOL BlackIs1, + int Columns, + int Rows) { m_Encoding = K; m_bEndOfLine = EndOfLine; m_bByteAlign = EncodedByteAlign; m_bBlack = BlackIs1; m_OrigWidth = Columns; m_OrigHeight = Rows; - if (m_OrigWidth == 0) { + if (m_OrigWidth == 0) m_OrigWidth = width; - } - if (m_OrigHeight == 0) { + if (m_OrigHeight == 0) m_OrigHeight = height; - } // Should not overflow. Checked by FPDFAPI_CreateFaxDecoder. m_Pitch = (static_cast(m_OrigWidth) + 31) / 32 * 4; m_OutputWidth = m_OrigWidth; @@ -660,8 +521,13 @@ FX_BOOL CCodec_FaxDecoder::Create(const uint8_t* src_buf, m_SrcSize = src_size; m_nComps = 1; m_bpc = 1; - return TRUE; } + +CCodec_FaxDecoder::~CCodec_FaxDecoder() { + FX_Free(m_pScanlineBuf); + FX_Free(m_pRefBuf); +} + FX_BOOL CCodec_FaxDecoder::v_Rewind() { FXSYS_memset(m_pRefBuf, 0xff, m_Pitch); bitpos = 0; @@ -747,64 +613,6 @@ void FaxG4Decode(const uint8_t* src_buf, *pbitpos = bitpos; } -class CCodec_FaxEncoder { - public: - CCodec_FaxEncoder(const uint8_t* src_buf, int width, int height, int pitch); - ~CCodec_FaxEncoder(); - void Encode(uint8_t*& dest_buf, uint32_t& dest_size); - void Encode2DLine(const uint8_t* scan_line); - CFX_BinaryBuf m_DestBuf; - uint8_t* m_pRefLine; - uint8_t* m_pLineBuf; - int m_Cols, m_Rows, m_Pitch; - const uint8_t* m_pSrcBuf; -}; -CCodec_FaxEncoder::CCodec_FaxEncoder(const uint8_t* src_buf, - int width, - int height, - int pitch) { - m_pSrcBuf = src_buf; - m_Cols = width; - m_Rows = height; - m_Pitch = pitch; - m_pRefLine = FX_Alloc(uint8_t, m_Pitch); - FXSYS_memset(m_pRefLine, 0xff, m_Pitch); - m_pLineBuf = FX_Alloc2D(uint8_t, m_Pitch, 8); - m_DestBuf.EstimateSize(0, 10240); -} -CCodec_FaxEncoder::~CCodec_FaxEncoder() { - FX_Free(m_pRefLine); - FX_Free(m_pLineBuf); -} -void CCodec_FaxEncoder::Encode(uint8_t*& dest_buf, uint32_t& dest_size) { - int dest_bitpos = 0; - uint8_t last_byte = 0; - for (int i = 0; i < m_Rows; i++) { - const uint8_t* scan_line = m_pSrcBuf + i * m_Pitch; - FXSYS_memset(m_pLineBuf, 0, m_Pitch * 8); - m_pLineBuf[0] = last_byte; - FaxEncode2DLine(m_pLineBuf, dest_bitpos, scan_line, m_pRefLine, m_Cols); - m_DestBuf.AppendBlock(m_pLineBuf, dest_bitpos / 8); - last_byte = m_pLineBuf[dest_bitpos / 8]; - dest_bitpos %= 8; - FXSYS_memcpy(m_pRefLine, scan_line, m_Pitch); - } - if (dest_bitpos) { - m_DestBuf.AppendByte(last_byte); - } - dest_size = m_DestBuf.GetSize(); - dest_buf = m_DestBuf.DetachBuffer(); -} -FX_BOOL CCodec_FaxModule::Encode(const uint8_t* src_buf, - int width, - int height, - int pitch, - uint8_t*& dest_buf, - uint32_t& dest_size) { - CCodec_FaxEncoder encoder(src_buf, width, height, pitch); - encoder.Encode(dest_buf, dest_size); - return TRUE; -} CCodec_ScanlineDecoder* CCodec_FaxModule::CreateDecoder( const uint8_t* src_buf, uint32_t src_size, @@ -816,8 +624,6 @@ CCodec_ScanlineDecoder* CCodec_FaxModule::CreateDecoder( FX_BOOL BlackIs1, int Columns, int Rows) { - CCodec_FaxDecoder* pDecoder = new CCodec_FaxDecoder; - pDecoder->Create(src_buf, src_size, width, height, K, EndOfLine, - EncodedByteAlign, BlackIs1, Columns, Rows); - return pDecoder; + return new CCodec_FaxDecoder(src_buf, src_size, width, height, K, EndOfLine, + EncodedByteAlign, BlackIs1, Columns, Rows); } diff --git a/core/fxcodec/codec/fx_codec_flate.cpp b/core/fxcodec/codec/fx_codec_flate.cpp index c23de277fb..cd24b6624c 100644 --- a/core/fxcodec/codec/fx_codec_flate.cpp +++ b/core/fxcodec/codec/fx_codec_flate.cpp @@ -29,30 +29,34 @@ static int FPDFAPI_FlateGetTotalOut(void* context) { static int FPDFAPI_FlateGetTotalIn(void* context) { return ((z_stream*)context)->total_in; } -static void FPDFAPI_FlateCompress(unsigned char* dest_buf, + +static bool FPDFAPI_FlateCompress(unsigned char* dest_buf, unsigned long* dest_size, const unsigned char* src_buf, unsigned long src_size) { - compress(dest_buf, dest_size, src_buf, src_size); + return compress(dest_buf, dest_size, src_buf, src_size) == Z_OK; } + void* FPDFAPI_FlateInit(void* (*alloc_func)(void*, unsigned int, unsigned int), void (*free_func)(void*, void*)) { z_stream* p = (z_stream*)alloc_func(0, 1, sizeof(z_stream)); - if (!p) { - return NULL; - } + if (!p) + return nullptr; + FXSYS_memset(p, 0, sizeof(z_stream)); p->zalloc = alloc_func; p->zfree = free_func; inflateInit(p); return p; } + void FPDFAPI_FlateInput(void* context, const unsigned char* src_buf, unsigned int src_size) { ((z_stream*)context)->next_in = (unsigned char*)src_buf; ((z_stream*)context)->avail_in = src_size; } + int FPDFAPI_FlateOutput(void* context, unsigned char* dest_buf, unsigned int dest_size) { @@ -67,16 +71,20 @@ int FPDFAPI_FlateOutput(void* context, } return ret; } + int FPDFAPI_FlateGetAvailIn(void* context) { return ((z_stream*)context)->avail_in; } + int FPDFAPI_FlateGetAvailOut(void* context) { return ((z_stream*)context)->avail_out; } + void FPDFAPI_FlateEnd(void* context) { inflateEnd((z_stream*)context); ((z_stream*)context)->zfree(0, context); } + } // extern "C" namespace { @@ -104,6 +112,7 @@ class CLZWDecoder { uint32_t m_StackLen; int m_CodeLen; }; + void CLZWDecoder::AddCode(uint32_t prefix_code, uint8_t append_char) { if (m_nCodes + m_Early == 4094) { return; @@ -229,108 +238,42 @@ int CLZWDecoder::Decode(uint8_t* dest_buf, return 0; } -uint8_t PaethPredictor(int a, int b, int c) { +uint8_t PathPredictor(int a, int b, int c) { int p = a + b - c; int pa = FXSYS_abs(p - a); int pb = FXSYS_abs(p - b); int pc = FXSYS_abs(p - c); - if (pa <= pb && pa <= pc) { + if (pa <= pb && pa <= pc) return (uint8_t)a; - } - if (pb <= pc) { + if (pb <= pc) return (uint8_t)b; - } return (uint8_t)c; } -FX_BOOL PNG_PredictorEncode(uint8_t*& data_buf, - uint32_t& data_size, - int predictor, - int Colors, - int BitsPerComponent, - int Columns) { - const int BytesPerPixel = (Colors * BitsPerComponent + 7) / 8; - const int row_size = (Colors * BitsPerComponent * Columns + 7) / 8; - if (row_size <= 0) - return FALSE; - const int row_count = (data_size + row_size - 1) / row_size; - const int last_row_size = data_size % row_size; +void PNG_PredictorEncode(uint8_t** data_buf, uint32_t* data_size) { + const int row_size = 7; + const int row_count = (*data_size + row_size - 1) / row_size; + const int last_row_size = *data_size % row_size; uint8_t* dest_buf = FX_Alloc2D(uint8_t, row_size + 1, row_count); int byte_cnt = 0; - uint8_t* pSrcData = data_buf; + uint8_t* pSrcData = *data_buf; uint8_t* pDestData = dest_buf; for (int row = 0; row < row_count; row++) { - if (predictor == 10) { - pDestData[0] = 0; - int move_size = row_size; - if (move_size * (row + 1) > (int)data_size) { - move_size = data_size - (move_size * row); - } - FXSYS_memmove(pDestData + 1, pSrcData, move_size); - pDestData += (move_size + 1); - pSrcData += move_size; - byte_cnt += move_size; - continue; - } - for (int byte = 0; byte < row_size && byte_cnt < (int)data_size; byte++) { - switch (predictor) { - case 11: { - pDestData[0] = 1; - uint8_t left = 0; - if (byte >= BytesPerPixel) { - left = pSrcData[byte - BytesPerPixel]; - } - pDestData[byte + 1] = pSrcData[byte] - left; - } break; - case 12: { - pDestData[0] = 2; - uint8_t up = 0; - if (row) { - up = pSrcData[byte - row_size]; - } - pDestData[byte + 1] = pSrcData[byte] - up; - } break; - case 13: { - pDestData[0] = 3; - uint8_t left = 0; - if (byte >= BytesPerPixel) { - left = pSrcData[byte - BytesPerPixel]; - } - uint8_t up = 0; - if (row) { - up = pSrcData[byte - row_size]; - } - pDestData[byte + 1] = pSrcData[byte] - (left + up) / 2; - } break; - case 14: { - pDestData[0] = 4; - uint8_t left = 0; - if (byte >= BytesPerPixel) { - left = pSrcData[byte - BytesPerPixel]; - } - uint8_t up = 0; - if (row) { - up = pSrcData[byte - row_size]; - } - uint8_t upper_left = 0; - if (byte >= BytesPerPixel && row) { - upper_left = pSrcData[byte - row_size - BytesPerPixel]; - } - pDestData[byte + 1] = - pSrcData[byte] - PaethPredictor(left, up, upper_left); - } break; - default: { pDestData[byte + 1] = pSrcData[byte]; } break; - } - byte_cnt++; + for (int byte = 0; byte < row_size && byte_cnt < (int)*data_size; byte++) { + pDestData[0] = 2; + uint8_t up = 0; + if (row) + up = pSrcData[byte - row_size]; + pDestData[byte + 1] = pSrcData[byte] - up; + ++byte_cnt; } pDestData += (row_size + 1); pSrcData += row_size; } - FX_Free(data_buf); - data_buf = dest_buf; - data_size = (row_size + 1) * row_count - - (last_row_size > 0 ? (row_size - last_row_size) : 0); - return TRUE; + FX_Free(*data_buf); + *data_buf = dest_buf; + *data_size = (row_size + 1) * row_count - + (last_row_size > 0 ? (row_size - last_row_size) : 0); } void PNG_PredictLine(uint8_t* pDestData, @@ -390,7 +333,7 @@ void PNG_PredictLine(uint8_t* pDestData, if (byte >= BytesPerPixel && pLastLine) { upper_left = pLastLine[byte - BytesPerPixel]; } - pDestData[byte] = raw_byte + PaethPredictor(left, up, upper_left); + pDestData[byte] = raw_byte + PathPredictor(left, up, upper_left); break; } default: @@ -475,7 +418,7 @@ FX_BOOL PNG_Predictor(uint8_t*& data_buf, if (byte >= BytesPerPixel && row) { upper_left = pDestData[byte - row_size - BytesPerPixel]; } - pDestData[byte] = raw_byte + PaethPredictor(left, up, upper_left); + pDestData[byte] = raw_byte + PathPredictor(left, up, upper_left); break; } default: @@ -494,73 +437,6 @@ FX_BOOL PNG_Predictor(uint8_t*& data_buf, return TRUE; } -void TIFF_PredictorEncodeLine(uint8_t* dest_buf, - int row_size, - int BitsPerComponent, - int Colors, - int Columns) { - int BytesPerPixel = BitsPerComponent * Colors / 8; - if (BitsPerComponent < 8) { - uint8_t mask = 0x01; - if (BitsPerComponent == 2) { - mask = 0x03; - } else if (BitsPerComponent == 4) { - mask = 0x0F; - } - int row_bits = Colors * BitsPerComponent * Columns; - for (int i = row_bits - BitsPerComponent; i >= BitsPerComponent; - i -= BitsPerComponent) { - int col = i % 8; - int index = i / 8; - int col_pre = - (col == 0) ? (8 - BitsPerComponent) : (col - BitsPerComponent); - int index_pre = (col == 0) ? (index - 1) : index; - uint8_t cur = (dest_buf[index] >> (8 - col - BitsPerComponent)) & mask; - uint8_t left = - (dest_buf[index_pre] >> (8 - col_pre - BitsPerComponent)) & mask; - cur -= left; - cur &= mask; - cur <<= (8 - col - BitsPerComponent); - dest_buf[index] &= ~(mask << ((8 - col - BitsPerComponent))); - dest_buf[index] |= cur; - } - } else if (BitsPerComponent == 8) { - for (int i = row_size - 1; i >= BytesPerPixel; i--) { - dest_buf[i] -= dest_buf[i - BytesPerPixel]; - } - } else { - for (int i = row_size - BytesPerPixel; i >= BytesPerPixel; - i -= BytesPerPixel) { - uint16_t pixel = (dest_buf[i] << 8) | dest_buf[i + 1]; - pixel -= - (dest_buf[i - BytesPerPixel] << 8) | dest_buf[i - BytesPerPixel + 1]; - dest_buf[i] = pixel >> 8; - dest_buf[i + 1] = (uint8_t)pixel; - } - } -} - -FX_BOOL TIFF_PredictorEncode(uint8_t*& data_buf, - uint32_t& data_size, - int Colors, - int BitsPerComponent, - int Columns) { - int row_size = (Colors * BitsPerComponent * Columns + 7) / 8; - if (row_size == 0) - return FALSE; - const int row_count = (data_size + row_size - 1) / row_size; - const int last_row_size = data_size % row_size; - for (int row = 0; row < row_count; row++) { - uint8_t* scan_line = data_buf + row * row_size; - if ((row + 1) * row_size > (int)data_size) { - row_size = last_row_size; - } - TIFF_PredictorEncodeLine(scan_line, row_size, BitsPerComponent, Colors, - Columns); - } - return TRUE; -} - void TIFF_PredictLine(uint8_t* dest_buf, uint32_t row_size, int BitsPerComponent, @@ -965,41 +841,29 @@ uint32_t CCodec_FlateModule::FlateOrLZWDecode(FX_BOOL bLZW, } return ret ? offset : FX_INVALID_OFFSET; } -FX_BOOL CCodec_FlateModule::Encode(const uint8_t* src_buf, + +bool CCodec_FlateModule::Encode(const uint8_t* src_buf, + uint32_t src_size, + uint8_t** dest_buf, + uint32_t* dest_size) { + *dest_size = src_size + src_size / 1000 + 12; + *dest_buf = FX_Alloc(uint8_t, *dest_size); + unsigned long temp_size = *dest_size; + if (!FPDFAPI_FlateCompress(*dest_buf, &temp_size, src_buf, src_size)) + return false; + + *dest_size = (uint32_t)temp_size; + return true; +} + +bool CCodec_FlateModule::PngEncode(const uint8_t* src_buf, uint32_t src_size, - int predictor, - int Colors, - int BitsPerComponent, - int Columns, - uint8_t*& dest_buf, - uint32_t& dest_size) { - if (predictor != 2 && predictor < 10) { - return Encode(src_buf, src_size, dest_buf, dest_size); - } - uint8_t* pSrcBuf = NULL; - pSrcBuf = FX_Alloc(uint8_t, src_size); + uint8_t** dest_buf, + uint32_t* dest_size) { + uint8_t* pSrcBuf = FX_Alloc(uint8_t, src_size); FXSYS_memcpy(pSrcBuf, src_buf, src_size); - FX_BOOL ret = TRUE; - if (predictor == 2) { - ret = TIFF_PredictorEncode(pSrcBuf, src_size, Colors, BitsPerComponent, - Columns); - } else if (predictor >= 10) { - ret = PNG_PredictorEncode(pSrcBuf, src_size, predictor, Colors, - BitsPerComponent, Columns); - } - if (ret) - ret = Encode(pSrcBuf, src_size, dest_buf, dest_size); + PNG_PredictorEncode(&pSrcBuf, &src_size); + bool ret = Encode(pSrcBuf, src_size, dest_buf, dest_size); FX_Free(pSrcBuf); return ret; } -FX_BOOL CCodec_FlateModule::Encode(const uint8_t* src_buf, - uint32_t src_size, - uint8_t*& dest_buf, - uint32_t& dest_size) { - dest_size = src_size + src_size / 1000 + 12; - dest_buf = FX_Alloc(uint8_t, dest_size); - unsigned long temp_size = dest_size; - FPDFAPI_FlateCompress(dest_buf, &temp_size, src_buf, src_size); - dest_size = (uint32_t)temp_size; - return TRUE; -} diff --git a/core/fxcodec/codec/fx_codec_jpeg.cpp b/core/fxcodec/codec/fx_codec_jpeg.cpp index c41dde943f..cbfea59329 100644 --- a/core/fxcodec/codec/fx_codec_jpeg.cpp +++ b/core/fxcodec/codec/fx_codec_jpeg.cpp @@ -23,15 +23,15 @@ extern "C" { } extern "C" { -static void _JpegScanSOI(const uint8_t*& src_buf, uint32_t& src_size) { - if (src_size == 0) { +static void JpegScanSOI(const uint8_t** src_buf, uint32_t* src_size) { + if (*src_size == 0) return; - } + uint32_t offset = 0; - while (offset < src_size - 1) { - if (src_buf[offset] == 0xff && src_buf[offset + 1] == 0xd8) { - src_buf += offset; - src_size -= offset; + while (offset < *src_size - 1) { + if ((*src_buf)[offset] == 0xff && (*src_buf)[offset + 1] == 0xd8) { + *src_buf += offset; + *src_size -= offset; return; } offset++; @@ -74,187 +74,31 @@ static void _error_do_nothing1(j_common_ptr cinfo, int) {} extern "C" { static void _error_do_nothing2(j_common_ptr cinfo, char*) {} }; -#define JPEG_MARKER_EXIF (JPEG_APP0 + 1) + #define JPEG_MARKER_ICC (JPEG_APP0 + 2) -#define JPEG_MARKER_AUTHORTIME (JPEG_APP0 + 3) #define JPEG_MARKER_MAXSIZE 0xFFFF -#define JPEG_OVERHEAD_LEN 14 -static FX_BOOL _JpegEmbedIccProfile(j_compress_ptr cinfo, - const uint8_t* icc_buf_ptr, - uint32_t icc_length) { - if (!icc_buf_ptr || icc_length == 0) { - return FALSE; - } - uint32_t icc_segment_size = (JPEG_MARKER_MAXSIZE - 2 - JPEG_OVERHEAD_LEN); - uint32_t icc_segment_num = (icc_length / icc_segment_size) + 1; - if (icc_segment_num > 255) { - return FALSE; - } - uint32_t icc_data_length = - JPEG_OVERHEAD_LEN + (icc_segment_num > 1 ? icc_segment_size : icc_length); - uint8_t* icc_data = FX_Alloc(uint8_t, icc_data_length); - FXSYS_memcpy(icc_data, "\x49\x43\x43\x5f\x50\x52\x4f\x46\x49\x4c\x45\x00", - 12); - icc_data[13] = (uint8_t)icc_segment_num; - for (uint8_t i = 0; i < (icc_segment_num - 1); i++) { - icc_data[12] = i + 1; - FXSYS_memcpy(icc_data + JPEG_OVERHEAD_LEN, - icc_buf_ptr + i * icc_segment_size, icc_segment_size); - jpeg_write_marker(cinfo, JPEG_MARKER_ICC, icc_data, icc_data_length); - } - icc_data[12] = (uint8_t)icc_segment_num; - uint32_t icc_size = (icc_segment_num - 1) * icc_segment_size; - FXSYS_memcpy(icc_data + JPEG_OVERHEAD_LEN, icc_buf_ptr + icc_size, - icc_length - icc_size); - jpeg_write_marker(cinfo, JPEG_MARKER_ICC, icc_data, - JPEG_OVERHEAD_LEN + icc_length - icc_size); - FX_Free(icc_data); - return TRUE; -} -extern "C" { -static void _dest_do_nothing(j_compress_ptr cinfo) {} -}; -extern "C" { -static boolean _dest_empty(j_compress_ptr cinfo) { - return FALSE; -} -}; -#define JPEG_BLOCK_SIZE 1048576 -static void _JpegEncode(const CFX_DIBSource* pSource, - uint8_t*& dest_buf, - FX_STRSIZE& dest_size, - int quality, - const uint8_t* icc_buf, - uint32_t icc_length) { - struct jpeg_error_mgr jerr; - jerr.error_exit = _error_do_nothing; - jerr.emit_message = _error_do_nothing1; - jerr.output_message = _error_do_nothing; - jerr.format_message = _error_do_nothing2; - jerr.reset_error_mgr = _error_do_nothing; - struct jpeg_compress_struct cinfo; - memset(&cinfo, 0, sizeof(cinfo)); - cinfo.err = &jerr; - jpeg_create_compress(&cinfo); - int Bpp = pSource->GetBPP() / 8; - uint32_t nComponents = Bpp >= 3 ? (pSource->IsCmykImage() ? 4 : 3) : 1; - uint32_t pitch = pSource->GetPitch(); - uint32_t width = pdfium::base::checked_cast(pSource->GetWidth()); - uint32_t height = pdfium::base::checked_cast(pSource->GetHeight()); - FX_SAFE_UINT32 safe_buf_len = width; - safe_buf_len *= height; - safe_buf_len *= nComponents; - safe_buf_len += 1024; - if (icc_length) { - safe_buf_len += 255 * 18; - safe_buf_len += icc_length; - } - uint32_t dest_buf_length = 0; - if (!safe_buf_len.IsValid()) { - dest_buf = nullptr; - } else { - dest_buf_length = safe_buf_len.ValueOrDie(); - dest_buf = FX_TryAlloc(uint8_t, dest_buf_length); - const int MIN_TRY_BUF_LEN = 1024; - while (!dest_buf && dest_buf_length > MIN_TRY_BUF_LEN) { - dest_buf_length >>= 1; - dest_buf = FX_TryAlloc(uint8_t, dest_buf_length); - } - } - if (!dest_buf) { - FX_OutOfMemoryTerminate(); - } - struct jpeg_destination_mgr dest; - dest.init_destination = _dest_do_nothing; - dest.term_destination = _dest_do_nothing; - dest.empty_output_buffer = _dest_empty; - dest.next_output_byte = dest_buf; - dest.free_in_buffer = dest_buf_length; - cinfo.dest = &dest; - cinfo.image_width = width; - cinfo.image_height = height; - cinfo.input_components = nComponents; - if (nComponents == 1) { - cinfo.in_color_space = JCS_GRAYSCALE; - } else if (nComponents == 3) { - cinfo.in_color_space = JCS_RGB; - } else { - cinfo.in_color_space = JCS_CMYK; - } - uint8_t* line_buf = NULL; - if (nComponents > 1) { - line_buf = FX_Alloc2D(uint8_t, width, nComponents); - } - jpeg_set_defaults(&cinfo); - if (quality != 75) { - jpeg_set_quality(&cinfo, quality, TRUE); - } - jpeg_start_compress(&cinfo, TRUE); - _JpegEmbedIccProfile(&cinfo, icc_buf, icc_length); - JSAMPROW row_pointer[1]; - JDIMENSION row; - while (cinfo.next_scanline < cinfo.image_height) { - const uint8_t* src_scan = pSource->GetScanline(cinfo.next_scanline); - if (nComponents > 1) { - uint8_t* dest_scan = line_buf; - if (nComponents == 3) { - for (uint32_t i = 0; i < width; i++) { - dest_scan[0] = src_scan[2]; - dest_scan[1] = src_scan[1]; - dest_scan[2] = src_scan[0]; - dest_scan += 3; - src_scan += Bpp; - } - } else { - for (uint32_t i = 0; i < pitch; i++) { - *dest_scan++ = ~*src_scan++; - } - } - row_pointer[0] = line_buf; - } else { - row_pointer[0] = (uint8_t*)src_scan; - } - row = cinfo.next_scanline; - jpeg_write_scanlines(&cinfo, row_pointer, 1); - if (cinfo.next_scanline == row) { - dest_buf = - FX_Realloc(uint8_t, dest_buf, dest_buf_length + JPEG_BLOCK_SIZE); - dest.next_output_byte = dest_buf + dest_buf_length - dest.free_in_buffer; - dest_buf_length += JPEG_BLOCK_SIZE; - dest.free_in_buffer += JPEG_BLOCK_SIZE; - } - } - jpeg_finish_compress(&cinfo); - jpeg_destroy_compress(&cinfo); - FX_Free(line_buf); - dest_size = dest_buf_length - (FX_STRSIZE)dest.free_in_buffer; -} #ifdef PDF_ENABLE_XFA -static void _JpegLoadAttribute(struct jpeg_decompress_struct* pInfo, - CFX_DIBAttribute* pAttribute) { - if (pInfo == NULL || pAttribute == NULL) { +static void JpegLoadAttribute(struct jpeg_decompress_struct* pInfo, + CFX_DIBAttribute* pAttribute) { + if (!pInfo || !pAttribute) return; - } - if (pAttribute) { - pAttribute->m_nXDPI = pInfo->X_density; - pAttribute->m_nYDPI = pInfo->Y_density; - pAttribute->m_wDPIUnit = pInfo->density_unit; - } + + pAttribute->m_nXDPI = pInfo->X_density; + pAttribute->m_nYDPI = pInfo->Y_density; + pAttribute->m_wDPIUnit = pInfo->density_unit; } #endif // PDF_ENABLE_XFA -static FX_BOOL _JpegLoadInfo(const uint8_t* src_buf, - uint32_t src_size, - int& width, - int& height, - int& num_components, - int& bits_per_components, - FX_BOOL& color_transform, - uint8_t** icc_buf_ptr, - uint32_t* icc_length) { - _JpegScanSOI(src_buf, src_size); +static bool JpegLoadInfo(const uint8_t* src_buf, + uint32_t src_size, + int* width, + int* height, + int* num_components, + int* bits_per_components, + bool* color_transform) { + JpegScanSOI(&src_buf, &src_size); struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr jerr; jerr.error_exit = _error_fatal; @@ -266,9 +110,9 @@ static FX_BOOL _JpegLoadInfo(const uint8_t* src_buf, cinfo.err = &jerr; jmp_buf mark; cinfo.client_data = &mark; - if (setjmp(mark) == -1) { - return FALSE; - } + if (setjmp(mark) == -1) + return false; + jpeg_create_decompress(&cinfo); struct jpeg_source_mgr src; src.init_source = _src_do_nothing; @@ -281,30 +125,21 @@ static FX_BOOL _JpegLoadInfo(const uint8_t* src_buf, cinfo.src = &src; if (setjmp(mark) == -1) { jpeg_destroy_decompress(&cinfo); - return FALSE; - } - if (icc_buf_ptr && icc_length) { - jpeg_save_markers(&cinfo, JPEG_MARKER_ICC, JPEG_MARKER_MAXSIZE); + return false; } int ret = jpeg_read_header(&cinfo, TRUE); if (ret != JPEG_HEADER_OK) { jpeg_destroy_decompress(&cinfo); - return FALSE; + return false; } - width = cinfo.image_width; - height = cinfo.image_height; - num_components = cinfo.num_components; - color_transform = + *width = cinfo.image_width; + *height = cinfo.image_height; + *num_components = cinfo.num_components; + *color_transform = cinfo.jpeg_color_space == JCS_YCbCr || cinfo.jpeg_color_space == JCS_YCCK; - bits_per_components = cinfo.data_precision; - if (icc_buf_ptr) { - *icc_buf_ptr = NULL; - } - if (icc_length) { - *icc_length = 0; - } + *bits_per_components = cinfo.data_precision; jpeg_destroy_decompress(&cinfo); - return TRUE; + return true; } class CCodec_JpegDecoder : public CCodec_ScanlineDecoder { @@ -351,18 +186,19 @@ CCodec_JpegDecoder::CCodec_JpegDecoder() { FXSYS_memset(&src, 0, sizeof(src)); m_nDefaultScaleDenom = 1; } + CCodec_JpegDecoder::~CCodec_JpegDecoder() { FX_Free(m_pScanlineBuf); - if (m_bInited) { + if (m_bInited) jpeg_destroy_decompress(&cinfo); - } } + FX_BOOL CCodec_JpegDecoder::InitDecode() { cinfo.err = &jerr; cinfo.client_data = &m_JmpBuf; - if (setjmp(m_JmpBuf) == -1) { + if (setjmp(m_JmpBuf) == -1) return FALSE; - } + jpeg_create_decompress(&cinfo); m_bInited = TRUE; cinfo.src = &src; @@ -376,15 +212,15 @@ FX_BOOL CCodec_JpegDecoder::InitDecode() { cinfo.image_width = m_OrigWidth; cinfo.image_height = m_OrigHeight; int ret = jpeg_read_header(&cinfo, TRUE); - if (ret != JPEG_HEADER_OK) { + if (ret != JPEG_HEADER_OK) return FALSE; - } - if (cinfo.saw_Adobe_marker) { + + if (cinfo.saw_Adobe_marker) m_bJpegTransform = TRUE; - } - if (cinfo.num_components == 3 && !m_bJpegTransform) { + + if (cinfo.num_components == 3 && !m_bJpegTransform) cinfo.out_color_space = cinfo.jpeg_color_space; - } + m_OrigWidth = cinfo.image_width; m_OrigHeight = cinfo.image_height; m_OutputWidth = m_OrigWidth; @@ -392,13 +228,14 @@ FX_BOOL CCodec_JpegDecoder::InitDecode() { m_nDefaultScaleDenom = cinfo.scale_denom; return TRUE; } + FX_BOOL CCodec_JpegDecoder::Create(const uint8_t* src_buf, uint32_t src_size, int width, int height, int nComps, FX_BOOL ColorTransform) { - _JpegScanSOI(src_buf, src_size); + JpegScanSOI(&src_buf, &src_size); m_SrcBuf = src_buf; m_SrcSize = src_size; jerr.error_exit = _error_fatal; @@ -419,15 +256,15 @@ FX_BOOL CCodec_JpegDecoder::Create(const uint8_t* src_buf, } m_OutputWidth = m_OrigWidth = width; m_OutputHeight = m_OrigHeight = height; - if (!InitDecode()) { + if (!InitDecode()) return FALSE; - } - if (cinfo.num_components < nComps) { + + if (cinfo.num_components < nComps) return FALSE; - } - if ((int)cinfo.image_width < width) { + + if ((int)cinfo.image_width < width) return FALSE; - } + m_Pitch = (static_cast(cinfo.image_width) * cinfo.num_components + 3) / 4 * 4; @@ -462,19 +299,19 @@ FX_BOOL CCodec_JpegDecoder::v_Rewind() { m_bStarted = TRUE; return TRUE; } + uint8_t* CCodec_JpegDecoder::v_GetNextLine() { if (setjmp(m_JmpBuf) == -1) return nullptr; int nlines = jpeg_read_scanlines(&cinfo, &m_pScanlineBuf, 1); - if (nlines < 1) { - return nullptr; - } - return m_pScanlineBuf; + return nlines > 0 ? m_pScanlineBuf : nullptr; } + uint32_t CCodec_JpegDecoder::GetSrcOffset() { return (uint32_t)(m_SrcSize - src.bytes_in_buffer); } + CCodec_ScanlineDecoder* CCodec_JpegModule::CreateDecoder( const uint8_t* src_buf, uint32_t src_size, @@ -482,42 +319,28 @@ CCodec_ScanlineDecoder* CCodec_JpegModule::CreateDecoder( int height, int nComps, FX_BOOL ColorTransform) { - if (!src_buf || src_size == 0) { - return NULL; - } - CCodec_JpegDecoder* pDecoder = new CCodec_JpegDecoder; + if (!src_buf || src_size == 0) + return nullptr; + + std::unique_ptr pDecoder(new CCodec_JpegDecoder); if (!pDecoder->Create(src_buf, src_size, width, height, nComps, ColorTransform)) { - delete pDecoder; - return NULL; + return nullptr; } - return pDecoder; -} -FX_BOOL CCodec_JpegModule::LoadInfo(const uint8_t* src_buf, - uint32_t src_size, - int& width, - int& height, - int& num_components, - int& bits_per_components, - FX_BOOL& color_transform, - uint8_t** icc_buf_ptr, - uint32_t* icc_length) { - return _JpegLoadInfo(src_buf, src_size, width, height, num_components, - bits_per_components, color_transform, icc_buf_ptr, - icc_length); + return pDecoder.release(); } -FX_BOOL CCodec_JpegModule::Encode(const CFX_DIBSource* pSource, - uint8_t*& dest_buf, - FX_STRSIZE& dest_size, - int quality, - const uint8_t* icc_buf, - uint32_t icc_length) { - if (pSource->GetBPP() < 8 || pSource->GetPalette()) - return FALSE; - _JpegEncode(pSource, dest_buf, dest_size, quality, icc_buf, icc_length); - return TRUE; +bool CCodec_JpegModule::LoadInfo(const uint8_t* src_buf, + uint32_t src_size, + int* width, + int* height, + int* num_components, + int* bits_per_components, + bool* color_transform) { + return JpegLoadInfo(src_buf, src_size, width, height, num_components, + bits_per_components, color_transform); } + struct FXJPEG_Context { jmp_buf m_JumpMark; jpeg_decompress_struct m_Info; @@ -622,7 +445,7 @@ int CCodec_JpegModule::ReadHeader(FXJPEG_Context* ctx, *height = ctx->m_Info.image_height; *nComps = ctx->m_Info.num_components; #ifdef PDF_ENABLE_XFA - _JpegLoadAttribute(&ctx->m_Info, pAttribute); + JpegLoadAttribute(&ctx->m_Info, pAttribute); #endif return 0; } diff --git a/core/fxge/apple/apple_int.h b/core/fxge/apple/apple_int.h index ddfa792cf4..1ef1f6c2b9 100644 --- a/core/fxge/apple/apple_int.h +++ b/core/fxge/apple/apple_int.h @@ -87,9 +87,6 @@ class CFX_QuartzDeviceDriver : public IFX_RenderDeviceDriver { // IFX_RenderDeviceDriver int GetDeviceCaps(int caps_id) override; CFX_Matrix GetCTM() const override; - FX_BOOL IsPSPrintDriver() override { return FALSE; } - FX_BOOL StartRendering() override { return TRUE; } - void EndRendering() override {} void SaveState() override; void RestoreState(bool bKeepSaved) override; FX_BOOL SetClip_PathFill(const CFX_PathData* pPathData, @@ -107,13 +104,6 @@ class CFX_QuartzDeviceDriver : public IFX_RenderDeviceDriver { int alpha_flag = 0, void* pIccTransform = NULL, int blend_type = FXDIB_BLEND_NORMAL) override; - FX_BOOL SetPixel(int x, - int y, - uint32_t color, - int alpha_flag = 0, - void* pIccTransform = NULL) override { - return FALSE; - } FX_BOOL FillRect(const FX_RECT* pRect, uint32_t fill_color, int alpha_flag = 0, @@ -133,7 +123,6 @@ class CFX_QuartzDeviceDriver : public IFX_RenderDeviceDriver { int top, void* pIccTransform = NULL, FX_BOOL bDEdge = FALSE) override; - CFX_DIBitmap* GetBackDrop() override { return NULL; } FX_BOOL SetDIBits(const CFX_DIBSource* pBitmap, uint32_t color, const FX_RECT* pSrcRect, @@ -164,10 +153,6 @@ class CFX_QuartzDeviceDriver : public IFX_RenderDeviceDriver { int blend_type = FXDIB_BLEND_NORMAL) override { return FALSE; } - FX_BOOL ContinueDIBits(void* handle, IFX_Pause* pPause) override { - return FALSE; - } - void CancelDIBits(void* handle) override {} FX_BOOL DrawDeviceText(int nChars, const FXTEXT_CHARPOS* pCharPos, CFX_Font* pFont, @@ -177,7 +162,6 @@ class CFX_QuartzDeviceDriver : public IFX_RenderDeviceDriver { uint32_t color, int alpha_flag = 0, void* pIccTransform = NULL) override; - void* GetPlatformSurface() const override { return NULL; } void ClearDriver() override; protected: diff --git a/core/fxge/ge/fx_ge_ps.cpp b/core/fxge/ge/fx_ge_ps.cpp deleted file mode 100644 index b112cf3630..0000000000 --- a/core/fxge/ge/fx_ge_ps.cpp +++ /dev/null @@ -1,706 +0,0 @@ -// Copyright 2014 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#include "core/fxge/include/fx_ge.h" - -#include "core/fxcodec/include/fx_codec.h" -#include "core/fxge/ge/fx_text_int.h" - -struct PSGlyph { - CFX_Font* m_pFont; - uint32_t m_GlyphIndex; - FX_BOOL m_bGlyphAdjust; - FX_FLOAT m_AdjustMatrix[4]; -}; -class CPSFont { - public: - PSGlyph m_Glyphs[256]; - int m_nGlyphs; -}; -CFX_PSRenderer::CFX_PSRenderer() { - m_pOutput = NULL; - m_bColorSet = m_bGraphStateSet = FALSE; - m_bInited = FALSE; -} -CFX_PSRenderer::~CFX_PSRenderer() { - for (int i = 0; i < (int)m_PSFontList.GetSize(); i++) { - CPSFont* pFont = m_PSFontList[i]; - delete pFont; - } -} -#define OUTPUT_PS(str) m_pOutput->OutputPS(str, sizeof str - 1) -void CFX_PSRenderer::Init(IFX_PSOutput* pOutput, - int pslevel, - int width, - int height, - FX_BOOL bCmykOutput) { - m_PSLevel = pslevel; - m_pOutput = pOutput; - m_ClipBox.left = m_ClipBox.top = 0; - m_ClipBox.right = width; - m_ClipBox.bottom = height; - m_bCmykOutput = bCmykOutput; -} -FX_BOOL CFX_PSRenderer::StartRendering() { - if (m_bInited) { - return TRUE; - } - static const char init_str[] = - "\nsave\n/im/initmatrix load def\n" - "/n/newpath load def/m/moveto load def/l/lineto load def/c/curveto load " - "def/h/closepath load def\n" - "/f/fill load def/F/eofill load def/s/stroke load def/W/clip load " - "def/W*/eoclip load def\n" - "/rg/setrgbcolor load def/k/setcmykcolor load def\n" - "/J/setlinecap load def/j/setlinejoin load def/w/setlinewidth load " - "def/M/setmiterlimit load def/d/setdash load def\n" - "/q/gsave load def/Q/grestore load def/iM/imagemask load def\n" - "/Tj/show load def/Ff/findfont load def/Fs/scalefont load def/Sf/setfont " - "load def\n" - "/cm/concat load def/Cm/currentmatrix load def/mx/matrix load " - "def/sm/setmatrix load def\n"; - OUTPUT_PS(init_str); - m_bInited = TRUE; - return TRUE; -} -void CFX_PSRenderer::EndRendering() { - if (m_bInited) { - OUTPUT_PS("\nrestore\n"); - } - m_bInited = FALSE; -} -void CFX_PSRenderer::SaveState() { - StartRendering(); - OUTPUT_PS("q\n"); - m_ClipBoxStack.Add(m_ClipBox); -} - -void CFX_PSRenderer::RestoreState(bool bKeepSaved) { - StartRendering(); - if (bKeepSaved) { - OUTPUT_PS("Q\nq\n"); - } else { - OUTPUT_PS("Q\n"); - } - m_bColorSet = FALSE; - m_bGraphStateSet = FALSE; - int size = m_ClipBoxStack.GetSize(); - if (!size) - return; - - m_ClipBox = m_ClipBoxStack.GetAt(size - 1); - if (!bKeepSaved) - m_ClipBoxStack.RemoveAt(size - 1); -} - -void CFX_PSRenderer::OutputPath(const CFX_PathData* pPathData, - const CFX_Matrix* pObject2Device) { - int nPoints = pPathData->GetPointCount(); - CFX_ByteTextBuf buf; - buf.EstimateSize(nPoints * 10); - for (int i = 0; i < nPoints; i++) { - uint8_t flag = pPathData->GetFlag(i); - FX_FLOAT x = pPathData->GetPointX(i); - FX_FLOAT y = pPathData->GetPointY(i); - if (pObject2Device) { - pObject2Device->Transform(x, y); - } - buf << x << " " << y; - switch (flag & FXPT_TYPE) { - case FXPT_MOVETO: - buf << " m "; - break; - case FXPT_LINETO: - if (flag & FXPT_CLOSEFIGURE) { - buf << " l h "; - } else { - buf << " l "; - } - break; - case FXPT_BEZIERTO: { - FX_FLOAT x1 = pPathData->GetPointX(i + 1); - FX_FLOAT x2 = pPathData->GetPointX(i + 2); - FX_FLOAT y1 = pPathData->GetPointY(i + 1); - FX_FLOAT y2 = pPathData->GetPointY(i + 2); - if (pObject2Device) { - pObject2Device->Transform(x1, y1); - pObject2Device->Transform(x2, y2); - } - buf << " " << x1 << " " << y1 << " " << x2 << " " << y2; - if (flag & FXPT_CLOSEFIGURE) { - buf << " c h\n"; - } else { - buf << " c\n"; - } - i += 2; - break; - } - } - } - m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize()); -} -void CFX_PSRenderer::SetClip_PathFill(const CFX_PathData* pPathData, - const CFX_Matrix* pObject2Device, - int fill_mode) { - StartRendering(); - OutputPath(pPathData, pObject2Device); - CFX_FloatRect rect = pPathData->GetBoundingBox(); - if (pObject2Device) { - rect.Transform(pObject2Device); - } - m_ClipBox.Intersect(rect.GetOutterRect()); - if ((fill_mode & 3) == FXFILL_WINDING) { - OUTPUT_PS("W n\n"); - } else { - OUTPUT_PS("W* n\n"); - } -} -void CFX_PSRenderer::SetClip_PathStroke(const CFX_PathData* pPathData, - const CFX_Matrix* pObject2Device, - const CFX_GraphStateData* pGraphState) { - StartRendering(); - SetGraphState(pGraphState); - if (pObject2Device) { - CFX_ByteTextBuf buf; - buf << "mx Cm [" << pObject2Device->a << " " << pObject2Device->b << " " - << pObject2Device->c << " " << pObject2Device->d << " " - << pObject2Device->e << " " << pObject2Device->f << "]cm "; - m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize()); - } - OutputPath(pPathData, NULL); - CFX_FloatRect rect = pPathData->GetBoundingBox(pGraphState->m_LineWidth, - pGraphState->m_MiterLimit); - rect.Transform(pObject2Device); - m_ClipBox.Intersect(rect.GetOutterRect()); - if (pObject2Device) { - OUTPUT_PS("strokepath W n sm\n"); - } else { - OUTPUT_PS("strokepath W n\n"); - } -} -FX_BOOL CFX_PSRenderer::DrawPath(const CFX_PathData* pPathData, - const CFX_Matrix* pObject2Device, - const CFX_GraphStateData* pGraphState, - uint32_t fill_color, - uint32_t stroke_color, - int fill_mode, - int alpha_flag, - void* pIccTransform) { - StartRendering(); - int fill_alpha = FXGETFLAG_COLORTYPE(alpha_flag) - ? FXGETFLAG_ALPHA_FILL(alpha_flag) - : FXARGB_A(fill_color); - int stroke_alpha = FXGETFLAG_COLORTYPE(alpha_flag) - ? FXGETFLAG_ALPHA_STROKE(alpha_flag) - : FXARGB_A(stroke_color); - if (fill_alpha && fill_alpha < 255) { - return FALSE; - } - if (stroke_alpha && stroke_alpha < 255) { - return FALSE; - } - if (fill_alpha == 0 && stroke_alpha == 0) { - return FALSE; - } - if (stroke_alpha) { - SetGraphState(pGraphState); - if (pObject2Device) { - CFX_ByteTextBuf buf; - buf << "mx Cm [" << pObject2Device->a << " " << pObject2Device->b << " " - << pObject2Device->c << " " << pObject2Device->d << " " - << pObject2Device->e << " " << pObject2Device->f << "]cm "; - m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize()); - } - } - OutputPath(pPathData, stroke_alpha ? NULL : pObject2Device); - if (fill_mode && fill_alpha) { - SetColor(fill_color, alpha_flag, pIccTransform); - if ((fill_mode & 3) == FXFILL_WINDING) { - if (stroke_alpha) { - OUTPUT_PS("q f Q "); - } else { - OUTPUT_PS("f"); - } - } else if ((fill_mode & 3) == FXFILL_ALTERNATE) { - if (stroke_alpha) { - OUTPUT_PS("q F Q "); - } else { - OUTPUT_PS("F"); - } - } - } - if (stroke_alpha) { - SetColor(stroke_color, alpha_flag, pIccTransform); - if (pObject2Device) { - OUTPUT_PS("s sm"); - } else { - OUTPUT_PS("s"); - } - } - OUTPUT_PS("\n"); - return TRUE; -} -void CFX_PSRenderer::SetGraphState(const CFX_GraphStateData* pGraphState) { - CFX_ByteTextBuf buf; - if (!m_bGraphStateSet || - m_CurGraphState.m_LineCap != pGraphState->m_LineCap) { - buf << pGraphState->m_LineCap << " J\n"; - } - if (!m_bGraphStateSet || - m_CurGraphState.m_DashCount != pGraphState->m_DashCount || - FXSYS_memcmp(m_CurGraphState.m_DashArray, pGraphState->m_DashArray, - sizeof(FX_FLOAT) * m_CurGraphState.m_DashCount)) { - buf << "["; - for (int i = 0; i < pGraphState->m_DashCount; i++) { - buf << pGraphState->m_DashArray[i] << " "; - } - buf << "]" << pGraphState->m_DashPhase << " d\n"; - } - if (!m_bGraphStateSet || - m_CurGraphState.m_LineJoin != pGraphState->m_LineJoin) { - buf << pGraphState->m_LineJoin << " j\n"; - } - if (!m_bGraphStateSet || - m_CurGraphState.m_LineWidth != pGraphState->m_LineWidth) { - buf << pGraphState->m_LineWidth << " w\n"; - } - if (!m_bGraphStateSet || - m_CurGraphState.m_MiterLimit != pGraphState->m_MiterLimit) { - buf << pGraphState->m_MiterLimit << " M\n"; - } - m_CurGraphState.Copy(*pGraphState); - m_bGraphStateSet = TRUE; - if (buf.GetSize()) { - m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize()); - } -} -static void FaxCompressData(uint8_t* src_buf, - int width, - int height, - uint8_t*& dest_buf, - uint32_t& dest_size) { - CCodec_ModuleMgr* pEncoders = CFX_GEModule::Get()->GetCodecModule(); - if (width * height > 128 && pEncoders && - pEncoders->GetFaxModule()->Encode(src_buf, width, height, (width + 7) / 8, - dest_buf, dest_size)) { - FX_Free(src_buf); - } else { - dest_buf = src_buf; - dest_size = (width + 7) / 8 * height; - } -} -static void PSCompressData(int PSLevel, - uint8_t* src_buf, - uint32_t src_size, - uint8_t*& output_buf, - uint32_t& output_size, - const FX_CHAR*& filter) { - output_buf = src_buf; - output_size = src_size; - filter = ""; - if (src_size < 1024) { - return; - } - CCodec_ModuleMgr* pEncoders = CFX_GEModule::Get()->GetCodecModule(); - uint8_t* dest_buf = NULL; - uint32_t dest_size = src_size; - if (PSLevel >= 3) { - if (pEncoders && - pEncoders->GetFlateModule()->Encode(src_buf, src_size, dest_buf, - dest_size)) { - filter = "/FlateDecode filter "; - } - } else { - if (pEncoders && - pEncoders->GetBasicModule()->RunLengthEncode(src_buf, src_size, - dest_buf, dest_size)) { - filter = "/RunLengthDecode filter "; - } - } - if (dest_size < src_size) { - output_buf = dest_buf; - output_size = dest_size; - } else { - filter = NULL; - FX_Free(dest_buf); - } -} -FX_BOOL CFX_PSRenderer::SetDIBits(const CFX_DIBSource* pSource, - uint32_t color, - int left, - int top, - int alpha_flag, - void* pIccTransform) { - StartRendering(); - CFX_Matrix matrix((FX_FLOAT)(pSource->GetWidth()), 0.0f, 0.0f, - -(FX_FLOAT)(pSource->GetHeight()), (FX_FLOAT)(left), - (FX_FLOAT)(top + pSource->GetHeight())); - return DrawDIBits(pSource, color, &matrix, 0, alpha_flag, pIccTransform); -} -FX_BOOL CFX_PSRenderer::StretchDIBits(const CFX_DIBSource* pSource, - uint32_t color, - int dest_left, - int dest_top, - int dest_width, - int dest_height, - uint32_t flags, - int alpha_flag, - void* pIccTransform) { - StartRendering(); - CFX_Matrix matrix((FX_FLOAT)(dest_width), 0.0f, 0.0f, - (FX_FLOAT)(-dest_height), (FX_FLOAT)(dest_left), - (FX_FLOAT)(dest_top + dest_height)); - return DrawDIBits(pSource, color, &matrix, flags, alpha_flag, pIccTransform); -} -FX_BOOL CFX_PSRenderer::DrawDIBits(const CFX_DIBSource* pSource, - uint32_t color, - const CFX_Matrix* pMatrix, - uint32_t flags, - int alpha_flag, - void* pIccTransform) { - StartRendering(); - if ((pMatrix->a == 0 && pMatrix->b == 0) || - (pMatrix->c == 0 && pMatrix->d == 0)) { - return TRUE; - } - if (pSource->HasAlpha()) { - return FALSE; - } - int alpha = FXGETFLAG_COLORTYPE(alpha_flag) ? FXGETFLAG_ALPHA_FILL(color) - : FXARGB_A(color); - if (pSource->IsAlphaMask() && (alpha < 255 || pSource->GetBPP() != 1)) { - return FALSE; - } - OUTPUT_PS("q\n"); - CFX_ByteTextBuf buf; - buf << "[" << pMatrix->a << " " << pMatrix->b << " " << pMatrix->c << " " - << pMatrix->d << " " << pMatrix->e << " " << pMatrix->f << "]cm "; - int width = pSource->GetWidth(); - int height = pSource->GetHeight(); - buf << width << " " << height; - if (pSource->GetBPP() == 1 && !pSource->GetPalette()) { - int pitch = (width + 7) / 8; - uint32_t src_size = height * pitch; - uint8_t* src_buf = FX_Alloc(uint8_t, src_size); - for (int row = 0; row < height; row++) { - const uint8_t* src_scan = pSource->GetScanline(row); - FXSYS_memcpy(src_buf + row * pitch, src_scan, pitch); - } - uint8_t* output_buf; - uint32_t output_size; - FaxCompressData(src_buf, width, height, output_buf, output_size); - if (pSource->IsAlphaMask()) { - SetColor(color, alpha_flag, pIccTransform); - m_bColorSet = FALSE; - buf << " true["; - } else { - buf << " 1["; - } - buf << width << " 0 0 -" << height << " 0 " << height - << "]currentfile/ASCII85Decode filter "; - if (output_buf != src_buf) - buf << "<>/CCITTFaxDecode filter "; - if (pSource->IsAlphaMask()) { - buf << "iM\n"; - } else { - buf << "false 1 colorimage\n"; - } - m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize()); - WritePSBinary(output_buf, output_size); - FX_Free(output_buf); - } else { - CFX_DIBSource* pConverted = (CFX_DIBSource*)pSource; - if (pIccTransform) { - FXDIB_Format format = m_bCmykOutput ? FXDIB_Cmyk : FXDIB_Rgb; - pConverted = pSource->CloneConvert(format, NULL, pIccTransform); - } else { - switch (pSource->GetFormat()) { - case FXDIB_1bppRgb: - case FXDIB_Rgb32: - pConverted = pSource->CloneConvert(FXDIB_Rgb); - break; - case FXDIB_8bppRgb: - if (pSource->GetPalette()) { - pConverted = pSource->CloneConvert(FXDIB_Rgb); - } - break; - case FXDIB_1bppCmyk: - pConverted = pSource->CloneConvert(FXDIB_Cmyk); - break; - case FXDIB_8bppCmyk: - if (pSource->GetPalette()) { - pConverted = pSource->CloneConvert(FXDIB_Cmyk); - } - break; - default: - break; - } - } - if (!pConverted) { - OUTPUT_PS("\nQ\n"); - return FALSE; - } - int Bpp = pConverted->GetBPP() / 8; - uint8_t* output_buf = NULL; - FX_STRSIZE output_size = 0; - const FX_CHAR* filter = NULL; - if (flags & FXRENDER_IMAGE_LOSSY) { - CCodec_ModuleMgr* pEncoders = CFX_GEModule::Get()->GetCodecModule(); - if (pEncoders && - pEncoders->GetJpegModule()->Encode(pConverted, output_buf, - output_size)) { - filter = "/DCTDecode filter "; - } - } - if (!filter) { - int src_pitch = width * Bpp; - output_size = height * src_pitch; - output_buf = FX_Alloc(uint8_t, output_size); - for (int row = 0; row < height; row++) { - const uint8_t* src_scan = pConverted->GetScanline(row); - uint8_t* dest_scan = output_buf + row * src_pitch; - if (Bpp == 3) { - for (int col = 0; col < width; col++) { - *dest_scan++ = src_scan[2]; - *dest_scan++ = src_scan[1]; - *dest_scan++ = *src_scan; - src_scan += 3; - } - } else { - FXSYS_memcpy(dest_scan, src_scan, src_pitch); - } - } - uint8_t* compressed_buf; - uint32_t compressed_size; - PSCompressData(m_PSLevel, output_buf, output_size, compressed_buf, - compressed_size, filter); - if (output_buf != compressed_buf) { - FX_Free(output_buf); - } - output_buf = compressed_buf; - output_size = compressed_size; - } - if (pConverted != pSource) { - delete pConverted; - pConverted = NULL; - } - buf << " 8["; - buf << width << " 0 0 -" << height << " 0 " << height << "]"; - buf << "currentfile/ASCII85Decode filter "; - if (filter) { - buf << filter; - } - buf << "false " << Bpp; - buf << " colorimage\n"; - m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize()); - WritePSBinary(output_buf, output_size); - FX_Free(output_buf); - } - OUTPUT_PS("\nQ\n"); - return TRUE; -} -void CFX_PSRenderer::SetColor(uint32_t color, - int alpha_flag, - void* pIccTransform) { - if (!CFX_GEModule::Get()->GetCodecModule() || - !CFX_GEModule::Get()->GetCodecModule()->GetIccModule()) { - pIccTransform = NULL; - } - FX_BOOL bCMYK = FALSE; - if (pIccTransform) { - CCodec_IccModule* pIccModule = - CFX_GEModule::Get()->GetCodecModule()->GetIccModule(); - color = FXGETFLAG_COLORTYPE(alpha_flag) ? FXCMYK_TODIB(color) - : FXARGB_TODIB(color); - uint8_t* pColor = (uint8_t*)&color; - pIccModule->TranslateScanline(pIccTransform, pColor, pColor, 1); - color = m_bCmykOutput ? FXCMYK_TODIB(color) : FXARGB_TODIB(color); - bCMYK = m_bCmykOutput; - } else { - bCMYK = FXGETFLAG_COLORTYPE(alpha_flag); - } - if (bCMYK != m_bCmykOutput || !m_bColorSet || m_LastColor != color) { - CFX_ByteTextBuf buf; - if (bCMYK) { - buf << FXSYS_GetCValue(color) / 255.0 << " " - << FXSYS_GetMValue(color) / 255.0 << " " - << FXSYS_GetYValue(color) / 255.0 << " " - << FXSYS_GetKValue(color) / 255.0 << " k\n"; - } else { - buf << FXARGB_R(color) / 255.0 << " " << FXARGB_G(color) / 255.0 << " " - << FXARGB_B(color) / 255.0 << " rg\n"; - } - if (bCMYK == m_bCmykOutput) { - m_bColorSet = TRUE; - m_LastColor = color; - } - m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize()); - } -} -void CFX_PSRenderer::FindPSFontGlyph(CFX_FaceCache* pFaceCache, - CFX_Font* pFont, - const FXTEXT_CHARPOS& charpos, - int& ps_fontnum, - int& ps_glyphindex) { - for (int i = 0; i < (int)m_PSFontList.GetSize(); i++) { - CPSFont* pPSFont = m_PSFontList[i]; - for (int j = 0; j < pPSFont->m_nGlyphs; j++) - if (pPSFont->m_Glyphs[j].m_pFont == pFont && - pPSFont->m_Glyphs[j].m_GlyphIndex == charpos.m_GlyphIndex) { - if ((!pPSFont->m_Glyphs[j].m_bGlyphAdjust && !charpos.m_bGlyphAdjust) || - (pPSFont->m_Glyphs[j].m_bGlyphAdjust && charpos.m_bGlyphAdjust && - (FXSYS_fabs(pPSFont->m_Glyphs[j].m_AdjustMatrix[0] - - charpos.m_AdjustMatrix[0]) < 0.01 && - FXSYS_fabs(pPSFont->m_Glyphs[j].m_AdjustMatrix[1] - - charpos.m_AdjustMatrix[1]) < 0.01 && - FXSYS_fabs(pPSFont->m_Glyphs[j].m_AdjustMatrix[2] - - charpos.m_AdjustMatrix[2]) < 0.01 && - FXSYS_fabs(pPSFont->m_Glyphs[j].m_AdjustMatrix[3] - - charpos.m_AdjustMatrix[3]) < 0.01))) { - ps_fontnum = i; - ps_glyphindex = j; - return; - } - } - } - if (m_PSFontList.GetSize() == 0 || - m_PSFontList[m_PSFontList.GetSize() - 1]->m_nGlyphs == 256) { - CPSFont* pPSFont = new CPSFont; - pPSFont->m_nGlyphs = 0; - m_PSFontList.Add(pPSFont); - CFX_ByteTextBuf buf; - buf << "8 dict begin/FontType 3 def/FontMatrix[1 0 0 1 0 0]def\n" - "/FontBBox[0 0 0 0]def/Encoding 256 array def 0 1 255{Encoding " - "exch/.notdef put}for\n" - "/CharProcs 1 dict def CharProcs begin/.notdef {} def end\n" - "/BuildGlyph{1 0 -10 -10 10 10 setcachedevice exch/CharProcs get " - "exch 2 copy known not{pop/.notdef}if get exec}bind def\n" - "/BuildChar{1 index/Encoding get exch get 1 index/BuildGlyph get " - "exec}bind def\n" - "currentdict end\n"; - buf << "/X" << m_PSFontList.GetSize() - 1 << " exch definefont pop\n"; - m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize()); - buf.Clear(); - } - ps_fontnum = m_PSFontList.GetSize() - 1; - CPSFont* pPSFont = m_PSFontList[ps_fontnum]; - ps_glyphindex = pPSFont->m_nGlyphs; - pPSFont->m_Glyphs[ps_glyphindex].m_GlyphIndex = charpos.m_GlyphIndex; - pPSFont->m_Glyphs[ps_glyphindex].m_pFont = pFont; - pPSFont->m_Glyphs[ps_glyphindex].m_bGlyphAdjust = charpos.m_bGlyphAdjust; - if (charpos.m_bGlyphAdjust) { - pPSFont->m_Glyphs[ps_glyphindex].m_AdjustMatrix[0] = - charpos.m_AdjustMatrix[0]; - pPSFont->m_Glyphs[ps_glyphindex].m_AdjustMatrix[1] = - charpos.m_AdjustMatrix[1]; - pPSFont->m_Glyphs[ps_glyphindex].m_AdjustMatrix[2] = - charpos.m_AdjustMatrix[2]; - pPSFont->m_Glyphs[ps_glyphindex].m_AdjustMatrix[3] = - charpos.m_AdjustMatrix[3]; - } - pPSFont->m_nGlyphs++; - CFX_Matrix matrix; - if (charpos.m_bGlyphAdjust) - matrix.Set(charpos.m_AdjustMatrix[0], charpos.m_AdjustMatrix[1], - charpos.m_AdjustMatrix[2], charpos.m_AdjustMatrix[3], 0, 0); - matrix.Concat(1.0f, 0, 0, 1.0f, 0, 0); - const CFX_PathData* pPathData = pFaceCache->LoadGlyphPath( - pFont, charpos.m_GlyphIndex, charpos.m_FontCharWidth); - if (!pPathData) { - return; - } - CFX_PathData TransformedPath(*pPathData); - if (charpos.m_bGlyphAdjust) { - TransformedPath.Transform(&matrix); - } - CFX_ByteTextBuf buf; - buf << "/X" << ps_fontnum << " Ff/CharProcs get begin/" << ps_glyphindex - << "{n "; - for (int p = 0; p < TransformedPath.GetPointCount(); p++) { - FX_FLOAT x = TransformedPath.GetPointX(p), y = TransformedPath.GetPointY(p); - switch (TransformedPath.GetFlag(p) & FXPT_TYPE) { - case FXPT_MOVETO: { - buf << x << " " << y << " m\n"; - break; - } - case FXPT_LINETO: { - buf << x << " " << y << " l\n"; - break; - } - case FXPT_BEZIERTO: { - buf << x << " " << y << " " << TransformedPath.GetPointX(p + 1) << " " - << TransformedPath.GetPointY(p + 1) << " " - << TransformedPath.GetPointX(p + 2) << " " - << TransformedPath.GetPointY(p + 2) << " c\n"; - p += 2; - break; - } - } - } - buf << "f}bind def end\n"; - buf << "/X" << ps_fontnum << " Ff/Encoding get " << ps_glyphindex << "/" - << ps_glyphindex << " put\n"; - m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize()); -} -FX_BOOL CFX_PSRenderer::DrawText(int nChars, - const FXTEXT_CHARPOS* pCharPos, - CFX_Font* pFont, - CFX_FontCache* pCache, - const CFX_Matrix* pObject2Device, - FX_FLOAT font_size, - uint32_t color, - int alpha_flag, - void* pIccTransform) { - StartRendering(); - int alpha = FXGETFLAG_COLORTYPE(alpha_flag) ? FXGETFLAG_ALPHA_FILL(alpha_flag) - : FXARGB_A(color); - if (alpha < 255) { - return FALSE; - } - if ((pObject2Device->a == 0 && pObject2Device->b == 0) || - (pObject2Device->c == 0 && pObject2Device->d == 0)) { - return TRUE; - } - SetColor(color, alpha_flag, pIccTransform); - CFX_ByteTextBuf buf; - buf << "q[" << pObject2Device->a << " " << pObject2Device->b << " " - << pObject2Device->c << " " << pObject2Device->d << " " - << pObject2Device->e << " " << pObject2Device->f << "]cm\n"; - if (!pCache) { - pCache = CFX_GEModule::Get()->GetFontCache(); - } - CFX_FaceCache* pFaceCache = pCache->GetCachedFace(pFont); - FX_FONTCACHE_DEFINE(pCache, pFont); - int last_fontnum = -1; - for (int i = 0; i < nChars; i++) { - int ps_fontnum, ps_glyphindex; - FindPSFontGlyph(pFaceCache, pFont, pCharPos[i], ps_fontnum, ps_glyphindex); - if (last_fontnum != ps_fontnum) { - buf << "/X" << ps_fontnum << " Ff " << font_size << " Fs Sf "; - last_fontnum = ps_fontnum; - } - buf << pCharPos[i].m_OriginX << " " << pCharPos[i].m_OriginY << " m"; - CFX_ByteString hex; - hex.Format("<%02X>", ps_glyphindex); - buf << hex.AsStringC() << "Tj\n"; - } - buf << "Q\n"; - m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize()); - return TRUE; -} -void CFX_PSRenderer::WritePSBinary(const uint8_t* data, int len) { - uint8_t* dest_buf; - uint32_t dest_size; - CCodec_ModuleMgr* pEncoders = CFX_GEModule::Get()->GetCodecModule(); - if (pEncoders && - pEncoders->GetBasicModule()->A85Encode(data, len, dest_buf, dest_size)) { - m_pOutput->OutputPS((const FX_CHAR*)dest_buf, dest_size); - FX_Free(dest_buf); - } else { - m_pOutput->OutputPS((const FX_CHAR*)data, len); - } -} diff --git a/core/fxge/ge/fx_ge_text.cpp b/core/fxge/ge/fx_ge_text.cpp index 042e6f2b5b..3243b505b1 100644 --- a/core/fxge/ge/fx_ge_text.cpp +++ b/core/fxge/ge/fx_ge_text.cpp @@ -471,7 +471,7 @@ FX_BOOL CFX_RenderDevice::DrawNormalText(int nChars, } char2device.Scale(font_size, -font_size); if (FXSYS_fabs(char2device.a) + FXSYS_fabs(char2device.b) > 50 * 1.0f || - ((m_DeviceClass == FXDC_PRINTER && !m_pDeviceDriver->IsPSPrintDriver()) && + ((m_DeviceClass == FXDC_PRINTER) && !(text_flags & FXTEXT_PRINTIMAGETEXT))) { if (pFont->GetFace() || (pFont->GetSubstFont()->m_SubstFlags & FXFONT_SUBST_GLYPHPATH)) { diff --git a/core/fxge/include/fx_ge.h b/core/fxge/include/fx_ge.h index 8111e46d13..1cbbc2cc97 100644 --- a/core/fxge/include/fx_ge.h +++ b/core/fxge/include/fx_ge.h @@ -433,8 +433,6 @@ class IFX_RenderDeviceDriver { virtual CFX_Matrix GetCTM() const { return CFX_Matrix(); } - virtual FX_BOOL IsPSPrintDriver() { return FALSE; } - virtual FX_BOOL StartRendering() { return TRUE; } virtual void EndRendering() {} @@ -563,98 +561,4 @@ class IFX_RenderDeviceDriver { } }; -class IFX_PSOutput { - public: - virtual void Release() = 0; - virtual void OutputPS(const FX_CHAR* str, int len) = 0; - - protected: - virtual ~IFX_PSOutput() {} -}; - -class CFX_PSRenderer { - public: - CFX_PSRenderer(); - ~CFX_PSRenderer(); - - void Init(IFX_PSOutput* pOutput, - int ps_level, - int width, - int height, - FX_BOOL bCmykOutput); - FX_BOOL StartRendering(); - void EndRendering(); - void SaveState(); - void RestoreState(bool bKeepSaved); - void SetClip_PathFill(const CFX_PathData* pPathData, - const CFX_Matrix* pObject2Device, - int fill_mode); - void SetClip_PathStroke(const CFX_PathData* pPathData, - const CFX_Matrix* pObject2Device, - const CFX_GraphStateData* pGraphState); - FX_RECT GetClipBox() { return m_ClipBox; } - FX_BOOL DrawPath(const CFX_PathData* pPathData, - const CFX_Matrix* pObject2Device, - const CFX_GraphStateData* pGraphState, - uint32_t fill_color, - uint32_t stroke_color, - int fill_mode, - int alpha_flag = 0, - void* pIccTransform = NULL); - FX_BOOL SetDIBits(const CFX_DIBSource* pBitmap, - uint32_t color, - int dest_left, - int dest_top, - int alpha_flag = 0, - void* pIccTransform = NULL); - FX_BOOL StretchDIBits(const CFX_DIBSource* pBitmap, - uint32_t color, - int dest_left, - int dest_top, - int dest_width, - int dest_height, - uint32_t flags, - int alpha_flag = 0, - void* pIccTransform = NULL); - FX_BOOL DrawDIBits(const CFX_DIBSource* pBitmap, - uint32_t color, - const CFX_Matrix* pMatrix, - uint32_t flags, - int alpha_flag = 0, - void* pIccTransform = NULL); - FX_BOOL DrawText(int nChars, - const FXTEXT_CHARPOS* pCharPos, - CFX_Font* pFont, - CFX_FontCache* pCache, - const CFX_Matrix* pObject2Device, - FX_FLOAT font_size, - uint32_t color, - int alpha_flag = 0, - void* pIccTransform = NULL); - - private: - void OutputPath(const CFX_PathData* pPathData, - const CFX_Matrix* pObject2Device); - void SetGraphState(const CFX_GraphStateData* pGraphState); - void SetColor(uint32_t color, int alpha_flag, void* pIccTransform); - void FindPSFontGlyph(CFX_FaceCache* pFaceCache, - CFX_Font* pFont, - const FXTEXT_CHARPOS& charpos, - int& ps_fontnum, - int& ps_glyphindex); - void WritePSBinary(const uint8_t* data, int len); - - IFX_PSOutput* m_pOutput; - int m_PSLevel; - CFX_GraphStateData m_CurGraphState; - FX_BOOL m_bGraphStateSet; - FX_BOOL m_bCmykOutput; - FX_BOOL m_bColorSet; - uint32_t m_LastColor; - FX_RECT m_ClipBox; - CFX_ArrayTemplate m_PSFontList; - CFX_ArrayTemplate m_ClipBoxStack; - FX_BOOL m_bInited; -}; - #endif // CORE_FXGE_INCLUDE_FX_GE_H_ diff --git a/core/fxge/include/fx_ge_win32.h b/core/fxge/include/fx_ge_win32.h index b4337c117c..a6d8fbb378 100644 --- a/core/fxge/include/fx_ge_win32.h +++ b/core/fxge/include/fx_ge_win32.h @@ -22,6 +22,7 @@ typedef struct WINDIB_Open_Args_ { const FX_WCHAR* path_name; } WINDIB_Open_Args_; + class CFX_WindowsDIB : public CFX_DIBitmap { public: static CFX_ByteString GetBitmapInfo(const CFX_DIBitmap* pBitmap); @@ -62,26 +63,20 @@ class CFX_WindowsDIB : public CFX_DIBitmap { HBITMAP m_hOldBitmap; }; + class CFX_WindowsDevice : public CFX_RenderDevice { public: - static IFX_RenderDeviceDriver* CreateDriver(HDC hDC, - FX_BOOL bCmykOutput = FALSE); + static IFX_RenderDeviceDriver* CreateDriver(HDC hDC); - CFX_WindowsDevice(HDC hDC, - FX_BOOL bCmykOutput = FALSE, - FX_BOOL bForcePSOutput = FALSE, - int psLevel = 2); + explicit CFX_WindowsDevice(HDC hDC); + ~CFX_WindowsDevice(); HDC GetDC() const; - - FX_BOOL m_bForcePSOutput; - - static int m_psLevel; }; + class CFX_WinBitmapDevice : public CFX_RenderDevice { public: CFX_WinBitmapDevice(int width, int height, FXDIB_Format format); - ~CFX_WinBitmapDevice(); HDC GetDC() { return m_hDC; } diff --git a/core/fxge/win32/fx_win32_device.cpp b/core/fxge/win32/fx_win32_device.cpp index 240fb30e9e..b791217052 100644 --- a/core/fxge/win32/fx_win32_device.cpp +++ b/core/fxge/win32/fx_win32_device.cpp @@ -1408,31 +1408,19 @@ FX_BOOL CGdiDisplayDriver::StretchDIBits(const CFX_DIBSource* pSource, return FALSE; } -// static -int CFX_WindowsDevice::m_psLevel = 2; - -CFX_WindowsDevice::CFX_WindowsDevice(HDC hDC, - FX_BOOL bCmykOutput, - FX_BOOL bForcePSOutput, - int psLevel) { - m_bForcePSOutput = bForcePSOutput; - m_psLevel = psLevel; - if (bForcePSOutput) { - IFX_RenderDeviceDriver* pDriver = new CPSPrinterDriver; - ((CPSPrinterDriver*)pDriver)->Init(hDC, psLevel, bCmykOutput); - SetDeviceDriver(pDriver); - return; - } - SetDeviceDriver(CreateDriver(hDC, bCmykOutput)); +CFX_WindowsDevice::CFX_WindowsDevice(HDC hDC) { + SetDeviceDriver(CreateDriver(hDC)); } +CFX_WindowsDevice::~CFX_WindowsDevice() {} + HDC CFX_WindowsDevice::GetDC() const { IFX_RenderDeviceDriver* pRDD = GetDeviceDriver(); return pRDD ? reinterpret_cast(pRDD->GetPlatformSurface()) : nullptr; } -IFX_RenderDeviceDriver* CFX_WindowsDevice::CreateDriver(HDC hDC, - FX_BOOL bCmykOutput) { +// static +IFX_RenderDeviceDriver* CFX_WindowsDevice::CreateDriver(HDC hDC) { int device_type = ::GetDeviceCaps(hDC, TECHNOLOGY); int obj_type = ::GetObjectType(hDC); bool use_printer = device_type == DT_RASPRINTER || diff --git a/core/fxge/win32/fx_win32_print.cpp b/core/fxge/win32/fx_win32_print.cpp index e46beeefbd..08029bc351 100644 --- a/core/fxge/win32/fx_win32_print.cpp +++ b/core/fxge/win32/fx_win32_print.cpp @@ -276,199 +276,4 @@ FX_BOOL CGdiPrinterDriver::StartDIBits(const CFX_DIBSource* pSource, return ret; } -CPSOutput::CPSOutput(HDC hDC) { - m_hDC = hDC; - m_pBuf = NULL; -} -CPSOutput::~CPSOutput() { - FX_Free(m_pBuf); -} -void CPSOutput::Init() { - m_pBuf = FX_Alloc(FX_CHAR, 1026); -} -void CPSOutput::OutputPS(const FX_CHAR* str, int len) { - if (len < 0) { - len = (int)FXSYS_strlen(str); - } - int sent_len = 0; - while (len > 0) { - int send_len = len > 1024 ? 1024 : len; - *(uint16_t*)m_pBuf = send_len; - FXSYS_memcpy(m_pBuf + 2, str + sent_len, send_len); - ExtEscape(m_hDC, PASSTHROUGH, send_len + 2, m_pBuf, 0, NULL); - sent_len += send_len; - len -= send_len; - } -} -CPSPrinterDriver::CPSPrinterDriver() { - m_pPSOutput = NULL; - m_bCmykOutput = FALSE; -} -CPSPrinterDriver::~CPSPrinterDriver() { - EndRendering(); - delete m_pPSOutput; -} -FX_BOOL CPSPrinterDriver::Init(HDC hDC, int pslevel, FX_BOOL bCmykOutput) { - m_hDC = hDC; - m_HorzSize = ::GetDeviceCaps(m_hDC, HORZSIZE); - m_VertSize = ::GetDeviceCaps(m_hDC, VERTSIZE); - m_Width = ::GetDeviceCaps(m_hDC, HORZRES); - m_Height = ::GetDeviceCaps(m_hDC, VERTRES); - m_nBitsPerPixel = ::GetDeviceCaps(m_hDC, BITSPIXEL); - m_pPSOutput = new CPSOutput(hDC); - ((CPSOutput*)m_pPSOutput)->Init(); - m_PSRenderer.Init(m_pPSOutput, pslevel, m_Width, m_Height, bCmykOutput); - m_bCmykOutput = bCmykOutput; - HRGN hRgn = ::CreateRectRgn(0, 0, 1, 1); - int ret = ::GetClipRgn(hDC, hRgn); - if (ret == 1) { - ret = ::GetRegionData(hRgn, 0, NULL); - if (ret) { - RGNDATA* pData = (RGNDATA*)FX_Alloc(uint8_t, ret); - ret = ::GetRegionData(hRgn, ret, pData); - if (ret) { - CFX_PathData path; - path.AllocPointCount(pData->rdh.nCount * 5); - for (uint32_t i = 0; i < pData->rdh.nCount; i++) { - RECT* pRect = (RECT*)(pData->Buffer + pData->rdh.nRgnSize * i); - path.AppendRect((FX_FLOAT)pRect->left, (FX_FLOAT)pRect->bottom, - (FX_FLOAT)pRect->right, (FX_FLOAT)pRect->top); - } - m_PSRenderer.SetClip_PathFill(&path, NULL, FXFILL_WINDING); - } - FX_Free(pData); - } - } - ::DeleteObject(hRgn); - return TRUE; -} -int CPSPrinterDriver::GetDeviceCaps(int caps_id) { - switch (caps_id) { - case FXDC_DEVICE_CLASS: - return FXDC_PRINTER; - case FXDC_PIXEL_WIDTH: - return m_Width; - case FXDC_PIXEL_HEIGHT: - return m_Height; - case FXDC_BITS_PIXEL: - return m_nBitsPerPixel; - case FXDC_RENDER_CAPS: - return m_bCmykOutput ? FXRC_BIT_MASK | FXRC_CMYK_OUTPUT : FXRC_BIT_MASK; - case FXDC_HORZ_SIZE: - return m_HorzSize; - case FXDC_VERT_SIZE: - return m_VertSize; - } - return 0; -} -FX_BOOL CPSPrinterDriver::StartRendering() { - return m_PSRenderer.StartRendering(); -} -void CPSPrinterDriver::EndRendering() { - m_PSRenderer.EndRendering(); -} -void CPSPrinterDriver::SaveState() { - m_PSRenderer.SaveState(); -} - -void CPSPrinterDriver::RestoreState(bool bKeepSaved) { - m_PSRenderer.RestoreState(bKeepSaved); -} - -FX_BOOL CPSPrinterDriver::SetClip_PathFill(const CFX_PathData* pPathData, - const CFX_Matrix* pObject2Device, - int fill_mode) { - m_PSRenderer.SetClip_PathFill(pPathData, pObject2Device, fill_mode); - return TRUE; -} -FX_BOOL CPSPrinterDriver::SetClip_PathStroke( - const CFX_PathData* pPathData, - const CFX_Matrix* pObject2Device, - const CFX_GraphStateData* pGraphState) { - m_PSRenderer.SetClip_PathStroke(pPathData, pObject2Device, pGraphState); - return TRUE; -} -FX_BOOL CPSPrinterDriver::DrawPath(const CFX_PathData* pPathData, - const CFX_Matrix* pObject2Device, - const CFX_GraphStateData* pGraphState, - FX_ARGB fill_color, - FX_ARGB stroke_color, - int fill_mode, - int alpha_flag, - void* pIccTransform, - int blend_type) { - if (blend_type != FXDIB_BLEND_NORMAL) { - return FALSE; - } - return m_PSRenderer.DrawPath(pPathData, pObject2Device, pGraphState, - fill_color, stroke_color, fill_mode & 3, - alpha_flag, pIccTransform); -} -FX_BOOL CPSPrinterDriver::GetClipBox(FX_RECT* pRect) { - *pRect = m_PSRenderer.GetClipBox(); - return TRUE; -} -FX_BOOL CPSPrinterDriver::SetDIBits(const CFX_DIBSource* pBitmap, - uint32_t color, - const FX_RECT* pSrcRect, - int left, - int top, - int blend_type, - int alpha_flag, - void* pIccTransform) { - if (blend_type != FXDIB_BLEND_NORMAL) { - return FALSE; - } - return m_PSRenderer.SetDIBits(pBitmap, color, left, top, alpha_flag, - pIccTransform); -} -FX_BOOL CPSPrinterDriver::StretchDIBits(const CFX_DIBSource* pBitmap, - uint32_t color, - int dest_left, - int dest_top, - int dest_width, - int dest_height, - const FX_RECT* pClipRect, - uint32_t flags, - int alpha_flag, - void* pIccTransform, - int blend_type) { - if (blend_type != FXDIB_BLEND_NORMAL) { - return FALSE; - } - return m_PSRenderer.StretchDIBits(pBitmap, color, dest_left, dest_top, - dest_width, dest_height, flags, alpha_flag, - pIccTransform); -} -FX_BOOL CPSPrinterDriver::StartDIBits(const CFX_DIBSource* pBitmap, - int bitmap_alpha, - uint32_t color, - const CFX_Matrix* pMatrix, - uint32_t render_flags, - void*& handle, - int alpha_flag, - void* pIccTransform, - int blend_type) { - if (blend_type != FXDIB_BLEND_NORMAL) { - return FALSE; - } - if (bitmap_alpha < 255) { - return FALSE; - } - handle = NULL; - return m_PSRenderer.DrawDIBits(pBitmap, color, pMatrix, render_flags, - alpha_flag, pIccTransform); -} -FX_BOOL CPSPrinterDriver::DrawDeviceText(int nChars, - const FXTEXT_CHARPOS* pCharPos, - CFX_Font* pFont, - CFX_FontCache* pCache, - const CFX_Matrix* pObject2Device, - FX_FLOAT font_size, - uint32_t color, - int alpha_flag, - void* pIccTransform) { - return m_PSRenderer.DrawText(nChars, pCharPos, pFont, pCache, pObject2Device, - font_size, color, alpha_flag, pIccTransform); -} #endif diff --git a/core/fxge/win32/win32_int.h b/core/fxge/win32/win32_int.h index 9d62b41a8e..08253b33de 100644 --- a/core/fxge/win32/win32_int.h +++ b/core/fxge/win32/win32_int.h @@ -273,96 +273,4 @@ class CGdiPrinterDriver : public CGdiDeviceDriver { const int m_VertSize; }; -class CPSOutput : public IFX_PSOutput { - public: - explicit CPSOutput(HDC hDC); - ~CPSOutput() override; - - // IFX_PSOutput - void Release() override { delete this; } - void OutputPS(const FX_CHAR* str, int len) override; - - void Init(); - - HDC m_hDC; - FX_CHAR* m_pBuf; -}; - -class CPSPrinterDriver : public IFX_RenderDeviceDriver { - public: - CPSPrinterDriver(); - FX_BOOL Init(HDC hDC, int ps_level, FX_BOOL bCmykOutput); - ~CPSPrinterDriver() override; - - protected: - // IFX_RenderDeviceDriver - int GetDeviceCaps(int caps_id) override; - FX_BOOL IsPSPrintDriver() override { return TRUE; } - FX_BOOL StartRendering() override; - void EndRendering() override; - void SaveState() override; - void RestoreState(bool bKeepSaved) override; - FX_BOOL SetClip_PathFill(const CFX_PathData* pPathData, - const CFX_Matrix* pObject2Device, - int fill_mode) override; - FX_BOOL SetClip_PathStroke(const CFX_PathData* pPathData, - const CFX_Matrix* pObject2Device, - const CFX_GraphStateData* pGraphState) override; - FX_BOOL DrawPath(const CFX_PathData* pPathData, - const CFX_Matrix* pObject2Device, - const CFX_GraphStateData* pGraphState, - uint32_t fill_color, - uint32_t stroke_color, - int fill_mode, - int alpha_flag, - void* pIccTransform, - int blend_type) override; - FX_BOOL GetClipBox(FX_RECT* pRect) override; - FX_BOOL SetDIBits(const CFX_DIBSource* pBitmap, - uint32_t color, - const FX_RECT* pSrcRect, - int left, - int top, - int blend_type, - int alpha_flag, - void* pIccTransform) override; - FX_BOOL StretchDIBits(const CFX_DIBSource* pBitmap, - uint32_t color, - int dest_left, - int dest_top, - int dest_width, - int dest_height, - const FX_RECT* pClipRect, - uint32_t flags, - int alpha_flag, - void* pIccTransform, - int blend_type) override; - FX_BOOL StartDIBits(const CFX_DIBSource* pBitmap, - int bitmap_alpha, - uint32_t color, - const CFX_Matrix* pMatrix, - uint32_t render_flags, - void*& handle, - int alpha_flag, - void* pIccTransform, - int blend_type) override; - FX_BOOL DrawDeviceText(int nChars, - const FXTEXT_CHARPOS* pCharPos, - CFX_Font* pFont, - CFX_FontCache* pCache, - const CFX_Matrix* pObject2Device, - FX_FLOAT font_size, - uint32_t color, - int alpha_flag, - void* pIccTransform) override; - void* GetPlatformSurface() const override { return (void*)m_hDC; } - - HDC m_hDC; - FX_BOOL m_bCmykOutput; - int m_Width, m_Height, m_nBitsPerPixel; - int m_HorzSize, m_VertSize; - CPSOutput* m_pPSOutput; - CFX_PSRenderer m_PSRenderer; -}; - #endif // CORE_FXGE_WIN32_WIN32_INT_H_ diff --git a/pdfium.gyp b/pdfium.gyp index ae30a3c1e5..d561b9de41 100644 --- a/pdfium.gyp +++ b/pdfium.gyp @@ -709,7 +709,6 @@ 'core/fxge/ge/fx_ge_fontmap.cpp', 'core/fxge/ge/fx_ge_linux.cpp', 'core/fxge/ge/fx_ge_path.cpp', - 'core/fxge/ge/fx_ge_ps.cpp', 'core/fxge/ge/fx_ge_text.cpp', 'core/fxge/ge/fx_text_int.h', 'core/fxge/include/fx_dib.h', -- cgit v1.2.3