Skip to content

Commit

Permalink
Add ot_tag_to_script/language functions
Browse files Browse the repository at this point in the history
We have buffer setters that take OT tags and call hb_ot_tag_to_*
functions, but not standalone bindings of these functions.
  • Loading branch information
khaledhosny committed Mar 15, 2024
1 parent e5ca0ab commit 4945419
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/uharfbuzz/_harfbuzz.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,28 @@ def shape(font: Font, buffer: Buffer,
free(hb_features)


def ot_tag_to_script(tag: str) -> str:
cdef bytes packed = tag.encode()
cdef hb_tag_t hb_tag = hb_tag_from_string(<char*>packed, -1)
cdef hb_script_t hb_script = hb_ot_tag_to_script(hb_tag)
cdef char cstr[5]
hb_tag_to_string(hb_script, cstr)
cstr[4] = b'\0'
packed = cstr
return packed.decode()


def ot_tag_to_language(tag: str) -> str:
cdef bytes packed = tag.encode()
cdef hb_tag_t hb_tag = hb_tag_from_string(<char*>packed, -1)
cdef hb_language_t hb_language = hb_ot_tag_to_language(hb_tag)
cdef const_char* cstr = hb_language_to_string(hb_language)
if cstr is NULL:
return None
packed = cstr
return packed.decode()


def ot_layout_lookup_get_glyph_alternates(
face: Face, lookup_index : int, glyph : hb_codepoint_t) -> List[int]:
cdef list alternates = []
Expand Down
6 changes: 6 additions & 0 deletions tests/test_uharfbuzz.py
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,12 @@ def test_ot_layout_has_substitution(self, opensans):
def test_ot_layout_has_no_substitution(self, mathfont):
assert hb.ot_layout_has_substitution(mathfont.face) == False

def test_ot_tag_to_script(self):
assert hb.ot_tag_to_script("mym2") == "Mymr"

def test_ot_tag_to_language(self):
assert hb.ot_tag_to_language("BGR") == "bg"


class TestOTColor:
def test_ot_color_has_palettes(self, colorv0font):
Expand Down

0 comments on commit 4945419

Please sign in to comment.