Skip to content

Commit

Permalink
chore: add a little code documentations
Browse files Browse the repository at this point in the history
  • Loading branch information
haashemi committed Mar 16, 2024
1 parent a61fed2 commit dbb0bcd
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
6 changes: 5 additions & 1 deletion draw_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"golang.org/x/image/vector"
)

// drawingState holds the essential information of drawing glyphs.
type drawingState struct {
vec *vector.Rasterizer

Expand All @@ -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 {
Expand All @@ -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.
Expand Down
10 changes: 9 additions & 1 deletion face.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,25 @@ 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

blob hb.Blob
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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion features.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
15 changes: 14 additions & 1 deletion font.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit dbb0bcd

Please sign in to comment.