Allow apps to provide a TypefaceFactory for subtitles (e.g. for tts:fontFamily referencing a font in the app's assets directory)
@icbaker is already working on this.
Since May 17, 2023.
- Dominant language
- Java
- Stars
- 3k
- Forks
- 955
- Avg merge
- 12d 14h
- Merged PRs (30d)
- 2
Description
How can I configure exoplayer to use a custom font for subtitles, when the font name is referenced within the TTML sample?
The font is not already installed on the users device and would be distributed via the assets directory.
So given:
-
a ttf inside my assets directory:
-
The font is referenced inside a TTML sample's tts:fontFamily attribute, eg:
<head>
<styling>
<style tts:fontFamily="ReithSans, proportionalSansSerif, SansSerif, default" xml:id="textStyle" />
</styling>
</head>
<body style="textStyle">
<p>custom font appears here</p>
...
how best can I make sure ReithSans is used in the resulting Cues?
--
I note that TtmlRenderUtil is using TypefaceSpan(String fontFamily) rather than TypefaceSpan(Typeface typeface). I can see from breakpoints that the cues contain a TypefaceSpan with the string description of font families, including ReithSans, but from my understanding of Typeface this will be used to locate system fonts rather than fonts in the assets directory.
My current workaround is to intercept cues and replace the TypefaceSpan manually when adding a TextOutput:
fun attachSubtitlesView(subtitleView: SubtitleView) {
simpleExoplayer.addTextOutput { cues ->
val cuesWithFixedStyling = cues.map { it.applyTypefaceSpan(typeface) }
subtitleView.onCues(cuesWithFixedStyling)
}
}
private fun Cue.applyCustomSpans(typeface: Typeface): Cue {
val text = this.text as? SpannableStringBuilder
?: return this
val spanToReplace = text
.getSpans(0, text.length, TypefaceSpan::class.java)
.firstOrNull()
?: return this
text.removeSpan(spanToReplace)
text.setSpan(TypefaceSpan(typeface), 0, text.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
return this.buildUpon()
.setText(text)
.build()
}
but I would prefer not to have to recreate cues like this.
Is there a better approach?
--
I am using exoplayer 2.12.0 but have linked to TtmlRenderUtil in androidx/media above.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.