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