ChevalierJoseph commited on
Commit
faf982f
·
verified ·
1 Parent(s): ab1a091

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -31
app.py CHANGED
@@ -9,8 +9,8 @@ import zipfile
9
  import tempfile
10
  import os
11
  from fontTools.ttLib import TTFont
12
- from fontTools.pens.ttGlyphPen import TTGlyphPen
13
- from fontTools.ttLib.tables import ttProgram
14
 
15
  def load_model():
16
  tokenizer = AutoTokenizer.from_pretrained("ChevalierJoseph/typtop")
@@ -60,45 +60,59 @@ def generate_svg_files(glyphs, width=100, height=100):
60
 
61
  def create_otf_font(glyphs, output_path="font.otf"):
62
  font = TTFont()
63
- font['head'] = TTFont.newTable('head')
64
- font['hhea'] = TTFont.newTable('hhea')
65
- font['maxp'] = TTFont.newTable('maxp')
66
- font['name'] = TTFont.newTable('name')
67
- font['OS/2'] = TTFont.newTable('OS/2')
68
- font['post'] = TTFont.newTable('post')
69
- font['hmtx'] = TTFont.newTable('hmtx')
70
- font['cmap'] = TTFont.newTable('cmap')
71
-
72
- font['head'].fontRevision = 1.0
73
- font['hhea'].ascent = 800
74
- font['hhea'].descent = -200
75
- font['hhea'].lineGap = 0
76
- font['maxp'].numGlyphs = len(glyphs)
77
-
78
- cmap = font['cmap']
79
- cmap_format_4 = TTFont.newTable('cmap_format_4')
80
- cmap_format_4.platformID = 3 # Windows
81
- cmap_format_4.platEncID = 1 # Unicode BMP
82
- cmap_format_4.language = 0 # Any language
83
- cmap_format_4.cmap = {}
84
- cmap.tables = [cmap_format_4]
85
-
86
- glyf_table = font['glyf'] = TTFont.newTable('glyf')
 
 
 
 
 
 
 
 
87
 
88
  for lettre, path in glyphs:
89
  unicode_val = ord(lettre)
90
  glyph_name = f"glyph{unicode_val}"
91
 
 
92
  glyph = glyf_table.glyphClass()
93
  glyph.name = glyph_name
94
- glyph.numberOfContours = 0 # Placeholder logic here for drawing the glyph
95
 
96
- glyf_table.glyphs[glyph_name] = glyph
97
- cmap_format_4.cmap[unicode_val] = glyph_name
 
 
 
 
98
 
99
- # Set metrics
100
- font['hmtx'][glyph_name] = (1000, 0)
 
101
 
 
102
  font.save(output_path)
103
  return output_path
104
 
 
9
  import tempfile
10
  import os
11
  from fontTools.ttLib import TTFont
12
+ from fontTools.ttLib.tables import _h_e_a_d, _h_h_e_a, _m_a_x_p, _n_a_m_e, _O_S_2, _p_o_s_t
13
+ from fontTools.ttLib.tables._c_m_a_p import CmapSubtable
14
 
15
  def load_model():
16
  tokenizer = AutoTokenizer.from_pretrained("ChevalierJoseph/typtop")
 
60
 
61
  def create_otf_font(glyphs, output_path="font.otf"):
62
  font = TTFont()
63
+
64
+ # Create and add mandatory tables
65
+ head = _h_e_a_d.table__h_e_a_d()
66
+ hhea = _h_h_e_a.table__h_h_e_a()
67
+ maxp = _m_a_x_p.table__m_a_x_p()
68
+ name = _n_a_m_e.table__n_a_m_e()
69
+ os2 = _O_S_2.table__O_S_2()
70
+ post = _p_o_s_t.table__p_o_s_t()
71
+ hmtx = TTFont.newTable('hmtx')
72
+ cmap = TTFont.newTable('cmap')
73
+
74
+ font['head'] = head
75
+ font['hhea'] = hhea
76
+ font['maxp'] = maxp
77
+ font['name'] = name
78
+ font['OS/2'] = os2
79
+ font['post'] = post
80
+ font['hmtx'] = hmtx
81
+ font['cmap'] = cmap
82
+
83
+ # Set required fields
84
+ head.fontRevision = 1.0
85
+ hhea.ascent = 800
86
+ hhea.descent = -200
87
+ hhea.lineGap = 0
88
+ maxp.numGlyphs = len(glyphs)
89
+
90
+ # Create a cmap table
91
+ cmap_table = CmapSubtable.newSubtable(3, 1, 0) # Windows platform, Unicode BMP encoding
92
+
93
+ # Add glyphs to the font
94
+ glyf_table = TTFont.newTable('glyf')
95
 
96
  for lettre, path in glyphs:
97
  unicode_val = ord(lettre)
98
  glyph_name = f"glyph{unicode_val}"
99
 
100
+ # Create a simple glyph (this is a placeholder)
101
  glyph = glyf_table.glyphClass()
102
  glyph.name = glyph_name
 
103
 
104
+ glyf_table[glyph_name] = glyph
105
+
106
+ # Map unicode to glyph name
107
+ cmap_table.cmap[unicode_val] = glyph_name
108
+ # Set metrics for each glyph
109
+ hmtx[glyph_name] = (1000, 0) # (advanceWidth, leftSideBearing)
110
 
111
+ # Assign the cmap and glyf tables to the font
112
+ cmap.tables = [cmap_table]
113
+ font['glyf'] = glyf_table
114
 
115
+ # Save the font
116
  font.save(output_path)
117
  return output_path
118