我有一个包含字体和元数据的文本文件
10 52
a 10
.oooo.
`P )88b
.oP"888
d8( 888
`Y888""8o
b 11
.o8
"888
888oooo.
d88' `88b
888 888
888 888
`Y8bod8P'
c 10
.ooooo.
d88' `"Y8
888
888 .o8
`Y8bod8P'
+++++++++++++++++++++++++++
X 15
ooooooo ooooo
`8888 d8'
Y888..8P
`8888'
.8PY888.
d8' `888b
o888o o88888o
Y 14
oooooo oooo
`888. .8'
`888. .8'
`888.8'
`888'
888
o888o
Z 14
oooooooooooo
d'""""""d888'
.888P
d888'
.888P
d888' .P
.8888888888P
字母是大写的,小写的。我想把它们从文件中取出来,把每封信都存储在一个列表中。因此,我可以在以后使用它…
我尝试使用过滤器…但我不能将结果分割成数组/列表使用chunked()
或widowed()
,因为字母是5或7或更多行。
val romanFont = File("roman.txt").readLines().filter { it.length > 5 }
文件有52个字母…任何想法?
看起来每个字母都有7行。这对于ascii字体也是有意义的。每个字体字母都以一行开头,一行以字母开头,后跟一个空格和一个数字。因此,我们可以通过一个简单的正则表达式(^[a-zA-Z] [0-9]{1,2}
)来识别这种模式。现在我们使用这些知识遍历所有行并收集Letter-List:
val font = """
a 10
.oooo.
`P )88b
.oP"888
d8( 888
`Y888""8o
b 11
.o8
"888
888oooo.
d88' `88b
888 888
888 888
`Y8bod8P'
""".trimIndent()
fun main() {
fun String.isLetterStart() = this.matches(Regex("^[a-zA-Z] [0-9]{1,2}"))
val letters = mutableListOf<Letter>()
var lineCount = 0
var char = ""
var asciFontLetter = ""
font.lines().forEach { line ->
if (lineCount > 0) {
if (lineCount == 8) {
lineCount = 0
letters += Letter(char, asciFontLetter)
asciFontLetter = "n"
} else {
lineCount++
asciFontLetter += line + "n"
}
}
if (line.isLetterStart() && lineCount == 0) {
char = line.substringBefore(" ")
lineCount++
}
}
println(letters)
}
data class Letter(val char: String, val bitmapFontLetter: String)
🚀这将打印出
[Letter(char=a, bitmapFontLetter=
.oooo.
`P )88b
.oP"888
d8( 888
`Y888""8o
), Letter(char=b, bitmapFontLetter=
.o8
"888
888oooo.
d88' `88b
888 888
888 888
`Y8bod8P'
)]