From b68ea5f65f679f5e97a2c62bb962d7215c8548ad Mon Sep 17 00:00:00 2001 From: bflatastic Date: Sat, 21 Nov 2026 21:27:22 +0700 Subject: [PATCH] type-w3: net change of 1 commits - type-w3: repair 1 --- crates/font-manager/src/axes/button.rs | 40 +- .../font-manager/src/axes/button/stories.rs | 38 ++ crates/text/src/ui/character.rs | 508 ++++++++++++------ crates/text/src/ui/character/paragraph.rs | 52 +- crates/text/src/ui/character/rows.rs | 150 +++++- crates/text/src/ui/character/stories.rs | 100 +++- 6 files changed, 689 insertions(+), 199 deletions(-) create mode 100644 crates/font-manager/src/axes/button/stories.rs diff --git a/crates/font-manager/src/axes/button.rs b/crates/font-manager/src/axes/button.rs index ad360071f..55c653132 100644 --- a/crates/font-manager/src/axes/button.rs +++ b/crates/font-manager/src/axes/button.rs @@ -1,16 +1,25 @@ //! The axes behind one button: a `Button` that opens the axis sliders in a popover. -use craft_ui::contract::{Bind, Pattern, Props}; +#[cfg(feature = "screen-testing")] +mod stories; + +use craft_ui::buttons::Button; +use craft_ui::contract::{Bind, Pattern, PopupKind, Props, Remember as _, Show as _, ShowContainer as _, clicked}; +use craft_ui::layout_components::Stack; +use craft_ui::popover::Popover; use craft_ui::runtime::env::Label; use egui::Ui; -use super::{AxisSlidersOutput, AxisValues, FontAxis, NamedInstance}; +use super::{AxisSliders, AxisSlidersOutput, AxisValues, FontAxis, NamedInstance}; + +/// Whether the popover of the sliders is open. +#[derive(Clone, Default)] +struct Open(bool); /// A `Button` with the label that opens an anchored `Popover` holding `AxisSliders` over the same props. The open /// state is the button's own memory. The output is the sliders' (`edit`, `instance`), with the button's response. #[must_use = "show the button with `show`"] #[derive(Pattern, Props)] -#[allow(dead_code)] // P3(type-w3): ty.X21 pub struct AxesButton<'a> { label: Label, axes: &'a [FontAxis], @@ -26,7 +35,6 @@ pub struct AxesButton<'a> { italic: bool, /// The axes (by tag) the selection mixes: each shows `Mixed` and is left out of the edit's held values. #[prop] - #[allow(dead_code)] // P3(type-w3): ty.X21 mixed: &'a [[u8; 4]], } @@ -38,7 +46,27 @@ impl<'a> AxesButton<'a> { /// Draws the button and, while it is open, the sliders; reports the sliders' edit made this pass. pub fn show(self, ui: &mut Ui) -> AxisSlidersOutput { - let _ = ui; - todo!("ty.X21") + let AxesButton { label, axes, mut values, instances, weight, italic, mixed } = self; + let mut edit = Default::default(); + let mut instance = None; + let shown = Stack::new().show(ui, |ui, cx| { + let mut open = cx.memory::().0; + let button = Button::new(label.clone()).popup(PopupKind::Dialog).expanded(open).show(ui).response; + if clicked(&button) { + open = !open; + } + let _ = Popover::new(label.clone(), Bind::bound(&mut open)).opened_by(&button).show(ui, |ui, _| { + let mut sliders = AxisSliders::new(label.clone(), axes, values.reborrowed()).instances(instances).italic(italic).mixed(mixed); + if let Some(weight) = weight { + sliders = sliders.weight(weight); + } + let out = sliders.show(ui); + edit = out.edit; + instance = out.instance; + }); + cx.memory::().0 = open; + button + }); + AxisSlidersOutput { response: shown.inner.unwrap_or(shown.output), edit, instance } } } diff --git a/crates/font-manager/src/axes/button/stories.rs b/crates/font-manager/src/axes/button/stories.rs new file mode 100644 index 000000000..9823e3ccd --- /dev/null +++ b/crates/font-manager/src/axes/button/stories.rs @@ -0,0 +1,38 @@ +//! Stories of `AxesButton`: the button closed, and its popover of sliders opened by the button. + +use craft_ui::contract::Bind; +use craft_ui::testing::{Drive, Fixture, StoryData, StoryOutput, story}; +use egui::Ui; + +use super::AxesButton; +use crate::axes::{AxisValues, FontAxis, NamedInstance}; + +const WGHT: [u8; 4] = *b"wght"; +const WDTH: [u8; 4] = *b"wdth"; +const LABEL: &str = "Axes"; + +fn demo(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { + let axes = [FontAxis::new(WGHT, "Weight", 100.0, 400.0, 900.0), FontAxis::new(WDTH, "Width", 75.0, 100.0, 100.0)]; + let instance = |name: &str, weight: f32, width: f32| NamedInstance::new(name, vec![(WGHT, weight), (WDTH, width)]); + let instances = [instance("Light", 300.0, 100.0), instance("Regular", 400.0, 100.0), instance("Bold Condensed", 700.0, 75.0)]; + let values: &mut AxisValues = data.value("values", vec![(WGHT, 400.0), (WDTH, 100.0)]); + AxesButton::new(LABEL, &axes, Bind::bound(values)).instances(&instances).show(ui).into() +} + +#[story(AxesButton<'static>, covers = [Default], drive = Drive::Never)] +fn axes_button_closed(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { + demo(ui, data) +} + +/// Clicks the button, which opens the popover. +fn open_button(fixture: &mut Fixture) { + if let Some(button) = fixture.by_label(LABEL) { + fixture.click(&button); + } + fixture.settle(); +} + +#[story(AxesButton<'static>, covers = [], drive = Drive::Custom(open_button))] +fn axes_button_open(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { + demo(ui, data) +} diff --git a/crates/text/src/ui/character.rs b/crates/text/src/ui/character.rs index 6b33ab85b..c491e90fc 100644 --- a/crates/text/src/ui/character.rs +++ b/crates/text/src/ui/character.rs @@ -8,7 +8,7 @@ #[cfg(feature = "font-picker")] use craft_font_manager::Catalog; #[cfg(feature = "font-picker")] -use craft_font_manager::axes::AxisSliders; +use craft_font_manager::axes::{AxesButton, AxisSliders}; #[cfg(feature = "font-picker")] use craft_font_manager::picker::{FamilySet, FilterLayout, FontFilters, FontPickerSettings, FontScript}; use std::ops::RangeInclusive; @@ -16,7 +16,7 @@ use std::ops::RangeInclusive; use std::sync::Arc; use crate::ui::length::DocUnit; -use crate::ui::type_options::TextAntiAlias; +use crate::ui::type_options::{TextAntiAlias, TypeOptionWords}; use craft_behavior::{ColumnWidth, EditEvent, IconId, Preview}; use craft_theme::{Rgba, Srgba}; use craft_ui::choice::Swatch; @@ -45,8 +45,8 @@ pub use paragraph_dialog::{ SpacingKind, SpecialIndent, }; use rows::{ - LengthRow, Lengths, PlainRow, Toggle, agree, family_field, hidden_side, leading_menu, leading_row, mark, mixed, number_change, number_presets, - plain, select_row, size_menu, string_entries, toggle_row, + LengthRow, Lengths, PlainRow, Toggle, agree, family_field_keyed, font_height_entries, hidden_side, leading_menu, leading_row_keyed, mark, mixed, + number_change, number_presets, plain, select_row, size_menu, string_entries, toggle_row, }; pub use style_options::{ STYLE_COLOR_KEY, StyleAttribute, StyleOptionValues, StyleOptionsChange, TextStyleOptions, TextStyleOptionsOutput, TextStyleOptionsWords, @@ -151,15 +151,8 @@ impl CharacterRows { pub const EAST_ASIAN: CharacterRows = CharacterRows(1 << 8); /// Horizontal and vertical diacritic positions. pub const MIDDLE_EASTERN: CharacterRows = CharacterRows(1 << 9); - /// The baseline-shift row. - pub const BASELINE: CharacterRows = CharacterRows(1 << 10); - /// The style toggles: Faux Bold and Italic, Superscript and Subscript. - pub const STYLE_TOGGLES: CharacterRows = CharacterRows(1 << 11); - /// The Properties panel's section: family, style, Variable Font, Size, Leading, Kerning, Tracking. - pub const PROPERTIES: CharacterRows = CharacterRows(1 << 12); /// The set `show` draws by default. - pub const PHOTOSHOP: CharacterRows = - CharacterRows(Self::COLOR.0 | Self::FAUX.0 | Self::SCALES.0 | Self::OPENTYPE.0 | Self::BASELINE.0 | Self::STYLE_TOGGLES.0); + pub const PHOTOSHOP: CharacterRows = CharacterRows(Self::COLOR.0 | Self::FAUX.0 | Self::SCALES.0 | Self::OPENTYPE.0); /// Whether every group in `rows` is on. pub const fn contains(self, rows: CharacterRows) -> bool { @@ -252,20 +245,120 @@ pub const TSUME_PRESETS: [f32; 5] = [0.0, 25.0, 50.0, 75.0, 100.0]; /// The diacritic position presets, thousandths of an em. pub const DIACRITIC_PRESETS: [f32; 7] = [-200.0, -100.0, -50.0, 0.0, 50.0, 100.0, 200.0]; /// The range of the rotation field, degrees. -#[allow(dead_code)] // P3(type-w3): ty.X34 const ROTATION_RANGE: RangeInclusive = -360.0..=360.0; /// The range of the Insert Aki fields, thousandths of an em. -#[allow(dead_code)] // P3(type-w3): ty.X34 const AKI_RANGE: RangeInclusive = 0.0..=1000.0; /// The range of the Tsume field, percent. -#[allow(dead_code)] // P3(type-w3): ty.X34 const TSUME_RANGE: RangeInclusive = 0.0..=100.0; /// The range of the diacritic position fields, thousandths of an em. -#[allow(dead_code)] // P3(type-w3): ty.X34 const DIACRITIC_RANGE: RangeInclusive = -1000.0..=1000.0; /// Two columns of the same share, for the rows of two fields. const TWO_COLUMNS: [ColumnWidth; 2] = [ColumnWidth::fill(1.0), ColumnWidth::fill(1.0)]; +/// A field of the rows' flow, in the order the rows are drawn. Cells flow two to a row; a full cell takes a row of its +/// own and ends the run of cells before it. +#[derive(Clone, Copy)] +enum Cell { + Size, + Leading, + FontHeight, + Kerning, + Tracking, + VerticalScale, + HorizontalScale, + BaselineShift, + Color, + Rotation, + AntiAlias, + AkiLeft, + AkiRight, + Tsume, + DiacriticX, + DiacriticY, +} + +/// A number field that is neither a length nor named: its mark, value, range and the change it makes. +struct PlainCell { + label: Label, + mark: &'static str, + value: Option, + range: RangeInclusive, + percent: bool, + presets: &'static [f32], + change: fn(f32) -> CharacterChange, +} + +impl Cell { + /// Every cell in drawing order. + const ALL: [Cell; 16] = [ + Cell::Size, + Cell::Leading, + Cell::FontHeight, + Cell::Kerning, + Cell::Tracking, + Cell::VerticalScale, + Cell::HorizontalScale, + Cell::BaselineShift, + Cell::Color, + Cell::Rotation, + Cell::AntiAlias, + Cell::AkiLeft, + Cell::AkiRight, + Cell::Tsume, + Cell::DiacriticX, + Cell::DiacriticY, + ]; + + /// Whether the cell takes a row of its own. + fn full(self) -> bool { + matches!(self, Cell::FontHeight | Cell::AntiAlias) + } + + /// Whether the groups `rows` turns on include the cell. + fn shown(self, rows: CharacterRows) -> bool { + match self { + Cell::Size | Cell::Leading | Cell::Kerning | Cell::Tracking => true, + Cell::FontHeight => rows.contains(CharacterRows::FONT_HEIGHT), + Cell::VerticalScale | Cell::HorizontalScale | Cell::BaselineShift => rows.contains(CharacterRows::SCALES), + Cell::Color => rows.contains(CharacterRows::COLOR), + Cell::Rotation => rows.contains(CharacterRows::ROTATION), + Cell::AntiAlias => rows.contains(CharacterRows::ANTI_ALIAS), + Cell::AkiLeft | Cell::AkiRight | Cell::Tsume => rows.contains(CharacterRows::EAST_ASIAN), + Cell::DiacriticX | Cell::DiacriticY => rows.contains(CharacterRows::MIDDLE_EASTERN), + } + } + + /// The field of a cell that is a plain number; `None` for the others. + fn plain(self, words: &CharacterWords, values: &CharacterValues) -> Option { + let cell = |label: &Label, + mark: &'static str, + value: Option, + range: RangeInclusive, + percent: bool, + presets: &'static [f32], + change: fn(f32) -> CharacterChange| PlainCell { label: label.clone(), mark, value, range, percent, presets, change }; + Some(match self { + Cell::VerticalScale => cell(&words.vertical_scale, "↕T", values.vertical_scale, SCALE_RANGE, true, &[], CharacterChange::VerticalScale), + Cell::HorizontalScale => { + cell(&words.horizontal_scale, "↔T", values.horizontal_scale, SCALE_RANGE, true, &[], CharacterChange::HorizontalScale) + } + Cell::Rotation => cell(&words.rotation, "↻", values.rotation, ROTATION_RANGE, false, &ROTATION_PRESETS, CharacterChange::Rotation), + Cell::AkiLeft => cell(&words.aki_left, "|A", values.aki_left, AKI_RANGE, false, &AKI_PRESETS, CharacterChange::AkiLeft), + Cell::AkiRight => cell(&words.aki_right, "A|", values.aki_right, AKI_RANGE, false, &AKI_PRESETS, CharacterChange::AkiRight), + Cell::Tsume => cell(&words.tsume, "|A|", values.tsume, TSUME_RANGE, true, &TSUME_PRESETS, CharacterChange::Tsume), + Cell::DiacriticX => { + cell(&words.diacritic_x, "Á↔", values.diacritic_x, DIACRITIC_RANGE, false, &DIACRITIC_PRESETS, CharacterChange::DiacriticX) + } + Cell::DiacriticY => { + cell(&words.diacritic_y, "Á↕", values.diacritic_y, DIACRITIC_RANGE, false, &DIACRITIC_PRESETS, CharacterChange::DiacriticY) + } + Cell::Size | Cell::Leading | Cell::FontHeight | Cell::Kerning | Cell::Tracking | Cell::BaselineShift | Cell::Color | Cell::AntiAlias => { + return None; + } + }) + } +} + /// The variable-font axes of the current face: what the axis sliders under the style row show. #[cfg(feature = "font-picker")] #[derive(Clone, Copy, Debug)] @@ -364,21 +457,21 @@ impl<'a> FontPickerSource<'a> { } /// The host's family sets, passed on to the family field's picker. - pub fn sets(self, sets: &'a [FamilySet<'a>]) -> Self { - let _ = sets; - todo!("ty.X31") + pub fn sets(mut self, sets: &'a [FamilySet<'a>]) -> Self { + self.sets = sets; + self } /// The scripts the picker's script row offers. - pub fn scripts(self, scripts: &'a [FontScript]) -> Self { - let _ = scripts; - todo!("ty.X31") + pub fn scripts(mut self, scripts: &'a [FontScript]) -> Self { + self.scripts = scripts; + self } /// The layout of the picker's filter area. - pub fn filter_layout(self, layout: FilterLayout) -> Self { - let _ = layout; - todo!("ty.X31") + pub fn filter_layout(mut self, layout: FilterLayout) -> Self { + self.filter_layout = layout; + self } } @@ -528,8 +621,7 @@ pub struct CharacterValues { pub diacritic_x: Option, /// Vertical diacritic position, thousandths of an em. pub diacritic_y: Option, - /// The pair kerning Metrics or Optical gives at the caret; while the kerning is one of them the field shows it in - /// parentheses, and typing a number sets `Manual`. + /// The pair kerning Metrics or Optical gives at the caret; typing a number in the kerning field sets `Manual`. pub auto_kerning: Option, /// The anti-aliasing method. pub anti_alias: Option, @@ -538,9 +630,37 @@ pub struct CharacterValues { } impl CharacterValues { + /// The axis values of the face, none while the selection has no variable axes. + fn held_axes(&self) -> &[([u8; 4], f32)] { + self.axes.as_deref().unwrap_or_default() + } + + /// The axes two selections share with equal values, and the tags of the others: an axis mixes where either + /// selection mixes it or the values differ. + fn mix_axes(&self, other: &Self) -> (Option>, Vec<[u8; 4]>) { + let shared: Vec<([u8; 4], f32)> = self + .held_axes() + .iter() + .filter(|(tag, value)| { + !self.mixed_axes.contains(tag) && !other.mixed_axes.contains(tag) && other.held_axes().contains(&(*tag, *value)) + }) + .copied() + .collect(); + let mut mixed: Vec<[u8; 4]> = Vec::new(); + let tags = self.held_axes().iter().map(|(tag, _)| tag).chain(other.held_axes().iter().map(|(tag, _)| tag)); + for tag in tags.chain(&self.mixed_axes).chain(&other.mixed_axes) { + if !mixed.contains(tag) && !shared.iter().any(|(shared, _)| shared == tag) { + mixed.push(*tag); + } + } + let axes = if self.axes.is_none() && other.axes.is_none() { None } else { Some(shared) }; + (axes, mixed) + } + /// The values of two selections together: each field stays where both are equal and becomes `None` where they /// differ. pub fn mixed_with(&self, other: &Self) -> Self { + let (axes, mixed_axes) = self.mix_axes(other); CharacterValues { family: agree(&self.family, &other.family), style: agree(&self.style, &other.style), @@ -560,8 +680,8 @@ impl CharacterValues { strikethrough: agree(&self.strikethrough, &other.strikethrough), opentype: self.opentype.iter().copied().filter(|tag| other.opentype.contains(tag)).collect(), language: agree(&self.language, &other.language), - axes: agree(&self.axes, &other.axes), - mixed_axes: self.mixed_axes.iter().chain(&other.mixed_axes).copied().collect(), // P3(type-w3): ty.X29 + axes, + mixed_axes, rotation: agree(&self.rotation, &other.rotation), aki_left: agree(&self.aki_left, &other.aki_left), aki_right: agree(&self.aki_right, &other.aki_right), @@ -672,8 +792,9 @@ impl Outcome for CharacterControlsOutput { /// The Character panel's controls, in Photoshop's rows: family; style; size and leading; kerning and tracking; /// vertical and horizontal scale; baseline shift and colour; the style toggles (Faux Bold, Faux Italic, All Caps, -/// Small Caps, then Superscript and Subscript when [`superscript`](Self::superscript) is on, Underline, -/// Strikethrough); the OpenType buttons; the language when [`languages`](Self::languages) is given. +/// Small Caps, then Superscript and Subscript with [`CharacterRows::SUPERSCRIPT`], Underline, Strikethrough); the +/// OpenType buttons; the language when [`languages`](Self::languages) is given. [`rows`](Self::rows) turns the other +/// groups (rotation, anti-aliasing, font height reference, East Asian and Middle Eastern fields) on or off. /// /// ```ignore /// let out = CharacterControls::new("Character", &values, &families, &styles).unit(DocUnit::points()).show(ui); @@ -755,10 +876,10 @@ impl<'a> CharacterControls<'a> { pub fn show(self, ui: &mut Ui) -> CharacterControlsOutput { let values = self.values; let (families, styles, languages) = (self.families, self.styles, self.languages); - let (unit, superscript) = (self.unit, self.superscript); - let _ = (self.rows, self.anti_alias_methods); // P3(type-w3): ty.X23, ty.X27 + let unit = self.unit; + let (rows, anti_alias_methods) = (self.rows, self.anti_alias_methods); #[cfg(feature = "font-picker")] - let _ = (self.axes_layout,); // P3(type-w3): ty.X26 + let axes_layout = self.axes_layout; #[cfg(feature = "font-picker")] let mut picker = self.picker; #[cfg(feature = "font-picker")] @@ -793,121 +914,202 @@ impl<'a> CharacterControls<'a> { let mut preview_family = Preview::Unchanged; let mut rested_family = None; let mut requests = HostRequests::new(); + let mut picker_id = None; + let cells: Vec = Cell::ALL.into_iter().filter(|cell| cell.shown(rows)).collect(); let shown = Stack::new().label(self.label).show(ui, |ui, _| { #[cfg(feature = "font-picker")] - let family = family_field(ui, &words, values, families, picker.as_mut()); + let family = family_field_keyed(ui, &words, values, families, Some(keys::FAMILY), picker.as_mut()); #[cfg(not(feature = "font-picker"))] - let family = family_field(ui, &words, values, families); + let family = family_field_keyed(ui, &words, values, families, Some(keys::FAMILY)); preview_family = family.preview; rested_family = family.rested; + picker_id = family.picker; edits.extend(family.edits); edits.extend(select_row(ui, &words.style, hidden_side(), mixed(values.style.clone()), &style_entries, CharacterChange::Style)); #[cfg(feature = "font-picker")] if let Some(axes) = axes { let held = values.axes.clone().unwrap_or_default(); - let mut sliders = AxisSliders::new(words.axes.clone(), axes.axes, Bind::given(held)).instances(axes.instances).italic(axes.italic); - if let Some(weight) = axes.weight { - sliders = sliders.weight(weight); - } - edits.extend(sliders.show(ui).edit.into_iter().map(|event| event.map(CharacterChange::Axes))); + let edit = match axes_layout { + AxesLayout::Sliders => { + let mut sliders = AxisSliders::new(words.axes.clone(), axes.axes, Bind::given(held)) + .instances(axes.instances) + .italic(axes.italic) + .mixed(&values.mixed_axes); + if let Some(weight) = axes.weight { + sliders = sliders.weight(weight); + } + sliders.show(ui).edit + } + AxesLayout::Button => { + let mut button = AxesButton::new(words.axes.clone(), axes.axes, Bind::given(held)) + .instances(axes.instances) + .italic(axes.italic) + .mixed(&values.mixed_axes); + if let Some(weight) = axes.weight { + button = button.weight(weight); + } + button.show(ui).edit + } + }; + edits.extend(edit.into_iter().map(|event| event.map(CharacterChange::Axes))); } - Grid::new(&TWO_COLUMNS).fill_width().show(ui, |ui, _| { - let size = LengthRow { - label: words.size.clone(), - side: mark(GlyphLabel::text("tT"), None, FieldLayout::Inside), - value: values.size, - range: SIZE_RANGE, - presets: &size_presets, - }; - let (event, _) = lengths.row(ui, size); - edits.extend(number_change(event, CharacterChange::Size)); - let side = mark(GlyphLabel::text("A↕"), Some(words.leading_hint.clone()), FieldLayout::Inside); - edits.extend(leading_row(&lengths, ui, &words, side, values.leading, values.size, &leading_presets)); - }); - Grid::new(&TWO_COLUMNS).fill_width().show(ui, |ui, _| { - let event = lengths.kerning( - ui, - words.kerning.clone(), - GlyphLabel::icon(IconId::new("text_cursor")), - words.kerning_hint.clone(), - kerning_shown, - &kerning_presets, - ); - edits.extend(event.map(|value| CharacterChange::Kerning(kerning_of(value)))); - let tracking = PlainRow { - label: words.tracking.clone(), - side: mark(GlyphLabel::text("VA"), Some(words.tracking_hint.clone()), FieldLayout::Inside), - value: plain(values.tracking), - range: TRACKING_RANGE, - percent: false, - presets: &tracking_presets, - }; - let (event, _) = lengths.plain(ui, tracking); - edits.extend(number_change(event, CharacterChange::Tracking)); - }); - Grid::new(&TWO_COLUMNS).fill_width().show(ui, |ui, _| { - let vertical = PlainRow { - label: words.vertical_scale.clone(), - side: mark(GlyphLabel::text("↕T"), None, FieldLayout::Inside), - value: plain(values.vertical_scale), - range: SCALE_RANGE, - percent: true, - presets: &[], - }; - let (event, _) = lengths.plain(ui, vertical); - edits.extend(number_change(event, CharacterChange::VerticalScale)); - let horizontal = PlainRow { - label: words.horizontal_scale.clone(), - side: mark(GlyphLabel::text("↔T"), None, FieldLayout::Inside), - value: plain(values.horizontal_scale), - range: SCALE_RANGE, - percent: true, - presets: &[], - }; - let (event, _) = lengths.plain(ui, horizontal); - edits.extend(number_change(event, CharacterChange::HorizontalScale)); - }); - Grid::new(&TWO_COLUMNS).fill_width().show(ui, |ui, _| { - let shift = LengthRow { - label: words.baseline_shift.clone(), - side: mark(GlyphLabel::text("Aª"), None, FieldLayout::Inside), - value: values.baseline_shift, - range: SHIFT_RANGE, - presets: &[], - }; - let (event, _) = lengths.row(ui, shift); - edits.extend(number_change(event, CharacterChange::BaselineShift)); - let color = mixed(values.color.map(Srgba::from)); - let side = SideLabel::new().kind(LabelKind::Glyph(GlyphLabel::text(words.color.clone()))); - let well = Field::new(words.text_color.clone()) - .side(side) - .show(ui, |ui, _| Swatch::new(words.text_color.clone(), color).picker(COLOR_KEY).sizing(Sizing::FillWidth).show(ui).pick_color); - if let Some(request) = well.inner.flatten() { - requests.request_color(request); + let mut draw = |ui: &mut Ui, cell: Cell| { + if let Some(spec) = cell.plain(&words, values) { + let presets = number_presets(&units, std::iter::once(spec.presets)); + let row = PlainRow { + label: spec.label, + side: mark(GlyphLabel::text(spec.mark), None, FieldLayout::Inside), + value: plain(spec.value), + range: spec.range, + percent: spec.percent, + presets: &presets, + }; + let (event, _) = lengths.plain(ui, row); + edits.extend(number_change(event, spec.change)); + return; } - }); + match cell { + Cell::Size => { + let size = LengthRow { + label: words.size.clone(), + side: mark(GlyphLabel::text("tT"), None, FieldLayout::Inside), + value: values.size, + range: SIZE_RANGE, + presets: &size_presets, + }; + let (event, _) = lengths.row_keyed(ui, size, Some(keys::SIZE)); + edits.extend(number_change(event, CharacterChange::Size)); + } + Cell::Leading => { + let side = mark(GlyphLabel::text("A↕"), Some(words.leading_hint.clone()), FieldLayout::Inside); + edits.extend(leading_row_keyed( + &lengths, + ui, + &words, + side, + values.leading, + values.size, + &leading_presets, + Some(keys::LEADING), + )); + } + Cell::FontHeight => { + let entries = font_height_entries(&words); + edits.extend(select_row( + ui, + &words.font_height, + hidden_side(), + mixed(values.font_height), + &entries, + CharacterChange::FontHeight, + )); + } + Cell::Kerning => { + // PLAN: `NumberField::named` has no per-value display text, so the field shows the name + // (Metrics, Optical) and `auto_kerning` is not shown. + let event = lengths.kerning( + ui, + words.kerning.clone(), + GlyphLabel::icon(IconId::new("text_cursor")), + words.kerning_hint.clone(), + kerning_shown, + &kerning_presets, + Some(keys::KERNING), + ); + edits.extend(event.map(|value| CharacterChange::Kerning(kerning_of(value)))); + } + Cell::Tracking => { + let tracking = PlainRow { + label: words.tracking.clone(), + side: mark(GlyphLabel::text("VA"), Some(words.tracking_hint.clone()), FieldLayout::Inside), + value: plain(values.tracking), + range: TRACKING_RANGE, + percent: false, + presets: &tracking_presets, + }; + let (event, _) = lengths.plain_keyed(ui, tracking, Some(keys::TRACKING)); + edits.extend(number_change(event, CharacterChange::Tracking)); + } + Cell::BaselineShift => { + let shift = LengthRow { + label: words.baseline_shift.clone(), + side: mark(GlyphLabel::text("Aª"), None, FieldLayout::Inside), + value: values.baseline_shift, + range: SHIFT_RANGE, + presets: &[], + }; + let (event, _) = lengths.row(ui, shift); + edits.extend(number_change(event, CharacterChange::BaselineShift)); + } + Cell::Color => { + let color = mixed(values.color.map(Srgba::from)); + let side = SideLabel::new().kind(LabelKind::Glyph(GlyphLabel::text(words.color.clone()))); + let well = Field::new(words.text_color.clone()).side(side).show(ui, |ui, _| { + Swatch::new(words.text_color.clone(), color).picker(COLOR_KEY).sizing(Sizing::FillWidth).show(ui).pick_color + }); + if let Some(request) = well.inner.flatten() { + requests.request_color(request); + } + } + Cell::AntiAlias => { + let option_words = env.words::(); + let entries: Vec> = + anti_alias_methods.iter().map(|&method| Entry::new(method, method.label(&option_words).clone())).collect(); + edits.extend(select_row( + ui, + &words.anti_alias, + hidden_side(), + mixed(values.anti_alias), + &entries, + CharacterChange::AntiAlias, + )); + } + // The plain number cells are drawn above. + Cell::VerticalScale + | Cell::HorizontalScale + | Cell::Rotation + | Cell::AkiLeft + | Cell::AkiRight + | Cell::Tsume + | Cell::DiacriticX + | Cell::DiacriticY => {} + } + }; + for run in cells.chunk_by(|a, b| !a.full() && !b.full()) { + match run { + [cell] if cell.full() => draw(ui, *cell), + _ => { + Grid::new(&TWO_COLUMNS).fill_width().show(ui, |ui, _| { + for &cell in run { + draw(ui, cell); + } + }); + } + } + } let caps = values.caps; let position = values.position; - let mut toggles: Vec> = vec![ - Toggle::new(&words.faux_bold, "text_bold", values.faux_bold, Box::new(CharacterChange::FauxBold)), - Toggle::new(&words.faux_italic, "text_italic", values.faux_italic, Box::new(CharacterChange::FauxItalic)), - Toggle::new( - &words.all_caps, - "text_all_caps", - caps.map(|caps| caps == Caps::AllCaps), - Box::new(|on| CharacterChange::Caps(if on { Caps::AllCaps } else { Caps::Normal })), - ), - Toggle::new( - &words.small_caps, - "text_small_caps", - caps.map(|caps| caps == Caps::SmallCaps), - Box::new(|on| CharacterChange::Caps(if on { Caps::SmallCaps } else { Caps::Normal })), - ), - ]; - if superscript { + let mut toggles: Vec> = Vec::new(); + if rows.contains(CharacterRows::FAUX) { + toggles.push(Toggle::new(&words.faux_bold, "text_bold", values.faux_bold, Box::new(CharacterChange::FauxBold))); + toggles.push(Toggle::new(&words.faux_italic, "text_italic", values.faux_italic, Box::new(CharacterChange::FauxItalic))); + } + toggles.push(Toggle::new( + &words.all_caps, + "text_all_caps", + caps.map(|caps| caps == Caps::AllCaps), + Box::new(|on| CharacterChange::Caps(if on { Caps::AllCaps } else { Caps::Normal })), + )); + toggles.push(Toggle::new( + &words.small_caps, + "text_small_caps", + caps.map(|caps| caps == Caps::SmallCaps), + Box::new(|on| CharacterChange::Caps(if on { Caps::SmallCaps } else { Caps::Normal })), + )); + if rows.contains(CharacterRows::SUPERSCRIPT) { toggles.push(Toggle::new( &words.superscript, "text_superscript", @@ -925,31 +1127,33 @@ impl<'a> CharacterControls<'a> { toggles.push(Toggle::new(&words.strikethrough, "text_strikethrough", values.strikethrough, Box::new(CharacterChange::Strikethrough))); edits.extend(toggle_row(ui, None, &toggles)); - let names = [ - &words.standard_ligatures, - &words.contextual_alternates, - &words.discretionary_ligatures, - &words.swash, - &words.stylistic_alternates, - &words.titling_alternates, - &words.ordinals, - &words.fractions, - ]; - let features: Vec> = OPENTYPE_FEATURES - .iter() - .zip(OPENTYPE_ICONS) - .zip(names) - .map(|((&tag, icon), name)| { - Toggle::new(name, icon, Some(values.opentype.contains(&tag)), Box::new(move |on| CharacterChange::OpenType { tag, on })) - }) - .collect(); - edits.extend(toggle_row(ui, None, &features)); + if rows.contains(CharacterRows::OPENTYPE) { + let names = [ + &words.standard_ligatures, + &words.contextual_alternates, + &words.discretionary_ligatures, + &words.swash, + &words.stylistic_alternates, + &words.titling_alternates, + &words.ordinals, + &words.fractions, + ]; + let features: Vec> = OPENTYPE_FEATURES + .iter() + .zip(OPENTYPE_ICONS) + .zip(names) + .map(|((&tag, icon), name)| { + Toggle::new(name, icon, Some(values.opentype.contains(&tag)), Box::new(move |on| CharacterChange::OpenType { tag, on })) + }) + .collect(); + edits.extend(toggle_row(ui, None, &features)); + } if let Some(entries) = &language_entries { edits.extend(select_row(ui, &words.language, hidden_side(), mixed(values.language.clone()), entries, CharacterChange::Language)); } }); - CharacterControlsOutput { response: shown.output, edits, preview_family, rested_family, requests, picker: None } + CharacterControlsOutput { response: shown.output, edits, preview_family, rested_family, requests, picker: picker_id } } } diff --git a/crates/text/src/ui/character/paragraph.rs b/crates/text/src/ui/character/paragraph.rs index 9d54c126b..570424453 100644 --- a/crates/text/src/ui/character/paragraph.rs +++ b/crates/text/src/ui/character/paragraph.rs @@ -223,16 +223,16 @@ impl<'a> ParagraphControls<'a> { } } - /// Draws the host's rows after the alignment and direction rows, before the indents, and returns the changes the - /// user made. - pub fn show_with(self, ui: &mut Ui, between: impl FnOnce(&mut Ui)) -> ParagraphControlsOutput { - let _ = (self, ui, between); - todo!("ty.X38") - } - /// Shows the controls and returns the changes the user made. pub fn show(self, ui: &mut Ui) -> ParagraphControlsOutput { - let _ = (self.direction_row,); // P3(type-w3): ty.X37 + self.show_with(ui, |_| {}) + } + + /// As [`show`](Self::show), with the host's rows drawn by `between` after the alignment and direction rows and + /// before the indents. + pub fn show_with(self, ui: &mut Ui, between: impl FnOnce(&mut Ui)) -> ParagraphControlsOutput { + let mut between = Some(between); + let direction_row = self.direction_row; let values = self.values; let point_text = self.point_text; let vertical = self.vertical; @@ -244,12 +244,21 @@ impl<'a> ParagraphControls<'a> { let aligns: Vec> = align_toggles(values, &words, vertical).into_iter().chain(justify_toggles(values, &words, point_text)).collect(); - let directions = direction_toggles(values, &words); + let directions = match direction_row { + DirectionRow::Automatic => direction_toggles(values, &words), + DirectionRow::Explicit => explicit_toggles(values, &words), + DirectionRow::Hidden => Vec::new(), + }; let mut edits: Vec> = Vec::new(); let shown = Stack::new().label(self.label).show(ui, |ui, _| { edits.extend(toggle_row(ui, Some(&words.alignment), &aligns)); - edits.extend(toggle_row(ui, Some(&words.direction), &directions)); + if direction_row != DirectionRow::Hidden { + edits.extend(toggle_row(ui, Some(&words.direction), &directions)); + } + if let Some(between) = between.take() { + between(ui); + } Grid::new(&TWO_COLUMNS).fill_width().show(ui, |ui, _| { let start = LengthRow { @@ -349,14 +358,19 @@ pub(crate) fn justify_toggles(values: &ParagraphValues, words: &ParagraphWords, /// The three direction toggles. pub(crate) fn direction_toggles(values: &ParagraphValues, words: &ParagraphWords) -> Vec> { - [ - (ParagraphDirection::Automatic, "pilcrow", &words.direction_auto), - (ParagraphDirection::LeftToRight, "pilcrow_right", &words.direction_ltr), - (ParagraphDirection::RightToLeft, "pilcrow_left", &words.direction_rtl), + std::iter::once(direction_toggle(values, ParagraphDirection::Automatic, "pilcrow", &words.direction_auto)) + .chain(explicit_toggles(values, words)) + .collect() +} + +/// The Left to Right and Right to Left toggles: the row of [`DirectionRow::Explicit`]. +fn explicit_toggles(values: &ParagraphValues, words: &ParagraphWords) -> Vec> { + vec![ + direction_toggle(values, ParagraphDirection::LeftToRight, "pilcrow_right", &words.direction_ltr), + direction_toggle(values, ParagraphDirection::RightToLeft, "pilcrow_left", &words.direction_rtl), ] - .into_iter() - .map(|(direction, icon, name)| { - Toggle::new(name, icon, Some(values.direction == Some(direction)), Box::new(move |_| ParagraphChange::Direction(direction))) - }) - .collect() +} + +fn direction_toggle(values: &ParagraphValues, direction: ParagraphDirection, icon: &'static str, name: &Label) -> Toggle { + Toggle::new(name, icon, Some(values.direction == Some(direction)), Box::new(move |_| ParagraphChange::Direction(direction))) } diff --git a/crates/text/src/ui/character/rows.rs b/crates/text/src/ui/character/rows.rs index 28cd3d646..04273d419 100644 --- a/crates/text/src/ui/character/rows.rs +++ b/crates/text/src/ui/character/rows.rs @@ -4,7 +4,7 @@ use std::ops::RangeInclusive; use std::sync::Arc; use crate::ui::length::{DocUnit, length_field, shown}; -use craft_behavior::{EditEvent, EditEvents, IconId, Mixed, NumberCodec, Preview}; +use craft_behavior::{DocKey, EditEvent, EditEvents, IconId, Mixed, NumberCodec, Preview, Value}; use craft_ui::buttons::ToggleButton; use craft_ui::contract::{ Bind, Entry, FieldLayout, GlyphLabel, LabelKind, Props as _, Show as _, ShowContainer as _, SideLabel, Sizing, TooltipSpec, @@ -12,14 +12,17 @@ use craft_ui::contract::{ use craft_ui::form::Field; use craft_ui::layout_components::Row; use craft_ui::pickers::Select; +use craft_ui::primitives::{Glyph, IconSpec}; use craft_ui::runtime::env::{Label, UnitKind, Units}; +use craft_ui::style::UiTokens; use craft_ui::text::{NamedOr, NumberField, NumberPick}; -use egui::Ui; +use egui::{Color32, Painter, Rect, Shape, Stroke, Ui, vec2}; #[cfg(feature = "font-picker")] use super::FontPickerSource; use super::{ - AUTO_LEADING, AUTO_ROW, CharacterChange, CharacterValues, CharacterWords, KerningName, LEADING_RANGE, Leading, PRESET_DECIMALS, SIZE_PRESETS, + AUTO_LEADING, AUTO_ROW, CharacterChange, CharacterValues, CharacterWords, FontHeightReference, KerningName, LEADING_RANGE, Leading, PRESET_DECIMALS, + SIZE_PRESETS, TRACKING_RANGE, }; #[cfg(feature = "font-picker")] @@ -54,11 +57,20 @@ impl Lengths<'_> { /// A field over a length in points, shown in the unit: its event in points and the preset row picked. The field /// fills its grid cell. pub(crate) fn row(&self, ui: &mut Ui, spec: LengthRow<'_>) -> (EditEvents>, Option) { + self.row_keyed(ui, spec, None) + } + + /// As [`row`](Self::row), the field carrying the document `key` a host focuses it by. + pub(crate) fn row_keyed(&self, ui: &mut Ui, spec: LengthRow<'_>, key: Option) -> (EditEvents>, Option) { let LengthRow { label, side, value, range, presets } = spec; let points = value.map_or(Mixed::Mixed, |value| Mixed::Same(Some(f64::from(value)))); let field = Field::new(label.clone()).side(side).show(ui, |ui, _| { - let out = - length_field(self.unit, label.clone(), points, range.clone()).presets(presets).scrub_by_label().sizing(Sizing::FillWidth).show(ui); + let field = length_field(self.unit, label.clone(), points, range.clone()).presets(presets).scrub_by_label().sizing(Sizing::FillWidth); + let out = match key { + Some(key) => field.doc_key(key), + None => field, + } + .show(ui); (out.edit.events, out.preset) }); field.inner.unwrap_or_default() @@ -73,17 +85,30 @@ impl Lengths<'_> { hint: Label, value: Option>, presets: &[Entry], + key: Option, ) -> EditEvents> { let units = self.units; let shown = Field::new(label.clone()).side(mark(glyph, Some(hint), FieldLayout::Beside)).show(ui, |ui, _| { let field = NumberField::named(label.clone(), Bind::given(mixed(value))).range(TRACKING_RANGE).presets(presets).scrub_by_label(); - field.codec(NumberCodec::new(units.reader(None), 0)).show(ui).edit.events + let field = field.codec(NumberCodec::new(units.reader(None), 0)); + match key { + Some(key) => field.doc_key(key), + None => field, + } + .show(ui) + .edit + .events }); shown.inner.unwrap_or_default() } /// A field over a number that is not a length: its event and the preset row picked. pub(crate) fn plain(&self, ui: &mut Ui, spec: PlainRow<'_>) -> (EditEvents>, Option) { + self.plain_keyed(ui, spec, None) + } + + /// As [`plain`](Self::plain), the field carrying the document `key` a host focuses it by. + pub(crate) fn plain_keyed(&self, ui: &mut Ui, spec: PlainRow<'_>, key: Option) -> (EditEvents>, Option) { let PlainRow { label, side, value, range, percent, presets } = spec; let units = self.units; let shown = Field::new(label.clone()).side(side).show(ui, |ui, _| { @@ -93,7 +118,11 @@ impl Lengths<'_> { } else { field.codec(NumberCodec::new(units.reader(None), 0)) }; - let out = field.show(ui); + let out = match key { + Some(key) => field.doc_key(key), + None => field, + } + .show(ui); (out.edit.events, out.preset) }); shown.inner.unwrap_or_default() @@ -177,6 +206,21 @@ pub(crate) fn leading_row( leading: Option, size: Option, presets: &[Entry], +) -> Vec> { + leading_row_keyed(lengths, ui, words, side, leading, size, presets, None) +} + +/// As [`leading_row`], the field carrying the document `key` a host focuses it by. +#[allow(clippy::too_many_arguments)] +pub(crate) fn leading_row_keyed( + lengths: &Lengths<'_>, + ui: &mut Ui, + words: &CharacterWords, + side: SideLabel, + leading: Option, + size: Option, + presets: &[Entry], + key: Option, ) -> Vec> { let value = match leading { Some(Leading::Fixed(points)) => Some(points), @@ -184,7 +228,7 @@ pub(crate) fn leading_row( None => None, }; let row = LengthRow { label: words.leading.clone(), side, value, range: LEADING_RANGE, presets }; - let (event, picked) = lengths.row(ui, row); + let (event, picked) = lengths.row_keyed(ui, row, key); let mut edits: Vec<_> = number_change(event, |points| CharacterChange::Leading(Leading::Fixed(points))).into_iter().collect(); if picked == Some(NumberPick::Choice(AUTO_ROW)) { edits.push(picked_event(CharacterChange::Leading(Leading::Auto), words.leading.as_str())); @@ -198,13 +242,13 @@ pub(crate) fn string_entries(pairs: impl Iterator) -> V } /// A row with a select over `entries`; its pick as a change. -pub(crate) fn select_row( +pub(crate) fn select_row( ui: &mut Ui, label: &Label, side: SideLabel, - value: Mixed, - entries: &[Entry], - change: impl Fn(String) -> C, + value: Mixed, + entries: &[Entry], + change: impl Fn(V) -> C, ) -> EditEvents { let shown = Field::new(label.clone()) .side(side) @@ -265,15 +309,37 @@ pub(crate) struct FamilyField { pub preview: Preview, /// The family the pointer or keyboard rested on in the open list. pub rested: Option, + /// The picker's id this pass; `None` without a picker. + pub picker: Option, } /// The family field of the character controls, the style options and the type bar: the font picker in a /// hidden-label field when a `picker` source is given, else a select over `families`. +#[cfg(feature = "font-picker")] pub(crate) fn family_field( ui: &mut Ui, words: &CharacterWords, values: &CharacterValues, families: &[String], + picker: Option<&mut FontPickerSource<'_>>, +) -> FamilyField { + family_field_keyed(ui, words, values, families, None, picker) +} + +/// [`family_field`] without a picker. +#[cfg(not(feature = "font-picker"))] +pub(crate) fn family_field(ui: &mut Ui, words: &CharacterWords, values: &CharacterValues, families: &[String]) -> FamilyField { + family_field_keyed(ui, words, values, families, None) +} + +/// As [`family_field`], the picker carrying the document `key` a host focuses it by (the plain select has none). +pub(crate) fn family_field_keyed( + ui: &mut Ui, + words: &CharacterWords, + values: &CharacterValues, + families: &[String], + #[cfg(feature = "font-picker")] key: Option, + #[cfg(not(feature = "font-picker"))] _key: Option, #[cfg(feature = "font-picker")] picker: Option<&mut FontPickerSource<'_>>, ) -> FamilyField { let value = mixed(values.family.clone()); @@ -283,13 +349,20 @@ pub(crate) fn family_field( let mut field = FontPicker::new(words.family.clone(), Bind::given(value.clone()), source.catalog) .settings(source.settings.reborrowed()) .filters(source.filters.reborrowed()) - .served(source.served); + .served(source.served) + .sets(source.sets) + .scripts(source.scripts) + .filter_layout(source.filter_layout); if let Some(text) = source.preview_text { field = field.preview_text(text); } + if let Some(key) = key { + field = field.doc_key(key); + } field.show(ui) }); let Some(out) = shown.inner else { return FamilyField::default() }; + let id = craft_ui::contract::Outcome::response(&out).id; let mut edits: Vec> = out .edit .events @@ -300,7 +373,7 @@ pub(crate) fn family_field( if let Some(style) = out.picked_style { edits.push(picked_event(CharacterChange::Style(style), words.style.as_str())); } - return FamilyField { edits, preview: out.preview, rested: out.rested }; + return FamilyField { edits, preview: out.preview, rested: out.rested, picker: Some(id) }; } let entries = string_entries(families.iter().map(|name| (name.clone(), name.clone()))); let edits = select_row(ui, &words.family, hidden_side(), value, &entries, CharacterChange::Family).into_iter().collect(); @@ -311,3 +384,52 @@ pub(crate) fn family_field( pub(crate) fn agree(a: &Option, b: &Option) -> Option { if a == b { a.clone() } else { None } } + +/// The share of a glyph's side its stroke may take. +const STROKE_SHARE: f32 = 0.2; + +/// The mark of a Font Height Reference value: the box or the guide lines the value measures from. +struct FontHeightGlyph(FontHeightReference); + +static EM_BOX: FontHeightGlyph = FontHeightGlyph(FontHeightReference::EmBox); +static CAP_HEIGHT: FontHeightGlyph = FontHeightGlyph(FontHeightReference::CapHeight); +static X_HEIGHT: FontHeightGlyph = FontHeightGlyph(FontHeightReference::XHeight); +static ICF_BOX: FontHeightGlyph = FontHeightGlyph(FontHeightReference::IcfBox); + +impl Glyph for FontHeightGlyph { + fn paint(&self, painter: &Painter, tokens: &UiTokens, rect: Rect, color: Color32, _mirrored: bool) { + let side = rect.width().min(rect.height()); + if !rect.is_finite() || side <= 0.0 { + return; + } + let thin = tokens.stroke.thin; + let stroke = Stroke::new(if thin.is_finite() { thin.clamp(0.0, side * STROKE_SHARE) } else { 0.0 }, color); + let at = |(x, y): (f32, f32)| rect.center() + vec2(x - 0.5, y - 0.5) * side; + let boxed = |left: f32, top: f32, right: f32, bottom: f32| { + Shape::closed_line([(left, top), (right, top), (right, bottom), (left, bottom)].map(at).to_vec(), stroke) + }; + let line = |from: (f32, f32), to: (f32, f32)| Shape::line_segment([at(from), at(to)], stroke); + // A letter between a top guide and the baseline: the guide's height is what the value measures. + let guided = |top: f32| vec![line((0.1, top), (0.9, top)), line((0.1, 0.8), (0.9, 0.8)), line((0.5, top), (0.5, 0.8))]; + let shapes = match self.0 { + FontHeightReference::EmBox => vec![boxed(0.1, 0.1, 0.9, 0.9), line((0.1, 0.75), (0.9, 0.75))], + FontHeightReference::CapHeight => guided(0.2), + FontHeightReference::XHeight => guided(0.45), + FontHeightReference::IcfBox => vec![boxed(0.2, 0.2, 0.8, 0.8)], + }; + painter.extend(shapes); + } +} + +/// The Set Font Height Reference select's entries, each with the mark of its value. +pub(crate) fn font_height_entries(words: &CharacterWords) -> Vec> { + [ + (FontHeightReference::EmBox, &words.em_box, &EM_BOX), + (FontHeightReference::CapHeight, &words.cap_height, &CAP_HEIGHT), + (FontHeightReference::XHeight, &words.x_height, &X_HEIGHT), + (FontHeightReference::IcfBox, &words.icf_box, &ICF_BOX), + ] + .into_iter() + .map(|(value, name, glyph)| Entry::new(value, name.clone()).icon(IconSpec::glyph(glyph))) + .collect() +} diff --git a/crates/text/src/ui/character/stories.rs b/crates/text/src/ui/character/stories.rs index 5a845053c..6d8daf005 100644 --- a/crates/text/src/ui/character/stories.rs +++ b/crates/text/src/ui/character/stories.rs @@ -1,14 +1,16 @@ //! Stories of the Character and Paragraph controls, the Paragraph dialog and its preview. #[cfg(feature = "font-picker")] -use super::FaceAxes; +use super::{AxesLayout, FaceAxes}; use super::{ - BaselinePosition, Caps, CharacterControls, CharacterValues, Kerning, Leading, LineSpacing, ParagraphAlign, ParagraphControls, ParagraphDialog, - ParagraphFormat, ParagraphPreview, ParagraphValues, SpacingKind, SpecialIndent, TextAlign, + BaselinePosition, Caps, CharacterControls, CharacterRows, CharacterValues, DirectionRow, FontHeightReference, Kerning, Leading, LineSpacing, + ParagraphAlign, ParagraphControls, ParagraphDialog, ParagraphFormat, ParagraphPreview, ParagraphValues, SpacingKind, SpecialIndent, TextAlign, }; +use crate::ui::type_options::TextAntiAlias; use craft_theme::Rgba; use craft_ui::contract::Show as _; use craft_ui::dialog::{DialogStack, StackDialog as _}; +use craft_ui::display::Text; use craft_ui::testing::{Drive, StoryData, StoryOutput, story}; use egui::Ui; @@ -24,9 +26,9 @@ fn languages() -> Vec<(String, String)> { [("en-US", "English: USA"), ("fr-FR", "French"), ("de-DE", "German")].map(|(tag, name)| (tag.to_owned(), name.to_owned())).to_vec() } -#[story(CharacterControls<'static>, covers = [], drive = Drive::Never)] -fn single_values(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { - let values = CharacterValues { +/// The values every row story starts from. +fn single() -> CharacterValues { + CharacterValues { family: Some("Ubuntu".to_owned()), style: Some("Bold".to_owned()), size: Some(24.0), @@ -46,9 +48,51 @@ fn single_values(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { opentype: vec!["liga", "frac"], language: Some("en-US".to_owned()), ..CharacterValues::default() - }; + } +} + +#[story(CharacterControls<'static>, covers = [], drive = Drive::Never)] +fn single_values(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { + let values = single(); let (families, styles, languages) = (families(), styles(), languages()); - CharacterControls::new("Character", &values, &families, &styles).superscript(true).languages(&languages).show(ui).into() + CharacterControls::new("Character", &values, &families, &styles) + .rows(CharacterRows::default() | CharacterRows::SUPERSCRIPT) + .languages(&languages) + .show(ui) + .into() +} + +#[story(CharacterControls<'static>, covers = [], drive = Drive::Never)] +fn rows_illustrator(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { + let values = CharacterValues { + rotation: Some(0.0), + anti_alias: Some(TextAntiAlias::Sharp), + font_height: Some(FontHeightReference::CapHeight), + ..single() + }; + let rows = CharacterRows::default() + | CharacterRows::SCALES + | CharacterRows::ROTATION + | CharacterRows::ANTI_ALIAS + | CharacterRows::FONT_HEIGHT + | CharacterRows::SUPERSCRIPT; + let (families, styles) = (families(), styles()); + CharacterControls::new("Character", &values, &families, &styles).rows(rows).show(ui).into() +} + +#[story(CharacterControls<'static>, covers = [], drive = Drive::Never)] +fn rows_east_asian_and_middle_eastern(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { + let values = CharacterValues { + aki_left: Some(0.0), + aki_right: Some(250.0), + tsume: Some(50.0), + diacritic_x: Some(0.0), + diacritic_y: Some(0.0), + ..single() + }; + let rows = CharacterRows::default() | CharacterRows::EAST_ASIAN | CharacterRows::MIDDLE_EASTERN; + let (families, styles) = (families(), styles()); + CharacterControls::new("Character", &values, &families, &styles).rows(rows).show(ui).into() } #[story(CharacterControls<'static>, covers = [], drive = Drive::Never)] @@ -73,6 +117,28 @@ fn paragraph_text(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { ParagraphControls::new("Paragraph", &values).show(ui).into() } +#[story(ParagraphControls<'static>, covers = [], drive = Drive::Never)] +fn direction_explicit(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { + let values = ParagraphValues { align: Some(TextAlign::Left), direction: Some(super::ParagraphDirection::RightToLeft), ..ParagraphValues::default() }; + ParagraphControls::new("Paragraph", &values).direction_row(DirectionRow::Explicit).show(ui).into() +} + +#[story(ParagraphControls<'static>, covers = [], drive = Drive::Never)] +fn direction_hidden(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { + let values = ParagraphValues { align: Some(TextAlign::Left), ..ParagraphValues::default() }; + ParagraphControls::new("Paragraph", &values).direction_row(DirectionRow::Hidden).show(ui).into() +} + +#[story(ParagraphControls<'static>, covers = [], drive = Drive::Never)] +fn between_rows(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { + let values = ParagraphValues { align: Some(TextAlign::Left), space_after: Some(6.0), ..ParagraphValues::default() }; + ParagraphControls::new("Paragraph", &values) + .show_with(ui, |ui| { + Text::new("Host rows").show(ui); + }) + .into() +} + // No variable face ships with the stories, so the sliders have no axis to list; the state shows the slot is taken. #[cfg(feature = "font-picker")] #[story(CharacterControls<'static>, covers = [], drive = Drive::Never)] @@ -87,6 +153,24 @@ fn with_axes(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { CharacterControls::new("Character", &values, &families, &styles).axes(FaceAxes::new(&[], &[])).show(ui).into() } +#[cfg(feature = "font-picker")] +#[story(CharacterControls<'static>, covers = [], drive = Drive::Never)] +fn axes_button(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { + let values = CharacterValues { + family: Some("Ubuntu".to_owned()), + style: Some("Bold".to_owned()), + axes: Some(vec![(*b"wght", 700.0)]), + ..CharacterValues::default() + }; + let axes = [craft_font_manager::axes::FontAxis::new(*b"wght", "Weight", 100.0, 400.0, 900.0)]; + let (families, styles) = (families(), styles()); + CharacterControls::new("Character", &values, &families, &styles) + .axes(FaceAxes::new(&axes, &[])) + .axes_layout(AxesLayout::Button) + .show(ui) + .into() +} + #[story(ParagraphControls<'static>, covers = [], drive = Drive::Never)] fn vertical(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { let values = ParagraphValues { align: Some(TextAlign::Center), space_after: Some(6.0), ..ParagraphValues::default() };