Yubyf / TrueTypeParser-Light

TrueType Font Parser for Android

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TrueTypeParser-Light

Maven Central GitHub Workflow Status (branch) License API GitHub top language

TrueType Font Parser Light for Android based on Apache FOP.

Installation

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>

Usage

Parse

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)

Locale related fields(Map)

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.sampleTexts

And 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 Map that has the same language as the given java.util.Locale
  • Tag of java.util.Locale.US generated by java.util.Locale#toLanguageTag()
  • Locale with English language
  • Tag of java.util.Locale.ROOT generated by java.util.Locale#toLanguageTag()
  • Locale of first entry in field Map

Standard fields(String/Primitives)

val vendorURL : String = ttfFile.vendorURL
val designerURL : String = ttfFile.designerURL
val weight : Int = ttfFile.weightClass

Variation fields

// 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

ChangeLog

2.1.4

  • Fixed wrong fixed float value read from font file.

2.1.3

  • Added properties variationAxes and variationInstances for variable fonts.

2.1.2

  • Added property variable to indicate if the font is a variable font.

2.1.1

  • Replaced getXXX() functions of locale related fields with operator function get() and general function getValueOrFallbackByLocale().

2.0.1

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 as manufacturer, designer, vendorURL, designerURL, preferSubfamily, etc.
  • Modified the locale related fields to map type, users can get the corresponding field values by Locale as key. Please see the Usage for details of usage.

License

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.

About

TrueType Font Parser for Android

License:Apache License 2.0


Languages

Language:Kotlin 100.0%