1 // Copyright 2016 The PDFium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 6 7 #ifndef CORE_FXGE_WIN32_CFX_PSRENDERER_H_ 8 #define CORE_FXGE_WIN32_CFX_PSRENDERER_H_ 9 10 #include <stddef.h> 11 #include <stdint.h> 12 13 #include <memory> 14 #include <sstream> 15 #include <vector> 16 17 #include "core/fxcrt/bytestring.h" 18 #include "core/fxcrt/data_vector.h" 19 #include "core/fxcrt/fx_coordinates.h" 20 #include "core/fxcrt/fx_memory_wrappers.h" 21 #include "core/fxcrt/fx_stream.h" 22 #include "core/fxcrt/fx_string_wrappers.h" 23 #include "core/fxcrt/retain_ptr.h" 24 #include "core/fxcrt/unowned_ptr.h" 25 #include "core/fxge/cfx_graphstatedata.h" 26 #include "third_party/abseil-cpp/absl/types/optional.h" 27 #include "third_party/base/containers/span.h" 28 29 class CFX_DIBBase; 30 class CFX_Font; 31 class CFX_GlyphCache; 32 class CFX_PSFontTracker; 33 class CFX_Path; 34 class TextCharPos; 35 struct CFX_FillRenderOptions; 36 struct FXDIB_ResampleOptions; 37 38 struct EncoderIface { 39 DataVector<uint8_t> (*pA85EncodeFunc)(pdfium::span<const uint8_t> src_span); 40 DataVector<uint8_t> (*pFaxEncodeFunc)(RetainPtr<CFX_DIBBase> src); 41 DataVector<uint8_t> (*pFlateEncodeFunc)(pdfium::span<const uint8_t> src_span); 42 bool (*pJpegEncodeFunc)(const RetainPtr<CFX_DIBBase>& pSource, 43 uint8_t** dest_buf, 44 size_t* dest_size); 45 DataVector<uint8_t> (*pRunLengthEncodeFunc)( 46 pdfium::span<const uint8_t> src_span); 47 }; 48 49 class CFX_PSRenderer { 50 public: 51 enum class RenderingLevel { 52 kLevel2, 53 kLevel3, 54 kLevel3Type42, 55 }; 56 57 CFX_PSRenderer(CFX_PSFontTracker* font_tracker, 58 const EncoderIface* encoder_iface); 59 ~CFX_PSRenderer(); 60 61 void Init(const RetainPtr<IFX_RetainableWriteStream>& stream, 62 RenderingLevel level, 63 int width, 64 int height); 65 void SaveState(); 66 void RestoreState(bool bKeepSaved); 67 void SetClip_PathFill(const CFX_Path& path, 68 const CFX_Matrix* pObject2Device, 69 const CFX_FillRenderOptions& fill_options); 70 void SetClip_PathStroke(const CFX_Path& path, 71 const CFX_Matrix* pObject2Device, 72 const CFX_GraphStateData* pGraphState); GetClipBox()73 FX_RECT GetClipBox() { return m_ClipBox; } 74 bool DrawPath(const CFX_Path& path, 75 const CFX_Matrix* pObject2Device, 76 const CFX_GraphStateData* pGraphState, 77 uint32_t fill_color, 78 uint32_t stroke_color, 79 const CFX_FillRenderOptions& fill_options); 80 bool SetDIBits(const RetainPtr<CFX_DIBBase>& pBitmap, 81 uint32_t color, 82 int dest_left, 83 int dest_top); 84 bool StretchDIBits(const RetainPtr<CFX_DIBBase>& pBitmap, 85 uint32_t color, 86 int dest_left, 87 int dest_top, 88 int dest_width, 89 int dest_height, 90 const FXDIB_ResampleOptions& options); 91 bool DrawDIBits(const RetainPtr<CFX_DIBBase>& pBitmap, 92 uint32_t color, 93 const CFX_Matrix& matrix, 94 const FXDIB_ResampleOptions& options); 95 bool DrawText(int nChars, 96 const TextCharPos* pCharPos, 97 CFX_Font* pFont, 98 const CFX_Matrix& mtObject2Device, 99 float font_size, 100 uint32_t color); 101 102 static absl::optional<ByteString> GenerateType42SfntDataForTesting( 103 const ByteString& psname, 104 pdfium::span<const uint8_t> font_data); 105 106 static ByteString GenerateType42FontDictionaryForTesting( 107 const ByteString& psname, 108 const FX_RECT& bbox, 109 size_t num_glyphs, 110 size_t glyphs_per_descendant_font); 111 112 private: 113 struct Glyph; 114 115 struct FaxCompressResult { 116 FaxCompressResult(); 117 FaxCompressResult(const FaxCompressResult&) = delete; 118 FaxCompressResult& operator=(const FaxCompressResult&) = delete; 119 FaxCompressResult(FaxCompressResult&&) noexcept; 120 FaxCompressResult& operator=(FaxCompressResult&&) noexcept; 121 ~FaxCompressResult(); 122 123 DataVector<uint8_t> data; 124 bool compressed = false; 125 }; 126 127 struct PSCompressResult { 128 PSCompressResult(); 129 PSCompressResult(const PSCompressResult&) = delete; 130 PSCompressResult& operator=(const PSCompressResult&) = delete; 131 PSCompressResult(PSCompressResult&&) noexcept; 132 PSCompressResult& operator=(PSCompressResult&&) noexcept; 133 ~PSCompressResult(); 134 135 DataVector<uint8_t> data; 136 ByteString filter; 137 }; 138 139 void StartRendering(); 140 void EndRendering(); 141 void OutputPath(const CFX_Path& path, const CFX_Matrix* pObject2Device); 142 void SetGraphState(const CFX_GraphStateData* pGraphState); 143 void SetColor(uint32_t color); 144 void FindPSFontGlyph(CFX_GlyphCache* pGlyphCache, 145 CFX_Font* pFont, 146 const TextCharPos& charpos, 147 int* ps_fontnum, 148 int* ps_glyphindex); 149 void DrawTextAsType3Font(int char_count, 150 const TextCharPos* char_pos, 151 CFX_Font* font, 152 float font_size, 153 fxcrt::ostringstream& buf); 154 bool DrawTextAsType42Font(int char_count, 155 const TextCharPos* char_pos, 156 CFX_Font* font, 157 float font_size, 158 fxcrt::ostringstream& buf); 159 FaxCompressResult FaxCompressData(RetainPtr<CFX_DIBBase> src) const; 160 absl::optional<PSCompressResult> PSCompressData( 161 pdfium::span<const uint8_t> src_span) const; 162 void WritePreambleString(ByteStringView str); 163 void WritePSBinary(pdfium::span<const uint8_t> data); 164 void WriteStream(fxcrt::ostringstream& stream); 165 void WriteString(ByteStringView str); 166 167 bool m_bInited = false; 168 bool m_bGraphStateSet = false; 169 bool m_bColorSet = false; 170 absl::optional<RenderingLevel> m_Level; 171 uint32_t m_LastColor = 0; 172 FX_RECT m_ClipBox; 173 CFX_GraphStateData m_CurGraphState; 174 UnownedPtr<CFX_PSFontTracker> const m_pFontTracker; 175 UnownedPtr<const EncoderIface> const m_pEncoderIface; 176 RetainPtr<IFX_RetainableWriteStream> m_pStream; 177 std::vector<std::unique_ptr<Glyph>> m_PSFontList; 178 fxcrt::ostringstream m_PreambleOutput; 179 fxcrt::ostringstream m_Output; 180 std::vector<FX_RECT> m_ClipBoxStack; 181 }; 182 183 #endif // CORE_FXGE_WIN32_CFX_PSRENDERER_H_ 184