gg: support getting system font on Android (#8611)
parent
e57b73bcfc
commit
de9813233f
|
@ -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 {
|
||||||
|
|
Loading…
Reference in New Issue