GenerateNLVersion.kt 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import org.w3c.dom.Attr
  2. import org.w3c.dom.Document
  3. import org.w3c.dom.Node
  4. import org.w3c.dom.NodeList
  5. import java.io.File
  6. import java.io.FileOutputStream
  7. import java.util.concurrent.TimeUnit
  8. import javax.xml.parsers.DocumentBuilderFactory
  9. import javax.xml.transform.TransformerFactory
  10. import javax.xml.transform.dom.DOMSource
  11. import javax.xml.transform.stream.StreamResult
  12. import javax.xml.xpath.XPathConstants
  13. import javax.xml.xpath.XPathFactory
  14. /**
  15. * The scripts generates no ligature version of JetBrains Mono called JetBrains Mono NL
  16. *
  17. * ttx command is required to run this script
  18. *
  19. * @author Konstantin Bulenkov
  20. */
  21. @Suppress("RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
  22. fun main() {
  23. File("./fonts/ttf/")
  24. .listFiles { _, name -> name.endsWith(".ttf") && !name.startsWith("JetBrainsMonoNL") }
  25. .forEach {
  26. val ttx = it.nameWithoutExtension + ".ttx"
  27. val dir = it.parentFile
  28. File(dir, ttx).deleteAndLog()
  29. val doc = ttf2Document(it)
  30. File(dir, ttx).deleteAndLog()
  31. if (doc != null) {
  32. generateNoLigaturesFont(File(dir, it.name), doc)
  33. }
  34. }
  35. }
  36. fun ttf2Document(file: File): Document? {
  37. "ttx ${file.name}".runCommand(file.parentFile)
  38. val ttx = file.parentFile.listFiles { _, name -> name == "${file.nameWithoutExtension}.ttx" }?.first() ?: return null
  39. val documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder()
  40. return documentBuilder.parse(ttx)
  41. }
  42. fun generateNoLigaturesFont(file: File, doc: Document) {
  43. val nlName = file.nameWithoutExtension.replace("JetBrainsMono", "JetBrainsMonoNL")
  44. val ttx = "$nlName.ttx"
  45. val ttf = "$nlName.ttf"
  46. val dir = file.parentFile
  47. File(dir, ttf).deleteAndLog()
  48. doc.removeLigas("/ttFont/GlyphOrder", "GlyphID")
  49. doc.removeLigas("/ttFont/glyf", "TTGlyph")
  50. doc.removeLigas("/ttFont/hmtx", "mtx")
  51. doc.removeLigas("/ttFont/post/extraNames", "psName")
  52. doc.removeLigas("/ttFont/GDEF/GlyphClassDef", "ClassDef", attName = "glyph")
  53. doc.removeNode("/ttFont/GPOS")
  54. doc.removeNode("/ttFont/GSUB")
  55. val xPath = XPathFactory.newInstance().newXPath()
  56. val nameRecords = (xPath.evaluate("/ttFont/name/namerecord", doc, XPathConstants.NODESET) as NodeList).asList()
  57. nameRecords.forEach {
  58. if (!it.textContent.contains("trademark")) {
  59. it.textContent = it.textContent
  60. .replace("JetBrains Mono", "JetBrains Mono NL")
  61. .replace("JetBrainsMono", "JetBrainsMonoNL")
  62. }
  63. }
  64. val ttxFile = File(dir, ttx)
  65. doc.saveAs(ttxFile)
  66. "ttx $ttx".runCommand(dir)
  67. ttxFile.deleteAndLog()
  68. }
  69. class NodeListWrapper(val nodeList: NodeList) : AbstractList<Node>(), RandomAccess {
  70. override val size: Int
  71. get() = nodeList.length
  72. override fun get(index: Int): Node = nodeList.item(index)
  73. }
  74. ////////////////////// Utility functions and data classes //////////////////////
  75. fun NodeList.asList(): List<Node> = NodeListWrapper(this)
  76. fun String.runCommand(workingDir: File) {
  77. ProcessBuilder(*split(" ").toTypedArray())
  78. .directory(workingDir)
  79. .redirectOutput(ProcessBuilder.Redirect.INHERIT)
  80. .redirectError(ProcessBuilder.Redirect.INHERIT)
  81. .start()
  82. .waitFor(1, TimeUnit.MINUTES)
  83. }
  84. fun Document.saveAs(file: File) {
  85. val transformer = TransformerFactory.newInstance().newTransformer()
  86. transformer.transform(DOMSource(this), StreamResult(FileOutputStream(file)))
  87. }
  88. fun Document.removeLigas(parentPath: String, nodeName: String, attName:String = "name") {
  89. val xPath = XPathFactory.newInstance().newXPath()
  90. val parent = xPath.evaluate(parentPath, this, XPathConstants.NODE) as Node
  91. val nodeFilter = "$parentPath/$nodeName[substring(@$attName, string-length(@$attName)-4) = '.liga']"
  92. val nodes = (xPath.evaluate(nodeFilter, this, XPathConstants.NODESET) as NodeList).asList()
  93. nodes.forEach { parent.removeChild(it) }
  94. }
  95. fun Document.removeNode(path: String) {
  96. val xPath = XPathFactory.newInstance().newXPath()
  97. val parent = xPath.evaluate(path.substringBeforeLast("/"), this, XPathConstants.NODE)
  98. if (parent is Node) {
  99. val child = xPath.evaluate(path, this, XPathConstants.NODE)
  100. if (child is Node) {
  101. parent.removeChild(child)
  102. }
  103. }
  104. }
  105. fun File.deleteAndLog() {
  106. if (!exists()) return
  107. println("Deleting $absolutePath")
  108. val result = delete()
  109. println("[$result]".toUpperCase())
  110. if (!result) deleteOnExit()
  111. }