Files
vectorcraft/crates/text/src/tests/versions.rs
T
jmvergesandClaude Opus 5.5 bb50d438c8 .ai import: type from the text document takes the face of its exact PostScript name and recorded version (#6983)
Type read from a text document's font record was set in whichever installed
face first answered to the record's PostScript name (ignoring case and
punctuation), and its family was then swapped for any available family
spelled alike but for case and punctuation (`FontDb::find_family`): a record
naming `SampleInk`, the PostScript name of `Sample/Ink`, set its type in
`SAMPLE INK`. Copies of one family and style under other PostScript names or
versions were not told apart either, so the type could set at other widths
than the file's own page.

- `named_font` / `document_font` (crates/eps/src/import/text.rs) give the
  family and style of the installed face of exactly that PostScript name,
  else one that differs only in case or punctuation, and name its version
  when the family and style alone resolve to another installed copy.
- The font record's key 5, its version string (name ID 5), is read and picks
  that version among the faces of the name. Each record is resolved once.
- `FontDb::postscript_faces` lists every installed face a PostScript name
  answers to, loading their families; the import prefers among them one of
  exactly that name.
- Type a PostScript program sets (EPS import) and legacy .ai type take the
  face of their font's name the same way.
- The .ai save writes a static font's version (`/5`) when its PostScript name
  alone would give the reader another installed copy (another version, or the
  same name in another family), keeps the version in the style it compares,
  and checks that the type comes back in the same family, style and face,
  taking the record's version as the reader does (up to 256 bytes), so such
  type round-trips without a note.

Measured on 224 users' files (used locally, never committed): 16 files set
some type in another face; 12 render differently against their own page,
10 closer and 2 further by 0.03% of the page; envelopes of type that open
live go from 154 to 159, none fewer in any file; no file opens another way.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 00:33:13 +00:00

67 lines
3.3 KiB
Rust

//! Two installed versions of one family and style (a font library keeps an old version beside a
//! new one): both load, the family and style alone keep resolving to the first, and type that names
//! the other version is set in it.
use kurbo::Point;
use vectorcraft_doc::CharStyle;
use crate::test_fonts::{TWIN_FAMILY as FAMILY, TWIN_VERSIONS as VERSIONS, twin_font};
use crate::*;
/// A database of the bundled fonts, the newer version of the test family loaded first.
fn db() -> FontDb {
let db = FontDb::with_font_dirs(vec![]);
assert_eq!(db.add_font(twin_font(true).expect("the newer test font builds")), 1);
assert_eq!(db.add_font(twin_font(false).expect("the older test font builds")), 1, "another version is kept");
assert_eq!(db.add_font(twin_font(false).expect("the older test font builds")), 0, "the same version isn't added twice");
db
}
fn width(db: &FontDb, version: Option<&str>) -> f64 {
let st = CharStyle { font_family: FAMILY.into(), font_version: version.map(Into::into), size: 100.0, ..CharStyle::default() };
layout(db, &TextObject::point(Point::ZERO, "Hamburgefonstiv", st)).glyphs.iter().map(|g| g.advance).sum()
}
#[test]
fn a_version_is_found_by_its_version_string() {
let db = db();
let face = |v: Option<&str>| db.face_version(FAMILY, "Regular", v).expect("a face");
assert_eq!(db.face(FAMILY, "Regular").expect("a face").version, VERSIONS[1], "the first loaded stays the default");
assert_eq!(face(None).version, VERSIONS[1]);
assert_eq!(face(Some(VERSIONS[0])).version, VERSIONS[0]);
assert_eq!(face(Some("Version 9.000")).version, VERSIONS[1], "a version not installed: the default");
let all = db.versions(&face(None));
assert_eq!(all.iter().map(|f| f.version.as_str()).collect::<Vec<_>>(), [VERSIONS[1], VERSIONS[0]]);
}
#[test]
fn type_naming_a_version_is_set_in_it() {
let db = db();
let (newer, older) = (width(&db, None), width(&db, Some(VERSIONS[0])));
assert!((newer - older).abs() > 50.0, "the two versions set the line differently: {newer} vs {older}");
assert_eq!(width(&db, Some(VERSIONS[1])), newer);
}
/// Every installed face a PostScript name answers to, ignoring case and punctuation: the face
/// `find_postscript` gives first, then the copies in other families and the versions, in load
/// order; none for a name no face has.
#[test]
fn every_face_of_a_postscript_name_is_found() {
use crate::test_fonts::named_copy;
let db = FontDb::with_font_dirs(vec![]);
for (inter, names) in [
(true, ("Facetest Ink", "Bold", "Version 1.000", "FacetestInkBold")),
(false, ("Facetest-Ink", "Bold", "Version 1.000", "FacetestInk-Bold")),
(true, ("Facetest-Ink", "Bold", "Version 2.000", "FACETESTINK-BOLD")),
(false, ("Other Ink", "Bold", "Version 1.000", "OtherInk-Bold")),
] {
assert_eq!(db.add_font(named_copy(inter, names).expect("the test font builds")), 1, "{names:?}");
}
let names = |name: &str| db.postscript_faces(name).iter().map(|f| f.postscript_name()).collect::<Vec<_>>();
let first = db.find_postscript("FacetestInk-Bold").expect("a face").postscript_name();
assert_eq!(first, "FacetestInkBold");
assert_eq!(names("FacetestInk-Bold"), ["FacetestInkBold", "FacetestInk-Bold", "FACETESTINK-BOLD"]);
assert_eq!(names("OtherInk-Bold"), ["OtherInk-Bold"]);
assert!(names("NoSuchInk-Bold").is_empty());
}