Skip to content

Commit

Permalink
apply font ascent
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzhirkevich committed Jul 23, 2024
1 parent da562ab commit c88515c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public class LottieFontSpec internal constructor(
public val style : FontStyle,
public val weight : FontWeight,
public val path : String?,
public val origin: FontOrigin
public val origin: FontOrigin,
public val accent : Float
) {
public enum class FontOrigin {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ internal class FontList(
) {

@Transient
private val map = list.map {
private val map = list.flatMap {
listOf(it.name to it.spec, it.family to it.spec)
}.flatten().toMap()
}.toMap()

fun find(family: String): LottieFontSpec? {
return map[family]
Expand All @@ -44,7 +44,10 @@ internal class LottieFontAsset(
val path : String? = null,

@SerialName("origin")
val origin : FontOrigin? = null
val origin : FontOrigin? = null,

@SerialName("ascent")
val ascent : Float = 0f
) {

@Transient
Expand Down Expand Up @@ -74,7 +77,8 @@ internal class LottieFontAsset(
style = fontStyle,
weight = weight,
path = path,
origin = origin?.toSpecOrigin() ?: LottieFontSpec.FontOrigin.Unknown
origin = origin?.toSpecOrigin() ?: LottieFontSpec.FontOrigin.Unknown,
accent = ascent
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,15 +213,35 @@ internal class TextLayer(

val hasFontFamily = configureTextStyle(drawScope, document, state)

val ascent = document.fontFamily?.let {
state.composition.animation.fonts?.find(it)
}?.accent ?: 0f

if (hasFontFamily || state.enableTextGrouping) {
drawTextWithFonts(state, drawScope, document)
drawTextWithFonts(
state = state,
ascent = ascent,
drawScope = drawScope,
document = document
)
} else {
val glyphs = state.composition.findGlyphs(document.fontFamily)

if (glyphs != null) {
drawTextWithGlyphs(drawScope, document, state, glyphs)
drawTextWithGlyphs(
drawScope = drawScope,
document = document,
ascent = ascent,
state = state,
glyphs = glyphs
)
} else {
drawTextWithFonts(state, drawScope, document)
drawTextWithFonts(
state = state,
ascent = ascent,
drawScope = drawScope,
document = document
)
}
}

Expand Down Expand Up @@ -319,7 +339,6 @@ internal class TextLayer(
val fontFamily = animationState.fonts[strFontFamily]

val fontSpec = animationState.composition.animation.fonts?.find(strFontFamily)

val weight = fontSpec?.weight ?: FontWeight.Normal
val style = fontSpec?.style ?: FontStyle.Normal

Expand Down Expand Up @@ -378,6 +397,7 @@ internal class TextLayer(

private fun drawTextWithFonts(
state: AnimationState,
ascent: Float,
drawScope: DrawScope,
document: TextDocument
) {
Expand Down Expand Up @@ -408,15 +428,23 @@ internal class TextLayer(
lines.fastForEachIndexed { idx, line ->

canvas.save()
if (offsetCanvas(state, canvas, document, alLinesIdx + idx, line.width, true)) {
if (offsetCanvas(
state = state,
canvas = canvas,
document = document,
lineIndex = alLinesIdx + idx,
lineWidth = line.width,
accent = ascent,
withFonts = true
)) {
drawFontTextLine(
line.text,
measurer,
document,
drawScope,
canvas,
tracking,
state.enableTextGrouping
text = line.text,
textMeasurer = measurer,
documentData = document,
drawScope = drawScope,
canvas = canvas,
tracking = tracking,
drawFullLine = state.enableTextGrouping
)
}

Expand All @@ -428,6 +456,7 @@ internal class TextLayer(
private fun drawTextWithGlyphs(
drawScope: DrawScope,
document: TextDocument,
ascent: Float,
state: AnimationState,
glyphs : Map<String, CharacterData>
) {
Expand All @@ -446,12 +475,20 @@ internal class TextLayer(
textLines.fastForEachIndexed { outerIndex, line ->
val boxWidth = document.wrapSize?.getOrNull(0) ?: 0f

val lines = splitGlyphTextIntoLines(measurer, line,document.fontScale, boxWidth, tracking, glyphs);
val lines = splitGlyphTextIntoLines(measurer, line, document.fontScale, boxWidth, tracking, glyphs);

lines.forEachIndexed { innerIndex, l ->
canvas.save()

if (offsetCanvas(state, canvas, document, outerIndex + innerIndex, l.width,false)) {
if (offsetCanvas(
state = state,
canvas = canvas,
document = document,
lineIndex = outerIndex + innerIndex,
lineWidth = l.width,
accent = ascent,
withFonts = false
)) {
drawGlyphTextLine(
text = l.text,
state = state,
Expand Down Expand Up @@ -573,10 +610,14 @@ internal class TextLayer(
document: TextDocument,
lineIndex: Int,
lineWidth: Float,
accent : Float,
withFonts : Boolean,
): Boolean {

val position = document.wrapPosition?.toOffset() ?: Offset.Zero
val position = document.wrapPosition?.toOffset()
?.plus(Offset(x = 0f, y = accent/100f * document.fontSize))
?: Offset.Zero

val size = document.wrapSize?.let { Size(it[0], it[1]) } ?: Size.Zero

val lineSpacing = textAnimation?.style?.lineSpacing?.interpolated(state) ?: 0f
Expand Down

0 comments on commit c88515c

Please sign in to comment.