markFeature.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. from fontbuild.features import updateFeature
  2. aliases = [["uni0430", "a"], ["uni0435", "e"], ["uni0440", "p"], ["uni0441", "c"], ["uni0445", "x"], ["uni0455", "s"], ["uni0456", "i"], ["uni0471", "psi"]]
  3. def GetAliaseName(gname):
  4. for i in range (len(aliases)):
  5. if (gname == aliases[i][1]):
  6. return aliases[i][0]
  7. return None
  8. def CreateAccNameList(font, acc_anchor_name, bCombAccentOnly = True):
  9. #combrange = range(0x0300,0x0370) + range(0x1AB0,0x1ABF) + range(0x1DC0,0x1DE0)
  10. lst = []
  11. for g in font:
  12. if bCombAccentOnly and g.width != 0: #((g.unicode < 0x0300) or (g.unicode > 0x362)):
  13. continue
  14. for anchor in g.anchors:
  15. if acc_anchor_name == anchor.name:
  16. lst.append(g.name)
  17. return lst
  18. def CreateAccGlyphList(font, acc_list, acc_anchor_name):
  19. g_list = []
  20. for g in font:
  21. if g.name in acc_list:
  22. for anchor in g.anchors:
  23. if acc_anchor_name == anchor.name:
  24. g_list.append([g.name, anchor.x, anchor.y])
  25. break
  26. return g_list
  27. def CreateGlyphList(font, acc_list, anchor_name):
  28. g_list = []
  29. for g in font:
  30. if g.name in acc_list:
  31. continue
  32. for anchor in g.anchors:
  33. if anchor_name == anchor.name:
  34. g_list.append([g.name, anchor.x, anchor.y])
  35. break
  36. return g_list
  37. def Create_mark_lookup(accent_g_list, base_g_list, lookupname, acc_class, lookAliases = True):
  38. txt = "lookup " + lookupname + " {\n"
  39. for acc in accent_g_list:
  40. txt += " markClass " + acc[0] + " <anchor " + `acc[1]` + " " + `acc[2]` + "> " + acc_class +";\n"
  41. for base in base_g_list:
  42. txt += " pos base " + base[0] + " <anchor " + `base[1]` + " " + `base[2]` + "> mark " + acc_class + ";\n"
  43. if (lookAliases):
  44. base2 = GetAliaseName(base[0])
  45. if (None == base2):
  46. continue
  47. txt += " pos base " + base2 + " <anchor " + `base[1]` + " " + `base[2]` + "> mark " + acc_class + ";\n"
  48. txt += "} " + lookupname + ";\n"
  49. return txt
  50. ##### main ##############
  51. def GenerateFeature_mark(font):
  52. combination_anchor_list = [
  53. ["top", "_marktop", True, True],
  54. ["bottom", "_markbottom", True, True],
  55. ["top_dd", "_marktop_dd", True, False],
  56. ["bottom_dd", "_markbottom_dd", True, False],
  57. ["rhotichook", "_markrhotichook", False, False],
  58. ["top0315", "_marktop0315", False, False],
  59. ["parent_top", "_markparent_top", False, False],
  60. ["parenthesses.w1", "_markparenthesses.w1", False, False],
  61. ["parenthesses.w2", "_markparenthesses.w2", False, False],
  62. ["parenthesses.w3", "_markparenthesses.w3", False, False]
  63. ]
  64. text = "feature mark {\n"
  65. for n in range(len(combination_anchor_list)):
  66. accent_name_list = []
  67. accent_mark_list = []
  68. base_mark_list = []
  69. anchors_pair = combination_anchor_list[n]
  70. anchor_name = anchors_pair[0]
  71. acc_anchor_name = anchors_pair[1]
  72. comb_accent_only = anchors_pair[2]
  73. expand_to_composits = anchors_pair[3]
  74. lookupname = "mark"+`n+1`
  75. classname = "@MC_" + anchor_name
  76. accent_name_list = CreateAccNameList(font, acc_anchor_name, comb_accent_only)
  77. accent_mark_list = CreateAccGlyphList(font, accent_name_list, acc_anchor_name)
  78. base_mark_list = CreateGlyphList(font, accent_name_list, anchor_name)
  79. text += Create_mark_lookup(accent_mark_list, base_mark_list, lookupname, classname, expand_to_composits)
  80. text += "} mark;\n"
  81. updateFeature(font, "mark", text)