Add CFX_PSRenderer::WritePreambleString().

This allows CFX_PSRenderer to write font data into a preamble, separate
from the normal output that uses the fonts.

Bug: chromium:1232526
Change-Id: Ie7044a612d6fdbdb82ce623d576b220d0349c847
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/84936
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Hui Yingst <nigi@chromium.org>
diff --git a/core/fxge/win32/cfx_psrenderer.cpp b/core/fxge/win32/cfx_psrenderer.cpp
index dd3b398..dfecb1f 100644
--- a/core/fxge/win32/cfx_psrenderer.cpp
+++ b/core/fxge/win32/cfx_psrenderer.cpp
@@ -105,6 +105,13 @@
   WriteString("\nrestore\n");
   m_bInited = false;
 
+  // Flush `m_PreambleOutput` if it is not empty.
+  std::streampos preamble_pos = m_PreambleOutput.tellp();
+  if (preamble_pos > 0) {
+    m_pStream->WriteBlock(m_PreambleOutput.str().c_str(), preamble_pos);
+    m_PreambleOutput.str(std::string());
+  }
+
   // Flush `m_Output`. It's never empty because of the WriteString() call above.
   m_pStream->WriteBlock(m_Output.str().c_str(), m_Output.tellp());
   m_Output.str(std::string());
@@ -712,6 +719,10 @@
   }
 }
 
+void CFX_PSRenderer::WritePreambleString(ByteStringView str) {
+  m_PreambleOutput << str;
+}
+
 void CFX_PSRenderer::WritePSBinary(pdfium::span<const uint8_t> data) {
   std::unique_ptr<uint8_t, FxFreeDeleter> dest_buf;
   uint32_t dest_size;
diff --git a/core/fxge/win32/cfx_psrenderer.h b/core/fxge/win32/cfx_psrenderer.h
index c59b5ae..091ef82 100644
--- a/core/fxge/win32/cfx_psrenderer.h
+++ b/core/fxge/win32/cfx_psrenderer.h
@@ -141,6 +141,7 @@
                       uint8_t** output_buf,
                       uint32_t* output_size,
                       const char** filter) const;
+  void WritePreambleString(ByteStringView str);
   void WritePSBinary(pdfium::span<const uint8_t> data);
   void WriteStream(std::ostringstream& stream);
   void WriteString(ByteStringView str);
@@ -156,6 +157,7 @@
   UnownedPtr<const EncoderIface> const m_pEncoderIface;
   RetainPtr<IFX_RetainableWriteStream> m_pStream;
   std::vector<std::unique_ptr<Glyph>> m_PSFontList;
+  std::ostringstream m_PreambleOutput;
   std::ostringstream m_Output;
   std::vector<FX_RECT> m_ClipBoxStack;
 };