@@ -22,11 +22,13 @@ fn debug_font_println(s string) {
2222// If the env variable `VUI_FONT` is set this is used instead.
2323// NOTE that, in some cases, the function calls out to external OS programs
2424// so running this in a hot loop is not advised.
25+ @[manualfree]
2526pub fn default () string {
2627 env_font := os.getenv ('VUI_FONT' )
2728 if env_font != '' && os.exists (env_font) {
2829 return env_font
2930 }
31+ unsafe { env_font.free () }
3032 $if windows {
3133 if os.exists ('C:\\ Windows\\ Fonts\\ segoeui.ttf' ) {
3234 debug_font_println ('Using font "C:\\ Windows\\ Fonts\\ segoeui.ttf"' )
@@ -44,6 +46,7 @@ pub fn default() string {
4446 return font
4547 }
4648 }
49+ unsafe { fonts.free () }
4750 }
4851 $if android {
4952 xml_files := ['/system/etc/system_fonts.xml' , '/system/etc/fonts.xml' ,
@@ -54,6 +57,7 @@ pub fn default() string {
5457 if os.is_file (xml_file) && os.is_readable (xml_file) {
5558 xml := os.read_file (xml_file) or { continue }
5659 lines := xml.split ('\n ' )
60+ unsafe { xml.free () }
5761 mut candidate_font := ''
5862 for line in lines {
5963 if line.contains ('<font' ) {
@@ -65,12 +69,16 @@ pub fn default() string {
6569 debug_font_println ('Using font "${candidate_path }"' )
6670 return candidate_path
6771 }
72+ unsafe { candidate_path.free () }
6873 }
6974 }
7075 }
7176 }
77+ unsafe { candidate_font.free () }
7278 }
7379 }
80+ unsafe { font_locations.free () }
81+ unsafe { xml_files.free () }
7482 }
7583 mut fm := os.execute ("fc-match --format='%{file}\n ' -s" )
7684 if fm.exit_code == 0 {
@@ -81,59 +89,93 @@ pub fn default() string {
8189 return l
8290 }
8391 }
92+ unsafe { lines.free () }
8493 } else {
8594 panic ('fc-match failed to fetch system font' )
8695 }
96+ unsafe { fm.free () }
8797 panic ('failed to init the font' )
8898}
8999
90100// get_path_variant returns the `font_path` file name replaced with the
91101// file name of the font's `variant` version if it exists.
102+ @[manualfree]
92103pub fn get_path_variant (font_path string , variant Variant) string {
93104 // TODO: find some way to make this shorter and more eye-pleasant
94105 // NotoSans, LiberationSans, DejaVuSans, Arial and SFNS should work
95106 mut file := os.file_name (font_path)
107+ defer { unsafe { file.free () } }
108+
96109 mut fpath := font_path.replace (file, '' )
97- file = file.replace ('.ttf' , '' )
110+ defer { unsafe { fpath.free () } }
111+
112+ mut_replace (file, '.ttf' , '' )
113+
114+ flower := file.to_lower ()
115+ defer { unsafe { flower.free () } }
98116
99117 match variant {
100118 .normal {}
101119 .bold {
102120 if fpath.ends_with ('-Regular' ) {
103- file = file. replace ( '-Regular' , '-Bold' )
121+ mut_replace ( file, '-Regular' , '-Bold' )
104122 } else if file.starts_with ('DejaVuSans' ) {
105- file + = '-Bold'
106- } else if file. to_lower () .starts_with ('arial' ) {
107- file + = 'bd'
123+ mut_plus ( file, '-Bold' )
124+ } else if flower .starts_with ('arial' ) {
125+ mut_plus ( file, 'bd' )
108126 } else {
109- file + = '-bold'
127+ mut_plus ( file, '-bold' )
110128 }
111129 $if macos {
112130 if os.exists ('SFNS-bold' ) {
113- file = 'SFNS-bold'
131+ mut_assign ( file, 'SFNS-bold' )
114132 }
115133 }
116134 }
117135 .italic {
118136 if file.ends_with ('-Regular' ) {
119- file = file. replace ( '-Regular' , '-Italic' )
137+ mut_replace ( file, '-Regular' , '-Italic' )
120138 } else if file.starts_with ('DejaVuSans' ) {
121- file + = '-Oblique'
122- } else if file. to_lower () .starts_with ('arial' ) {
123- file + = 'i'
139+ mut_plus ( file, '-Oblique' )
140+ } else if flower .starts_with ('arial' ) {
141+ mut_plus ( file, 'i' )
124142 } else {
125- file + = 'Italic'
143+ mut_plus ( file, 'Italic' )
126144 }
127145 }
128146 .mono {
129147 if ! file.ends_with ('Mono-Regular' ) && file.ends_with ('-Regular' ) {
130- file = file. replace ( '-Regular' , 'Mono-Regular' )
131- } else if file. to_lower () .starts_with ('arial' ) {
148+ mut_replace ( file, '-Regular' , 'Mono-Regular' )
149+ } else if flower .starts_with ('arial' ) {
132150 // Arial has no mono variant
133151 } else {
134- file + = 'Mono'
152+ mut_plus ( file, 'Mono' )
135153 }
136154 }
137155 }
138- return '${fpath }${file }.ttf'
156+ res := '${fpath }${file }.ttf'
157+ return res
158+ }
159+
160+ fn mut_replace (s & string , find string , replacement string ) {
161+ new := (* s).replace (find, replacement)
162+ unsafe { s.free () }
163+ unsafe {
164+ * s = new
165+ }
166+ }
167+
168+ fn mut_plus (s & string , tail string ) {
169+ new := (* s) + tail
170+ unsafe { s.free () }
171+ unsafe {
172+ * s = new
173+ }
174+ }
175+
176+ fn mut_assign (s & string , value string ) {
177+ unsafe { s.free () }
178+ unsafe {
179+ * s = value.clone ()
180+ }
139181}
0 commit comments