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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
use crate::*;
use std::sync::Arc;
use windows::core::ComInterface;
use windows::Win32::Graphics::{
    Direct2D::Common::*, Direct2D::*, DirectWrite::*, Imaging::D2D::*, Imaging::*,
};
use windows::Win32::System::Com::{CoCreateInstance, CLSCTX_INPROC_SERVER};

pub trait Fill {
    fn fill(&self, dc: &ID2D1DeviceContext5, brush: &ID2D1Brush);
}

pub trait Stroke {
    fn stroke(
        &self,
        dc: &ID2D1DeviceContext5,
        brush: &ID2D1Brush,
        width: f32,
        style: Option<&ID2D1StrokeStyle>,
    );
}

pub struct DrawCommand {
    dc: ID2D1DeviceContext5,
}

impl DrawCommand {
    #[inline]
    pub fn clear(&self, color: impl Into<Rgba<f32>>) {
        unsafe {
            let color = Wrapper(color.into()).into();
            self.dc.Clear(Some(&color));
        }
    }

    #[inline]
    pub fn fill(&self, object: &impl Fill, brush: &Brush) {
        object.fill(&self.dc, &brush.handle());
    }

    #[inline]
    pub fn stroke(
        &self,
        object: &impl Stroke,
        brush: &Brush,
        width: f32,
        stroke_style: Option<&StrokeStyle>,
    ) {
        object.stroke(
            &self.dc,
            &brush.handle(),
            width,
            stroke_style.map(|ss| ss.handle()),
        );
    }

    #[inline]
    pub fn draw_image(
        &self,
        image: &Image,
        dest_rect: impl Into<Rect<f32>>,
        src_rect: Option<Rect<f32>>,
        interpolation: Interpolation,
    ) {
        let dest: D2D_RECT_F = Wrapper(dest_rect.into()).into();
        let src: Option<D2D_RECT_F> = src_rect.map(|src| Wrapper(src).into());
        unsafe {
            self.dc.DrawBitmap2(
                image.handle(),
                Some(&dest),
                1.0,
                D2D1_INTERPOLATION_MODE(interpolation as _),
                src.as_ref().map(|src| src as _),
                None,
            );
        }
    }

    #[inline]
    pub fn push_clip(&self, rect: impl Into<Rect<f32>>) {
        unsafe {
            self.dc.PushAxisAlignedClip(
                &Wrapper(rect.into()).into(),
                D2D1_ANTIALIAS_MODE_PER_PRIMITIVE,
            );
        }
    }

    #[inline]
    pub fn pop_clip(&self) {
        unsafe {
            self.dc.PopAxisAlignedClip();
        }
    }

    #[inline]
    pub fn clip<F, R>(&self, rect: impl Into<Rect<f32>>, f: F) -> R
    where
        F: FnOnce(&Self) -> R,
    {
        self.push_clip(rect);
        let ret = f(self);
        self.pop_clip();
        ret
    }
}

pub trait Target {
    fn bitmap(&self) -> &ID2D1Bitmap1;
    fn size(&self) -> Size<f32>;
    fn physical_size(&self) -> Size<u32>;
}

pub trait Backend {
    type RenderTarget: Target;

    fn d2d1_factory(&self) -> &ID2D1Factory6;
    fn d2d1_device(&self) -> &ID2D1Device5;
    fn begin_draw(&self, target: &Self::RenderTarget);
    fn end_draw(&self, target: &Self::RenderTarget, ret: Result<()>) -> Result<()>;
}

#[derive(Clone, Debug)]
struct FontFileLoader {
    factory: IDWriteFactory6,
    loader: IDWriteInMemoryFontFileLoader,
}

impl Drop for FontFileLoader {
    fn drop(&mut self) {
        unsafe {
            self.factory
                .UnregisterFontFileLoader(&self.loader)
                .unwrap_or(());
        }
    }
}

pub struct LockGuard<'a> {
    multithread: &'a ID2D1Multithread,
}

impl<'a> Drop for LockGuard<'a> {
    #[inline]
    fn drop(&mut self) {
        unsafe {
            self.multithread.Leave();
        }
    }
}

#[derive(Clone, Debug)]
pub struct Factory {
    d2d1_factory: ID2D1Factory6,
    d2d1_device_context: ID2D1DeviceContext5,
    dwrite_factory: IDWriteFactory6,
    wic_imaging_factory: IWICImagingFactory2,
    font_loader: Arc<FontFileLoader>,
    multithread: ID2D1Multithread,
}

impl Factory {
    #[inline]
    pub fn create_solid_color_brush(&self, color: impl Into<Rgba<f32>>) -> Result<Brush> {
        Brush::solid_color(&self.d2d1_device_context, color)
    }

    #[inline]
    pub fn create_gradient_stop_collection<T>(
        &self,
        mode: GradientMode,
        stops: &[T],
    ) -> Result<GradientStopCollection>
    where
        T: Into<GradientStop> + Clone,
    {
        GradientStopCollection::new(&self.d2d1_device_context, mode, stops)
    }

    #[inline]
    pub fn create_linear_gradient_brush(
        &self,
        start: impl Into<Point<f32>>,
        end: impl Into<Point<f32>>,
        stops: &GradientStopCollection,
    ) -> Result<Brush> {
        Brush::linear_gradient(&self.d2d1_device_context, start, end, stops)
    }

    #[inline]
    pub fn create_radial_gradient_brush(
        &self,
        ellipse: impl Into<Ellipse>,
        offset: impl Into<Point<f32>>,
        stops: &GradientStopCollection,
    ) -> Result<Brush> {
        Brush::radial_gradient(&self.d2d1_device_context, ellipse, offset, stops)
    }

    #[inline]
    pub fn create_text_format(
        &self,
        font: Font,
        size: impl Into<f32>,
        style: Option<&TextStyle>,
        locale: Option<&str>,
    ) -> Result<TextFormat> {
        TextFormat::new(
            &self.dwrite_factory,
            &self.font_loader.loader,
            font,
            size.into(),
            style,
            locale.unwrap_or(""),
        )
    }

    #[inline]
    pub fn create_text_layout(
        &self,
        text: impl AsRef<str>,
        format: &TextFormat,
        alignment: TextAlignment,
        size: Option<Size<f32>>,
    ) -> Result<TextLayout> {
        TextLayout::new(&self.dwrite_factory, text.as_ref(), format, alignment, size)
    }

    #[inline]
    pub fn create_stroke_style(&self, props: &StrokeStyleProperties) -> Result<StrokeStyle> {
        StrokeStyle::new(&self.d2d1_factory, props)
    }

    #[inline]
    pub fn create_image_from_file(&self, path: impl AsRef<std::path::Path>) -> Result<Image> {
        Image::from_file(&self.d2d1_device_context, &self.wic_imaging_factory, path)
    }

    #[inline]
    pub fn create_filled_path(&self) -> Result<PathBuilder<FilledPath>> {
        let geometry = unsafe { self.d2d1_factory.CreatePathGeometry()? };
        PathBuilder::new(geometry)
    }

    #[inline]
    pub fn create_hollow_path(&self) -> Result<PathBuilder<HollowPath>> {
        let geometry = unsafe { self.d2d1_factory.CreatePathGeometry()? };
        PathBuilder::new(geometry)
    }

    #[inline]
    pub fn lock(&self) -> LockGuard {
        unsafe {
            self.multithread.Enter();
        }
        LockGuard {
            multithread: &self.multithread,
        }
    }
}

#[derive(Debug)]
pub struct Context<T> {
    pub(crate) backend: T,
    pub(crate) d2d1_device_context: ID2D1DeviceContext5,
    dwrite_factory: IDWriteFactory6,
    wic_imaging_factory: IWICImagingFactory2,
    font_loader: Arc<FontFileLoader>,
}

impl<T> Context<T>
where
    T: Backend,
{
    pub fn new(backend: T) -> Result<Self> {
        unsafe {
            let d2d1_device_context = backend
                .d2d1_device()
                .CreateDeviceContext6(D2D1_DEVICE_CONTEXT_OPTIONS_NONE)?;
            let dwrite_factory: IDWriteFactory6 = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED)?;
            let wic_imaging_factory =
                CoCreateInstance(&CLSID_WICImagingFactory2, None, CLSCTX_INPROC_SERVER)?;
            let font_loader = {
                let loader = dwrite_factory.CreateInMemoryFontFileLoader()?;
                dwrite_factory.RegisterFontFileLoader(&loader)?;
                Arc::new(FontFileLoader {
                    factory: dwrite_factory.clone(),
                    loader,
                })
            };
            Ok(Self {
                backend,
                d2d1_device_context,
                dwrite_factory,
                wic_imaging_factory,
                font_loader,
            })
        }
    }

    #[inline]
    pub fn create_factory(&self) -> Factory {
        Factory {
            d2d1_factory: self.backend.d2d1_factory().clone(),
            d2d1_device_context: self.d2d1_device_context.clone(),
            dwrite_factory: self.dwrite_factory.clone(),
            wic_imaging_factory: self.wic_imaging_factory.clone(),
            font_loader: self.font_loader.clone(),
            multithread: self.backend.d2d1_factory().cast().unwrap(),
        }
    }

    #[inline]
    pub fn set_dpi(&self, dpi: f32) {
        unsafe {
            self.d2d1_device_context.SetDpi(dpi, dpi);
        }
    }

    #[inline]
    pub fn set_scale_factor(&self, scale: f32) {
        self.set_dpi(scale * 96.0)
    }

    #[inline]
    pub fn draw<R>(
        &self,
        target: &T::RenderTarget,
        f: impl FnOnce(&DrawCommand) -> R,
    ) -> Result<R> {
        let ctx = &self.d2d1_device_context;
        unsafe {
            self.backend.begin_draw(target);
            ctx.SetTarget(target.bitmap());
            ctx.BeginDraw();
            let ret = f(&DrawCommand { dc: ctx.clone() });
            let e = ctx.EndDraw(None, None);
            ctx.SetTarget(None);
            self.backend.end_draw(target, e.map_err(|e| e.into()))?;
            Ok(ret)
        }
    }
}