MaterialIconFont.kt 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. @file:Suppress("unused")
  2. package eu.mjdev.desktop.components.fonticon
  3. import androidx.compose.ui.text.font.FontFamily
  4. import me.xdrop.fuzzywuzzy.FuzzySearch
  5. class MaterialIconFont(
  6. private val ttfFileName: String,
  7. private val codepointsFileName: String,
  8. private val fontFile: FontFile = FontFile(ttfFileName),
  9. private val codePointsFile: CodePointsFile = CodePointsFile(codepointsFileName)
  10. ) {
  11. val fontFamily: FontFamily
  12. get() = fontFile.fontFamily
  13. fun iconForName(
  14. name: String?,
  15. ): Int? = runCatching {
  16. if (name != null) {
  17. codePointsFile.icons.map { icon ->
  18. Pair(FuzzySearch.ratio(name, icon.key), icon)
  19. }.maxByOrNull { it.first }?.second?.value
  20. } else null
  21. }.getOrNull()
  22. }
  23. val MaterialSymbolsOutlined
  24. get() = MaterialIconFont(
  25. "icons/MaterialSymbolsOutlined.ttf",
  26. "icons/MaterialSymbolsOutlined.codepoints"
  27. )
  28. val MaterialSymbolsRounded
  29. get() = MaterialIconFont(
  30. "icons/MaterialSymbolsRounded.ttf",
  31. "icons/MaterialSymbolsRounded.codepoints"
  32. )
  33. val MaterialSymbolsSharp
  34. get() = MaterialIconFont(
  35. "icons/MaterialSymbolsSharp.ttf",
  36. "icons/MaterialSymbolsSharp.codepoints"
  37. )