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

Adding several additional font functions. #175

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
58 changes: 58 additions & 0 deletions src/uharfbuzz/_harfbuzz.pyx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#cython: language_level=3
cimport cython
import os
import warnings
from enum import IntEnum, IntFlag
Expand Down Expand Up @@ -648,6 +649,38 @@ cdef class Font:
def synthetic_slant(self, value: float):
hb_font_set_synthetic_slant(self._hb_font, value)

@property
def synthetic_bold(self) -> tuple[float, float, bool]:
cdef float x_embolden
cdef float y_embolden
cdef hb_bool_t in_place
hb_font_get_synthetic_bold(self._hb_font, &x_embolden, &y_embolden, &in_place)
return (x_embolden, y_embolden, bool(in_place))

@synthetic_bold.setter
def synthetic_bold(self, value: float|tuple[float]|tuple[float,float]|tuple[float,float,bool]):
cdef float x_embolden
cdef float y_embolden
cdef hb_bool_t in_place = False
if isinstance(value, tuple):
if len(value) == 1:
x_embolden = y_embolden = value[0]
elif len(value) == 2:
x_embolden, y_embolden = value
else:
x_embolden, y_embolden, in_place = value
else:
x_embolden = y_embolden = value
hb_font_set_synthetic_bold(self._hb_font, x_embolden, y_embolden, in_place)

@property
def var_named_instance(self) -> int:
return hb_font_get_var_named_instance(self._hb_font)

@var_named_instance.setter
def var_named_instance(self, value: int):
hb_font_set_var_named_instance(self._hb_font, value)

def set_variations(self, variations: Dict[str, float]) -> None:
cdef unsigned int size
cdef hb_variation_t* hb_variations
Expand All @@ -668,6 +701,11 @@ cdef class Font:
finally:
free(hb_variations)

def set_variation(self, name: str, value: float) -> None:
packed = name.encode()
cdef hb_tag_t tag = hb_tag_from_string(packed, -1)
hb_font_set_variation(self._hb_font, tag, value)

def get_glyph_name(self, gid: int):
cdef char name[64]
cdef bytes packed
Expand Down Expand Up @@ -761,6 +799,26 @@ cdef class Font:
finally:
free(coords_2dot14)

def get_var_coords_design(self):
cdef unsigned int length
cdef const float *coords
coords = hb_font_get_var_coords_design(self._hb_font, &length)
return [coords[i] for i in range(length)]

def set_var_coords_design(self, coords):
cdef unsigned int length
cdef cython.float *c_coords
length = len(coords)
c_coords = <cython.float*>malloc(length * sizeof(cython.float))
if c_coords is NULL:
raise MemoryError()
try:
for i in range(length):
c_coords[i] = coords[i]
hb_font_set_var_coords_design(self._hb_font, c_coords, length)
finally:
free(c_coords)

def glyph_to_string(self, gid: int):
cdef char name[64]
cdef bytes packed
Expand Down
20 changes: 20 additions & 0 deletions src/uharfbuzz/charfbuzz.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,32 @@ cdef extern from "hb.h":
void hb_font_set_ppem(hb_font_t* font, unsigned int x_ppem, unsigned int y_ppem)
float hb_font_get_ptem(hb_font_t* font)
void hb_font_set_ptem(hb_font_t* font, float ptem)
void hb_font_get_synthetic_bold (hb_font_t *font,
float *x_embolden,
float *y_embolden,
hb_bool_t *in_place)
void hb_font_set_synthetic_bold (hb_font_t *font,
float x_embolden,
float y_embolden,
hb_bool_t in_place)
float hb_font_get_synthetic_slant (hb_font_t *font)
void hb_font_set_synthetic_slant (hb_font_t *font, float slant)
void hb_font_set_variations(
hb_font_t* font,
const hb_variation_t* variations,
unsigned int variations_length)
void hb_font_set_variation(
hb_font_t *font,
hb_tag_t tag,
float value);
void hb_font_set_var_named_instance (hb_font_t *font, unsigned int instance_index)
unsigned int hb_font_get_var_named_instance (hb_font_t *font)
void hb_font_set_var_coords_design(
hb_font_t *font,
const float *coords,
unsigned int coords_length)
const float * hb_font_get_var_coords_design(
hb_font_t *font, unsigned int *length)
hb_bool_t hb_font_get_glyph_name(
hb_font_t* font,
hb_codepoint_t glyph,
Expand Down
Loading