writer

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2024 License: MIT Imports: 9 Imported by: 5

README

banner

Writer is a simple and easy-to-use package to write any text on images. It uses harfbuzz for shaping and also works totally fine on bi-directional texts.

[!NOTE] Writer is currently work-in-progress and also looking for feedback.

Getting started:

Requirements:

To get started using Writer, you should first have the latest version of Harfbuzz installed on your machine. You also need pkg-config for go-harfbuzz.

Installation
go get github.com/haashemi/writer@main
Code example:

There are a few examples in the examples folder, but here's one:

package main

import (
	"image"
	"image/png"
	"os"

	"github.com/haashemi/writer"
)

func main() {
	// Load face from the font file
	face := writer.NewFaceFromFile("./fonts/Vazirmatn-ExtraBold.ttf")
	defer face.Close()

	// Create a font from the face
	font := writer.NewFont(face, 45)
	defer font.Close()

	// Create a new writer instance.
	w, _ := writer.NewWriter(font, "Hello World!", writer.DefaultOptions)
	defer w.Close()

	// Create a new image. (doesn't matter how.)
	img := image.NewNRGBA(w.Bounds())

	// Write it on your image
	w.Write(img, image.Point{}, image.White)

	// Save the image. (doesn't matter how.)
	f, _ := os.Create("basic/result.png")
	png.Encode(f, img)
}
Result:

hello world

Contributions:

All types of contributions are highly appreciated. especially giving me feedback on its API and how I can make it better, simpler, and easier to use.

Documentation

Index

Constants

This section is empty.

Variables

DefaultFeatures contains “enabled by default” features.

View Source
var DefaultOptions = Options{
	Bidi:     true,
	Features: DefaultFeatures,
}

DefaultOptions contains the recommended default options

Functions

This section is empty.

Types

type Face

type Face struct {
	io.Closer
	// contains filtered or unexported fields
}

Face holds a harfbuzz Face object.

func NewFace

func NewFace(data []byte) *Face

NewFace returns a newly initialized harfbuzz Face object from “data” bytes.

func NewFaceFromFile

func NewFaceFromFile(filename string) *Face

NewFaceFromFile returns a newly initialized harfbuzz Face object from “filename” file.

func (*Face) Close

func (f *Face) Close() error

Close destroys the harfbuzz Blob and Face objects and frees the memory.

type Feature

type Feature = hb.Feature

Feature is just an alias to harfbuzz's Feature type.

type Font

type Font struct {
	io.Closer
	// contains filtered or unexported fields
}

Font holds a harfbuzz Font object.

func NewFont

func NewFont(face *Face, size int32) *Font

NewFont returns a new Font from the “face” with size of “size”.

func (*Font) Close

func (f *Font) Close() error

Close destroys the font and frees the memory.

func (*Font) Extents

func (f *Font) Extents() (FontExtents, bool)

Extents returns the FontExtents of the Font.

func (*Font) SetSize

func (f *Font) SetSize(size int32)

SetSize updates the font size.

func (*Font) Size

func (f *Font) Size() int32

Size returns the font size.

TODO: It may not be accurate if scaled manually. A better way should be found.

type FontExtents

type FontExtents = hb.FontExtents

FontExtents is just an alias to harfbuzz's FontExtents type.

type Options

type Options struct {
	Bidi     bool
	Features []Feature // Feature are the OpenType feature you want to enable.

}

Options holds the features used to modify, shape, and write the text.

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

func NewWriter

func NewWriter(font *Font, text string, opts Options) (w *Writer, err error)

func (*Writer) Advance

func (w *Writer) Advance() int32

Advance returns how far the text will go after drawing.

func (*Writer) Bounds

func (w *Writer) Bounds() image.Rectangle

Bounds returns the after-drawing text bounds.

func (*Writer) Close

func (w *Writer) Close() error

Close destroys the writer's buffer and frees the memory.

func (*Writer) Write

func (w *Writer) Write(img draw.Image, at image.Point, color image.Image)

Write draws the text on the “image” at “at” with "color”.

Directories

Path Synopsis
example module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL