Skip to content

Commit

Permalink
Add Arial and Consolas as backup fonts on Linux and mention font name…
Browse files Browse the repository at this point in the history
… when one fails to load.
  • Loading branch information
zacharee committed Jan 16, 2024
1 parent fd52489 commit c21736b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ internal class FontFamilyResolverImpl(
platformFontLoader = platformFontLoader,
onAsyncCompletion = { /* nothing */ },
createDefaultTypeface = createDefaultTypeface
) ?: throw IllegalStateException("Could not load font")
) ?: throw FontLoadFailedException(typeRequest.fontFamily)
}
}

Expand Down Expand Up @@ -100,7 +100,7 @@ internal class FontFamilyResolverImpl(
platformFontLoader,
onAsyncCompletion,
createDefaultTypeface
) ?: throw IllegalStateException("Could not load font")
) ?: throw FontLoadFailedException(typefaceRequest.fontFamily)
}
return result
}
Expand Down Expand Up @@ -197,7 +197,7 @@ internal class TypefaceRequestCache {
}
}
} catch (cause: Exception) {
throw IllegalStateException("Could not load font", cause)
throw FontLoadFailedException(typefaceRequest.fontFamily, cause)
}
synchronized(lock) {
// async result may have completed prior to this block entering, do not overwrite
Expand All @@ -222,7 +222,7 @@ internal class TypefaceRequestCache {
val next = try {
resolveTypeface(typeRequest)
} catch (cause: Exception) {
throw IllegalStateException("Could not load font", cause)
throw FontLoadFailedException(typeRequest.fontFamily, cause)
}

// only cache immutable, should not reach as FontListFontFamilyTypefaceAdapter already
Expand All @@ -245,4 +245,12 @@ internal class TypefaceRequestCache {
get() = synchronized(lock) {
resultCache.size
}
}
}

internal class FontLoadFailedException(
fontFamily: FontFamily?,
cause: Throwable? = null,
) : IllegalStateException(
message = "Failed to load font $fontFamily. Is it installed on the system?",
cause = cause,
)
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,9 @@ private val GenericFontFamiliesMapping: Map<String, List<String>> by lazy {
when (currentPlatform()) {
Platform.Linux ->
mapOf(
FontFamily.SansSerif.name to listOf("Noto Sans", "DejaVu Sans"),
FontFamily.SansSerif.name to listOf("Noto Sans", "DejaVu Sans", "Arial"),
FontFamily.Serif.name to listOf("Noto Serif", "DejaVu Serif", "Times New Roman"),
FontFamily.Monospace.name to listOf("Noto Sans Mono", "DejaVu Sans Mono"),
FontFamily.Monospace.name to listOf("Noto Sans Mono", "DejaVu Sans Mono", "Consolas"),
// better alternative?
FontFamily.Cursive.name to listOf("Comic Sans MS")
)
Expand Down

0 comments on commit c21736b

Please sign in to comment.