Skip to content

Commit

Permalink
Merge pull request #99 from harfbuzz/blob
Browse files Browse the repository at this point in the history
Add Blob class
  • Loading branch information
khaledhosny authored Aug 2, 2021
2 parents a8f5605 + 84d9429 commit cd91f94
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ MANIFEST
build
dist
_skbuild/
venv/

# Unit test / coverage files
.tox/*
Expand Down
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
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
libraries = []
if platform.system() != 'Windows':
extra_compile_args.append('-std=c++11')
define_macros.append(('HAVE_MMAP', '1'))
define_macros.append(('HAVE_UNISTD_H', '1'))
define_macros.append(('HAVE_SYS_MMAN_H', '1'))
else:
define_macros.append(('HAVE_DIRECTWRITE', '1'))
#define_macros.append(('HAVE_UNISCRIBE', '1'))
Expand Down
59 changes: 40 additions & 19 deletions src/uharfbuzz/_harfbuzz.pyx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#cython: language_level=3
import os
from enum import IntEnum
from .charfbuzz cimport *
from libc.stdlib cimport free, malloc
from libc.string cimport const_char
from collections import namedtuple
from typing import Callable, Dict, List, Sequence, Tuple, Union
from pathlib import Path


cdef extern from "Python.h":
Expand Down Expand Up @@ -102,8 +104,7 @@ cdef class Buffer:
self._message_callback = None

def __dealloc__(self):
if self._hb_buffer is not NULL:
hb_buffer_destroy(self._hb_buffer)
hb_buffer_destroy(self._hb_buffer)

# DEPRECATED: use the normal constructor
@classmethod
Expand Down Expand Up @@ -276,6 +277,30 @@ cdef class Buffer:
hb_buffer_set_message_func(self._hb_buffer, msgcallback, <void*>callback, NULL)


cdef class Blob:
cdef hb_blob_t* _hb_blob
cdef object _data

def __cinit__(self, bytes data):
if data is not None:
self._data = data
self._hb_blob = hb_blob_create(
data, len(data), HB_MEMORY_MODE_READONLY, NULL, NULL)
else:
self._hb_blob = hb_blob_get_empty()

@classmethod
def from_file_path(cls, filename: Union[str, Path]):
cdef bytes packed = os.fsencode(filename)
cdef Blob inst = cls(None)
inst._hb_blob = hb_blob_create_from_file(<char*>packed)
return inst

def __dealloc__(self):
hb_blob_destroy(self._hb_blob)
self._data = None


cdef hb_user_data_key_t k


Expand All @@ -299,22 +324,21 @@ 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
self._hb_face = hb_face_get_empty()

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

# DEPRECATED: use the normal constructor
@classmethod
Expand Down Expand Up @@ -363,8 +387,7 @@ cdef class Font:
self._face = face

def __dealloc__(self):
if self._hb_font is not NULL:
hb_font_destroy(self._hb_font)
hb_font_destroy(self._hb_font)
self._face = self._ffuncs = None

# DEPRECATED: use the normal constructor
Expand Down Expand Up @@ -561,8 +584,7 @@ cdef class FontFuncs:
self._hb_ffuncs = hb_font_funcs_create()

def __dealloc__(self):
if self._hb_ffuncs is not NULL:
hb_font_funcs_destroy(self._hb_ffuncs)
hb_font_funcs_destroy(self._hb_ffuncs)

# DEPRECATED: use the normal constructor
@classmethod
Expand Down Expand Up @@ -842,8 +864,7 @@ cdef class DrawFuncs:
self._user_data = None

def __dealloc__(self):
if self._hb_drawfuncs is not NULL:
hb_draw_funcs_destroy(self._hb_drawfuncs)
hb_draw_funcs_destroy(self._hb_drawfuncs)

def draw_glyph(self, font: Font, gid: int, user_data: object):
self._user_data = user_data
Expand Down
13 changes: 13 additions & 0 deletions src/uharfbuzz/charfbuzz.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,19 @@ cdef extern from "hb.h":
hb_memory_mode_t mode,
void* user_data, hb_destroy_func_t destroy)

hb_blob_t* hb_blob_create_from_file(
const char *file_name)

void hb_blob_destroy(hb_blob_t* blob)

const char* hb_blob_get_data(
hb_blob_t *blob, unsigned int *length)

unsigned int hb_blob_get_length(
hb_blob_t *blob)

hb_blob_t* hb_blob_get_empty()

# hb-buffer.h
ctypedef struct hb_buffer_t:
pass
Expand Down Expand Up @@ -164,6 +175,8 @@ cdef extern from "hb.h":
hb_bool_t replace)
void hb_face_destroy(hb_face_t* face)

hb_face_t* hb_face_get_empty()

# hb-font.h
ctypedef struct hb_font_funcs_t:
pass
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 cd91f94

Please sign in to comment.