Created
May 26, 2016 05:48
-
-
Save metajack/dce493ece604f563bae3141a59d86cb0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate dwrite; | |
extern crate kernel32; | |
extern crate winapi; | |
use dwrite::DWriteCreateFactory; | |
use kernel32::GetUserDefaultLocaleName; | |
use std::{mem, ptr}; | |
use winapi::dwrite::{DWRITE_FACTORY_TYPE_SHARED}; | |
use winapi::dwrite::{IDWriteFactory, IDWriteFontCollection, IDWriteFontFamily, IDWriteLocalizedStrings}; | |
use winapi::dwrite::UuidOfIDWriteFactory; | |
use winapi::minwindef::{BOOL, FALSE}; | |
use winapi::unknwnbase::IUnknown; | |
use winapi::winerror::S_OK; | |
fn main() { | |
let mut factory: *mut IUnknown = ptr::null_mut(); | |
unsafe { | |
let hr = DWriteCreateFactory( | |
DWRITE_FACTORY_TYPE_SHARED, | |
&UuidOfIDWriteFactory, | |
&mut factory); | |
assert!(hr == S_OK); | |
let factory: *mut IDWriteFactory = mem::transmute(factory); | |
let mut font_collection: *mut IDWriteFontCollection = ptr::null_mut(); | |
let hr = (*factory).GetSystemFontCollection(&mut font_collection, FALSE); | |
assert!(hr == S_OK); | |
let family_count = (*font_collection).GetFontFamilyCount(); | |
println!("{} font families", family_count); | |
for i in 0..family_count { | |
let mut family: *mut IDWriteFontFamily = ptr::null_mut(); | |
let hr = (*font_collection).GetFontFamily(i, &mut family); | |
assert!(hr == S_OK); | |
let mut family_names: *mut IDWriteLocalizedStrings = ptr::null_mut(); | |
let hr = (*family).GetFamilyNames(&mut family_names); | |
assert!(hr == S_OK); | |
let mut locale: [winapi::wchar_t; 85] = [0; 85]; | |
let res = GetUserDefaultLocaleName(locale.as_mut_ptr(), 85); | |
assert!(res != 0); | |
let mut index: u32 = 0; | |
let mut exists: BOOL = FALSE; | |
let hr = (*family_names).FindLocaleName(locale.as_ptr(), &mut index, &mut exists); | |
assert!(hr == S_OK); | |
if exists == FALSE { | |
index = 0; | |
} | |
let mut length: u32 = 0; | |
let hr = (*family_names).GetStringLength(index, &mut length); | |
assert!(hr == S_OK); | |
let mut name: Vec<winapi::wchar_t> = Vec::with_capacity(length as usize + 1); | |
let hr = (*family_names).GetString(index, name.as_mut_ptr(), length + 1); | |
assert!(hr == 0); | |
name.set_len(length as usize+ 1); | |
let name = String::from_utf16(&name).ok().unwrap(); | |
println!("{}", name); | |
(*family_names).Release(); | |
(*family).Release(); | |
} | |
(*font_collection).Release(); | |
(*factory).Release(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment