Commits

saar drimer  committed 6d5adf0

glyphs are now cached for faster processing

  • Participants
  • Parent commits 9fe2452

Comments (0)

Files changed (1)

File utils/glyphs.py

 from Point import Point
 
 
-def get_glyphs(cfg, text, font, location=Point(), scale=1, line_number=1, rotate=0):
+def get_glyphs(cfg, text, font_name, location=Point(), scale=1, line_number=1, rotate=0):
     """
     Returns an array of paths that correspond to the 'text' string
     and initial 'location'
     """
 
-    # TODO: this isn't very efficient; better to send this function a whole block
-
     # open font SVG
     try: 
-        font = et.ElementTree(file=os.path.join(cfg['base_dir'], cfg['pcbmode']['locations']['fonts'], font + '.svg')) 
+        font_data = et.ElementTree(file=os.path.join(cfg['base_dir'], cfg['pcbmode']['locations']['fonts'], font_name + '.svg')) 
     except IOError as e:
         print "I/O error({0}): {1}".format(e.errno, e.strerror)
 
+    # check and create data structure for storing processed glyphs
+    if cfg.get('fonts') is None:
+        cfg['fonts'] = {}
+    if cfg['fonts'].get(font_name) is None:
+        cfg['fonts'][font_name] = {}
+
     # get dimensions of font
     # TODO: check how this is spec'd variable-width fonts
-    font_horiz_adv_x = float(font.find("//n:font", namespaces={'n': cfg['namespace']['svg']}).get('horiz-adv-x'))
-    glyph_height = float(font.find("//n:font-face", namespaces={'n': cfg['namespace']['svg']}).get('units-per-em'))
-    glyph_ascent = float(font.find("//n:font-face", namespaces={'n': cfg['namespace']['svg']}).get('ascent'))
-    glyph_decent = float(font.find("//n:font-face", namespaces={'n': cfg['namespace']['svg']}).get('descent'))
+    font_horiz_adv_x = float(font_data.find("//n:font", namespaces={'n': cfg['namespace']['svg']}).get('horiz-adv-x'))
+    glyph_height = float(font_data.find("//n:font-face", namespaces={'n': cfg['namespace']['svg']}).get('units-per-em'))
+    glyph_ascent = float(font_data.find("//n:font-face", namespaces={'n': cfg['namespace']['svg']}).get('ascent'))
+    glyph_decent = float(font_data.find("//n:font-face", namespaces={'n': cfg['namespace']['svg']}).get('descent'))
 
     text_height = glyph_height*scale
     text_width = 0
 
     offset_x = 0
 
+    # split text into charcters (also find unicade chars)
     text = re.findall(r'(\&#x[0-9abcdef]*;|.)', text)
 
-    h = HTMLParser.HTMLParser()
+    # instantiate HTML parser
+    htmlpar = HTMLParser.HTMLParser()
 
     for i, symbol in enumerate(text[:]):
 
-        symbol = h.unescape(symbol)
-    
-        # get the glyph definition
-        glyph = font.find(u'//n:glyph[@unicode="%s"]' % symbol, namespaces={'n': cfg['namespace']['svg']})
+        symbol = htmlpar.unescape(symbol)
+
+        # get the glyph definition from the file
+        glyph = font_data.find(u'//n:glyph[@unicode="%s"]' % symbol, namespaces={'n': cfg['namespace']['svg']})
+
         
         if glyph == None:
             print "ERROR: glyph not found for symbol %s" % symbol 
             # spaces won't have paths, so skip the following
             if symbol != ' ':
 
-                # get the path of the glyph
-                glyph_path = glyph.get('d')
+                # check if this glyph has already been processed
+                glyph_path = cfg['fonts'][font_name].get(symbol)
+
+                if glyph_path is None:
+                    # get the path of the glyph
+                    glyph_path = glyph.get('d')
+                    # check to see if there's a gerber 'level polarity' ('LP') directive
+                    glyph_path = svg.mirror_path_over_axis(glyph_path, 'x', glyph_width)
+                    # convert to a relative path 
+                    glyph_path = svg.absolute_to_relative_path(glyph_path)
+                    cfg['fonts'][font_name][symbol] = glyph_path
+
 
-                # check to see if there's a gerber 'level polarity' ('LP') directive
                 gerber_lp = glyph.get('gerber_lp') or ''
-                 
-                # mirror the path over x-axis
-                # TODO: figure out whether this is needed for all fonts
-                fd = svg.mirror_path_over_axis(glyph_path, 'x', glyph_width)
-                 
-                # convert to a relative path 
-                fd_r = svg.absolute_to_relative_path(fd)
-                 
+
                 # rotate and scale path
-                fd_width, fd_height, fd_r_norm = svg.transform_path(fd_r, False, scale, rotate)
+                fd_w, fd_h, glyph_path = svg.transform_path(glyph_path, False, scale, rotate)
                 
                 line_extra = glyph_height*scale*(line_number - 1)
 
-                paths.append({"d":fd_r_norm, 
-                              "symbol":symbol, 
-                              "location":[place.x, place.y - line_extra], 
-                              "gerber_lp":gerber_lp})
+                paths.append({"d": glyph_path, 
+                              "symbol": symbol, 
+                              "location": [place.x, place.y - line_extra], 
+                              "gerber_lp": gerber_lp})