Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
ctext_only_printer_driver.cpp
Go to the documentation of this file.
1// Copyright 2020 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#include "core/fxge/win32/ctext_only_printer_driver.h"
6
7#include <limits.h>
8#include <stddef.h>
9
10#include <algorithm>
11
12#include "core/fxcrt/check_op.h"
13#include "core/fxcrt/compiler_specific.h"
14#include "core/fxcrt/fx_memcpy_wrappers.h"
15#include "core/fxcrt/fx_string.h"
16#include "core/fxcrt/fx_system.h"
17#include "core/fxcrt/notreached.h"
18#include "core/fxge/agg/cfx_agg_imagerenderer.h"
19#include "core/fxge/cfx_font.h"
20#include "core/fxge/dib/cfx_dibbase.h"
21#include "core/fxge/dib/cfx_dibitmap.h"
22#include "core/fxge/text_char_pos.h"
23
24CTextOnlyPrinterDriver::CTextOnlyPrinterDriver(HDC hDC)
25 : m_hDC(hDC),
30 m_OriginY(0.0f),
31 m_SetOrigin(false) {
32 m_nBitsPerPixel = ::GetDeviceCaps(m_hDC, BITSPIXEL);
33}
34
35CTextOnlyPrinterDriver::~CTextOnlyPrinterDriver() = default;
36
37DeviceType CTextOnlyPrinterDriver::GetDeviceType() const {
38 return DeviceType::kPrinter;
39}
40
41int CTextOnlyPrinterDriver::GetDeviceCaps(int caps_id) const {
42 switch (caps_id) {
44 return m_Width;
46 return m_Height;
47 case FXDC_BITS_PIXEL:
48 return m_nBitsPerPixel;
50 return 0;
51 case FXDC_HORZ_SIZE:
52 return m_HorzSize;
53 case FXDC_VERT_SIZE:
54 return m_VertSize;
55 default:
57 }
58}
59
60void CTextOnlyPrinterDriver::SaveState() {}
61
62void CTextOnlyPrinterDriver::RestoreState(bool bKeepSaved) {}
63
64bool CTextOnlyPrinterDriver::SetClip_PathFill(
65 const CFX_Path& path,
66 const CFX_Matrix* pObject2Device,
67 const CFX_FillRenderOptions& fill_options) {
68 return true;
69}
70
71bool CTextOnlyPrinterDriver::SetClip_PathStroke(
72 const CFX_Path& path,
73 const CFX_Matrix* pObject2Device,
74 const CFX_GraphStateData* pGraphState) {
75 return false;
76}
77
78bool CTextOnlyPrinterDriver::DrawPath(
79 const CFX_Path& path,
80 const CFX_Matrix* pObject2Device,
81 const CFX_GraphStateData* pGraphState,
82 uint32_t fill_color,
83 uint32_t stroke_color,
84 const CFX_FillRenderOptions& fill_options) {
85 return false;
86}
87
88bool CTextOnlyPrinterDriver::SetDIBits(RetainPtr<const CFX_DIBBase> bitmap,
89 uint32_t color,
90 const FX_RECT& src_rect,
91 int left,
92 int top,
93 BlendMode blend_type) {
94 return false;
95}
96
97FX_RECT CTextOnlyPrinterDriver::GetClipBox() const {
98 return FX_RECT(0, 0, m_Width, m_Height);
99}
100
101bool CTextOnlyPrinterDriver::StretchDIBits(RetainPtr<const CFX_DIBBase> bitmap,
102 uint32_t color,
103 int dest_left,
104 int dest_top,
105 int dest_width,
106 int dest_height,
107 const FX_RECT* pClipRect,
108 const FXDIB_ResampleOptions& options,
109 BlendMode blend_type) {
110 return false;
111}
112
114 RetainPtr<const CFX_DIBBase> bitmap,
115 float alpha,
116 uint32_t color,
117 const CFX_Matrix& matrix,
118 const FXDIB_ResampleOptions& options,
119 BlendMode blend_type) {
120 return {Result::kNotSupported, nullptr};
121}
122
123bool CTextOnlyPrinterDriver::DrawDeviceText(
124 pdfium::span<const TextCharPos> pCharPos,
125 CFX_Font* pFont,
126 const CFX_Matrix& mtObject2Device,
127 float font_size,
128 uint32_t color,
129 const CFX_TextRenderOptions& /*options*/) {
131 return false;
132 }
133 if (pCharPos.empty() || !pFont) {
134 return false;
135 }
136
137 // Scale factor used to minimize the kerning problems caused by rounding
138 // errors below. Value chosen based on the title of https://crbug.com/18383
139 const double kScaleFactor = 10;
140
141 // Detect new lines and add clrf characters (since this is Windows only).
142 // These characters are removed by SkPDF, but the new line information is
143 // preserved in the text location. clrf characters seem to be ignored by
144 // label printers that use this driver.
145 WideString wsText;
146 size_t len = pCharPos.size();
147 float fOffsetY = mtObject2Device.f * kScaleFactor;
148 if (m_SetOrigin && FXSYS_roundf(m_OriginY) != FXSYS_roundf(fOffsetY)) {
149 wsText += L"\r\n";
150 len += 2;
151 }
152 wsText.Reserve(len);
153 m_OriginY = fOffsetY;
154 m_SetOrigin = true;
155
156 // Text
157 for (const auto& charpos : pCharPos) {
158 // Only works with PDFs from Skia's PDF generator. Cannot handle arbitrary
159 // values from PDFs.
160 DCHECK_EQ(charpos.m_AdjustMatrix[0], 0);
161 DCHECK_EQ(charpos.m_AdjustMatrix[1], 0);
162 DCHECK_EQ(charpos.m_AdjustMatrix[2], 0);
163 DCHECK_EQ(charpos.m_AdjustMatrix[3], 0);
164 DCHECK_EQ(charpos.m_Origin.y, 0);
165 wsText += charpos.m_Unicode;
166 }
167 ByteString text = wsText.ToDefANSI();
168 auto text_span = text.span();
169 while (!text_span.empty()) {
170 uint8_t buffer[1026];
171 size_t send_len = std::min<size_t>(text_span.size(), 1024);
172 *(reinterpret_cast<uint16_t*>(buffer)) = static_cast<uint16_t>(send_len);
173 UNSAFE_TODO(FXSYS_memcpy(buffer + 2, text_span.data(), send_len));
174 ::GdiComment(m_hDC, static_cast<UINT>(send_len + 2), buffer);
175 text_span = text_span.subspan(send_len);
176 }
177 return true;
178}
179
180bool CTextOnlyPrinterDriver::MultiplyAlpha(float alpha) {
181 // Not needed. All callers are using `CFX_DIBitmap`-backed raster devices
182 // anyway.
184}
185
186bool CTextOnlyPrinterDriver::MultiplyAlphaMask(
187 RetainPtr<const CFX_DIBitmap> mask) {
188 // Not needed. All callers are using `CFX_DIBitmap`-backed raster devices
189 // anyway.
191}
fxcrt::ByteString ByteString
Definition bytestring.h:180
WindowsPrintMode g_pdfium_print_mode
#define DCHECK_EQ(x, y)
Definition check_op.h:17
DeviceType GetDeviceType() const override
~CTextOnlyPrinterDriver() override
bool SetClip_PathFill(const CFX_Path &path, const CFX_Matrix *pObject2Device, const CFX_FillRenderOptions &fill_options) override
bool DrawDeviceText(pdfium::span< const TextCharPos > pCharPos, CFX_Font *pFont, const CFX_Matrix &mtObject2Device, float font_size, uint32_t color, const CFX_TextRenderOptions &options) override
void RestoreState(bool bKeepSaved) override
bool MultiplyAlpha(float alpha) override
bool SetClip_PathStroke(const CFX_Path &path, const CFX_Matrix *pObject2Device, const CFX_GraphStateData *pGraphState) override
StartResult StartDIBits(RetainPtr< const CFX_DIBBase > bitmap, float alpha, uint32_t color, const CFX_Matrix &matrix, const FXDIB_ResampleOptions &options, BlendMode blend_type) override
int GetDeviceCaps(int caps_id) const override
bool StretchDIBits(RetainPtr< const CFX_DIBBase > bitmap, uint32_t color, int dest_left, int dest_top, int dest_width, int dest_height, const FX_RECT *pClipRect, const FXDIB_ResampleOptions &options, BlendMode blend_type) override
bool DrawPath(const CFX_Path &path, const CFX_Matrix *pObject2Device, const CFX_GraphStateData *pGraphState, uint32_t fill_color, uint32_t stroke_color, const CFX_FillRenderOptions &fill_options) override
FX_RECT GetClipBox() const override
bool MultiplyAlphaMask(RetainPtr< const CFX_DIBitmap > mask) override
bool SetDIBits(RetainPtr< const CFX_DIBBase > bitmap, uint32_t color, const FX_RECT &src_rect, int left, int top, BlendMode blend_type) override
WideString & operator+=(const wchar_t *str)
ByteString ToDefANSI() const
#define UNSAFE_TODO(...)
BlendMode
Definition fx_dib.h:119
int FXSYS_roundf(float f)
#define NOTREACHED_NORETURN()
Definition notreached.h:22
#define FXDC_BITS_PIXEL
#define FXDC_RENDER_CAPS
#define FXDC_PIXEL_WIDTH
#define FXDC_VERT_SIZE
#define FXDC_PIXEL_HEIGHT
#define FXDC_HORZ_SIZE
constexpr FX_RECT(int l, int t, int r, int b)
fxcrt::WideString WideString
Definition widestring.h:207