dwrote 0.11.5

Lightweight binding to DirectWrite.
Documentation
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::cell::UnsafeCell;
use std::mem::{self, zeroed};
use std::slice;
use std::{error, fmt, ptr};
use winapi::ctypes::c_void;
use winapi::shared::minwindef::{BOOL, FALSE, TRUE};
use winapi::shared::winerror::S_OK;
use winapi::um::dcommon::DWRITE_MEASURING_MODE;
use winapi::um::dwrite::IDWriteRenderingParams;
use winapi::um::dwrite::DWRITE_FONT_FACE_TYPE_TRUETYPE;
use winapi::um::dwrite::{IDWriteFontFace, IDWriteFontFile};
use winapi::um::dwrite::{DWRITE_FONT_FACE_TYPE_BITMAP, DWRITE_FONT_FACE_TYPE_CFF};
use winapi::um::dwrite::{DWRITE_FONT_FACE_TYPE_RAW_CFF, DWRITE_FONT_FACE_TYPE_TYPE1};
use winapi::um::dwrite::{DWRITE_FONT_FACE_TYPE_TRUETYPE_COLLECTION, DWRITE_FONT_FACE_TYPE_VECTOR};
use winapi::um::dwrite::{DWRITE_FONT_SIMULATIONS, DWRITE_GLYPH_METRICS};
use winapi::um::dwrite::{DWRITE_GLYPH_OFFSET, DWRITE_MATRIX, DWRITE_RENDERING_MODE};
use winapi::um::dwrite::{DWRITE_RENDERING_MODE_DEFAULT, DWRITE_RENDERING_MODE_NATURAL_SYMMETRIC};
use winapi::um::dwrite_1::IDWriteFontFace1;
use winapi::um::dwrite_3::{
    IDWriteFontFace5, IDWriteFontResource, DWRITE_FONT_AXIS_ATTRIBUTES_VARIABLE,
    DWRITE_FONT_AXIS_VALUE,
};
use winapi::um::winnt::HRESULT;
use winapi::Interface;
use wio::com::ComPtr;

use super::{DWriteFactory, DefaultDWriteRenderParams, FontFile, FontMetrics};
use crate::com_helpers::Com;
use crate::geometry_sink_impl::GeometrySinkImpl;
use crate::outline_builder::OutlineBuilder;
use crate::FontSimulations;

pub struct FontFace {
    native: UnsafeCell<ComPtr<IDWriteFontFace>>,
    face1: UnsafeCell<Option<ComPtr<IDWriteFontFace1>>>,
    face5: UnsafeCell<Option<ComPtr<IDWriteFontFace5>>>,
}

impl FontFace {
    pub fn take(native: ComPtr<IDWriteFontFace>) -> FontFace {
        let cell = UnsafeCell::new(native);
        FontFace {
            native: cell,
            face1: UnsafeCell::new(None),
            face5: UnsafeCell::new(None),
        }
    }

    pub unsafe fn as_ptr(&self) -> *mut IDWriteFontFace {
        (*self.native.get()).as_raw()
    }

    unsafe fn raw_files(&self) -> Result<Vec<*mut IDWriteFontFile>, HRESULT> {
        let mut number_of_files: u32 = 0;
        let hr = (*self.native.get()).GetFiles(&mut number_of_files, ptr::null_mut());
        if hr != S_OK {
            return Err(hr);
        }

        let mut file_ptrs: Vec<*mut IDWriteFontFile> =
            vec![ptr::null_mut(); number_of_files as usize];
        let hr = (*self.native.get()).GetFiles(&mut number_of_files, file_ptrs.as_mut_ptr());
        if hr != S_OK {
            return Err(hr);
        }
        Ok(file_ptrs)
    }

    #[deprecated(note = "Use `files` instead.")]
    pub fn get_files(&self) -> Vec<FontFile> {
        self.files().unwrap()
    }

    pub fn files(&self) -> Result<Vec<FontFile>, HRESULT> {
        unsafe {
            self.raw_files().map(|file_ptrs| {
                file_ptrs
                    .iter()
                    .map(|p| FontFile::take(ComPtr::from_raw(*p)))
                    .collect()
            })
        }
    }

    pub fn create_font_face_with_simulations(
        &self,
        simulations: DWRITE_FONT_SIMULATIONS,
    ) -> FontFace {
        unsafe {
            let file_ptrs = self.raw_files().unwrap();
            let face_type = (*self.native.get()).GetType();
            let face_index = (*self.native.get()).GetIndex();
            let mut face: *mut IDWriteFontFace = ptr::null_mut();
            let hr = (*DWriteFactory()).CreateFontFace(
                face_type,
                file_ptrs.len() as u32,
                file_ptrs.as_ptr(),
                face_index,
                simulations,
                &mut face,
            );
            for p in file_ptrs {
                let _ = ComPtr::<IDWriteFontFile>::from_raw(p);
            }
            assert!(hr == 0);
            FontFace::take(ComPtr::from_raw(face))
        }
    }

    pub fn get_glyph_count(&self) -> u16 {
        unsafe { (*self.native.get()).GetGlyphCount() }
    }

    pub fn metrics(&self) -> FontMetrics {
        unsafe {
            let font_1 = self.get_face1();
            match font_1 {
                None => {
                    let mut metrics = mem::zeroed();
                    (*self.native.get()).GetMetrics(&mut metrics);
                    FontMetrics::Metrics0(metrics)
                }
                Some(font_1) => {
                    let mut metrics_1 = mem::zeroed();
                    font_1.GetMetrics(&mut metrics_1);
                    FontMetrics::Metrics1(metrics_1)
                }
            }
        }
    }

    #[deprecated(note = "Use `glyph_indices` instead.")]
    pub fn get_glyph_indices(&self, code_points: &[u32]) -> Vec<u16> {
        self.glyph_indices(code_points).unwrap()
    }

    pub fn glyph_indices(&self, code_points: &[u32]) -> Result<Vec<u16>, HRESULT> {
        let mut glyph_indices: Vec<u16> = vec![0; code_points.len()];
        unsafe {
            let hr = (*self.native.get()).GetGlyphIndices(
                code_points.as_ptr(),
                code_points.len() as u32,
                glyph_indices.as_mut_ptr(),
            );
            if hr != S_OK {
                return Err(hr);
            }
            Ok(glyph_indices)
        }
    }

    #[deprecated(note = "Use `design_glyph_metrics` instead.")]
    pub fn get_design_glyph_metrics(
        &self,
        glyph_indices: &[u16],
        is_sideways: bool,
    ) -> Vec<DWRITE_GLYPH_METRICS> {
        self.design_glyph_metrics(glyph_indices, is_sideways)
            .unwrap()
    }

    pub fn design_glyph_metrics(
        &self,
        glyph_indices: &[u16],
        is_sideways: bool,
    ) -> Result<Vec<DWRITE_GLYPH_METRICS>, HRESULT> {
        unsafe {
            let mut metrics: Vec<DWRITE_GLYPH_METRICS> = vec![zeroed(); glyph_indices.len()];
            let hr = (*self.native.get()).GetDesignGlyphMetrics(
                glyph_indices.as_ptr(),
                glyph_indices.len() as u32,
                metrics.as_mut_ptr(),
                is_sideways as BOOL,
            );
            if hr != S_OK {
                return Err(hr);
            }
            Ok(metrics)
        }
    }

    #[deprecated(note = "Use `gdi_compatible_glyph_metrics` instead.")]
    pub fn get_gdi_compatible_glyph_metrics(
        &self,
        em_size: f32,
        pixels_per_dip: f32,
        transform: *const DWRITE_MATRIX,
        use_gdi_natural: bool,
        glyph_indices: &[u16],
        is_sideways: bool,
    ) -> Vec<DWRITE_GLYPH_METRICS> {
        self.gdi_compatible_glyph_metrics(
            em_size,
            pixels_per_dip,
            transform,
            use_gdi_natural,
            glyph_indices,
            is_sideways,
        )
        .unwrap()
    }

    pub fn gdi_compatible_glyph_metrics(
        &self,
        em_size: f32,
        pixels_per_dip: f32,
        transform: *const DWRITE_MATRIX,
        use_gdi_natural: bool,
        glyph_indices: &[u16],
        is_sideways: bool,
    ) -> Result<Vec<DWRITE_GLYPH_METRICS>, HRESULT> {
        unsafe {
            let mut metrics: Vec<DWRITE_GLYPH_METRICS> = vec![zeroed(); glyph_indices.len()];
            let hr = (*self.native.get()).GetGdiCompatibleGlyphMetrics(
                em_size,
                pixels_per_dip,
                transform,
                use_gdi_natural as BOOL,
                glyph_indices.as_ptr(),
                glyph_indices.len() as u32,
                metrics.as_mut_ptr(),
                is_sideways as BOOL,
            );
            if hr != S_OK {
                return Err(hr);
            }
            Ok(metrics)
        }
    }

    #[deprecated(note = "Use `font_table` instead.")]
    pub fn get_font_table(&self, opentype_table_tag: u32) -> Option<Vec<u8>> {
        self.font_table(opentype_table_tag).unwrap()
    }

    /// Returns the contents of the OpenType table with the given tag.
    ///
    /// NB: The bytes of the tag are reversed! You probably want to use the `u32::swap_bytes()`
    /// method on the tag value before calling this method.
    pub fn font_table(&self, opentype_table_tag: u32) -> Result<Option<Vec<u8>>, HRESULT> {
        let mut table_data_ptr: *const u8 = ptr::null_mut();
        let mut table_size: u32 = 0;
        let mut table_context: *mut c_void = ptr::null_mut();
        let mut exists: BOOL = FALSE;
        unsafe {
            let hr = (*self.native.get()).TryGetFontTable(
                opentype_table_tag,
                &mut table_data_ptr as *mut *const _ as *mut *const c_void,
                &mut table_size,
                &mut table_context,
                &mut exists,
            );
            if hr != S_OK {
                return Err(hr);
            }

            if exists == FALSE {
                return Ok(None);
            }

            let table_bytes = slice::from_raw_parts(table_data_ptr, table_size as usize).to_vec();

            (*self.native.get()).ReleaseFontTable(table_context);

            Ok(Some(table_bytes))
        }
    }

    pub fn get_recommended_rendering_mode(
        &self,
        em_size: f32,
        pixels_per_dip: f32,
        measure_mode: DWRITE_MEASURING_MODE,
        rendering_params: *mut IDWriteRenderingParams,
    ) -> DWRITE_RENDERING_MODE {
        unsafe {
            let mut render_mode: DWRITE_RENDERING_MODE = DWRITE_RENDERING_MODE_DEFAULT;
            let hr = (*self.native.get()).GetRecommendedRenderingMode(
                em_size,
                pixels_per_dip,
                measure_mode,
                rendering_params,
                &mut render_mode,
            );

            if hr != S_OK {
                return DWRITE_RENDERING_MODE_NATURAL_SYMMETRIC;
            }

            render_mode
        }
    }

    pub fn get_recommended_rendering_mode_default_params(
        &self,
        em_size: f32,
        pixels_per_dip: f32,
        measure_mode: DWRITE_MEASURING_MODE,
    ) -> DWRITE_RENDERING_MODE {
        self.get_recommended_rendering_mode(
            em_size,
            pixels_per_dip,
            measure_mode,
            DefaultDWriteRenderParams(),
        )
    }

    #[deprecated(note = "Use `glyph_run_outline` instead.")]
    pub fn get_glyph_run_outline(
        &self,
        em_size: f32,
        glyph_indices: &[u16],
        glyph_advances: Option<&[f32]>,
        glyph_offsets: Option<&[DWRITE_GLYPH_OFFSET]>,
        is_sideways: bool,
        is_right_to_left: bool,
        outline_builder: Box<dyn OutlineBuilder>,
    ) {
        self.glyph_run_outline(
            em_size,
            glyph_indices,
            glyph_advances,
            glyph_offsets,
            is_sideways,
            is_right_to_left,
            outline_builder,
        )
        .unwrap()
    }

    pub fn glyph_run_outline(
        &self,
        em_size: f32,
        glyph_indices: &[u16],
        glyph_advances: Option<&[f32]>,
        glyph_offsets: Option<&[DWRITE_GLYPH_OFFSET]>,
        is_sideways: bool,
        is_right_to_left: bool,
        outline_builder: Box<dyn OutlineBuilder>,
    ) -> Result<(), GlyphRunOutlineError> {
        let glyph_advances = match glyph_advances {
            None => ptr::null(),
            Some(glyph_advances) => {
                if glyph_advances.len() != glyph_indices.len() {
                    return Err(GlyphRunOutlineError::InvalidInput);
                }
                glyph_advances.as_ptr()
            }
        };
        let glyph_offsets = match glyph_offsets {
            None => ptr::null(),
            Some(glyph_offsets) => {
                if glyph_offsets.len() != glyph_indices.len() {
                    return Err(GlyphRunOutlineError::InvalidInput);
                }
                glyph_offsets.as_ptr()
            }
        };
        let is_sideways = if is_sideways { TRUE } else { FALSE };
        let is_right_to_left = if is_right_to_left { TRUE } else { FALSE };
        let geometry_sink = GeometrySinkImpl::new(outline_builder);
        let geometry_sink = geometry_sink.into_interface();
        unsafe {
            let hr = (*self.native.get()).GetGlyphRunOutline(
                em_size,
                glyph_indices.as_ptr(),
                glyph_advances,
                glyph_offsets,
                glyph_indices.len() as u32,
                is_sideways,
                is_right_to_left,
                geometry_sink,
            );
            if hr != S_OK {
                return Err(GlyphRunOutlineError::Win32Error(hr));
            }
        }
        Ok(())
    }

    pub fn has_kerning_pairs(&self) -> bool {
        unsafe {
            match self.get_face1() {
                Some(face1) => face1.HasKerningPairs() == TRUE,
                None => false,
            }
        }
    }

    #[deprecated(note = "Use `glyph_pair_kerning_adjustment` instead.")]
    pub fn get_glyph_pair_kerning_adjustment(&self, first_glyph: u16, second_glyph: u16) -> i32 {
        self.glyph_pair_kerning_adjustment(first_glyph, second_glyph)
            .unwrap()
    }

    pub fn glyph_pair_kerning_adjustment(
        &self,
        first_glyph: u16,
        second_glyph: u16,
    ) -> Result<i32, HRESULT> {
        unsafe {
            match self.get_face1() {
                Some(face1) => {
                    let mut adjustments = [0; 2];
                    let hr = face1.GetKerningPairAdjustments(
                        2,
                        [first_glyph, second_glyph].as_ptr(),
                        adjustments.as_mut_ptr(),
                    );
                    if hr != S_OK {
                        return Err(hr);
                    }

                    Ok(adjustments[0])
                }
                None => Ok(0),
            }
        }
    }

    #[inline]
    pub fn get_type(&self) -> FontFaceType {
        unsafe {
            match (*self.native.get()).GetType() {
                DWRITE_FONT_FACE_TYPE_CFF => FontFaceType::Cff,
                DWRITE_FONT_FACE_TYPE_RAW_CFF => FontFaceType::RawCff,
                DWRITE_FONT_FACE_TYPE_TRUETYPE => FontFaceType::TrueType,
                DWRITE_FONT_FACE_TYPE_TRUETYPE_COLLECTION => FontFaceType::TrueTypeCollection,
                DWRITE_FONT_FACE_TYPE_TYPE1 => FontFaceType::Type1,
                DWRITE_FONT_FACE_TYPE_VECTOR => FontFaceType::Vector,
                DWRITE_FONT_FACE_TYPE_BITMAP => FontFaceType::Bitmap,
                _ => FontFaceType::Unknown,
            }
        }
    }

    #[inline]
    pub fn get_index(&self) -> u32 {
        unsafe { (*self.native.get()).GetIndex() }
    }

    #[inline]
    unsafe fn get_face1(&self) -> Option<ComPtr<IDWriteFontFace1>> {
        self.get_interface(&self.face1)
    }

    #[inline]
    unsafe fn get_face5(&self) -> Option<ComPtr<IDWriteFontFace5>> {
        self.get_interface(&self.face5)
    }

    #[inline]
    unsafe fn get_interface<I: Interface>(
        &self,
        interface: &UnsafeCell<Option<ComPtr<I>>>,
    ) -> Option<ComPtr<I>> {
        if (*interface.get()).is_none() {
            *interface.get() = (*self.native.get()).cast().ok()
        }
        (*interface.get()).clone()
    }

    pub fn has_variations(&self) -> bool {
        unsafe {
            match self.get_face5() {
                Some(face5) => face5.HasVariations() == TRUE,
                None => false,
            }
        }
    }

    /// If this font has variations, return a [`Vec<DWRITE_FONT_AXIS_VALUE`] of the
    /// variation axes and their values. If the font does not have variations,
    /// return an empty `Vec`.
    pub fn variations(&self) -> Result<Vec<DWRITE_FONT_AXIS_VALUE>, HRESULT> {
        let face5 = unsafe { self.get_face5() };
        let Some(face5) = face5 else {
            return Ok(vec![]);
        };
        if unsafe { face5.HasVariations() != TRUE } {
            return Ok(vec![]);
        }
        let axis_count = unsafe { face5.GetFontAxisValueCount() as usize };
        if axis_count == 0 {
            return Ok(vec![]);
        }

        let mut resource: *mut IDWriteFontResource = ptr::null_mut();
        let hr = unsafe { face5.GetFontResource(&mut resource) };
        if hr != S_OK || resource.is_null() {
            return Err(hr);
        }

        let mut axis_values = Vec::with_capacity(axis_count);
        axis_values.resize(
            axis_count,
            DWRITE_FONT_AXIS_VALUE {
                axisTag: 0,
                value: 0.,
            },
        );

        let hr = unsafe { face5.GetFontAxisValues(axis_values.as_mut_ptr(), axis_count as u32) };
        if hr != S_OK {
            return Err(hr);
        }

        let resource = unsafe { &*resource };
        Ok(axis_values
            .iter()
            .enumerate()
            .filter_map(|(index, axis_value)| {
                let attributes = unsafe { resource.GetFontAxisAttributes(index as u32) };
                if attributes & DWRITE_FONT_AXIS_ATTRIBUTES_VARIABLE == 0 {
                    None
                } else {
                    Some(*axis_value)
                }
            })
            .collect())
    }

    pub fn create_font_face_with_variations(
        &self,
        simulations: DWRITE_FONT_SIMULATIONS,
        axis_values: &[DWRITE_FONT_AXIS_VALUE],
    ) -> Option<FontFace> {
        unsafe {
            if let Some(face5) = self.get_face5() {
                let mut resource: *mut IDWriteFontResource = ptr::null_mut();
                let hr = face5.GetFontResource(&mut resource);
                if hr == S_OK && !resource.is_null() {
                    let resource = ComPtr::from_raw(resource);
                    let mut var_face: *mut IDWriteFontFace5 = ptr::null_mut();
                    let hr = resource.CreateFontFace(
                        simulations,
                        axis_values.as_ptr(),
                        axis_values.len() as u32,
                        &mut var_face,
                    );
                    if hr == S_OK && !var_face.is_null() {
                        let var_face = ComPtr::from_raw(var_face).cast().unwrap();
                        return Some(FontFace::take(var_face));
                    }
                }
            }
            None
        }
    }

    pub fn simulations(&self) -> FontSimulations {
        unsafe {
            std::mem::transmute::<DWRITE_FONT_SIMULATIONS, FontSimulations>(
                (*self.native.get()).GetSimulations(),
            )
        }
    }
}

impl Clone for FontFace {
    fn clone(&self) -> FontFace {
        unsafe {
            FontFace {
                native: UnsafeCell::new((*self.native.get()).clone()),
                face1: UnsafeCell::new(None),
                face5: UnsafeCell::new(None),
            }
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum FontFaceType {
    Unknown,
    Cff,
    RawCff,
    TrueType,
    TrueTypeCollection,
    Type1,
    Vector,
    Bitmap,
}

#[derive(Debug)]
pub enum GlyphRunOutlineError {
    InvalidInput,
    Win32Error(HRESULT),
}

impl fmt::Display for GlyphRunOutlineError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidInput => write!(f, "Invalid input"),
            Self::Win32Error(code) => write!(f, "{:#x}", code),
        }
    }
}

impl error::Error for GlyphRunOutlineError {}