tinygo-org / tinyfont

Text library for TinyGo displays
https://tinygo.org
BSD 3-Clause "New" or "Revised" License
49 stars 12 forks source link

tinyfontgen: Added support for generating fonts from ttf #37

Closed sago35 closed 2 years ago

sago35 commented 2 years ago

This PR adds the ability to create tinyfont from ttf-font.

For example, it can be generated and used with the following command.

$ go run ./cmd/tinyfontgen-ttf --size 32 --package rubikmoonrocks --fontname RubikMoonrocks ./_fonts/RubikMoonrocks-Regular.ttf --output rubikmoonrocks/font.go

$ tinygo flash --target wioterminal --size short ./examples/ttf
   code    data     bss |   flash     ram
  36052     228    6240 |   36280    6468

file : ./examples/ttf

package main

import (
    "image/color"

    "tinygo.org/x/drivers/ili9341"
    "tinygo.org/x/tinyfont"
    "tinygo.org/x/tinyfont/examples/initdisplay"
    "tinygo.org/x/tinyfont/rubikmoonrocks"
)

var (
    font = &rubikmoonrocks.RubikMoonrocks
)

func main() {
    display := initdisplay.InitDisplay()
    dev := display.(*ili9341.Device)
    dev.SetRotation(ili9341.Rotation270)

    tinyfont.WriteLine(display, font, 5, 50, "tinyfont", color.RGBA{0x00, 0x00, 0x00, 0xFF})
    tinyfont.WriteLine(display, font, 5, 100, font.Name, color.RGBA{0x00, 0x00, 0x00, 0xFF})

    select {}
}

font : https://fonts.google.com/specimen/Rubik+Moonrocks?query=rub

sago35 commented 2 years ago

Since most of the font data is stored in ROM, it is easier to use even higher resolution fonts than before. Currently, only the combination of white background and black text color is supported. In the future, it should be possible to create them in a variety of colors.

sago35 commented 2 years ago

font : https://fonts.google.com/specimen/Rubik+Moonrocks?query=rub IMG_20220716_115752.jpg

font : https://fonts.google.com/noto/specimen/Noto+Sans+JP (japanese) mb52ih.jpg

sago35 commented 2 years ago

Thanks for the review. Merged.