2017-03-05 15:47:28 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* dynamic_font_stb.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2017-03-05 15:47:28 +01:00
|
|
|
/*************************************************************************/
|
2018-01-01 14:40:08 +01:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
|
2017-03-05 15:47:28 +01: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. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2016-05-29 16:37:26 +02:00
|
|
|
#include "dynamic_font_stb.h"
|
|
|
|
|
|
|
|
#ifndef FREETYPE_ENABLED
|
|
|
|
|
|
|
|
#define STB_TRUETYPE_IMPLEMENTATION
|
|
|
|
#include "os/file_access.h"
|
|
|
|
|
|
|
|
void DynamicFontData::lock() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
fr = font_data.read();
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (fr.ptr() != last_data_ptr) {
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
last_data_ptr = fr.ptr();
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
if (!stbtt_InitFont(&info, last_data_ptr, 0)) {
|
2017-03-05 16:44:50 +01:00
|
|
|
valid = false;
|
2016-05-29 16:37:26 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
valid = true;
|
2016-05-29 16:37:26 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
last_data_ptr = fr.ptr();
|
2016-05-29 16:37:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DynamicFontData::unlock() {
|
|
|
|
|
2017-01-07 22:25:37 +01:00
|
|
|
fr = PoolVector<uint8_t>::Read();
|
2016-05-29 16:37:26 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void DynamicFontData::set_font_data(const PoolVector<uint8_t> &p_font) {
|
2016-05-29 16:37:26 +02:00
|
|
|
//clear caches and stuff
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND(font_data.size());
|
|
|
|
font_data = p_font;
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
lock();
|
|
|
|
|
|
|
|
if (valid) {
|
|
|
|
stbtt_GetFontVMetrics(&info, &ascent, &descent, &linegap);
|
2017-03-05 16:44:50 +01:00
|
|
|
descent = -descent + linegap;
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 32; i < 1024; i++) {
|
|
|
|
for (int j = 32; j < 1024; j++) {
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int kern = stbtt_GetCodepointKernAdvance(&info, i, j);
|
|
|
|
if (kern != 0) {
|
2016-05-29 16:37:26 +02:00
|
|
|
KerningPairKey kpk;
|
2017-03-05 16:44:50 +01:00
|
|
|
kpk.A = i;
|
|
|
|
kpk.B = j;
|
|
|
|
kerning_map[kpk] = kern;
|
2016-05-29 16:37:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unlock();
|
|
|
|
//clear existing stuff
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!valid);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<DynamicFontAtSize> DynamicFontData::_get_dynamic_font_at_size(int p_size) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(!valid, Ref<DynamicFontAtSize>());
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
if (size_cache.has(p_size)) {
|
2017-03-05 16:44:50 +01:00
|
|
|
return Ref<DynamicFontAtSize>(size_cache[p_size]);
|
2016-05-29 16:37:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Ref<DynamicFontAtSize> dfas;
|
|
|
|
dfas.instance();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
dfas->font = Ref<DynamicFontData>(this);
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
size_cache[p_size] = dfas.ptr();
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
dfas->size = p_size;
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
lock();
|
|
|
|
|
|
|
|
dfas->scale = stbtt_ScaleForPixelHeight(&info, p_size);
|
|
|
|
|
|
|
|
unlock();
|
|
|
|
|
|
|
|
return dfas;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
DynamicFontData::DynamicFontData() {
|
|
|
|
last_data_ptr = NULL;
|
|
|
|
valid = false;
|
2016-05-29 16:37:26 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
DynamicFontData::~DynamicFontData() {
|
2016-05-29 16:37:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////
|
|
|
|
|
|
|
|
float DynamicFontAtSize::get_height() const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return (font->ascent + font->descent) * scale;
|
2016-05-29 16:37:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
float DynamicFontAtSize::get_ascent() const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return font->ascent * scale;
|
2016-05-29 16:37:26 +02:00
|
|
|
}
|
|
|
|
float DynamicFontAtSize::get_descent() const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return font->descent * scale;
|
2016-05-29 16:37:26 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Size2 DynamicFontAtSize::get_char_size(CharType p_char, CharType p_next) const {
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const_cast<DynamicFontAtSize *>(this)->_update_char(p_char);
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
const Character *c = char_map.getptr(p_char);
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(!c, Size2());
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Size2 ret(c->advance, get_height());
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
if (p_next) {
|
|
|
|
DynamicFontData::KerningPairKey kpk;
|
2017-03-05 16:44:50 +01:00
|
|
|
kpk.A = p_char;
|
|
|
|
kpk.B = p_next;
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const Map<DynamicFontData::KerningPairKey, int>::Element *K = font->kerning_map.find(kpk);
|
2016-05-29 16:37:26 +02:00
|
|
|
if (K) {
|
2017-03-05 16:44:50 +01:00
|
|
|
ret.x += K->get() * scale;
|
2016-05-29 16:37:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-03-17 09:44:34 +01:00
|
|
|
float DynamicFontAtSize::draw_char(RID p_canvas_item, const Point2 &p_pos, CharType p_char, CharType p_next, const Color &p_modulate, bool p_outline) const {
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const_cast<DynamicFontAtSize *>(this)->_update_char(p_char);
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const Character *c = char_map.getptr(p_char);
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
if (!c) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-17 09:44:34 +01:00
|
|
|
if (!p_outline) {
|
|
|
|
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);
|
|
|
|
}
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
//textures[c->texture_idx].texture->draw(p_canvas_item,Vector2());
|
|
|
|
|
|
|
|
float ret = c->advance;
|
|
|
|
if (p_next) {
|
|
|
|
DynamicFontData::KerningPairKey kpk;
|
2017-03-05 16:44:50 +01:00
|
|
|
kpk.A = p_char;
|
|
|
|
kpk.B = p_next;
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const Map<DynamicFontData::KerningPairKey, int>::Element *K = font->kerning_map.find(kpk);
|
2016-05-29 16:37:26 +02:00
|
|
|
if (K) {
|
2017-03-05 16:44:50 +01:00
|
|
|
ret += K->get() * scale;
|
2016-05-29 16:37:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DynamicFontAtSize::_update_char(CharType p_char) {
|
|
|
|
|
|
|
|
if (char_map.has(p_char))
|
|
|
|
return;
|
|
|
|
|
|
|
|
font->lock();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int w, h, xofs, yofs;
|
|
|
|
unsigned char *cpbitmap = stbtt_GetCodepointBitmap(&font->info, scale, scale, p_char, &w, &h, &xofs, &yofs);
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
if (!cpbitmap) {
|
|
|
|
//no glyph
|
|
|
|
|
|
|
|
int advance;
|
|
|
|
stbtt_GetCodepointHMetrics(&font->info, p_char, &advance, 0);
|
|
|
|
//print_line("char has no bitmap: "+itos(p_char)+" but advance is "+itos(advance*scale));
|
|
|
|
Character ch;
|
2017-03-05 16:44:50 +01:00
|
|
|
ch.texture_idx = -1;
|
|
|
|
ch.advance = advance * scale;
|
|
|
|
ch.h_align = 0;
|
|
|
|
ch.v_align = 0;
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
char_map[p_char] = ch;
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
font->unlock();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int mw = w + rect_margin * 2;
|
|
|
|
int mh = h + rect_margin * 2;
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (mw > 4096 || mh > 4096) {
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
stbtt_FreeBitmap(cpbitmap, NULL);
|
2016-05-29 16:37:26 +02:00
|
|
|
font->unlock();
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND(mw > 4096);
|
|
|
|
ERR_FAIL_COND(mh > 4096);
|
2016-05-29 16:37:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//find a texture to fit this...
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int tex_index = -1;
|
|
|
|
int tex_x = 0;
|
|
|
|
int tex_y = 0;
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < textures.size(); i++) {
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
CharTexture &ct = textures[i];
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
if (mw > ct.texture_size || mh > ct.texture_size) //too big for this texture
|
|
|
|
continue;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
tex_y = 0x7FFFFFFF;
|
|
|
|
tex_x = 0;
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int j = 0; j < ct.texture_size - mw; j++) {
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int max_y = 0;
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int k = j; k < j + mw; k++) {
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
int y = ct.offsets[k];
|
2017-03-05 16:44:50 +01:00
|
|
|
if (y > max_y)
|
|
|
|
max_y = y;
|
2016-05-29 16:37:26 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (max_y < tex_y) {
|
|
|
|
tex_y = max_y;
|
|
|
|
tex_x = j;
|
2016-05-29 16:37:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (tex_y == 0x7FFFFFFF || tex_y + mh > ct.texture_size)
|
2016-05-29 16:37:26 +02:00
|
|
|
continue; //fail, could not fit it here
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
tex_index = i;
|
2016-05-29 16:37:26 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-01-14 12:26:56 +01:00
|
|
|
//print_line("CHAR: "+String::chr(p_char)+" TEX INDEX: "+itos(tex_index)+" X: "+itos(tex_x)+" Y: "+itos(tex_y));
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (tex_index == -1) {
|
2016-05-29 16:37:26 +02:00
|
|
|
//could not find texture to fit, create one
|
|
|
|
tex_x = 0;
|
|
|
|
tex_y = 0;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int texsize = MAX(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-29 16:37:26 +02:00
|
|
|
|
2017-08-17 23:35:55 +02:00
|
|
|
texsize = next_power_of_2(texsize);
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
texsize = MIN(texsize, 4096);
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
CharTexture tex;
|
2017-03-05 16:44:50 +01:00
|
|
|
tex.texture_size = texsize;
|
|
|
|
tex.imgdata.resize(texsize * texsize * 2); //grayscale alpha
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
//zero texture
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<uint8_t>::Write w = tex.imgdata.write();
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND(texsize * texsize * 2 > tex.imgdata.size());
|
|
|
|
for (int i = 0; i < texsize * texsize * 2; i++) {
|
|
|
|
w[i] = 0;
|
2016-05-29 16:37:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tex.offsets.resize(texsize);
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < texsize; i++) //zero offsets
|
|
|
|
tex.offsets[i] = 0;
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
textures.push_back(tex);
|
2017-03-05 16:44:50 +01:00
|
|
|
tex_index = textures.size() - 1;
|
2016-05-29 16:37:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//fit character in char texture
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
CharTexture &tex = textures[tex_index];
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
{
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<uint8_t>::Write wr = tex.imgdata.write();
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < h; i++) {
|
|
|
|
for (int j = 0; j < w; j++) {
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int ofs = ((i + tex_y + rect_margin) * tex.texture_size + j + tex_x + rect_margin) * 2;
|
2016-05-29 16:37:26 +02:00
|
|
|
ERR_FAIL_COND(ofs >= tex.imgdata.size());
|
2017-03-05 16:44:50 +01:00
|
|
|
wr[ofs + 0] = 255; //grayscale as 1
|
|
|
|
wr[ofs + 1] = cpbitmap[i * w + j]; //alpha as 0
|
2016-05-29 16:37:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//blit to image and texture
|
|
|
|
{
|
2017-11-12 11:45:29 +01:00
|
|
|
Ref<Image> img = memnew(Image(tex.texture_size, tex.texture_size, 0, Image::FORMAT_LA8, tex.imgdata));
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
if (tex.texture.is_null()) {
|
|
|
|
tex.texture.instance();
|
2017-03-05 16:44:50 +01:00
|
|
|
tex.texture->create_from_image(img, Texture::FLAG_FILTER);
|
2016-05-29 16:37:26 +02:00
|
|
|
} else {
|
|
|
|
tex.texture->set_data(img); //update
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update height array
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int k = tex_x; k < tex_x + mw; k++) {
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
tex.offsets[k] = tex_y + mh;
|
2016-05-29 16:37:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int advance;
|
|
|
|
stbtt_GetCodepointHMetrics(&font->info, p_char, &advance, 0);
|
|
|
|
|
|
|
|
Character chr;
|
2017-03-05 16:44:50 +01:00
|
|
|
chr.h_align = xofs;
|
|
|
|
chr.v_align = yofs + get_ascent();
|
|
|
|
chr.advance = advance * scale;
|
|
|
|
chr.texture_idx = tex_index;
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
chr.rect = Rect2(tex_x + rect_margin, tex_y + rect_margin, w, h);
|
2016-05-29 16:37:26 +02: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-05 16:44:50 +01:00
|
|
|
char_map[p_char] = chr;
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
stbtt_FreeBitmap(cpbitmap, NULL);
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
font->unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
DynamicFontAtSize::DynamicFontAtSize() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
rect_margin = 1;
|
2016-05-29 16:37:26 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
DynamicFontAtSize::~DynamicFontAtSize() {
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!font.ptr());
|
|
|
|
font->size_cache.erase(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////
|
|
|
|
|
|
|
|
void DynamicFont::_bind_methods() {
|
|
|
|
|
2017-08-09 13:19:41 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_font_data", "data"), &DynamicFont::set_font_data);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_font_data"), &DynamicFont::get_font_data);
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_size", "data"), &DynamicFont::set_size);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_size"), &DynamicFont::get_size);
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "font/size"), "set_size", "get_size");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "font/font", PROPERTY_HINT_RESOURCE_TYPE, "DynamicFontData"), "set_font_data", "get_font_data");
|
2016-05-29 16:37:26 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void DynamicFont::set_font_data(const Ref<DynamicFontData> &p_data) {
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
data = p_data;
|
|
|
|
data_at_size = data->_get_dynamic_font_at_size(size);
|
2016-05-29 16:37:26 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Ref<DynamicFontData> DynamicFont::get_font_data() const {
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void DynamicFont::set_size(int p_size) {
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (size == p_size)
|
2016-05-29 16:37:26 +02:00
|
|
|
return;
|
2017-03-05 16:44:50 +01:00
|
|
|
size = p_size;
|
|
|
|
ERR_FAIL_COND(p_size < 1);
|
2016-05-29 16:37:26 +02:00
|
|
|
if (!data.is_valid())
|
|
|
|
return;
|
2017-03-05 16:44:50 +01:00
|
|
|
data_at_size = data->_get_dynamic_font_at_size(size);
|
2016-05-29 16:37:26 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
int DynamicFont::get_size() const {
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
float DynamicFont::get_height() const {
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
if (!data_at_size.is_valid())
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return data_at_size->get_height();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
float DynamicFont::get_ascent() const {
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
if (!data_at_size.is_valid())
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return data_at_size->get_ascent();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
float DynamicFont::get_descent() const {
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
if (!data_at_size.is_valid())
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return data_at_size->get_descent();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Size2 DynamicFont::get_char_size(CharType p_char, CharType p_next) const {
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
if (!data_at_size.is_valid())
|
2017-03-05 16:44:50 +01:00
|
|
|
return Size2(1, 1);
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return data_at_size->get_char_size(p_char, p_next);
|
2016-05-29 16:37:26 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool DynamicFont::is_distance_field_hint() const {
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-17 09:44:34 +01:00
|
|
|
float DynamicFont::draw_char(RID p_canvas_item, const Point2 &p_pos, CharType p_char, CharType p_next, const Color &p_modulate, bool p_outline) const {
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
if (!data_at_size.is_valid())
|
|
|
|
return 0;
|
|
|
|
|
2018-03-17 09:44:34 +01:00
|
|
|
return data_at_size->draw_char(p_canvas_item, p_pos, p_char, p_next, p_modulate, p_outline);
|
2016-05-29 16:37:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DynamicFont::DynamicFont() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
size = 16;
|
2016-05-29 16:37:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DynamicFont::~DynamicFont() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
RES ResourceFormatLoaderDynamicFont::load(const String &p_path, const String &p_original_path, Error *r_error) {
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
if (r_error)
|
2017-03-05 16:44:50 +01:00
|
|
|
*r_error = ERR_FILE_CANT_OPEN;
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
|
|
|
|
ERR_FAIL_COND_V(!f, RES());
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<uint8_t> data;
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
data.resize(f->get_len());
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(data.size() == 0, RES());
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
{
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<uint8_t>::Write w = data.write();
|
2017-03-05 16:44:50 +01:00
|
|
|
f->get_buffer(w.ptr(), data.size());
|
2016-05-29 16:37:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Ref<DynamicFontData> dfd;
|
|
|
|
dfd.instance();
|
|
|
|
dfd->set_font_data(data);
|
|
|
|
|
|
|
|
if (r_error)
|
2017-03-05 16:44:50 +01:00
|
|
|
*r_error = OK;
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
return dfd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceFormatLoaderDynamicFont::get_recognized_extensions(List<String> *p_extensions) const {
|
|
|
|
|
|
|
|
p_extensions->push_back("ttf");
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool ResourceFormatLoaderDynamicFont::handles_type(const String &p_type) const {
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return (p_type == "DynamicFontData");
|
2016-05-29 16:37:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
String ResourceFormatLoaderDynamicFont::get_resource_type(const String &p_path) const {
|
|
|
|
|
2017-11-12 11:45:29 +01:00
|
|
|
String el = p_path.get_extension().to_lower();
|
2017-03-05 16:44:50 +01:00
|
|
|
if (el == "ttf")
|
2016-05-29 16:37:26 +02:00
|
|
|
return "DynamicFontData";
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|