1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

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

This commit is contained in:
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

View File

@ -82,18 +82,25 @@ 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 {
// 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 { bytes = os.read_apk_asset(c.font_path) or {
println('failed to load font "$c.font_path"') println('failed to load font "$c.font_path"')
return none return none
} }
}
} $else { } $else {
bytes = os.read_bytes(c.font_path) or { bytes = os.read_bytes(c.font_path) or {
println('failed to load font "$c.font_path"') println('failed to load font "$c.font_path"')
@ -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 {