Skip to content
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

Merged
merged 16 commits into from
Apr 19, 2018
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions README.md
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
6 changes: 0 additions & 6 deletions README.rst

This file was deleted.

10 changes: 10 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from skbuild import setup
import os
from io import open
Copy link
Member

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



here = os.path.abspath(os.path.dirname(__file__))

# Get the long description from the README file
with open(os.path.join(here, 'README.md'), encoding='utf-8') as f:
long_description = f.read()

setup(
name="uharfbuzz",
version="0.1.0.dev0",
description="Streamlined Cython bindings for the harfbuzz shaping engine",
long_description=long_description,
long_description_content_type='text/markdown',
author="Adrien Tétar",
author_email="adri-from-59@hotmail.fr",
url="https://github.com/trufont/uharfbuzz",
Expand Down
31 changes: 26 additions & 5 deletions src/uharfbuzz/_harfbuzz.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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..

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Copy link
Member Author

@anthrotype anthrotype Apr 19, 2018

Choose a reason for hiding this comment

The 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?

Copy link
Member

Choose a reason for hiding this comment

The 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


Expand Down Expand Up @@ -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(
Copy link
Member

Choose a reason for hiding this comment

The 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,
Expand Down Expand Up @@ -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)
9 changes: 9 additions & 0 deletions src/uharfbuzz/charfbuzz.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ cdef extern from "hb.h":
hb_language_t hb_buffer_get_language(hb_buffer_t* buffer)
void hb_buffer_set_language(hb_buffer_t* buffer, hb_language_t language)
void hb_buffer_destroy(hb_buffer_t* buffer)
ctypedef hb_bool_t(*hb_buffer_message_func_t) (
hb_buffer_t* buffer, hb_font_t* font, const char* message,
void* user_data)
void hb_buffer_set_message_func(
hb_buffer_t* buffer, hb_buffer_message_func_t func,
void* user_data, hb_destroy_func_t destroy)

# hb-face.h
ctypedef struct hb_face_t:
Expand Down Expand Up @@ -197,3 +203,6 @@ cdef extern from "hb-ot.h":
unsigned int start_offset,
unsigned int* script_count, # in/out
hb_tag_t* script_tags) # out

# hb-ot-font.h
void hb_ot_font_set_funcs(hb_font_t* font)