Skip to content

Commit

Permalink
Merge pull request #657 from takahirom/takahirom/fix-when-the-font-is…
Browse files Browse the repository at this point in the history
…-not-avaible-case/2025-02-15

Fix issue where Roborazzi crash when the font is not available
  • Loading branch information
takahirom authored Feb 16, 2025
2 parents 4a1c149 + d86d26c commit a188aa5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
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

0 comments on commit a188aa5

Please sign in to comment.