1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use crate::error::DWResult;
use crate::pixel_snapping::PixelSnapping;
use crate::text_renderer::custom::{
DrawGlyphRun, DrawInlineObject, DrawStrikethrough, DrawUnderline,
};
use com_wrapper::ComWrapper;
use dcommon::helpers::deref_com_wrapper;
use dcommon::helpers::unwrap_opt_com;
use winapi::ctypes::c_void;
use winapi::shared::winerror::SUCCEEDED;
use winapi::um::dwrite::IDWriteTextRenderer;
use wio::com::ComPtr;
pub mod custom;
#[repr(transparent)]
#[derive(ComWrapper)]
#[com(send)]
pub struct TextRenderer {
ptr: ComPtr<IDWriteTextRenderer>,
}
impl TextRenderer {
pub fn new(renderer: impl custom::CustomTextRenderer) -> TextRenderer {
custom::com_renderer::ComRenderer::new(renderer)
}
pub fn draw_glyph_run(&mut self, context: &DrawGlyphRun) -> DWResult<()> {
unsafe {
let hr = self.ptr.DrawGlyphRun(
context.context.ptr(),
context.baseline_origin.x,
context.baseline_origin.y,
context.measuring_mode.value,
&context.glyph_run.into_raw(),
&context.glyph_run_desc.into_raw(),
unwrap_opt_com(context.client_effect),
);
if SUCCEEDED(hr) {
Ok(())
} else {
Err(hr.into())
}
}
}
pub fn draw_underline(&mut self, context: &DrawUnderline) -> DWResult<()> {
unsafe {
let hr = self.ptr.DrawUnderline(
context.context.ptr(),
context.baseline_origin.x,
context.baseline_origin.y,
&context.underline.into_raw(),
unwrap_opt_com(context.client_effect),
);
if SUCCEEDED(hr) {
Ok(())
} else {
Err(hr.into())
}
}
}
pub fn draw_strikethrough(&mut self, context: &DrawStrikethrough) -> DWResult<()> {
unsafe {
let hr = self.ptr.DrawStrikethrough(
context.context.ptr(),
context.baseline_origin.x,
context.baseline_origin.y,
&context.strikethrough.into_raw(),
unwrap_opt_com(context.client_effect),
);
if SUCCEEDED(hr) {
Ok(())
} else {
Err(hr.into())
}
}
}
pub fn draw_inline_object(&mut self, context: &DrawInlineObject) -> DWResult<()> {
unsafe {
let hr = self.ptr.DrawInlineObject(
context.context.ptr(),
context.origin.x,
context.origin.y,
context.inline_object.get_raw(),
context.is_sideways as i32,
context.is_right_to_left as i32,
unwrap_opt_com(context.client_effect),
);
if SUCCEEDED(hr) {
Ok(())
} else {
Err(hr.into())
}
}
}
}
impl std::ops::Deref for TextRenderer {
type Target = PixelSnapping;
fn deref(&self) -> &PixelSnapping {
unsafe { deref_com_wrapper(self) }
}
}
#[derive(Copy, Clone)]
pub struct DrawContext(usize);
impl DrawContext {
pub unsafe fn from_usize(value: usize) -> Self {
DrawContext(value)
}
pub unsafe fn from_ptr(value: *mut c_void) -> Self {
DrawContext(value as usize)
}
pub fn value(&self) -> usize {
self.0
}
pub unsafe fn ptr(&self) -> *mut c_void {
self.0 as *mut c_void
}
}
impl std::fmt::Debug for DrawContext {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
fmt.debug_tuple("DrawContext")
.field(&format_args!("{:x}", self.0))
.finish()
}
}