v/vlib/freetype/freetype.v

455 lines
12 KiB
V
Raw Normal View History

2020-01-23 21:04:46 +01:00
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module freetype
import os
import gx
import gg
import glm
import gl
2020-05-16 16:12:23 +02:00
2020-05-01 10:57:05 +02:00
/*
TODO
!!!!!!
Use a font atlas
!!!!!!
*/
#flag windows -I @VROOT/thirdparty/freetype/include
#flag windows -L @VROOT/thirdparty/freetype/win64
#flag solaris -I/opt/local/include/freetype2
#flag solaris -L/opt/local/lib
#flag darwin -I/usr/local/include/freetype2
2020-02-24 16:54:04 +01:00
// MacPorts
#flag darwin -I/opt/local/include/freetype2
2020-02-24 16:54:04 +01:00
#flag darwin -L/opt/local/lib
#flag freebsd -I/usr/local/include/freetype2
2019-12-15 04:18:14 +01:00
#flag freebsd -Wl -L/usr/local/lib
#flag -lfreetype
2020-05-16 16:12:23 +02:00
// #flag -I @VROOT/thirdparty/freetype
// #flag @VROOT/thirdparty/freetype/libfreetype.a
#flag darwin -lpng -lbz2 -lz
#flag linux -I/usr/include/freetype2
#flag linux -I.
#include "ft2build.h"
#include FT_FREETYPE_H
2019-11-24 04:27:02 +01:00
fn C.FT_Init_FreeType() voidptr
2020-05-16 16:12:23 +02:00
fn C.FT_New_Face() voidptr
2020-05-16 16:12:23 +02:00
fn C.FT_Set_Pixel_Sizes()
2020-01-12 20:08:24 +01:00
pub const (
default_font_size = 12
)
struct Character {
2020-05-16 16:12:23 +02:00
code i64
texture_id u32
size gg.Vec2
horizontal_bearing_px gg.Vec2
horizontal_advance_px u32
vertical_bearing_px gg.Vec2
vertical_advance_px u32
}
[typedef]
2020-05-16 16:12:23 +02:00
struct C.FT_Library {
}
2019-11-20 05:10:19 +01:00
pub struct FreeType {
shader gl.Shader
width int
height int
vao u32
rect_vao u32
rect_vbo u32
line_vao u32
line_vbo u32
vbo u32
chars []Character
face &C.FT_FaceRec
scale int // retina = 2 , normal = 1
mut:
utf_runes []string
utf_chars []Character
}
struct C.Bitmap {
2020-05-16 16:12:23 +02:00
width int
rows int
buffer int
}
struct C.Advance {
x int
y int
}
[typedef]
struct C.FT_Glyph_Metrics {
width int
height int
horiBearingX int
horiBearingY int
horiAdvance int
vertBearingX int
vertBearingY int
vertAdvance int
}
struct C.Glyph {
2020-05-16 16:12:23 +02:00
bitmap C.Bitmap
bitmap_left int
2020-05-16 16:12:23 +02:00
bitmap_top int
advance Advance
metrics C.FT_Glyph_Metrics
}
[typedef]
struct C.FT_FaceRec {
2020-05-16 16:12:23 +02:00
glyph &C.Glyph
family_name charptr
2020-05-16 16:12:23 +02:00
style_name charptr
}
2020-05-16 16:12:23 +02:00
// /type FT_Face &C.FT_FaceRec
fn C.FT_Load_Char(voidptr, i64, int) int
fn ft_load_char(face &C.FT_FaceRec, code i64) Character {
2020-05-16 16:12:23 +02:00
// println('\nftload_char( code=$code)')
// C.printf('face=%p\n', face)
// C.printf('cobj=%p\n', _face.cobj)
ret := C.FT_Load_Char(face, code, C.FT_LOAD_RENDER | C.FT_LOAD_FORCE_AUTOHINT)
// println('ret=$ret')
if ret != 0 {
2020-05-16 16:12:23 +02:00
println('freetype: failed to load glyph (utf32 code=$code, ' + 'error code=$ret)')
return Character{
code: code
}
}
// Generate texture
mut texture := 0
C.glGenTextures(1, &texture)
C.glBindTexture(C.GL_TEXTURE_2D, texture)
fgwidth := (*face).glyph.bitmap.width
2020-05-16 16:12:23 +02:00
fgrows := (*face).glyph.bitmap.rows
C.glTexImage2D(C.GL_TEXTURE_2D, 0, C.GL_RED, fgwidth, fgrows, 0, C.GL_RED, C.GL_UNSIGNED_BYTE,
(*face).glyph.bitmap.buffer)
// Set texture options
2019-08-22 23:00:31 +02:00
C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_WRAP_S, C.GL_CLAMP_TO_EDGE)
C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_WRAP_T, C.GL_CLAMP_TO_EDGE)
C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MIN_FILTER, C.GL_LINEAR)
C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MAG_FILTER, C.GL_LINEAR)
// Create the character
return Character {
code: code
texture_id: u32(texture)
size: gg.vec2(fgwidth, fgrows)
// Note: advance is number of 1/64 pixels
// Bitshift by 6 to get value in pixels (2^6 = 64 (divide amount of 1/64th pixels by 64 to get amount of pixels))
horizontal_bearing_px: gg.vec2((*face).glyph.metrics.horiBearingX >> 6, (*face).glyph.metrics.horiBearingY >> 6)
vertical_bearing_px: gg.vec2((*face).glyph.metrics.vertBearingX >> 6, (*face).glyph.metrics.vertBearingY >> 6) // not used for now
horizontal_advance_px: u32((*face).glyph.metrics.horiAdvance) >> 6
vertical_advance_px: u32((*face).glyph.metrics.vertAdvance) >> 6
}
}
2019-11-20 05:10:19 +01:00
pub fn new_context(cfg gg.Cfg) &FreeType {
scale := cfg.scale
// Can only have text in ortho mode
if !cfg.use_ortho {
2020-05-16 16:12:23 +02:00
return &FreeType{
face: 0
}
}
width := cfg.width * scale
height := cfg.height * scale
font_size := cfg.font_size * scale
// exit('fs=$font_size')
// if false {
// retina
// width = width * 2// scale// 2
// height = height * 2// scale// 2
// font_size *= scale// 2
// }
/*
gl.viewport(0, 0, width, height)
*/
// gl.enable(GL_CULL_FACE) // TODO NEED CULL?
2019-08-22 23:00:31 +02:00
gl.enable(C.GL_BLEND)
C.glBlendFunc(C.GL_SRC_ALPHA, C.GL_ONE_MINUS_SRC_ALPHA)
shader := gl.new_shader('text')
shader.use()
2020-05-16 16:12:23 +02:00
projection := glm.ortho(0, width, 0, height) // 0 at BOT
shader.set_mat4('projection', projection)
// FREETYPE
ft := C.FT_Library{}
// All functions return a value different than 0 whenever
// an error occurred
mut ret := C.FT_Init_FreeType(&ft)
if ret != 0 {
panic('freetype: Could not init FreeType Library')
}
// Load font as face
mut font_path := cfg.font_path
if font_path == '' {
font_path = 'RobotoMono-Regular.ttf'
}
if !os.exists(font_path) {
font_path = os.resource_abs_path(font_path)
}
if !os.exists(font_path) {
eprintln('freetype: font "$font_path" does not exist')
return voidptr(0)
}
2020-05-16 16:12:23 +02:00
face := &C.FT_FaceRec{
glyph: 0
}
ret = int(C.FT_New_Face(ft, font_path.str, 0, &face))
2020-05-16 16:12:23 +02:00
if ret != 0 {
eprintln('freetype: failed to load font (error=$ret) from path: $font_path')
exit(1)
}
// Set size to load glyphs as
C.FT_Set_Pixel_Sizes(face, 0, font_size)
// Disable byte-alignment restriction
C.glPixelStorei(C.GL_UNPACK_ALIGNMENT, 1)
// Gen texture
// Load first 128 characters of ASCII set
mut chars := []Character{}
2020-05-16 16:12:23 +02:00
for c in 0 .. 128 {
2019-12-12 12:44:29 +01:00
ch := ft_load_char(face, i64(c))
// s := utf32_to_str(uint(0x043f))
// s := 'п'
// ch = ft_load_char(f, s.utf32_code())
// # unsigned long c = FT_Get_Char_Index(face, 0x043f );
// # printf("!!!!!!!!! %lu\n", c);
// # c = FT_Get_Char_Index(face, 0xd0bf );
// # printf("!!!!!!!!! %lu\n", c);
// # ch = gg__ft_load_char(f, 0xd0bf) ; // UTF 8
chars << ch
}
2020-05-16 16:12:23 +02:00
// ch := Character{}
// Configure VAO
vao := gl.gen_vertex_array()
2020-05-16 16:12:23 +02:00
// println('new gg text context vao=$vao')
vbo := gl.gen_buffer()
gl.bind_vao(vao)
2019-08-22 23:00:31 +02:00
gl.bind_buffer(C.GL_ARRAY_BUFFER, vbo)
// # glBufferData(GL_ARRAY_BUFFER, sizeof(GLf32) * 6 * 4, NULL, GL_DYNAMIC_DRAW);
gl.enable_vertex_attrib_array(0)
2019-08-22 23:00:31 +02:00
gl.vertex_attrib_pointer(0, 4, C.GL_FLOAT, false, 4, 0)
// # glVertexAttribPointer(0, 4, GL_FLOAT,false, 4 * sizeof(GLf32), 0);
// gl.bind_buffer(GL_ARRAY_BUFFER, uint(0))
// # glBindVertexArray(0);
2020-05-16 16:12:23 +02:00
ctx := &FreeType{
shader: shader
width: width
height: height
scale: scale
vao: vao
vbo: vbo
chars: chars
face: face
}
2020-05-16 16:12:23 +02:00
// ctx.init_utf8_runes()
return ctx
}
2020-05-16 16:12:23 +02:00
pub fn (mut ctx FreeType) draw_text(_x, _y int, text string, cfg gx.TextCfg) {
// utext := text.ustring_tmp()
2019-08-18 17:11:17 +02:00
utext := text.ustring()
ctx.private_draw_text(_x, _y, utext, cfg)
}
2020-05-16 16:12:23 +02:00
fn (mut ctx FreeType) draw_text_fast(_x, _y int, text ustring, cfg gx.TextCfg) {
ctx.private_draw_text(_x, _y, text, cfg)
}
2020-05-16 16:12:23 +02:00
fn (mut ctx FreeType) private_draw_text(_x, _y int, utext ustring, cfg gx.TextCfg) {
/*
if utext.s.contains('on_seg') {
println('\nat(0)')
println(utext.runes)
firstc := utext.at(0)
println('drawtext "$utext.s" len=$utext.s.len ulen=$utext.len x=$_x firstc=$firstc')
if firstc != ' ' {
exit(1)
}
}
*/
mut x := f32(_x)
mut y := f32(_y)
wx, wy := ctx.text_size(utext.s)
2020-05-13 00:50:46 +02:00
yoffset := if ctx.scale > 1 { 5 } else { -1 } // 5 hidpi, -1 lowdpi
// println('scale=$ctx.scale size=$cfg.size')
2020-05-22 17:36:09 +02:00
if cfg.align == gx.align_right {
2020-05-16 16:12:23 +02:00
// width := utext.len * 7
width := wx
x -= f32(width + 10)
}
x *= f32(ctx.scale)
y *= f32(ctx.scale)
y += f32(yoffset)
2020-05-16 16:12:23 +02:00
y = f32(ctx.height) - y // invert y direction
color := cfg.color
// Activate corresponding render state
ctx.shader.use()
ctx.shader.set_color('textColor', color)
C.glActiveTexture(C.GL_TEXTURE0)
gl.bind_vao(ctx.vao)
// Iterate through all characters
// utext := text.ustring()
2020-05-16 16:12:23 +02:00
for i in 0 .. utext.len {
rune_ := utext.at(i)
// println('$i => $rune_')
mut ch := Character{}
mut found := false
2020-05-16 16:12:23 +02:00
if rune_.len == 1 {
idx := rune_[0]
if idx < 0 || idx >= ctx.chars.len {
2020-05-16 16:12:23 +02:00
println('BADE RUNE $rune_')
continue
}
found = true
2020-05-16 16:12:23 +02:00
ch = ctx.chars[rune_[0]]
} else if rune_.len > 1 {
// TODO O(1) use map
2020-05-16 16:12:23 +02:00
for j in 0 .. ctx.utf_runes.len {
rune_j := ctx.utf_runes[j]
2020-05-16 16:12:23 +02:00
if rune_j == rune_ {
ch = ctx.utf_chars[j]
found = true
break
}
}
}
// A new Unicode character. Load it and cache it.
2020-05-16 16:12:23 +02:00
if !found && rune_.len > 0 && rune_[0] > 32 {
// c := rune_[0]
// println('cant draw rune "$rune_" code=$c, loading')
// continue
ch = ft_load_char(ctx.face, rune_.utf32_code())
// println('done loading')
ctx.utf_runes << rune_
ctx.utf_chars << ch
2020-05-16 16:12:23 +02:00
// exit(1)
// continue
}
xpos := x + f32(ch.horizontal_bearing_px.x) * 1
ypos := y - f32(ch.size.y + wy - ch.horizontal_bearing_px.y) * 1
2020-05-16 16:12:23 +02:00
// ypos := y - wy
w := f32(ch.size.x) * 1
h := f32(ch.size.y) * 1
// Update VBO for each character
2020-05-16 16:12:23 +02:00
vertices := [
xpos, ypos + h, 0.0, 0.0,
xpos, ypos, 0.0, 1.0,
xpos + w, ypos, 1.0, 1.0,
xpos, ypos + h, 0.0, 0.0,
xpos + w, ypos, 1.0, 1.0,
xpos + w, ypos + h, 1.0, 0.0]
// Render glyph texture over quad
C.glBindTexture(C.GL_TEXTURE_2D, ch.texture_id)
// Update content of VBO memory
2019-08-22 23:00:31 +02:00
gl.bind_buffer(C.GL_ARRAY_BUFFER, ctx.vbo)
// glBufferSubData(..)
2019-08-22 23:00:31 +02:00
C.glBufferData(C.GL_ARRAY_BUFFER, 96, vertices.data, C.GL_DYNAMIC_DRAW)
// Render quad
2019-08-22 23:00:31 +02:00
gl.draw_arrays(C.GL_TRIANGLES, 0, 6)
x += f32(ch.horizontal_advance_px)
2020-01-20 05:14:49 +01:00
// Stop drawing if the limit is reached
2020-05-16 16:12:23 +02:00
if cfg.max_width > 0 {
2020-01-20 05:14:49 +01:00
if x >= cfg.max_width {
2020-05-16 16:12:23 +02:00
// break
2020-01-20 05:14:49 +01:00
}
}
}
gl.bind_vao(u32(0))
C.glBindTexture(C.GL_TEXTURE_2D, 0)
}
2020-05-16 16:12:23 +02:00
pub fn (mut ctx FreeType) draw_text_def(x, y int, text string) {
cfg := gx.TextCfg{
2020-05-22 17:36:09 +02:00
color: gx.black
2020-01-12 20:08:24 +01:00
size: default_font_size
2020-04-02 01:45:22 +02:00
align: gx.align_left
}
ctx.draw_text(x, y, text, cfg)
}
2020-05-16 16:12:23 +02:00
pub fn (mut ctx FreeType) text_width(s string) int {
x, _ := ctx.text_size(s)
return x
}
2020-05-16 16:12:23 +02:00
pub fn (mut ctx FreeType) text_height(s string) int {
_, y := ctx.text_size(s)
return y
}
2020-05-16 16:12:23 +02:00
pub fn (mut ctx FreeType) text_size(s string) (int, int) {
// t := time.ticks()
utext := s.ustring()
mut x := u32(0)
mut maxy := u32(0)
2020-05-16 16:12:23 +02:00
mut rune_ := ''
mut ch := Character{}
2020-05-16 16:12:23 +02:00
for i in 0 .. utext.len {
rune_ = utext.at(i)
ch = Character{}
mut found := false
2020-05-16 16:12:23 +02:00
if rune_.len == 1 {
idx := rune_[0]
if idx < 0 || idx >= ctx.chars.len {
2020-05-16 16:12:23 +02:00
println('BADE RUNE $rune_')
continue
}
found = true
2020-05-16 16:12:23 +02:00
ch = ctx.chars[rune_[0]]
} else if rune_.len > 1 {
// TODO O(1) use map
2020-05-16 16:12:23 +02:00
for j in 0 .. ctx.utf_runes.len {
rune_j := ctx.utf_runes[j]
2020-05-16 16:12:23 +02:00
if rune_j == rune_ {
ch = ctx.utf_chars[j]
found = true
break
}
}
}
2020-05-16 16:12:23 +02:00
if !found && rune_.len > 0 && rune_[0] > 32 {
ch = ft_load_char(ctx.face, rune_.utf32_code())
ctx.utf_runes << rune_
ctx.utf_chars << ch
}
x += ch.horizontal_advance_px
if maxy < ch.vertical_advance_px {
maxy = ch.vertical_advance_px
}
}
2020-05-16 16:12:23 +02:00
// println('text width "$s" = ${time.ticks() - t} ms')
// scaled_x := x
// scaled_y := maxy
scaled_x := int(f64(x) / ctx.scale)
scaled_y := int(f64(maxy) / ctx.scale)
// println('text_size of "${s}" | x,y: $x,$maxy | scaled_x: ${scaled_x:3d} | scaled_y: ${scaled_y:3d} ')
return scaled_x, scaled_y
}
2020-04-02 01:45:22 +02:00
/*
pub fn (f FT_Face) str() string {
return 'FT_Face{ style_name: ${ptr_str(f.style_name)} family_name: ${ptr_str(f.family_name)} }'
}
2020-04-02 01:45:22 +02:00
*/
pub fn (ac []Character) str() string {
mut res := []string{}
for c in ac {
res << ' Character{ code: $c.code , texture_id: $c.texture_id }'
}
return '[\n' + res.join(',\n') + ']'
}