Paste maps Docs' private font names first, against the shared vectors
The web's familyForStack (a8a9f3e) and DocsFonts.family(cssStack:) now
apply one rule, and both are tested against
fixtures/vectors/font-stacks.json. Its extra first check is here too: a
name is matched against Docs' private CSS name ("Docs Inter") before the
Word name, the family name and the fallback names, because a run copied
out of web Docs carries it.
The shared vectors passed on the old resolver: in both private-name cases
the next name happens to map to the same family. So a second test builds
every family's copied-out stack from fonts.json ("Docs <name>" then its
fallback stack). It failed for five families before the change: Source
Sans 3 became Carlito (Segoe UI), IBM Plex Sans became Inter (Helvetica
Neue), Source Serif 4 and Literata became Caladea (Georgia), and
JetBrains Mono became Liberation Mono (Consolas).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+38
−4
native/apple/Sources/DocsKit/DocsFonts.swift+9 −4native/apple/Tests/DocsKitTests/DocsFontPickerTests.swift+29 −0
CI passed — run details
diff --git a/native/apple/Sources/DocsKit/DocsFonts.swift b/native/apple/Sources/DocsKit/DocsFonts.swift| index 66fe0d4..39c9542 100644 |
| --- a/native/apple/Sources/DocsKit/DocsFonts.swift |
| +++ b/native/apple/Sources/DocsKit/DocsFonts.swift |
| @@ -209,19 +209,24 @@ public enum DocsFonts { |
| /// The family a pasted font stack stands for, deterministically, from the |
| /// shared list alone. The stack is read in order and the first name that |
| /// maps wins; a later name is only consulted when an earlier one maps to |
| - /// nothing. Each name is matched exactly first, against each family's |
| - /// Word name and then its own name (the rule internal/fonts' ByDocxName |
| + /// nothing. Each name is matched exactly first, against Docs' private CSS |
| + /// name ("Docs Inter", which text copied out of web Docs carries), then |
| + /// each family's Word name and then its own name (the rule internal/fonts' ByDocxName |
| /// applies to .docx: Calibri is Carlito, Cambria is Caladea), and then |
| /// against the names each family stands in for, its fallback stack, in |
| /// picker order: Georgia is Caladea, Helvetica is Liberation Sans, Menlo |
| /// is JetBrains Mono, generic serif is Caladea. Nil when no name maps, |
| - /// which is the default: an unknown face pastes as Carlito. |
| + /// which is the default: an unknown face pastes as Carlito. The web's |
| + /// familyForStack is the same rule; fixtures/vectors/font-stacks.json |
| + /// holds the cases both are tested against. |
| static func family(cssStack stack: String) -> DocsFontFamily? { |
| let names = stack.split(separator: ",").map { |
| $0.trimmingCharacters(in: .whitespacesAndNewlines).trimmingCharacters(in: CharacterSet(charactersIn: "\"'")).lowercased() |
| }.filter { !$0.isEmpty } |
| for name in names { |
| - if let family = catalog.families.first(where: { $0.docxName.lowercased() == name }) |
| + // A run copied out of web Docs names its private face first. |
| + if let family = catalog.families.first(where: { ("docs " + $0.name).lowercased() == name }) |
| + ?? catalog.families.first(where: { $0.docxName.lowercased() == name }) |
| ?? catalog.families.first(where: { $0.name.lowercased() == name }) |
| ?? catalog.families.first(where: { $0.fallback.contains { $0.lowercased() == name } }) { |
| return family |
diff --git a/native/apple/Tests/DocsKitTests/DocsFontPickerTests.swift b/native/apple/Tests/DocsKitTests/DocsFontPickerTests.swift| index 3d3260a..cff8c65 100644 |
| --- a/native/apple/Tests/DocsKitTests/DocsFontPickerTests.swift |
| +++ b/native/apple/Tests/DocsKitTests/DocsFontPickerTests.swift |
| @@ -99,6 +99,35 @@ final class DocsFontPickerTests: XCTestCase { |
| XCTAssertEqual(DocsFonts.family(cssStack: "Wingdings, Calibri, Arial")?.key, "carlito") |
| } |
| + /// fixtures/vectors/font-stacks.json is the one list of paste cases the |
| + /// web's familyForStack and this resolver are both held to. |
| + @MainActor func testSharedFontStackVectors() throws { |
| + let url = DocsFontsTests.repository.appendingPathComponent("fixtures/vectors/font-stacks.json") |
| + let json = try XCTUnwrap(try JSONSerialization.jsonObject(with: Data(contentsOf: url)) as? [String: Any]) |
| + let cases = try XCTUnwrap(json["cases"] as? [[Any]]) |
| + XCTAssertEqual(cases.count, 18) |
| + for pair in cases { |
| + let stack = try XCTUnwrap(pair.first as? String) |
| + let expected = pair.last as? String |
| + XCTAssertEqual(DocsFonts.family(cssStack: stack)?.key, expected, stack) |
| + } |
| + } |
| + |
| + /// A run copied out of web Docs names its private face first, then that |
| + /// family's fallback stack (internal/fonts' generated stylesheet). The |
| + /// private name decides: Source Sans 3's fallbacks start with Segoe UI, |
| + /// which is Carlito's, and IBM Plex Sans's with Helvetica Neue, Inter's. |
| + @MainActor func testDocsPrivateFontNamesMapToTheirFamily() throws { |
| + let data = try Data(contentsOf: DocsFontsTests.repository.appendingPathComponent("internal/fonts/fonts.json")) |
| + let families = try XCTUnwrap((try JSONSerialization.jsonObject(with: data) as? [String: Any])?["families"] as? [[String: Any]]) |
| + for family in families { |
| + let name = try XCTUnwrap(family["name"] as? String), key = try XCTUnwrap(family["key"] as? String) |
| + let fallback = try XCTUnwrap(family["fallback"] as? [String]) |
| + let stack = (["\"Docs \(name)\""] + fallback.map { $0.contains(" ") ? "\"\($0)\"" : $0 }).joined(separator: ", ") |
| + XCTAssertEqual(DocsFonts.family(cssStack: stack)?.key, key, stack) |
| + } |
| + } |
| + |
| /// The same order through an HTML paste, which is where it writes a mark. |
| @MainActor func testPastedStackKeepsItsFirstMappedFamily() { |
| let blocks = DocsHTMLClipboard.blocks("<p><span style='font-family: Georgia, \"Times New Roman\", serif'>Serif</span></p>") |