2bc41aeb8602
paragraph.rs
//! Paragraph panel: seven alignment buttons, Paragraph Direction (with the Indic options), the
//! Bullets and Numbered List buttons and their presets (More Options opens Bullets and
//! Numbering…), indents, space before/after, Hyphenate, Insert Kashidas (with the Indic options),
//! Mojikumi Set and Kinsoku Set (with the East Asian options); the panel menu turns Roman Hanging
//! Punctuation on and off, opens Justification… and Hyphenation…, picks the Kinsoku Shori Type
//! (with the East Asian options) and picks the Single-line or Every-line Composer.
use egui::{Ui, vec2};
use serde_json::{Value, json};
use vectorcraft_doc::{
Composer, HyphenationOptions, JustificationOptions, Justify, KinsokuShoriType, Mojikumi, NodeKind, ParaDirection, ParaStyle, SingleWordJustify,
};
use super::character::{Metric, Shared, text_style};
use super::{pstate, set_pstate};
use crate::VectorcraftApp;
use crate::theme::Tokens;
use crate::widgets::{self, menu_item};
pub const ALIGNMENTS: [(Justify, &str, &str, &str); 7] = [
(Justify::Left, "dc-para-left", "Align left", "left"),
(Justify::Center, "dc-para-center", "Align center", "center"),
(Justify::Right, "dc-para-right", "Align right", "right"),
(Justify::JustifyLeft, "dc-para-justify-left", "Justify with last line aligned left", "justifyLeft"),
(Justify::JustifyCenter, "dc-para-justify-center", "Justify with last line aligned center", "justifyCenter"),
(Justify::JustifyRight, "dc-para-justify-right", "Justify with last line aligned right", "justifyRight"),
(Justify::JustifyAll, "dc-para-justify-all", "Justify all lines", "justifyAll"),
];
/// Paragraph attributes apply to the paragraphs of every frame of the selected stories or, while
/// the Type tool edits text, to the paragraphs its selection (or caret) touches (ending its typing
/// session).
fn format(app: &mut VectorcraftApp, p: Value) {
para_cmd(app, "text.setFormat", p);
}
/// The alignment shortcuts' commands (Cmd+Shift+L, C, R, J and F, as in page layout apps) and the
/// `justify` each sets ([`align`]).
pub const ALIGN_COMMANDS: [(&str, &str); 5] = [
("type.alignLeft", "left"),
("type.alignCenter", "center"),
("type.alignRight", "right"),
("type.justifyLeft", "justifyLeft"),
("type.justifyAll", "justifyAll"),
];
/// Align the paragraphs of the selected type, or those the Type tool's selection touches, as
/// `justify` (an [`ALIGN_COMMANDS`] alignment, #1111).
pub(crate) fn align(app: &mut VectorcraftApp, justify: &str) -> Result<Value, String> {
text_style(app).ok_or("select some type first")?;
para_cmd(app, "text.setStyle", json!({ "justify": justify }));
Ok(Value::Null)
}
/// `type.lineComposer` (Cmd+Alt+Shift+C): the Single-line Composer for the paragraphs the panel
/// acts on when they all break their lines every-line (either shaping), else the Every-line
/// Composer. Paragraphs that all use a Standard composer keep it; any other mix takes the Middle
/// Eastern & South Asian one. Returns the composer set.
pub(crate) fn toggle_composer(app: &mut VectorcraftApp) -> Result<Value, String> {
text_style(app).ok_or("select some type first")?;
let to_single = shared_para(app, |p| p.composer.is_single_line()) == Some(false);
let standard = shared_para(app, |p| p.composer.shapes_complex_scripts()) == Some(false);
let base = if standard { Composer::StandardEveryLine } else { Composer::EveryLine };
let id = composer_id(base.with_breaking(to_single));
format(app, json!({ "composer": id }));
Ok(json!(id))
}
/// The `text.setFormat` value of `composer`.
fn composer_id(composer: Composer) -> &'static str {
match composer {
Composer::SingleLine => "singleLine",
Composer::EveryLine => "everyLine",
Composer::StandardSingleLine => "standardSingleLine",
Composer::StandardEveryLine => "standardEveryLine",
}
}
/// `type.hyphenate` (Cmd+Alt+Shift+H): what the Hyphenate checkbox does, on the paragraphs the
/// panel acts on. Returns whether they now hyphenate.
pub(crate) fn toggle_hyphenate(app: &mut VectorcraftApp) -> Result<Value, String> {
text_style(app).ok_or("select some type first")?;
let on = shared_hyphenate(app) != Some(true);
format(app, json!({ "hyphenate": on }));
Ok(json!(on))
}
fn para_cmd(app: &mut VectorcraftApp, cmd: &str, p: Value) {
run_on_paragraphs(app, cmd, p).ok();
}
/// Run `cmd` on the paragraphs the Paragraph panel acts on ([`format`]) and return its result. With
/// the Selection tool these are the paragraphs of every frame of the selected stories
/// ([`super::character::selected_stories`]).
pub(crate) fn run_on_paragraphs(app: &mut VectorcraftApp, cmd: &str, mut p: Value) -> Result<Value, String> {
if let Some((id, a, b)) = super::character::text_editing_kept(app)? {
match super::character::text_story(app) {
// After Select All in threaded text: every paragraph of every frame.
Some(story) => p["ids"] = json!(story.iter().map(|f| f.0).collect::<Vec<_>>()),
None => {
p["ids"] = json!([id.0]);
p["start"] = json!(a);
p["end"] = json!(b);
}
}
} else {
super::character::target_selected_stories(app, &mut p);
}
app.run(cmd, p)
}
/// Does the paragraph being edited (at the caret), else the selected text's first paragraph, run
/// right to left: its Paragraph Direction, else from its first strong character?
fn resolved_rtl(app: &VectorcraftApp, para: &ParaStyle) -> bool {
let editing = super::character::text_editing(app);
let text = editing.and_then(|(id, _, _)| match app.session.active().and_then(|d| d.doc.node(id)).map(|n| &n.kind) {
Some(NodeKind::Text(t)) => Some(t.as_ref()),
_ => None,
});
let Some(t) = text.or_else(|| super::character::first_selected_text(app)) else { return para.direction == Some(ParaDirection::RightToLeft) };
let plain = t.plain_text();
let paragraph = vectorcraft_text::edit::paragraph_at(&plain, editing.map_or(0, |(_, a, _)| a));
vectorcraft_text::paragraph_is_rtl(plain.get(paragraph).unwrap_or_default(), para.direction)
}
/// The alignment button that shows `justify` in a paragraph running right to left (`rtl`) or not:
/// Auto aligns to the start of its direction.
fn shown_alignment(justify: Justify, rtl: bool) -> Justify {
match justify {
Justify::Auto if rtl => Justify::Right,
Justify::Auto => Justify::Left,
j => j,
}
}
/// Add the displayed alignments touched by the byte range (all paragraphs without a range).
/// Walk forward through run fragments so paragraph lookup never rescans an earlier prefix.
/// Only Auto needs paragraph text; borrow a fragment until style runs split it.
fn collect_text_alignment(text: &vectorcraft_doc::TextObject, range: Option<(usize, usize)>, shared: &mut Option<Justify>) -> bool {
use std::borrow::Cow;
let (a, b) = range.map_or((0, usize::MAX), |(a, b)| (a.min(b), a.max(b)));
let mut record = |index, plain: &str, start, end| {
if end < a || start > b {
return true;
}
let para = text.para_at(index);
let rtl = para.justify == Justify::Auto && vectorcraft_text::paragraph_is_rtl(plain, para.direction);
let value = shown_alignment(para.justify, rtl);
if shared.is_some_and(|old| old != value) {
return false;
}
*shared = Some(value);
true
};
let (mut index, mut start, mut end) = (0, 0, 0);
let mut plain = Cow::Borrowed("");
for run in &text.runs {
let mut rest = run.text.as_str();
while !rest.is_empty() {
let span = vectorcraft_text::edit::paragraph_at(rest, 0);
let part = rest.get(span.clone()).unwrap_or_default();
let para = text.para_at(index);
if para.justify == Justify::Auto && !part.is_empty() {
if plain.is_empty() {
plain = Cow::Borrowed(part);
} else {
plain.to_mut().push_str(part);
}
}
end += part.len();
if span.end == rest.len() {
break; // The same paragraph can continue in the next style run.
}
if !record(index, &plain, start, end) {
return false;
}
// Like TextObject::paragraphs_in, a range ending at the next paragraph's start
// includes that paragraph; the newline itself belongs to the preceding one.
end += 1;
start = end;
index += 1;
if start > b {
return true;
}
plain = Cow::Borrowed("");
rest = rest.get(span.end + 1..).unwrap_or_default();
}
}
record(index, &plain, start, end) // Includes empty text and the paragraph after a final newline.
}
/// The frames of the threaded story whose whole text the Type tool has selected after Select All
/// ([`super::character::text_story`]), in thread order.
fn story_texts(app: &VectorcraftApp) -> Option<Vec<&vectorcraft_doc::TextObject>> {
let st = app.session.active()?;
let story = super::character::text_story(app)?;
Some(
story
.into_iter()
.filter_map(|id| match st.doc.node(id).map(|n| &n.kind) {
Some(NodeKind::Text(t)) => Some(t.as_ref()),
_ => None,
})
.collect(),
)
}
/// Visit every paragraph the Paragraph panel acts on.
fn visit_paragraphs(app: &VectorcraftApp, mut f: impl FnMut(&ParaStyle)) {
let Some(st) = app.session.active() else { return };
if let Some(texts) = story_texts(app) {
for t in texts {
for i in 0..t.paragraph_count() {
f(t.para_at(i));
}
}
return;
}
if let Some((id, a, b)) = super::character::text_editing(app)
&& let Some(NodeKind::Text(text)) = st.doc.node(id).map(|n| &n.kind)
{
let range = text.paragraphs_in(a, b);
for i in range {
f(text.para_at(i));
}
return;
}
for t in super::character::selected_story_texts(app) {
for i in 0..t.paragraph_count() {
f(t.para_at(i));
}
}
}
/// A distance shared by every paragraph in scope, or `None` when they differ (blank spin field).
pub(crate) fn shared_para_field(app: &VectorcraftApp, field: impl Fn(&ParaStyle) -> f64) -> Option<f64> {
let mut acc = Shared::None;
let mut any = false;
visit_paragraphs(app, |p| {
any = true;
acc.add(Metric(field(p)));
});
if !any {
return text_style(app).map(|(_, p)| field(&p));
}
acc.value().map(|m| m.0)
}
/// A justification option shared by every paragraph in scope, or `None` when they differ.
pub(crate) fn shared_justification_f64(app: &VectorcraftApp, f: impl Fn(&JustificationOptions) -> f64) -> Option<f64> {
let mut acc = Shared::None;
let mut any = false;
visit_paragraphs(app, |p| {
any = true;
acc.add(Metric(f(&p.justification)));
});
if !any {
return text_style(app).map(|(_, p)| f(&p.justification));
}
acc.value().map(|m| m.0)
}
/// Single Word Justification shared by every paragraph in scope, or `None` when they differ.
pub(crate) fn shared_single_word_justify(app: &VectorcraftApp) -> Option<SingleWordJustify> {
shared_para(app, |p| p.justification.single_word)
}
/// A paragraph attribute shared by every paragraph in scope, or `None` when they differ.
pub(crate) fn shared_para<T: PartialEq>(app: &VectorcraftApp, field: impl Fn(&ParaStyle) -> T) -> Option<T> {
let mut acc = Shared::None;
let mut any = false;
visit_paragraphs(app, |p| {
any = true;
acc.add(field(p));
});
if !any {
return text_style(app).map(|(_, p)| field(&p));
}
acc.value()
}
/// The composer every paragraph in scope shares, or `None` when they differ (neither menu item checked).
fn shared_composer(app: &VectorcraftApp) -> Option<Composer> {
shared_para(app, |p| p.composer)
}
/// Hyphenation shared by every paragraph in scope, or `None` when they differ (checkbox off).
pub(crate) fn shared_hyphenate(app: &VectorcraftApp) -> Option<bool> {
shared_para(app, |p| p.hyphenate)
}
/// Plain text of paragraph `index` in `plain` (newline-separated paragraphs).
fn paragraph_plain_at(plain: &str, index: usize) -> &str {
let mut rest = plain;
let mut idx = 0;
while idx <= index && (!rest.is_empty() || idx == 0) {
let span = vectorcraft_text::edit::paragraph_at(rest, 0);
let part = rest.get(span.clone()).unwrap_or("");
if idx == index {
return part;
}
idx += 1;
if span.end >= rest.len() {
break;
}
rest = rest.get(span.end + 1..).unwrap_or("");
}
""
}
/// Whether the Paragraph Direction buttons would highlight RTL for this paragraph.
fn displayed_paragraph_rtl(text: &vectorcraft_doc::TextObject, index: usize) -> bool {
let plain = text.plain_text();
let para = text.para_at(index);
vectorcraft_text::paragraph_is_rtl(paragraph_plain_at(&plain, index), para.direction)
}
/// Collect the direction the Paragraph panel shows for each paragraph in `text`; `false` is LTR.
fn collect_text_direction(text: &vectorcraft_doc::TextObject, range: Option<(usize, usize)>, shared: &mut Option<bool>) -> bool {
let para_range = range.map(|(a, b)| text.paragraphs_in(a, b)).unwrap_or_else(|| 0..text.paragraph_count());
for i in para_range {
let rtl = displayed_paragraph_rtl(text, i);
if shared.is_some_and(|old| old != rtl) {
return false;
}
*shared = Some(rtl);
}
true
}
/// `None` when paragraphs in scope disagree on displayed direction (neither direction button on).
fn selected_paragraph_direction(app: &VectorcraftApp, fallback: &ParaStyle) -> Option<bool> {
let default = Some(resolved_rtl(app, fallback));
let Some(st) = app.session.active() else { return default };
let mut shared = None;
if let Some(texts) = story_texts(app) {
return if texts.into_iter().all(|t| collect_text_direction(t, None, &mut shared)) { shared.or(default) } else { None };
}
if let Some((id, a, b)) = super::character::text_editing(app)
&& let Some(NodeKind::Text(text)) = st.doc.node(id).map(|n| &n.kind)
{
return if collect_text_direction(text, Some((a, b)), &mut shared) { shared.or(default) } else { None };
}
let texts = super::character::selected_story_texts(app);
if texts.into_iter().all(|t| collect_text_direction(t, None, &mut shared)) { shared.or(default) } else { None }
}
/// A hyphenation option shared by every paragraph in scope, or `None` when they differ.
pub(crate) fn shared_hyphenation_field<T: PartialEq + Copy>(app: &VectorcraftApp, f: impl Fn(&HyphenationOptions) -> T) -> Option<T> {
shared_para(app, |p| f(&p.hyphenation))
}
/// The hyphenation zone shared by every paragraph in scope, or `None` when they differ.
pub(crate) fn shared_hyphenation_zone(app: &VectorcraftApp) -> Option<f64> {
shared_para_field(app, |p| p.hyphenation.zone)
}
/// None means differing displayed alignments, following the other mixed-value controls.
fn selected_alignment(app: &VectorcraftApp, fallback: &ParaStyle) -> Option<Justify> {
let default = Some(shown_alignment(fallback.justify, fallback.direction == Some(ParaDirection::RightToLeft)));
let Some(st) = app.session.active() else { return default };
let mut shared = None;
if let Some(texts) = story_texts(app) {
return if texts.into_iter().all(|t| collect_text_alignment(t, None, &mut shared)) { shared.or(default) } else { None };
}
if let Some((id, a, b)) = super::character::text_editing(app)
&& let Some(NodeKind::Text(text)) = st.doc.node(id).map(|n| &n.kind)
{
return if collect_text_alignment(text, Some((a, b)), &mut shared) { shared.or(default) } else { None };
}
let texts = super::character::selected_story_texts(app);
if texts.into_iter().all(|t| collect_text_alignment(t, None, &mut shared)) { shared.or(default) } else { None }
}
/// The seven alignment buttons shared by Paragraph and Properties; no button is selected
/// when the paragraphs they act on have differing displayed alignments.
pub fn alignment_buttons(app: &mut VectorcraftApp, ui: &mut Ui, para: &ParaStyle, size: f32) {
first_alignment_buttons(app, ui, para, size, ALIGNMENTS.len());
}
/// The first `count` of the [`ALIGNMENTS`] buttons.
fn first_alignment_buttons(app: &mut VectorcraftApp, ui: &mut Ui, para: &ParaStyle, size: f32, count: usize) {
let shown = selected_alignment(app, para);
for (j, icon, tip, id) in ALIGNMENTS.into_iter().take(count) {
if widgets::icon_button(ui, icon, tip, shown == Some(j), size).clicked() {
para_cmd(app, "text.setStyle", json!({"justify": id}));
}
}
}
/// The Control bar's alignment buttons: Align left, Align center and Align right. The public
/// documentation has the Control panel format paragraphs while type is selected, with a way to the
/// Paragraph panel (<https://helpx.adobe.com/illustrator/using/formatting-paragraphs.html>), and
/// its alignment buttons align text as the Paragraph panel's do
/// (<https://helpx.adobe.com/illustrator/using/align-text.html>). The bar keeps to the three that
/// don't justify, for its room; all seven are in the Paragraph options the link opens.
const CONTROL_BAR_ALIGNMENTS: usize = 3;
/// The spacing between the Control bar's alignment buttons, as in the Paragraph panel's row.
const CONTROL_BAR_BUTTON_SPACING: f32 = 3.0;
/// The width of the Paragraph panel in the Control bar's popover, as the Character panel's.
const CONTROL_BAR_POPOVER_WIDTH: f32 = 260.0;
/// The Control bar's Paragraph link, as it is drawn and measured.
fn control_bar_link_text(ui: &Ui) -> egui::RichText {
egui::RichText::new(tl!("Paragraph:")).size(12.0).color(Tokens::get(ui.ctx()).text).underline()
}
/// The Control bar's paragraph controls while type is selected or edited (#5372), after its
/// Character controls: the Paragraph link, which opens the Paragraph panel in a popover under it,
/// and the Align left, Align center and Align right buttons, `size` points square as the bar's others. `reserve` gives the room the controls
/// after them keep, asked only when type is selected: short of room, the buttons are left out,
/// then the link too, each noted with [`crate::chrome::left_out`]. Returns whether the link shows.
pub(crate) fn control_bar(app: &mut VectorcraftApp, ui: &mut Ui, size: f32, reserve: impl FnOnce(&Ui) -> f32) -> bool {
let Some((_, para)) = text_style(app) else { return false };
let text = control_bar_link_text(ui);
let link_width = widgets::text_width(ui, text.clone());
let room = ui.available_width() - reserve(ui);
let link_room = link_width + ui.spacing().item_spacing.x;
if room < link_room {
crate::chrome::left_out(ui);
return false;
}
let link = ui.link(text).on_hover_text(tl!("Paragraph options"));
widgets::popover(&link, link.clicked(), |ui| {
ui.set_width(CONTROL_BAR_POPOVER_WIDTH);
show(app, ui);
});
if room >= link_room + (size + CONTROL_BAR_BUTTON_SPACING) * CONTROL_BAR_ALIGNMENTS as f32 {
let spacing = ui.spacing().item_spacing.x;
ui.spacing_mut().item_spacing.x = CONTROL_BAR_BUTTON_SPACING;
first_alignment_buttons(app, ui, ¶, size, CONTROL_BAR_ALIGNMENTS);
ui.spacing_mut().item_spacing.x = spacing;
} else {
crate::chrome::left_out(ui);
}
true
}
/// Type › Bullets and Numbering › Apply Bullets (`type.applyBullets {bullet?}`): the paragraphs of
/// the selected type, or those the Type tool's selection touches, become a bulleted list with
/// `bullet`; without it, a list keeps its bullet and other paragraphs take the dot.
pub(crate) fn apply_bullets(app: &mut VectorcraftApp, p: &Value) -> Result<Value, String> {
text_style(app).ok_or("select some type first")?;
let params = match p.get("bullet") {
Some(b) => json!({ "bullet": b }),
None => json!({ "list": "bullet" }),
};
run_on_paragraphs(app, "text.setFormat", params)
}
/// Type › Bullets and Numbering › Apply Numbers (`type.applyNumbers {number?}`): the paragraphs of
/// the selected type, or those the Type tool's selection touches, become a numbered list with
/// number style `number`; without it, a numbered list keeps its style and other paragraphs are
/// numbered 1, 2, 3.
pub(crate) fn apply_numbers(app: &mut VectorcraftApp, p: &Value) -> Result<Value, String> {
text_style(app).ok_or("select some type first")?;
let params = match p.get("number") {
Some(n) => json!({ "number": n }),
None => json!({ "list": "number" }),
};
run_on_paragraphs(app, "text.setFormat", params)
}
/// The Bullets presets, in the order they are listed: (name, `text.setFormat`'s `bullet`, bullet;
/// None for the preset that turns the list off).
pub(crate) fn bullet_presets() -> [(&'static str, &'static str, Option<vectorcraft_doc::text::Bullet>); 5] {
use vectorcraft_doc::text::Bullet;
[
(tl!("None"), "none", None),
(tl!("Filled Dot"), "dot", Some(Bullet::Dot)),
(tl!("Dash"), "dash", Some(Bullet::Dash)),
(tl!("Hollow Circle"), "circle", Some(Bullet::Circle)),
(tl!("Filled Square"), "square", Some(Bullet::Square)),
]
}
/// The first `n` numbers of number style `style`, as the Numbered List presets show them ("1, 2,
/// 3").
pub(crate) fn number_sample(style: vectorcraft_doc::text::NumberStyle, n: u32) -> String {
(1..=n).map(|k| style.format(k)).collect::<Vec<_>>().join(", ")
}
/// Type › Bullets and Numbering › Convert to Text (`type.convertListToText`): the markers of the
/// list paragraphs the Paragraph panel acts on ([`run_on_paragraphs`]: with the Selection tool,
/// every frame of the selected stories) become characters of the text and the lists are turned
/// off (`text.convertListToText`, which also moves the Type tool's anchor and caret past the
/// characters inserted at or before them).
pub(crate) fn convert_list_to_text(app: &mut VectorcraftApp) -> Result<Value, String> {
text_style(app).ok_or("select some type first")?;
run_on_paragraphs(app, "text.convertListToText", json!({}))
}
/// The Bullets button and the arrow beside it (Paragraph panel). The button makes the paragraphs
/// the panel acts on a bulleted list (`text.setFormat {list: "bullet"}`) and shows pressed while
/// they all are one. The arrow lists the presets (`{bullet}`): None turns the list off, and the
/// one every paragraph has is checked. More Options after them opens Bullets and Numbering on its
/// Bullets tab.
pub fn bullets_button(app: &mut VectorcraftApp, ui: &mut Ui, size: f32) {
let t = Tokens::get(ui.ctx());
let shown = shared_para(app, |p| p.list.filter(|l| l.number.is_none()).map(|l| l.bullet));
if widgets::icon_button(ui, "dc-para-bullets", tl!("Bullets"), matches!(shown, Some(Some(_))), size).clicked() {
format(app, json!({"list": "bullet"}));
}
let (rect, arrow) = ui.allocate_exact_size(vec2(14.0, size), egui::Sense::click());
if arrow.hovered() {
ui.painter().rect_filled(rect, egui::CornerRadius::same(4), t.hover);
}
crate::icons::paint(ui, "chevron-down", egui::Rect::from_center_size(rect.center(), vec2(10.0, 10.0)), t.text_dim);
arrow.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, ui.is_enabled(), tl!("Bullets")));
let arrow = arrow.on_hover_text(tl!("Bullets"));
let mut more = false;
let picked = egui::Popup::menu(&arrow).show(|ui| {
let mut picked = None;
for (name, key, bullet) in bullet_presets() {
if bullet_row(ui, name, bullet, shown == Some(bullet)) {
picked = Some(key);
}
}
more = more_options_row(ui);
picked
});
if let Some(key) = picked.and_then(|r| r.inner) {
format(app, json!({ "bullet": key }));
}
if more {
more_options(app, "bullets");
}
}
/// A row of the Bullets presets: a check when `checked`, the bullet drawn (a slash for None), then
/// its name. Whether it was clicked.
fn bullet_row(ui: &mut Ui, name: &str, bullet: Option<vectorcraft_doc::text::Bullet>, checked: bool) -> bool {
let t = Tokens::get(ui.ctx());
let (rect, resp) = ui.allocate_exact_size(vec2(130.0, 22.0), egui::Sense::click());
if resp.hovered() {
ui.painter().rect_filled(rect, egui::CornerRadius::same(4), t.hover);
}
let painter = ui.painter();
let font = egui::FontId::proportional(12.5);
if checked {
painter.text(rect.left_center() + vec2(6.0, 0.0), egui::Align2::LEFT_CENTER, "✓", font.clone(), t.text_strong);
}
paint_bullet(painter, rect.left_center() + vec2(28.0, 0.0), bullet, t.text_strong);
painter.text(rect.left_center() + vec2(42.0, 0.0), egui::Align2::LEFT_CENTER, name, font, t.text_strong);
resp.widget_info(|| egui::WidgetInfo::selected(egui::Role::RadioButton, ui.is_enabled(), checked, name));
resp.clicked()
}
/// Draw `bullet` centered on `c` in `color` as the presets show it (a slash for None).
pub(crate) fn paint_bullet(painter: &egui::Painter, c: egui::Pos2, bullet: Option<vectorcraft_doc::text::Bullet>, color: egui::Color32) {
use vectorcraft_doc::text::Bullet;
let stroke = egui::Stroke::new(1.5, color);
match bullet {
None => painter.line_segment([c + vec2(-5.0, 5.0), c + vec2(5.0, -5.0)], stroke),
Some(Bullet::Dot) => painter.circle_filled(c, 3.0, color),
Some(Bullet::Dash) => painter.line_segment([c - vec2(4.0, 0.0), c + vec2(4.0, 0.0)], stroke),
Some(Bullet::Circle) => painter.circle_stroke(c, 3.0, egui::Stroke::new(1.2, color)),
Some(Bullet::Square) => painter.rect_filled(egui::Rect::from_center_size(c, vec2(6.0, 6.0)), 0.0, color),
};
}
/// More Options at the end of the Bullets and Numbered List presets, under a line. Whether it was
/// clicked.
fn more_options_row(ui: &mut Ui) -> bool {
let t = Tokens::get(ui.ctx());
ui.separator();
let (rect, resp) = ui.allocate_exact_size(vec2(130.0, 22.0), egui::Sense::click());
if resp.hovered() {
ui.painter().rect_filled(rect, egui::CornerRadius::same(4), t.hover);
}
let name = tl!("More Options");
ui.painter().text(rect.left_center() + vec2(22.0, 0.0), egui::Align2::LEFT_CENTER, name, egui::FontId::proportional(12.5), t.text_strong);
resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, ui.is_enabled(), name));
resp.clicked()
}
/// More Options: open Bullets and Numbering on `tab` (`ui.bulletsAndNumbering`), or show why it
/// can't open.
fn more_options(app: &mut VectorcraftApp, tab: &str) {
if let Err(e) = app.run("ui.bulletsAndNumbering", json!({ "tab": tab })) {
app.status(e);
}
}
/// The Numbered List button and the arrow beside it (Paragraph panel). The button makes the
/// paragraphs the panel acts on a numbered list (`text.setFormat {list: "number"}`) and shows
/// pressed while they all are one. The arrow lists the presets (`{number}`), each shown by its
/// first three numbers: None turns the list off, and the one every paragraph has is checked. More
/// Options after them opens Bullets and Numbering on its Numbers tab.
pub fn numbers_button(app: &mut VectorcraftApp, ui: &mut Ui, size: f32) {
use vectorcraft_doc::text::NumberStyle;
let t = Tokens::get(ui.ctx());
let shown = shared_para(app, |p| p.list.and_then(|l| l.number));
if widgets::icon_button(ui, "dc-para-numbers", tl!("Numbered List"), matches!(shown, Some(Some(_))), size).clicked() {
format(app, json!({"list": "number"}));
}
let (rect, arrow) = ui.allocate_exact_size(vec2(14.0, size), egui::Sense::click());
if arrow.hovered() {
ui.painter().rect_filled(rect, egui::CornerRadius::same(4), t.hover);
}
crate::icons::paint(ui, "chevron-down", egui::Rect::from_center_size(rect.center(), vec2(10.0, 10.0)), t.text_dim);
arrow.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, ui.is_enabled(), tl!("Numbered List")));
let arrow = arrow.on_hover_text(tl!("Numbered List"));
let mut more = false;
let picked = egui::Popup::menu(&arrow).show(|ui| {
let mut picked = None;
if bullet_row(ui, tl!("None"), None, shown == Some(None)) {
picked = Some(None);
}
for style in NumberStyle::ALL {
if number_row(ui, &number_sample(style, 3), shown == Some(Some(style))) {
picked = Some(Some(style));
}
}
more = more_options_row(ui);
picked
});
if let Some(style) = picked.and_then(|r| r.inner) {
format(app, json!({ "number": style.map_or(json!("none"), |s| json!(s)) }));
}
if more {
more_options(app, "numbers");
}
}
/// A row of the Numbered List presets: a check when `checked`, then the style's first numbers
/// (`sample`). Whether it was clicked.
fn number_row(ui: &mut Ui, sample: &str, checked: bool) -> bool {
let t = Tokens::get(ui.ctx());
let (rect, resp) = ui.allocate_exact_size(vec2(130.0, 22.0), egui::Sense::click());
if resp.hovered() {
ui.painter().rect_filled(rect, egui::CornerRadius::same(4), t.hover);
}
let painter = ui.painter();
let font = egui::FontId::proportional(12.5);
if checked {
painter.text(rect.left_center() + vec2(6.0, 0.0), egui::Align2::LEFT_CENTER, "✓", font.clone(), t.text_strong);
}
painter.text(rect.left_center() + vec2(22.0, 0.0), egui::Align2::LEFT_CENTER, sample, font, t.text_strong);
resp.widget_info(|| egui::WidgetInfo::selected(egui::Role::RadioButton, ui.is_enabled(), checked, sample));
resp.clicked()
}
/// The Hyphenate checkbox for the paragraphs the panel acts on.
pub fn hyphenate_check(app: &mut VectorcraftApp, ui: &mut Ui, _para: &ParaStyle) {
let hyphenate = shared_hyphenate(app);
if widgets::check(ui, tl!("Hyphenate"), hyphenate == Some(true), true) {
format(app, json!({"hyphenate": hyphenate != Some(true)}));
}
}
pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
let t = Tokens::get(ui.ctx());
let Some((_, para)) = text_style(app) else {
super::empty_state(ui, "pilcrow", tl!("No text selected"), tl!("Select a text object to edit its paragraph attributes."));
return;
};
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = 3.0;
alignment_buttons(app, ui, ¶, 28.0);
// Paragraph direction, with the Middle Eastern (Indic) options.
if app.session.prefs.show_indic_options {
ui.add_space(6.0);
let direction = selected_paragraph_direction(app, ¶);
for (to_rtl, icon, tip, id) in [
(false, "dc-para-ltr", tl!("Left-to-Right Paragraph Direction"), "leftToRight"),
(true, "dc-para-rtl", tl!("Right-to-Left Paragraph Direction"), "rightToLeft"),
] {
if widgets::icon_button(ui, icon, tip, direction == Some(to_rtl), 28.0).clicked() {
format(app, json!({"direction": id}));
}
}
}
});
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = 0.0;
bullets_button(app, ui, 28.0);
ui.add_space(6.0);
numbers_button(app, ui, 28.0);
});
ui.add_space(4.0);
let fw = ((ui.available_width() - 66.0) / 2.0).clamp(60.0, 100.0);
// Indents and paragraph spacing are distances (General); type sizes follow Units ▸ Type.
let unit = app.session.general_unit();
let label = |ui: &mut Ui, s: &str, tip: &str| {
let l = ui.add_sized(vec2(22.0, 24.0), egui::Label::new(egui::RichText::new(s).size(11.5).strong().color(t.text))).on_hover_text(tip);
crate::scrub::note_label(ui, l.rect);
};
egui::Grid::new("para-grid").num_columns(4).spacing([4.0, 4.0]).show(ui, |ui| {
label(ui, "→|", tl!("Left Indent"));
if let Some(v) =
widgets::spin_field_named(ui, "pa-li", Some(tl!("Left Indent")), shared_para_field(app, |p| p.left_indent), unit, fw, 1.0, -1296.0, &[])
{
format(app, json!({"leftIndent": v}));
}
label(ui, "|←", tl!("Right Indent"));
if let Some(v) =
widgets::spin_field_named(ui, "pa-ri", Some(tl!("Right Indent")), shared_para_field(app, |p| p.right_indent), unit, fw, 1.0, -1296.0, &[])
{
format(app, json!({"rightIndent": v}));
}
ui.end_row();
label(ui, "1→", tl!("First-line Left Indent"));
if let Some(v) = widgets::spin_field_named(
ui,
"pa-fi",
Some(tl!("First-line Left Indent")),
shared_para_field(app, |p| p.first_line_indent),
unit,
fw,
1.0,
-1296.0,
&[],
) {
format(app, json!({"firstLineIndent": v}));
}
ui.label("");
ui.label("");
ui.end_row();
label(ui, "↑¶", tl!("Space Before Paragraph"));
if let Some(v) = widgets::spin_field_named(
ui,
"pa-sb",
Some(tl!("Space Before Paragraph")),
shared_para_field(app, |p| p.space_before),
unit,
fw,
1.0,
0.0,
&[],
) {
format(app, json!({"spaceBefore": v}));
}
label(ui, "↓¶", tl!("Space After Paragraph"));
if let Some(v) = widgets::spin_field_named(
ui,
"pa-sa",
Some(tl!("Space After Paragraph")),
shared_para_field(app, |p| p.space_after),
unit,
fw,
1.0,
0.0,
&[],
) {
format(app, json!({"spaceAfter": v}));
}
ui.end_row();
});
ui.add_space(4.0);
if pstate::<bool>(ui.ctx(), "pa-hide-options") {
return;
}
hyphenate_check(app, ui, ¶);
// How long the kashidas are that justify Arabic lines, with the Middle Eastern (Indic) options.
if app.session.prefs.show_indic_options {
ui.horizontal(|ui| {
use vectorcraft_doc::text::Kashida;
widgets::dim_label(ui, tl!("Insert Kashidas"));
let kinds = [
(tl!("None"), Kashida::None, "none"),
(tl!("Short"), Kashida::Short, "short"),
(tl!("Medium"), Kashida::Medium, "medium"),
(tl!("Long"), Kashida::Long, "long"),
(tl!("Stylistic"), Kashida::Stylistic, "stylistic"),
];
let names = kinds.map(|k| k.0);
let current = kinds.iter().find(|k| k.1 == para.kashida).map_or(names[0], |k| k.0);
// Already translated: shown as they are.
if let Some(i) = widgets::dropdown_names(ui, "pa-kashida", current, &names, ui.available_width() - 4.0)
&& let Some((_, _, key)) = kinds.get(i)
{
format(app, json!({"kashida": key}));
}
});
}
// Japanese composition: how punctuation is spaced (JLREQ 3.1), with the East Asian options.
if app.session.prefs.show_east_asian_options {
ui.horizontal(|ui| {
widgets::dim_label(ui, tl!("Mojikumi Set"));
let sets = mojikumi_sets();
let names = sets.map(|s| s.0);
let current = shared_para(app, |p| p.mojikumi).and_then(|m| sets.iter().find(|s| s.1 == m).map(|s| s.0)).unwrap_or("");
// Already translated: shown as they are.
if let Some(i) = widgets::dropdown_names(ui, "pa-mojikumi", current, &names, ui.available_width() - 4.0)
&& let Some((_, _, key)) = sets.get(i)
{
format(app, json!({"mojikumi": key}));
}
});
// Which characters may not start or end a line.
ui.horizontal(|ui| {
widgets::dim_label(ui, tl!("Kinsoku Set"));
let sets = [(tl!("None"), "None", "none"), (tl!("Hard"), "Hard", "hard"), (tl!("Soft"), "Soft", "soft")];
let custom: Vec<String> = app.session.active().map(|st| st.doc.kinsoku_sets.iter().map(|k| k.name.clone()).collect()).unwrap_or_default();
let mut names: Vec<&str> = sets.iter().map(|s| s.0).collect();
names.extend(custom.iter().map(String::as_str));
names.push(tl!("Kinsoku Shori Settings…"));
let current = shared_kinsoku_name(app);
let current = current.as_deref().map_or("", |n| sets.iter().find(|s| s.1 == n).map_or(n, |s| s.0));
// Already translated: shown as they are.
if let Some(i) = widgets::dropdown_names(ui, "pa-kinsoku", current, &names, ui.available_width() - 4.0) {
if let Some((_, _, key)) = sets.get(i) {
format(app, json!({"kinsoku": key}));
} else if let Some(name) = custom.get(i - sets.len()) {
format(app, json!({"kinsoku": name}));
} else {
crate::menus::invoke(app, crate::dialogs::kinsoku_sets::COMMAND, json!({}));
}
}
});
}
}
pub fn menu(app: &mut VectorcraftApp, ui: &mut Ui) {
let style = text_style(app);
let has = style.is_some();
let hidden: bool = pstate(ui.ctx(), "pa-hide-options");
if menu_item(ui, if hidden { tl!("Show Options") } else { tl!("Hide Options") }, true, false) {
set_pstate(ui.ctx(), "pa-hide-options", !hidden);
}
ui.separator();
// Quotation marks, hyphens, full stops and commas at a line's ends hang outside the margin.
let roman = style.as_ref().is_some_and(|(_, p)| p.roman_hanging);
if menu_item(ui, tl!("Roman Hanging Punctuation"), has, roman) {
format(app, json!({"romanHangingPunctuation": !roman}));
}
if menu_item(ui, tl!("Justification…"), has, false)
&& let Err(e) = app.run("ui.justification", json!({}))
{
app.status(e);
}
if menu_item(ui, tl!("Hyphenation…"), has, false)
&& let Err(e) = app.run("ui.hyphenation", json!({}))
{
app.status(e);
}
// How leading is measured (Japanese layout measures it from em box top to em box top), with
// the East Asian options.
if app.session.prefs.show_east_asian_options {
ui.separator();
let top_to_top = style.as_ref().is_some_and(|(_, p)| p.leading_model == vectorcraft_doc::LeadingModel::EmBoxTop);
if menu_item(ui, tl!("Top-to-Top Leading"), has, top_to_top) {
format(app, json!({"leadingModel": "emBoxTop"}));
}
if menu_item(ui, tl!("Bottom-to-Bottom Leading"), has, has && !top_to_top) {
format(app, json!({"leadingModel": "romanBaseline"}));
}
// Hanging punctuation: a comma or full stop ending a line stands outside it.
let hang = style.as_ref().map(|(_, p)| p.burasagari);
ui.add_enabled_ui(has, |ui| {
// Indented like the items beside it (their check column).
ui.menu_button(format!(" {}", tl!("Burasagari")), |ui| {
use vectorcraft_doc::Burasagari;
for (label, b, key) in [
(tl!("None"), Burasagari::None, "none"),
(tl!("Regular"), Burasagari::Standard, "standard"),
(tl!("Force"), Burasagari::Forced, "forced"),
] {
if menu_item(ui, label, true, hang == Some(b)) {
format(app, json!({"burasagari": key}));
}
}
});
});
// A repeat mark starting a line is drawn as the character it repeats.
let repeat = shared_para(app, |p| p.kurikaeshi_moji_shori) == Some(true);
if menu_item(ui, tl!("Kurikaeshi Moji Shori"), has, repeat) {
toggle_kurikaeshi_moji_shori(app);
}
// How a line keeps the kinsoku characters off its ends: the selected paragraphs' type is
// checked, and none is when they differ.
let kinsoku_type = shared_para(app, |p| p.kinsoku_type);
ui.add_enabled_ui(has, |ui| {
ui.menu_button(format!(" {}", tl!("Kinsoku Shori Type")), |ui| {
for (label, t, key) in [
(tl!("Push In First"), KinsokuShoriType::PushInFirst, "pushInFirst"),
(tl!("Push Out First"), KinsokuShoriType::PushOutFirst, "pushOutFirst"),
(tl!("Push Out Only"), KinsokuShoriType::PushOutOnly, "pushOutOnly"),
] {
if menu_item(ui, label, true, kinsoku_type == Some(t)) {
set_kinsoku_type(app, key);
}
}
});
});
// Inseparable pairs stay on one line; only with a kinsoku set.
let (kinsoku_on, bunri) = bunri_kinshi_item(app);
if menu_item(ui, tl!("Bunri-Kinshi"), has && kinsoku_on, bunri) {
toggle_bunri_kinshi(app);
}
// Korean lines break between any two syllables, not only between words.
let anywhere = shared_para(app, |p| p.korean_line_break_anywhere) == Some(true);
if menu_item(ui, tl!("Korean Line Break Anywhere"), has, anywhere) {
toggle_korean_line_break_anywhere(app);
}
}
ui.separator();
let composer = shared_composer(app);
for (c, label) in [
(Composer::SingleLine, tl!("Middle Eastern & South Asian Single-line Composer")),
(Composer::EveryLine, tl!("Middle Eastern & South Asian Every-line Composer")),
(Composer::StandardSingleLine, tl!("Standard Single-line Composer")),
(Composer::StandardEveryLine, tl!("Standard Every-line Composer")),
] {
if menu_item(ui, label, has, composer == Some(c)) {
format(app, json!({ "composer": composer_id(c) }));
}
}
ui.separator();
if menu_item(ui, tl!("Reset Panel"), has, false) {
reset_panel(app);
}
}
/// Kinsoku Shori Type (`key`: `"pushInFirst"`, `"pushOutFirst"` or `"pushOutOnly"`) for the
/// paragraphs the panel acts on ([`format`]), in one undo step.
fn set_kinsoku_type(app: &mut VectorcraftApp, key: &str) {
format(app, json!({"kinsokuType": key}));
}
/// The Mojikumi Set menu's entries, translated: each one's label, its set and its `mojikumi` value
/// for `text.setFormat`. The five predefined sets come first, then Line-end Punctuation Half Width,
/// the set new type gets.
fn mojikumi_sets() -> [(&'static str, Mojikumi, &'static str); 6] {
[
(tl!("None"), Mojikumi::None, "none"),
(tl!("YakumonoHankaku"), Mojikumi::YakumonoHankaku, "yakumonoHankaku"),
(tl!("GyomatsuYakumonoHankaku"), Mojikumi::GyomatsuYakumonoHankaku, "gyomatsuYakumonoHankaku"),
(tl!("GyomatsuYakumonoZenkaku"), Mojikumi::GyomatsuYakumonoZenkaku, "gyomatsuYakumonoZenkaku"),
(tl!("YakumonoZenkaku"), Mojikumi::YakumonoZenkaku, "yakumonoZenkaku"),
(tl!("Line-end Punctuation Half Width"), Mojikumi::LineEndHalf, "lineEndHalf"),
]
}
/// Bunri-Kinshi's menu item for the paragraphs the panel acts on ([`format`]): (available, checked).
/// It is available when none of them has kinsoku None, and checked when every one of them has it.
fn bunri_kinshi_item(app: &VectorcraftApp) -> (bool, bool) {
(shared_para(app, ParaStyle::kinsoku_on) == Some(true), shared_para(app, |p| p.bunri_kinshi) == Some(true))
}
/// The Kinsoku set every paragraph the panel acts on ([`format`]) has, by name (None, Hard, Soft or
/// a custom set's), or `None` when they differ.
fn shared_kinsoku_name(app: &VectorcraftApp) -> Option<String> {
let mut first: Option<String> = None;
let mut differ = false;
visit_paragraphs(app, |p| match &first {
None => first = Some(p.kinsoku_name().to_string()),
Some(n) => differ |= n != p.kinsoku_name(),
});
match first {
None => text_style(app).map(|(_, p)| p.kinsoku_name().to_string()),
Some(_) if differ => None,
n => n,
}
}
/// Bunri-Kinshi (Paragraph panel menu): a click turns it off for all the paragraphs the panel acts
/// on ([`format`]) when every one of them has it, and on for all of them otherwise, in one undo step.
fn toggle_bunri_kinshi(app: &mut VectorcraftApp) {
let on = shared_para(app, |p| p.bunri_kinshi) == Some(true);
format(app, json!({"bunriKinshi": !on}));
}
/// Kurikaeshi Moji Shori (Paragraph panel menu): the menu item is checked when every paragraph the
/// panel acts on ([`format`]) has it, and a click turns it off for all of them then, and on for all
/// of them otherwise, in one undo step.
fn toggle_kurikaeshi_moji_shori(app: &mut VectorcraftApp) {
let on = shared_para(app, |p| p.kurikaeshi_moji_shori) == Some(true);
format(app, json!({"kurikaeshiMojiShori": !on}));
}
/// Korean Line Break Anywhere (Paragraph panel menu): the menu item is checked when every paragraph
/// the panel acts on ([`format`]) has it, and a click turns it off for all of them then, and on for
/// all of them otherwise, in one undo step.
fn toggle_korean_line_break_anywhere(app: &mut VectorcraftApp) {
let on = shared_para(app, |p| p.korean_line_break_anywhere) == Some(true);
format(app, json!({"koreanLineBreakAnywhere": !on}));
}
/// Reset Panel: the paragraphs the panel acts on ([`format`]) take Auto alignment, no list, no
/// indents or paragraph spacing, Hyphenate off with the default Hyphenation and Justification
/// options, Auto direction, Bottom-to-Bottom Leading, the Line-end Punctuation Half Width Mojikumi
/// Set, Regular burasagari, the Hard kinsoku set, Push Out Only, the Every-line Composer, Bunri-Kinshi,
/// Kurikaeshi Moji Shori, Roman Hanging Punctuation and Korean Line Break Anywhere off, no kashidas and no tab stops, as one undo
/// step named Reset Panel (inside an undo group that is already open, as part of its step).
fn reset_panel(app: &mut VectorcraftApp) {
let h = HyphenationOptions::default();
let j = vectorcraft_doc::JustificationOptions::default();
// The Type tool's typing session stays an undo step of its own.
super::character::end_typing(app);
let open = app.session.active().is_some_and(|st| st.undo_group.is_some());
if !open {
app.session.begin_named_undo_group(Some("Reset Panel".into()));
}
para_cmd(app, "text.setStyle", json!({"justify": "auto"}));
format(
app,
json!({
"list": "none", "leftIndent": 0, "rightIndent": 0, "firstLineIndent": 0, "spaceBefore": 0, "spaceAfter": 0, "hyphenate": false,
"wordsLongerThan": h.words_longer_than, "afterFirst": h.after_first, "beforeLast": h.before_last,
"hyphenLimit": h.hyphen_limit, "hyphenationZone": h.zone, "hyphenateCapitalized": h.capitalized, "kashida": "none",
"wordSpacing": j.word_spacing, "letterSpacing": j.letter_spacing, "glyphScaling": j.glyph_scaling,
"autoLeading": j.auto_leading, "singleWordJustification": "fullJustify",
"direction": "auto", "leadingModel": "romanBaseline", "mojikumi": "lineEndHalf", "burasagari": "standard", "kinsoku": "hard",
"kinsokuType": "pushOutOnly", "composer": "everyLine", "romanHangingPunctuation": false, "koreanLineBreakAnywhere": false,
"bunriKinshi": false, "kurikaeshiMojiShori": false
}),
);
para_cmd(app, "text.tabs.clear", json!({}));
// Closes the active document's undo group only, so an undo group open in another document
// stays open.
if !open && let Some(st) = app.session.active_mut() {
st.undo_group = None;
}
}
#[cfg(test)]
mod tests;
No file is open. Choose one in the tree, or press Ctrl+O (⌘O on a Mac) to find one by name.
Syntax highlighting could not load. The unmodified read-only text remains available above.