Skip to content

Commit 2d42b1e

Browse files
committed
[win] Share font data between text objects
1 parent e6e3738 commit 2d42b1e

File tree

5 files changed

+23
-20
lines changed

5 files changed

+23
-20
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ You can find its changes [documented below](#070---2021-01-01).
77

88
### Highlights
99
- International text input support (IME) on macOS.
10+
- Rich text and complex script support on Linux.
1011

1112
### Added
1213
- Add `scroll()` method in WidgetExt ([#1600] by [@totsteps])
@@ -37,6 +38,7 @@ You can find its changes [documented below](#070---2021-01-01).
3738
- Spacers in `Flex` are now implemented by calculating the space in `Flex` instead of creating a widget for it ([#1584] by [@JAicewizard])
3839
- Padding is generic over child widget, impls WidgetWrapper ([#1634] by [@cmyr])
3940
- Menu support was rewritten with support for `Data` ([#1625] by [@jneem])
41+
- Update to piet v0.4.0 (rich text on linux!) ([#1677] by [@cmyr])
4042

4143
### Deprecated
4244

@@ -650,6 +652,7 @@ Last release without a changelog :(
650652
[#1641]: https://github.com/linebender/druid/pull/1641
651653
[#1647]: https://github.com/linebender/druid/pull/1647
652654
[#1662]: https://github.com/linebender/druid/pull/1662
655+
[#1677]: https://github.com/linebender/druid/pull/1677
653656

654657
[Unreleased]: https://github.com/linebender/druid/compare/v0.7.0...master
655658
[0.7.0]: https://github.com/linebender/druid/compare/v0.6.0...v0.7.0

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: 12 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,7 @@ 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 mut piet_ctx = Piet::new(d2d, text.clone(), rt);
439438

440439
// Clear the background if transparency DC is found
441440
if let Some(dc) = dc_for_transparency {
@@ -815,7 +814,7 @@ impl WndProc for MyWndProc {
815814
let invalid = self.take_invalid();
816815
if !invalid.rects().is_empty() {
817816
s.handler.rebuild_resources();
818-
s.render(&self.d2d_factory, &self.dwrite_factory, &invalid);
817+
s.render(&self.d2d_factory, &self.text, &invalid);
819818
if let Some(ref mut ds) = s.dxgi_state {
820819
let mut dirty_rects = util::region_to_rectis(&invalid, self.scale());
821820
let params = DXGI_PRESENT_PARAMETERS {
@@ -950,11 +949,7 @@ impl WndProc for MyWndProc {
950949
if let Err(e) = s.rebuild_render_target(&self.d2d_factory, scale) {
951950
error!("error building render target: {}", e);
952951
}
953-
s.render(
954-
&self.d2d_factory,
955-
&self.dwrite_factory,
956-
&size_dp.to_rect().into(),
957-
);
952+
s.render(&self.d2d_factory, &self.text, &size_dp.to_rect().into());
958953
let present_after = match self.present_strategy {
959954
PresentStrategy::Sequential => 1,
960955
_ => 0,
@@ -1330,12 +1325,13 @@ impl WindowBuilder {
13301325
unsafe {
13311326
let class_name = super::util::CLASS_NAME.to_wide();
13321327
let dwrite_factory = DwriteFactory::new().unwrap();
1333-
let dw_clone = dwrite_factory.clone();
1328+
let fonts = self.app.fonts.clone();
1329+
let text = PietText::new_with_shared_fonts(dwrite_factory, Some(fonts));
13341330
let wndproc = MyWndProc {
13351331
app: self.app.clone(),
13361332
handle: Default::default(),
13371333
d2d_factory: D2DFactory::new().unwrap(),
1338-
dwrite_factory: dw_clone,
1334+
text: text.clone(),
13391335
state: RefCell::new(None),
13401336
present_strategy: self.present_strategy,
13411337
};
@@ -1382,7 +1378,7 @@ impl WindowBuilder {
13821378
};
13831379
let win = Rc::new(window);
13841380
let handle = WindowHandle {
1385-
dwrite_factory,
1381+
text,
13861382
state: Rc::downgrade(&win),
13871383
};
13881384

@@ -1962,7 +1958,7 @@ impl WindowHandle {
19621958
}
19631959

19641960
pub fn text(&self) -> PietText {
1965-
PietText::new(self.dwrite_factory.clone())
1961+
self.text.clone()
19661962
}
19671963

19681964
pub fn add_text_field(&self) -> TextFieldToken {
@@ -2176,7 +2172,7 @@ impl Default for WindowHandle {
21762172
fn default() -> Self {
21772173
WindowHandle {
21782174
state: Default::default(),
2179-
dwrite_factory: DwriteFactory::new().unwrap(),
2175+
text: PietText::new_with_shared_fonts(DwriteFactory::new().unwrap(), None),
21802176
}
21812177
}
21822178
}

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)