@@ -144,10 +144,7 @@ private class Font(val fontFile: File) {
144144 " $postScriptName (weight=$weight , italic=$italic )"
145145}
146146
147- private sealed class FontMatch {
148- object NoFont : FontMatch()
149- class HasFont (val font : Font ) : FontMatch()
150- }
147+ private data class FontMatch (val font : Font ? , val exactMatch : Boolean )
151148
152149private class FontCollection (fontFiles : List <File >) {
153150 val fonts = fontFiles.mapNotNull { file ->
@@ -171,27 +168,25 @@ private class FontCollection(fontFiles: List<File>) {
171168
172169 private fun matchFont (state : State ) =
173170 when (val exactMatch = byFullName[state.font]) {
174- is Font -> exactMatch
171+ is Font -> FontMatch ( exactMatch, true )
175172 else -> when (val family = byFamilyName[state.font]) {
176- null -> null
177- else -> family.minBy { similarity(state, it) }
173+ null -> FontMatch (null , false )
174+ else -> family.minBy { similarity(state, it) }?.let {
175+ FontMatch (it, false )
176+ } ? : FontMatch (null , false )
178177 }
179178 }
180179
181- fun match (state : State ): Font ? {
180+ fun match (state : State ): FontMatch {
182181 val s = state.copy(font = state.font.toLowerCase(), drawing = false )
183182
184183 return when (val f = cache[s]) {
185- is FontMatch .HasFont -> f.font
186- FontMatch .NoFont -> null
187184 null -> {
188185 val font = matchFont(s)
189- cache[s] = when (font) {
190- null -> FontMatch .NoFont
191- else -> FontMatch .HasFont (font)
192- }
186+ cache[s] = font
193187 font
194188 }
189+ else -> f
195190 }
196191 }
197192}
@@ -328,7 +323,8 @@ fun verifyFonts(assFile: ASSFile, fontFiles: List<File>): FontReport {
328323 }
329324
330325 parseLine(line.text, lineStyle, styles).forEach { (state, text) ->
331- when (val font = fonts.match(state)) {
326+ val (font, exactMatch) = fonts.match(state)
327+ when (font) {
332328 null -> report.missingFont(lineNum, state.font)
333329 else -> {
334330 if (state.italic && ! font.italic) {
@@ -339,11 +335,11 @@ fun verifyFonts(assFile: ASSFile, fontFiles: List<File>): FontReport {
339335 report.fauxBold(lineNum, state.weight, font.weight, state.font)
340336 }
341337
342- if (! state.italic && font.italic) {
338+ if (! state.italic && font.italic && ! exactMatch ) {
343339 report.italicMismatch(lineNum, state.font)
344340 }
345341
346- if (state.weight <= font.weight - 150 ) {
342+ if (state.weight <= font.weight - 150 && ! exactMatch ) {
347343 report.weightMismatch(lineNum, state.weight, font.weight, state.font)
348344 }
349345
0 commit comments