| 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 |
#![allow(non_upper_case_globals)] |
| 6 |
|
| 7 |
#[cfg(feature = "serde_serialization")] |
| 8 |
extern crate serde; |
| 9 |
#[cfg_attr(feature = "serde_serialization", macro_use)] |
| 10 |
#[cfg(feature = "serde_serialization")] |
| 11 |
extern crate serde_derive; |
| 12 |
|
| 13 |
#[macro_use] |
| 14 |
extern crate lazy_static; |
| 15 |
extern crate libc; |
| 16 |
extern crate winapi; |
| 17 |
|
| 18 |
include!("types.rs"); |
| 19 |
|
| 20 |
use std::ffi::CString; |
| 21 |
use std::ptr; |
| 22 |
use winapi::Interface; |
| 23 |
use winapi::shared::guiddef::REFIID; |
| 24 |
use winapi::shared::winerror::S_OK; |
| 25 |
use winapi::um::dwrite::DWRITE_FACTORY_TYPE; |
| 26 |
use winapi::um::dwrite::DWRITE_FACTORY_TYPE_SHARED; |
| 27 |
use winapi::um::dwrite::IDWriteFactory; |
| 28 |
use winapi::um::dwrite::IDWriteRenderingParams; |
| 29 |
use winapi::um::unknwnbase::IUnknown; |
| 30 |
use winapi::um::winnt::LPCSTR; |
| 31 |
|
| 32 |
pub use winapi::um::winnt::HRESULT; |
| 33 |
|
| 34 |
mod helpers; |
| 35 |
use helpers::ToWide; |
| 36 |
use std::os::raw::c_void; |
| 37 |
|
| 38 |
#[cfg(test)] |
| 39 |
mod test; |
| 40 |
|
| 41 |
// We still use the DWrite structs for things like metrics; re-export them |
| 42 |
// here |
| 43 |
pub use winapi::shared::windef::RECT; |
| 44 |
pub use winapi::um::dcommon::DWRITE_MEASURING_MODE; |
| 45 |
pub use winapi::um::dcommon::{ |
| 46 |
DWRITE_MEASURING_MODE_GDI_CLASSIC, DWRITE_MEASURING_MODE_GDI_NATURAL, |
| 47 |
DWRITE_MEASURING_MODE_NATURAL, |
| 48 |
}; |
| 49 |
pub use winapi::um::dwrite::DWRITE_FONT_METRICS as FontMetrics0; |
| 50 |
pub use winapi::um::dwrite::DWRITE_FONT_SIMULATIONS; |
| 51 |
pub use winapi::um::dwrite::DWRITE_GLYPH_OFFSET as GlyphOffset; |
| 52 |
pub use winapi::um::dwrite::DWRITE_RENDERING_MODE; |
| 53 |
pub use winapi::um::dwrite::DWRITE_TEXTURE_TYPE; |
| 54 |
pub use winapi::um::dwrite::{DWRITE_TEXTURE_ALIASED_1x1, DWRITE_TEXTURE_CLEARTYPE_3x1}; |
| 55 |
pub use winapi::um::dwrite::{ |
| 56 |
DWRITE_FONT_SIMULATIONS_BOLD, DWRITE_FONT_SIMULATIONS_NONE, DWRITE_FONT_SIMULATIONS_OBLIQUE, |
| 57 |
}; |
| 58 |
pub use winapi::um::dwrite::{DWRITE_GLYPH_RUN, DWRITE_MATRIX}; |
| 59 |
pub use winapi::um::dwrite::{ |
| 60 |
DWRITE_RENDERING_MODE_ALIASED, DWRITE_RENDERING_MODE_CLEARTYPE_GDI_CLASSIC, |
| 61 |
DWRITE_RENDERING_MODE_CLEARTYPE_GDI_NATURAL, DWRITE_RENDERING_MODE_CLEARTYPE_NATURAL, |
| 62 |
DWRITE_RENDERING_MODE_CLEARTYPE_NATURAL_SYMMETRIC, DWRITE_RENDERING_MODE_DEFAULT, |
| 63 |
DWRITE_RENDERING_MODE_GDI_CLASSIC, DWRITE_RENDERING_MODE_GDI_NATURAL, |
| 64 |
DWRITE_RENDERING_MODE_NATURAL, DWRITE_RENDERING_MODE_NATURAL_SYMMETRIC, |
| 65 |
DWRITE_RENDERING_MODE_OUTLINE, |
| 66 |
}; |
| 67 |
pub use winapi::um::dwrite_1::DWRITE_FONT_METRICS1 as FontMetrics1; |
| 68 |
pub use winapi::um::dwrite_3::DWRITE_FONT_AXIS_VALUE; |
| 69 |
use winapi::um::libloaderapi::{GetProcAddress, LoadLibraryW}; |
| 70 |
|
| 71 |
#[macro_use] |
| 72 |
mod com_helpers; |
| 73 |
|
| 74 |
mod bitmap_render_target; |
| 75 |
pub use bitmap_render_target::BitmapRenderTarget; |
| 76 |
mod font; |
| 77 |
pub use font::{Font, FontMetrics, InformationalStringId}; |
| 78 |
mod font_collection; |
| 79 |
pub use font_collection::FontCollection; |
| 80 |
mod font_face; |
| 81 |
pub use font_face::{FontFace, FontFaceType}; |
| 82 |
mod font_fallback; |
| 83 |
pub use font_fallback::{FallbackResult, FontFallback}; |
| 84 |
mod font_family; |
| 85 |
pub use font_family::FontFamily; |
| 86 |
mod font_file; |
| 87 |
pub use font_file::FontFile; |
| 88 |
mod gdi_interop; |
| 89 |
pub use gdi_interop::GdiInterop; |
| 90 |
mod outline_builder; |
| 91 |
pub use outline_builder::OutlineBuilder; |
| 92 |
mod rendering_params; |
| 93 |
pub use rendering_params::RenderingParams; |
| 94 |
mod text_analysis_source; |
| 95 |
pub use text_analysis_source::TextAnalysisSource; |
| 96 |
mod glyph_run_analysis; |
| 97 |
pub use glyph_run_analysis::GlyphRunAnalysis; |
| 98 |
|
| 99 |
// This is an internal implementation of FontFileLoader, for our utility |
| 100 |
// functions. We don't wrap the DWriteFontFileLoader interface and |
| 101 |
// related things. |
| 102 |
mod font_file_loader_impl; |
| 103 |
|
| 104 |
// This is an implementation of `FontCollectionLoader` for client code. |
| 105 |
mod font_collection_impl; |
| 106 |
pub use font_collection_impl::CustomFontCollectionLoaderImpl; |
| 107 |
|
| 108 |
// This is an implementation of `TextAnalysisSource` for client code. |
| 109 |
mod text_analysis_source_impl; |
| 110 |
pub use text_analysis_source_impl::{ |
| 111 |
CustomTextAnalysisSourceImpl, NumberSubstitution, TextAnalysisSourceMethods, |
| 112 |
}; |
| 113 |
|
| 114 |
// This is an internal implementation of `GeometrySink` so that we can |
| 115 |
// expose `IDWriteGeometrySink` in an idiomatic way. |
| 116 |
mod geometry_sink_impl; |
| 117 |
|
| 118 |
lazy_static! { |
| 119 |
static ref DWRITE_FACTORY_RAW_PTR: usize = { |
| 120 |
unsafe { |
| 121 |
type DWriteCreateFactoryType = |
| 122 |
extern "system" fn(DWRITE_FACTORY_TYPE, REFIID, *mut *mut IUnknown) -> HRESULT; |
| 123 |
|
| 124 |
let dwrite_dll = LoadLibraryW("dwrite.dll".to_wide_null().as_ptr()); |
| 125 |
assert!(!dwrite_dll.is_null()); |
| 126 |
let create_factory_name = CString::new("DWriteCreateFactory").unwrap(); |
| 127 |
let dwrite_create_factory_ptr = |
| 128 |
GetProcAddress(dwrite_dll, create_factory_name.as_ptr() as LPCSTR); |
| 129 |
assert!(!dwrite_create_factory_ptr.is_null()); |
| 130 |
|
| 131 |
let dwrite_create_factory = mem::transmute::<*const c_void, DWriteCreateFactoryType>( |
| 132 |
dwrite_create_factory_ptr as *const _, |
| 133 |
); |
| 134 |
|
| 135 |
let mut factory: *mut IDWriteFactory = ptr::null_mut(); |
| 136 |
let hr = dwrite_create_factory( |
| 137 |
DWRITE_FACTORY_TYPE_SHARED, |
| 138 |
&IDWriteFactory::uuidof(), |
| 139 |
&mut factory as *mut *mut IDWriteFactory as *mut *mut IUnknown, |
| 140 |
); |
| 141 |
assert!(hr == S_OK); |
| 142 |
factory as usize |
| 143 |
} |
| 144 |
}; |
| 145 |
static ref DEFAULT_DWRITE_RENDERING_PARAMS_RAW_PTR: usize = { |
| 146 |
unsafe { |
| 147 |
let mut default_rendering_params: *mut IDWriteRenderingParams = ptr::null_mut(); |
| 148 |
let hr = (*DWriteFactory()).CreateRenderingParams(&mut default_rendering_params); |
| 149 |
assert!(hr == S_OK); |
| 150 |
default_rendering_params as usize |
| 151 |
} |
| 152 |
}; |
| 153 |
} // end lazy static |
| 154 |
|
| 155 |
// FIXME vlad would be nice to return, say, FactoryPtr<IDWriteFactory> |
| 156 |
// that has a DerefMut impl, so that we can write |
| 157 |
// DWriteFactory().SomeOperation() as opposed to |
| 158 |
// (*DWriteFactory()).SomeOperation() |
| 159 |
#[allow(non_snake_case)] |
| 160 |
fn DWriteFactory() -> *mut IDWriteFactory { |
| 161 |
(*DWRITE_FACTORY_RAW_PTR) as *mut IDWriteFactory |
| 162 |
} |
| 163 |
|
| 164 |
#[allow(non_snake_case)] |
| 165 |
fn DefaultDWriteRenderParams() -> *mut IDWriteRenderingParams { |
| 166 |
(*DEFAULT_DWRITE_RENDERING_PARAMS_RAW_PTR) as *mut IDWriteRenderingParams |
| 167 |
} |