TrueType Font Parser Light for Android based on Apache FOP.
1. Download the latest AAR .
2. Added the dependency from mavenCentral:
Kotlin DSL
implementation("io.github.yubyf:truetypeparser-light:$latest_version")Groovy
implementation 'io.github.yubyf:truetypeparser-light:${latest_version}'Maven:
<dependency>
<groupId>io.github.yubyf</groupId>
<artifactId>truetypeparser-light</artifactId>
<version>$latest_version</version>
<type>aar</type>
</dependency>val fontPath = "Your font path"
val ttfFile: TTFFile = TTFFile.open(File(fontPath))or
val inputStream = "Your font InputStream(File|Assets|...)"
val ttfFile: TTFFile = TTFFile.open(inputStream)All keys of locale related fields are standardized from languageId of Macintosh or Microsoft platform as the tags for identifying languages based on the IETF BCP 47 specification - BCP47, like en-US, zh-CN, etc.
val copyrightMap : Map<String, String> = ttfFile.copyrights
val fullNameMap : Map<String, String> = ttfFile.fullNames
val postscriptNameMap : Map<String, String> = ttfFile.postscriptNames
val familyMap : Map<String, String> = ttfFile.families
val subfamilyMap : Map<String, String> = ttfFile.subfamilies
val manufacturerMap : Map<String, String> = ttfFile.manufacturers
val designerMap : Map<String, String> = ttfFile.designers
val sampleTextMap : Map<String, String> = ttfFile.sampleTextsAnd for ease of use, lib also provides a getter function for each field with java.util.Locale as the parameter and the String as the return value:
// Kotlin style getter operator function
val copyright : String = copyrightMap[Locale.ENGLISH]
val fullName : String = fullNameMap[Locale.SIMPLIFIED_CHINESE]
val family : String = familyMap[Locale.JAPANESE]
// General getter function
val copyright : String = getValueOrFallbackByLocale(copyrightMap, Locale.ENGLISH)
val fullName : String = getValueOrFallbackByLocale(fullNameMap, Locale.SIMPLIFIED_CHINESE)
val family : String = getValueOrFallbackByLocale(familyMap, Locale.JAPANESE)
...If no value is found for the specified java.util.Locale, fallback and try to find the first matching locale in the following order and returns the value corresponding to the found locale:
- Locale in field
Mapthat has the same language as the givenjava.util.Locale - Tag of
java.util.Locale.USgenerated byjava.util.Locale#toLanguageTag() - Locale with English language
- Tag of
java.util.Locale.ROOTgenerated byjava.util.Locale#toLanguageTag() - Locale of first entry in field
Map
val vendorURL : String = ttfFile.vendorURL
val designerURL : String = ttfFile.designerURL
val weight : Int = ttfFile.weightClass// Whether the font is variable font.
val variable : Boolean = ttfFile.variable
// Get the variation axes.
val axes : List<VariationAxis> = ttfFile.variationAxes
// Get the variation instances.
val instances : List<VariationInstance> = ttfFile.variationInstances- Fixed wrong fixed float value read from font file.
- Added properties
variationAxesandvariationInstancesfor variable fonts.
- Added property
variableto indicate if the font is a variable font.
- Replaced
getXXX()functions of locale related fields with operator functionget()and general functiongetValueOrFallbackByLocale().
Since the original repo has not been updated, this fork has made several optimizations to the TrueTypeParser-Light module:
- Refactored with Kotlin.
- Optimized the memory usage of file reading(issue).
- Added several extra font properties in
TTFFile, such asmanufacturer,designer,vendorURL,designerURL,preferSubfamily, etc. - Modified the
localerelated fields tomaptype, users can get the corresponding field values byLocaleas key. Please see the Usage for details of usage.
Copyright (c) 2022 Alex Liu
Copyright (C) 2016 Jared Rummler
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.