diff --git a/draw_funcs.go b/draw_funcs.go index 6472d53..b8686c1 100644 --- a/draw_funcs.go +++ b/draw_funcs.go @@ -11,6 +11,7 @@ import ( "golang.org/x/image/vector" ) +// drawingState holds the essential information of drawing glyphs. type drawingState struct { vec *vector.Rasterizer @@ -21,6 +22,8 @@ type drawingState struct { topY, bottomY float32 } +// processY sets the highest and the lowest Y values passed to the draw methods +// to the current drawingState. func (ds *drawingState) processY(ys ...float32) { for _, y := range ys { if y > ds.bottomY { @@ -33,7 +36,8 @@ func (ds *drawingState) processY(ys ...float32) { } } -// drawFuncs +// drawFuncs contains the internal drawing methods used to pass to the harfbuzz +// hb_font_draw method for drawing the glyphs. var drawFuncs = hb.DrawFuncsCreate() // This methods adds all drawing methods to drawFuncs variable. diff --git a/face.go b/face.go index 35f006f..3fb2679 100644 --- a/face.go +++ b/face.go @@ -6,6 +6,10 @@ import ( "github.com/haashemi/go-harfbuzz/hb" ) +// Just to make sure if [Face] implements [io.Closer]. +var _ io.Closer = &Face{} + +// Face holds a harfbuzz Face object. type Face struct { io.Closer @@ -13,11 +17,14 @@ type Face struct { face hb.Face } -func (f *Face) Close() { +// Close destroys the harfbuzz Blob and Face objects and frees the memory. +func (f *Face) Close() error { hb.FaceDestroy(f.face) hb.BlobDestroy(f.blob) + return nil } +// NewFace returns a newly initialized harfbuzz Face object from “data” bytes. func NewFace(data []byte) *Face { blob := hb.BlobCreate(data, hb.MemoryModeDuplicate, nil, nil) face := hb.FaceCreate(blob, 0) @@ -28,6 +35,7 @@ func NewFace(data []byte) *Face { } } +// NewFaceFromFile returns a newly initialized harfbuzz Face object from “filename” file. func NewFaceFromFile(filename string) *Face { blob := hb.BlobCreateFromFile(filename) face := hb.FaceCreate(blob, 0) diff --git a/features.go b/features.go index 68cc47c..f92b3da 100644 --- a/features.go +++ b/features.go @@ -70,7 +70,7 @@ func JustificationAlternatives() Feature { return Feature{Tag: tag("jalt"), Value: 1, Start: 0, End: 4294967295} } -// tag converts str to a Tag +// tag converts “str” to a Tag func tag(str string) hb.Tag { if len(str) < 4 { return hb.Tag{} diff --git a/font.go b/font.go index 1174c0d..1e4f64e 100644 --- a/font.go +++ b/font.go @@ -7,35 +7,48 @@ import ( "github.com/haashemi/go-harfbuzz/hb" ) +// FontExtents is just an alias to harfbuzz's FontExtents type. type FontExtents = hb.FontExtents +// Just to make sure if [Font] implements [io.Closer]. +var _ io.Closer = &Font{} + +// Font holds a harfbuzz Font object. type Font struct { io.Closer font hb.Font } +// NewFont returns a new Font from the “face” with size of “size”. func NewFont(face *Face, size int32) *Font { font := &Font{font: hb.FontCreate(face.face)} font.SetSize(size) return font } +// Extents returns the FontExtents of the Font. func (f *Font) Extents() (FontExtents, bool) { return hb.FontGetHExtents(f.font) } +// Size returns the font size. +// +// TODO: It may not be accurate if scaled manually. A better way should be found. func (f *Font) Size() int32 { _, h := hb.FontGetScale(f.font) return h / 64 } +// SetSize updates the font size. func (f *Font) SetSize(size int32) { hb.FontSetScale(f.font, size*64, size*64) } -func (f *Font) Close() { +// Close destroys the font and frees the memory. +func (f *Font) Close() error { hb.FontDestroy(f.font) + return nil } func (f *Font) draw(glyph uint32, dfuncs hb.DrawFuncs, drawData unsafe.Pointer) { diff --git a/writer.go b/writer.go index 9b4330d..10437da 100644 --- a/writer.go +++ b/writer.go @@ -107,6 +107,7 @@ func (w *Writer) Write(img draw.Image, at image.Point, color image.Image) { state.vec.Draw(img, bounds.Add(at), color, image.Point{}) } +// Close destroys the writer's buffer and frees the memory. func (w *Writer) Close() error { hb.BufferDestroy(w.buf) return nil