Advertisement
vito-Z80

Correct font region for atlas texture

Sep 4th, 2020 (edited)
1,658
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.29 KB | None | 0 0
  1.  
  2.     private fun correctFontRegions(atlasImageFileNameNoExtension: String) {
  3.         val sb = StringBuilder()
  4.         val x = "x=".intern()
  5.         val y = "y=".intern()
  6.         val nl = "\n".intern()
  7.         val space = ' '
  8.         val fontsList = files?.filter { it.extension == "fnt" } ?: return
  9.         fontsList.forEach files@{ f ->
  10.             val lines = lazy { f.readLines() }.value
  11.             if (lines.any { it.contains(f.nameWithoutExtension) }) {
  12.                 val newLines = StringBuilder()
  13.                 val fontRegion = atlas.findRegion(f.nameWithoutExtension)
  14.                 val regionX = fontRegion.regionX
  15.                 val regionY = fontRegion.regionY
  16.                 lines.forEach fileLines@{ line ->
  17.                     if (line.contains(x)) {
  18.                         sb.clear()
  19.                         val strX = line.indexOf(x) + x.length
  20.                         var strXEnd = strX
  21.                         while (line[strXEnd] != space) strXEnd++
  22.                         strXEnd--
  23.                         val fontCharX = line.slice(strX..strXEnd).toInt()
  24.                         val newX = fontCharX + regionX
  25.                         sb.append(line.replaceRange(strX, strXEnd + 1, newX.toString()))
  26.  
  27.                         val strY = sb.indexOf(y) + y.length
  28.                         var strYEnd = strY
  29.                         while (sb[strYEnd] != space) strYEnd++
  30.                         strYEnd--
  31.                         val fontCharY = sb.slice(strY..strYEnd).toString().toInt()
  32.                         val newY = fontCharY + regionY
  33.                         sb.replaceRange(strY, strYEnd + 1, newY.toString()).also { newLines.append(it.toString() + nl) }
  34.                     } else {
  35.                         if (line.contains(f.nameWithoutExtension)) {
  36.                             line.replace(f.nameWithoutExtension, atlasImageFileNameNoExtension).also { newLines.append(it + nl) }
  37.                         } else {
  38.                             newLines.append(line + nl)
  39.                         }
  40.                     }
  41.                 }
  42.                 f.writeText(newLines.toString())
  43.                 Gdx.app.log(f.name, "has been fixed")
  44.             } else {
  45.                 Gdx.app.log(f.name, "has already been corrected earlier.")
  46.             }
  47.         }
  48.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement