}
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()
}
}