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