Use AutoCloseable constructor with Graphics::dispose instead of finally block
authorLanius Trolling <lanius@laniustrolling.dev>
Fri, 21 Mar 2025 10:16:28 +0000 (06:16 -0400)
committerLanius Trolling <lanius@laniustrolling.dev>
Fri, 21 Mar 2025 10:16:28 +0000 (06:16 -0400)
src/main/kotlin/info/mechyrdia/lore/FontDrawing.kt

index e6acfde69a34d25053939a88b6bf9033586ffd18..77e3c1147fd4ab2a242e9b1b5e36712c74618eb6 100644 (file)
@@ -163,60 +163,60 @@ object MechyrdiaSansFont {
        }
        
        private fun layoutText(text: String, file: TTFFile, font: Font, align: TextAlignment): GeneralPath {
-               val img = BufferedImage(256, 160, BufferedImage.TYPE_INT_ARGB)
+               val img = BufferedImage(4, 4, BufferedImage.TYPE_INT_ARGB)
                val g2d = img.createGraphics()
-               try {
-                       val charHolder = CharArray(2)
-                       val lineHeight = file.rawLowerCaseAscent - file.rawLowerCaseDescent
-                       
-                       val lines = text.split("\r\n", "\n", "\r")
-                       val lineGlyphs = lines.map { file.getGlyphs(it) }
-                       val lineBasics = lineGlyphs.map { file.getBasicWidths(it) }
-                       val lineAdjust = lineGlyphs.zip(lineBasics) { glyphs, widths -> file.getGlyphPositions(glyphs, widths) }
-                       val lineWidths = lineBasics.zip(lineAdjust) { width, adjust -> getWidth(width, adjust) }
-                       val blockWidth = lineWidths.max()
-                       var ly = 0
-                       
-                       yieldThread()
-                       
-                       val shape = GeneralPath()
-                       val tf = AffineTransform()
-                       for ((li, line) in lines.withIndex()) {
-                               if (line.isNotBlank()) {
-                                       val lineWidth = lineWidths[li]
-                                       val lx = align.processWidth(blockWidth - lineWidth)
-                                       
-                                       var cx = 0
-                                       var cy = 0
-                                       
-                                       val basicAdv = lineBasics[li]
-                                       val adjusted = lineAdjust[li]
-                                       val glyphSeq = lineGlyphs[li]
-                                       for ((ci, codePoint) in glyphSeq.getCharacterArray(false).withIndex()) {
-                                               val length = Character.toChars(codePoint, charHolder, 0)
-                                               val glyph = font.layoutGlyphVector(g2d.fontRenderContext, charHolder, 0, length, Font.LAYOUT_LEFT_TO_RIGHT)
-                                               val glyphShape = glyph.outline as GeneralPath
-                                               val glyphShift = adjusted[ci]
+               AutoCloseable(g2d::dispose).use { _ ->
+                       try {
+                               val charHolder = CharArray(2)
+                               val lineHeight = file.rawLowerCaseAscent - file.rawLowerCaseDescent
+                               
+                               val lines = text.split("\r\n", "\n", "\r")
+                               val lineGlyphs = lines.map { file.getGlyphs(it) }
+                               val lineBasics = lineGlyphs.map { file.getBasicWidths(it) }
+                               val lineAdjust = lineGlyphs.zip(lineBasics) { glyphs, widths -> file.getGlyphPositions(glyphs, widths) }
+                               val lineWidths = lineBasics.zip(lineAdjust) { width, adjust -> getWidth(width, adjust) }
+                               val blockWidth = lineWidths.max()
+                               var ly = 0
+                               
+                               yieldThread()
+                               
+                               val shape = GeneralPath()
+                               val tf = AffineTransform()
+                               for ((li, line) in lines.withIndex()) {
+                                       if (line.isNotBlank()) {
+                                               val lineWidth = lineWidths[li]
+                                               val lx = align.processWidth(blockWidth - lineWidth)
                                                
-                                               tf.setToTranslation((lx + cx + glyphShift[0]).toDouble(), (ly + cy + glyphShift[1]).toDouble())
-                                               shape.append(glyphShape.getPathIterator(tf), false)
+                                               var cx = 0
+                                               var cy = 0
                                                
-                                               cx += glyphShift[2] + basicAdv[ci]
-                                               cy += glyphShift[3]
+                                               val basicAdv = lineBasics[li]
+                                               val adjusted = lineAdjust[li]
+                                               val glyphSeq = lineGlyphs[li]
+                                               for ((ci, codePoint) in glyphSeq.getCharacterArray(false).withIndex()) {
+                                                       val length = Character.toChars(codePoint, charHolder, 0)
+                                                       val glyph = font.layoutGlyphVector(g2d.fontRenderContext, charHolder, 0, length, Font.LAYOUT_LEFT_TO_RIGHT)
+                                                       val glyphShape = glyph.outline as GeneralPath
+                                                       val glyphShift = adjusted[ci]
+                                                       
+                                                       tf.setToTranslation((lx + cx + glyphShift[0]).toDouble(), (ly + cy + glyphShift[1]).toDouble())
+                                                       shape.append(glyphShape.getPathIterator(tf), false)
+                                                       
+                                                       cx += glyphShift[2] + basicAdv[ci]
+                                                       cy += glyphShift[3]
+                                               }
                                        }
+                                       
+                                       ly += lineHeight
+                                       
+                                       yieldThread()
                                }
                                
-                               ly += lineHeight
-                               
-                               yieldThread()
+                               return shape
+                       } catch (ex: Exception) {
+                               FontsLogger.error("Error converting text $text to font shape", ex)
+                               return GeneralPath()
                        }
-                       
-                       return shape
-               } catch (ex: Exception) {
-                       FontsLogger.error("Error converting text $text to font shape", ex)
-                       return GeneralPath()
-               } finally {
-                       g2d.dispose()
                }
        }