@@ -54,31 +54,38 @@ class CaptchaFragment : Fragment(), WelcomeActivityCallback {
54
54
private fun createAsciiArtBitmap (text : String ): Bitmap {
55
55
val paint = Paint ().apply {
56
56
typeface = Typeface .MONOSPACE
57
- textSize = 24f
58
- color = Color .GREEN
57
+ textSize = 16f
58
+ color = Color .WHITE
59
59
isAntiAlias = false
60
+ letterSpacing = 0.05f
60
61
}
62
+ val lines = text.split(" \n " ).filter { it.isNotEmpty() }
63
+ val testRect = android.graphics.Rect ()
64
+ paint.getTextBounds(" █" , 0 , 1 , testRect)
65
+ val charWidth = testRect.width().toFloat()
66
+ val charHeight = testRect.height().toFloat()
61
67
62
- val lines = text.split(" \n " )
63
- val maxWidth = lines.maxOfOrNull { paint.measureText(it) }?.toInt() ? : 0
64
- val lineHeight = paint.fontMetrics.let { it.bottom - it.top }
65
- val totalHeight = (lineHeight * lines.size).toInt()
68
+ val maxLineLength = lines.maxOfOrNull { it.length } ? : 0
69
+ val calculatedWidth = (charWidth * maxLineLength * 1.1f ).toInt() // Add 10% buffer
66
70
71
+ // Use actual character height for line spacing
72
+ val lineSpacing = charHeight * 1.2f // 20% extra for line spacing
73
+ val totalHeight = (lineSpacing * lines.size).toInt()
74
+
75
+ // Generous padding for TV display
76
+ val padding = 80
67
77
val bitmap = Bitmap .createBitmap(
68
- maxWidth + 20 ,
69
- totalHeight + 20 ,
78
+ calculatedWidth + padding ,
79
+ totalHeight + padding ,
70
80
Bitmap .Config .ARGB_8888
71
81
)
72
-
73
82
val canvas = Canvas (bitmap)
74
83
canvas.drawColor(Color .BLACK )
75
-
76
- var y = - paint.fontMetrics.top + 10
84
+ var y = charHeight + (padding / 2 )
77
85
for (line in lines) {
78
- canvas.drawText(line, 10f , y, paint)
79
- y + = lineHeight
86
+ canvas.drawText(line, (padding / 2 ).toFloat() , y, paint)
87
+ y + = lineSpacing
80
88
}
81
-
82
89
return bitmap
83
90
}
84
91
0 commit comments