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
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ plotters = { version = "0.3", default-features = false }
plotters-backend = "0.3"
iced_widget = { version = "0.14", features = ["canvas"] }
iced_graphics = "0.14"
once_cell = "1"

[dev-dependencies]
plotters = { version = "0.3", default-features = false, features = [
Expand Down
23 changes: 12 additions & 11 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use iced_widget::{
text::Alignment,
text::Shaping,
};
use once_cell::unsync::Lazy;
use plotters_backend::{
//FontTransform,
BackendColor,
Expand All @@ -28,6 +27,7 @@ use plotters_backend::{
text_anchor,
};
use std::collections::HashSet;
use std::sync::{LazyLock, Mutex};

/// The Iced drawing backend
pub(crate) struct IcedChartBackend<'a, B> {
Expand Down Expand Up @@ -302,25 +302,26 @@ where
}
}

#[allow(static_mut_refs)]
fn style_to_font<S: BackendTextStyle>(style: &S) -> Font {
// iced font family requires static str
static mut FONTS: Lazy<HashSet<String>> = Lazy::new(HashSet::new);
static FONTS: LazyLock<Mutex<HashSet<&'static str>>> =
LazyLock::new(|| Mutex::new(HashSet::new()));

Font {
family: match style.family() {
FontFamily::Serif => font::Family::Serif,
FontFamily::SansSerif => font::Family::SansSerif,
FontFamily::Monospace => font::Family::Monospace,
FontFamily::Name(s) => {
let s = unsafe {
if !FONTS.contains(s) {
FONTS.insert(String::from(s));
FontFamily::Name(s) => FONTS.lock().map_or_else(
|_| font::Family::default(),
|mut vec_guard| {
if !vec_guard.contains(s) {
vec_guard.insert(String::from(s).leak());
}
FONTS.get(s).unwrap().as_str()
};
font::Family::Name(s)
}

font::Family::Name(*vec_guard.get(s).unwrap())
},
),
},
weight: match style.style() {
FontStyle::Bold => font::Weight::Bold,
Expand Down