Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue where Roborazzi crash when the font is not available #657

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import java.awt.BasicStroke
import java.awt.Color
import java.awt.Font
import java.awt.Graphics2D
import java.awt.GraphicsEnvironment
import java.awt.Rectangle
import java.awt.RenderingHints
import java.awt.font.FontRenderContext
Expand Down Expand Up @@ -237,9 +238,9 @@ class AwtRoboCanvas(width: Int, height: Int, filled: Boolean, bufferedImageType:
}

override fun differ(
other: RoboCanvas,
resizeScale: Double,
imageComparator: ImageComparator
other: RoboCanvas,
resizeScale: Double,
imageComparator: ImageComparator
): ImageComparator.ComparisonResult {
other as AwtRoboCanvas
val otherImage = other.bufferedImage
Expand Down Expand Up @@ -443,7 +444,7 @@ class AwtRoboCanvas(width: Int, height: Int, filled: Boolean, bufferedImageType:
// fill with 4dp margin
val textMargin = (4 * oneDpPx).toInt()
// Set size to 12dp
val font = Font("Courier New", Font.BOLD, fontSize)
val font = getFont(Font.BOLD, fontSize)
val textLayout = TextLayout(text, font, comparisonImageGraphics.fontRenderContext)
val bounds = textLayout.bounds
val rect = Rectangle(
Expand Down Expand Up @@ -519,7 +520,8 @@ class AwtRoboCanvas(width: Int, height: Int, filled: Boolean, bufferedImageType:
for (x in 0 until width) {
for (y in 0 until height) {
if (x >= originalImage.width || y >= originalImage.height
|| x >= comparedImage.width || y >= comparedImage.height) {
|| x >= comparedImage.width || y >= comparedImage.height
) {
diffImage.setRGB(x, y, -0x10000)
continue
}
Expand Down Expand Up @@ -553,9 +555,26 @@ private fun BufferedImage.scale(scale: Double): BufferedImage {
return after
}

internal val preferredFontName: String by lazy {
getSystemProperty("roborazzi.theme.typography.font.name", "Courier New")
}

internal fun getFont(style: Int, size: Int): Font {
return if (hasPreferredFont) {
Font(preferredFontName, style, size)
} else {
Font(Font.MONOSPACED, style, size)
}
}

internal val hasPreferredFont: Boolean by lazy {
GraphicsEnvironment.getLocalGraphicsEnvironment()
.availableFontFamilyNames.any { it.equals(preferredFontName, ignoreCase = true) }
}

private fun <T> BufferedImage.graphics(block: (Graphics2D) -> T): T {
val graphics = createGraphics()
graphics.font = Font("Courier New", Font.BOLD, 12)
graphics.font = getFont(Font.BOLD, 12)
val result = block(graphics)
graphics.dispose()
return result
Expand Down
1 change: 1 addition & 0 deletions sample-generate-preview-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ android {
it.systemProperties["robolectric.pixelCopyRenderMode"] = "hardware"
// For large preview
it.maxHeapSize = "4096m"
it.jvmArgs("-noverify")
}
}
}
Expand Down
Loading