summaryrefslogtreecommitdiffstats
path: root/third_party/rust/dwrote/src/glyph_run_analysis.rs
blob: 5224c60bc95293acfab76f043cd42a54f56d81ad (plain)
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
/* 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;
use std::ptr;
use winapi::shared::windef::RECT;
use winapi::um::dcommon::DWRITE_MEASURING_MODE;
use winapi::um::dwrite::DWRITE_TEXTURE_CLEARTYPE_3x1;
use winapi::um::dwrite::IDWriteGlyphRunAnalysis;
use winapi::um::dwrite::{DWRITE_TEXTURE_ALIASED_1x1, DWRITE_GLYPH_RUN, DWRITE_TEXTURE_TYPE};
use winapi::um::dwrite::{DWRITE_MATRIX, DWRITE_RENDERING_MODE};
use winapi::um::winnt::HRESULT;
use wio::com::ComPtr;

use super::DWriteFactory;

pub struct GlyphRunAnalysis {
    native: UnsafeCell<ComPtr<IDWriteGlyphRunAnalysis>>,
}

impl GlyphRunAnalysis {
    pub fn create(
        glyph_run: &DWRITE_GLYPH_RUN,
        pixels_per_dip: f32,
        transform: Option<DWRITE_MATRIX>,
        rendering_mode: DWRITE_RENDERING_MODE,
        measuring_mode: DWRITE_MEASURING_MODE,
        baseline_x: f32,
        baseline_y: f32,
    ) -> Result<GlyphRunAnalysis, HRESULT> {
        unsafe {
            let mut native: *mut IDWriteGlyphRunAnalysis = ptr::null_mut();
            let hr = (*DWriteFactory()).CreateGlyphRunAnalysis(
                glyph_run as *const DWRITE_GLYPH_RUN,
                pixels_per_dip,
                transform
                    .as_ref()
                    .map(|x| x as *const _)
                    .unwrap_or(ptr::null()),
                rendering_mode,
                measuring_mode,
                baseline_x,
                baseline_y,
                &mut native,
            );
            if hr != 0 {
                Err(hr)
            } else {
                Ok(GlyphRunAnalysis::take(ComPtr::from_raw(native)))
            }
        }
    }

    pub fn take(native: ComPtr<IDWriteGlyphRunAnalysis>) -> GlyphRunAnalysis {
        GlyphRunAnalysis {
            native: UnsafeCell::new(native),
        }
    }

    pub fn get_alpha_texture_bounds(
        &self,
        texture_type: DWRITE_TEXTURE_TYPE,
    ) -> Result<RECT, HRESULT> {
        unsafe {
            let mut rect: RECT = mem::zeroed();
            rect.left = 1234;
            rect.top = 1234;
            let hr = (*self.native.get()).GetAlphaTextureBounds(texture_type, &mut rect);
            if hr != 0 {
                Err(hr)
            } else {
                Ok(rect)
            }
        }
    }

    pub fn create_alpha_texture(
        &self,
        texture_type: DWRITE_TEXTURE_TYPE,
        rect: RECT,
    ) -> Result<Vec<u8>, HRESULT> {
        unsafe {
            let rect_pixels = (rect.right - rect.left) * (rect.bottom - rect.top);
            let rect_bytes = rect_pixels
                * match texture_type {
                    DWRITE_TEXTURE_ALIASED_1x1 => 1,
                    DWRITE_TEXTURE_CLEARTYPE_3x1 => 3,
                    _ => panic!("bad texture type specified"),
                };

            let mut out_bytes: Vec<u8> = vec![0; rect_bytes as usize];
            let hr = (*self.native.get()).CreateAlphaTexture(
                texture_type,
                &rect,
                out_bytes.as_mut_ptr(),
                out_bytes.len() as u32,
            );
            if hr != 0 {
                Err(hr)
            } else {
                Ok(out_bytes)
            }
        }
    }
}