gg: support getting system font on Android (#8611)

pull/8613/head
Larpon 2021-02-06 17:36:43 +01:00 committed by GitHub
parent e57b73bcfc
commit de9813233f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 37 additions and 5 deletions

View File

@ -82,17 +82,24 @@ fn new_ft(c FTConfig) ?&FT {
// Load default font // 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"') println('failed to load font "$c.font_path"')
return none return none
} }
} }
mut bytes := []byte{} mut bytes := []byte{}
$if android { $if android {
bytes = os.read_apk_asset(c.font_path) or { // First try any filesystem paths
println('failed to load font "$c.font_path"') bytes = os.read_bytes(c.font_path) or { []byte{} }
return none 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 { } $else {
bytes = os.read_bytes(c.font_path) or { 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('<font') {
candidate_font = line.all_after('>').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') } s := os.exec('fc-list') or { panic('failed to fetch system fonts') }
system_fonts := s.output.split('\n') system_fonts := s.output.split('\n')
for line in system_fonts { for line in system_fonts {