diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:44:51 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:44:51 +0000 |
commit | 9e3c08db40b8916968b9f30096c7be3f00ce9647 (patch) | |
tree | a68f146d7fa01f0134297619fbe7e33db084e0aa /third_party/rust/dwrote/src/helpers.rs | |
parent | Initial commit. (diff) | |
download | thunderbird-upstream.tar.xz thunderbird-upstream.zip |
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/dwrote/src/helpers.rs')
-rw-r--r-- | third_party/rust/dwrote/src/helpers.rs | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/third_party/rust/dwrote/src/helpers.rs b/third_party/rust/dwrote/src/helpers.rs new file mode 100644 index 0000000000..804ff9e40c --- /dev/null +++ b/third_party/rust/dwrote/src/helpers.rs @@ -0,0 +1,68 @@ +/* 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::ffi::OsStr; +use std::os::windows::ffi::OsStrExt; +use winapi::ctypes::wchar_t; +use winapi::shared::minwindef::{BOOL, FALSE}; +use winapi::shared::winerror::S_OK; +use winapi::um::dwrite::IDWriteLocalizedStrings; +use winapi::um::winnls::GetUserDefaultLocaleName; +use wio::com::ComPtr; + +lazy_static! { + static ref SYSTEM_LOCALE: Vec<wchar_t> = { + unsafe { + let mut locale: Vec<wchar_t> = vec![0; 85]; + GetUserDefaultLocaleName(locale.as_mut_ptr(), locale.len() as i32 - 1); + locale + } + }; + static ref EN_US_LOCALE: Vec<wchar_t> = { OsStr::new("en-us").to_wide_null() }; +} + +pub fn get_locale_string(strings: &mut ComPtr<IDWriteLocalizedStrings>) -> String { + unsafe { + let mut index: u32 = 0; + let mut exists: BOOL = FALSE; + let hr = strings.FindLocaleName((*SYSTEM_LOCALE).as_ptr(), &mut index, &mut exists); + if hr != S_OK || exists == FALSE { + let hr = strings.FindLocaleName((*EN_US_LOCALE).as_ptr(), &mut index, &mut exists); + if hr != S_OK || exists == FALSE { + // Ultimately fall back to first locale on list + index = 0; + } + } + + let mut length: u32 = 0; + let hr = strings.GetStringLength(index, &mut length); + assert!(hr == 0); + + let mut name: Vec<wchar_t> = Vec::with_capacity(length as usize + 1); + let hr = strings.GetString(index, name.as_mut_ptr(), length + 1); + assert!(hr == 0); + name.set_len(length as usize); + + String::from_utf16(&name).ok().unwrap() + } +} + +// ToWide from https://github.com/retep998/wio-rs/blob/master/src/wide.rs + +pub trait ToWide { + fn to_wide(&self) -> Vec<u16>; + fn to_wide_null(&self) -> Vec<u16>; +} + +impl<T> ToWide for T +where + T: AsRef<OsStr>, +{ + fn to_wide(&self) -> Vec<u16> { + self.as_ref().encode_wide().collect() + } + fn to_wide_null(&self) -> Vec<u16> { + self.as_ref().encode_wide().chain(Some(0)).collect() + } +} |