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

add a sample.py script #2

merged 16 commits into from
Apr 19, 2018

Conversation

anthrotype
Copy link
Member

I was just playing with the api and tried to rewrite the sample script from harfbuzz upstream repo using uharfbuzz:
https://github.com/harfbuzz/harfbuzz/blob/master/src/sample.py

just scrap it if you think we don't need it here

the python callback for the buffer message func only takes a str
(message) for now... Not sure how or whether I should pass buffer and
font (or even user_data) on to it.
uuzz/_harfbuzz.pyx
@classmethod
def create(self, blob: Blob, index: int):
def create(cls, fontdata: bytes, index: int):
Copy link
Member Author

@anthrotype anthrotype Mar 24, 2018

Choose a reason for hiding this comment

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

since it's a python function, here we could probably accept not only just bytes, but a union of bytes/bytearray/array.array or anything that exposes the buffer interface really.

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 meant not only

Copy link
Member

Choose a reason for hiding this comment

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

so you're looking for memoryview? also can you keep the name blob (and below hb_blob), thus far I keep the same signature names as harfbuzz. (and bytes is a binary "blob" so.. we can revisit later if needed)

@anthrotype
Copy link
Member Author

yo @adrientetar

Copy link
Member

@adrientetar adrientetar left a comment

Choose a reason for hiding this comment

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

Thanks, I thought of putting an example such as this. Good to have!

@@ -0,0 +1,45 @@
#!/usr/bin/python3

from __future__ import print_function
Copy link
Member

Choose a reason for hiding this comment

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

we don't need this?

import sys


with open(sys.argv[1], 'rb') as fp:
Copy link
Member

Choose a reason for hiding this comment

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

nit: fp → fontfile?

face = hb.Face.create(fontdata, 0)
font = hb.Font.create(face)
upem = face.upem
del face
Copy link
Member

Choose a reason for hiding this comment

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

nit: font keeps a reference to face, so "del face" isn't doing much here. Is it here to somehow highlight that face isn't used after that point?

Copy link
Member Author

Choose a reason for hiding this comment

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

not really, was just a blind copy from harfbuzz own example script

x_advance = pos.x_advance
x_offset = pos.x_offset
y_offset = pos.y_offset
print("gid%d=%d@%d,%d+%d" % (gid, cluster, x_advance, x_offset, y_offset))
Copy link
Member

Choose a reason for hiding this comment

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

nit: could use an f-string ;)

# 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

@classmethod
def create(self, blob: Blob, index: int):
def create(cls, fontdata: bytes, index: int):
Copy link
Member

Choose a reason for hiding this comment

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

so you're looking for memoryview? also can you keep the name blob (and below hb_blob), thus far I keep the same signature names as harfbuzz. (and bytes is a binary "blob" so.. we can revisit later if needed)

@@ -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


cpdef void ot_font_set_funcs(Font font):
Copy link
Member

Choose a reason for hiding this comment

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

Why cpdef here? I used def for all module level functions.

I don't mind cpdef (is it more efficient?) but we should use it consistently in the file then

Copy link
Member Author

Choose a reason for hiding this comment

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

it's only more efficient in so far as you also need to call a cpdef function from within cython (in which case it uses its faster cdef version). But if we only call this from python, then def is ok.

@@ -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)
Copy link
Member

Choose a reason for hiding this comment

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

nit: so far I put the stars before the space

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 copy-pasted it from the harfbuzz headers, which put the stars after the space, but I don't mind.

@@ -0,0 +1,45 @@
#!/usr/bin/python3
Copy link
Member

Choose a reason for hiding this comment

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

I think we could simply put this example in the readme, it's the first file ppl look at so more convenient.

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

setup.py Outdated
@@ -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

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

yes, and pxd too

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

@adrientetar adrientetar merged commit 950a051 into master Apr 19, 2018
@adrientetar adrientetar deleted the sample-py branch April 19, 2018 16:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants