Example #1
0
 def get(self, fontname):
   self.response.headers['Access-Control-Allow-Origin'] = '*'
   bandwidth = self.request.headers.get('X-TachyFont-bandwidth', '0')
   self.response.headers['Content-Type'] = 'text/plain'
   self.response.headers['Content-Type'] = 'text/richtext'
   basename = fontname.split('/')[0]
   zip_path = fontname_to_zipfile(basename)
   zf = zipfile.ZipFile(zip_path, 'r')
   # base = zf.open('base', 'r')
   # display_file(base)
   base = zf.open('base', 'r')
   bandwidth_limited_write(base, self.response.out, bandwidth, True)
Example #2
0
 def get(self, fontname):
   self.response.headers['Access-Control-Allow-Origin'] = '*'
   bandwidth = self.request.headers.get('X-TachyFont-bandwidth', '0')
   self.response.headers['Content-Type'] = 'text/plain'
   self.response.headers['Content-Type'] = 'text/richtext'
   parts = fontname.split('/')
   family = parts[0]
   weight = parts[1]
   zip_path = fontname_to_zipfile(family, weight)
   zf = zipfile.ZipFile(zip_path, 'r')
   # base = zf.open('base', 'r')
   # display_file(base)
   base = zf.open('base', 'r')
   bandwidth_limited_write(base, self.response.out, bandwidth, True)
Example #3
0
def prepare_bundle(request, major, minor):
    """Parse requests, then prepares response bundle for glyphs.

  Args:
    request: object, the request object.
  Returns:
    string: the glyph and metadata
  """
    glyph_request = _parse_json(request.body)
    family = glyph_request['name']
    weight = glyph_request['weight']
    zip_path = fontname_to_zipfile(family, weight)
    codepoints = glyph_request['arr']
    elapsed_time('prepare_bundle for {0} characters'.format(len(codepoints)),
                 True)
    zf = zipfile.ZipFile(zip_path, 'r')
    cp_file = zf.open('codepoints', 'r')
    gid_file = zf.open('gids', 'r')

    cmap = _build_cmap(cp_file, gid_file)
    # Make these seekable.
    cidx_file = StringIO(zf.open('closure_idx', 'r').read())
    cdata_file = StringIO(zf.open('closure_data', 'r').read())
    closure_reader = ClosureReader(cidx_file, cdata_file)
    gids = set()
    for code in codepoints:
        if code in cmap:
            gids.update(closure_reader.read(cmap[code]))


#   for _code in codepoints:
#     if _code in cmap:
#       _gid = cmap[_code]
#       _closure_gids = closure_reader.read(cmap[_code])
#       print "  0x%05x (%d): %s" % (_code, _code, tuple(_closure_gids))
#     else:
#       print "code 0x%05x not in the font" % (_code)

    closure_reader.close()

    fingerprint_str = zf.open('sha1_fingerprint').read()
    if len(fingerprint_str) != 40:
        raise
    fingerprint = a2b_hex(fingerprint_str)

    elapsed_time('gather glyph info')

    glyph_info_file = zf.open('glyph_table', 'r')
    glyph_info = bytearray(glyph_info_file.read())  # Glyph meta data.
    elapsed_time('read glyph table ({0} bytes)'.format(len(glyph_info)))

    data = zf.open('glyph_data', 'r')
    data_bytes = bytearray(data.read())
    elapsed_time('read glyph data ({0} bytes)'.format(len(data_bytes)))

    (glyf_table, has_hmtx, has_vmtx, has_cff, header_size,
     entry_size) = (_parse_glyf_table(glyph_info))
    mtx_count = has_hmtx + (has_vmtx >> 1)
    # Assemble the flag bits.
    flag_mtx = has_hmtx | has_vmtx | has_cff
    elapsed_time('open & parse glyph table')

    bundle_header = 'BSAC'
    bundle_header += struct.pack('>BBBB', major, minor, 0, 0)
    bundle_header += fingerprint
    bundle_header += struct.pack('>HH', len(gids), flag_mtx)
    bundle_length = len(bundle_header)
    bundle_length += len(gids) * entry_size

    # Pre-flight to get the length
    for gid in gids:
        assert gid < len(glyf_table)
        bundle_length += glyf_table[gid][mtx_count +
                                         2]  # + 2 to get past glyph gid
    bundle_bytes = bytearray(bundle_length)
    bundle_pos = 0
    length = len(bundle_header)
    bundle_bytes[bundle_pos:bundle_pos + length] = bundle_header
    bundle_pos += length
    elapsed_time('calc bundle length')
    if has_cff:
        delta = -1  # What does -1 mean?
    else:
        delta = 0
    # Copy in the data from glyph_info and data_bytes
    for gid in sorted(gids):
        entry_offset = header_size + gid * entry_size
        bundle_bytes[bundle_pos:bundle_pos +
                     entry_size] = (glyph_info[entry_offset:entry_offset +
                                               entry_size])
        bundle_pos += entry_size

        data_offset = glyf_table[gid][mtx_count + 1] + delta
        data_size = glyf_table[gid][mtx_count + 2]
        bundle_bytes[bundle_pos:bundle_pos +
                     data_size] = (data_bytes[data_offset:data_offset +
                                              data_size])
        bundle_pos += data_size
    elapsed_time('build bundle')

    zf.close()
    #print "bundle bytes"
    #display_bytes(bundle_bytes)
    elapsed_time('close files')
    return str(bundle_bytes)
def prepare_bundle(request, major, minor):
  """Parse requests, then prepares response bundle for glyphs.

  Args:
    request: object, the request object.
  Returns:
    string: the glyph and metadata
  """
  glyph_request = _parse_json(request.body)
  family = glyph_request['name']
  weight = glyph_request['weight']
  zip_path = fontname_to_zipfile(family, weight)
  codepoints = glyph_request['arr']
  elapsed_time('prepare_bundle for {0} characters'.format(len(codepoints)),
               True)
  zf = zipfile.ZipFile(zip_path, 'r')
  cp_file = zf.open('codepoints', 'r')
  gid_file = zf.open('gids', 'r')

  cmap = _build_cmap(cp_file, gid_file)
  # Make these seekable.
  cidx_file = StringIO(zf.open('closure_idx', 'r').read())
  cdata_file = StringIO(zf.open('closure_data', 'r').read())
  closure_reader = ClosureReader(cidx_file, cdata_file)
  gids = set()
  for code in codepoints:
    if code in cmap:
      gids.update(closure_reader.read(cmap[code]))

#   for _code in codepoints:
#     if _code in cmap:
#       _gid = cmap[_code]
#       _closure_gids = closure_reader.read(cmap[_code])
#       print "  0x%05x (%d): %s" % (_code, _code, tuple(_closure_gids))
#     else:
#       print "code 0x%05x not in the font" % (_code)

  closure_reader.close()

  fingerprint_str = zf.open('sha1_fingerprint').read()
  if len(fingerprint_str) != 40:
    raise
  fingerprint = a2b_hex(fingerprint_str)

  elapsed_time('gather glyph info')

  glyph_info_file = zf.open('glyph_table', 'r')
  glyph_info = bytearray(glyph_info_file.read())  # Glyph meta data.
  elapsed_time('read glyph table ({0} bytes)'.format(len(glyph_info)))

  data = zf.open('glyph_data', 'r')
  data_bytes = bytearray(data.read())
  elapsed_time('read glyph data ({0} bytes)'.format(len(data_bytes)))

  (glyf_table, has_hmtx, has_vmtx, has_cff, header_size, entry_size) = (
      _parse_glyf_table(glyph_info))
  mtx_count = has_hmtx + (has_vmtx >> 1)
  # Assemble the flag bits.
  flag_mtx = has_hmtx | has_vmtx  | has_cff
  elapsed_time('open & parse glyph table')

  bundle_header = 'BSAC'
  bundle_header += struct.pack('>BBBB', major, minor, 0, 0)
  bundle_header += fingerprint
  bundle_header += struct.pack('>HH', len(gids), flag_mtx)
  bundle_length = len(bundle_header)
  bundle_length += len(gids) * entry_size

  # Pre-flight to get the length
  for gid in gids:
    assert gid < len(glyf_table)
    bundle_length += glyf_table[gid][mtx_count + 2]  # + 2 to get past glyph gid
  bundle_bytes = bytearray(bundle_length)
  bundle_pos = 0
  length = len(bundle_header)
  bundle_bytes[bundle_pos:bundle_pos+length] = bundle_header
  bundle_pos += length
  elapsed_time('calc bundle length')
  if has_cff:
    delta = -1  # What does -1 mean?
  else:
    delta = 0
  # Copy in the data from glyph_info and data_bytes
  for gid in sorted(gids):
    entry_offset = header_size + gid * entry_size
    bundle_bytes[bundle_pos:bundle_pos + entry_size] = (
        glyph_info[entry_offset:entry_offset + entry_size])
    bundle_pos += entry_size

    data_offset = glyf_table[gid][mtx_count + 1] + delta
    data_size = glyf_table[gid][mtx_count + 2]
    bundle_bytes[bundle_pos:bundle_pos + data_size] = (
        data_bytes[data_offset:data_offset + data_size])
    bundle_pos += data_size
  elapsed_time('build bundle')

  zf.close()
  #print "bundle bytes"
  #display_bytes(bundle_bytes)
  elapsed_time('close files')
  return str(bundle_bytes)