oficina / docs Public

Paste reads a font stack in order, first family that maps

Astra's P2 on #84. family(cssStack:) tried every name in the stack for
an exact family or Word name before trying any as a fallback alias, so a
later exact name beat an earlier alias: Georgia, "Times New Roman", serif
pasted as Liberation Serif instead of Caladea, and Menlo, "Courier New",
monospace as Liberation Mono instead of JetBrains Mono. Each name is now
tried by its Word name, its own name and then as an alias before the next
name is consulted. No name mapping is still the default, Carlito.

The new stack cases and the HTML paste case failed on the old resolver
with exactly those results.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev 5d8c2a518b057b10dc45e1e72e868af73ff618c9 parent 85309e0
2 files changed, +27 −12
  • native/apple/Sources/DocsKit/DocsFonts.swift +11 −12
  • native/apple/Tests/DocsKitTests/DocsFontPickerTests.swift +16 −0
diff --git a/native/apple/Sources/DocsKit/DocsFonts.swift b/native/apple/Sources/DocsKit/DocsFonts.swift
index bd728fd..66fe0d4 100644
--- a/native/apple/Sources/DocsKit/DocsFonts.swift
+++ b/native/apple/Sources/DocsKit/DocsFonts.swift
@@ -206,28 +206,27 @@ public enum DocsFonts {
font(catalog.standard, bold: false, italic: false, size: size)
}
- /// The family a pasted font name stands for, deterministically, from the
- /// shared list alone. A CSS stack is read in order. A name is first
- /// matched exactly, against each family's name and its Word name (the
- /// rule internal/fonts' ByDocxName applies to .docx); failing that,
+ /// 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
+ /// 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 nothing matches,
- /// which is the default: an unknown sans-serif face pastes as Carlito.
+ /// is JetBrains Mono, generic serif is Caladea. Nil when no name maps,
+ /// which is the default: an unknown face pastes as Carlito.
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 {
- // A twin's Word name wins over a family that happens to be
- // called that, as in ByDocxName: Calibri is Carlito.
- if let family = catalog.families.first(where: { $0.docxName.lowercased() == name }) ?? catalog.families.first(where: { $0.name.lowercased() == name }) {
+ if let family = 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
}
}
- for name in names {
- if let family = catalog.families.first(where: { $0.fallback.contains { $0.lowercased() == name } }) { return family }
- }
return nil
}
}
diff --git a/native/apple/Tests/DocsKitTests/DocsFontPickerTests.swift b/native/apple/Tests/DocsKitTests/DocsFontPickerTests.swift
index bedca9b..3d3260a 100644
--- a/native/apple/Tests/DocsKitTests/DocsFontPickerTests.swift
+++ b/native/apple/Tests/DocsKitTests/DocsFontPickerTests.swift
@@ -90,5 +90,21 @@ final class DocsFontPickerTests: XCTestCase {
XCTAssertEqual(DocsFonts.family(cssStack: "Source Serif 4")?.key, "sourceserif")
XCTAssertEqual(DocsFonts.family(cssStack: "sans-serif")?.key, "carlito")
XCTAssertNil(DocsFonts.family(cssStack: "Wingdings"))
+ // A stack is read in order: the first name that maps wins, whether it
+ // maps by its own name or as a name a family stands in for.
+ XCTAssertEqual(DocsFonts.family(cssStack: "Georgia, \"Times New Roman\", serif")?.key, "caladea")
+ XCTAssertEqual(DocsFonts.family(cssStack: "Menlo, \"Courier New\", monospace")?.key, "jetbrains")
+ XCTAssertEqual(DocsFonts.family(cssStack: "Helvetica, Calibri, sans-serif")?.key, "sans")
+ XCTAssertEqual(DocsFonts.family(cssStack: "Wingdings, Cambria, Georgia")?.key, "caladea")
+ XCTAssertEqual(DocsFonts.family(cssStack: "Wingdings, Calibri, Arial")?.key, "carlito")
+ }
+
+ /// 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>")
+ guard case .object(let paragraph)? = blocks.first, case .array(let children)? = paragraph["children"],
+ case .object(let text)? = children.first, case .array(let runs)? = text["runs"],
+ case .object(let run)? = runs.first, case .object(let marks)? = run["marks"] else { return XCTFail("no run: \(blocks)") }
+ XCTAssertEqual(marks.keys.filter { $0.hasPrefix("font_") }, ["font_caladea"])
}
}