flowchart LR
O["raw options<br>family, weight, size,<br>line_height, letter_spacing…"]
O -->|"BareFontInfo::create<br>(normalizes)"| B["BareFontInfo<br>8-axis identity"]
B -->|get_id| K["cache key string"]
B -->|"FontInfo::estimated<br>(untrusted)"| E["FontInfo"]
B -->|"browser measurement<br>internal/viewer/browser/config"| M["FontInfo (trusted)"]
E --> R["render / layout"]
M --> R///|
test "get_id joins the eight axes in declaration order" {
let bare = @config.BareFontInfo::create(
font_family="Menlo",
font_weight="normal",
font_size=12.0,
font_feature_settings=@config.editor_font_ligatures_off,
font_variation_settings=@config.font_variation_off,
line_height=18.0,
letter_spacing=0.0,
pixel_ratio=2.0,
)
debug_inspect(
bare.get_id(),
content=(
#|"2-Menlo-normal-12-\"liga\" off, \"calt\" off-normal-18-0"
),
)
}flowchart TB
I["raw line_height"] --> Z{"== 0 ?"}
Z -->|yes| G["golden_line_height_ratio × font_size"]
Z -->|no| S{"< 8 ?"}
S -->|yes| EM["line_height × font_size<br>(treated as em)"]
S -->|no| PX["line_height<br>(treated as px)"]
G --> R["round"]
EM --> R
PX --> R
R --> F["max(result, minimum_line_height = 8)"]///|
fn line_height_for(raw : Double, size : Double) -> Int32 {
@config.BareFontInfo::create(
font_family="Menlo",
font_weight="normal",
font_size=size,
font_feature_settings=@config.editor_font_ligatures_off,
font_variation_settings=@config.font_variation_off,
line_height=raw,
letter_spacing=0.0,
pixel_ratio=1.0,
).line_height
}
///|
test "zero means the platform golden ratio times the font size" {
let ratio = if @base_common.is_macintosh { 1.5 } else { 1.35 }
let expected = (ratio * 14.0).round().to_int()
debug_inspect(
(line_height_for(0.0, 14.0) == expected, expected >= 8),
content=(
#|(true, true)
),
)
}///|
test "below 8 is an em multiplier, 8 and above is a pixel value" {
debug_inspect(
(
// em multipliers
line_height_for(1.5, 20.0),
line_height_for(2.0, 20.0),
// pixel values
line_height_for(24.0, 20.0),
line_height_for(24.4, 20.0),
// rounded, then floored at the minimum
line_height_for(0.1, 20.0),
8,
),
content=(
#|(30, 40, 24, 24, 8, 8)
),
)
}///|
fn translated(weight : String) -> (String, String) {
let bare = @config.BareFontInfo::create(
font_family="Menlo",
font_weight=weight,
font_size=12.0,
font_feature_settings=@config.editor_font_ligatures_off,
font_variation_settings="translate",
line_height=0.0,
letter_spacing=0.0,
pixel_ratio=1.0,
)
(bare.font_weight, bare.font_variation_settings)
}
///|
test "translate emits a wght axis for numeric weights only" {
debug_inspect(
(
translated("normal"),
translated("bold"),
translated("600"),
// parseInt decimal-prefix semantics: trailing junk is ignored
translated("350abc"),
// no leading digits: parseInt yields NaN, and it is emitted verbatim
translated("lighter"),
),
content=(
#|(
#| ("normal", "normal"),
#| ("bold", "normal"),
#| ("normal", "'wght' 600"),
#| ("normal", "'wght' 350"),
#| ("normal", "'wght' NaN"),
#|)
),
)
}///|
test "equals ignores pixel_ratio and trust, while == does not" {
let bare = @config.BareFontInfo::create(
font_family="Menlo",
font_weight="normal",
font_size=12.0,
font_feature_settings=@config.editor_font_ligatures_off,
font_variation_settings=@config.font_variation_off,
line_height=0.0,
letter_spacing=0.0,
pixel_ratio=1.0,
)
let one = @config.FontInfo::estimated(bare)
let retina = {
..one,
pixel_ratio: 2.0,
is_trusted: true,
is_monospace: true,
}
debug_inspect(
(one.equals(retina), one == retina),
content=(
#|(true, false)
),
)
}///|
test "headless font info is explicitly untrusted" {
let fallback = @config.FontInfo::default()
debug_inspect(
(
fallback.is_trusted,
@config.FontInfo::estimated(fallback.bare()).is_trusted,
fallback.bare().font_size == fallback.font_size,
),
content=(
#|(false, false, true)
),
)
}///|
test "the three platform stacks are named constants" {
debug_inspect(
(
@config.default_mac_font_family, @config.default_windows_font_family, @config.default_linux_font_family,
),
content=(
#|(
#| "Menlo, Monaco, 'Courier New', monospace",
#| "Consolas, 'Courier New', monospace",
#| "'Droid Sans Mono', monospace",
#|)
),
)
}///|
test "the platform-independent parts of the defaults" {
let defaults = @config.editor_font_defaults
debug_inspect(
(
defaults.font_weight,
defaults.line_height,
defaults.letter_spacing,
// the size is 12 on macOS and 14 elsewhere
defaults.font_size == 12.0 || defaults.font_size == 14.0,
),
content=(
#|("normal", 0, 0, true)
),
)
}///|
test "EditorLayoutInfo bands are left offsets plus widths" {
let layout : @config.EditorLayoutInfo = {
width: 800.0,
height: 600.0,
glyph_margin_left: 0.0,
glyph_margin_width: 0.0,
line_numbers_left: 0.0,
line_numbers_width: 40.0,
decorations_left: 40.0,
decorations_width: 10.0,
content_left: 50.0,
content_width: 736.0,
horizontal_scrollbar_height: 12.0,
vertical_scrollbar_width: 14.0,
}
debug_inspect(
(
layout.content_left == layout.decorations_left + layout.decorations_width,
layout.content_left +
layout.content_width +
layout.vertical_scrollbar_width ==
layout.width,
),
content=(
#|(true, true)
),
)
}moon test --target js viewer/common/config
moon test --target native viewer/common/configfn BareFontInfo::create(font_family~ : String, font_weight~ : String, font_size~ : Double, font_feature_settings~ : String, font_variation_settings~ : String, line_height~ : Double, letter_spacing~ : Double, pixel_ratio~ : Double) -> BareFontInfopub(all) struct EditorLayoutInfo {
width : Double
height : Double
glyph_margin_left : Double
glyph_margin_width : Double
line_numbers_left : Double
line_numbers_width : Double
decorations_left : Double
decorations_width : Double
content_left : Double
content_width : Double
horizontal_scrollbar_height : Double
vertical_scrollbar_width : Double
} derive(Eq, Debug)pub(all) struct FontInfo {
pixel_ratio : Double
font_family : String
font_weight : String
font_size : Double
font_feature_settings : String
font_variation_settings : String
line_height : Int
letter_spacing : Double
is_trusted : Bool
is_monospace : Bool
typical_halfwidth_character_width : Double
typical_fullwidth_character_width : Double
can_use_halfwidth_rightwards_arrow : Bool
space_width : Double
middot_width : Double
wsmiddot_width : Double
max_digit_width : Double
} derive(Eq, Debug)let default_linux_font_family : Stringlet default_windows_font_family : Stringlet editor_font_ligatures_off : StringInstall
Download zipReadonly MoonBit code viewer harness inspired by Monaco and CodeMirror.