Skip to content

Commit

Permalink
Add basic Blob class
Browse files Browse the repository at this point in the history
  • Loading branch information
khaledhosny committed Aug 1, 2021
1 parent a8f5605 commit 5d0e66b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 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
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
26 changes: 26 additions & 0 deletions src/uharfbuzz/_harfbuzz.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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 @@ -276,6 +277,31 @@ 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 = NULL

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

def __dealloc__(self):
if self._hb_blob is not NULL:
hb_blob_destroy(self._hb_blob)
self._data = None


cdef hb_user_data_key_t k


Expand Down
9 changes: 9 additions & 0 deletions src/uharfbuzz/charfbuzz.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,17 @@ 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-buffer.h
ctypedef struct hb_buffer_t:
pass
Expand Down

0 comments on commit 5d0e66b

Please sign in to comment.