Refactor conversion of String to GlyphSequence
authorLanius Trolling <lanius@laniustrolling.dev>
Thu, 7 Mar 2024 12:53:45 +0000 (07:53 -0500)
committerLanius Trolling <lanius@laniustrolling.dev>
Thu, 7 Mar 2024 12:53:45 +0000 (07:53 -0500)
src/jvmMain/kotlin/info/mechyrdia/lore/fonts.kt

index 46f72bc075480027a2c2a3f6853633270f57ed2a..5924dcd00970a88938c7b17d8b5e4e8e458de496 100644 (file)
@@ -103,24 +103,18 @@ object MechyrdiaSansFont {
                }
        }
        
-       private fun TTFFile.getGlyphs(str: String): GlyphSequence {
-               val length = str.codePointCount(0, str.length)
-               val codeSeq = str.toCodePointSequence()
-               val glyphSeq = codeSeq.map { getGlyph(it) }
-               
-               val codes = codeSeq.iterator().let { iter ->
-                       IntArray(length) {
-                               assert(iter.hasNext())
-                               iter.next()
-                       }
-               }
+       private fun String.toCodePointArray(): IntArray {
+               val iter = toCodePointSequence().iterator()
                
-               val glyphs = glyphSeq.iterator().let { iter ->
-                       IntArray(length) {
-                               assert(iter.hasNext())
-                               iter.next()
-                       }
+               return IntArray(codePointCount(0, length)) { _ ->
+                       assert(iter.hasNext())
+                       iter.next()
                }
+       }
+       
+       private fun TTFFile.getGlyphs(str: String): GlyphSequence {
+               val codes = str.toCodePointArray()
+               val glyphs = IntArray(codes.size) { i -> getGlyph(codes[i]) }
                
                return GlyphSequence(IntBuffer.wrap(codes), IntBuffer.wrap(glyphs), null)
        }