-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFancyTextDecorator.kt
134 lines (118 loc) · 4.12 KB
/
FancyTextDecorator.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package signature
import utils.*
import java.io.File
import java.util.*
class FancyTextDecorator(
private val name: String,
private val title: String,
mediumFontDir: File,
romanFontDir: File,
) {
// Maps of letter to that letter's font
private val mediumFont = mutableMapOf<String, Font>()
private val romanFont = mutableMapOf<String, Font>()
init {
mediumFont.putAll(initializeFont(mediumFontDir, 4))
romanFont.putAll(initializeFont(romanFontDir, 11))
}
private var decoratedNameLength = calculateLength(name, 10, romanFont)
private var decoratedTitleLength = calculateLength(title, 5, mediumFont)
private var lineLength =
if (decoratedNameLength > decoratedTitleLength ) {
decoratedNameLength + (BORDER.length * 4) + (SPACE.length * 4)
} else {
decoratedTitleLength + (BORDER.length * 4) + (SPACE.length * 4)
}
private val midIndex = (lineLength - (if (name.length > title.length) decoratedTitleLength else decoratedNameLength)) / 2
private fun initializeFont(file: File, steps: Int): MutableMap<String, Font> {
val font = mutableMapOf<String, Font>()
val lines = file.readLines()
var letter: String
var width: Int
for (index in 1..lines.lastIndex step steps) {
lines[index].split(' ').apply {
letter = first()
width = last().toInt()
}
val values = buildList {
for (i in index + 1..<index + steps) {
add(lines[i])
}
}
font[letter] = Font(width, values)
}
return font
}
fun decorateText(): String {
return buildString {
appendLine(getDecorators())
for (i in 0..<10) {
appendLine(decorateNameRow(i))
}
for (i in 0..<3) {
appendLine(getTitleRow(i))
}
appendLine(getDecorators())
}
}
private fun decorateNameRow(index: Int): StringBuilder {
val row = StringBuilder()
row.append(BORDER.repeat(2))
val text = buildString {
name.forEach { c ->
val str = c.toString()
append(if (str == SPACE) SPACE.repeat(10) else romanFont[c.toString()]!!.structure[index])
}
}
if (name.length > title.length) {
row.append(TWO_SPACES)
row.append(text)
row.append(TWO_SPACES)
} else {
row.append(SPACE.repeat(lineLength - 4))
row.replace(midIndex, midIndex + decoratedNameLength, text)
}
row.append(BORDER.repeat(2))
return row
}
private fun getDecorators(): String {
return BORDER.repeat(lineLength)
}
private fun getTitleRow(index: Int): String {
val text = buildString {
title.forEach{ c ->
val str = c.toString()
append(if (str == SPACE) SPACE.repeat(5) else mediumFont[c.toString()]!!.structure[index])
}
}
return buildString {
append(BORDER.repeat(2))
if (name.length > title.length) {
append(SPACE.repeat(lineLength - 4))
replace(midIndex, midIndex + decoratedTitleLength, text)
} else {
append(TWO_SPACES + text + TWO_SPACES)
}
append(BORDER.repeat(2))
}
}
private fun calculateLength(vale: String, spaceAmount: Int, font: Map<String, Font>): Int {
var length = 0
vale.forEach {
length += when (it) {
' ' -> spaceAmount
else -> font[it.toString()]!!.length
}
}
return length
}
}
fun main() {
val sc = Scanner(System.`in`)
val fullName = getInput(sc, "Enter name and surname")
val title = getInput(sc, "Enter person's status").uppercase()
val mediumFont = File("src/font/medium.txt")
val romanFont = File("src/font/roman.txt")
println(FancyTextDecorator(fullName, title, mediumFont, romanFont).decorateText())
sc.close()
}