1 let measuring_text : CString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" 2 let measuring_text_length = measuring_text.length 3 4 type Font = 5 val size : u32 6 val font : mem skia/Font 7 font.set_subpixel true 8 set_edging FontEdging/SubpixelAntiAlias 9 set_hinting FontHinting/Slight 10 set_size size.as<f32> 11 12 let sizes = 13 let mut bounds = Rect.new 14 font.measure_text measuring_text.unwrap measuring_text_length.as<usize> 15 TextEncoding/UTF8 bounds@mut_ptr null 16 let height = bounds.bottom - bounds.top |> as<u32> 17 let offset_y = bounds.top 18 let line_height = height.as<f32> * 2 |> as<u32> 19 20 font.measure_text "A A".ptr 3 TextEncoding/UTF8 bounds@mut_ptr null 21 let width_with_space = bounds.right - bounds.left 22 font.measure_text "AA".ptr 2 TextEncoding/UTF8 bounds@mut_ptr null 23 let width_without_space = bounds.right - bounds.left 24 let advance = width_with_space - width_without_space 25 26 (=height, =line_height, =offset_y, =advance) 27 28 inherit control/Font height = sizes.height 29 line_height = sizes.line_height 30 advance = sizes.advance 31 offset_y = sizes.offset_y 32 offset_multiplier = 0.27 33 34 type FontManager = 35 let develop_path : String @auto 36 let font_mgr = sk/make_font_manager null sk/make_freetype_font_scanner 37 let font_path = "$develop_path/data/fonts/JetBrainsMono/JetBrainsMono-Medium.ttf" 38 let c_font_path = font_path.to_c_string 39 let typeface = font_mgr.make_from_file c_font_path 40 kd/free c_font_path 41 let fonts = Map<u32, Font>.new 42 43 def get_font (size : u32) = 44 if not fonts.contains size then 45 let font = Font size (skia/Font typeface@copy) 46 fonts.add size font 47 48 fonts[size] 49