-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add a sample.py script #2
Changes from 12 commits
28215a5
c3e5235
55b6d2b
953b5a6
1288bb6
df9301f
f7c86cd
e782ab0
56a9891
9fee103
ec6accf
cfd0e48
c70f467
b7dd16e
a726ce0
ef522ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
## uharfbuzz | ||
|
||
Streamlined Cython bindings for the [HarfBuzz][hb] shaping engine. | ||
|
||
|
||
### Example | ||
|
||
```python | ||
import uharfbuzz as hb | ||
import sys | ||
|
||
|
||
with open(sys.argv[1], 'rb') as fontfile: | ||
fontdata = fontfile.read() | ||
|
||
text = sys.argv[2] | ||
|
||
face = hb.Face.create(fontdata) | ||
font = hb.Font.create(face) | ||
upem = face.upem | ||
|
||
font.scale = (upem, upem) | ||
hb.ot_font_set_funcs(font) | ||
|
||
buf = hb.Buffer.create() | ||
|
||
buf.add_str(text) | ||
buf.guess_segment_properties() | ||
|
||
features = {"kern": True, "liga": True} | ||
hb.shape(font, buf, features) | ||
|
||
infos = buf.glyph_infos | ||
positions = buf.glyph_positions | ||
|
||
for info, pos in zip(infos, positions): | ||
gid = info.codepoint | ||
cluster = info.cluster | ||
x_advance = pos.x_advance | ||
x_offset = pos.x_offset | ||
y_offset = pos.y_offset | ||
print(f"gid{gid}={cluster}@{x_advance},{x_offset}+{y_offset}") | ||
``` | ||
|
||
|
||
[hb]: https://github.com/harfbuzz/harfbuzz |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,7 @@ cdef class GlyphPosition: | |
|
||
cdef class Buffer: | ||
cdef hb_buffer_t* _hb_buffer | ||
cdef object _message_func | ||
|
||
def __cinit__(self): | ||
self._hb_buffer = NULL | ||
|
@@ -111,7 +112,6 @@ cdef class Buffer: | |
positions.append(position) | ||
return positions | ||
|
||
|
||
@property | ||
def language(self) -> str: | ||
cdef const_char* cstr = hb_language_to_string( | ||
|
@@ -178,6 +178,23 @@ cdef class Buffer: | |
def guess_segment_properties(self) -> None: | ||
hb_buffer_guess_segment_properties(self._hb_buffer) | ||
|
||
def set_message_func(self, func: Callable[[str], bool]) -> None: | ||
# XXX I'm passing the python callback as user_data and recovering it | ||
# from the C-API func below. Is there a better way? | ||
# Also, the callback is only taking a message argument, no buffer, | ||
# nor font or its own user_data. Not sure whether/how to pass them on | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could probably look into this, but do we at all need implementing "message func" in the first place? The nice thing about the other stuff I put thus far is I use it so I know what things need to be passed to python or not.. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know if we need to implement it. I was just trying to port the simple debugger that is exemplified in the harfbuzz's original sample.py, but using uharfbuzz. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we can leave it out for now, I've never needed to use it myself There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @adrientetar I removed it from the example. Shall I remove the set_message_func method altogether from the pyx source file as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, and pxd too |
||
hb_buffer_set_message_func( | ||
self._hb_buffer, _buffer_message_func, <void*>func, NULL) | ||
self._message_func = func | ||
|
||
|
||
cdef hb_bool_t _buffer_message_func(hb_buffer_t* buffer, hb_font_t* font, | ||
const char* message, void* user_data): | ||
cdef object py_func = <object>user_data | ||
cdef str py_message = (<bytes>message).decode("utf-8") | ||
return bool(py_func(py_message)) | ||
|
||
|
||
cdef hb_user_data_key_t k | ||
|
||
|
||
|
@@ -209,13 +226,13 @@ cdef class Face: | |
if self._hb_face is not NULL: | ||
hb_face_destroy(self._hb_face) | ||
|
||
""" use bytes/bytearray, not Blob | ||
@classmethod | ||
def create(self, blob: Blob, index: int): | ||
def create(cls, bytes blob, int index=0): | ||
cdef Face inst = cls() | ||
inst._hb_face = hb_face_create(blob, index) | ||
cdef hb_blob_t* cblob = hb_blob_create( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: name it hb_blob |
||
blob, len(blob), HB_MEMORY_MODE_READONLY, NULL, NULL) | ||
inst._hb_face = hb_face_create(cblob, index) | ||
return inst | ||
""" | ||
|
||
@classmethod | ||
def create_for_tables(cls, | ||
|
@@ -451,3 +468,7 @@ def ot_layout_table_get_script_tags(face: Face, tag: str) -> List[str]: | |
packed = cstr | ||
tags.append(packed.decode()) | ||
return tags | ||
|
||
|
||
def void ot_font_set_funcs(Font font): | ||
hb_ot_font_set_funcs(font._hb_font) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can you sort the imports