Skip to content

Commit 720b3c6

Browse files
committed
[win] Share font data between text objects
1 parent 3000c62 commit 720b3c6

File tree

4 files changed

+21
-20
lines changed

4 files changed

+21
-20
lines changed

druid-shell/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ hdr = ["piet-common/hdr"]
4040
[dependencies]
4141
# NOTE: When changing the piet or kurbo versions, ensure that
4242
# the kurbo version included in piet is compatible with the kurbo version specified here.
43-
piet-common = "=0.4.0"
43+
piet-common = "=0.4.1"
4444
kurbo = "0.8.1"
4545

4646
tracing = "0.1.22"
@@ -99,7 +99,7 @@ version = "0.3.44"
9999
features = ["Window", "MouseEvent", "CssStyleDeclaration", "WheelEvent", "KeyEvent", "KeyboardEvent"]
100100

101101
[dev-dependencies]
102-
piet-common = { version = "=0.4.0", features = ["png"] }
102+
piet-common = { version = "=0.4.1", features = ["png"] }
103103
static_assertions = "1.1.0"
104104
test-env-log = { version = "0.2.5", features = ["trace"], default-features = false }
105105
tracing-subscriber = "0.2.15"

druid-shell/src/platform/windows/application.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ use winapi::um::winuser::{
3232
IDI_APPLICATION, MSG, PM_NOREMOVE, WM_TIMER, WNDCLASSW,
3333
};
3434

35+
use piet_common::D2DLoadedFonts;
36+
3537
use crate::application::AppHandler;
3638

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

4851
struct State {
@@ -57,7 +60,8 @@ impl Application {
5760
quitting: false,
5861
windows: HashSet::new(),
5962
}));
60-
Ok(Application { state })
63+
let fonts = D2DLoadedFonts::default();
64+
Ok(Application { state, fonts })
6165
}
6266

6367
/// Initialize the app. At the moment, this is mostly needed for hi-dpi.

druid-shell/src/platform/windows/window.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ enum DeferredOp {
167167

168168
#[derive(Clone)]
169169
pub struct WindowHandle {
170-
dwrite_factory: DwriteFactory,
170+
text: PietText,
171171
state: Weak<WindowState>,
172172
}
173173

@@ -243,7 +243,7 @@ struct MyWndProc {
243243
app: Application,
244244
handle: RefCell<WindowHandle>,
245245
d2d_factory: D2DFactory,
246-
dwrite_factory: DwriteFactory,
246+
text: PietText,
247247
state: RefCell<Option<WndState>>,
248248
present_strategy: PresentStrategy,
249249
}
@@ -419,7 +419,7 @@ impl WndState {
419419
}
420420

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

425425
rt.begin_draw();
@@ -434,8 +434,8 @@ impl WndState {
434434
.unwrap()
435435
});
436436

437-
let text = PietText::new(dw.clone());
438-
let mut piet_ctx = Piet::new(d2d, text, rt);
437+
//let text = PietText::new(dw.clone());
438+
let mut piet_ctx = Piet::new(d2d, text.clone(), rt);
439439

440440
// Clear the background if transparency DC is found
441441
if let Some(dc) = dc_for_transparency {
@@ -815,7 +815,7 @@ impl WndProc for MyWndProc {
815815
let invalid = self.take_invalid();
816816
if !invalid.rects().is_empty() {
817817
s.handler.rebuild_resources();
818-
s.render(&self.d2d_factory, &self.dwrite_factory, &invalid);
818+
s.render(&self.d2d_factory, &self.text, &invalid);
819819
if let Some(ref mut ds) = s.dxgi_state {
820820
let mut dirty_rects = util::region_to_rectis(&invalid, self.scale());
821821
let params = DXGI_PRESENT_PARAMETERS {
@@ -950,11 +950,7 @@ impl WndProc for MyWndProc {
950950
if let Err(e) = s.rebuild_render_target(&self.d2d_factory, scale) {
951951
error!("error building render target: {}", e);
952952
}
953-
s.render(
954-
&self.d2d_factory,
955-
&self.dwrite_factory,
956-
&size_dp.to_rect().into(),
957-
);
953+
s.render(&self.d2d_factory, &self.text, &size_dp.to_rect().into());
958954
let present_after = match self.present_strategy {
959955
PresentStrategy::Sequential => 1,
960956
_ => 0,
@@ -1330,12 +1326,13 @@ impl WindowBuilder {
13301326
unsafe {
13311327
let class_name = super::util::CLASS_NAME.to_wide();
13321328
let dwrite_factory = DwriteFactory::new().unwrap();
1333-
let dw_clone = dwrite_factory.clone();
1329+
let fonts = self.app.fonts.clone();
1330+
let text = PietText::new_with_shared_fonts(dwrite_factory, Some(fonts));
13341331
let wndproc = MyWndProc {
13351332
app: self.app.clone(),
13361333
handle: Default::default(),
13371334
d2d_factory: D2DFactory::new().unwrap(),
1338-
dwrite_factory: dw_clone,
1335+
text: text.clone(),
13391336
state: RefCell::new(None),
13401337
present_strategy: self.present_strategy,
13411338
};
@@ -1382,7 +1379,7 @@ impl WindowBuilder {
13821379
};
13831380
let win = Rc::new(window);
13841381
let handle = WindowHandle {
1385-
dwrite_factory,
1382+
text,
13861383
state: Rc::downgrade(&win),
13871384
};
13881385

@@ -1962,7 +1959,7 @@ impl WindowHandle {
19621959
}
19631960

19641961
pub fn text(&self) -> PietText {
1965-
PietText::new(self.dwrite_factory.clone())
1962+
self.text.clone()
19661963
}
19671964

19681965
pub fn add_text_field(&self) -> TextFieldToken {
@@ -2176,7 +2173,7 @@ impl Default for WindowHandle {
21762173
fn default() -> Self {
21772174
WindowHandle {
21782175
state: Default::default(),
2179-
dwrite_factory: DwriteFactory::new().unwrap(),
2176+
text: PietText::new_with_shared_fonts(DwriteFactory::new().unwrap(), None),
21802177
}
21812178
}
21822179
}

druid/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ console_error_panic_hook = { version = "0.1.6" }
7878
float-cmp = { version = "0.8.0", features = ["std"], default-features = false }
7979
# tempfile 3.2.0 broke wasm; I assume it will be yanked (Jan 12, 2021)
8080
tempfile = "=3.1.0"
81-
piet-common = { version = "=0.4.0", features = ["png"] }
81+
piet-common = { version = "=0.4.1", features = ["png"] }
8282
pulldown-cmark = { version = "0.8", default-features = false }
8383
test-env-log = { version = "0.2.5", features = ["trace"], default-features = false }
8484

0 commit comments

Comments
 (0)