mirror of
https://github.com/storytold/figcraft.git
synced 2026-12-04 19:41:51 -05:00
Merge pull request #2668 from storytold/fix/2338-range-position
M4: superscript and subscript per range of characters (#2338)
This commit is contained in:
+48
-3
@@ -114,6 +114,17 @@ pub enum TextCase {
|
||||
SmallCaps,
|
||||
}
|
||||
|
||||
/// Where characters sit on the line: superscript (smaller and raised) or subscript (smaller and
|
||||
/// lowered), drawn with the font's `sups` / `subs` glyphs or synthesised without them.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum TextPosition {
|
||||
#[default]
|
||||
None,
|
||||
Superscript,
|
||||
Subscript,
|
||||
}
|
||||
|
||||
impl TextCase {
|
||||
pub fn apply(self, s: &str) -> String {
|
||||
self.apply_after(s, None)
|
||||
@@ -339,6 +350,9 @@ pub struct TextStyle {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub decoration_look: Option<Box<DecorationLook>>,
|
||||
pub case: TextCase,
|
||||
/// Superscript or subscript for these chars; `None` leaves the layer's `sups` / `subs`.
|
||||
#[serde(skip_serializing_if = "crate::node::is_default")]
|
||||
pub position: TextPosition,
|
||||
/// Run fills (None = node fills).
|
||||
pub fills: Option<Vec<Paint>>,
|
||||
/// Hyperlink.
|
||||
@@ -391,6 +405,7 @@ impl Default for TextStyle {
|
||||
decoration: TextDecoration::None,
|
||||
decoration_look: None,
|
||||
case: TextCase::Original,
|
||||
position: TextPosition::None,
|
||||
fills: None,
|
||||
link: None,
|
||||
variations: std::collections::BTreeMap::new(),
|
||||
@@ -416,10 +431,12 @@ impl TextStyle {
|
||||
|| self.variations.get("slnt").is_some_and(|v| *v != 0.0)
|
||||
}
|
||||
|
||||
/// Take what text style `def` sets (font, size, line height, letter spacing, decoration, case
|
||||
/// and axis values) and link to it as `id`; fills and hyperlinks stay.
|
||||
/// Take what text style `def` sets (font, size, line height, letter spacing, decoration, case,
|
||||
/// a position when it has one, and axis values) and link to it as `id`; fills, hyperlinks and
|
||||
/// a position the style doesn't set stay.
|
||||
pub fn take_text_style(&mut self, def: &TextStyle, id: &str) {
|
||||
*self = TextStyle { fills: self.fills.take(), link: self.link.take(), style_id: Some(id.to_string()), ..def.clone() };
|
||||
let position = if def.position == TextPosition::None { self.position } else { def.position };
|
||||
*self = TextStyle { fills: self.fills.take(), link: self.link.take(), style_id: Some(id.to_string()), position, ..def.clone() };
|
||||
}
|
||||
|
||||
/// Whether these chars show what text style `def` sets.
|
||||
@@ -432,6 +449,7 @@ impl TextStyle {
|
||||
&& self.decoration == def.decoration
|
||||
&& self.decoration_look == def.decoration_look
|
||||
&& self.case == def.case
|
||||
&& (def.position == TextPosition::None || self.position == def.position)
|
||||
&& self.variations == def.variations
|
||||
}
|
||||
|
||||
@@ -719,6 +737,33 @@ impl TextData {
|
||||
};
|
||||
}
|
||||
|
||||
/// Where chars styled `s` sit: their own position, else the layer's `sups` / `subs` (`sups`
|
||||
/// first, as drawn).
|
||||
pub fn position_of(&self, s: &TextStyle) -> TextPosition {
|
||||
let on = |tag: &str| self.features.get(tag).copied().unwrap_or(false);
|
||||
match s.position {
|
||||
TextPosition::None if on("sups") => TextPosition::Superscript,
|
||||
TextPosition::None if on("subs") => TextPosition::Subscript,
|
||||
p => p,
|
||||
}
|
||||
}
|
||||
|
||||
/// Set chars `start..end` to `position`. The layer's `sups` / `subs` move onto its chars
|
||||
/// first, so a range set to none is drawn on the line.
|
||||
pub fn set_position(&mut self, start: usize, end: usize, position: TextPosition) {
|
||||
let layer = self.position_of(&TextStyle::default());
|
||||
self.features.remove("sups");
|
||||
self.features.remove("subs");
|
||||
if layer != TextPosition::None {
|
||||
self.restyle(0, self.char_count(), |s| {
|
||||
if s.position == TextPosition::None {
|
||||
s.position = layer;
|
||||
}
|
||||
});
|
||||
}
|
||||
self.restyle(start, end, |s| s.position = position);
|
||||
}
|
||||
|
||||
/// The style at char index `i`.
|
||||
pub fn style_at(&self, i: usize) -> &TextStyle {
|
||||
self.runs.iter().find(|r| r.start <= i && i < r.end).map(|r| &r.style).unwrap_or(&self.style)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
//! Document inspection as JSON, so agents can verify their work without screenshots.
|
||||
|
||||
use figcraft_doc::{Document, Node, NodeId, NodeKind, StrokeCap, StrokeJoin, TextAlignV, TextCase, TextData, TextDecoration};
|
||||
use figcraft_doc::{
|
||||
Document, Node, NodeId, NodeKind, StrokeCap, StrokeJoin, TextAlignV, TextCase, TextData, TextDecoration, TextPosition, TextStyle,
|
||||
};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::Session;
|
||||
@@ -111,6 +113,7 @@ pub const NODE_KEYS: &[&str] = &[
|
||||
"textDecorationThickness",
|
||||
"textDirection",
|
||||
"textPath",
|
||||
"textPosition",
|
||||
"textStyleRanges",
|
||||
"type",
|
||||
"variableModes",
|
||||
@@ -448,9 +451,7 @@ fn text_props(t: &TextData, o: &mut serde_json::Map<String, Value>, options: &In
|
||||
insert_prop(o, options, "textAlignVertical", || json!(t.align_v));
|
||||
}
|
||||
insert_prop(o, options, "textAutoResize", || json!(t.auto_resize));
|
||||
if t.style.case != TextCase::default() {
|
||||
insert_prop(o, options, "textCase", || json!(t.style.case));
|
||||
}
|
||||
case_props(&t.style, o, options);
|
||||
for (k, v) in [("paragraphSpacing", t.paragraph_spacing), ("paragraphIndent", t.paragraph_indent)] {
|
||||
if v != 0.0 {
|
||||
insert_prop(o, options, k, || json!(v));
|
||||
@@ -529,6 +530,16 @@ fn text_props(t: &TextData, o: &mut serde_json::Map<String, Value>, options: &In
|
||||
}
|
||||
}
|
||||
|
||||
/// The base style's case and position, when not the defaults.
|
||||
fn case_props(s: &TextStyle, o: &mut serde_json::Map<String, Value>, options: &InspectOptions) {
|
||||
if s.case != TextCase::default() {
|
||||
insert_prop(o, options, "textCase", || json!(s.case));
|
||||
}
|
||||
if s.position != TextPosition::None {
|
||||
insert_prop(o, options, "textPosition", || json!(s.position));
|
||||
}
|
||||
}
|
||||
|
||||
fn kind_props(doc: &Document, n: &Node, o: &mut serde_json::Map<String, Value>, options: &InspectOptions) {
|
||||
match &n.kind {
|
||||
NodeKind::Ellipse { start, sweep, inner } if (*sweep).abs() < std::f64::consts::TAU - 1e-9 || *inner > 0.0 => {
|
||||
|
||||
@@ -4,7 +4,7 @@ use figcraft_color::{Color, Paint};
|
||||
use figcraft_doc::{
|
||||
AutoLayout, Brush, ChildLayout, DecorationStyle, DevStatus, Document, Effect, GridTrack, LayoutMode, LetterSpacing, LineHeight, ListStyle,
|
||||
MAX_LIST_INDENT, MaskType, Node, NodeId, NodeKind, Sizing, StrokeCap, StrokeProps, TextAutoResize, TextCase, TextData, TextDecoration,
|
||||
WidthPoint, WidthPreset, WidthProfile, style_weight, valid_axis_tag, valid_feature_tag,
|
||||
TextPosition, WidthPoint, WidthPreset, WidthProfile, style_weight, valid_axis_tag, valid_feature_tag,
|
||||
};
|
||||
use figcraft_geom::{Affine, Point};
|
||||
use serde_json::{Map, Value, json};
|
||||
@@ -980,6 +980,7 @@ fn apply_text(cmd: &str, doc: &mut Document, id: NodeId, props: &Map<String, Val
|
||||
| "letterSpacing"
|
||||
| "textDecoration"
|
||||
| "textCase"
|
||||
| "textPosition"
|
||||
| "hyperlink"
|
||||
| "textFills"
|
||||
| "fontVariations"
|
||||
@@ -1402,6 +1403,10 @@ pub fn apply_text_style(cmd: &str, t: &mut TextData, start: usize, end: usize, k
|
||||
let c: TextCase = enum_of(v).ok_or_else(|| bad(cmd, "textCase: original|upper|lower|title|smallCaps"))?;
|
||||
t.restyle(start, end, |s| s.case = c);
|
||||
}
|
||||
"textPosition" => {
|
||||
let p: TextPosition = enum_of(v).ok_or_else(|| bad(cmd, "textPosition: none|superscript|subscript"))?;
|
||||
t.set_position(start, end, p);
|
||||
}
|
||||
"hyperlink" => {
|
||||
let l = string_val(v);
|
||||
t.restyle(start, end, |s| s.link = l.clone());
|
||||
|
||||
@@ -604,7 +604,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
spec!(
|
||||
"text.style",
|
||||
"Style text range",
|
||||
"{id?, start?, end?, fontFamily?, fontStyle?, fontWeight?, fontVariations? | variationSettings?, fontSize?, lineHeight?, letterSpacing?, textDecoration?, textDecorationStyle?, textDecorationThickness?, textDecorationOffset?, textDecorationSkipInk?, textDecorationColor?, textCase?, hyperlink?, textFills?}",
|
||||
"{id?, start?, end?, fontFamily?, fontStyle?, fontWeight?, fontVariations? | variationSettings?, fontSize?, lineHeight?, letterSpacing?, textDecoration?, textDecorationStyle?, textDecorationThickness?, textDecorationOffset?, textDecorationSkipInk?, textDecorationColor?, textCase?, textPosition? (none|superscript|subscript), hyperlink?, textFills?}",
|
||||
true,
|
||||
|s, p| {
|
||||
let ids = req_targets("text.style", s, p)?;
|
||||
|
||||
@@ -392,6 +392,11 @@ fn text_decls(doc: &Document, n: &Node, tx: &figcraft_doc::TextData, t: &mut Tok
|
||||
TextDecoration::Underline => put("text-decoration", "underline".into()),
|
||||
TextDecoration::Strikethrough => put("text-decoration", "line-through".into()),
|
||||
}
|
||||
match tx.position_of(s) {
|
||||
figcraft_doc::TextPosition::None => {}
|
||||
figcraft_doc::TextPosition::Superscript => put("vertical-align", "super".into()),
|
||||
figcraft_doc::TextPosition::Subscript => put("vertical-align", "sub".into()),
|
||||
}
|
||||
decoration_decls(s, put);
|
||||
if tx.leading_trim == figcraft_doc::LeadingTrim::CapHeight {
|
||||
put("text-box", "trim-both cap alphabetic".into());
|
||||
|
||||
@@ -710,6 +710,62 @@ fn superscript_and_subscript_are_synthesised_without_glyphs_for_them() {
|
||||
assert_eq!(glyphs(&s), plain, "off again");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_range_of_characters_is_raised_or_lowered_on_its_own() {
|
||||
// #2338: a footnote marker, "ab(1)" with only "(1)" raised, through `text.style`.
|
||||
let font = test_mono_font();
|
||||
let mut s = Session::new();
|
||||
let t = text(&mut s, "ab(1)", json!({"fontFamily": font, "fontSize": 40}));
|
||||
let glyphs = |s: &Session| -> Vec<(f64, f64)> {
|
||||
let (l, _) = figcraft_text::layout_node(s.doc().unwrap().node(t).unwrap()).unwrap();
|
||||
l.glyphs.iter().map(|g| (g.size, g.y)).collect()
|
||||
};
|
||||
let raised = |g: &[(f64, f64)], p: &[(f64, f64)]| g.len() == p.len() && g.iter().zip(p).all(|(g, p)| g.0 < p.0 && g.1 < p.1);
|
||||
let plain = glyphs(&s);
|
||||
assert_eq!(plain.len(), 5);
|
||||
s.execute("text.style", &json!({"id": t, "start": 2, "end": 5, "textPosition": "superscript"})).unwrap();
|
||||
let g = glyphs(&s);
|
||||
assert_eq!(g[..2], plain[..2], "the chars before stay on the line");
|
||||
assert!(raised(&g[2..], &plain[2..]), "smaller and raised: {g:?} against {plain:?}");
|
||||
s.execute("text.style", &json!({"id": t, "start": 2, "end": 5, "textPosition": "subscript"})).unwrap();
|
||||
let g = glyphs(&s);
|
||||
assert!(g[2..].iter().zip(&plain[2..]).all(|(g, p)| g.0 < p.0 && g.1 > p.1), "smaller and lowered: {g:?} against {plain:?}");
|
||||
|
||||
// The layer's `sups` move onto its chars, so a range set to none sits on the line.
|
||||
s.execute("text.style", &json!({"id": t, "textPosition": "none"})).unwrap();
|
||||
assert_eq!(glyphs(&s), plain);
|
||||
s.execute("text.setFeatures", &json!({"ids": [t], "features": {"sups": true}})).unwrap();
|
||||
s.execute("text.style", &json!({"id": t, "start": 0, "end": 2, "textPosition": "none"})).unwrap();
|
||||
let g = glyphs(&s);
|
||||
assert_eq!(g[..2], plain[..2], "set to none despite the layer's sups");
|
||||
assert!(raised(&g[2..], &plain[2..]), "the rest still raised: {g:?} against {plain:?}");
|
||||
assert!(s.doc().unwrap().node(t).unwrap().kind.text().unwrap().features.is_empty(), "the chars hold the position now");
|
||||
|
||||
// A text style keeps the position and raises the chars it's applied to.
|
||||
let u = text(&mut s, "ab(1)", json!({"fontFamily": font, "fontSize": 40}));
|
||||
s.execute("style.create", &json!({"type": "text", "name": "Note", "value": {"fontFamily": font, "fontSize": 40, "position": "superscript"}}))
|
||||
.unwrap();
|
||||
s.execute("style.apply", &json!({"ids": [u], "style": "Note", "start": 2, "end": 5})).unwrap();
|
||||
let (l, _) = figcraft_text::layout_node(s.doc().unwrap().node(u).unwrap()).unwrap();
|
||||
let g: Vec<(f64, f64)> = l.glyphs.iter().map(|g| (g.size, g.y)).collect();
|
||||
assert_eq!(g[..2], plain[..2]);
|
||||
assert!(raised(&g[2..], &plain[2..]), "the style's chars raised: {g:?} against {plain:?}");
|
||||
|
||||
// A style that sets no position leaves the chars' own, and they stay linked to it when the
|
||||
// layer's `sups` move onto them.
|
||||
let v = text(&mut s, "ab(1)", json!({"fontFamily": font, "fontSize": 40}));
|
||||
s.execute("style.create", &json!({"type": "text", "name": "Body", "value": {"fontFamily": font, "fontSize": 40}})).unwrap();
|
||||
s.execute("style.apply", &json!({"ids": [v], "style": "Body"})).unwrap();
|
||||
s.execute("text.setFeatures", &json!({"ids": [v], "features": {"sups": true}})).unwrap();
|
||||
s.execute("text.style", &json!({"id": v, "start": 0, "end": 2, "textPosition": "none"})).unwrap();
|
||||
let linked = |s: &Session| s.doc().unwrap().node(v).unwrap().kind.text().unwrap().spans().iter().all(|(_, _, st)| st.style_id.is_some());
|
||||
assert!(linked(&s), "still linked to Body");
|
||||
s.execute("style.apply", &json!({"ids": [v], "style": "Body"})).unwrap();
|
||||
let (l, _) = figcraft_text::layout_node(s.doc().unwrap().node(v).unwrap()).unwrap();
|
||||
let g: Vec<(f64, f64)> = l.glyphs.iter().map(|g| (g.size, g.y)).collect();
|
||||
assert!(g[..2] == plain[..2] && raised(&g[2..], &plain[2..]), "applying Body again keeps the positions: {g:?}");
|
||||
}
|
||||
|
||||
/// A synthetic monospaced TrueType font (outline-less glyphs, every printable ASCII character 500 of
|
||||
/// 1000 units wide), loaded into the font database so text measures the same on every machine.
|
||||
fn test_mono_font() -> &'static str {
|
||||
|
||||
@@ -176,6 +176,9 @@ impl<'d> Exporter<'d> {
|
||||
if !td.features.is_empty() {
|
||||
self.warn("OpenType features were left out");
|
||||
}
|
||||
if styles.clone().any(|st| st.position != figcraft_doc::TextPosition::None) {
|
||||
self.warn("superscript and subscript were left out");
|
||||
}
|
||||
}
|
||||
|
||||
/// The editable network (with per-point corner radii, handle mirroring and end caps) and,
|
||||
|
||||
@@ -6,7 +6,7 @@ use std::sync::Arc;
|
||||
|
||||
use figcraft_doc::{
|
||||
DecorationLength, DecorationStyle, LeadingTrim, LetterSpacing, LineHeight, ListStyle, ParaList, TextAlignH, TextAutoResize, TextCase, TextData,
|
||||
TextDecoration, TextDirection, TextStyle, WrapStyle, color::PaintKind, valid_feature_tag,
|
||||
TextDecoration, TextDirection, TextPosition, TextStyle, WrapStyle, color::PaintKind, valid_feature_tag,
|
||||
};
|
||||
use figcraft_geom::{Affine, BezPath, Rect};
|
||||
use harfrust::{Direction, Feature, ShapeOptions, ShaperInstance, Tag, UnicodeBuffer, Variation};
|
||||
@@ -1708,6 +1708,16 @@ fn shape_segment(chars: &[char], seg: &Segment, face: &Arc<Face>, features: &[Fe
|
||||
}
|
||||
let small_at = |src: usize| src.checked_sub(start).and_then(|i| small.get(i)).copied().unwrap_or(false);
|
||||
let mut feats: Vec<Feature> = features.to_vec();
|
||||
// These chars' own position wins over the layer's `sups` / `subs` (#2338).
|
||||
let run_script = match style.position {
|
||||
TextPosition::None => None,
|
||||
TextPosition::Superscript => Some(b"sups"),
|
||||
TextPosition::Subscript => Some(b"subs"),
|
||||
};
|
||||
if let Some(tag) = run_script {
|
||||
feats.retain(|f| f.tag != Tag::new(b"sups") && f.tag != Tag::new(b"subs"));
|
||||
feats.push(Feature::new(Tag::new(tag), 1, ..));
|
||||
}
|
||||
if native_smcp {
|
||||
feats.push(Feature::new(Tag::new(b"smcp"), 1, ..));
|
||||
}
|
||||
|
||||
@@ -210,7 +210,7 @@ pub(crate) fn field_row<R>(ui: &mut Ui, label: &str, add: impl FnOnce(&mut Ui, f
|
||||
#[cfg(test)]
|
||||
mod variable_tests {
|
||||
use egui::{Event, Modifiers, PointerButton, Pos2, RawInput};
|
||||
use figcraft_doc::WrapStyle;
|
||||
use figcraft_doc::{TextPosition, WrapStyle};
|
||||
use figcraft_engine::Session;
|
||||
|
||||
use super::*;
|
||||
@@ -432,7 +432,8 @@ mod variable_tests {
|
||||
|
||||
#[test]
|
||||
fn details_position_sets_superscript_subscript_or_neither() {
|
||||
// #2338: Type settings ▸ Details ▸ Position, for the selected layer.
|
||||
// #2338: Type settings ▸ Details ▸ Position, for the selected layer, then for the selected
|
||||
// chars while editing.
|
||||
let mut p = settings_panel();
|
||||
// The tab is a clickable label: its text is the node's value.
|
||||
let tab = p.nodes.iter().find(|n| n.value() == Some("Details") || n.label() == Some("Details")).expect("the Details tab");
|
||||
@@ -443,18 +444,30 @@ mod variable_tests {
|
||||
let toggled = |l: &str| p.nodes.iter().find(|n| n.label() == Some(l)).and_then(|n| n.toggled());
|
||||
["None", "Superscript", "Subscript"].into_iter().filter(|l| toggled(l) == Some(egui::accesskit::Toggled::True)).collect::<Vec<_>>()
|
||||
};
|
||||
let feats = |p: &Panel| {
|
||||
let t = p.app.session.doc().unwrap().node(p.text).unwrap().kind.text().unwrap().features.clone();
|
||||
(t.get("sups").copied(), t.get("subs").copied())
|
||||
let positions = |p: &Panel| {
|
||||
let t = p.app.session.doc().unwrap().node(p.text).unwrap().kind.text().unwrap().clone();
|
||||
t.spans().into_iter().map(|(a, b, st)| (a, b, t.position_of(st))).collect::<Vec<_>>()
|
||||
};
|
||||
assert_eq!(on(&p), ["None"]);
|
||||
for (label, want) in [("Superscript", (Some(true), None)), ("Subscript", (None, Some(true))), ("None", (None, None))] {
|
||||
for (label, want) in [("Superscript", TextPosition::Superscript), ("Subscript", TextPosition::Subscript), ("None", TextPosition::None)] {
|
||||
let at = node_center(&p, label);
|
||||
p.click(at);
|
||||
p.frame(vec![]);
|
||||
assert_eq!(feats(&p), want, "{label}");
|
||||
assert_eq!(positions(&p), [(0, 7, want)], "{label}");
|
||||
assert_eq!(on(&p), [label]);
|
||||
}
|
||||
// While editing, the selected chars ("two") and nothing else.
|
||||
figcraft_engine::textedit::begin(&mut p.app.session, p.text, Some(4), false).unwrap();
|
||||
figcraft_engine::textedit::set_caret(&mut p.app.session, 7, true).unwrap();
|
||||
p.frame(vec![]);
|
||||
let at = node_center(&p, "Superscript");
|
||||
p.click(at);
|
||||
assert_eq!(positions(&p), [(0, 4, TextPosition::None), (4, 7, TextPosition::Superscript)]);
|
||||
assert_eq!(on(&p), ["Superscript"], "the control shows the selected chars' position");
|
||||
figcraft_engine::textedit::set_caret(&mut p.app.session, 0, false).unwrap();
|
||||
figcraft_engine::textedit::set_caret(&mut p.app.session, 7, true).unwrap();
|
||||
p.frame(vec![]);
|
||||
assert!(on(&p).is_empty(), "chars that differ: {:?}", on(&p));
|
||||
assert!(p.app.ui.last_error.is_none(), "{:?}", p.app.ui.last_error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -469,7 +469,7 @@ fn type_settings(
|
||||
let face = uniform(styles, |s| (s.font_family.clone(), s.font_style.clone())).and_then(|(f, s)| figcraft_text::find_face(&f, &s));
|
||||
let face_name = face.as_ref().map(|f| format!("{} {}", f.family, f.style)).unwrap_or_else(|| tr("Mixed fonts").into());
|
||||
widgets::label(ui, &face_name, t.text_secondary);
|
||||
position_row(app, ui, doc, ids, w);
|
||||
position_row(app, ui, doc, ids, apply_style, w);
|
||||
egui::ScrollArea::vertical().max_height(320.0).show(ui, |ui| {
|
||||
for (tag, label, default_on) in FEATURES {
|
||||
let state = common(doc, ids, |n| n.kind.text().map(|t| t.features.get(tag).copied())).flatten();
|
||||
@@ -486,17 +486,22 @@ fn type_settings(
|
||||
}
|
||||
}
|
||||
|
||||
/// Position: the layers' text as superscript or subscript (`sups`, `subs`; drawn smaller and
|
||||
/// raised or lowered where the font has no glyphs for them), or neither. `sups` wins when a layer
|
||||
/// has both, as when it is drawn (#2338).
|
||||
fn position_row(app: &mut FigApp, ui: &mut Ui, doc: &Document, ids: &[NodeId], w: f32) {
|
||||
/// Position: superscript or subscript (drawn smaller and raised or lowered where the font has no
|
||||
/// glyphs for them), or neither, for the selected chars while editing, else whole layers (#2338).
|
||||
fn position_row(app: &mut FigApp, ui: &mut Ui, doc: &Document, ids: &[NodeId], apply_style: &dyn Fn(&mut FigApp, &str, Value), w: f32) {
|
||||
caption(ui, tr("Position"));
|
||||
let on = |t: &figcraft_doc::TextData, tag: &str| t.features.get(tag).copied().unwrap_or(false);
|
||||
let active = common(doc, ids, |n| n.kind.text().map(|t| if on(t, "sups") { 1 } else { usize::from(on(t, "subs")) * 2 })).flatten();
|
||||
let range = figcraft_engine::textedit::selected_range(&app.session);
|
||||
let active = common(doc, ids, |n| {
|
||||
let t = n.kind.text()?;
|
||||
let sel = range.filter(|r| r.0 == n.id && r.1 < r.2);
|
||||
let mut at = t.spans().into_iter().filter(|(s, e, _)| sel.is_none_or(|(_, a, b)| *s < b && *e > a)).map(|(_, _, st)| t.position_of(st));
|
||||
let first = at.next()?;
|
||||
at.all(|p| p == first).then_some(first as usize)
|
||||
})
|
||||
.flatten();
|
||||
let items = [("—", tr_noop("None")), ("X²", tr_noop("Superscript")), ("X₂", tr_noop("Subscript"))];
|
||||
if let Some(i) = text_seg(ui, "position", &items, active, w) {
|
||||
let want = |k: usize| if i == k { json!(true) } else { Value::Null };
|
||||
app.run("text.setFeatures", json!({"ids": ids, "features": {"sups": want(1), "subs": want(2)}}));
|
||||
apply_style(app, "textPosition", json!((["none", "superscript", "subscript"].get(i).copied().unwrap_or("none"))));
|
||||
}
|
||||
ui.add_space(4.0);
|
||||
}
|
||||
|
||||
@@ -321,6 +321,9 @@ Or in a project's `.mcp.json`:
|
||||
`textDecorationOffset` (`auto`, px, or `"10%"` of the font size; the offset runs from the baseline
|
||||
down to an underline's top edge), `textDecorationSkipInk` (underlines break around descenders) and
|
||||
`textDecorationColor` (a colour, or `auto` for the text's) are character props too.
|
||||
`textPosition` (`none`, `superscript`, `subscript`) is one as well, so `text.style` with
|
||||
`start`/`end` raises or lowers only those chars; it wins over the layer's `sups` / `subs`
|
||||
features, which move onto the chars the first time a position is set.
|
||||
- **Wrap style** (`auto`, `balance`, `pretty`) works at three levels: `wrapStyle` on `node.set`
|
||||
sets the layer (every paragraph); with `start`/`end` it sets the paragraphs those chars touch,
|
||||
and a paragraph's own style beats the layer's. `text.wrapStyle` sets the selected paragraphs while
|
||||
|
||||
+3
-2
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user