Пример #1
0
def glyph_metrics(string: str) -> Iterable[Metrics]:
    font, upem, buffer = setup_harfbuzz()
    HarfBuzz.buffer_clear_contents(buffer)

    if False:
        HarfBuzz.buffer_add_utf8(buffer, string.encode("utf-8"), 0, -1)
    elif sys.maxunicode == 0x10FFFF:
        HarfBuzz.buffer_add_utf32(
            buffer,
            array.array("I", string.encode("utf-32"))[1:], 0, -1)
    else:
        HarfBuzz.buffer_add_utf16(
            buffer,
            array.array("H", string.encode("utf-16"))[1:], 0, -1)

    # If this doesn't get run, the Python interpreter crashes
    HarfBuzz.buffer_guess_segment_properties(buffer)

    HarfBuzz.shape(font, buffer, [])
    codepoints = [
        info.codepoint for info in HarfBuzz.buffer_get_glyph_infos(buffer)
    ]
    positions = HarfBuzz.buffer_get_glyph_positions(buffer)

    for extents, pos in zip(glyphs_extents(font, codepoints), positions):
        yield Metrics(
            Positions(pos.x_advance, pos.y_advance, pos.x_offset,
                      pos.y_offset),
            extents,
            upem,
        )
Пример #2
0
def runHB(text, buf, font, ttfont):
    HarfBuzz.buffer_clear_contents(buf)
    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(b"ar"))

    HarfBuzz.shape(font, buf, [])

    info = HarfBuzz.buffer_get_glyph_infos(buf)
    out = "|".join([ttfont.getGlyphName(i.codepoint) for i in info])

    return "[%s]" % out
Пример #3
0
def runHB(text, buf, font, ttfont):
    HarfBuzz.buffer_clear_contents(buf)
    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(b"ar"))

    HarfBuzz.shape(font, buf, [])

    info = HarfBuzz.buffer_get_glyph_infos(buf)
    positions = HarfBuzz.buffer_get_glyph_positions(buf)
    out = []
    for i, p in zip(info, positions):
        text = ""
        text += ttfont.getGlyphName(i.codepoint)
        text += " w=%d" % p.x_advance
        if p.x_offset:
            text += " x=%d" % p.x_offset
        if p.y_offset:
            text += " y=%d" % p.y_offset
        out.append(text)

    return "[%s]" % "|".join(out)