Font picker: one sample per row, in the sample text

Every row's sample shows the picker's sample text: the host's preview text, else the
sample word. The Arabic second sample and its script check are gone. A face that
lacks the text's characters keeps the existing fallback: its name, else its first
glyphs.

A sample, in a row, the banner or the panel, is laid out and drawn only in its own
face once it is ready. Until then (and for good when the face is unavailable or the
family has none, SampleFace::Missing) it keeps its band, LINE_EM of its size high,
and draws nothing, instead of laying the text out in the interface face, which may
lack its characters.

The Arabic story becomes font_picker_arabic_sample_text: an Arabic preview text over
a catalog with Noto Sans Arabic.
This commit is contained in:
aiongg
2026-11-22 16:08:54 +00:00
parent e8254eb4b4
commit a47b795b1f
12 changed files with 119 additions and 128 deletions
@@ -286,11 +286,11 @@ impl FamilyCard {
ends
}
/// The face the sample is drawn in: the card's own, or the interface face when there is none.
/// The face the sample is drawn in: the card's own, or none.
fn face(&self) -> SampleFace<'_> {
match &self.data.face {
Some((owner, face, catalog)) => SampleFace::Face { catalog, owner: *owner, face },
None => SampleFace::Interface,
None => SampleFace::Missing,
}
}
+1 -1
View File
@@ -497,7 +497,7 @@ impl Sheet {
};
SampleFace::Face { catalog, owner: *owner, face }
}
None => SampleFace::Interface,
None => SampleFace::Missing,
};
StyleRow::new(line, &self.title, &self.sample, points, face, words)
});
@@ -62,7 +62,7 @@ fn list(ui: &mut Ui, count: usize) -> StoryOutput {
let line = &lines[*key];
let face = match &line.face {
Some((owner, face)) => SampleFace::Face { catalog: &catalog, owner: *owner, face },
None => SampleFace::Interface,
None => SampleFace::Missing,
};
StyleRow::new(line, "Hack", PANGRAM, POINTS, face, &words)
})
@@ -88,5 +88,5 @@ fn style_list_many(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
fn style_row_installing(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
let line = StyleLine { label: "Bold".to_owned(), face: None, action: Action::Running(Some(0.4)) };
let words = Env::of(ui).words::<DetailsWords>();
show_as_only_item(ui, StyleRow::new(&line, "Hack", PANGRAM, POINTS, SampleFace::Interface, &words)).into()
show_as_only_item(ui, StyleRow::new(&line, "Hack", PANGRAM, POINTS, SampleFace::Missing, &words)).into()
}
+28 -27
View File
@@ -17,7 +17,12 @@ use crate::panel::faces::{self, Owner, Shown};
#[cfg(feature = "suite")]
pub(crate) mod stories;
/// The ink of a sample: the text colour for a face that drew, the muted one for the interface face standing in.
/// The height of a sample whose face is not ready, in ems of its size: a usual line height (Inter's line box is 1.21
/// em), so a row keeps about the height it has once the face draws, without laying the text out in a face that may
/// lack its characters.
const LINE_EM: f32 = 1.2;
/// The ink of a sample.
#[derive(Clone, Debug, PartialEq, ComponentStyle)]
#[non_exhaustive]
pub(crate) struct FontSampleStyle {
@@ -25,8 +30,6 @@ pub(crate) struct FontSampleStyle {
metrics: Metrics,
/// The sample in its own face (`text.normal`).
ink: StyleColor,
/// The sample in the interface face, because its own is not available (`text.muted`).
muted: StyleColor,
}
/// The face a sample is drawn in.
@@ -34,26 +37,32 @@ pub(crate) struct FontSampleStyle {
pub(crate) enum SampleFace<'a> {
/// A face of `catalog`, which the document-face registry loads when the sample is measured or painted.
Face { catalog: &'a Catalog, owner: Owner, face: &'a FaceInfo },
/// No face is here to draw with: the sample shows in the interface face.
Interface,
/// No face is here to draw with: the sample keeps its band and shows nothing.
Missing,
}
impl SampleFace<'_> {
pub(crate) fn shown(&self, at: &impl craft_ui::primitives::FacesCx) -> Shown {
/// The font that draws the sample once its face is ready; none while it loads, when it is not available, or for
/// [`SampleFace::Missing`]. A sample is laid out and drawn only in this font, never in the interface face, which
/// may lack the sample's characters.
pub(crate) fn ready(&self, at: &impl craft_ui::primitives::FacesCx) -> Option<FontRef> {
match self {
SampleFace::Face { catalog, owner, face } => faces::shown(at, catalog, *owner, face),
SampleFace::Interface => Shown::Unavailable,
SampleFace::Face { catalog, owner, face } => match faces::shown(at, catalog, *owner, face) {
Shown::Ready(font) => Some(font),
_ => None,
},
SampleFace::Missing => None,
}
}
}
/// A sample of `text` in `face`, `points` high. While the face loads the sample shows nothing and keeps its band;
/// a face that is not available shows the sample in the interface face, muted. `points` is the user's sample size
/// (data, as `DocRun::size`), not a UI size. The label names the sample for a screen reader (the family and the
/// sample text).
/// A sample of `text` in `face`, `points` high. Until the face is ready (while it loads, and for good when it is not
/// available) the sample shows nothing and keeps its band. `points` is the user's sample size (data, as
/// `DocRun::size`), not a UI size. The label names the sample for a screen reader (the family and the sample text).
///
/// Its measure is the sample laid out in the face at `points` on one row, cut with an ellipsis where the room does
/// not fit it (`faces::sample_text`, the layout `paint` draws).
/// not fit it (`faces::sample_text`, the layout `paint` draws); without a ready face, no width and [`LINE_EM`] of
/// `points` high.
#[derive(craft_ui::contract::Component)]
#[component(content_painter)]
pub(crate) struct FontSample<'a> {
@@ -81,8 +90,7 @@ impl Component for FontSample<'_> {
type Output = egui::Response;
fn style(cx: &StyleCx<'_>, _: FontSampleStyleKey) -> FontSampleStyle {
let text = &cx.tokens.color.text;
FontSampleStyle { metrics: Metrics::new(), ink: cx.color(text.normal), muted: cx.color(text.muted) }
FontSampleStyle { metrics: Metrics::new(), ink: cx.color(cx.tokens.color.text.normal) }
}
fn sense(&self) -> Sense {
@@ -90,24 +98,17 @@ impl Component for FontSample<'_> {
}
fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 {
// A face that is loading keeps the band the interface face would take.
let font = match self.face.shown(cx) {
Shown::Ready(font) => font,
_ => FontRef::proportional(),
let Some(font) = self.face.ready(cx) else {
return Vec2::new(0.0, self.points * LINE_EM);
};
let runs = [DocRun::new(self.text.as_ref(), font, self.points, DataColor::new(Color32::PLACEHOLDER))];
cx.layout_doc(&faces::sample_text(&runs, cx.room)).size()
}
fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) {
let painter = ui.content_painter(cx);
let ink = |color: StyleColor| DataColor::new(color.egui());
match self.face.shown(cx) {
Shown::Ready(font) => faces::paint_sample(&painter, cx.content_rect, &font, &self.text, self.points, ink(cx.style.ink)),
Shown::Unavailable => {
faces::paint_sample(&painter, cx.content_rect, &FontRef::proportional(), &self.text, self.points, ink(cx.style.muted));
}
_ => {}
if let Some(font) = self.face.ready(cx) {
let ink = DataColor::new(cx.style.ink.egui());
faces::paint_sample(&ui.content_painter(cx), cx.content_rect, &font, &self.text, self.points, ink);
}
}
@@ -18,12 +18,12 @@ fn ready(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
let faces = catalog.styles("Hack");
let face = match faces.first() {
Some(face) => SampleFace::Face { catalog: &catalog, owner: Owner::Installed, face },
None => SampleFace::Interface,
None => SampleFace::Missing,
};
FontSample::new("Hack: sample", PANGRAM, face, POINTS).show(ui).into()
}
#[story(FontSample<'static>, covers = [], drive = Drive::Never)]
fn unavailable(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
FontSample::new("Hack: sample", PANGRAM, SampleFace::Interface, POINTS).show(ui).into()
FontSample::new("Hack: sample", PANGRAM, SampleFace::Missing, POINTS).show(ui).into()
}
+3 -3
View File
@@ -74,7 +74,6 @@ craft_ui::words! {
decorative: "fontPicker.classDecorative" = "Decorative";
show_styles: "fontPicker.showStyles" = "Show styles";
hide_styles: "fontPicker.hideStyles" = "Hide styles";
sample_arabic: "fontPicker.sampleArabic" = "أبجد";
format_open_type: "fontPicker.formatOpenType" = "OpenType";
format_true_type: "fontPicker.formatTrueType" = "TrueType";
format_variable: "fontPicker.formatVariable" = "Variable";
@@ -493,7 +492,8 @@ fn read<T: Value>(bind: &mut Option<Bind<'_, T>>, cx: &mut impl StageAccess) ->
/// The font family field. Typing filters the list to the families whose name (in any language) matches, the best
/// match first, and opens it; a click on the name while the list is closed, the chevron and Alt+Down open it on every family (the click selects the name), Alt+Up closes it. Rows: a star, the
/// family name in the interface face, a sample in the family's own face at the preview size and the class mark.
/// family name in the interface face, the sample text (the preview text, else "Sample") in the family's own face at the
/// preview size and the class mark.
/// Recent families form a group at the top. Up, Down and Page keys move the keyboard row, which previews, Enter or a
/// click on a row picks it, Enter on a typed name that is a family picks that family, Escape closes the list and
/// restores the field, and Tab picks the keyboard row and moves on. While an input method composes, Enter, Escape
@@ -517,7 +517,7 @@ pub struct FontPicker<'a> {
picked_style: Option<String>,
rested: Option<String>,
preview: Preview<String>,
/// The preview banner's text.
/// The preview banner's text and every row's sample (the sample word when not given).
#[prop]
preview_text: Option<&'a str>,
/// Families the host can fetch but that are not installed, listed with the served mark.
+8 -14
View File
@@ -4,14 +4,13 @@
use craft_behavior::Sense;
use craft_theme::TypeRole;
use craft_ui::contract::{Common, Component, Cx, MeasureCx, PaintUi, Props as _, Role};
use craft_ui::fonts::FontRef;
use craft_ui::primitives::DocRun;
use craft_ui::primitives::TextRun;
use craft_ui::runtime::env::Label;
use craft_ui::style::{ComponentStyle, Corners, DataColor, Metrics, StyleColor, StyleCx, SurfaceKind};
use egui::{Color32, Pos2, Rect, Vec2};
use crate::panel::faces::{self, Shown};
use crate::panel::faces;
use crate::panel::specimen::SampleFace;
use super::SAMPLE_EM;
@@ -40,7 +39,7 @@ pub(crate) struct FontPreviewBannerStyle {
}
/// The preview at the top of the font list: a well (`SurfaceKind::Sunken`) as wide as the list, the family's name in
/// the caption role and under it `text` in the family's face, one line cut at the end. While the face loads the band
/// the caption role and under it `text` in the family's face, one line cut at the end. Until the face is ready the band
/// stays empty. The label names it for a screen reader.
#[derive(craft_ui::contract::Component)]
#[component(content_painter)]
@@ -89,12 +88,10 @@ impl Component for FontPreviewBanner<'_> {
fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 {
let style = cx.style;
let inner = cx.room.map(|room| (room - 2.0 * style.pad).max(0.0));
let font = match self.face.shown(cx) {
Shown::Ready(font) => font,
_ => FontRef::proportional(),
};
let runs = [DocRun::new(self.text, font, style.band * SAMPLE_EM, DataColor::new(Color32::PLACEHOLDER))];
let sample = cx.layout_doc(&faces::sample_text(&runs, inner)).size().x;
let sample = self.face.ready(cx).map_or(0.0, |font| {
let runs = [DocRun::new(self.text, font, style.band * SAMPLE_EM, DataColor::new(Color32::PLACEHOLDER))];
cx.layout_doc(&faces::sample_text(&runs, inner)).size().x
});
let name = TextRun::new(self.name).role(TypeRole::Caption);
let name = inner.map_or_else(|| cx.layout(&name), |width| cx.layout_at(&name, width)).size().x;
Vec2::new(2.0 * style.pad + sample.max(name), 2.0 * style.pad + style.name_line + style.gap + style.band)
@@ -113,11 +110,8 @@ impl Component for FontPreviewBanner<'_> {
let band = Rect::from_min_max(Pos2::new(inner.left(), name.bottom() + style.gap), inner.max);
let points = style.band * SAMPLE_EM;
let ink = DataColor::new(style.ink.egui());
let painter = ui.content_painter(cx);
match self.face.shown(cx) {
Shown::Ready(font) => faces::paint_sample(&painter, band, &font, self.text, points, ink),
Shown::Unavailable => faces::paint_sample(&painter, band, &FontRef::proportional(), self.text, points, ink),
_ => {}
if let Some(font) = self.face.ready(cx) {
faces::paint_sample(&ui.content_painter(cx), band, &font, self.text, points, ink);
}
}
@@ -16,13 +16,13 @@ fn host_text(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
let faces = catalog.styles("Hack");
let face = match faces.first() {
Some(face) => SampleFace::Face { catalog: &catalog, owner: Owner::Installed, face },
None => SampleFace::Interface,
None => SampleFace::Missing,
};
FontPreviewBanner::new("Font preview: Hack", "Hack", "The quick brown fox", face).show(ui).into()
}
/// A family the catalog lacks: the sample word in the interface face.
/// A family the catalog lacks: the name over an empty sample band.
#[story(FontPreviewBanner<'static>, covers = [], drive = Drive::Never)]
fn unavailable(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
FontPreviewBanner::new("Font preview: Gotham", "Gotham", "Sample", SampleFace::Interface).show(ui).into()
FontPreviewBanner::new("Font preview: Gotham", "Gotham", "Sample", SampleFace::Missing).show(ui).into()
}
+10 -56
View File
@@ -60,25 +60,7 @@ impl Traits {
pub struct FamilyInfos {
pub(super) families: HashMap<String, Info>,
traits: Mutex<HashMap<String, Option<Traits>>>,
samples: Mutex<HashMap<(String, String, String), Samples>>,
}
/// The texts a row's thumbnails paint.
#[derive(Clone, Default)]
pub(super) struct Samples {
/// The Latin sample, the name or the face's first glyphs.
pub(super) latin: String,
/// The Arabic second sample, when the face draws it and the Latin sample.
pub(super) arabic: Option<String>,
}
/// The Arabic letters a face must map besides the Arabic sample: alef, lam, meem and ain.
const ARABIC_CORE: &str = "\u{627}\u{644}\u{645}\u{639}";
/// Whether a face gets the Arabic second sample: `drawn` (the face's character map) draws the Latin sample, the core
/// Arabic letters and the Arabic sample.
fn has_arabic_sample(drawn: &dyn Fn(&str) -> bool, latin: &str, arabic: &str) -> bool {
drawn(latin) && drawn(ARABIC_CORE) && drawn(arabic)
samples: Mutex<HashMap<(String, String), String>>,
}
impl FamilyInfos {
@@ -121,32 +103,30 @@ impl FamilyInfos {
lock(&self.traits).get(family).and_then(|t| t.as_ref()).and_then(|t| t.class)
}
/// What the row's samples say: `sample` when the family's face draws it, else its name, else the
/// first glyphs the face has (a symbol or non-Latin face); and `arabic` as a second sample when the face
/// draws that too. Both come from one read of the face's character map.
pub(super) fn sample_text(&self, catalog: &Catalog, family: &str, sample: &str, arabic: &str, shown: &str) -> Samples {
let key = (family.to_owned(), sample.to_owned(), arabic.to_owned());
/// What the row's sample says: `sample` when the family's face draws it, else its name, else the first glyphs the
/// face has (a symbol face, or a face without the sample's script).
pub(super) fn sample_text(&self, catalog: &Catalog, family: &str, sample: &str, shown: &str) -> String {
let key = (family.to_owned(), sample.to_owned());
if let Some(text) = lock(&self.samples).get(&key) {
return text.clone();
}
let text = self.families.get(family).and_then(|info| draws(catalog, &info.regular, sample, arabic, shown)).unwrap_or_default();
let text = self.families.get(family).and_then(|info| draws(catalog, &info.regular, sample, shown)).unwrap_or_default();
lock(&self.samples).insert(key, text.clone());
text
}
}
fn draws(catalog: &Catalog, face: &FaceInfo, sample: &str, arabic: &str, shown: &str) -> Option<Samples> {
fn draws(catalog: &Catalog, face: &FaceInfo, sample: &str, shown: &str) -> Option<String> {
let data = catalog.load(face).ok()?;
let font = FontRef::from_index(&data.bytes, face.index).ok()?;
let map = font.charmap();
let inked = inked(&font);
let drawn = |t: &str| t.chars().filter(|c| !c.is_whitespace()).all(|c| map.map(c).is_some_and(&inked));
if drawn(sample) {
let arabic = has_arabic_sample(&drawn, sample, arabic).then(|| arabic.to_owned());
return Some(Samples { latin: sample.to_owned(), arabic });
return Some(sample.to_owned());
}
if drawn(shown) {
return Some(Samples { latin: shown.to_owned(), arabic: None });
return Some(shown.to_owned());
}
let first = map
.mappings()
@@ -155,7 +135,7 @@ fn draws(catalog: &Catalog, face: &FaceInfo, sample: &str, arabic: &str, shown:
.filter(|c| !c.is_whitespace() && !c.is_control())
.take(6)
.collect();
Some(Samples { latin: first, arabic: None })
Some(first)
}
/// Whether a glyph of `font` has an outline, which is all the sample painter draws: a TrueType glyph with contours, or
@@ -386,29 +366,3 @@ pub(super) fn list(
}
(rows, comparable)
}
#[cfg(test)]
pub(crate) mod tests {
use craft_ui::testing::trial;
use super::{ARABIC_CORE, has_arabic_sample};
const LATIN: &str = "AaBbCc";
/// "أبجد هوز".
const ARABIC: &str = "\u{623}\u{628}\u{62C}\u{62F} \u{647}\u{648}\u{632}";
fn maps(chars: &str) -> impl Fn(&str) -> bool + '_ {
move |text| text.chars().filter(|c| !c.is_whitespace()).all(|c| chars.contains(c))
}
#[trial]
fn an_arabic_sample_needs_the_latin_sample_and_every_arabic_test_letter() {
let all = format!("{LATIN}{ARABIC_CORE}{ARABIC}");
assert!(has_arabic_sample(&maps(&all), LATIN, ARABIC));
assert!(!has_arabic_sample(&maps(LATIN), LATIN, ARABIC), "a Latin-only face");
for missing in LATIN.chars().chain(ARABIC_CORE.chars()).chain(ARABIC.chars().filter(|c| !c.is_whitespace())) {
let partial: String = all.chars().filter(|c| *c != missing).collect();
assert!(!has_arabic_sample(&maps(&partial), LATIN, ARABIC), "without U+{:04X}", missing as u32);
}
}
}
+8 -13
View File
@@ -280,11 +280,11 @@ fn class_column(ui: &mut egui::Ui, filters: &mut FontFilters, words: &FontPicker
}
/// The sample of a family row: `text` in the regular face of `family` at `points`, named "{shown}: {text}" as the font
/// manager names its samples, so the row keeps the family's name to itself; the interface face for a family the
/// catalog lacks.
/// manager names its samples, so the row keeps the family's name to itself; no face for a family the catalog
/// lacks.
fn sample<'a>(catalog: &'a Catalog, infos: &'a FamilyInfos, family: &str, shown: &str, text: String, points: f32) -> FontSample<'a> {
let face =
infos.families.get(family).map_or(SampleFace::Interface, |info| SampleFace::Face { catalog, owner: Owner::Installed, face: &info.regular });
infos.families.get(family).map_or(SampleFace::Missing, |info| SampleFace::Face { catalog, owner: Owner::Installed, face: &info.regular });
FontSample::new(format!("{shown}: {text}"), text, face, points)
}
@@ -383,6 +383,8 @@ pub(super) fn popup<'a>(picker: &mut FontPicker<'a>, ui: &mut OverlayUi<'_>, cx:
let banner_family = lead.unwrap_or_else(|| family.clone());
// The library picker always shows the banner, with the sample word when the host gives no text.
let banner_text = picker.preview_text.or((!plain).then_some(words.sample.as_str()));
// Every row's sample is the banner's text: the host's text, else the sample word.
let sample_text = picker.preview_text.unwrap_or(words.sample.as_str());
let name = words.list.clone();
let mut stars: Vec<(String, bool)> = Vec::new();
let mut over_rows = false;
@@ -392,7 +394,7 @@ pub(super) fn popup<'a>(picker: &mut FontPicker<'a>, ui: &mut OverlayUi<'_>, cx:
Stack::new().show(ui, |ui, _| {
if let Some(text) = banner_text {
let shown_name = banner_shown_name(&slots, &infos, &banner_family, &language_code, settings.as_ref());
let face = infos.families.get(&banner_family).map_or(SampleFace::Interface, |info| SampleFace::Face {
let face = infos.families.get(&banner_family).map_or(SampleFace::Missing, |info| SampleFace::Face {
catalog,
owner: Owner::Installed,
face: &info.regular,
@@ -464,15 +466,8 @@ pub(super) fn popup<'a>(picker: &mut FontPicker<'a>, ui: &mut OverlayUi<'_>, cx:
if size == ThumbSize::None {
return line;
}
let samples =
infos.sample_text(catalog, &row.family, words.sample.as_str(), words.sample_arabic.as_str(), &row.shown);
line = line.cell_field(SAMPLE, sample(catalog, &infos, &row.family, &row.shown, samples.latin, points), |_| {});
// The Arabic sample ends the name's band, so the sample column keeps one width; it
// drops out before it would narrow the name.
match samples.arabic {
Some(arabic) => line.optional_field(sample(catalog, &infos, &row.family, &row.shown, arabic, points), |_| {}),
None => line,
}
let text = infos.sample_text(catalog, &row.family, sample_text, &row.shown);
line.cell_field(SAMPLE, sample(catalog, &infos, &row.family, &row.shown, text, points), |_| {})
}
});
over_rows = rows.response.contains_pointer();
+9 -5
View File
@@ -60,7 +60,7 @@ fn styled_catalog() -> Arc<Catalog> {
Arc::clone(CATALOG.get_or_init(|| with_inter(&[])))
}
/// The styled catalog with Noto Sans Arabic, which draws both samples, when the build has the craft fonts set.
/// The styled catalog with Noto Sans Arabic, when the build has the craft fonts set.
fn arabic_catalog() -> Arc<Catalog> {
static CATALOG: OnceLock<Arc<Catalog>> = OnceLock::new();
Arc::clone(CATALOG.get_or_init(|| with_inter(&STORY_FONTS.iter().map(|face| face.bytes).collect::<Vec<_>>())))
@@ -182,14 +182,18 @@ fn font_picker_banner_text(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
show_with(ui, data, demo("Ubuntu", FontPickerSettings::default(), FontFilters::default()), &catalog(), &extras)
}
/// Large samples: a face with Arabic letters (Noto Sans Arabic, when the build has the craft fonts set) gets the second
/// sample after the Latin one.
/// An Arabic sample text, at Large: every row's sample shows it in the family's face, Noto Sans Arabic (when the build
/// has the craft fonts set) draws it, and a face without Arabic letters shows its name.
#[story(FontPicker<'static>, covers = [Open], drive = Drive::Custom(open_list))]
fn font_picker_arabic_sample(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
fn font_picker_arabic_sample_text(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
let settings = FontPickerSettings { preview: crate::picker::FontPreviewSize::Large, ..FontPickerSettings::default() };
show_with(ui, data, demo("Inter", settings, FontFilters::default()), &arabic_catalog(), &Extras::default())
let extras = Extras { banner: Some(ARABIC_SAMPLE_TEXT), ..Extras::default() };
show_with(ui, data, demo("Inter", settings, FontFilters::default()), &arabic_catalog(), &extras)
}
/// "أبجد هوز", the first words of the abjad order.
const ARABIC_SAMPLE_TEXT: &str = "\u{623}\u{628}\u{62C}\u{62F} \u{647}\u{648}\u{632}";
/// Settings whose recents are the families `recent`.
fn recents(recent: &[&str]) -> FontPickerSettings {
FontPickerSettings { recent: recent.iter().map(|family| (*family).to_owned()).collect(), ..FontPickerSettings::default() }
+43
View File
@@ -501,6 +501,49 @@ fn library_rig() -> (Fixture, Rc<RefCell<Library>>) {
(fx, state)
}
/// "أبجد هوز", which neither Inter nor Hack draws.
const ARABIC_TEXT: &str = "\u{623}\u{628}\u{62C}\u{62F} \u{647}\u{648}\u{632}";
/// An open library list over Inter and Hack, with Gotham served, `family` current and `text` as the preview text.
fn sample_rig(family: &'static str, text: &'static str) -> Fixture {
let catalog = catalog_of(&[INTER_REGULAR, epaint_default_fonts::HACK_REGULAR]);
let served = vec!["Gotham".to_owned()];
let mut family = family.to_owned();
let mut settings = FontPickerSettings::default();
let mut fx = Fixture::new(move |ui| {
FontPicker::new("Font family", Bind::bound(&mut family), &catalog)
.settings(Bind::bound(&mut settings))
.served(&served)
.preview_text(text)
.show(ui);
});
fx.in_room(Room::Window);
fx.settle();
open(&mut fx);
fx
}
/// Each row has one sample, the picker's sample text, in its own face; a face that lacks the text's letters shows its
/// name instead; and a family without a face shows no sample at all, not the text in the interface face.
#[trial]
fn each_row_samples_the_preview_text_and_a_face_without_it_shows_its_name() {
let fx = sample_rig("Inter", "Hello");
for family in ["Inter", "Hack"] {
assert!(fx.by_label(&format!("{family}: Hello")).is_some(), "{family}'s sample is the preview text");
assert!(fx.by_label(&format!("{family}: Sample")).is_none(), "{family} has no sample word");
}
let fx = sample_rig("Gotham", ARABIC_TEXT);
assert!(fx.by_label("Font preview: Gotham").is_some(), "the banner shows the served family");
for family in ["Inter", "Hack"] {
assert!(fx.by_label(&format!("{family}: {family}")).is_some(), "{family} lacks Arabic: its sample is its name");
}
assert!(
fx.texts().iter().all(|painted| painted.text != ARABIC_TEXT),
"no face draws the text: neither the rows nor the banner of a family without a face paint it"
);
}
/// Production's and Photoshop's row: the star first, then the names in one column whether or not a family has styles
/// to open (a recent row included), the sample after the name; and a rule, not blank room, ends the recent group.
#[trial]