Skip to content

Commit

Permalink
Add more meaningful error messages around font loading
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszkwiecinski committed Aug 26, 2021
1 parent e919805 commit dc4b7ba
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 75 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ jobs:
fail-fast: false
matrix:
gradle: [ 6.5, 6.7.1, current, rc ]
agp: [ 4.1.0, 7.0.1, 7.1.0-alpha08 ]
agp: [ 4.1.0, 7.0.1, 7.1.0-alpha10 ]
exclude:
- gradle: 6.5
agp: 4.2.2
Expand All @@ -75,9 +75,9 @@ jobs:
- gradle: 6.7.1
agp: 7.0.1
- gradle: 6.5
agp: 7.1.0-alpha08
agp: 7.1.0-alpha10
- gradle: 6.7.1
agp: 7.1.0-alpha08
agp: 7.1.0-alpha10

name: Run Gradle-${{ matrix.gradle }}, AGP-${{ matrix.agp }}
steps:
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,19 @@ easylauncher {
| Custom font (locally installed) | `customRibbon(position: "top", font: "ComicSansMs")` | ![](icons/ribbon_custom_font.png) |
| Custom font (using font pack) | `customRibbon(position: "top", font: file("fonts/CustomFont.ttf"))` | ![](icons/ribbon_custom_font.png) |

<details>
<summary>Troubleshooting on "Problem reading font data."</summary>

When using docker or a _minimalistic_ environment one might encounter above error message when loading Fonts.
The solution is to make sure font support was installed i.e. by calling:
```
apk add --no-cache freetype fontconfig ttf-dejavu
```

See [related issue](https://github.com/usefulness/easylauncher-gradle-plugin/issues/201) for more information.

</details>

## Chrome-like filters

| Filter | Command | Result |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ class ChromeLikeFilter(

private val ribbonColor = ribbonColor ?: Color.DARK_GRAY
private val labelColor = labelColor ?: Color.WHITE
private val font = fontResource?.takeIf { it.exists() }
?.let { Font.createFont(Font.TRUETYPE_FONT, it) }
?: fontName?.let { Font(it, Font.PLAIN, 1) }
?: DEFAULT_EASYLAUNCHER_FONT
private val font = getFont(
resource = fontResource,
name = fontName
)
private val overlayHeight = overlayHeight ?: OVERLAY_HEIGHT
private val gravity = gravity ?: Gravity.BOTTOM

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ class ColorRibbonFilter(
private val ribbonColor = ribbonColor ?: Color(0, 0x72, 0, 0x99)
private val labelColor = labelColor ?: Color.WHITE
private val gravity = gravity ?: Gravity.TOPLEFT
private val font = fontResource?.takeIf { it.exists() }
?.let { Font.createFont(Font.TRUETYPE_FONT, it) }
?: fontName?.let { Font(it, Font.PLAIN, 1) }
?: DEFAULT_EASYLAUNCHER_FONT
private val font = getFont(
resource = fontResource,
name = fontName
)

@Suppress("ComplexMethod")
override fun apply(image: BufferedImage, adaptive: Boolean) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.project.starter.easylauncher.filter

import java.awt.Font
import java.io.File

private object FontLoader

private val EASYLAUNCHER_DEFAULT_FONT by lazy {
FontLoader::class.java.classLoader
.getResourceAsStream("Roboto-Regular.ttf")
.use {
runCatching { Font.createFont(Font.TRUETYPE_FONT, it) }
.getOrElse { error("Couldn't load bundled font. Visit issue #201 for more details") }
}
}

internal fun getFont(
resource: File?,
name: String?,
): Font {
if (resource != null) {
if (!resource.exists()) {
error("${resource.path} does not exits. Make sure it points at existing font resource.")
}

return runCatching { Font.createFont(Font.TRUETYPE_FONT, resource) }
.getOrElse { throw IllegalStateException("Error while loading ${resource.path}", it) }
}

if (name != null) {
return runCatching { Font.decode(name) }
.getOrElse { throw IllegalStateException("Couldn't load font $name.", it) }
}

return EASYLAUNCHER_DEFAULT_FONT
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.project.starter.easylauncher.plugin

import com.android.build.gradle.BaseExtension
import com.android.build.gradle.api.BaseVariant
import com.android.build.gradle.tasks.ExtractDeepLinksTask
import com.project.starter.easylauncher.filter.EasyLauncherFilter
import org.gradle.api.Plugin
import org.gradle.api.Project
Expand Down Expand Up @@ -99,12 +98,10 @@ class EasyLauncherPlugin : Plugin<Project> {

// They don't care: https://issuetracker.google.com/issues/187096666 🤷‍
// Discussion: https://github.com/usefulness/easylauncher-gradle-plugin/issues/165
tasks.withType(ExtractDeepLinksTask::class.java).configureEach {
val easylauncherTaskName = name.replace("extractDeepLinks", "easylauncher")
// Workaround for: https://github.com/gradle/gradle/issues/8057
if (tasks.names.contains(easylauncherTaskName)) {
it.dependsOn(easylauncherTaskName)
}
val extractDeeplinksTask = name.replace("easylauncher", "extractDeepLinks")
// Workaround for: https://github.com/gradle/gradle/issues/8057
if (tasks.names.contains(extractDeeplinksTask)) {
tasks.named(extractDeeplinksTask).configure { it.dependsOn(name) }
}

return tasks.register(name, EasyLauncherTask::class.java) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import com.project.starter.easylauncher.plugin.utils.WithGradleProjectTest
import com.project.starter.easylauncher.plugin.utils.buildScript
import com.project.starter.easylauncher.plugin.utils.libraryBuildscript
import org.assertj.core.api.Assertions.assertThat
import org.gradle.testkit.runner.TaskOutcome
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import java.awt.GraphicsEnvironment

internal class EasyLauncherConfigTest : WithGradleProjectTest() {

private val fixtureFont = GraphicsEnvironment.getLocalGraphicsEnvironment().allFonts.first()

@BeforeEach
fun setUp() {
rootDirectory.resolve("src/main/AndroidManifest.xml") {
Expand All @@ -24,14 +28,6 @@ internal class EasyLauncherConfigTest : WithGradleProjectTest() {
@Test
fun `custom ribbon`() {
rootDirectory.resolve("build.gradle").buildScript(
androidBlock = {
"""
buildTypes {
debug { }
release { }
}
""".trimIndent()
},
easylauncherBlock = {
"""
buildTypes {
Expand All @@ -40,7 +36,7 @@ internal class EasyLauncherConfigTest : WithGradleProjectTest() {
label: "bitcoin",
ribbonColor: "#8A123456",
labelColor: "#654321",
font: "Arial-Black",
font: "${fixtureFont.name}",
drawingOptions: ["IgnoreTransparentPixels"],
)
}
Expand All @@ -49,20 +45,12 @@ internal class EasyLauncherConfigTest : WithGradleProjectTest() {
}
)

runTask("assembleDebug", "--stacktrace")
runTask("assembleDebug")
}

@Test
fun `custom ribbon - drawing options`() {
rootDirectory.resolve("build.gradle").buildScript(
androidBlock = {
"""
buildTypes {
debug { }
release { }
}
""".trimIndent()
},
easylauncherBlock = {
"""
buildTypes {
Expand All @@ -71,7 +59,7 @@ internal class EasyLauncherConfigTest : WithGradleProjectTest() {
label: "bitcoin",
ribbonColor: "#8A123456",
labelColor: "#654321",
font: "Arial-Black",
font: "${fixtureFont.name}",
drawingOptions: ["IgnoreTransparentPixels", "AddExtraPadding"],
)
}
Expand All @@ -80,17 +68,9 @@ internal class EasyLauncherConfigTest : WithGradleProjectTest() {
}
)

runTask("assembleDebug", "--stacktrace")
runTask("assembleDebug")

rootDirectory.resolve("build.gradle").buildScript(
androidBlock = {
"""
buildTypes {
debug { }
release { }
}
""".trimIndent()
},
easylauncherBlock = {
"""
buildTypes {
Expand All @@ -99,7 +79,7 @@ internal class EasyLauncherConfigTest : WithGradleProjectTest() {
label: "bitcoin",
ribbonColor: "#8A123456",
labelColor: "#654321",
font: "Arial-Black",
font: "${fixtureFont.name}",
drawingOptions: ["AnUnknownOption"],
)
}
Expand All @@ -115,14 +95,6 @@ internal class EasyLauncherConfigTest : WithGradleProjectTest() {
@Test
fun `chrome like ribbon`() {
rootDirectory.resolve("build.gradle").buildScript(
androidBlock = {
"""
buildTypes {
debug { }
release { }
}
""".trimIndent()
},
easylauncherBlock = {
"""
buildTypes {
Expand All @@ -131,7 +103,7 @@ internal class EasyLauncherConfigTest : WithGradleProjectTest() {
label: "bitcoin",
ribbonColor: "#8A123456",
labelColor: "#654321",
font: "Tahoma",
font: "${fixtureFont.name}",
gravity: "top",
labelPadding: 12,
overlayHeight: 0.4,
Expand All @@ -143,31 +115,59 @@ internal class EasyLauncherConfigTest : WithGradleProjectTest() {
}
)

runTask("assembleDebug", "--stacktrace")
runTask("assembleDebug")
}

@Test
fun `library config`() {
rootDirectory.resolve("build.gradle").libraryBuildscript(
androidBlock = {
easylauncherBlock = {
"""
buildTypes {
debug {
filters = redRibbonFilter()
}
}
""".trimIndent()
}
)

runTask("assembleDebug")
}

@Test
fun `font error messages`() {
rootDirectory.resolve("build.gradle").buildScript(
androidBlock = { "" },
easylauncherBlock = {
"""
buildTypes {
debug { }
release { }
debug {
filters = chromeLike(font: "fixture-nonexistent-font")
}
}
""".trimIndent()
},
}
)

val nonExistentFontResult = runTask("assembleDebug")
assertThat(nonExistentFontResult.task(":easylauncherDebug")?.outcome).isEqualTo(TaskOutcome.SUCCESS)

rootDirectory.resolve("build.gradle").buildScript(
androidBlock = { "" },
easylauncherBlock = {
"""
buildTypes {
debug {
filters = redRibbonFilter()
filters = chromeLike(font: file("invalid-file.ttf"))
}
}
""".trimIndent()
}
)

runTask("assembleDebug", "--stacktrace")
val invalidFontFile = runTask("assembleDebug", shouldFail = true)

assertThat(invalidFontFile.output).contains("invalid-file.ttf does not exits. Make sure it points at existing font resource")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.project.starter.easylauncher.plugin.utils
import org.intellij.lang.annotations.Language
import java.io.File

fun File.buildScript(androidBlock: () -> String, easylauncherBlock: () -> String = { "" }) {
fun File.buildScript(androidBlock: () -> String = { "" }, easylauncherBlock: () -> String = { "" }) {
@Language("groovy")
val buildScript =
"""
Expand Down Expand Up @@ -37,7 +37,7 @@ fun File.buildScript(androidBlock: () -> String, easylauncherBlock: () -> String
writeText(buildScript)
}

fun File.libraryBuildscript(androidBlock: () -> String, easylauncherBlock: () -> String = { "" }) {
fun File.libraryBuildscript(androidBlock: () -> String = { "" }, easylauncherBlock: () -> String = { "" }) {
@Language("groovy")
val buildScript =
"""
Expand Down

0 comments on commit dc4b7ba

Please sign in to comment.