mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-18 06:39:10 +00:00
i18n: resolution fields show the ppi unit in the interface language
Flatten Transparency, Document Raster Effects Settings, Effect ›
Rasterize, the PNG options, the PDF save dialog's Compression section
and the Place dialog's file line wrote "ppi" as a literal, so a French
interface showed "ppi" where the catalogs write "ppp".
They now take the unit from the existing `{n} ppi` row, which every
catalog already translates. The dialog fields use a new `ppi_suffix`
helper next to `unit_suffix` in `dialogs/form.rs`. A field reads the
unit it shows, so in French "200 ppp" reads as 200.
Part of #7991, reported by bortekfr.
This commit is contained in:
@@ -167,7 +167,7 @@ pub(crate) use export_for_screens::{
|
||||
open_assets as open_export_for_screens_assets, preset_list as screen_preset_list, saved_options as screen_saved_options,
|
||||
saved_rows as screen_saved_rows, settings_format as screen_settings_format,
|
||||
};
|
||||
pub(crate) use form::{humanize, number_list_text, plugin_color_button, read_number_list, unit_suffix};
|
||||
pub(crate) use form::{humanize, number_list_text, plugin_color_button, ppi_suffix, read_number_list, unit_suffix};
|
||||
pub use new_document::{create_without_dialog as new_document_without_dialog, open as open_new_document, preset_card};
|
||||
pub(crate) use new_document::{popup_open as new_document_popup_open, preset_name};
|
||||
pub use png_options::open as open_raster_options;
|
||||
|
||||
@@ -220,7 +220,7 @@ pub(crate) fn options_editor(ui: &mut egui::Ui, id: &str, o: &mut FlattenOptions
|
||||
if let Some(n) = widgets::range_field_named(ui, (id, key), Some(label), *v, 1.0..=2400.0, "", 0, FIELD) {
|
||||
*v = n;
|
||||
}
|
||||
dim(ui, "ppi");
|
||||
dim(ui, super::ppi_suffix().trim());
|
||||
});
|
||||
ui.end_row();
|
||||
}
|
||||
|
||||
@@ -778,6 +778,12 @@ pub(crate) fn unit_suffix(unit: &str) -> String {
|
||||
if unit.is_empty() || unit.starts_with(['°', '%', '′', '″']) { unit.to_string() } else { format!(" {unit}") }
|
||||
}
|
||||
|
||||
/// What a resolution field shows after its value in the interface language: the `{n} ppi` row
|
||||
/// without its number (" ppi", French " ppp").
|
||||
pub(crate) fn ppi_suffix() -> String {
|
||||
crate::i18n::fill_once(tl!("{n} ppi"), &[("n", "")])
|
||||
}
|
||||
|
||||
/// A number of a list as [`value_field`] shows it: at most 4 decimals, without trailing zeros.
|
||||
fn number_text(x: f64) -> String {
|
||||
let s = format!("{x:.4}");
|
||||
@@ -1154,6 +1160,27 @@ mod plugin_field_tests {
|
||||
assert_eq!([shown("mm"), shown("°"), shown("%"), shown("1/min"), shown("µm")], ["5 mm", "5°", "5%", "5 1/min", "5 µm"]);
|
||||
}
|
||||
|
||||
/// A resolution field's unit is the `{n} ppi` row without its number.
|
||||
#[test]
|
||||
fn the_ppi_unit_is_translated() {
|
||||
assert_eq!(ppi_suffix(), " ppi");
|
||||
crate::i18n::set_current(crate::i18n::Lang::from_code("fr").unwrap());
|
||||
let fr = ppi_suffix();
|
||||
crate::i18n::set_current(crate::i18n::Lang::EN);
|
||||
assert_eq!(fr, " ppp");
|
||||
}
|
||||
|
||||
/// [`ppi_suffix`] drops the number from the `{n} ppi` row, so every catalog writes the number
|
||||
/// first and then a space before the unit.
|
||||
#[test]
|
||||
fn every_catalog_writes_the_ppi_unit_after_the_number() {
|
||||
for lang in crate::i18n::Lang::all() {
|
||||
let row = crate::i18n::tr(lang, "{n} ppi");
|
||||
let unit = row.strip_prefix("{n}").unwrap_or_default();
|
||||
assert!(unit.starts_with(char::is_whitespace) && !unit.trim().is_empty(), "{}: {row:?}", lang.code());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn number_lists_read_as_typed_within_their_limits() {
|
||||
let spec = ParamSpec::NumberList { min: 0.0, max: 360.0, default: vec![45.0], min_items: 1, max_items: 3 };
|
||||
|
||||
@@ -148,7 +148,7 @@ fn body(_: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> bool {
|
||||
d.fields.insert("__otherPpi".into(), json!(i == 3));
|
||||
}
|
||||
if preset.is_none()
|
||||
&& let Some(v) = widgets::range_field(ui, "ro-ppi-other", ppi, 1.0..=4608.0, " ppi", 0, 80.0)
|
||||
&& let Some(v) = widgets::range_field(ui, "ro-ppi-other", ppi, 1.0..=4608.0, &super::ppi_suffix(), 0, 80.0)
|
||||
{
|
||||
d.fields.insert("ppi".into(), json!(v));
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ fn body(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> bool {
|
||||
}
|
||||
}
|
||||
if preset.is_none()
|
||||
&& let Some(v) = widgets::plain_field(ui, "raster-ppi", ppi, " ppi", 0, 80.0)
|
||||
&& let Some(v) = widgets::plain_field(ui, "raster-ppi", ppi, &super::ppi_suffix(), 0, 80.0)
|
||||
{
|
||||
d.fields.insert("resolution".into(), json!(v));
|
||||
}
|
||||
@@ -124,7 +124,7 @@ pub(super) fn effect_fields(app: &VectorcraftApp, ui: &mut egui::Ui, d: &mut Dia
|
||||
}
|
||||
if !document
|
||||
&& preset.is_none()
|
||||
&& let Some(v) = widgets::plain_field(ui, "rasterize-ppi", ppi, " ppi", 0, 80.0)
|
||||
&& let Some(v) = widgets::plain_field(ui, "rasterize-ppi", ppi, &super::ppi_suffix(), 0, 80.0)
|
||||
{
|
||||
d.fields.insert("resolution".into(), json!(v));
|
||||
}
|
||||
|
||||
@@ -561,9 +561,10 @@ fn image_rows(ui: &mut egui::Ui, d: &mut Dialog, key: &str, title: &str) {
|
||||
ui.horizontal(|ui| {
|
||||
pick::<Downsample>(ui, d, &format!("{base}.downsample"), 150.0, |_| true);
|
||||
ui.label(tl!("to"));
|
||||
number(ui, d, &format!("{base}.ppi"), " ppi", on);
|
||||
let ppi = super::ppi_suffix();
|
||||
number(ui, d, &format!("{base}.ppi"), &ppi, on);
|
||||
ui.label(tl!("above"));
|
||||
number(ui, d, &format!("{base}.abovePpi"), " ppi", on);
|
||||
number(ui, d, &format!("{base}.abovePpi"), &ppi, on);
|
||||
});
|
||||
ui.horizontal(|ui| {
|
||||
ui.label(tl!("Compression:"));
|
||||
|
||||
@@ -3601,3 +3601,94 @@ fn color_halftones_max_radius_stays_in_pixels_whatever_the_units() {
|
||||
assert!(shows, "{typed} shows as {shown}, not in centimetres");
|
||||
}
|
||||
}
|
||||
|
||||
/// The resolution fields of Flatten Transparency, Document Raster Effects Settings, Effect ›
|
||||
/// Rasterize, the PNG options and the PDF save dialog's Compression section show their unit in the interface
|
||||
/// language (French "ppp"), as the `{n} ppi` row writes it.
|
||||
#[test]
|
||||
fn resolution_fields_show_the_ppi_unit_in_the_interface_language() {
|
||||
fn texts(s: &egui::Shape, out: &mut Vec<String>) {
|
||||
match s {
|
||||
egui::Shape::Text(t) => out.push(t.galley.text().to_string()),
|
||||
egui::Shape::Vec(v) => v.iter().for_each(|s| texts(s, out)),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
fn shown(app: &mut VectorcraftApp) -> Vec<String> {
|
||||
let ctx = egui::Context::default();
|
||||
theme::install_fonts(&ctx);
|
||||
theme::apply(&ctx, Default::default());
|
||||
let raw =
|
||||
|| egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1400.0, 1000.0))), ..Default::default() };
|
||||
// A window is measured in its first frame and drawn from the second.
|
||||
ctx.run_ui(raw(), |ui| show(app, ui.ctx())).textures_delta.clear();
|
||||
let mut out = ctx.run_ui(raw(), |ui| show(app, ui.ctx()));
|
||||
out.textures_delta.clear();
|
||||
let mut all = vec![];
|
||||
out.shapes.iter().for_each(|s| texts(&s.shape, &mut all));
|
||||
all
|
||||
}
|
||||
type Open = fn(&mut VectorcraftApp);
|
||||
let dialogs: [(&str, Open, &str); 5] = [
|
||||
(
|
||||
"Flatten Transparency",
|
||||
|app| {
|
||||
app.run("shape.rectangle", json!({"x": 10, "y": 10, "width": 50, "height": 50})).unwrap();
|
||||
app.run("transparency.set", json!({"opacity": 50})).unwrap();
|
||||
app.run("ui.flattenTransparencyDialog", json!({})).unwrap();
|
||||
},
|
||||
"ppp",
|
||||
),
|
||||
(
|
||||
"Document Raster Effects Settings",
|
||||
|app| {
|
||||
crate::menus::invoke(app, "document.rasterEffectsSettings", json!({}));
|
||||
app.ui.dialog.as_mut().unwrap().fields.insert("resolution".into(), json!(200));
|
||||
},
|
||||
"200 ppp",
|
||||
),
|
||||
(
|
||||
"Rasterize",
|
||||
|app| {
|
||||
app.run("shape.rectangle", json!({"x": 10, "y": 10, "width": 50, "height": 50})).unwrap();
|
||||
crate::menus::invoke(app, "effect.dialog", json!({"effect": "rasterize"}));
|
||||
let d = app.ui.dialog.as_mut().unwrap();
|
||||
d.fields.insert("useDocumentResolution".into(), json!(false));
|
||||
d.fields.insert("resolution".into(), json!(200));
|
||||
},
|
||||
"200 ppp",
|
||||
),
|
||||
(
|
||||
"PNG Options",
|
||||
|app| {
|
||||
png_options::open(app, vectorcraft_engine::cmd::fileio::format("png").unwrap(), json!({"format": "png", "path": "/out/a.png"}));
|
||||
let d = app.ui.dialog.as_mut().unwrap();
|
||||
d.fields.insert("ppi".into(), json!(200));
|
||||
d.fields.insert("__otherPpi".into(), json!(true));
|
||||
},
|
||||
"200 ppp",
|
||||
),
|
||||
(
|
||||
"Save PDF",
|
||||
|app| {
|
||||
app.run("ui.savePdfDialog", json!({"path": "/tmp/ppi.pdf"})).unwrap();
|
||||
app.ui.dialog.as_mut().unwrap().fields.insert("__section".into(), json!("Compression"));
|
||||
},
|
||||
" ppp",
|
||||
),
|
||||
];
|
||||
let mut bad = vec![];
|
||||
for (name, open, want) in dialogs {
|
||||
let mut app = app();
|
||||
open(&mut app);
|
||||
crate::i18n::set_current(crate::i18n::Lang::from_code("fr").unwrap());
|
||||
let texts = shown(&mut app);
|
||||
crate::i18n::set_current(crate::i18n::Lang::EN);
|
||||
let translated = texts.iter().any(|t| t.trim() == want.trim() || (want.starts_with(' ') && t.ends_with(want)));
|
||||
let english = texts.iter().any(|t| t.trim() == "ppi" || t.ends_with(" ppi"));
|
||||
if !translated || english {
|
||||
bad.push(format!("{name}: {texts:?}"));
|
||||
}
|
||||
}
|
||||
assert!(bad.is_empty(), "{}", bad.join("\n"));
|
||||
}
|
||||
|
||||
@@ -421,7 +421,8 @@ fn line(i: Result<Value, String>, units: vectorcraft_doc::Unit) -> String {
|
||||
match (i["pixelWidth"].as_u64(), i["pixelHeight"].as_u64()) {
|
||||
(Some(w), Some(h)) => {
|
||||
let ppi = i["ppi"][0].as_f64().unwrap_or(72.0);
|
||||
format!("{w} × {h} px, {} ppi, {} ({size})", fmt_ppi(ppi), i["colorMode"].as_str().unwrap_or("RGB"))
|
||||
let ppi = crate::i18n::fill_once(tl!("{n} ppi"), &[("n", &fmt_ppi(ppi))]);
|
||||
format!("{w} × {h} px, {ppi}, {} ({size})", i["colorMode"].as_str().unwrap_or("RGB"))
|
||||
}
|
||||
_ => format!("{} ({size})", i["format"].as_str().unwrap_or_default().to_uppercase()),
|
||||
}
|
||||
@@ -892,3 +893,20 @@ pub(crate) fn link_buttons(app: &mut VectorcraftApp, ui: &mut egui::Ui, id: u64,
|
||||
crate::panels::links::unembed(app, id, name);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// The Place dialog's line about an image writes its resolution with the `{n} ppi` row.
|
||||
#[test]
|
||||
fn the_file_line_writes_the_ppi_unit_in_the_interface_language() {
|
||||
let info = json!({ "pixelWidth": 300, "pixelHeight": 150, "ppi": [300.0, 300.0], "colorMode": "RGB", "width": 72.0, "height": 36.0 });
|
||||
let en = line(Ok(info.clone()), vectorcraft_doc::Unit::Points);
|
||||
crate::i18n::set_current(crate::i18n::Lang::from_code("fr").unwrap());
|
||||
let fr = line(Ok(info), vectorcraft_doc::Unit::Points);
|
||||
crate::i18n::set_current(crate::i18n::Lang::EN);
|
||||
assert!(en.starts_with("300 × 150 px, 300 ppi, RGB ("), "{en}");
|
||||
assert!(fr.starts_with("300 × 150 px, 300 ppp, RGB ("), "{fr}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -649,6 +649,15 @@ mod field_number_tests {
|
||||
assert_eq!(field_number("50% / 2", "%", false), Some(25.0));
|
||||
}
|
||||
|
||||
/// A resolution field reads the unit it shows: "200 ppp" in a French interface.
|
||||
#[test]
|
||||
fn a_resolution_field_reads_its_translated_unit() {
|
||||
crate::i18n::set_current(crate::i18n::Lang::from_code("fr").unwrap());
|
||||
let read = [field_number("200 ppp", &crate::dialogs::ppi_suffix(), false), field_number("200", &crate::dialogs::ppi_suffix(), false)];
|
||||
crate::i18n::set_current(crate::i18n::Lang::EN);
|
||||
assert_eq!(read, [Some(200.0), Some(200.0)]);
|
||||
}
|
||||
|
||||
/// A thousands comma is refused inside Kerning's pair of parentheses and before `%` or the
|
||||
/// suffix, as it is in plain text, rather than read as a decimal comma (`(1,000)` as 1).
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user