| 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 |
//! A custom implementation of the "text analysis source" interface so that |
| 6 |
//! we can convey data to the `FontFallback::map_characters` method. |
| 7 |
|
| 8 |
#![allow(non_snake_case)] |
| 9 |
|
| 10 |
use std::borrow::Cow; |
| 11 |
use std::ffi::OsStr; |
| 12 |
use std::mem; |
| 13 |
use std::os::windows::ffi::OsStrExt; |
| 14 |
use std::ptr::{self, null}; |
| 15 |
use std::sync::atomic::AtomicUsize; |
| 16 |
use winapi::ctypes::wchar_t; |
| 17 |
use winapi::shared::basetsd::UINT32; |
| 18 |
use winapi::shared::guiddef::REFIID; |
| 19 |
use winapi::shared::minwindef::{FALSE, TRUE, ULONG}; |
| 20 |
use winapi::shared::ntdef::LOCALE_NAME_MAX_LENGTH; |
| 21 |
use winapi::shared::winerror::{E_INVALIDARG, S_OK}; |
| 22 |
use winapi::um::dwrite::IDWriteNumberSubstitution; |
| 23 |
use winapi::um::dwrite::IDWriteTextAnalysisSource; |
| 24 |
use winapi::um::dwrite::IDWriteTextAnalysisSourceVtbl; |
| 25 |
use winapi::um::dwrite::DWRITE_NUMBER_SUBSTITUTION_METHOD; |
| 26 |
use winapi::um::dwrite::DWRITE_READING_DIRECTION; |
| 27 |
use winapi::um::unknwnbase::{IUnknown, IUnknownVtbl}; |
| 28 |
use winapi::um::winnt::HRESULT; |
| 29 |
use wio::com::ComPtr; |
| 30 |
|
| 31 |
use super::DWriteFactory; |
| 32 |
use crate::com_helpers::Com; |
| 33 |
use crate::helpers::ToWide; |
| 34 |
|
| 35 |
/// The Rust side of a custom text analysis source implementation. |
| 36 |
pub trait TextAnalysisSourceMethods { |
| 37 |
/// Determine the locale for a range of text. |
| 38 |
/// |
| 39 |
/// Return locale and length of text (in utf-16 code units) for which the |
| 40 |
/// locale is valid. |
| 41 |
fn get_locale_name<'a>(&'a self, text_position: u32) -> (Cow<'a, str>, u32); |
| 42 |
|
| 43 |
/// Get the text direction for the paragraph. |
| 44 |
fn get_paragraph_reading_direction(&self) -> DWRITE_READING_DIRECTION; |
| 45 |
} |
| 46 |
|
| 47 |
#[repr(C)] |
| 48 |
pub struct CustomTextAnalysisSourceImpl<'a> { |
| 49 |
// NB: This must be the first field. |
| 50 |
_refcount: AtomicUsize, |
| 51 |
inner: Box<dyn TextAnalysisSourceMethods + 'a>, |
| 52 |
text: Cow<'a, [wchar_t]>, |
| 53 |
number_subst: Option<NumberSubstitution>, |
| 54 |
locale_buf: [wchar_t; LOCALE_NAME_MAX_LENGTH], |
| 55 |
} |
| 56 |
|
| 57 |
/// A wrapped version of an `IDWriteNumberSubstitution` object. |
| 58 |
pub struct NumberSubstitution { |
| 59 |
native: ComPtr<IDWriteNumberSubstitution>, |
| 60 |
} |
| 61 |
|
| 62 |
// TODO: implement Clone, for convenience and efficiency? |
| 63 |
|
| 64 |
static TEXT_ANALYSIS_SOURCE_VTBL: IDWriteTextAnalysisSourceVtbl = IDWriteTextAnalysisSourceVtbl { |
| 65 |
parent: implement_iunknown!(static IDWriteTextAnalysisSource, CustomTextAnalysisSourceImpl), |
| 66 |
GetLocaleName: CustomTextAnalysisSourceImpl_GetLocaleName, |
| 67 |
GetNumberSubstitution: CustomTextAnalysisSourceImpl_GetNumberSubstitution, |
| 68 |
GetParagraphReadingDirection: CustomTextAnalysisSourceImpl_GetParagraphReadingDirection, |
| 69 |
GetTextAtPosition: CustomTextAnalysisSourceImpl_GetTextAtPosition, |
| 70 |
GetTextBeforePosition: CustomTextAnalysisSourceImpl_GetTextBeforePosition, |
| 71 |
}; |
| 72 |
|
| 73 |
impl<'a> CustomTextAnalysisSourceImpl<'a> { |
| 74 |
/// Create a new custom TextAnalysisSource for the given text and a trait |
| 75 |
/// implementation. |
| 76 |
/// |
| 77 |
/// Note: this method has no NumberSubsitution specified. See |
| 78 |
/// `from_text_and_number_subst_native` if you need number substitution. |
| 79 |
pub fn from_text_native( |
| 80 |
inner: Box<dyn TextAnalysisSourceMethods + 'a>, |
| 81 |
text: Cow<'a, [wchar_t]>, |
| 82 |
) -> CustomTextAnalysisSourceImpl<'a> { |
| 83 |
assert!(text.len() <= (std::u32::MAX as usize)); |
| 84 |
CustomTextAnalysisSourceImpl { |
| 85 |
_refcount: AtomicUsize::new(1), |
| 86 |
inner, |
| 87 |
text, |
| 88 |
number_subst: None, |
| 89 |
locale_buf: [0u16; LOCALE_NAME_MAX_LENGTH], |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/// Create a new custom TextAnalysisSource for the given text and a trait |
| 94 |
/// implementation. |
| 95 |
/// |
| 96 |
/// Note: this method only supports a single `NumberSubstitution` for the |
| 97 |
/// entire string. |
| 98 |
pub fn from_text_and_number_subst_native( |
| 99 |
inner: Box<dyn TextAnalysisSourceMethods + 'a>, |
| 100 |
text: Cow<'a, [wchar_t]>, |
| 101 |
number_subst: NumberSubstitution, |
| 102 |
) -> CustomTextAnalysisSourceImpl<'a> { |
| 103 |
assert!(text.len() <= (std::u32::MAX as usize)); |
| 104 |
CustomTextAnalysisSourceImpl { |
| 105 |
_refcount: AtomicUsize::new(1), |
| 106 |
inner, |
| 107 |
text, |
| 108 |
number_subst: Some(number_subst), |
| 109 |
locale_buf: [0u16; LOCALE_NAME_MAX_LENGTH], |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
impl Com<IDWriteTextAnalysisSource> for CustomTextAnalysisSourceImpl<'_> { |
| 115 |
type Vtbl = IDWriteTextAnalysisSourceVtbl; |
| 116 |
#[inline] |
| 117 |
fn vtbl() -> &'static IDWriteTextAnalysisSourceVtbl { |
| 118 |
&TEXT_ANALYSIS_SOURCE_VTBL |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
impl Com<IUnknown> for CustomTextAnalysisSourceImpl<'_> { |
| 123 |
type Vtbl = IUnknownVtbl; |
| 124 |
#[inline] |
| 125 |
fn vtbl() -> &'static IUnknownVtbl { |
| 126 |
&TEXT_ANALYSIS_SOURCE_VTBL.parent |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
unsafe extern "system" fn CustomTextAnalysisSourceImpl_GetLocaleName( |
| 131 |
this: *mut IDWriteTextAnalysisSource, |
| 132 |
text_position: UINT32, |
| 133 |
text_length: *mut UINT32, |
| 134 |
locale_name: *mut *const wchar_t, |
| 135 |
) -> HRESULT { |
| 136 |
let this = CustomTextAnalysisSourceImpl::from_interface(this); |
| 137 |
let (locale, text_len) = this.inner.get_locale_name(text_position); |
| 138 |
|
| 139 |
// Copy the locale data into the buffer |
| 140 |
for (i, c) in OsStr::new(&*locale).encode_wide().chain(Some(0)).enumerate() { |
| 141 |
// -1 here is deliberate: it ensures that we never write to the last character in |
| 142 |
// this.locale_buf, so that the buffer is always null-terminated. |
| 143 |
if i >= this.locale_buf.len() - 1 { |
| 144 |
break |
| 145 |
} |
| 146 |
|
| 147 |
*this.locale_buf.get_unchecked_mut(i) = c; |
| 148 |
} |
| 149 |
|
| 150 |
*text_length = text_len; |
| 151 |
*locale_name = this.locale_buf.as_ptr(); |
| 152 |
S_OK |
| 153 |
} |
| 154 |
|
| 155 |
unsafe extern "system" fn CustomTextAnalysisSourceImpl_GetNumberSubstitution( |
| 156 |
this: *mut IDWriteTextAnalysisSource, |
| 157 |
text_position: UINT32, |
| 158 |
text_length: *mut UINT32, |
| 159 |
number_substitution: *mut *mut IDWriteNumberSubstitution, |
| 160 |
) -> HRESULT { |
| 161 |
let this = CustomTextAnalysisSourceImpl::from_interface(this); |
| 162 |
if text_position >= (this.text.len() as u32) { |
| 163 |
return E_INVALIDARG; |
| 164 |
} |
| 165 |
|
| 166 |
*text_length = (this.text.len() as UINT32) - text_position; |
| 167 |
*number_substitution = match &this.number_subst { |
| 168 |
Some(number_subst) => { |
| 169 |
let com_ptr = &number_subst.native; |
| 170 |
com_ptr.AddRef(); |
| 171 |
com_ptr.as_raw() |
| 172 |
}, |
| 173 |
None => std::ptr::null_mut() |
| 174 |
}; |
| 175 |
|
| 176 |
S_OK |
| 177 |
} |
| 178 |
|
| 179 |
unsafe extern "system" fn CustomTextAnalysisSourceImpl_GetParagraphReadingDirection( |
| 180 |
this: *mut IDWriteTextAnalysisSource, |
| 181 |
) -> DWRITE_READING_DIRECTION { |
| 182 |
let this = CustomTextAnalysisSourceImpl::from_interface(this); |
| 183 |
this.inner.get_paragraph_reading_direction() |
| 184 |
} |
| 185 |
|
| 186 |
unsafe extern "system" fn CustomTextAnalysisSourceImpl_GetTextAtPosition( |
| 187 |
this: *mut IDWriteTextAnalysisSource, |
| 188 |
text_position: UINT32, |
| 189 |
text_string: *mut *const wchar_t, |
| 190 |
text_length: *mut UINT32, |
| 191 |
) -> HRESULT { |
| 192 |
let this = CustomTextAnalysisSourceImpl::from_interface(this); |
| 193 |
if text_position >= (this.text.len() as u32) { |
| 194 |
*text_string = null(); |
| 195 |
*text_length = 0; |
| 196 |
return S_OK; |
| 197 |
} |
| 198 |
*text_string = this.text.as_ptr().add(text_position as usize); |
| 199 |
*text_length = (this.text.len() as UINT32) - text_position; |
| 200 |
S_OK |
| 201 |
} |
| 202 |
|
| 203 |
unsafe extern "system" fn CustomTextAnalysisSourceImpl_GetTextBeforePosition( |
| 204 |
this: *mut IDWriteTextAnalysisSource, |
| 205 |
text_position: UINT32, |
| 206 |
text_string: *mut *const wchar_t, |
| 207 |
text_length: *mut UINT32, |
| 208 |
) -> HRESULT { |
| 209 |
let this = CustomTextAnalysisSourceImpl::from_interface(this); |
| 210 |
if text_position == 0 || text_position > (this.text.len() as u32) { |
| 211 |
*text_string = null(); |
| 212 |
*text_length = 0; |
| 213 |
return S_OK; |
| 214 |
} |
| 215 |
*text_string = this.text.as_ptr(); |
| 216 |
*text_length = text_position; |
| 217 |
S_OK |
| 218 |
} |
| 219 |
|
| 220 |
impl NumberSubstitution { |
| 221 |
pub fn new( |
| 222 |
subst_method: DWRITE_NUMBER_SUBSTITUTION_METHOD, |
| 223 |
locale: &str, |
| 224 |
ignore_user_overrides: bool, |
| 225 |
) -> NumberSubstitution { |
| 226 |
unsafe { |
| 227 |
let mut native: *mut IDWriteNumberSubstitution = ptr::null_mut(); |
| 228 |
let hr = (*DWriteFactory()).CreateNumberSubstitution( |
| 229 |
subst_method, |
| 230 |
locale.to_wide_null().as_ptr(), |
| 231 |
if ignore_user_overrides { TRUE } else { FALSE }, |
| 232 |
&mut native, |
| 233 |
); |
| 234 |
assert_eq!(hr, 0, "error creating number substitution"); |
| 235 |
NumberSubstitution { |
| 236 |
native: ComPtr::from_raw(native), |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
} |