1 // Copyright 2016 PDFium Authors. All rights reserved.
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 <memory>
11 #include <vector>
12 
13 #include "core/fxcrt/fx_coordinates.h"
14 #include "core/fxcrt/fx_memory_wrappers.h"
15 #include "core/fxcrt/fx_stream.h"
16 #include "core/fxcrt/fx_system.h"
17 #include "core/fxcrt/retain_ptr.h"
18 #include "core/fxge/cfx_graphstatedata.h"
19 
20 class CFX_DIBBase;
21 class CFX_GlyphCache;
22 class CFX_Font;
23 class CFX_PathData;
24 class CPSFont;
25 class TextCharPos;
26 struct FXDIB_ResampleOptions;
27 
28 struct EncoderIface {
29   bool (*pA85EncodeFunc)(pdfium::span<const uint8_t> src_buf,
30                          std::unique_ptr<uint8_t, FxFreeDeleter>* dest_buf,
31                          uint32_t* dest_size);
32   void (*pFaxEncodeFunc)(const uint8_t* src_buf,
33                          int width,
34                          int height,
35                          int pitch,
36                          std::unique_ptr<uint8_t, FxFreeDeleter>* dest_buf,
37                          uint32_t* dest_size);
38   bool (*pFlateEncodeFunc)(const uint8_t* src_buf,
39                            uint32_t src_size,
40                            std::unique_ptr<uint8_t, FxFreeDeleter>* dest_buf,
41                            uint32_t* dest_size);
42   bool (*pJpegEncodeFunc)(const RetainPtr<CFX_DIBBase>& pSource,
43                           uint8_t** dest_buf,
44                           size_t* dest_size);
45   bool (*pRunLengthEncodeFunc)(
46       pdfium::span<const uint8_t> src_buf,
47       std::unique_ptr<uint8_t, FxFreeDeleter>* dest_buf,
48       uint32_t* dest_size);
49 };
50 
51 class CFX_PSRenderer {
52  public:
53   explicit CFX_PSRenderer(const EncoderIface* pEncoderIface);
54   ~CFX_PSRenderer();
55 
56   void Init(const RetainPtr<IFX_RetainableWriteStream>& stream,
57             int pslevel,
58             int width,
59             int height,
60             bool bCmykOutput);
61   bool StartRendering();
62   void EndRendering();
63   void SaveState();
64   void RestoreState(bool bKeepSaved);
65   void SetClip_PathFill(const CFX_PathData* pPathData,
66                         const CFX_Matrix* pObject2Device,
67                         int fill_mode);
68   void SetClip_PathStroke(const CFX_PathData* pPathData,
69                           const CFX_Matrix* pObject2Device,
70                           const CFX_GraphStateData* pGraphState);
GetClipBox()71   FX_RECT GetClipBox() { return m_ClipBox; }
72   bool DrawPath(const CFX_PathData* pPathData,
73                 const CFX_Matrix* pObject2Device,
74                 const CFX_GraphStateData* pGraphState,
75                 uint32_t fill_color,
76                 uint32_t stroke_color,
77                 int fill_mode);
78   bool SetDIBits(const RetainPtr<CFX_DIBBase>& pBitmap,
79                  uint32_t color,
80                  int dest_left,
81                  int dest_top);
82   bool StretchDIBits(const RetainPtr<CFX_DIBBase>& pBitmap,
83                      uint32_t color,
84                      int dest_left,
85                      int dest_top,
86                      int dest_width,
87                      int dest_height,
88                      const FXDIB_ResampleOptions& options);
89   bool DrawDIBits(const RetainPtr<CFX_DIBBase>& pBitmap,
90                   uint32_t color,
91                   const CFX_Matrix& matrix,
92                   const FXDIB_ResampleOptions& options);
93   bool DrawText(int nChars,
94                 const TextCharPos* pCharPos,
95                 CFX_Font* pFont,
96                 const CFX_Matrix& mtObject2Device,
97                 float font_size,
98                 uint32_t color);
99 
100  private:
101   void OutputPath(const CFX_PathData* pPathData,
102                   const CFX_Matrix* pObject2Device);
103   void SetGraphState(const CFX_GraphStateData* pGraphState);
104   void SetColor(uint32_t color);
105   void FindPSFontGlyph(CFX_GlyphCache* pGlyphCache,
106                        CFX_Font* pFont,
107                        const TextCharPos& charpos,
108                        int* ps_fontnum,
109                        int* ps_glyphindex);
110   bool FaxCompressData(std::unique_ptr<uint8_t, FxFreeDeleter> src_buf,
111                        int width,
112                        int height,
113                        std::unique_ptr<uint8_t, FxFreeDeleter>* dest_buf,
114                        uint32_t* dest_size) const;
115   void PSCompressData(uint8_t* src_buf,
116                       uint32_t src_size,
117                       uint8_t** output_buf,
118                       uint32_t* output_size,
119                       const char** filter) const;
120   void WritePSBinary(const uint8_t* data, int len);
121   void WriteToStream(std::ostringstream* stringStream);
122 
123   bool m_bInited = false;
124   bool m_bGraphStateSet = false;
125   bool m_bCmykOutput;
126   bool m_bColorSet = false;
127   int m_PSLevel = 0;
128   uint32_t m_LastColor = 0;
129   FX_RECT m_ClipBox;
130   CFX_GraphStateData m_CurGraphState;
131   const EncoderIface* const m_pEncoderIface;
132   RetainPtr<IFX_RetainableWriteStream> m_pStream;
133   std::vector<std::unique_ptr<CPSFont>> m_PSFontList;
134   std::vector<FX_RECT> m_ClipBoxStack;
135 };
136 
137 #endif  // CORE_FXGE_WIN32_CFX_PSRENDERER_H_
138