Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,27 @@ serde_serialization = ["serde", "serde_derive"]
[dependencies]
libc = "0.2"
lazy_static = "1"
winapi = { version = "0.3.6", features = ["dwrite", "dwrite_1", "dwrite_3", "winnt", "unknwnbase", "libloaderapi", "winnls"] }
serde = { version = "1.0", optional = true }
serde_derive = { version = "1.0", optional = true }
wio = "0.2"
windows-core = ">=0.59, <=0.62"
windows-numerics = "0.3.1"
windows-sys = { version = ">=0.59, <=0.62", features = [
"Win32_Graphics_Gdi",
"Win32_System_Com",
"Win32_System_LibraryLoader",
"Win32_System_SystemServices",
"Win32_Globalization"
] }
windows = { version = ">=0.59, <=0.62", features = [
"Win32_Graphics_DirectWrite",
"Win32_Graphics_Gdi",
"Win32_Graphics_Direct2D",
"Win32_Graphics_Direct2D_Common",
"Win32_System_Com",
"Win32_System_LibraryLoader",
"Win32_System_SystemServices",
"Win32_Globalization"
] }

[package.metadata.docs.rs]
targets = ["x86_64-pc-windows-msvc"]
74 changes: 35 additions & 39 deletions src/bitmap_render_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,37 @@
* 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::cell::UnsafeCell;
use std::mem::{size_of, zeroed};
use std::mem::{ManuallyDrop, MaybeUninit, size_of};
use std::slice;
use winapi::ctypes::c_void;
use winapi::shared::windef::{HDC, RECT};
use winapi::um::dcommon::DWRITE_MEASURING_MODE;
use winapi::um::dwrite::IDWriteBitmapRenderTarget;
use winapi::um::dwrite::{DWRITE_GLYPH_OFFSET, DWRITE_GLYPH_RUN};
use winapi::um::wingdi::{GetCurrentObject, GetObjectW, BITMAP, OBJ_BITMAP, RGB};
use wio::com::ComPtr;

use windows::Win32::Foundation::{COLORREF, FALSE, RECT};
use windows::Win32::Graphics::Gdi::{BITMAP, GetCurrentObject, GetObjectW, HDC, OBJ_BITMAP};
use windows::Win32::Graphics::DirectWrite::{IDWriteBitmapRenderTarget, DWRITE_GLYPH_RUN, DWRITE_MEASURING_MODE, DWRITE_GLYPH_OFFSET};

use super::{FontFace, RenderingParams};

pub struct BitmapRenderTarget {
native: UnsafeCell<ComPtr<IDWriteBitmapRenderTarget>>,
native: IDWriteBitmapRenderTarget,
}

impl BitmapRenderTarget {
pub fn take(native: ComPtr<IDWriteBitmapRenderTarget>) -> BitmapRenderTarget {
BitmapRenderTarget {
native: UnsafeCell::new(native),
}
pub fn take(native: IDWriteBitmapRenderTarget) -> BitmapRenderTarget {
BitmapRenderTarget { native }
}

pub unsafe fn as_ptr(&self) -> *mut IDWriteBitmapRenderTarget {
(*self.native.get()).as_raw()
pub fn as_ptr(&self) -> &IDWriteBitmapRenderTarget {
&self.native
}

// A dip is 1/96th of an inch, so this value is the number of pixels per inch divided by 96.
pub fn set_pixels_per_dip(&self, ppd: f32) {
unsafe {
(*self.native.get()).SetPixelsPerDip(ppd);
let _ = self.native.SetPixelsPerDip(ppd);
}
}

pub fn get_memory_dc(&self) -> HDC {
unsafe { (*self.native.get()).GetMemoryDC() }
unsafe { self.native.GetMemoryDC() }
}

pub fn draw_glyph_run(
Expand All @@ -61,29 +55,30 @@ impl BitmapRenderTarget {
let r = (color.0 * 255.0) as u8;
let g = (color.1 * 255.0) as u8;
let b = (color.2 * 255.0) as u8;

let mut glyph_run: DWRITE_GLYPH_RUN = zeroed();
glyph_run.fontFace = font_face.as_ptr();
glyph_run.fontEmSize = em_size;
glyph_run.glyphCount = glyph_indices.len() as u32;
glyph_run.glyphIndices = glyph_indices.as_ptr();
glyph_run.glyphAdvances = glyph_advances.as_ptr();
glyph_run.glyphOffsets = glyph_offsets.as_ptr();
glyph_run.isSideways = 0;
glyph_run.bidiLevel = 0;

let mut rect: RECT = zeroed();
let hr = (*self.native.get()).DrawGlyphRun(
let color = COLORREF((r as u32) | ((g as u32) << 8) | ((b as u32) << 16));

let glyph_run = DWRITE_GLYPH_RUN {
fontFace: ManuallyDrop::new(Some(font_face.as_ptr().clone())),
fontEmSize: em_size,
glyphCount: glyph_indices.len() as u32,
glyphIndices: glyph_indices.as_ptr(),
glyphAdvances: glyph_advances.as_ptr(),
glyphOffsets: glyph_offsets.as_ptr(),
isSideways: FALSE,
bidiLevel: 0,
};

let mut rect = MaybeUninit::uninit();
self.native.DrawGlyphRun(
baseline_origin_x,
baseline_origin_y,
measuring_mode,
&glyph_run,
rendering_params.as_ptr(),
RGB(r, g, b),
&mut rect,
);
assert!(hr == 0);
rect
color,
Some(rect.as_mut_ptr()),
).unwrap();
rect.assume_init()
}
}

Expand All @@ -96,12 +91,13 @@ impl BitmapRenderTarget {
// Now grossness to pull out the pixels
unsafe {
let memory_dc = self.get_memory_dc();
let mut bitmap: BITMAP = zeroed();
let mut bitmap = MaybeUninit::<BITMAP>::uninit();
let ret = GetObjectW(
GetCurrentObject(memory_dc, OBJ_BITMAP),
GetCurrentObject(HDC(memory_dc.0), OBJ_BITMAP),
size_of::<BITMAP>() as i32,
&mut bitmap as *mut _ as *mut c_void,
Some(bitmap.as_mut_ptr() as *mut _),
);
let bitmap = bitmap.assume_init();
assert!(ret == size_of::<BITMAP>() as i32);
assert!(bitmap.bmBitsPixel == 32);

Expand Down
127 changes: 0 additions & 127 deletions src/com_helpers.rs

This file was deleted.

Loading