directwrite 0.1.4

A safe abstraction for interacting with DirectWrite, intended initially to be used with direct2d for easy text rendering.
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
use drawing_effect::DrawingEffect;
use enums::{FontStretch, FontStyle, FontWeight};
use error::DWResult;
use factory::Factory;
use font_collection::FontCollection;
use helpers::InternalConstructor;
use inline_object::IntoInlineObject;
use text_format::TextFormat;
use text_renderer::{Context, TextRenderer, TextRendererComRef};

use std::{mem, ops, ptr, u32};

use winapi::shared::winerror::{SUCCEEDED, S_OK};
use winapi::um::dwrite::*;
use wio::com::ComPtr;
use wio::wide::ToWide;

pub use self::builder::TextLayoutBuilder;

pub mod builder;
pub mod metrics;

/// The TextLayout interface represents a block of text after it has been fully analyzed and formatted.
#[repr(C)]
#[derive(Clone, PartialEq)]
pub struct TextLayout {
    ptr: ComPtr<IDWriteTextLayout>,
}

impl TextLayout {
    pub fn create<'a>(factory: &'a Factory) -> TextLayoutBuilder<'a> {
        unsafe { TextLayoutBuilder::new(&*factory.get_raw()) }
    }

    pub fn as_format(&self) -> TextFormat {
        unsafe { TextFormat::from_raw(self.ptr.clone().up().into_raw()) }
    }

    /// Determines the minimum possible width the layout can be set to without emergency breaking
    /// between the characters of whole words occurring.
    pub fn determine_min_width(&self) -> f32 {
        unsafe {
            let mut value = 0.0;
            self.ptr.DetermineMinWidth(&mut value);
            value
        }
    }

    pub fn draw(
        &self,
        renderer: &mut TextRenderer,
        origin_x: f32,
        origin_y: f32,
        context: Context,
    ) -> DWResult<()> {
        unsafe {
            let mut renderer = TextRendererComRef::new(renderer);

            let hr = self
                .ptr
                .Draw(context.0, renderer.as_raw(), origin_x, origin_y);
            if SUCCEEDED(hr) {
                Ok(())
            } else {
                Err(hr.into())
            }
        }
    }

    /// Gets the number of ClusterMetrics objects which exist for this TextLayout
    pub fn get_cluster_metrics_count(&self) -> usize {
        unsafe {
            let mut count = 0;
            self.ptr.GetClusterMetrics(ptr::null_mut(), 0, &mut count);
            count as usize
        }
    }

    /// Retrieves the ClusterMetrics for the glyph clusters in this layout. You should ensure the
    /// slice is large enough to hold all of the metrics, which can be obtained by calling
    /// `get_cluster_metrics_count`. If the slice is not large enough, it will return
    /// Err(actual_count), otherwise returns Ok(actual_count).
    pub fn get_cluster_metrics_slice(
        &self,
        buf: &mut [metrics::ClusterMetrics],
    ) -> Result<usize, usize> {
        assert!(buf.len() <= u32::MAX as usize);
        unsafe {
            let mut actual_count = 0;
            let buf_ptr = buf.as_mut_ptr() as *mut DWRITE_CLUSTER_METRICS;
            let res = self
                .ptr
                .GetClusterMetrics(buf_ptr, buf.len() as u32, &mut actual_count);

            if res == S_OK {
                Ok(actual_count as usize)
            } else {
                Err(actual_count as usize)
            }
        }
    }

    /// Fill all of the Cluster metrics into a Vec. This function will resize the Vec to fit all
    /// of the metrics structures exactly.
    pub fn get_cluster_metrics(&self, buf: &mut Vec<metrics::ClusterMetrics>) {
        let count = self.get_cluster_metrics_count();
        buf.resize(count, Default::default());
        assert_eq!(self.get_cluster_metrics_slice(buf), Ok(count));
    }

    /// Gets the font collection of the text at the specified position. Also returns the text range
    /// which has identical formatting to the current character.
    pub fn get_font_collection(&self, position: u32) -> DWResult<(FontCollection, TextRange)> {
        unsafe {
            let mut collection = ptr::null_mut();
            let mut range = mem::uninitialized();
            let res = self
                .ptr
                .GetFontCollection(position, &mut collection, &mut range);
            if res < 0 {
                return Err(res.into());
            }
            Ok((FontCollection::from_raw(collection), range.into()))
        }
    }

    /// Gets the font em height of the text at the specified position. Also returns the text range
    /// which has identical formatting to the current character.
    pub fn get_font_size(&self, position: u32) -> DWResult<(f32, TextRange)> {
        unsafe {
            let mut font_size = 0.0;
            let mut range = mem::uninitialized();
            let res = self.ptr.GetFontSize(position, &mut font_size, &mut range);
            if res < 0 {
                return Err(res.into());
            }
            Ok((font_size, range.into()))
        }
    }

    /// Gets the font stretch of the text at the specified position. Also returns the text range
    /// which has identical formatting to the current character.
    pub fn get_font_stretch(&self, position: u32) -> DWResult<(FontStretch, TextRange)> {
        unsafe {
            let (mut stretch, mut range) = mem::uninitialized();
            let res = self.ptr.GetFontStretch(position, &mut stretch, &mut range);
            if res < 0 {
                return Err(res.into());
            }

            Ok((FontStretch::from_u32(stretch).unwrap(), range.into()))
        }
    }

    /// Gets the font style of the text at the specified position. Also returns the text range
    /// which has identical formatting to the current character.
    pub fn get_font_style(&self, position: u32) -> DWResult<(FontStyle, TextRange)> {
        unsafe {
            let (mut style, mut range) = mem::uninitialized();
            let res = self.ptr.GetFontStyle(position, &mut style, &mut range);
            if res < 0 {
                return Err(res.into());
            }

            Ok((FontStyle::from_u32(style).unwrap(), range.into()))
        }
    }

    /// Gets the font weight of the text at the specified position. Also returns the text range
    /// which has identical formatting to the current character.
    pub fn get_font_weight(&self, position: u32) -> DWResult<(FontWeight, TextRange)> {
        unsafe {
            let (mut weight, mut range) = mem::uninitialized();
            let res = self.ptr.GetFontWeight(position, &mut weight, &mut range);
            if res < 0 {
                return Err(res.into());
            }

            Ok((FontWeight::from_u32(weight).unwrap(), range.into()))
        }
    }

    /// Gets the inline object at the position as-is. May return ptr::null_mut()
    pub fn get_inline_object(
        &self,
        position: u32,
    ) -> DWResult<(*mut IDWriteInlineObject, TextRange)> {
        unsafe {
            let mut range = mem::uninitialized();
            let mut ptr = ptr::null_mut();
            let hr = self.ptr.GetInlineObject(position, &mut ptr, &mut range);
            if SUCCEEDED(hr) {
                Ok((ptr, range.into()))
            } else {
                Err(hr.into())
            }
        }
    }

    /// Get the number of LineMetrics objects that you need room for when calling
    /// `get_line_metrics_slice`
    pub fn get_line_metrics_count(&self) -> usize {
        unsafe {
            let mut count = 0;
            self.ptr.GetLineMetrics(ptr::null_mut(), 0, &mut count);
            count as usize
        }
    }

    /// Retrieves the information about each individual text line of the text string. You should
    /// first call `get_line_metrics_count` to know how large your slice must be to fit all of
    /// the metrics objects. The return value will contain the actual number of elements in the
    /// layout, but the official documentation does *not* specify whether the array will be filled
    /// with any values in the Err case, so that behavior is not guaranteed between windows
    /// versions.
    pub fn get_line_metrics_slice(&self, buf: &mut [metrics::LineMetrics]) -> Result<usize, usize> {
        assert!(buf.len() <= u32::MAX as usize);
        unsafe {
            let mut actual_count = 0;
            let buf_ptr = buf.as_mut_ptr() as *mut DWRITE_LINE_METRICS;
            let res = self
                .ptr
                .GetLineMetrics(buf_ptr, buf.len() as u32, &mut actual_count);

            if res == S_OK {
                Ok(actual_count as usize)
            } else {
                Err(actual_count as usize)
            }
        }
    }

    /// Retrieves the information about each individual text line of the text string. Resizes `buf`
    /// to fit all of the elements.
    pub fn get_line_metrics(&self, buf: &mut Vec<metrics::LineMetrics>) {
        let count = self.get_line_metrics_count();
        unsafe {
            buf.clear();
            buf.reserve(count);
            buf.set_len(count);
        }
        assert_eq!(self.get_line_metrics_slice(buf), Ok(count));
    }

    /// Gets the layout maximum height.
    pub fn get_max_height(&self) -> f32 {
        unsafe { self.ptr.GetMaxHeight() }
    }

    /// Gets the layout maximum width.
    pub fn get_max_width(&self) -> f32 {
        unsafe { self.ptr.GetMaxWidth() }
    }

    /// Retrieves overall metrics for the formatted string.
    pub fn get_metrics(&self) -> metrics::Metrics {
        unsafe {
            let mut metrics = mem::zeroed();
            self.ptr.GetMetrics(&mut metrics);

            metrics::Metrics::build(metrics)
        }
    }

    /// Returns the overhangs (in DIPs) of the layout and all objects contained in it, including
    /// text glyphs and inline objects.
    pub fn get_overhang_metrics(&self) -> metrics::OverhangMetrics {
        unsafe {
            let mut metrics = mem::zeroed();
            self.ptr.GetOverhangMetrics(&mut metrics);

            metrics::OverhangMetrics::build(metrics)
        }
    }

    pub fn get_strikethrough(&self, position: u32) -> DWResult<(bool, TextRange)> {
        unsafe {
            let (mut strikethrough, mut range) = mem::uninitialized();
            let res = self
                .ptr
                .GetStrikethrough(position, &mut strikethrough, &mut range);
            if res < 0 {
                return Err(res.into());
            }

            Ok((strikethrough != 0, range.into()))
        }
    }

    // TODO: Typography

    pub fn get_underline(&self, position: u32) -> DWResult<(bool, TextRange)> {
        unsafe {
            let (mut underline, mut range) = mem::uninitialized();
            let res = self.ptr.GetUnderline(position, &mut underline, &mut range);
            if res < 0 {
                return Err(res.into());
            }

            Ok((underline != 0, range.into()))
        }
    }

    /// The application calls this function passing in a specific pixel location relative to the
    /// top-left location of the layout box and obtains the information about the correspondent
    /// hit-test metrics of the text string where the hit-test has occurred. Returns None if the
    /// specified pixel location is outside the string.
    pub fn hit_test_point(&self, point_x: f32, point_y: f32) -> HitTestPoint {
        unsafe {
            let mut trail = 0;
            let mut inside = 0;
            let mut metrics = mem::uninitialized();
            self.ptr
                .HitTestPoint(point_x, point_y, &mut trail, &mut inside, &mut metrics);

            HitTestPoint {
                metrics: InternalConstructor::build(metrics),
                is_inside: inside != 0,
                is_trailing_hit: trail != 0,
            }
        }
    }

    /// The application calls this function to get the pixel location relative to the top-left of
    /// the layout box given the text position and the logical side of the position. This function
    /// is normally used as part of caret positioning of text where the caret is drawn at the
    /// location corresponding to the current text editing position. It may also be used as a way
    /// to programmatically obtain the geometry of a particular text position in UI automation.
    pub fn hit_test_text_position(
        &self,
        position: u32,
        trailing: bool,
    ) -> Option<HitTestTextPosition> {
        let trailing = if trailing { 0 } else { 1 };
        unsafe {
            let (mut x, mut y) = (0.0, 0.0);
            let mut metrics = mem::uninitialized();
            let res =
                self.ptr
                    .HitTestTextPosition(position, trailing, &mut x, &mut y, &mut metrics);
            if res != S_OK {
                return None;
            }

            Some(HitTestTextPosition {
                metrics: InternalConstructor::build(metrics),
                point_x: x,
                point_y: y,
            })
        }
    }

    /// The application calls this function to get a set of hit-test metrics corresponding to a
    /// range of text positions. One of the main usages is to implement highlight selection of
    /// the text string. origin_x and origin_y are added to the hit-test metrics returned.
    pub fn hit_test_text_range(
        &self,
        position: u32,
        length: u32,
        origin_x: f32,
        origin_y: f32,
        metrics: &mut Vec<metrics::HitTestMetrics>,
    ) -> bool {
        unsafe {
            // Calculate the total number of items we need
            let mut actual_count = 0;
            let res = self.ptr.HitTestTextRange(
                position,
                length,
                origin_x,
                origin_y,
                ptr::null_mut(),
                0,
                &mut actual_count,
            );
            if res != S_OK {
                return false;
            }

            metrics.set_len(actual_count as usize);
            let buf_ptr = metrics[..].as_mut_ptr() as *mut _;
            let len = metrics.len() as u32;
            let res = self.ptr.HitTestTextRange(
                position,
                length,
                origin_x,
                origin_y,
                buf_ptr,
                len,
                &mut actual_count,
            );
            if res != S_OK {
                metrics.set_len(0);
                return false;
            }

            metrics.set_len(actual_count as usize);
            true
        }
    }

    /// Sets the drawing style for text within a text range.
    pub fn set_drawing_effect<E, T>(&self, effect: &E, range: T) -> DWResult<()>
    where
        E: DrawingEffect,
        T: Into<TextRange>,
    {
        let range = range.into();
        let range = DWRITE_TEXT_RANGE {
            startPosition: range.start,
            length: range.length,
        };

        unsafe {
            let hr = self.ptr.SetDrawingEffect(effect.get_effect_ptr(), range);
            if SUCCEEDED(hr) {
                Ok(())
            } else {
                Err(hr.into())
            }
        }
    }

    /// Sets the font collection for text within a text range.
    pub fn set_font_collection<T>(&self, collection: FontCollection, range: T) -> DWResult<()>
    where
        T: Into<TextRange>,
    {
        let range = range.into();
        let range = DWRITE_TEXT_RANGE {
            startPosition: range.start,
            length: range.length,
        };

        unsafe {
            let hr = self.ptr.SetFontCollection(collection.get_raw(), range);
            if SUCCEEDED(hr) {
                Ok(())
            } else {
                Err(hr.into())
            }
        }
    }

    /// Sets the font style for text within a text range.
    pub fn set_font_style<T>(&self, style: FontStyle, range: T) -> DWResult<()>
    where
        T: Into<TextRange>,
    {
        let range = range.into();
        let range = DWRITE_TEXT_RANGE {
            startPosition: range.start,
            length: range.length,
        };

        unsafe {
            let hr = self.ptr.SetFontStyle(style as u32, range);
            if SUCCEEDED(hr) {
                Ok(())
            } else {
                Err(hr.into())
            }
        }
    }

    /// Sets the font weight for text within a text range.
    pub fn set_font_weight<T>(&self, weight: FontWeight, range: T) -> DWResult<()>
    where
        T: Into<TextRange>,
    {
        let range = range.into();
        let range = DWRITE_TEXT_RANGE {
            startPosition: range.start,
            length: range.length,
        };

        unsafe {
            let hr = self.ptr.SetFontWeight(weight as u32, range);
            if SUCCEEDED(hr) {
                Ok(())
            } else {
                Err(hr.into())
            }
        }
    }

    pub fn set_inline_object<I, T>(&self, iobj: I, range: T) -> DWResult<()>
    where
        I: IntoInlineObject,
        T: Into<TextRange>,
    {
        let range = range.into();
        let range = DWRITE_TEXT_RANGE {
            startPosition: range.start,
            length: range.length,
        };

        unsafe {
            let iobj = iobj.into_iobj();
            let hr = self.ptr.SetInlineObject(iobj, range);
            if SUCCEEDED(hr) {
                Ok(())
            } else {
                Err(hr.into())
            }
        }
    }

    pub fn set_locale_name<T>(&self, locale: &str, range: T) -> DWResult<()>
    where
        T: Into<TextRange>,
    {
        let range = range.into();

        let locale = locale.to_wide_null();
        let range = DWRITE_TEXT_RANGE {
            startPosition: range.start,
            length: range.length,
        };

        unsafe {
            let hr = self.ptr.SetLocaleName(locale.as_ptr(), range);
            if SUCCEEDED(hr) {
                Ok(())
            } else {
                Err(hr.into())
            }
        }
    }

    pub fn set_max_height(&self, maxh: f32) -> DWResult<()> {
        unsafe {
            let hr = self.ptr.SetMaxHeight(maxh);
            if SUCCEEDED(hr) {
                Ok(())
            } else {
                Err(hr.into())
            }
        }
    }

    pub fn set_max_width(&self, maxw: f32) -> DWResult<()> {
        unsafe {
            let hr = self.ptr.SetMaxWidth(maxw);
            if SUCCEEDED(hr) {
                Ok(())
            } else {
                Err(hr.into())
            }
        }
    }

    /// Sets strikethrough for text within a specified text range.
    pub fn set_strikethrough<T>(&self, strikethrough: bool, range: T) -> DWResult<()>
    where
        T: Into<TextRange>,
    {
        let range = range.into();

        let strikethrough = if strikethrough { 1 } else { 0 };
        let range = DWRITE_TEXT_RANGE {
            startPosition: range.start,
            length: range.length,
        };

        unsafe {
            let hr = self.ptr.SetStrikethrough(strikethrough, range);
            if SUCCEEDED(hr) {
                Ok(())
            } else {
                Err(hr.into())
            }
        }
    }

    // TODO: Typography

    /// Sets underlining for text within a specified text range.
    pub fn set_underline<T>(&self, underline: bool, range: T) -> DWResult<()>
    where
        T: Into<TextRange>,
    {
        let range = range.into();

        let underline = if underline { 1 } else { 0 };
        let range = DWRITE_TEXT_RANGE {
            startPosition: range.start,
            length: range.length,
        };

        unsafe {
            let hr = self.ptr.SetUnderline(underline, range);
            if SUCCEEDED(hr) {
                Ok(())
            } else {
                Err(hr.into())
            }
        }
    }

    pub unsafe fn from_raw(raw: *mut IDWriteTextLayout) -> Self {
        TextLayout {
            ptr: ComPtr::from_raw(raw),
        }
    }

    pub unsafe fn get_raw(&self) -> *mut IDWriteTextLayout {
        self.ptr.as_raw()
    }
}

#[derive(Copy, Clone)]
pub struct TextRange {
    pub start: u32,
    pub length: u32,
}

impl From<DWRITE_TEXT_RANGE> for TextRange {
    fn from(range: DWRITE_TEXT_RANGE) -> Self {
        TextRange {
            start: range.startPosition,
            length: range.length,
        }
    }
}

impl From<ops::Range<u32>> for TextRange {
    fn from(range: ops::Range<u32>) -> Self {
        assert!(
            range.end >= range.start,
            "Range end cannot come before range start"
        );
        TextRange {
            start: range.start,
            length: range.end - range.start,
        }
    }
}

impl From<ops::RangeTo<u32>> for TextRange {
    fn from(range: ops::RangeTo<u32>) -> Self {
        TextRange {
            start: 0,
            length: range.end,
        }
    }
}

impl From<ops::RangeFrom<u32>> for TextRange {
    fn from(range: ops::RangeFrom<u32>) -> Self {
        TextRange {
            start: range.start,
            length: u32::MAX,
        }
    }
}

impl From<ops::RangeFull> for TextRange {
    fn from(_range: ops::RangeFull) -> Self {
        TextRange {
            start: 0,
            length: u32::MAX,
        }
    }
}

// TODO: Re-enable when 1.26 drops
/*impl From<ops::RangeInclusive<u32>> for TextRange {
    fn from(mut range: ops::RangeInclusive<u32>) -> Self {
        /*assert!(range.end + 1 >= range.start, "Range end cannot come before range start");
        TextRange {
            start: range.start,
            length: (range.end + 1) - range.start,
        }*/

        // TODO: Accessing `RangeInclusive` directly is nightly-only, so
        // I'm relying on the implementation of size_hint for RangeInclusive
        // to get the values I want.
        let length = range.size_hint().0 as u32;
        let start = range.nth(0).unwrap_or(0);

        TextRange { start, length }
    }
}*/

#[derive(Copy, Clone)]
pub struct HitTestPoint {
    /// The output geometry fully enclosing the hit-test location. When is_inside is set to false,
    /// this structure represents the geometry enclosing the edge closest to the hit-test location.
    pub metrics: metrics::HitTestMetrics,
    /// An output flag that indicates whether the hit-test location is inside the text string. When
    /// false, the position nearest the text's edge is returned.
    pub is_inside: bool,
    /// An output flag that indicates whether the hit-test location is at the leading or the
    /// trailing side of the character. When is_inside is set to false, this value is set according
    /// to the output hitTestMetrics->textPosition value to represent the edge closest to the
    /// hit-test location.
    pub is_trailing_hit: bool,
}

#[derive(Copy, Clone)]
pub struct HitTestTextPosition {
    /// The output pixel location X, relative to the top-left location of the layout box.
    pub point_x: f32,
    /// The output pixel location Y, relative to the top-left location of the layout box.
    pub point_y: f32,

    /// The output geometry fully enclosing the specified text position.
    pub metrics: metrics::HitTestMetrics,
}

unsafe impl Send for TextLayout {}
unsafe impl Sync for TextLayout {}