tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

sample.py (1832B)


      1 #!/usr/bin/env python3
      2 
      3 import sys
      4 import array
      5 import gi
      6 gi.require_version('HarfBuzz', '0.0')
      7 from gi.repository import HarfBuzz as hb
      8 from gi.repository import GLib
      9 
     10 fontdata = open (sys.argv[1], 'rb').read ()
     11 text = sys.argv[2]
     12 # Need to create GLib.Bytes explicitly until this bug is fixed:
     13 # https://bugzilla.gnome.org/show_bug.cgi?id=729541
     14 blob = hb.glib_blob_create (GLib.Bytes.new (fontdata))
     15 face = hb.face_create (blob, 0)
     16 del blob
     17 font = hb.font_create (face)
     18 upem = hb.face_get_upem (face)
     19 del face
     20 hb.font_set_scale (font, upem, upem)
     21 #hb.ft_font_set_funcs (font)
     22 hb.ot_font_set_funcs (font)
     23 
     24 buf = hb.buffer_create ()
     25 class Debugger (object):
     26 def message (self, buf, font, msg, data, _x_what_is_this):
     27 	print (msg)
     28 	return True
     29 debugger = Debugger ()
     30 hb.buffer_set_message_func (buf, debugger.message, 1, 0)
     31 
     32 ##
     33 ## Add text to buffer
     34 ##
     35 #
     36 # See https://github.com/harfbuzz/harfbuzz/pull/271
     37 #
     38 # If you do not care about cluster values reflecting Python
     39 # string indices, then this is quickest way to add text to
     40 # buffer:
     41 # hb.buffer_add_utf8 (buf, text.encode('utf-8'), 0, -1)
     42 # Otherwise, then following handles both narrow and wide
     43 # Python builds (the first item in the array is BOM, so we skip it):
     44 if sys.maxunicode == 0x10FFFF:
     45 hb.buffer_add_utf32 (buf, array.array ('I', text.encode ('utf-32'))[1:], 0, -1)
     46 else:
     47 hb.buffer_add_utf16 (buf, array.array ('H', text.encode ('utf-16'))[1:], 0, -1)
     48 
     49 
     50 hb.buffer_guess_segment_properties (buf)
     51 
     52 hb.shape (font, buf, [])
     53 del font
     54 
     55 infos = hb.buffer_get_glyph_infos (buf)
     56 positions = hb.buffer_get_glyph_positions (buf)
     57 
     58 for info, pos in zip (infos, positions):
     59 gid = info.codepoint
     60 cluster = info.cluster
     61 x_advance = pos.x_advance
     62 x_offset = pos.x_offset
     63 y_offset = pos.y_offset
     64 
     65 print ("gid%d=%d@%d,%d+%d" % (gid, cluster, x_advance, x_offset, y_offset))