From c99857afac693661799242b58df1a3fbf672c161 Mon Sep 17 00:00:00 2001 From: Lanius Trolling Date: Fri, 21 Mar 2025 06:16:28 -0400 Subject: [PATCH] Use AutoCloseable constructor with Graphics::dispose instead of finally block --- .../kotlin/info/mechyrdia/lore/FontDrawing.kt | 94 +++++++++---------- 1 file changed, 47 insertions(+), 47 deletions(-) diff --git a/src/main/kotlin/info/mechyrdia/lore/FontDrawing.kt b/src/main/kotlin/info/mechyrdia/lore/FontDrawing.kt index e6acfde..77e3c11 100644 --- a/src/main/kotlin/info/mechyrdia/lore/FontDrawing.kt +++ b/src/main/kotlin/info/mechyrdia/lore/FontDrawing.kt @@ -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() } } -- 2.25.1