xref: /aosp_15_r20/external/pdfium/core/fxge/win32/cpsoutput.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
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 #include "core/fxge/win32/cpsoutput.h"
8 
9 #include <algorithm>
10 
11 #include "core/fxcrt/fx_system.h"
12 #include "third_party/base/containers/span.h"
13 
CPSOutput(HDC hDC,OutputMode mode)14 CPSOutput::CPSOutput(HDC hDC, OutputMode mode) : m_hDC(hDC), m_mode(mode) {}
15 
16 CPSOutput::~CPSOutput() = default;
17 
WriteBlock(pdfium::span<const uint8_t> input)18 bool CPSOutput::WriteBlock(pdfium::span<const uint8_t> input) {
19   while (!input.empty()) {
20     uint8_t buffer[1026];
21     size_t send_len = std::min<size_t>(input.size(), 1024);
22     *(reinterpret_cast<uint16_t*>(buffer)) = static_cast<uint16_t>(send_len);
23     memcpy(buffer + 2, input.data(), send_len);
24     switch (m_mode) {
25       case OutputMode::kExtEscape:
26         ExtEscape(m_hDC, PASSTHROUGH, static_cast<int>(send_len + 2),
27                   reinterpret_cast<const char*>(buffer), 0, nullptr);
28         break;
29       case OutputMode::kGdiComment:
30         GdiComment(m_hDC, static_cast<UINT>(send_len + 2), buffer);
31         break;
32     }
33     input = input.subspan(send_len);
34   }
35   return true;
36 }
37