Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Set kern group #244

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
16 changes: 16 additions & 0 deletions runebender-lib/src/app_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ impl AppDelegate<AppState> for Delegate {
{
data.workspace.rename_glyph(old.clone(), new.clone());
Handled::Yes
} else if let Some(consts::cmd::SetGlyphKernGroupArgs {
glyph_name,
group_key,
}) = cmd.get(consts::cmd::SET_GLYPH_KERN1_GROUP)
{
data.workspace
.set_glyph_group("public.kern1.", glyph_name.clone(), group_key.clone());
Handled::Yes
} else if let Some(consts::cmd::SetGlyphKernGroupArgs {
glyph_name,
group_key,
}) = cmd.get(consts::cmd::SET_GLYPH_KERN2_GROUP)
{
data.workspace
.set_glyph_group("public.kern2.", glyph_name.clone(), group_key.clone());
Handled::Yes
} else if cmd.is(consts::cmd::NEW_PREVIEW_WINDOW) {
let session_id = data.workspace.new_preview_session();
let new_win = WindowDesc::new(make_preview(session_id))
Expand Down
14 changes: 14 additions & 0 deletions runebender-lib/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ pub mod cmd {
pub new: GlyphName,
}

/// Change the kern1 group of a glyph.
pub const SET_GLYPH_KERN1_GROUP: Selector<SetGlyphKernGroupArgs> =
Selector::new("runebender.set-glyph-kern1-group");

/// Change the kern2 group of a glyph.
pub const SET_GLYPH_KERN2_GROUP: Selector<SetGlyphKernGroupArgs> =
Selector::new("runebender.set-glyph-kern2-group");

/// Arguments passed with the SET_GLYPH_KERN1_GROUP AND SET_GLYPH_KERN2_GROUP commands.
pub struct SetGlyphKernGroupArgs {
pub glyph_name: GlyphName,
pub group_key: String,
}

/// sent by the 'add component' menu item
pub const ADD_COMPONENT: Selector = Selector::new("runebender.add-component");

Expand Down
58 changes: 58 additions & 0 deletions runebender-lib/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ pub struct GlyphDetail {
// the full outline, including things like components
pub outline: Arc<BezPath>,
metrics: FontMetrics,
pub kern1_group: String,
pub kern2_group: String,
is_placeholder: bool,
}

Expand Down Expand Up @@ -356,6 +358,33 @@ impl Workspace {
}
}

pub fn set_glyph_group(&mut self, prefix: &str, glyph_name: GlyphName, group_name: String) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This definitely reenforces my suspicion that we should find some better way to store these. 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, though it's hard to think of a good clean place to introduce yet another intermediate datastructure

let font = self.font_mut();
if let Some(mut groups) = font.ufo.groups.clone() {
let mut do_remove = Vec::<String>::new(); // BTreeMap retain is nightly for now
for (gk, gv) in groups.iter_mut() {
if let Some(gn) = gk.strip_prefix(prefix) {
if gn != group_name {
(*gv).retain(|n| *n != glyph_name);
if (*gv).len() == 0 {
do_remove.push(gk.clone());
}
}
}
}
for gk in do_remove {
groups.remove(&gk);
}
if group_name != "" {
let k = [prefix, &group_name].join("");
let entry = groups.entry(k).or_insert_with(Vec::<GlyphName>::new);
entry.push(glyph_name);
entry.sort();
}
font.ufo.groups = Some(groups);
}
}

pub fn update_glyph_metadata(&mut self, changed: &Arc<Glyph>) {
// update the active session, if one exists
if let Some(session_id) = self.session_map.get(&changed.name) {
Expand Down Expand Up @@ -599,6 +628,7 @@ mod lenses {
use std::sync::Arc;

use druid::{Data, Lens};
use norad::glyph::Glyph;
use norad::GlyphName as GlyphName_;

use super::{
Expand Down Expand Up @@ -722,10 +752,13 @@ mod lenses {
let outline = state.font.get_bezier(&glyph.name);
let is_placeholder = outline.is_none();
let metrics = state.font.info.metrics.clone();
let (kern1_group, kern2_group) = find_kern_groups(&state.font, &glyph);
GlyphDetail {
glyph,
outline: outline.unwrap_or_else(|| state.font.font.placeholder.clone()),
is_placeholder,
kern1_group,
kern2_group,
metrics,
}
}
Expand Down Expand Up @@ -762,10 +795,13 @@ mod lenses {
let outline = data.get_bezier(&glyph.name);
let is_placeholder = outline.is_none();
let metrics = data.info.metrics.clone();
let (kern1_group, kern2_group) = find_kern_groups(data, glyph);
GlyphDetail {
glyph: Arc::clone(glyph),
outline: outline.unwrap_or_else(|| data.font.placeholder.clone()),
metrics,
kern1_group,
kern2_group,
is_placeholder,
}
});
Expand All @@ -786,10 +822,13 @@ mod lenses {
let outline = data.get_bezier(&glyph.name);
let is_placeholder = outline.is_none();
let metrics = data.info.metrics.clone();
let (kern1_group, kern2_group) = find_kern_groups(data, glyph);
GlyphDetail {
glyph: Arc::clone(glyph),
outline: outline.unwrap_or_else(|| data.font.placeholder.clone()),
metrics,
kern1_group,
kern2_group,
is_placeholder,
}
});
Expand Down Expand Up @@ -911,6 +950,25 @@ mod lenses {
f(&mut s)
}
}

fn find_kern_groups(data: &Workspace, glyph: &Glyph) -> (String, String) {
let mut kern1_group = "".to_string();
let mut kern2_group = "".to_string();
if let Some(gs) = &data.font.ufo.groups {
for (gk, gv) in gs.iter() {
if let Some(gn) = gk.strip_prefix("public.kern1.") {
if (*gv).iter().any(|n| *n == glyph.name) {
kern1_group = gn.to_string();
}
} else if let Some(gn) = gk.strip_prefix("public.kern2.") {
if (*gv).iter().any(|n| *n == glyph.name) {
kern2_group = gn.to_string();
}
}
}
}
(kern1_group, kern2_group)
}
}

//FIXME: put this in some `GlyphExt` trait or something
Expand Down
4 changes: 2 additions & 2 deletions runebender-lib/src/widgets/sidebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ fn selected_glyph_widget() -> impl Widget<GlyphDetail> {
.with_child(
Flex::row()
.with_child(
Label::new("kern group")
Label::dynamic(|d: &GlyphDetail, _| (*d.kern2_group).to_string())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so getting the editing working should be a two step process:

  1. use EditableLabel:

    EditableLabel::new(
        |s: &String, _: &_| s.to_string(),
        |s| Some(s.to_string())
    )
    .lens(GlyphDetail::kern1_group)
  2. Add some controller that sends a message to rename the glyph when the data changes.

That should get this working, I think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I thought, though it involves writing two more dummy lenses in GlyphDetail I think.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that sounds right. I don't love it, and also, it works,

.with_text_color(theme::SECONDARY_TEXT_COLOR)
.with_font(theme::UI_DETAIL_FONT),
)
.with_flex_spacer(1.0)
.with_child(
Label::new("kern group")
Label::dynamic(|d: &GlyphDetail, _| (*d.kern1_group).to_string())
.with_text_color(theme::SECONDARY_TEXT_COLOR)
.with_font(theme::UI_DETAIL_FONT),
)
Expand Down