luv

Workshop wiki

slug-harfbuzz.lisp

hal/shader/slug-harfbuzz.lisp

system luv · 27 definitions · on GitHub

HarfBuzz owns text-to-glyph shaping; Slug owns the resulting outlines.

in-package#:luv.slug
cffi:define-foreign-libraryharfbuzz
:darwin
:or"libharfbuzz.dylib""libharfbuzz.0.dylib"
:unix
:or"libharfbuzz.so""libharfbuzz.so.0"
cffi:use-foreign-libraryharfbuzz
cffi:defcstructhb-glyph-info
codepoint:uint32
mask:uint32
cluster:uint32
var1:uint32
var2:uint32
cffi:defcstructhb-glyph-position
x-advance:int32
y-advance:int32
x-offset:int32
y-offset:int32
var:uint32
cffi:defcfun
"hb_blob_create"hb-blob-create
:pointer
data:pointer
length:uint32
mode:int
user-data:pointer
destroy:pointer
cffi:defcfun
"hb_blob_destroy"hb-blob-destroy
:void
blob:pointer
cffi:defcfun
"hb_face_create"hb-face-create
:pointer
blob:pointer
index:uint32
cffi:defcfun
"hb_face_destroy"hb-face-destroy
:void
face:pointer
cffi:defcfun
"hb_face_get_upem"hb-face-get-upem
:uint32
face:pointer
cffi:defcfun
"hb_font_create"hb-font-create
:pointer
face:pointer
cffi:defcfun
"hb_font_destroy"hb-font-destroy
:void
font:pointer
cffi:defcfun
"hb_font_set_scale"hb-font-set-scale
:void
font:pointer
x-scale:int32
y-scale:int32
cffi:defcfun
"hb_ot_font_set_funcs"hb-ot-font-set-funcs
:void
font:pointer
cffi:defcfun
"hb_buffer_create"hb-buffer-create
:pointer
cffi:defcfun
"hb_buffer_destroy"hb-buffer-destroy
:void
buffer:pointer
cffi:defcfun
"hb_buffer_add_utf8"hb-buffer-add-utf8
:void
buffer:pointer
text:pointer
text-length:int
item-offset:uint32
item-length:int
cffi:defcfun
"hb_buffer_set_direction"hb-buffer-set-direction
:void
buffer:pointer
direction:uint32
cffi:defcfun
"hb_buffer_guess_segment_properties"hb-buffer-guess-segment-properties
:void
buffer:pointer
cffi:defcfun
"hb_shape"hb-shape
:void
font:pointer
buffer:pointer
features:pointer
feature-count:uint32
cffi:defcfun
"hb_buffer_get_glyph_infos"hb-buffer-get-glyph-infos
:pointer
buffer:pointer
length:pointer
cffi:defcfun
"hb_buffer_get_glyph_positions"hb-buffer-get-glyph-positions
:pointer
buffer:pointer
length:pointer
define-conditionslug-shaping-error
error
reason:initarg:reason:readerslug-shaping-error-reason
details:initarg:details:initformnil:readerslug-shaping-error-details
:report
lambda
conditionstream
formatstream"Cannot shape Slug text: ~A~@[ (~S)~]."
slug-shaping-error-reasoncondition
slug-shaping-error-detailscondition
defstructslug-shaped-glyphglyph-idclusterx-advancey-advancex-offsety-offset
defstructslug-shaped-textglyphsunits-per-emx-advancey-advance
defunread-slug-font-bytes
pathname
with-open-file
streampathname:direction:input:element-type'
unsigned-byte8
let
bytes
make-array
file-lengthstream
:element-type'
unsigned-byte8
unless
=
read-sequencebytesstream
lengthbytes
error'slug-shaping-error:reason:short-font-read:detailspathname
bytes
defunslug-harfbuzz-direction
direction

hb_direction_t uses these stable public values so directions compose as bit flags in HarfBuzz's C ABI.

ecasedirection
:ltr4
:rtl5
:ttb6
:btt7
defuncollect-slug-shaped-glyphs
buffer
cffi:with-foreign-object
count:uint32
let*
infos
hb-buffer-get-glyph-infosbuffercount
positions
hb-buffer-get-glyph-positionsbuffercount
length
cffi:mem-refcount:uint32
glyphs
make-arraylength
dotimes
indexlengthglyphs
let
info
cffi:mem-aptrinfos'index
position
cffi:mem-aptrpositions'index
setf
arefglyphsindex
cffi:with-foreign-slots
codepointcluster
info
cffi:with-foreign-slots
x-advancey-advancex-offsety-offset
position
make-slug-shaped-glyph:glyph-idcodepoint:clustercluster:x-advancex-advance:y-advancey-advance:x-offsetx-offset:y-offsety-offset
defunshape-slug-text
stringfont-pathname&keydirection

Shape string with HarfBuzz, returning glyph IDs and font-unit placements.

direction may be :LTR, :RTL, :TTB, or :BTT. With no direction HarfBuzz guesses the segment properties from the Unicode text. #4G7064

let
bytes
blobnil
facenil
fontnil
buffernil
unwind-protect
cffi:with-pointer-to-vector-data
font-databytes

HB_MEMORY_MODE_DUPLICATE copies the bytes before this dynamic pointer leaves scope.

setfblob
hb-blob-createfont-data
lengthbytes
0
cffi:null-pointer
cffi:null-pointer
face
hb-face-createblob0
font
hb-font-createface
buffer
hb-buffer-create
when
some#'cffi:null-pointer-p
listblobfacefontbuffer
error'slug-shaping-error:reason:harfbuzz-allocation-failed
let
units-per-em
hb-face-get-upemface
unless
pluspunits-per-em
error'slug-shaping-error:reason:invalid-units-per-em:detailsunits-per-em
hb-ot-font-set-funcsfont
hb-font-set-scalefontunits-per-emunits-per-em
cffi:with-foreign-string
textbyte-count
string:encoding:utf-8
let
length
1-byte-count
hb-buffer-add-utf8buffertextlength0length
whendirection
hb-buffer-set-directionbuffer
hb-buffer-guess-segment-propertiesbuffer
hb-shapefontbuffer
cffi:null-pointer
0
let*
x-advance
loopforglyphacrossglyphssum
slug-shaped-glyph-x-advanceglyph
y-advance
loopforglyphacrossglyphssum
slug-shaped-glyph-y-advanceglyph
make-slug-shaped-text:glyphsglyphs:units-per-emunits-per-em:x-advancex-advance:y-advancey-advance
whenbuffer
hb-buffer-destroybuffer
whenfont
hb-font-destroyfont
whenface
hb-face-destroyface
whenblob
hb-blob-destroyblob