2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* font.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:11:45 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2018-01-02 10:27:24 +00:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 01:10:30 +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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "font.h"
|
|
|
|
|
|
|
|
#include "core/io/resource_loader.h"
|
2017-03-18 23:36:26 +00:00
|
|
|
#include "core/os/file_access.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void Font::draw_halign(RID p_canvas_item, const Point2 &p_pos, HAlign p_align, float p_width, const String &p_text, const Color &p_modulate) const {
|
2016-04-29 01:21:18 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
float length = get_string_size(p_text).width;
|
|
|
|
if (length >= p_width) {
|
|
|
|
draw(p_canvas_item, p_pos, p_text, p_modulate, p_width);
|
2016-04-29 01:21:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
float ofs;
|
2017-03-18 23:36:26 +00:00
|
|
|
switch (p_align) {
|
2016-04-29 01:21:18 +00:00
|
|
|
case HALIGN_LEFT: {
|
2017-03-18 23:36:26 +00:00
|
|
|
ofs = 0;
|
2016-04-29 01:21:18 +00:00
|
|
|
} break;
|
|
|
|
case HALIGN_CENTER: {
|
2017-03-18 23:36:26 +00:00
|
|
|
ofs = Math::floor((p_width - length) / 2.0);
|
2016-04-29 01:21:18 +00:00
|
|
|
} break;
|
|
|
|
case HALIGN_RIGHT: {
|
2017-03-18 23:36:26 +00:00
|
|
|
ofs = p_width - length;
|
2016-04-29 01:21:18 +00:00
|
|
|
} break;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
draw(p_canvas_item, p_pos + Point2(ofs, 0), p_text, p_modulate, p_width);
|
2016-04-29 01:21:18 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void Font::draw(RID p_canvas_item, const Point2 &p_pos, const String &p_text, const Color &p_modulate, int p_clip_w) const {
|
2016-04-29 01:21:18 +00:00
|
|
|
|
|
|
|
Vector2 ofs;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < p_text.length(); i++) {
|
2016-04-29 01:21:18 +00:00
|
|
|
|
|
|
|
int width = get_char_size(p_text[i]).width;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p_clip_w >= 0 && (ofs.x + width) > p_clip_w)
|
2016-04-29 01:21:18 +00:00
|
|
|
break; //clip
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ofs.x += draw_char(p_canvas_item, p_pos + ofs, p_text[i], p_text[i + 1], p_modulate);
|
2016-04-29 01:21:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-18 12:59:31 +00:00
|
|
|
void Font::update_changes() {
|
|
|
|
|
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
2016-04-29 01:21:18 +00:00
|
|
|
void Font::_bind_methods() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("draw", "canvas_item", "pos", "string", "modulate", "clip_w"), &Font::draw, DEFVAL(Color(1, 1, 1)), DEFVAL(-1));
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_ascent"), &Font::get_ascent);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_descent"), &Font::get_descent);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_height"), &Font::get_height);
|
|
|
|
ObjectTypeDB::bind_method(_MD("is_distance_field_hint"), &Font::is_distance_field_hint);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_string_size", "string"), &Font::get_string_size);
|
|
|
|
ObjectTypeDB::bind_method(_MD("draw_char", "canvas_item", "pos", "char", "next", "modulate"), &Font::draw_char, DEFVAL(-1), DEFVAL(Color(1, 1, 1)));
|
|
|
|
ObjectTypeDB::bind_method(_MD("update_changes"), &Font::update_changes);
|
2016-04-29 01:21:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Font::Font() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void BitmapFont::_set_chars(const DVector<int> &p_chars) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
int len = p_chars.size();
|
|
|
|
//char 1 charsize 1 texture, 4 rect, 2 align, advance 1
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND(len % 9);
|
2014-02-10 01:10:30 +00:00
|
|
|
if (!len)
|
|
|
|
return; //none to do
|
2017-03-18 23:36:26 +00:00
|
|
|
int chars = len / 9;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
DVector<int>::Read r = p_chars.read();
|
|
|
|
for (int i = 0; i < chars; i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
const int *data = &r[i * 9];
|
|
|
|
add_char(data[0], data[1], Rect2(data[2], data[3], data[4], data[5]), Size2(data[6], data[7]), data[8]);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 01:21:18 +00:00
|
|
|
DVector<int> BitmapFont::_get_chars() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
DVector<int> chars;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
const CharType *key = NULL;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
while ((key = char_map.next(key))) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
const Character *c = char_map.getptr(*key);
|
2014-02-10 01:10:30 +00:00
|
|
|
chars.push_back(*key);
|
|
|
|
chars.push_back(c->texture_idx);
|
|
|
|
chars.push_back(c->rect.pos.x);
|
|
|
|
chars.push_back(c->rect.pos.y);
|
|
|
|
|
|
|
|
chars.push_back(c->rect.size.x);
|
|
|
|
chars.push_back(c->rect.size.y);
|
|
|
|
chars.push_back(c->h_align);
|
|
|
|
chars.push_back(c->v_align);
|
|
|
|
chars.push_back(c->advance);
|
|
|
|
}
|
|
|
|
|
|
|
|
return chars;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void BitmapFont::_set_kernings(const DVector<int> &p_kernings) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int len = p_kernings.size();
|
|
|
|
ERR_FAIL_COND(len % 3);
|
2014-02-10 01:10:30 +00:00
|
|
|
if (!len)
|
|
|
|
return;
|
2017-03-18 23:36:26 +00:00
|
|
|
DVector<int>::Read r = p_kernings.read();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < len / 3; i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
const int *data = &r[i * 3];
|
|
|
|
add_kerning_pair(data[0], data[1], data[2]);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 01:21:18 +00:00
|
|
|
DVector<int> BitmapFont::_get_kernings() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
DVector<int> kernings;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (Map<KerningPairKey, int>::Element *E = kerning_map.front(); E; E = E->next()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
kernings.push_back(E->key().A);
|
|
|
|
kernings.push_back(E->key().B);
|
|
|
|
kernings.push_back(E->get());
|
|
|
|
}
|
|
|
|
|
|
|
|
return kernings;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void BitmapFont::_set_textures(const Vector<Variant> &p_textures) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < p_textures.size(); i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
Ref<Texture> tex = p_textures[i];
|
|
|
|
ERR_CONTINUE(!tex.is_valid());
|
|
|
|
add_texture(tex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 01:21:18 +00:00
|
|
|
Vector<Variant> BitmapFont::_get_textures() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Vector<Variant> rtextures;
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < textures.size(); i++)
|
2014-02-10 01:10:30 +00:00
|
|
|
rtextures.push_back(textures[i].get_ref_ptr());
|
|
|
|
return rtextures;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Error BitmapFont::create_from_fnt(const String &p_string) {
|
2014-02-10 01:10:30 +00:00
|
|
|
//fnt format used by angelcode bmfont
|
|
|
|
//http://www.angelcode.com/products/bmfont/
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
FileAccess *f = FileAccess::open(p_string, FileAccess::READ);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (!f) {
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_EXPLAIN("Can't open font: " + p_string);
|
2014-02-10 01:10:30 +00:00
|
|
|
ERR_FAIL_V(ERR_FILE_NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
clear();
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
while (true) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
String line = f->get_line();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int delimiter = line.find(" ");
|
|
|
|
String type = line.substr(0, delimiter);
|
|
|
|
int pos = delimiter + 1;
|
|
|
|
Map<String, String> keys;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
while (pos < line.size() && line[pos] == ' ')
|
2014-02-10 01:10:30 +00:00
|
|
|
pos++;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
while (pos < line.size()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int eq = line.find("=", pos);
|
|
|
|
if (eq == -1)
|
2014-02-10 01:10:30 +00:00
|
|
|
break;
|
2017-03-18 23:36:26 +00:00
|
|
|
String key = line.substr(pos, eq - pos);
|
|
|
|
int end = -1;
|
2014-02-10 01:10:30 +00:00
|
|
|
String value;
|
2017-03-18 23:36:26 +00:00
|
|
|
if (line[eq + 1] == '"') {
|
|
|
|
end = line.find("\"", eq + 2);
|
|
|
|
if (end == -1)
|
2014-02-10 01:10:30 +00:00
|
|
|
break;
|
2017-03-18 23:36:26 +00:00
|
|
|
value = line.substr(eq + 2, end - 1 - eq - 1);
|
|
|
|
pos = end + 1;
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2017-03-18 23:36:26 +00:00
|
|
|
end = line.find(" ", eq + 1);
|
|
|
|
if (end == -1)
|
|
|
|
end = line.size();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
value = line.substr(eq + 1, end - eq);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
pos = end;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
while (pos < line.size() && line[pos] == ' ')
|
2014-02-10 01:10:30 +00:00
|
|
|
pos++;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
keys[key] = value;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (type == "info") {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (keys.has("face"))
|
|
|
|
set_name(keys["face"]);
|
|
|
|
//if (keys.has("size"))
|
|
|
|
// font->set_height(keys["size"].to_int());
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
} else if (type == "common") {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (keys.has("lineHeight"))
|
|
|
|
set_height(keys["lineHeight"].to_int());
|
|
|
|
if (keys.has("base"))
|
|
|
|
set_ascent(keys["base"].to_int());
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
} else if (type == "page") {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (keys.has("file")) {
|
|
|
|
|
|
|
|
String file = keys["file"];
|
2017-03-18 23:36:26 +00:00
|
|
|
file = p_string.get_base_dir() + "/" + file;
|
2014-02-10 01:10:30 +00:00
|
|
|
Ref<Texture> tex = ResourceLoader::load(file);
|
|
|
|
if (tex.is_null()) {
|
|
|
|
ERR_PRINT("Can't load font texture!");
|
|
|
|
} else {
|
|
|
|
add_texture(tex);
|
|
|
|
}
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
} else if (type == "char") {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
CharType idx = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
if (keys.has("id"))
|
2017-03-18 23:36:26 +00:00
|
|
|
idx = keys["id"].to_int();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Rect2 rect;
|
|
|
|
|
|
|
|
if (keys.has("x"))
|
2017-03-18 23:36:26 +00:00
|
|
|
rect.pos.x = keys["x"].to_int();
|
2014-02-10 01:10:30 +00:00
|
|
|
if (keys.has("y"))
|
2017-03-18 23:36:26 +00:00
|
|
|
rect.pos.y = keys["y"].to_int();
|
2014-02-10 01:10:30 +00:00
|
|
|
if (keys.has("width"))
|
2017-03-18 23:36:26 +00:00
|
|
|
rect.size.width = keys["width"].to_int();
|
2014-02-10 01:10:30 +00:00
|
|
|
if (keys.has("height"))
|
2017-03-18 23:36:26 +00:00
|
|
|
rect.size.height = keys["height"].to_int();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Point2 ofs;
|
|
|
|
|
|
|
|
if (keys.has("xoffset"))
|
2017-03-18 23:36:26 +00:00
|
|
|
ofs.x = keys["xoffset"].to_int();
|
2014-02-10 01:10:30 +00:00
|
|
|
if (keys.has("yoffset"))
|
2017-03-18 23:36:26 +00:00
|
|
|
ofs.y = keys["yoffset"].to_int();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int texture = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
if (keys.has("page"))
|
2017-03-18 23:36:26 +00:00
|
|
|
texture = keys["page"].to_int();
|
|
|
|
int advance = -1;
|
2014-02-10 01:10:30 +00:00
|
|
|
if (keys.has("xadvance"))
|
2017-03-18 23:36:26 +00:00
|
|
|
advance = keys["xadvance"].to_int();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
add_char(idx, texture, rect, ofs, advance);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
} else if (type == "kerning") {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
CharType first = 0, second = 0;
|
|
|
|
int k = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (keys.has("first"))
|
2017-03-18 23:36:26 +00:00
|
|
|
first = keys["first"].to_int();
|
2014-02-10 01:10:30 +00:00
|
|
|
if (keys.has("second"))
|
2017-03-18 23:36:26 +00:00
|
|
|
second = keys["second"].to_int();
|
2014-02-10 01:10:30 +00:00
|
|
|
if (keys.has("amount"))
|
2017-03-18 23:36:26 +00:00
|
|
|
k = keys["amount"].to_int();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
add_kerning_pair(first, second, -k);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (f->eof_reached())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
memdelete(f);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2016-04-29 01:21:18 +00:00
|
|
|
void BitmapFont::set_height(float p_height) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
height = p_height;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
float BitmapFont::get_height() const {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return height;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void BitmapFont::set_ascent(float p_ascent) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ascent = p_ascent;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-04-29 01:21:18 +00:00
|
|
|
float BitmapFont::get_ascent() const {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return ascent;
|
|
|
|
}
|
2016-04-29 01:21:18 +00:00
|
|
|
float BitmapFont::get_descent() const {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return height - ascent;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void BitmapFont::add_texture(const Ref<Texture> &p_texture) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND(p_texture.is_null());
|
|
|
|
textures.push_back(p_texture);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 01:21:18 +00:00
|
|
|
int BitmapFont::get_texture_count() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return textures.size();
|
|
|
|
};
|
|
|
|
|
2016-04-29 01:21:18 +00:00
|
|
|
Ref<Texture> BitmapFont::get_texture(int p_idx) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
ERR_FAIL_INDEX_V(p_idx, textures.size(), Ref<Texture>());
|
|
|
|
return textures[p_idx];
|
|
|
|
};
|
|
|
|
|
2016-04-29 01:21:18 +00:00
|
|
|
int BitmapFont::get_character_count() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return char_map.size();
|
|
|
|
};
|
|
|
|
|
2016-04-29 01:21:18 +00:00
|
|
|
Vector<CharType> BitmapFont::get_char_keys() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Vector<CharType> chars;
|
|
|
|
chars.resize(char_map.size());
|
2017-03-18 23:36:26 +00:00
|
|
|
const CharType *ct = NULL;
|
2014-02-10 01:10:30 +00:00
|
|
|
int count = 0;
|
2017-03-18 23:36:26 +00:00
|
|
|
while ((ct = char_map.next(ct))) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
chars[count++] = *ct;
|
|
|
|
};
|
|
|
|
|
|
|
|
return chars;
|
|
|
|
};
|
|
|
|
|
2016-04-29 01:21:18 +00:00
|
|
|
BitmapFont::Character BitmapFont::get_character(CharType p_char) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (!char_map.has(p_char)) {
|
|
|
|
ERR_FAIL_V(Character());
|
|
|
|
};
|
|
|
|
|
|
|
|
return char_map[p_char];
|
|
|
|
};
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void BitmapFont::add_char(CharType p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p_advance < 0)
|
|
|
|
p_advance = p_rect.size.width;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Character c;
|
2017-03-18 23:36:26 +00:00
|
|
|
c.rect = p_rect;
|
|
|
|
c.texture_idx = p_texture_idx;
|
|
|
|
c.v_align = p_align.y;
|
|
|
|
c.advance = p_advance;
|
|
|
|
c.h_align = p_align.x;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
char_map[p_char] = c;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void BitmapFont::add_kerning_pair(CharType p_A, CharType p_B, int p_kerning) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
KerningPairKey kpk;
|
2017-03-18 23:36:26 +00:00
|
|
|
kpk.A = p_A;
|
|
|
|
kpk.B = p_B;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p_kerning == 0 && kerning_map.has(kpk)) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
kerning_map.erase(kpk);
|
|
|
|
} else {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
kerning_map[kpk] = p_kerning;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 01:21:18 +00:00
|
|
|
Vector<BitmapFont::KerningPairKey> BitmapFont::get_kerning_pair_keys() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2016-04-29 01:21:18 +00:00
|
|
|
Vector<BitmapFont::KerningPairKey> ret;
|
2014-02-10 01:10:30 +00:00
|
|
|
ret.resize(kerning_map.size());
|
2017-03-18 23:36:26 +00:00
|
|
|
int i = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (Map<KerningPairKey, int>::Element *E = kerning_map.front(); E; E = E->next()) {
|
|
|
|
ret[i++] = E->key();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int BitmapFont::get_kerning_pair(CharType p_A, CharType p_B) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
KerningPairKey kpk;
|
2017-03-18 23:36:26 +00:00
|
|
|
kpk.A = p_A;
|
|
|
|
kpk.B = p_B;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
const Map<KerningPairKey, int>::Element *E = kerning_map.find(kpk);
|
2014-02-10 01:10:30 +00:00
|
|
|
if (E)
|
|
|
|
return E->get();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-29 01:21:18 +00:00
|
|
|
void BitmapFont::set_distance_field_hint(bool p_distance_field) {
|
2015-03-21 03:43:33 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
distance_field_hint = p_distance_field;
|
2015-03-21 03:43:33 +00:00
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool BitmapFont::is_distance_field_hint() const {
|
2015-03-21 03:43:33 +00:00
|
|
|
|
|
|
|
return distance_field_hint;
|
|
|
|
}
|
|
|
|
|
2016-04-29 01:21:18 +00:00
|
|
|
void BitmapFont::clear() {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
height = 1;
|
|
|
|
ascent = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
char_map.clear();
|
|
|
|
textures.clear();
|
|
|
|
kerning_map.clear();
|
2017-03-18 23:36:26 +00:00
|
|
|
distance_field_hint = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Size2 Font::get_string_size(const String &p_string) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
float w = 0;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
int l = p_string.length();
|
2017-03-18 23:36:26 +00:00
|
|
|
if (l == 0)
|
|
|
|
return Size2(0, get_height());
|
2014-02-10 01:10:30 +00:00
|
|
|
const CharType *sptr = &p_string[0];
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < l; i++) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
w += get_char_size(sptr[i], sptr[i + 1]).width;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return Size2(w, get_height());
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-04-29 01:21:18 +00:00
|
|
|
void BitmapFont::set_fallback(const Ref<BitmapFont> &p_fallback) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
fallback = p_fallback;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Ref<BitmapFont> BitmapFont::get_fallback() const {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2016-04-29 01:21:18 +00:00
|
|
|
return fallback;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
float BitmapFont::draw_char(RID p_canvas_item, const Point2 &p_pos, CharType p_char, CharType p_next, const Color &p_modulate) const {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
const Character *c = char_map.getptr(p_char);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2015-12-11 02:02:40 +00:00
|
|
|
if (!c) {
|
|
|
|
if (fallback.is_valid())
|
2017-03-18 23:36:26 +00:00
|
|
|
return fallback->draw_char(p_canvas_item, p_pos, p_char, p_next, p_modulate);
|
2014-02-10 01:10:30 +00:00
|
|
|
return 0;
|
2015-12-11 02:02:40 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Point2 cpos = p_pos;
|
|
|
|
cpos.x += c->h_align;
|
|
|
|
cpos.y -= 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]->get_rid(), c->rect, p_modulate);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return get_char_size(p_char, p_next).width;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Size2 BitmapFont::get_char_size(CharType p_char, CharType p_next) const {
|
2015-12-11 02:02:40 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
const Character *c = char_map.getptr(p_char);
|
2015-12-11 02:02:40 +00:00
|
|
|
|
2016-04-29 01:21:18 +00:00
|
|
|
if (!c) {
|
|
|
|
if (fallback.is_valid())
|
2017-03-18 23:36:26 +00:00
|
|
|
return fallback->get_char_size(p_char, p_next);
|
2016-04-29 01:21:18 +00:00
|
|
|
return Size2();
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Size2 ret(c->advance, c->rect.size.y);
|
2016-04-29 01:21:18 +00:00
|
|
|
|
|
|
|
if (p_next) {
|
|
|
|
|
|
|
|
KerningPairKey kpk;
|
2017-03-18 23:36:26 +00:00
|
|
|
kpk.A = p_char;
|
|
|
|
kpk.B = p_next;
|
2016-04-29 01:21:18 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
const Map<KerningPairKey, int>::Element *E = kerning_map.find(kpk);
|
2016-04-29 01:21:18 +00:00
|
|
|
if (E) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ret.width -= E->get();
|
2016-04-29 01:21:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2015-12-11 02:02:40 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 01:21:18 +00:00
|
|
|
void BitmapFont::_bind_methods() {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("create_from_fnt", "path"), &BitmapFont::create_from_fnt);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_height", "px"), &BitmapFont::set_height);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_ascent", "px"), &BitmapFont::set_ascent);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("add_kerning_pair", "char_a", "char_b", "kerning"), &BitmapFont::add_kerning_pair);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_kerning_pair", "char_a", "char_b"), &BitmapFont::get_kerning_pair);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("add_texture", "texture:Texture"), &BitmapFont::add_texture);
|
|
|
|
ObjectTypeDB::bind_method(_MD("add_char", "character", "texture", "rect", "align", "advance"), &BitmapFont::add_char, DEFVAL(Point2()), DEFVAL(-1));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("get_texture_count"), &BitmapFont::get_texture_count);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_texture:Texture", "idx"), &BitmapFont::get_texture);
|
2015-03-22 04:46:18 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("get_char_size", "char", "next"), &BitmapFont::get_char_size, DEFVAL(0));
|
2015-03-22 04:46:18 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_distance_field_hint", "enable"), &BitmapFont::set_distance_field_hint);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("clear"), &BitmapFont::clear);
|
2015-03-21 03:43:33 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("_set_chars"), &BitmapFont::_set_chars);
|
|
|
|
ObjectTypeDB::bind_method(_MD("_get_chars"), &BitmapFont::_get_chars);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("_set_kernings"), &BitmapFont::_set_kernings);
|
|
|
|
ObjectTypeDB::bind_method(_MD("_get_kernings"), &BitmapFont::_get_kernings);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("_set_textures"), &BitmapFont::_set_textures);
|
|
|
|
ObjectTypeDB::bind_method(_MD("_get_textures"), &BitmapFont::_get_textures);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_fallback", "fallback"), &BitmapFont::set_fallback);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_fallback"), &BitmapFont::get_fallback);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "textures", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), _SCS("_set_textures"), _SCS("_get_textures"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT_ARRAY, "chars", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), _SCS("_set_chars"), _SCS("_get_chars"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT_ARRAY, "kernings", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), _SCS("_set_kernings"), _SCS("_get_kernings"));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "height", PROPERTY_HINT_RANGE, "-1024,1024,1"), _SCS("set_height"), _SCS("get_height"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "ascent", PROPERTY_HINT_RANGE, "-1024,1024,1"), _SCS("set_ascent"), _SCS("get_ascent"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "distance_field"), _SCS("set_distance_field_hint"), _SCS("is_distance_field_hint"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "fallback", PROPERTY_HINT_RESOURCE_TYPE, "BitmapFont"), _SCS("set_fallback"), _SCS("get_fallback"));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 01:21:18 +00:00
|
|
|
BitmapFont::BitmapFont() {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2016-04-29 01:21:18 +00:00
|
|
|
BitmapFont::~BitmapFont() {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
clear();
|
|
|
|
}
|