| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
use std::cell::UnsafeCell; |
| 6 |
use std::mem; |
| 7 |
use std::ptr; |
| 8 |
use winapi::shared::windef::RECT; |
| 9 |
use winapi::um::dcommon::DWRITE_MEASURING_MODE; |
| 10 |
use winapi::um::dwrite::DWRITE_TEXTURE_CLEARTYPE_3x1; |
| 11 |
use winapi::um::dwrite::IDWriteGlyphRunAnalysis; |
| 12 |
use winapi::um::dwrite::{DWRITE_TEXTURE_ALIASED_1x1, DWRITE_GLYPH_RUN, DWRITE_TEXTURE_TYPE}; |
| 13 |
use winapi::um::dwrite::{DWRITE_MATRIX, DWRITE_RENDERING_MODE}; |
| 14 |
use winapi::um::winnt::HRESULT; |
| 15 |
use wio::com::ComPtr; |
| 16 |
|
| 17 |
use super::DWriteFactory; |
| 18 |
|
| 19 |
pub struct GlyphRunAnalysis { |
| 20 |
native: UnsafeCell<ComPtr<IDWriteGlyphRunAnalysis>>, |
| 21 |
} |
| 22 |
|
| 23 |
impl GlyphRunAnalysis { |
| 24 |
pub fn create( |
| 25 |
glyph_run: &DWRITE_GLYPH_RUN, |
| 26 |
pixels_per_dip: f32, |
| 27 |
transform: Option<DWRITE_MATRIX>, |
| 28 |
rendering_mode: DWRITE_RENDERING_MODE, |
| 29 |
measuring_mode: DWRITE_MEASURING_MODE, |
| 30 |
baseline_x: f32, |
| 31 |
baseline_y: f32, |
| 32 |
) -> Result<GlyphRunAnalysis, HRESULT> { |
| 33 |
unsafe { |
| 34 |
let mut native: *mut IDWriteGlyphRunAnalysis = ptr::null_mut(); |
| 35 |
let hr = (*DWriteFactory()).CreateGlyphRunAnalysis( |
| 36 |
glyph_run as *const DWRITE_GLYPH_RUN, |
| 37 |
pixels_per_dip, |
| 38 |
transform |
| 39 |
.as_ref() |
| 40 |
.map(|x| x as *const _) |
| 41 |
.unwrap_or(ptr::null()), |
| 42 |
rendering_mode, |
| 43 |
measuring_mode, |
| 44 |
baseline_x, |
| 45 |
baseline_y, |
| 46 |
&mut native, |
| 47 |
); |
| 48 |
if hr != 0 { |
| 49 |
Err(hr) |
| 50 |
} else { |
| 51 |
Ok(GlyphRunAnalysis::take(ComPtr::from_raw(native))) |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
pub fn take(native: ComPtr<IDWriteGlyphRunAnalysis>) -> GlyphRunAnalysis { |
| 57 |
GlyphRunAnalysis { |
| 58 |
native: UnsafeCell::new(native), |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
pub fn get_alpha_texture_bounds( |
| 63 |
&self, |
| 64 |
texture_type: DWRITE_TEXTURE_TYPE, |
| 65 |
) -> Result<RECT, HRESULT> { |
| 66 |
unsafe { |
| 67 |
let mut rect: RECT = mem::zeroed(); |
| 68 |
rect.left = 1234; |
| 69 |
rect.top = 1234; |
| 70 |
let hr = (*self.native.get()).GetAlphaTextureBounds(texture_type, &mut rect); |
| 71 |
if hr != 0 { |
| 72 |
Err(hr) |
| 73 |
} else { |
| 74 |
Ok(rect) |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
pub fn create_alpha_texture( |
| 80 |
&self, |
| 81 |
texture_type: DWRITE_TEXTURE_TYPE, |
| 82 |
rect: RECT, |
| 83 |
) -> Result<Vec<u8>, HRESULT> { |
| 84 |
unsafe { |
| 85 |
let rect_pixels = (rect.right - rect.left) * (rect.bottom - rect.top); |
| 86 |
let rect_bytes = rect_pixels |
| 87 |
* match texture_type { |
| 88 |
DWRITE_TEXTURE_ALIASED_1x1 => 1, |
| 89 |
DWRITE_TEXTURE_CLEARTYPE_3x1 => 3, |
| 90 |
_ => panic!("bad texture type specified"), |
| 91 |
}; |
| 92 |
|
| 93 |
let mut out_bytes: Vec<u8> = vec![0; rect_bytes as usize]; |
| 94 |
let hr = (*self.native.get()).CreateAlphaTexture( |
| 95 |
texture_type, |
| 96 |
&rect, |
| 97 |
out_bytes.as_mut_ptr(), |
| 98 |
out_bytes.len() as u32, |
| 99 |
); |
| 100 |
if hr != 0 { |
| 101 |
Err(hr) |
| 102 |
} else { |
| 103 |
Ok(out_bytes) |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
} |