Skip to content

Commit

Permalink
Use Blob to create Face
Browse files Browse the repository at this point in the history
Fixes #52
  • Loading branch information
khaledhosny committed Aug 1, 2021
1 parent 5d0e66b commit 12a40a3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ import sys
import uharfbuzz as hb


with open(sys.argv[1], 'rb') as fontfile:
fontdata = fontfile.read()

fontfile = sys.argv[1]
text = sys.argv[2]

face = hb.Face(fontdata)
blob = hb.Blob.from_file_path(fontfile)
face = hb.Face(blob)
font = hb.Font(face)

buf = hb.Buffer()
Expand Down
16 changes: 8 additions & 8 deletions src/uharfbuzz/_harfbuzz.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -325,22 +325,22 @@ cdef hb_blob_t* _reference_table_func(
cdef class Face:
cdef hb_face_t* _hb_face
cdef object _reference_table_func
cdef object _blob
cdef Blob _blob

def __cinit__(self, bytes blob, int index=0):
cdef hb_blob_t* hb_blob
def __cinit__(self, blob: Union[Blob, bytes], int index=0):
if blob is not None:
self._blob = blob
hb_blob = hb_blob_create(
blob, len(blob), HB_MEMORY_MODE_READONLY, NULL, NULL)
self._hb_face = hb_face_create(hb_blob, index)
hb_blob_destroy(hb_blob)
if not isinstance(blob, Blob):
self._blob = Blob(blob)
else:
self._blob = blob
self._hb_face = hb_face_create(self._blob._hb_blob, index)
else:
self._hb_face = NULL

def __dealloc__(self):
if self._hb_face is not NULL:
hb_face_destroy(self._hb_face)
self._blob = None

# DEPRECATED: use the normal constructor
@classmethod
Expand Down
6 changes: 4 additions & 2 deletions tests/test_uharfbuzz.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def blankfont():
{gid=8, name="u1F4A9", code=0x1F4A9}, # PILE OF POO
]
"""
face = hb.Face(ADOBE_BLANK_TTF_PATH.read_bytes())
blob = hb.Blob.from_file_path(ADOBE_BLANK_TTF_PATH)
face = hb.Face(blob)
font = hb.Font(face)
return font

Expand All @@ -39,7 +40,8 @@ def opensans():
{gid=1, name="A", code=0x41},
]
"""
face = hb.Face(OPEN_SANS_TTF_PATH.read_bytes())
blob = hb.Blob(OPEN_SANS_TTF_PATH.read_bytes())
face = hb.Face(blob)
font = hb.Font(face)
return font

Expand Down

0 comments on commit 12a40a3

Please sign in to comment.