mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-21 09:20:19 +01:00
Introduce glyph fonts to correct spacing.
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright 2024 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package org.thoughtcrime.securesms.fonts
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Typeface
|
||||
import android.text.SpannableStringBuilder
|
||||
import android.text.TextPaint
|
||||
import android.text.style.MetricAffectingSpan
|
||||
|
||||
/**
|
||||
* Helper object for working with the SignalSymbols font
|
||||
*/
|
||||
object SignalSymbols {
|
||||
|
||||
enum class Glyph(val unicode: Char) {
|
||||
CHEVRON_RIGHT('\uE025'),
|
||||
PERSON_CIRCLE('\uE05E')
|
||||
}
|
||||
|
||||
enum class Weight {
|
||||
BOLD
|
||||
}
|
||||
|
||||
private val cache = mutableMapOf<Weight, Typeface>()
|
||||
|
||||
fun getSpannedString(
|
||||
context: Context,
|
||||
weight: Weight,
|
||||
glyph: Glyph
|
||||
): CharSequence {
|
||||
val typeface = getTypeface(context, weight)
|
||||
val span = CustomTypefaceSpan(typeface)
|
||||
|
||||
val text = SpannableStringBuilder(glyph.unicode.toString())
|
||||
text.setSpan(span, 0, text.length, 0)
|
||||
|
||||
return text
|
||||
}
|
||||
|
||||
private fun getTypeface(context: Context, weight: Weight): Typeface {
|
||||
return when (weight) {
|
||||
Weight.BOLD -> getBoldWeightedFont(context)
|
||||
else -> error("Unsupported weight: $weight")
|
||||
}
|
||||
}
|
||||
|
||||
private fun getBoldWeightedFont(context: Context): Typeface {
|
||||
return cache.getOrPut(
|
||||
Weight.BOLD
|
||||
) {
|
||||
Typeface.createFromAsset(
|
||||
context.assets,
|
||||
"fonts/SignalSymbols-Bold.otf"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom TypefaceSpan to support TypefaceSpan in API<28
|
||||
*
|
||||
* Source: https://www.youtube.com/watch?v=x-FcOX6ErdI&t=486s
|
||||
*/
|
||||
private class CustomTypefaceSpan(val font: Typeface?) : MetricAffectingSpan() {
|
||||
override fun updateMeasureState(textPaint: TextPaint) = update(textPaint)
|
||||
override fun updateDrawState(textPaint: TextPaint?) = update(textPaint)
|
||||
|
||||
private fun update(tp: TextPaint?) {
|
||||
tp.apply {
|
||||
val old = this!!.typeface
|
||||
val oldStyle = old?.style ?: 0
|
||||
val font = Typeface.create(font, oldStyle)
|
||||
typeface = font
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user