mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: check incorrect names
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
// 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
|
||||
@ -9,19 +8,17 @@ import gx
|
||||
import gg
|
||||
import glm
|
||||
import gl
|
||||
|
||||
/*
|
||||
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
|
||||
// MacPorts
|
||||
#flag darwin -I/opt/local/include/freetype2
|
||||
@ -29,49 +26,39 @@ Use a font atlas
|
||||
#flag freebsd -I/usr/local/include/freetype2
|
||||
#flag freebsd -Wl -L/usr/local/lib
|
||||
#flag -lfreetype
|
||||
|
||||
//#flag -I @VROOT/thirdparty/freetype
|
||||
|
||||
//#flag @VROOT/thirdparty/freetype/libfreetype.a
|
||||
// #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
|
||||
|
||||
fn C.FT_Init_FreeType() voidptr
|
||||
|
||||
fn C.FT_New_Face() voidptr
|
||||
|
||||
fn C.FT_Set_Pixel_Sizes()
|
||||
|
||||
|
||||
|
||||
pub const (
|
||||
default_font_size = 12
|
||||
)
|
||||
|
||||
struct Character {
|
||||
code i64
|
||||
|
||||
texture_id u32
|
||||
size gg.Vec2
|
||||
|
||||
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]
|
||||
struct C.FT_Library {}
|
||||
struct C.FT_Library {
|
||||
}
|
||||
|
||||
pub struct FreeType {
|
||||
shader gl.Shader
|
||||
// use_ortho bool
|
||||
width int
|
||||
height int
|
||||
vao u32
|
||||
@ -89,8 +76,8 @@ mut:
|
||||
}
|
||||
|
||||
struct C.Bitmap {
|
||||
width int
|
||||
rows int
|
||||
width int
|
||||
rows int
|
||||
buffer int
|
||||
}
|
||||
|
||||
@ -112,42 +99,43 @@ struct C.FT_Glyph_Metrics {
|
||||
}
|
||||
|
||||
struct C.Glyph {
|
||||
bitmap C.Bitmap
|
||||
bitmap C.Bitmap
|
||||
bitmap_left int
|
||||
bitmap_top int
|
||||
advance Advance
|
||||
metrics C.FT_Glyph_Metrics
|
||||
bitmap_top int
|
||||
advance Advance
|
||||
metrics C.FT_Glyph_Metrics
|
||||
}
|
||||
|
||||
[typedef]
|
||||
struct C.FT_FaceRec {
|
||||
glyph &C.Glyph
|
||||
glyph &C.Glyph
|
||||
family_name charptr
|
||||
style_name charptr
|
||||
style_name charptr
|
||||
}
|
||||
///type FT_Face &C.FT_FaceRec
|
||||
|
||||
// /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 {
|
||||
//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')
|
||||
// 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 {
|
||||
println('freetype: failed to load glyph (utf32 code=$code, ' +
|
||||
'error code=$ret)')
|
||||
return Character{code: code}
|
||||
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
|
||||
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)
|
||||
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
|
||||
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)
|
||||
@ -173,7 +161,9 @@ pub fn new_context(cfg gg.Cfg) &FreeType {
|
||||
scale := cfg.scale
|
||||
// Can only have text in ortho mode
|
||||
if !cfg.use_ortho {
|
||||
return &FreeType{face:0}
|
||||
return &FreeType{
|
||||
face: 0
|
||||
}
|
||||
}
|
||||
width := cfg.width * scale
|
||||
height := cfg.height * scale
|
||||
@ -193,7 +183,7 @@ pub fn new_context(cfg gg.Cfg) &FreeType {
|
||||
C.glBlendFunc(C.GL_SRC_ALPHA, C.GL_ONE_MINUS_SRC_ALPHA)
|
||||
shader := gl.new_shader('text')
|
||||
shader.use()
|
||||
projection := glm.ortho(0, width, 0, height)// 0 at BOT
|
||||
projection := glm.ortho(0, width, 0, height) // 0 at BOT
|
||||
shader.set_mat4('projection', projection)
|
||||
// FREETYPE
|
||||
ft := C.FT_Library{}
|
||||
@ -215,9 +205,11 @@ pub fn new_context(cfg gg.Cfg) &FreeType {
|
||||
eprintln('freetype: font "$font_path" does not exist')
|
||||
return 0
|
||||
}
|
||||
face := &C.FT_FaceRec{glyph:0}
|
||||
face := &C.FT_FaceRec{
|
||||
glyph: 0
|
||||
}
|
||||
ret = int(C.FT_New_Face(ft, font_path.str, 0, &face))
|
||||
if ret != 0 {
|
||||
if ret != 0 {
|
||||
eprintln('freetype: failed to load font (error=$ret) from path: $font_path')
|
||||
exit(1)
|
||||
}
|
||||
@ -228,7 +220,7 @@ pub fn new_context(cfg gg.Cfg) &FreeType {
|
||||
// Gen texture
|
||||
// Load first 128 characters of ASCII set
|
||||
mut chars := []Character{}
|
||||
for c in 0..128 {
|
||||
for c in 0 .. 128 {
|
||||
ch := ft_load_char(face, i64(c))
|
||||
// s := utf32_to_str(uint(0x043f))
|
||||
// s := 'п'
|
||||
@ -240,10 +232,10 @@ pub fn new_context(cfg gg.Cfg) &FreeType {
|
||||
// # ch = gg__ft_load_char(f, 0xd0bf) ; // UTF 8
|
||||
chars << ch
|
||||
}
|
||||
//ch := Character{}
|
||||
// ch := Character{}
|
||||
// Configure VAO
|
||||
vao := gl.gen_vertex_array()
|
||||
//println('new gg text context vao=$vao')
|
||||
// println('new gg text context vao=$vao')
|
||||
vbo := gl.gen_buffer()
|
||||
gl.bind_vao(vao)
|
||||
gl.bind_buffer(C.GL_ARRAY_BUFFER, vbo)
|
||||
@ -253,7 +245,7 @@ pub fn new_context(cfg gg.Cfg) &FreeType {
|
||||
// # glVertexAttribPointer(0, 4, GL_FLOAT,false, 4 * sizeof(GLf32), 0);
|
||||
// gl.bind_buffer(GL_ARRAY_BUFFER, uint(0))
|
||||
// # glBindVertexArray(0);
|
||||
ctx := &FreeType {
|
||||
ctx := &FreeType{
|
||||
shader: shader
|
||||
width: width
|
||||
height: height
|
||||
@ -263,21 +255,21 @@ pub fn new_context(cfg gg.Cfg) &FreeType {
|
||||
chars: chars
|
||||
face: face
|
||||
}
|
||||
//ctx.init_utf8_runes()
|
||||
// ctx.init_utf8_runes()
|
||||
return ctx
|
||||
}
|
||||
|
||||
pub fn (ctx mut FreeType) draw_text(_x, _y int, text string, cfg gx.TextCfg) {
|
||||
//utext := text.ustring_tmp()
|
||||
pub fn (mut ctx FreeType) draw_text(_x, _y int, text string, cfg gx.TextCfg) {
|
||||
// utext := text.ustring_tmp()
|
||||
utext := text.ustring()
|
||||
ctx.private_draw_text(_x, _y, utext, cfg)
|
||||
}
|
||||
|
||||
fn (ctx mut FreeType) draw_text_fast(_x, _y int, text ustring, cfg gx.TextCfg) {
|
||||
fn (mut ctx FreeType) draw_text_fast(_x, _y int, text ustring, cfg gx.TextCfg) {
|
||||
ctx.private_draw_text(_x, _y, text, cfg)
|
||||
}
|
||||
|
||||
fn (ctx mut FreeType) private_draw_text(_x, _y int, utext ustring, cfg gx.TextCfg) {
|
||||
fn (mut ctx FreeType) private_draw_text(_x, _y int, utext ustring, cfg gx.TextCfg) {
|
||||
/*
|
||||
if utext.s.contains('on_seg') {
|
||||
println('\nat(0)')
|
||||
@ -295,14 +287,14 @@ fn (ctx mut FreeType) private_draw_text(_x, _y int, utext ustring, cfg gx.TextCf
|
||||
yoffset := if ctx.scale > 1 { 5 } else { -1 } // 5 hidpi, -1 lowdpi
|
||||
// println('scale=$ctx.scale size=$cfg.size')
|
||||
if cfg.align == gx.ALIGN_RIGHT {
|
||||
//width := utext.len * 7
|
||||
// width := utext.len * 7
|
||||
width := wx
|
||||
x -= width + 10
|
||||
}
|
||||
x *= ctx.scale
|
||||
y *= ctx.scale
|
||||
y += yoffset
|
||||
y = f32(ctx.height) - y //invert y direction
|
||||
y = f32(ctx.height) - y // invert y direction
|
||||
color := cfg.color
|
||||
// Activate corresponding render state
|
||||
ctx.shader.use()
|
||||
@ -311,25 +303,24 @@ fn (ctx mut FreeType) private_draw_text(_x, _y int, utext ustring, cfg gx.TextCf
|
||||
gl.bind_vao(ctx.vao)
|
||||
// Iterate through all characters
|
||||
// utext := text.ustring()
|
||||
for i in 0..utext.len {
|
||||
_rune := utext.at(i)
|
||||
// println('$i => $_rune')
|
||||
for i in 0 .. utext.len {
|
||||
rune_ := utext.at(i)
|
||||
// println('$i => $rune_')
|
||||
mut ch := Character{}
|
||||
mut found := false
|
||||
if _rune.len == 1 {
|
||||
idx := _rune[0]
|
||||
if rune_.len == 1 {
|
||||
idx := rune_[0]
|
||||
if idx < 0 || idx >= ctx.chars.len {
|
||||
println('BADE RUNE $_rune')
|
||||
println('BADE RUNE $rune_')
|
||||
continue
|
||||
}
|
||||
found = true
|
||||
ch = ctx.chars[_rune[0]]
|
||||
}
|
||||
else if _rune.len > 1 {
|
||||
ch = ctx.chars[rune_[0]]
|
||||
} else if rune_.len > 1 {
|
||||
// TODO O(1) use map
|
||||
for j in 0..ctx.utf_runes.len {
|
||||
for j in 0 .. ctx.utf_runes.len {
|
||||
rune_j := ctx.utf_runes[j]
|
||||
if rune_j==_rune {
|
||||
if rune_j == rune_ {
|
||||
ch = ctx.utf_chars[j]
|
||||
found = true
|
||||
break
|
||||
@ -337,31 +328,30 @@ fn (ctx mut FreeType) private_draw_text(_x, _y int, utext ustring, cfg gx.TextCf
|
||||
}
|
||||
}
|
||||
// A new Unicode character. Load it and cache it.
|
||||
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
|
||||
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
|
||||
//exit(1)
|
||||
// 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
|
||||
//ypos := y - wy
|
||||
// ypos := y - wy
|
||||
w := f32(ch.size.x) * 1
|
||||
h := f32(ch.size.y) * 1
|
||||
// Update VBO for each character
|
||||
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
|
||||
]
|
||||
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
|
||||
@ -372,9 +362,9 @@ fn (ctx mut FreeType) private_draw_text(_x, _y int, utext ustring, cfg gx.TextCf
|
||||
gl.draw_arrays(C.GL_TRIANGLES, 0, 6)
|
||||
x += f32(ch.horizontal_advance_px)
|
||||
// Stop drawing if the limit is reached
|
||||
if cfg.max_width > 0 {
|
||||
if cfg.max_width > 0 {
|
||||
if x >= cfg.max_width {
|
||||
//break
|
||||
// break
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -382,8 +372,8 @@ fn (ctx mut FreeType) private_draw_text(_x, _y int, utext ustring, cfg gx.TextCf
|
||||
C.glBindTexture(C.GL_TEXTURE_2D, 0)
|
||||
}
|
||||
|
||||
pub fn (ctx mut FreeType) draw_text_def(x, y int, text string) {
|
||||
cfg := gx.TextCfg {
|
||||
pub fn (mut ctx FreeType) draw_text_def(x, y int, text string) {
|
||||
cfg := gx.TextCfg{
|
||||
color: gx.Black
|
||||
size: default_font_size
|
||||
align: gx.align_left
|
||||
@ -391,50 +381,49 @@ pub fn (ctx mut FreeType) draw_text_def(x, y int, text string) {
|
||||
ctx.draw_text(x, y, text, cfg)
|
||||
}
|
||||
|
||||
pub fn (ctx mut FreeType) text_width(s string) int {
|
||||
pub fn (mut ctx FreeType) text_width(s string) int {
|
||||
x, _ := ctx.text_size(s)
|
||||
return x
|
||||
}
|
||||
|
||||
pub fn (ctx mut FreeType) text_height(s string) int {
|
||||
pub fn (mut ctx FreeType) text_height(s string) int {
|
||||
_, y := ctx.text_size(s)
|
||||
return y
|
||||
}
|
||||
|
||||
pub fn (ctx mut FreeType) text_size(s string) (int, int) {
|
||||
//t := time.ticks()
|
||||
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)
|
||||
mut _rune := ''
|
||||
mut rune_ := ''
|
||||
mut ch := Character{}
|
||||
for i in 0..utext.len {
|
||||
_rune = utext.at(i)
|
||||
for i in 0 .. utext.len {
|
||||
rune_ = utext.at(i)
|
||||
ch = Character{}
|
||||
mut found := false
|
||||
if _rune.len == 1 {
|
||||
idx := _rune[0]
|
||||
if rune_.len == 1 {
|
||||
idx := rune_[0]
|
||||
if idx < 0 || idx >= ctx.chars.len {
|
||||
println('BADE RUNE $_rune')
|
||||
println('BADE RUNE $rune_')
|
||||
continue
|
||||
}
|
||||
found = true
|
||||
ch = ctx.chars[_rune[0]]
|
||||
}
|
||||
else if _rune.len > 1 {
|
||||
ch = ctx.chars[rune_[0]]
|
||||
} else if rune_.len > 1 {
|
||||
// TODO O(1) use map
|
||||
for j in 0..ctx.utf_runes.len {
|
||||
for j in 0 .. ctx.utf_runes.len {
|
||||
rune_j := ctx.utf_runes[j]
|
||||
if rune_j==_rune {
|
||||
if rune_j == rune_ {
|
||||
ch = ctx.utf_chars[j]
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found && _rune.len > 0 && _rune[0] > 32 {
|
||||
ch = ft_load_char(ctx.face, _rune.utf32_code())
|
||||
ctx.utf_runes << _rune
|
||||
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
|
||||
@ -442,12 +431,12 @@ pub fn (ctx mut FreeType) text_size(s string) (int, int) {
|
||||
maxy = ch.vertical_advance_px
|
||||
}
|
||||
}
|
||||
//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} ')
|
||||
// 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
|
||||
}
|
||||
|
||||
@ -456,7 +445,6 @@ pub fn (f FT_Face) str() string {
|
||||
return 'FT_Face{ style_name: ${ptr_str(f.style_name)} family_name: ${ptr_str(f.family_name)} }'
|
||||
}
|
||||
*/
|
||||
|
||||
pub fn (ac []Character) str() string {
|
||||
mut res := []string{}
|
||||
for c in ac {
|
||||
|
Reference in New Issue
Block a user