1 type Margin = struct 2 left : u32 3 right : u32 4 up : u32 5 down : u32 6 7 object Font = 8 val mono = "jetbrains_mono/24" 9 val space_offset_char = 'G' 10 11 type Font = 12 val name : String 13 val margin_up : f32 14 val margin_down : f32 15 val space_width : u32 16 val char_height : u32 17 val line_height : u32 18 val rectangles : Map<Char, Rectangle> 19 val offsets : Map<(Char, Char), i32> 20 val memory : ImageMemory 21 val memory_rgba : ImageMemory 22 23 object Font 24 def create (name : String) (file : FontFile) = 25 let rectangles = file.rectangles 26 let char_height = rectangles['a'].height 27 let line_height = char_height as f32 * 1.4 |> round as u32 28 let space_offset_char_width = rectangles[space_offset_char].width 29 assert file.space_width == space_offset_char_width 30 31 Font =name 32 margin_up = file.margin_up 33 margin_down = file.margin_down 34 space_width = file.space_width 35 =char_height 36 =line_height 37 =rectangles 38 offsets = file.offsets 39 memory = file.memory 40 memory_rgba = file.memory_rgba 41 42 type Font 43 def height = rectangles['a'].height 44 45 def offset left right = 46 let adjust (c : Char) = case c of 47 ' ' | '\n' -> Font.space_offset_char 48 else -> c 49 50 let l = adjust left 51 let r = adjust right 52 53 offsets.try_get (l, r) 54 |> or_else { 55 let arg = when 56 rectangles.contains l -> (l, Sys.unprintable_char) 57 rectangles.contains r -> (Sys.unprintable_char, r) 58 else -> (Sys.unprintable_char, Sys.unprintable_char) 59 60 offsets.try_get arg } 61 |> unwrap 62 63 def margin = 64 let divisor @param = 4 65 let l = height as f32 / divisor 66 let left = l.round as u32 67 let up = l * margin_up |> round as u32 68 let down = l * margin_down |> round as u32 69 70 Margin left left up down 71