def runHB(direction, script, language, features, text, fontname, positions): font = getHbFont(fontname) buf = HarfBuzz.buffer_create() text = toUnicode(text) HarfBuzz.buffer_add_utf8(buf, text.encode('utf-8'), 0, -1) HarfBuzz.buffer_set_direction(buf, HarfBuzz.direction_from_string(toBytes(direction))) HarfBuzz.buffer_set_script(buf, HarfBuzz.script_from_string(toBytes(script))) if language: HarfBuzz.buffer_set_language(buf, HarfBuzz.language_from_string(toBytes(language))) if features: features = [HarfBuzz.feature_from_string(toBytes(fea))[1] for fea in features.split(',')] else: features = [] HarfBuzz.shape(font, buf, features) info = HarfBuzz.buffer_get_glyph_infos(buf) ttfont = getTtFont(fontname) if positions: pos = HarfBuzz.buffer_get_glyph_positions(buf) glyphs = [] for i, p in zip(info, pos): glyph = ttfont.getGlyphName(i.codepoint) if p.x_offset or p.y_offset: glyph += "@%d,%d" % (p.x_offset, p.y_offset) glyph += "+%d" % p.x_advance if p.y_advance: glyph += ",%d" % p.y_advance glyphs.append(glyph) out = "|".join(glyphs) else: out = "|".join([ttfont.getGlyphName(i.codepoint) for i in info]) return "[%s]" % out
def ot_feature(bytename, value): b, O = hb.feature_from_string(bytename) if b: O.value = value return O else: raise ValueError('invalid opentype feature name ' + repr(str(bytename)))
def shape(text, font): text = toUnicode(text) buf = HarfBuzz.buffer_create() HarfBuzz.buffer_add_utf8(buf, text.encode('utf-8'), 0, -1) HarfBuzz.buffer_set_direction(buf, HarfBuzz.direction_t.RTL) HarfBuzz.buffer_set_script(buf, HarfBuzz.script_t.ARABIC) HarfBuzz.buffer_set_language(buf, HarfBuzz.language_from_string("ar")) HarfBuzz.shape(font, buf, [HarfBuzz.feature_from_string("+ss01")[1]]) glyphs = HarfBuzz.buffer_get_glyph_infos(buf) positions = HarfBuzz.buffer_get_glyph_positions(buf) return [(g.codepoint, p.x_advance, p.x_offset, p.y_offset) for g, p in zip(glyphs, positions)]