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