Class SubtitleFonts

java.lang.Object
com.castlabs.subtitles.presentation.SubtitleFonts

public final class SubtitleFonts extends Object
This class contains static helpers and functions to manage additional fonts for subtitles and closed captions.

To fulfill the FCC requirements for closed captions, you need to allow the user to select from different fonts, including "casual", "cursive", "small capitals", and a "serif monospace" font. These fonts are not available on all Android devices and this class allows to load custom fonts bundles with the assets of your Application.

The SDK comes bundles with an AAR file that contains some open fonts to fill the gaps. You can load the AAR by adding it to your apps' build script dependencies:


     dependencies{
         ...
         compile 'com.castlabs.player:subtitles-fonts:3.0.5'
         ...
     }
 
With that module on place, the following fonts will be available:
Additional Fonts in the Subtitles-Fonts module
Font Type License
MarckScript Regular Casual SIL OPEN FONT LICENSE Version 1.1
Lobster Two Regular Cursive SIL OPEN FONT LICENSE Version 1.1
Texgyrecursor Regular Serif Monospace GUST Font License Version 1.0
Uberlin Small Capitals Regular 1001Fonts Free For Commercial Use License (FFC)

If you want to use different fonts, you can use the set(int, Typeface) method to initialize the font cache with different typefaces. This registeres the "normal" typeface for a given font type. If you want to support styling like bold and italics for these custom fonts, you need to register the variants as well using setVariant(Context, int, Typeface, int).

Since:
3.1.0
  • Field Details

  • Method Details

    • get

      @NonNull public static Typeface get(int fontType, @NonNull Context context)
      Get the typeface for the given type. Unless a different variant was registered, this returns the "normal" variant of the given font type.
      Parameters:
      fontType - The font type
      context - The context
      Returns:
      The typeface that maps to the given type
    • get

      public static Typeface get(@NonNull String familyName, @NonNull Context context)
      Returns a Typeface that matches the given font family name. If the family name is a generic font family (i.e. from a TTML file), this will return the typeface for the "normal" variant for that generic font. If the family name does not resolve to a generic font, any custom font families are checked. If also not custom font was found that matches the family name, the default typeface will be returned.
      Parameters:
      familyName - The family name
      context - The context
      Returns:
      The typeface that best matches the given font family name
    • getVariant

      public static Typeface getVariant(Typeface source, int style)
      Returns a typeface object that best matches the specified existing typeface and the specified Style. Use this call if you want to pick a new style from the same family of an existing typeface object.
      Parameters:
      source - The source typeface
      style - The style (normal, bold, italic) of the typeface. One of Typeface.NORMAL, Typeface.BOLD, Typeface.ITALIC, or Typeface.BOLD_ITALIC
      Returns:
      The best matching typeface.
    • setVariant

      public static void setVariant(@NonNull Context context, int source, @NonNull Typeface variant, int variantStyle)
      Register a typeface variant for the given source typeface.
      Parameters:
      context - The context
      source - The source Typeface
      variant - The variant that will be registered
      variantStyle - The variant styl. One of Typeface.NORMAL, Typeface.BOLD, Typeface.ITALIC, or Typeface.BOLD_ITALIC
    • setVariant

      public static void setVariant(@NonNull Typeface source, @NonNull Typeface variant, int variantStyle)
      Register a typeface variant for the given source typeface.
      Parameters:
      source - The source Typeface. This is the "normal" type and what you registered with set(int, Typeface)
      variant - The variant that will be registered
      variantStyle - The variant styl. One of Typeface.NORMAL, Typeface.BOLD, Typeface.ITALIC, or Typeface.BOLD_ITALIC
    • set

      public static void set(int fontType, Typeface typeface)
      Set the typeface for a given type
      Parameters:
      fontType - The type
      typeface - The typeface
    • addCustomFont

      public static void addCustomFont(@NonNull String familyName, @NonNull Typeface typeface)
      Register the "normal" variant of a custom font. This is used to add custom font families that might be referenced from styled subtitle formats such as TTML. You should register the "normal" variant of the font here and then eventually add additional variants using setVariant(Typeface, Typeface, int). Please note that the family name here must be the same name also used in the TTML file that references the custom font.

      Please note that the generic TTML font types are already recognized by the SDK and there is no need to register them explicitly. This is only required for custom font families.

      Parameters:
      familyName - The family name (has to be the same as in the TTML file)
      typeface - The typeface