2017-03-05 15:47:28 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* dynamic_font_stb.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* 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
|
|
|
/*************************************************************************/
|
2019-01-01 12:53:14 +01:00
|
|
|
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2019 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
|
|
|
#ifndef DYNAMICFONT_STB_H
|
|
|
|
#define DYNAMICFONT_STB_H
|
|
|
|
|
|
|
|
#ifndef FREETYPE_ENABLED
|
|
|
|
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/io/resource_loader.h"
|
2016-05-29 16:37:26 +02:00
|
|
|
#include "font.h"
|
2017-04-28 19:28:21 +02:00
|
|
|
|
|
|
|
#include "thirdparty/misc/stb_truetype.h"
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
class DynamicFontAtSize;
|
|
|
|
class DynamicFont;
|
|
|
|
|
|
|
|
class DynamicFontData : public Resource {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
GDCLASS(DynamicFontData, Resource);
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
bool valid;
|
|
|
|
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<uint8_t> font_data;
|
|
|
|
PoolVector<uint8_t>::Read fr;
|
2017-03-05 16:44:50 +01:00
|
|
|
const uint8_t *last_data_ptr;
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
struct KerningPairKey {
|
|
|
|
|
|
|
|
union {
|
|
|
|
struct {
|
2017-03-05 16:44:50 +01:00
|
|
|
uint32_t A, B;
|
2016-05-29 16:37:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
uint64_t pair;
|
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ bool operator<(const KerningPairKey &p_r) const { return pair < p_r.pair; }
|
2016-05-29 16:37:26 +02:00
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Map<KerningPairKey, int> kerning_map;
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Map<int, DynamicFontAtSize *> size_cache;
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
friend class DynamicFontAtSize;
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
stbtt_fontinfo info;
|
|
|
|
int ascent;
|
|
|
|
int descent;
|
|
|
|
int linegap;
|
|
|
|
|
|
|
|
void lock();
|
|
|
|
void unlock();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
friend class DynamicFont;
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
Ref<DynamicFontAtSize> _get_dynamic_font_at_size(int p_size);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
public:
|
|
|
|
void set_font_data(const PoolVector<uint8_t> &p_font);
|
2016-05-29 16:37:26 +02:00
|
|
|
DynamicFontData();
|
|
|
|
~DynamicFontData();
|
|
|
|
};
|
|
|
|
|
|
|
|
class DynamicFontAtSize : public Reference {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
GDCLASS(DynamicFontAtSize, Reference);
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
int rect_margin;
|
|
|
|
|
|
|
|
struct CharTexture {
|
|
|
|
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<uint8_t> imgdata;
|
2016-05-29 16:37:26 +02:00
|
|
|
int texture_size;
|
|
|
|
Vector<int> offsets;
|
|
|
|
Ref<ImageTexture> texture;
|
|
|
|
};
|
|
|
|
|
|
|
|
Vector<CharTexture> textures;
|
|
|
|
|
|
|
|
struct Character {
|
|
|
|
|
|
|
|
int texture_idx;
|
|
|
|
Rect2 rect;
|
|
|
|
float v_align;
|
|
|
|
float h_align;
|
|
|
|
float advance;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Character() {
|
|
|
|
texture_idx = 0;
|
|
|
|
v_align = 0;
|
|
|
|
}
|
2016-05-29 16:37:26 +02:00
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
HashMap<CharType, Character> char_map;
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
_FORCE_INLINE_ void _update_char(CharType p_char);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
friend class DynamicFontData;
|
2016-05-29 16:37:26 +02:00
|
|
|
Ref<DynamicFontData> font;
|
|
|
|
float scale;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
public:
|
|
|
|
float get_height() const;
|
|
|
|
|
|
|
|
float get_ascent() const;
|
|
|
|
float get_descent() const;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Size2 get_char_size(CharType p_char, CharType p_next = 0) const;
|
2016-05-29 16:37:26 +02:00
|
|
|
|
2018-03-17 09:44:34 +01:00
|
|
|
float draw_char(RID p_canvas_item, const Point2 &p_pos, CharType p_char, CharType p_next = 0, const Color &p_modulate = Color(1, 1, 1), bool p_outline = false) const;
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
DynamicFontAtSize();
|
|
|
|
~DynamicFontAtSize();
|
|
|
|
};
|
|
|
|
|
|
|
|
///////////////
|
|
|
|
|
|
|
|
class DynamicFont : public Font {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
GDCLASS(DynamicFont, Font);
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
Ref<DynamicFontData> data;
|
|
|
|
Ref<DynamicFontAtSize> data_at_size;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
|
|
|
|
|
|
|
public:
|
2017-03-05 16:44:50 +01:00
|
|
|
void set_font_data(const Ref<DynamicFontData> &p_data);
|
2016-05-29 16:37:26 +02:00
|
|
|
Ref<DynamicFontData> get_font_data() const;
|
|
|
|
|
|
|
|
void set_size(int p_size);
|
|
|
|
int get_size() const;
|
|
|
|
|
|
|
|
virtual float get_height() const;
|
|
|
|
|
|
|
|
virtual float get_ascent() const;
|
|
|
|
virtual float get_descent() const;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual Size2 get_char_size(CharType p_char, CharType p_next = 0) const;
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
virtual bool is_distance_field_hint() const;
|
|
|
|
|
2018-03-17 09:44:34 +01:00
|
|
|
virtual float draw_char(RID p_canvas_item, const Point2 &p_pos, CharType p_char, CharType p_next = 0, const Color &p_modulate = Color(1, 1, 1), bool p_outline = false) const;
|
2016-05-29 16:37:26 +02:00
|
|
|
|
|
|
|
DynamicFont();
|
|
|
|
~DynamicFont();
|
|
|
|
};
|
|
|
|
|
|
|
|
/////////////
|
|
|
|
|
|
|
|
class ResourceFormatLoaderDynamicFont : public ResourceFormatLoader {
|
2018-06-11 02:59:53 +02:00
|
|
|
GDCLASS(ResourceFormatLoaderDynamicFont, ResourceFormatLoader)
|
2016-05-29 16:37:26 +02:00
|
|
|
public:
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
|
2016-05-29 16:37:26 +02:00
|
|
|
virtual void get_recognized_extensions(List<String> *p_extensions) const;
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual bool handles_type(const String &p_type) const;
|
2016-05-29 16:37:26 +02:00
|
|
|
virtual String get_resource_type(const String &p_path) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
#endif // DYNAMICFONT_H
|