// Copyright 2016 The PDFium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#include "core/fxge/win32/cpsoutput.h"

#include <algorithm>

#include "core/fxcrt/fx_system.h"
#include "core/fxcrt/span.h"

CPSOutput::CPSOutput(HDC hDC, OutputMode mode) : dc_handle_(hDC), mode_(mode) {}

CPSOutput::~CPSOutput() = default;

bool CPSOutput::WriteBlock(pdfium::span<const uint8_t> input) {
  uint8_t buffer[1026];
  auto [header_span, buffer_span] = pdfium::span(buffer).split_at<2>();
  auto header_span16 = fxcrt::reinterpret_span<uint16_t>(header_span);

  while (!input.empty()) {
    size_t send_len = std::min<size_t>(input.size(), 1024);
    header_span16[0] = static_cast<uint16_t>(send_len);
    buffer_span.copy_prefix_from(input.first(send_len));
    switch (mode_) {
      case OutputMode::kExtEscape:
        ExtEscape(dc_handle_, PASSTHROUGH, static_cast<int>(send_len + 2),
                  reinterpret_cast<const char*>(buffer), 0, nullptr);
        break;
      case OutputMode::kGdiComment:
        GdiComment(dc_handle_, static_cast<UINT>(send_len + 2), buffer);
        break;
    }
    input = input.subspan(send_len);
  }
  return true;
}
