2016-05-29 14:37:26 +00:00
|
|
|
#ifndef DYNAMICFONT_STB_H
|
|
|
|
#define DYNAMICFONT_STB_H
|
|
|
|
|
|
|
|
#ifndef FREETYPE_ENABLED
|
|
|
|
|
|
|
|
#include "font.h"
|
|
|
|
#include "io/resource_loader.h"
|
2017-04-28 17:28:21 +00:00
|
|
|
|
|
|
|
#include "thirdparty/misc/stb_truetype.h"
|
2016-05-29 14:37:26 +00:00
|
|
|
|
|
|
|
class DynamicFontAtSize;
|
|
|
|
class DynamicFont;
|
|
|
|
|
|
|
|
class DynamicFontData : public Resource {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
OBJ_TYPE(DynamicFontData, Resource);
|
2016-05-29 14:37:26 +00:00
|
|
|
|
|
|
|
bool valid;
|
|
|
|
|
|
|
|
DVector<uint8_t> font_data;
|
|
|
|
DVector<uint8_t>::Read fr;
|
2017-03-18 23:36:26 +00:00
|
|
|
const uint8_t *last_data_ptr;
|
2016-05-29 14:37:26 +00:00
|
|
|
|
|
|
|
struct KerningPairKey {
|
|
|
|
|
|
|
|
union {
|
|
|
|
struct {
|
2017-03-18 23:36:26 +00:00
|
|
|
uint32_t A, B;
|
2016-05-29 14:37:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
uint64_t pair;
|
|
|
|
};
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
_FORCE_INLINE_ bool operator<(const KerningPairKey &p_r) const { return pair < p_r.pair; }
|
2016-05-29 14:37:26 +00:00
|
|
|
};
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Map<KerningPairKey, int> kerning_map;
|
2016-05-29 14:37:26 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Map<int, DynamicFontAtSize *> size_cache;
|
2016-05-29 14:37:26 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
friend class DynamicFontAtSize;
|
2016-05-29 14:37:26 +00:00
|
|
|
|
|
|
|
stbtt_fontinfo info;
|
|
|
|
int ascent;
|
|
|
|
int descent;
|
|
|
|
int linegap;
|
|
|
|
|
|
|
|
void lock();
|
|
|
|
void unlock();
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
friend class DynamicFont;
|
2016-05-29 14:37:26 +00:00
|
|
|
|
|
|
|
Ref<DynamicFontAtSize> _get_dynamic_font_at_size(int p_size);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
public:
|
|
|
|
void set_font_data(const DVector<uint8_t> &p_font);
|
2016-05-29 14:37:26 +00:00
|
|
|
DynamicFontData();
|
|
|
|
~DynamicFontData();
|
|
|
|
};
|
|
|
|
|
|
|
|
class DynamicFontAtSize : public Reference {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
OBJ_TYPE(DynamicFontAtSize, Reference);
|
2016-05-29 14:37:26 +00:00
|
|
|
|
|
|
|
int rect_margin;
|
|
|
|
|
|
|
|
struct CharTexture {
|
|
|
|
|
|
|
|
DVector<uint8_t> imgdata;
|
|
|
|
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-18 23:36:26 +00:00
|
|
|
Character() {
|
|
|
|
texture_idx = 0;
|
|
|
|
v_align = 0;
|
|
|
|
}
|
2016-05-29 14:37:26 +00:00
|
|
|
};
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
HashMap<CharType, Character> char_map;
|
2016-05-29 14:37:26 +00:00
|
|
|
|
|
|
|
_FORCE_INLINE_ void _update_char(CharType p_char);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
friend class DynamicFontData;
|
2016-05-29 14:37:26 +00:00
|
|
|
Ref<DynamicFontData> font;
|
|
|
|
float scale;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
public:
|
|
|
|
float get_height() const;
|
|
|
|
|
|
|
|
float get_ascent() const;
|
|
|
|
float get_descent() const;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Size2 get_char_size(CharType p_char, CharType p_next = 0) const;
|
2016-05-29 14:37:26 +00:00
|
|
|
|
2017-03-18 23:36:26 +00: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)) const;
|
2016-05-29 14:37:26 +00:00
|
|
|
|
|
|
|
DynamicFontAtSize();
|
|
|
|
~DynamicFontAtSize();
|
|
|
|
};
|
|
|
|
|
|
|
|
///////////////
|
|
|
|
|
|
|
|
class DynamicFont : public Font {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
OBJ_TYPE(DynamicFont, Font);
|
2016-05-29 14:37:26 +00:00
|
|
|
|
|
|
|
Ref<DynamicFontData> data;
|
|
|
|
Ref<DynamicFontAtSize> data_at_size;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
|
|
|
|
|
|
|
public:
|
2017-03-18 23:36:26 +00:00
|
|
|
void set_font_data(const Ref<DynamicFontData> &p_data);
|
2016-05-29 14:37:26 +00: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-18 23:36:26 +00:00
|
|
|
virtual Size2 get_char_size(CharType p_char, CharType p_next = 0) const;
|
2016-05-29 14:37:26 +00:00
|
|
|
|
|
|
|
virtual bool is_distance_field_hint() const;
|
|
|
|
|
2017-03-18 23:36:26 +00: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)) const;
|
2016-05-29 14:37:26 +00:00
|
|
|
|
|
|
|
DynamicFont();
|
|
|
|
~DynamicFont();
|
|
|
|
};
|
|
|
|
|
|
|
|
/////////////
|
|
|
|
|
|
|
|
class ResourceFormatLoaderDynamicFont : public ResourceFormatLoader {
|
|
|
|
public:
|
2017-03-18 23:36:26 +00:00
|
|
|
virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
|
2016-05-29 14:37:26 +00:00
|
|
|
virtual void get_recognized_extensions(List<String> *p_extensions) const;
|
2017-03-18 23:36:26 +00:00
|
|
|
virtual bool handles_type(const String &p_type) const;
|
2016-05-29 14:37:26 +00:00
|
|
|
virtual String get_resource_type(const String &p_path) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
#endif // DYNAMICFONT_H
|