Cairn · module

Cairn.Font

lean/Cairn/Font.lean

Text metrics: the one place layout depends on the host.

Layout is parameterised by a TextMetrics. Theorems quantify over every metrics that is Valid; at run time the host supplies Core Text's (an assume claim, checked by logging and replay). Text indices are Unicode scalar positions (String.length), not bytes and not UTF-16. Grapheme clusters are not modelled in M1. A metrics measures one line; wrapped text (Node.para) is broken into lines by Lean from one measurement of the whole string (Cairn.Text.Wrap).

Imports Cairn.Geometry

Order

Structures 3

structure

Cairn.Font

lean/Cairn/Font.lean:39
Font : Type
Fields
family : Nat

0 is the system UI font, 1 the system monospaced font.

size : Nat

Points.

weight : Nat

100–900, CSS-style.

italic : Bool
width : FontWidth
tracking : Int

Extra space after every character, in hundredths of a point (SwiftUI's tracking): 140 is 1.4 pt. Measured and painted alike.

tabular : Bool

Digits all one width (SwiftUI's monospacedDigit), so a clock doesn't jitter.

structure

Cairn.TextMetrics

lean/Cairn/Font.lean:55
TextMetrics : Type
Fields
offsets : Array Int

Caret offsets: entry i is the x offset of the boundary before scalar i, so the array has s.length + 1 entries, starts at 0 and never decreases. The last entry is the width.

ascent : Int
descent : Int
structure

Cairn.TextMetrics.Valid

lean/Cairn/Font.lean:69
Valid (m : TextMetrics) : Prop

What every metrics must satisfy. Layout and text theorems assume this and nothing more.

Fields
size : (m.offsets f s).size = s.length + 1
zero : (m.offsets f s)[0]? = some 0
mono : (m.offsets f s)[i] ≤ (m.offsets f s)[j]
ascent : 0 ≤ m.ascent f
descent : 0 ≤ m.descent f

Inductive types 1

inductive

Cairn.FontWidth

lean/Cairn/Font.lean:21
FontWidth : Type

A font's width axis: SwiftUI's Font.Width presets, which the system font's variable width axis implements (NSFont.Width: standard 0, condensed −0.2, expanded 0.2, compressed −0.4).

Constructors
standard : FontWidth
condensed : FontWidth
expanded : FontWidth
compressed : FontWidth

Definitions 5

def

Cairn.FontWidth.code

lean/Cairn/Font.lean:26
code : FontWidth → Nat

The width's code on the wire and in the metrics ABI.

def

Cairn.FontWidth.ofCode?

lean/Cairn/Font.lean:29
ofCode? : Nat → Option FontWidth
def

Cairn.TextMetrics.width

lean/Cairn/Font.lean:64
width (m : TextMetrics) (f : Font) (s : String) : Int
def

Cairn.TextMetrics.lineHeight

lean/Cairn/Font.lean:66
lineHeight (m : TextMetrics) (f : Font) : Int
def

Cairn.TextMetrics.mono

lean/Cairn/Font.lean:78
mono : TextMetrics

A deterministic stand-in for tests and examples: every scalar is size * 6 / 10 wide.

Theorems 2

theorem

Cairn.FontWidth.ofCode_code

provedlean/Cairn/Font.lean:33
ofCode_code (w : FontWidth) : ofCode? w.code = some w
theorem

Cairn.FontWidth.code_lt

provedlean/Cairn/Font.lean:36
code_lt (w : FontWidth) : w.code < 4

Instances 10

instance

Cairn.instDecidableEqFontWidth

lean/Cairn/Font.lean:23
instDecidableEqFontWidth : DecidableEq FontWidth
instance

Cairn.instBEqFontWidth

lean/Cairn/Font.lean:23
instBEqFontWidth : BEq FontWidth
instance

Cairn.instHashableFontWidth

lean/Cairn/Font.lean:23
instHashableFontWidth : Hashable FontWidth
instance

Cairn.instReprFontWidth

lean/Cairn/Font.lean:23
instReprFontWidth : Repr FontWidth
instance

Cairn.instInhabitedFontWidth

lean/Cairn/Font.lean:23
instInhabitedFontWidth : Inhabited FontWidth
instance

Cairn.instInhabitedFont

lean/Cairn/Font.lean:53
instInhabitedFont : Inhabited Font
instance

Cairn.instReprFont

lean/Cairn/Font.lean:53
instReprFont : Repr Font
instance

Cairn.instDecidableEqFont

lean/Cairn/Font.lean:53
instDecidableEqFont : DecidableEq Font
instance

Cairn.instHashableFont

lean/Cairn/Font.lean:53
instHashableFont : Hashable Font
instance

Cairn.instBEqFont

lean/Cairn/Font.lean:53
instBEqFont : BEq Font