Skip to content

Commit

Permalink
[win] Share font data between text objects
Browse files Browse the repository at this point in the history
  • Loading branch information
cmyr committed Mar 29, 2021
1 parent 3000c62 commit 720b3c6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
4 changes: 2 additions & 2 deletions druid-shell/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ hdr = ["piet-common/hdr"]
[dependencies]
# NOTE: When changing the piet or kurbo versions, ensure that
# the kurbo version included in piet is compatible with the kurbo version specified here.
piet-common = "=0.4.0"
piet-common = "=0.4.1"
kurbo = "0.8.1"

tracing = "0.1.22"
Expand Down Expand Up @@ -99,7 +99,7 @@ version = "0.3.44"
features = ["Window", "MouseEvent", "CssStyleDeclaration", "WheelEvent", "KeyEvent", "KeyboardEvent"]

[dev-dependencies]
piet-common = { version = "=0.4.0", features = ["png"] }
piet-common = { version = "=0.4.1", features = ["png"] }
static_assertions = "1.1.0"
test-env-log = { version = "0.2.5", features = ["trace"], default-features = false }
tracing-subscriber = "0.2.15"
Expand Down
6 changes: 5 additions & 1 deletion druid-shell/src/platform/windows/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ use winapi::um::winuser::{
IDI_APPLICATION, MSG, PM_NOREMOVE, WM_TIMER, WNDCLASSW,
};

use piet_common::D2DLoadedFonts;

use crate::application::AppHandler;

use super::accels;
Expand All @@ -43,6 +45,7 @@ use super::window::{self, DS_REQUEST_DESTROY};
#[derive(Clone)]
pub(crate) struct Application {
state: Rc<RefCell<State>>,
pub(crate) fonts: D2DLoadedFonts,
}

struct State {
Expand All @@ -57,7 +60,8 @@ impl Application {
quitting: false,
windows: HashSet::new(),
}));
Ok(Application { state })
let fonts = D2DLoadedFonts::default();
Ok(Application { state, fonts })
}

/// Initialize the app. At the moment, this is mostly needed for hi-dpi.
Expand Down
29 changes: 13 additions & 16 deletions druid-shell/src/platform/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ enum DeferredOp {

#[derive(Clone)]
pub struct WindowHandle {
dwrite_factory: DwriteFactory,
text: PietText,
state: Weak<WindowState>,
}

Expand Down Expand Up @@ -243,7 +243,7 @@ struct MyWndProc {
app: Application,
handle: RefCell<WindowHandle>,
d2d_factory: D2DFactory,
dwrite_factory: DwriteFactory,
text: PietText,
state: RefCell<Option<WndState>>,
present_strategy: PresentStrategy,
}
Expand Down Expand Up @@ -419,7 +419,7 @@ impl WndState {
}

// Renders but does not present.
fn render(&mut self, d2d: &D2DFactory, dw: &DwriteFactory, invalid: &Region) {
fn render(&mut self, d2d: &D2DFactory, text: &PietText, invalid: &Region) {
let rt = self.render_target.as_mut().unwrap();

rt.begin_draw();
Expand All @@ -434,8 +434,8 @@ impl WndState {
.unwrap()
});

let text = PietText::new(dw.clone());
let mut piet_ctx = Piet::new(d2d, text, rt);
//let text = PietText::new(dw.clone());
let mut piet_ctx = Piet::new(d2d, text.clone(), rt);

// Clear the background if transparency DC is found
if let Some(dc) = dc_for_transparency {
Expand Down Expand Up @@ -815,7 +815,7 @@ impl WndProc for MyWndProc {
let invalid = self.take_invalid();
if !invalid.rects().is_empty() {
s.handler.rebuild_resources();
s.render(&self.d2d_factory, &self.dwrite_factory, &invalid);
s.render(&self.d2d_factory, &self.text, &invalid);
if let Some(ref mut ds) = s.dxgi_state {
let mut dirty_rects = util::region_to_rectis(&invalid, self.scale());
let params = DXGI_PRESENT_PARAMETERS {
Expand Down Expand Up @@ -950,11 +950,7 @@ impl WndProc for MyWndProc {
if let Err(e) = s.rebuild_render_target(&self.d2d_factory, scale) {
error!("error building render target: {}", e);
}
s.render(
&self.d2d_factory,
&self.dwrite_factory,
&size_dp.to_rect().into(),
);
s.render(&self.d2d_factory, &self.text, &size_dp.to_rect().into());
let present_after = match self.present_strategy {
PresentStrategy::Sequential => 1,
_ => 0,
Expand Down Expand Up @@ -1330,12 +1326,13 @@ impl WindowBuilder {
unsafe {
let class_name = super::util::CLASS_NAME.to_wide();
let dwrite_factory = DwriteFactory::new().unwrap();
let dw_clone = dwrite_factory.clone();
let fonts = self.app.fonts.clone();
let text = PietText::new_with_shared_fonts(dwrite_factory, Some(fonts));
let wndproc = MyWndProc {
app: self.app.clone(),
handle: Default::default(),
d2d_factory: D2DFactory::new().unwrap(),
dwrite_factory: dw_clone,
text: text.clone(),
state: RefCell::new(None),
present_strategy: self.present_strategy,
};
Expand Down Expand Up @@ -1382,7 +1379,7 @@ impl WindowBuilder {
};
let win = Rc::new(window);
let handle = WindowHandle {
dwrite_factory,
text,
state: Rc::downgrade(&win),
};

Expand Down Expand Up @@ -1962,7 +1959,7 @@ impl WindowHandle {
}

pub fn text(&self) -> PietText {
PietText::new(self.dwrite_factory.clone())
self.text.clone()
}

pub fn add_text_field(&self) -> TextFieldToken {
Expand Down Expand Up @@ -2176,7 +2173,7 @@ impl Default for WindowHandle {
fn default() -> Self {
WindowHandle {
state: Default::default(),
dwrite_factory: DwriteFactory::new().unwrap(),
text: PietText::new_with_shared_fonts(DwriteFactory::new().unwrap(), None),
}
}
}
2 changes: 1 addition & 1 deletion druid/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ console_error_panic_hook = { version = "0.1.6" }
float-cmp = { version = "0.8.0", features = ["std"], default-features = false }
# tempfile 3.2.0 broke wasm; I assume it will be yanked (Jan 12, 2021)
tempfile = "=3.1.0"
piet-common = { version = "=0.4.0", features = ["png"] }
piet-common = { version = "=0.4.1", features = ["png"] }
pulldown-cmark = { version = "0.8", default-features = false }
test-env-log = { version = "0.2.5", features = ["trace"], default-features = false }

Expand Down

0 comments on commit 720b3c6

Please sign in to comment.