2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* dynamic_font.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:11:45 +00:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-07 22:45:00 +00:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2016-06-18 12:46:12 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2016-05-29 14:37:26 +00:00
|
|
|
#ifdef FREETYPE_ENABLED
|
2016-05-02 02:12:30 +00:00
|
|
|
#include "dynamic_font.h"
|
|
|
|
#include "os/file_access.h"
|
2017-02-20 12:52:19 +00:00
|
|
|
#include "os/os.h"
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool DynamicFontData::CacheID::operator<(CacheID right) const {
|
2016-08-02 14:00:32 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (size < right.size)
|
2016-08-02 14:00:32 +00:00
|
|
|
return true;
|
|
|
|
if (mipmaps != right.mipmaps)
|
|
|
|
return right.mipmaps;
|
|
|
|
if (filter != right.filter)
|
|
|
|
return right.filter;
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Ref<DynamicFontAtSize> DynamicFontData::_get_dynamic_font_at_size(CacheID p_id) {
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2016-08-02 14:00:32 +00:00
|
|
|
if (size_cache.has(p_id)) {
|
2017-03-18 23:36:26 +00:00
|
|
|
return Ref<DynamicFontAtSize>(size_cache[p_id]);
|
2016-05-02 02:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ref<DynamicFontAtSize> dfas;
|
2016-05-29 14:37:26 +00:00
|
|
|
|
2016-05-02 02:12:30 +00:00
|
|
|
dfas.instance();
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
dfas->font = Ref<DynamicFontData>(this);
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
size_cache[p_id] = dfas.ptr();
|
|
|
|
dfas->id = p_id;
|
2016-05-29 14:37:26 +00:00
|
|
|
dfas->_load();
|
|
|
|
|
|
|
|
return dfas;
|
|
|
|
}
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void DynamicFontData::set_font_ptr(const uint8_t *p_font_mem, int p_font_mem_size) {
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
font_mem = p_font_mem;
|
|
|
|
font_mem_size = p_font_mem_size;
|
2016-05-29 14:37:26 +00:00
|
|
|
}
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void DynamicFontData::set_font_path(const String &p_path) {
|
2016-05-29 14:37:26 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
font_path = p_path;
|
2016-05-29 14:37:26 +00:00
|
|
|
}
|
|
|
|
|
2016-06-18 22:03:53 +00:00
|
|
|
String DynamicFontData::get_font_path() const {
|
|
|
|
return font_path;
|
|
|
|
}
|
|
|
|
|
2016-05-29 14:37:26 +00:00
|
|
|
void DynamicFontData::set_force_autohinter(bool p_force) {
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
force_autohinter = p_force;
|
2016-05-02 02:12:30 +00:00
|
|
|
}
|
|
|
|
|
2016-06-18 22:03:53 +00:00
|
|
|
void DynamicFontData::_bind_methods() {
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_font_path", "path"), &DynamicFontData::set_font_path);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_font_path"), &DynamicFontData::get_font_path);
|
2016-06-18 22:03:53 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "font_path", PROPERTY_HINT_FILE, "*.ttf,*.otf"), _SCS("set_font_path"), _SCS("get_font_path"));
|
2016-06-18 22:03:53 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
DynamicFontData::DynamicFontData() {
|
2016-05-29 14:37:26 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
force_autohinter = false;
|
|
|
|
font_mem = NULL;
|
|
|
|
font_mem_size = 0;
|
2016-05-02 02:12:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
DynamicFontData::~DynamicFontData() {
|
2016-05-02 02:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////
|
2017-03-18 23:36:26 +00:00
|
|
|
HashMap<String, Vector<uint8_t> > DynamicFontAtSize::_fontdata;
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2016-05-29 14:37:26 +00:00
|
|
|
Error DynamicFontAtSize::_load() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int error = FT_Init_FreeType(&library);
|
2016-05-29 14:37:26 +00:00
|
|
|
|
|
|
|
ERR_EXPLAIN(TTR("Error initializing FreeType."));
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND_V(error != 0, ERR_CANT_CREATE);
|
2016-05-29 14:37:26 +00:00
|
|
|
|
2017-02-20 12:52:19 +00:00
|
|
|
// FT_OPEN_STREAM is extremely slow only on Android.
|
2017-03-18 23:36:26 +00:00
|
|
|
if (OS::get_singleton()->get_name() == "Android" && font->font_mem == NULL && font->font_path != String()) {
|
2017-02-20 12:52:19 +00:00
|
|
|
// cache font only once for each font->font_path
|
|
|
|
if (_fontdata.has(font->font_path)) {
|
|
|
|
|
|
|
|
font->set_font_ptr(_fontdata[font->font_path].ptr(), _fontdata[font->font_path].size());
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
FileAccess *f = FileAccess::open(font->font_path, FileAccess::READ);
|
|
|
|
ERR_FAIL_COND_V(!f, ERR_CANT_OPEN);
|
2017-02-20 12:52:19 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
size_t len = f->get_len();
|
|
|
|
_fontdata[font->font_path] = Vector<uint8_t>();
|
|
|
|
Vector<uint8_t> &fontdata = _fontdata[font->font_path];
|
2017-02-20 12:52:19 +00:00
|
|
|
fontdata.resize(len);
|
|
|
|
f->get_buffer(fontdata.ptr(), len);
|
|
|
|
font->set_font_ptr(fontdata.ptr(), len);
|
|
|
|
f->close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (font->font_mem == NULL && font->font_path != String()) {
|
2016-05-29 14:37:26 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
FileAccess *f = FileAccess::open(font->font_path, FileAccess::READ);
|
|
|
|
ERR_FAIL_COND_V(!f, ERR_CANT_OPEN);
|
2016-05-29 14:37:26 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
memset(&stream, 0, sizeof(FT_StreamRec));
|
|
|
|
stream.base = NULL;
|
|
|
|
stream.size = f->get_len();
|
|
|
|
stream.pos = 0;
|
|
|
|
stream.descriptor.pointer = f;
|
|
|
|
stream.read = _ft_stream_io;
|
|
|
|
stream.close = _ft_stream_close;
|
2016-05-29 14:37:26 +00:00
|
|
|
|
|
|
|
FT_Open_Args fargs;
|
2017-03-18 23:36:26 +00:00
|
|
|
memset(&fargs, 0, sizeof(FT_Open_Args));
|
|
|
|
fargs.flags = FT_OPEN_STREAM;
|
|
|
|
fargs.stream = &stream;
|
|
|
|
error = FT_Open_Face(library, &fargs, 0, &face);
|
2016-05-29 14:37:26 +00:00
|
|
|
} else if (font->font_mem) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
memset(&stream, 0, sizeof(FT_StreamRec));
|
|
|
|
stream.base = (unsigned char *)font->font_mem;
|
|
|
|
stream.size = font->font_mem_size;
|
|
|
|
stream.pos = 0;
|
2016-05-29 14:37:26 +00:00
|
|
|
|
|
|
|
FT_Open_Args fargs;
|
2017-03-18 23:36:26 +00:00
|
|
|
memset(&fargs, 0, sizeof(FT_Open_Args));
|
|
|
|
fargs.memory_base = (unsigned char *)font->font_mem;
|
|
|
|
fargs.memory_size = font->font_mem_size;
|
|
|
|
fargs.flags = FT_OPEN_MEMORY;
|
|
|
|
fargs.stream = &stream;
|
|
|
|
error = FT_Open_Face(library, &fargs, 0, &face);
|
2016-05-29 14:37:26 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
ERR_EXPLAIN("DynamicFont uninitialized");
|
|
|
|
ERR_FAIL_V(ERR_UNCONFIGURED);
|
|
|
|
}
|
|
|
|
|
|
|
|
//error = FT_New_Face( library, src_path.utf8().get_data(),0,&face );
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (error == FT_Err_Unknown_File_Format) {
|
2016-05-29 14:37:26 +00:00
|
|
|
ERR_EXPLAIN(TTR("Unknown font format."));
|
2017-03-18 23:36:26 +00:00
|
|
|
FT_Done_FreeType(library);
|
2016-05-29 14:37:26 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
} else if (error) {
|
2016-05-29 14:37:26 +00:00
|
|
|
|
|
|
|
ERR_EXPLAIN(TTR("Error loading font."));
|
2017-03-18 23:36:26 +00:00
|
|
|
FT_Done_FreeType(library);
|
2016-05-29 14:37:26 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND_V(error, ERR_FILE_CANT_OPEN);
|
2016-05-29 14:37:26 +00:00
|
|
|
|
|
|
|
/*error = FT_Set_Char_Size(face,0,64*size,512,512);
|
|
|
|
|
|
|
|
if ( error ) {
|
|
|
|
FT_Done_FreeType( library );
|
|
|
|
ERR_EXPLAIN(TTR("Invalid font size."));
|
|
|
|
ERR_FAIL_COND_V( error, ERR_INVALID_PARAMETER );
|
|
|
|
}*/
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
error = FT_Set_Pixel_Sizes(face, 0, id.size);
|
2016-05-29 14:37:26 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ascent = face->size->metrics.ascender >> 6;
|
|
|
|
descent = -face->size->metrics.descender >> 6;
|
|
|
|
linegap = 0;
|
|
|
|
texture_flags = 0;
|
2016-08-02 14:00:32 +00:00
|
|
|
if (id.mipmaps)
|
2017-03-18 23:36:26 +00:00
|
|
|
texture_flags |= Texture::FLAG_MIPMAPS;
|
2016-08-02 14:00:32 +00:00
|
|
|
if (id.filter)
|
2017-03-18 23:36:26 +00:00
|
|
|
texture_flags |= Texture::FLAG_FILTER;
|
2016-05-29 14:37:26 +00:00
|
|
|
|
|
|
|
//print_line("ASCENT: "+itos(ascent)+" descent "+itos(descent)+" hinted: "+itos(face->face_flags&FT_FACE_FLAG_HINTER));
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
valid = true;
|
2016-05-29 14:37:26 +00:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2016-05-02 02:12:30 +00:00
|
|
|
float DynamicFontAtSize::get_height() const {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return ascent + descent;
|
2016-05-02 02:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float DynamicFontAtSize::get_ascent() const {
|
|
|
|
|
2016-05-29 14:37:26 +00:00
|
|
|
return ascent;
|
2016-05-02 02:12:30 +00:00
|
|
|
}
|
|
|
|
float DynamicFontAtSize::get_descent() const {
|
|
|
|
|
2016-05-29 14:37:26 +00:00
|
|
|
return descent;
|
2016-05-02 02:12:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Size2 DynamicFontAtSize::get_char_size(CharType p_char, CharType p_next, const Vector<Ref<DynamicFontAtSize> > &p_fallbacks) const {
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2016-05-29 14:37:26 +00:00
|
|
|
if (!valid)
|
2017-03-18 23:36:26 +00:00
|
|
|
return Size2(1, 1);
|
|
|
|
const_cast<DynamicFontAtSize *>(this)->_update_char(p_char);
|
2016-05-02 02:12:30 +00:00
|
|
|
|
|
|
|
const Character *c = char_map.getptr(p_char);
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND_V(!c, Size2());
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Size2 ret(0, get_height());
|
2016-05-30 22:41:32 +00:00
|
|
|
|
|
|
|
if (!c->found) {
|
|
|
|
|
|
|
|
//not found, try in fallbacks
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < p_fallbacks.size(); i++) {
|
2016-05-30 22:41:32 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
DynamicFontAtSize *fb = const_cast<DynamicFontAtSize *>(p_fallbacks[i].ptr());
|
2016-05-30 22:41:32 +00:00
|
|
|
if (!fb->valid)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
fb->_update_char(p_char);
|
|
|
|
const Character *ch = fb->char_map.getptr(p_char);
|
|
|
|
ERR_CONTINUE(!ch);
|
|
|
|
|
|
|
|
if (!ch->found)
|
|
|
|
continue;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
c = ch;
|
2016-05-30 22:41:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
//not found, try 0xFFFD to display 'not found'.
|
|
|
|
|
|
|
|
if (!c->found) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
const_cast<DynamicFontAtSize *>(this)->_update_char(0xFFFD);
|
2016-05-30 22:41:32 +00:00
|
|
|
c = char_map.getptr(0xFFFD);
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND_V(!c, Size2());
|
2016-05-30 22:41:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c->found) {
|
2017-03-18 23:36:26 +00:00
|
|
|
ret.x = c->advance;
|
2016-05-30 22:41:32 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 02:12:30 +00:00
|
|
|
if (p_next) {
|
2017-03-18 23:36:26 +00:00
|
|
|
FT_Vector delta;
|
|
|
|
FT_Get_Kerning(face, p_char, p_next, FT_KERNING_DEFAULT, &delta);
|
2016-05-30 22:41:32 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (delta.x == 0) {
|
|
|
|
for (int i = 0; i < p_fallbacks.size(); i++) {
|
2016-05-30 22:41:32 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
DynamicFontAtSize *fb = const_cast<DynamicFontAtSize *>(p_fallbacks[i].ptr());
|
2016-05-30 22:41:32 +00:00
|
|
|
if (!fb->valid)
|
|
|
|
continue;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
FT_Get_Kerning(fb->face, p_char, p_next, FT_KERNING_DEFAULT, &delta);
|
2016-05-30 22:41:32 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (delta.x == 0)
|
2016-05-30 22:41:32 +00:00
|
|
|
continue;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ret.x += delta.x >> 6;
|
2016-05-30 22:41:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2017-03-18 23:36:26 +00:00
|
|
|
ret.x += delta.x >> 6;
|
2016-05-30 22:41:32 +00:00
|
|
|
}
|
2016-05-02 02:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void DynamicFontAtSize::set_texture_flags(uint32_t p_flags) {
|
|
|
|
|
|
|
|
texture_flags = p_flags;
|
|
|
|
for (int i = 0; i < textures.size(); i++) {
|
2016-08-02 07:01:51 +00:00
|
|
|
Ref<ImageTexture> &tex = textures[i].texture;
|
|
|
|
if (!tex.is_null())
|
|
|
|
tex->set_flags(p_flags);
|
|
|
|
}
|
|
|
|
}
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
float DynamicFontAtSize::draw_char(RID p_canvas_item, const Point2 &p_pos, CharType p_char, CharType p_next, const Color &p_modulate, const Vector<Ref<DynamicFontAtSize> > &p_fallbacks) const {
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2016-05-29 14:37:26 +00:00
|
|
|
if (!valid)
|
|
|
|
return 0;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
const_cast<DynamicFontAtSize *>(this)->_update_char(p_char);
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
const Character *c = char_map.getptr(p_char);
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
float advance = 0;
|
2016-05-30 22:41:32 +00:00
|
|
|
|
|
|
|
if (!c->found) {
|
|
|
|
|
|
|
|
//not found, try in fallbacks
|
2017-03-18 23:36:26 +00:00
|
|
|
bool used_fallback = false;
|
2016-05-30 22:41:32 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < p_fallbacks.size(); i++) {
|
2016-05-30 22:41:32 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
DynamicFontAtSize *fb = const_cast<DynamicFontAtSize *>(p_fallbacks[i].ptr());
|
2016-05-30 22:41:32 +00:00
|
|
|
if (!fb->valid)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
fb->_update_char(p_char);
|
|
|
|
const Character *ch = fb->char_map.getptr(p_char);
|
|
|
|
ERR_CONTINUE(!ch);
|
|
|
|
|
|
|
|
if (!ch->found)
|
|
|
|
continue;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Point2 cpos = p_pos;
|
|
|
|
cpos.x += ch->h_align;
|
|
|
|
cpos.y -= get_ascent();
|
|
|
|
cpos.y += ch->v_align;
|
|
|
|
ERR_FAIL_COND_V(ch->texture_idx < -1 || ch->texture_idx >= fb->textures.size(), 0);
|
|
|
|
if (ch->texture_idx != -1)
|
|
|
|
VisualServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas_item, Rect2(cpos, ch->rect.size), fb->textures[ch->texture_idx].texture->get_rid(), ch->rect, p_modulate);
|
|
|
|
advance = ch->advance;
|
|
|
|
used_fallback = true;
|
2016-05-30 22:41:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
//not found, try 0xFFFD to display 'not found'.
|
|
|
|
|
|
|
|
if (!used_fallback) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
const_cast<DynamicFontAtSize *>(this)->_update_char(0xFFFD);
|
2016-05-30 22:41:32 +00:00
|
|
|
c = char_map.getptr(0xFFFD);
|
|
|
|
}
|
2016-05-02 02:12:30 +00:00
|
|
|
}
|
|
|
|
|
2016-05-30 22:41:32 +00:00
|
|
|
if (c->found) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Point2 cpos = p_pos;
|
|
|
|
cpos.x += c->h_align;
|
|
|
|
cpos.y -= get_ascent();
|
|
|
|
cpos.y += c->v_align;
|
|
|
|
ERR_FAIL_COND_V(c->texture_idx < -1 || c->texture_idx >= textures.size(), 0);
|
|
|
|
if (c->texture_idx != -1)
|
|
|
|
VisualServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas_item, Rect2(cpos, c->rect.size), textures[c->texture_idx].texture->get_rid(), c->rect, p_modulate);
|
|
|
|
advance = c->advance;
|
2016-05-30 22:41:32 +00:00
|
|
|
//textures[c->texture_idx].texture->draw(p_canvas_item,Vector2());
|
|
|
|
}
|
2016-05-02 02:12:30 +00:00
|
|
|
|
|
|
|
if (p_next) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
FT_Vector delta;
|
|
|
|
FT_Get_Kerning(face, p_char, p_next, FT_KERNING_DEFAULT, &delta);
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (delta.x == 0) {
|
|
|
|
for (int i = 0; i < p_fallbacks.size(); i++) {
|
2016-05-30 22:41:32 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
DynamicFontAtSize *fb = const_cast<DynamicFontAtSize *>(p_fallbacks[i].ptr());
|
2016-05-30 22:41:32 +00:00
|
|
|
if (!fb->valid)
|
|
|
|
continue;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
FT_Get_Kerning(fb->face, p_char, p_next, FT_KERNING_DEFAULT, &delta);
|
2016-05-30 22:41:32 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (delta.x == 0)
|
2016-05-30 22:41:32 +00:00
|
|
|
continue;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
advance += delta.x >> 6;
|
2016-05-30 22:41:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2017-03-18 23:36:26 +00:00
|
|
|
advance += delta.x >> 6;
|
2016-05-30 22:41:32 +00:00
|
|
|
}
|
2016-05-02 02:12:30 +00:00
|
|
|
}
|
|
|
|
|
2016-05-30 22:41:32 +00:00
|
|
|
return advance;
|
2016-05-02 02:12:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
unsigned long DynamicFontAtSize::_ft_stream_io(FT_Stream stream, unsigned long offset, unsigned char *buffer, unsigned long count) {
|
2016-05-29 14:37:26 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
FileAccess *f = (FileAccess *)stream->descriptor.pointer;
|
2016-05-29 14:37:26 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (f->get_pos() != offset) {
|
2016-05-29 14:37:26 +00:00
|
|
|
f->seek(offset);
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (count == 0)
|
2016-05-29 14:37:26 +00:00
|
|
|
return 0;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return f->get_buffer(buffer, count);
|
2016-05-29 14:37:26 +00:00
|
|
|
}
|
|
|
|
void DynamicFontAtSize::_ft_stream_close(FT_Stream stream) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
FileAccess *f = (FileAccess *)stream->descriptor.pointer;
|
2016-05-29 14:37:26 +00:00
|
|
|
f->close();
|
|
|
|
memdelete(f);
|
|
|
|
}
|
|
|
|
|
2016-05-02 02:12:30 +00:00
|
|
|
void DynamicFontAtSize::_update_char(CharType p_char) {
|
|
|
|
|
|
|
|
if (char_map.has(p_char))
|
|
|
|
return;
|
|
|
|
|
2016-05-29 14:37:26 +00:00
|
|
|
_THREAD_SAFE_METHOD_
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2016-05-29 14:37:26 +00:00
|
|
|
FT_GlyphSlot slot = face->glyph;
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (FT_Get_Char_Index(face, p_char) == 0) {
|
2016-05-30 22:41:32 +00:00
|
|
|
//not found
|
|
|
|
Character ch;
|
2017-03-18 23:36:26 +00:00
|
|
|
ch.texture_idx = -1;
|
|
|
|
ch.advance = 0;
|
|
|
|
ch.h_align = 0;
|
|
|
|
ch.v_align = 0;
|
|
|
|
ch.found = false;
|
2016-05-30 22:41:32 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
char_map[p_char] = ch;
|
2016-05-30 22:41:32 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
int error = FT_Load_Char(face, p_char, FT_LOAD_RENDER | (font->force_autohinter ? FT_LOAD_FORCE_AUTOHINT : 0));
|
2016-05-29 14:37:26 +00:00
|
|
|
if (!error) {
|
2017-03-18 23:36:26 +00:00
|
|
|
error = FT_Render_Glyph(face->glyph, ft_render_mode_normal);
|
2016-05-29 14:37:26 +00:00
|
|
|
}
|
|
|
|
if (error) {
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int advance = 0;
|
2016-05-29 14:37:26 +00:00
|
|
|
//stbtt_GetCodepointHMetrics(&font->info, p_char, &advance, 0);
|
2016-05-02 02:12:30 +00:00
|
|
|
//print_line("char has no bitmap: "+itos(p_char)+" but advance is "+itos(advance*scale));
|
|
|
|
Character ch;
|
2017-03-18 23:36:26 +00:00
|
|
|
ch.texture_idx = -1;
|
|
|
|
ch.advance = advance;
|
|
|
|
ch.h_align = 0;
|
|
|
|
ch.v_align = 0;
|
|
|
|
ch.found = false;
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
char_map[p_char] = ch;
|
2016-05-02 02:12:30 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-29 14:37:26 +00:00
|
|
|
int w = slot->bitmap.width;
|
|
|
|
int h = slot->bitmap.rows;
|
2016-07-07 22:32:28 +00:00
|
|
|
//int p = slot->bitmap.pitch;
|
2017-03-18 23:36:26 +00:00
|
|
|
int yofs = slot->bitmap_top;
|
|
|
|
int xofs = slot->bitmap_left;
|
|
|
|
int advance = slot->advance.x >> 6;
|
2016-05-29 14:37:26 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int mw = w + rect_margin * 2;
|
|
|
|
int mh = h + rect_margin * 2;
|
2016-05-29 14:37:26 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (mw > 4096 || mh > 4096) {
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND(mw > 4096);
|
|
|
|
ERR_FAIL_COND(mh > 4096);
|
2016-05-02 02:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//find a texture to fit this...
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int tex_index = -1;
|
|
|
|
int tex_x = 0;
|
|
|
|
int tex_y = 0;
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < textures.size(); i++) {
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
CharTexture &ct = textures[i];
|
2016-05-02 02:12:30 +00:00
|
|
|
|
|
|
|
if (mw > ct.texture_size || mh > ct.texture_size) //too big for this texture
|
|
|
|
continue;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
tex_y = 0x7FFFFFFF;
|
|
|
|
tex_x = 0;
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int j = 0; j < ct.texture_size - mw; j++) {
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int max_y = 0;
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int k = j; k < j + mw; k++) {
|
2016-05-02 02:12:30 +00:00
|
|
|
|
|
|
|
int y = ct.offsets[k];
|
2017-03-18 23:36:26 +00:00
|
|
|
if (y > max_y)
|
|
|
|
max_y = y;
|
2016-05-02 02:12:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (max_y < tex_y) {
|
|
|
|
tex_y = max_y;
|
|
|
|
tex_x = j;
|
2016-05-02 02:12:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (tex_y == 0x7FFFFFFF || tex_y + mh > ct.texture_size)
|
2016-05-02 02:12:30 +00:00
|
|
|
continue; //fail, could not fit it here
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
tex_index = i;
|
2016-05-02 02:12:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
// print_line("CHAR: "+String::chr(p_char)+" TEX INDEX: "+itos(tex_index)+" X: "+itos(tex_x)+" Y: "+itos(tex_y));
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (tex_index == -1) {
|
2016-05-02 02:12:30 +00:00
|
|
|
//could not find texture to fit, create one
|
2016-05-03 09:25:33 +00:00
|
|
|
tex_x = 0;
|
|
|
|
tex_y = 0;
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int texsize = MAX(id.size * 8, 256);
|
|
|
|
if (mw > texsize)
|
|
|
|
texsize = mw; //special case, adapt to it?
|
|
|
|
if (mh > texsize)
|
|
|
|
texsize = mh; //special case, adapt to it?
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-08-18 15:10:21 +00:00
|
|
|
texsize = next_power_of_2(texsize);
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
texsize = MIN(texsize, 4096);
|
2016-05-02 02:12:30 +00:00
|
|
|
|
|
|
|
CharTexture tex;
|
2017-03-18 23:36:26 +00:00
|
|
|
tex.texture_size = texsize;
|
|
|
|
tex.imgdata.resize(texsize * texsize * 2); //grayscale alpha
|
2016-05-02 02:12:30 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
//zero texture
|
|
|
|
DVector<uint8_t>::Write w = tex.imgdata.write();
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND(texsize * texsize * 2 > tex.imgdata.size());
|
|
|
|
for (int i = 0; i < texsize * texsize * 2; i++) {
|
|
|
|
w[i] = 0;
|
2016-05-02 02:12:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tex.offsets.resize(texsize);
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < texsize; i++) //zero offsets
|
|
|
|
tex.offsets[i] = 0;
|
2016-05-02 02:12:30 +00:00
|
|
|
|
|
|
|
textures.push_back(tex);
|
2017-03-18 23:36:26 +00:00
|
|
|
tex_index = textures.size() - 1;
|
2016-05-02 02:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//fit character in char texture
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
CharTexture &tex = textures[tex_index];
|
2016-05-02 02:12:30 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
DVector<uint8_t>::Write wr = tex.imgdata.write();
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < h; i++) {
|
|
|
|
for (int j = 0; j < w; j++) {
|
2016-05-29 14:37:26 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int ofs = ((i + tex_y + rect_margin) * tex.texture_size + j + tex_x + rect_margin) * 2;
|
2016-05-03 09:25:33 +00:00
|
|
|
ERR_FAIL_COND(ofs >= tex.imgdata.size());
|
2017-03-18 23:36:26 +00:00
|
|
|
wr[ofs + 0] = 255; //grayscale as 1
|
|
|
|
wr[ofs + 1] = slot->bitmap.buffer[i * slot->bitmap.width + j];
|
2016-05-02 02:12:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//blit to image and texture
|
|
|
|
{
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Image img(tex.texture_size, tex.texture_size, 0, Image::FORMAT_GRAYSCALE_ALPHA, tex.imgdata);
|
2016-05-02 02:12:30 +00:00
|
|
|
|
|
|
|
if (tex.texture.is_null()) {
|
|
|
|
tex.texture.instance();
|
2017-03-18 23:36:26 +00:00
|
|
|
tex.texture->create_from_image(img, Texture::FLAG_VIDEO_SURFACE | texture_flags);
|
2016-05-02 02:12:30 +00:00
|
|
|
} else {
|
|
|
|
tex.texture->set_data(img); //update
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update height array
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int k = tex_x; k < tex_x + mw; k++) {
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
tex.offsets[k] = tex_y + mh;
|
2016-05-02 02:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Character chr;
|
2017-03-18 23:36:26 +00:00
|
|
|
chr.h_align = xofs;
|
|
|
|
chr.v_align = ascent - yofs; // + ascent - descent;
|
|
|
|
chr.advance = advance;
|
|
|
|
chr.texture_idx = tex_index;
|
|
|
|
chr.found = true;
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
chr.rect = Rect2(tex_x + rect_margin, tex_y + rect_margin, w, h);
|
2016-05-02 02:12:30 +00:00
|
|
|
|
|
|
|
//print_line("CHAR: "+String::chr(p_char)+" TEX INDEX: "+itos(tex_index)+" RECT: "+chr.rect+" X OFS: "+itos(xofs)+" Y OFS: "+itos(yofs));
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
char_map[p_char] = chr;
|
2016-05-02 02:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DynamicFontAtSize::DynamicFontAtSize() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
valid = false;
|
|
|
|
rect_margin = 1;
|
|
|
|
ascent = 1;
|
|
|
|
descent = 1;
|
|
|
|
linegap = 1;
|
|
|
|
texture_flags = 0;
|
2016-05-02 02:12:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
DynamicFontAtSize::~DynamicFontAtSize() {
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2016-05-29 14:37:26 +00:00
|
|
|
if (valid) {
|
2017-03-18 23:36:26 +00:00
|
|
|
FT_Done_FreeType(library);
|
2016-08-02 14:00:32 +00:00
|
|
|
font->size_cache.erase(id);
|
2016-05-29 14:37:26 +00:00
|
|
|
}
|
2016-05-02 02:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void DynamicFont::_reload_cache() {
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND(cache_id.size < 1);
|
2016-08-02 14:00:32 +00:00
|
|
|
if (!data.is_valid())
|
|
|
|
return;
|
2017-03-18 23:36:26 +00:00
|
|
|
data_at_size = data->_get_dynamic_font_at_size(cache_id);
|
|
|
|
for (int i = 0; i < fallbacks.size(); i++) {
|
|
|
|
fallback_data_at_size[i] = fallbacks[i]->_get_dynamic_font_at_size(cache_id);
|
2016-08-02 14:00:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
emit_changed();
|
|
|
|
_change_notify();
|
|
|
|
}
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void DynamicFont::set_font_data(const Ref<DynamicFontData> &p_data) {
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
data = p_data;
|
2016-06-14 01:24:11 +00:00
|
|
|
if (data.is_valid())
|
2017-03-18 23:36:26 +00:00
|
|
|
data_at_size = data->_get_dynamic_font_at_size(cache_id);
|
2016-06-14 01:24:11 +00:00
|
|
|
else
|
2017-03-18 23:36:26 +00:00
|
|
|
data_at_size = Ref<DynamicFontAtSize>();
|
2016-06-14 01:24:11 +00:00
|
|
|
|
2016-06-01 01:28:27 +00:00
|
|
|
emit_changed();
|
2016-05-02 02:12:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Ref<DynamicFontData> DynamicFont::get_font_data() const {
|
2016-05-02 02:12:30 +00:00
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void DynamicFont::set_size(int p_size) {
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (cache_id.size == p_size)
|
2016-05-02 02:12:30 +00:00
|
|
|
return;
|
2017-03-18 23:36:26 +00:00
|
|
|
cache_id.size = p_size;
|
2016-08-02 14:00:32 +00:00
|
|
|
_reload_cache();
|
2016-05-02 02:12:30 +00:00
|
|
|
}
|
2016-08-02 07:01:51 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int DynamicFont::get_size() const {
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2016-08-02 14:00:32 +00:00
|
|
|
return cache_id.size;
|
2016-08-02 07:01:51 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool DynamicFont::get_use_mipmaps() const {
|
2016-08-02 07:01:51 +00:00
|
|
|
|
2016-08-02 14:00:32 +00:00
|
|
|
return cache_id.mipmaps;
|
2016-08-02 07:01:51 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void DynamicFont::set_use_mipmaps(bool p_enable) {
|
2016-08-02 07:01:51 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (cache_id.mipmaps == p_enable)
|
2016-08-02 07:01:51 +00:00
|
|
|
return;
|
2017-03-18 23:36:26 +00:00
|
|
|
cache_id.mipmaps = p_enable;
|
2016-08-02 14:00:32 +00:00
|
|
|
_reload_cache();
|
2016-08-02 07:01:51 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool DynamicFont::get_use_filter() const {
|
2016-08-02 07:01:51 +00:00
|
|
|
|
2016-08-02 14:00:32 +00:00
|
|
|
return cache_id.filter;
|
2016-08-02 07:01:51 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void DynamicFont::set_use_filter(bool p_enable) {
|
2016-08-02 07:01:51 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (cache_id.filter == p_enable)
|
2016-08-02 07:01:51 +00:00
|
|
|
return;
|
2017-03-18 23:36:26 +00:00
|
|
|
cache_id.filter = p_enable;
|
2016-08-02 14:00:32 +00:00
|
|
|
_reload_cache();
|
2016-08-02 07:01:51 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int DynamicFont::get_spacing(int p_type) const {
|
2016-08-02 10:05:20 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p_type == SPACING_TOP) {
|
2016-08-02 10:05:20 +00:00
|
|
|
return spacing_top;
|
2017-03-18 23:36:26 +00:00
|
|
|
} else if (p_type == SPACING_BOTTOM) {
|
2016-08-02 10:05:20 +00:00
|
|
|
return spacing_bottom;
|
2017-03-18 23:36:26 +00:00
|
|
|
} else if (p_type == SPACING_CHAR) {
|
2016-08-02 10:05:20 +00:00
|
|
|
return spacing_char;
|
2017-03-18 23:36:26 +00:00
|
|
|
} else if (p_type == SPACING_SPACE) {
|
2016-08-02 10:05:20 +00:00
|
|
|
return spacing_space;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void DynamicFont::set_spacing(int p_type, int p_value) {
|
2016-08-02 10:05:20 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p_type == SPACING_TOP) {
|
|
|
|
spacing_top = p_value;
|
|
|
|
} else if (p_type == SPACING_BOTTOM) {
|
|
|
|
spacing_bottom = p_value;
|
|
|
|
} else if (p_type == SPACING_CHAR) {
|
|
|
|
spacing_char = p_value;
|
|
|
|
} else if (p_type == SPACING_SPACE) {
|
|
|
|
spacing_space = p_value;
|
2016-08-02 10:05:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
emit_changed();
|
|
|
|
_change_notify();
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
float DynamicFont::get_height() const {
|
2016-05-02 02:12:30 +00:00
|
|
|
|
|
|
|
if (!data_at_size.is_valid())
|
|
|
|
return 1;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return data_at_size->get_height() + spacing_top + spacing_bottom;
|
2016-05-02 02:12:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
float DynamicFont::get_ascent() const {
|
2016-05-02 02:12:30 +00:00
|
|
|
|
|
|
|
if (!data_at_size.is_valid())
|
|
|
|
return 1;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return data_at_size->get_ascent() + spacing_top;
|
2016-05-02 02:12:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
float DynamicFont::get_descent() const {
|
2016-05-02 02:12:30 +00:00
|
|
|
|
|
|
|
if (!data_at_size.is_valid())
|
|
|
|
return 1;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return data_at_size->get_descent() + spacing_bottom;
|
2016-05-02 02:12:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Size2 DynamicFont::get_char_size(CharType p_char, CharType p_next) const {
|
2016-05-02 02:12:30 +00:00
|
|
|
|
|
|
|
if (!data_at_size.is_valid())
|
2017-03-18 23:36:26 +00:00
|
|
|
return Size2(1, 1);
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Size2 ret = data_at_size->get_char_size(p_char, p_next, fallback_data_at_size);
|
|
|
|
if (p_char == ' ')
|
|
|
|
ret.width += spacing_space + spacing_char;
|
2016-08-02 10:05:20 +00:00
|
|
|
else if (p_next)
|
2017-03-18 23:36:26 +00:00
|
|
|
ret.width += spacing_char;
|
2016-08-02 10:05:20 +00:00
|
|
|
|
|
|
|
return ret;
|
2016-05-02 02:12:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool DynamicFont::is_distance_field_hint() const {
|
2016-05-02 02:12:30 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
float DynamicFont::draw_char(RID p_canvas_item, const Point2 &p_pos, CharType p_char, CharType p_next, const Color &p_modulate) const {
|
2016-05-02 02:12:30 +00:00
|
|
|
|
|
|
|
if (!data_at_size.is_valid())
|
|
|
|
return 0;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return data_at_size->draw_char(p_canvas_item, p_pos, p_char, p_next, p_modulate, fallback_data_at_size) + spacing_char;
|
2016-05-30 22:41:32 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
void DynamicFont::set_fallback(int p_idx, const Ref<DynamicFontData> &p_data) {
|
2016-05-30 22:41:32 +00:00
|
|
|
|
|
|
|
ERR_FAIL_COND(p_data.is_null());
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_INDEX(p_idx, fallbacks.size());
|
|
|
|
fallbacks[p_idx] = p_data;
|
|
|
|
fallback_data_at_size[p_idx] = fallbacks[p_idx]->_get_dynamic_font_at_size(cache_id);
|
2016-05-30 22:41:32 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void DynamicFont::add_fallback(const Ref<DynamicFontData> &p_data) {
|
2016-05-30 22:41:32 +00:00
|
|
|
|
|
|
|
ERR_FAIL_COND(p_data.is_null());
|
|
|
|
fallbacks.push_back(p_data);
|
2017-03-18 23:36:26 +00:00
|
|
|
fallback_data_at_size.push_back(fallbacks[fallbacks.size() - 1]->_get_dynamic_font_at_size(cache_id)); //const..
|
2016-05-30 22:41:32 +00:00
|
|
|
|
|
|
|
_change_notify();
|
2016-06-01 01:28:27 +00:00
|
|
|
emit_changed();
|
|
|
|
_change_notify();
|
2016-05-30 22:41:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int DynamicFont::get_fallback_count() const {
|
|
|
|
return fallbacks.size();
|
|
|
|
}
|
|
|
|
Ref<DynamicFontData> DynamicFont::get_fallback(int p_idx) const {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_idx, fallbacks.size(), Ref<DynamicFontData>());
|
2016-05-30 22:41:32 +00:00
|
|
|
|
|
|
|
return fallbacks[p_idx];
|
|
|
|
}
|
|
|
|
void DynamicFont::remove_fallback(int p_idx) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_INDEX(p_idx, fallbacks.size());
|
2016-05-30 22:41:32 +00:00
|
|
|
fallbacks.remove(p_idx);
|
|
|
|
fallback_data_at_size.remove(p_idx);
|
2016-06-01 01:28:27 +00:00
|
|
|
emit_changed();
|
2016-05-30 22:41:32 +00:00
|
|
|
_change_notify();
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool DynamicFont::_set(const StringName &p_name, const Variant &p_value) {
|
2016-05-30 22:41:32 +00:00
|
|
|
|
|
|
|
String str = p_name;
|
|
|
|
if (str.begins_with("fallback/")) {
|
2017-03-18 23:36:26 +00:00
|
|
|
int idx = str.get_slicec('/', 1).to_int();
|
2016-05-30 22:41:32 +00:00
|
|
|
Ref<DynamicFontData> fd = p_value;
|
|
|
|
|
|
|
|
if (fd.is_valid()) {
|
2017-03-18 23:36:26 +00:00
|
|
|
if (idx == fallbacks.size()) {
|
2016-05-30 22:41:32 +00:00
|
|
|
add_fallback(fd);
|
|
|
|
return true;
|
2017-03-18 23:36:26 +00:00
|
|
|
} else if (idx >= 0 && idx < fallbacks.size()) {
|
|
|
|
set_fallback(idx, fd);
|
2016-05-30 22:41:32 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
} else if (idx >= 0 && idx < fallbacks.size()) {
|
2016-05-30 22:41:32 +00:00
|
|
|
remove_fallback(idx);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool DynamicFont::_get(const StringName &p_name, Variant &r_ret) const {
|
2016-05-30 22:41:32 +00:00
|
|
|
|
|
|
|
String str = p_name;
|
|
|
|
if (str.begins_with("fallback/")) {
|
2017-03-18 23:36:26 +00:00
|
|
|
int idx = str.get_slicec('/', 1).to_int();
|
2016-05-30 22:41:32 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (idx == fallbacks.size()) {
|
|
|
|
r_ret = Ref<DynamicFontData>();
|
2016-05-30 22:41:32 +00:00
|
|
|
return true;
|
2017-03-18 23:36:26 +00:00
|
|
|
} else if (idx >= 0 && idx < fallbacks.size()) {
|
|
|
|
r_ret = get_fallback(idx);
|
2016-05-30 22:41:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
void DynamicFont::_get_property_list(List<PropertyInfo> *p_list) const {
|
2016-05-30 22:41:32 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < fallbacks.size(); i++) {
|
|
|
|
p_list->push_back(PropertyInfo(Variant::OBJECT, "fallback/" + itos(i), PROPERTY_HINT_RESOURCE_TYPE, "DynamicFontData"));
|
2016-05-30 22:41:32 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
p_list->push_back(PropertyInfo(Variant::OBJECT, "fallback/" + itos(fallbacks.size()), PROPERTY_HINT_RESOURCE_TYPE, "DynamicFontData"));
|
2016-05-30 22:41:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DynamicFont::_bind_methods() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_font_data", "data:DynamicFontData"), &DynamicFont::set_font_data);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_font_data:DynamicFontData"), &DynamicFont::get_font_data);
|
2016-05-30 22:41:32 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_size", "data"), &DynamicFont::set_size);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_size"), &DynamicFont::get_size);
|
2016-08-02 07:01:51 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_use_mipmaps", "enable"), &DynamicFont::set_use_mipmaps);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_use_mipmaps"), &DynamicFont::get_use_mipmaps);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_use_filter", "enable"), &DynamicFont::set_use_filter);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_use_filter"), &DynamicFont::get_use_filter);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_spacing", "type", "value"), &DynamicFont::set_spacing);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_spacing", "type"), &DynamicFont::get_spacing);
|
2016-05-30 22:41:32 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("add_fallback", "data:DynamicFontData"), &DynamicFont::add_fallback);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_fallback", "idx", "data:DynamicFontData"), &DynamicFont::set_fallback);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_fallback:DynamicFontData", "idx"), &DynamicFont::get_fallback);
|
|
|
|
ObjectTypeDB::bind_method(_MD("remove_fallback", "idx"), &DynamicFont::remove_fallback);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_fallback_count"), &DynamicFont::get_fallback_count);
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "font/size"), _SCS("set_size"), _SCS("get_size"));
|
|
|
|
ADD_PROPERTYINZ(PropertyInfo(Variant::INT, "extra_spacing/top"), _SCS("set_spacing"), _SCS("get_spacing"), SPACING_TOP);
|
|
|
|
ADD_PROPERTYINZ(PropertyInfo(Variant::INT, "extra_spacing/bottom"), _SCS("set_spacing"), _SCS("get_spacing"), SPACING_BOTTOM);
|
|
|
|
ADD_PROPERTYINZ(PropertyInfo(Variant::INT, "extra_spacing/char"), _SCS("set_spacing"), _SCS("get_spacing"), SPACING_CHAR);
|
|
|
|
ADD_PROPERTYINZ(PropertyInfo(Variant::INT, "extra_spacing/space"), _SCS("set_spacing"), _SCS("get_spacing"), SPACING_SPACE);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "font/use_mipmaps"), _SCS("set_use_mipmaps"), _SCS("get_use_mipmaps"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "font/use_filter"), _SCS("set_use_filter"), _SCS("get_use_filter"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "font/font", PROPERTY_HINT_RESOURCE_TYPE, "DynamicFontData"), _SCS("set_font_data"), _SCS("get_font_data"));
|
2016-08-02 10:05:20 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
BIND_CONSTANT(SPACING_TOP);
|
|
|
|
BIND_CONSTANT(SPACING_BOTTOM);
|
|
|
|
BIND_CONSTANT(SPACING_CHAR);
|
|
|
|
BIND_CONSTANT(SPACING_SPACE);
|
2016-05-02 02:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DynamicFont::DynamicFont() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
spacing_top = 0;
|
|
|
|
spacing_bottom = 0;
|
|
|
|
spacing_char = 0;
|
|
|
|
spacing_space = 0;
|
2016-05-02 02:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DynamicFont::~DynamicFont() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
RES ResourceFormatLoaderDynamicFont::load(const String &p_path, const String &p_original_path, Error *r_error) {
|
2016-05-02 02:12:30 +00:00
|
|
|
|
|
|
|
if (r_error)
|
2017-03-18 23:36:26 +00:00
|
|
|
*r_error = ERR_FILE_CANT_OPEN;
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2016-05-29 14:37:26 +00:00
|
|
|
Ref<DynamicFontData> dfont;
|
2017-01-14 17:03:38 +00:00
|
|
|
dfont.instance();
|
2016-05-29 14:37:26 +00:00
|
|
|
dfont->set_font_path(p_path);
|
2016-05-02 02:12:30 +00:00
|
|
|
|
|
|
|
if (r_error)
|
2017-03-18 23:36:26 +00:00
|
|
|
*r_error = OK;
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2016-05-29 14:37:26 +00:00
|
|
|
return dfont;
|
2016-05-02 02:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceFormatLoaderDynamicFont::get_recognized_extensions(List<String> *p_extensions) const {
|
|
|
|
|
|
|
|
p_extensions->push_back("ttf");
|
2016-05-29 14:37:26 +00:00
|
|
|
p_extensions->push_back("otf");
|
2016-05-02 02:12:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool ResourceFormatLoaderDynamicFont::handles_type(const String &p_type) const {
|
2016-05-02 02:12:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return (p_type == "DynamicFontData");
|
2016-05-02 02:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String ResourceFormatLoaderDynamicFont::get_resource_type(const String &p_path) const {
|
|
|
|
|
|
|
|
String el = p_path.extension().to_lower();
|
2017-03-18 23:36:26 +00:00
|
|
|
if (el == "ttf" || el == "otf")
|
2016-05-02 02:12:30 +00:00
|
|
|
return "DynamicFontData";
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2016-05-29 14:37:26 +00:00
|
|
|
#endif
|