Convert Font file to gzib format using python code
importgzip# Open the font file for readingwithopen('font.ttf', 'rb') asf:
font_data=f.read()
# Compress the font data with gzip using a higher compression level (9)compressed_data=gzip.compress(font_data, compresslevel=9)
# Write the compressed data to a new filewithopen('font_compressed.ttf.gz', 'wb') asf:
f.write(compressed_data)
This code will read font file from res/raw folder and uncompressed font data to a temporary file
packagecom.faysal.gzibfontimportandroid.content.Contextimportandroid.graphics.Typefaceimportandroid.widget.Toastimportjava.io.Fileimportjava.lang.Exceptionimportjava.util.zip.GZIPInputStreamfunloadCompressedFontFromResource(context:Context, resourceId:Int): Typeface? {
try {
// Open the compressed font file from the app's resourcesval compressedFontData = context.resources.openRawResource(resourceId).readBytes()
// Decompress the font data from the gzip fileval gzipInputStream =GZIPInputStream(compressedFontData.inputStream())
val fontData = gzipInputStream.readBytes()
gzipInputStream.close()
// Write the uncompressed font data to a temporary fileval tempFontFile =File.createTempFile("font_compressed", "ttf", context.cacheDir)
tempFontFile.outputStream().use { output ->
output.write(fontData)
}
// Load the font from the temporary file into a Typeface objectreturnTypeface.createFromFile(tempFontFile)
} catch (e:Exception) {
Toast.makeText(context, e.localizedMessage, Toast.LENGTH_SHORT).show()
returnTypeface.DEFAULT
}
}
Finally in activity
val typeface = loadCompressedFontFromResource(context, R.raw.font_compressed)
textView.typeface = typeface