From de9813233f4d1d5565a0403017cc09ad781ae75e Mon Sep 17 00:00:00 2001 From: Larpon Date: Sat, 6 Feb 2021 17:36:43 +0100 Subject: [PATCH] gg: support getting system font on Android (#8611) --- vlib/gg/text_rendering.v | 42 +++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/vlib/gg/text_rendering.v b/vlib/gg/text_rendering.v index 2e51611196..8ee5122807 100644 --- a/vlib/gg/text_rendering.v +++ b/vlib/gg/text_rendering.v @@ -82,17 +82,24 @@ fn new_ft(c FTConfig) ?&FT { // Load default font } } - $if !android { - if c.font_path == '' || !os.exists(c.font_path) { + + if c.font_path == '' || !os.exists(c.font_path) { + $if !android { println('failed to load font "$c.font_path"') return none } } + mut bytes := []byte{} $if android { - bytes = os.read_apk_asset(c.font_path) or { - println('failed to load font "$c.font_path"') - return none + // First try any filesystem paths + bytes = os.read_bytes(c.font_path) or { []byte{} } + if bytes.len == 0 { + // ... then try the APK asset path + bytes = os.read_apk_asset(c.font_path) or { + println('failed to load font "$c.font_path"') + return none + } } } $else { bytes = os.read_bytes(c.font_path) or { @@ -258,6 +265,31 @@ pub fn system_font_path() string { } } } + $if android { + xml_files := ['/system/etc/system_fonts.xml', '/system/etc/fonts.xml', '/etc/system_fonts.xml', + '/etc/fonts.xml', '/data/fonts/fonts.xml', '/etc/fallback_fonts.xml'] + font_locations := ['/system/fonts', '/data/fonts'] + for xml_file in xml_files { + if os.is_file(xml_file) && os.is_readable(xml_file) { + xml := os.read_file(xml_file) or { continue } + lines := xml.split('\n') + mut candidate_font := '' + for line in lines { + if line.contains('').all_before('<').trim(' \n\t\r') + if candidate_font.contains('.ttf') { + for location in font_locations { + candidate_path := os.join_path(location, candidate_font) + if os.is_file(candidate_path) && os.is_readable(candidate_path) { + return candidate_path + } + } + } + } + } + } + } + } s := os.exec('fc-list') or { panic('failed to fetch system fonts') } system_fonts := s.output.split('\n') for line in system_fonts {