raylib-cpp
C++ object-oriented wrapper library for raylib.
Font.hpp
1 /*
2 * LICENSE: zlib/libpng
3 *
4 * raylib-cpp is licensed under an unmodified zlib/libpng license, which is an OSI-certified,
5 * BSD-like license that allows static linking with closed source software:
6 *
7 * Copyright (c) 2020 Rob Loach (@RobLoach)
8 *
9 * This software is provided "as-is", without any express or implied warranty. In no event
10 * will the authors be held liable for any damages arising from the use of this software.
11 *
12 * Permission is granted to anyone to use this software for any purpose, including commercial
13 * applications, and to alter it and redistribute it freely, subject to the following restrictions:
14 *
15 * 1. The origin of this software must not be misrepresented; you must not claim that you
16 * wrote the original software. If you use this software in a product, an acknowledgment
17 * in the product documentation would be appreciated but is not required.
18 *
19 * 2. Altered source versions must be plainly marked as such, and must not be misrepresented
20 * as being the original software.
21 *
22 * 3. This notice may not be removed or altered from any source distribution.
23 */
24 
25 #ifndef RAYLIB_CPP_INCLUDE_FONT_HPP_
26 #define RAYLIB_CPP_INCLUDE_FONT_HPP_
27 
28 #include <string>
29 
30 #include "./raylib.hpp"
31 #include "./raylib-cpp-utils.hpp"
32 
33 namespace raylib {
34 class Font : public ::Font {
35  public:
36  Font() {
37  set(::GetFontDefault());
38  }
39 
40  Font(const ::Font& font) {
41  set(font);
42  }
43 
44  Font(const std::string& fileName) {
45  set(::LoadFont(fileName.c_str()));
46  }
47 
48  Font(const std::string& fileName, int fontSize, int* fontChars, int charCount) {
49  set(::LoadFontEx(fileName.c_str(), fontSize, fontChars, charCount));
50  }
51 
52  Font(const ::Image& image, ::Color key, int firstChar) {
53  set(::LoadFontFromImage(image, key, firstChar));
54  }
55 
56  Font(const std::string& fileType, const unsigned char *fileData, int dataSize, int fontSize,
57  int *fontChars, int charsCount) {
58  set(::LoadFontFromMemory(fileType.c_str(), fileData, dataSize, fontSize, fontChars,
59  charsCount));
60  }
61 
62  ~Font() {
63  Unload();
64  }
65 
66  void Unload() {
67  UnloadFont(*this);
68  }
69 
70  GETTERSETTER(int, BaseSize, baseSize)
71  GETTERSETTER(int, CharsCount, charsCount)
72  GETTERSETTER(int, CharsPadding, charsPadding)
73  GETTERSETTER(::Texture2D, Texture, texture)
74  GETTERSETTER(::Rectangle*, Recs, recs)
75  GETTERSETTER(::CharInfo*, Chars, chars)
76 
77  Font& operator=(const ::Font& font) {
78  set(font);
79  return *this;
80  }
81 
82  Font& operator=(const Font& font) {
83  set(font);
84  return *this;
85  }
86 
87  inline Font& DrawText(const std::string& text, ::Vector2 position, float fontSize,
88  float spacing, ::Color tint = WHITE) {
89  ::DrawTextEx(*this, text.c_str(), position, fontSize, spacing, tint);
90  return *this;
91  }
92 
93  inline Font& DrawText(const std::string& text, ::Rectangle rec, float fontSize, float spacing,
94  bool wordWrap, ::Color tint = WHITE) {
95  ::DrawTextRec(*this, text.c_str(), rec, fontSize, spacing, wordWrap, tint);
96  return *this;
97  }
98 
99  inline Font& DrawText(const std::string& text, ::Rectangle rec, float fontSize, float spacing,
100  bool wordWrap, Color tint, int selectStart, int selectLength, ::Color selectText,
101  ::Color selectBack) {
102  ::DrawTextRecEx(*this, text.c_str(), rec, fontSize, spacing, wordWrap, tint,
103  selectStart, selectLength, selectText, selectBack);
104  return *this;
105  }
106 
110  inline Vector2 MeasureText(const std::string& text, float fontSize, float spacing) const {
111  return ::MeasureTextEx(*this, text.c_str(), fontSize, spacing);
112  }
113 
117  inline int GetGlyphIndex(int character) const {
118  return ::GetGlyphIndex(*this, character);
119  }
120 
124  inline Image ImageText(const std::string& text, float fontSize,
125  float spacing, ::Color tint) const {
126  return ::ImageTextEx(*this, text.c_str(), fontSize, spacing, tint);
127  }
128 
129  protected:
130  void set(const ::Font& font) {
131  baseSize = font.baseSize;
132  charsCount = font.charsCount;
133  texture = font.texture;
134  recs = font.recs;
135  chars = font.chars;
136  }
137 
138  void set(const Font& font) {
139  baseSize = font.baseSize;
140  charsCount = font.charsCount;
141  texture = font.texture;
142  recs = font.recs;
143  chars = font.chars;
144  }
145 };
146 } // namespace raylib
147 
148 #endif // RAYLIB_CPP_INCLUDE_FONT_HPP_
raylib::Font::Font
Font(const std::string &fileName)
Definition: Font.hpp:44
raylib
Provides all the classes associated with raylib-cpp.
Definition: AudioDevice.hpp:31
raylib::Font::set
void set(const ::Font &font)
Definition: Font.hpp:130
raylib::Font::ImageText
Image ImageText(const std::string &text, float fontSize, float spacing, ::Color tint) const
Create an image from text (custom sprite font)
Definition: Font.hpp:124
raylib::Image
Definition: Image.hpp:34
raylib::Font::DrawText
Font & DrawText(const std::string &text, ::Vector2 position, float fontSize, float spacing, ::Color tint=WHITE)
Definition: Font.hpp:87
raylib::Font::Unload
void Unload()
Definition: Font.hpp:66
raylib::Font::operator=
Font & operator=(const Font &font)
Definition: Font.hpp:82
raylib::Font::Font
Font()
Definition: Font.hpp:36
raylib::Font::DrawText
Font & DrawText(const std::string &text, ::Rectangle rec, float fontSize, float spacing, bool wordWrap, ::Color tint=WHITE)
Definition: Font.hpp:93
raylib::Font::~Font
~Font()
Definition: Font.hpp:62
raylib::Font::Font
Font(const ::Image &image, ::Color key, int firstChar)
Definition: Font.hpp:52
raylib::Font::Font
Font(const std::string &fileType, const unsigned char *fileData, int dataSize, int fontSize, int *fontChars, int charsCount)
Definition: Font.hpp:56
raylib::Rectangle
Definition: Rectangle.hpp:33
raylib::Font
Definition: Font.hpp:34
raylib::Font::GetGlyphIndex
int GetGlyphIndex(int character) const
Get index position for a unicode character on font.
Definition: Font.hpp:117
raylib::Font::DrawText
Font & DrawText(const std::string &text, ::Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint, int selectStart, int selectLength, ::Color selectText, ::Color selectBack)
Definition: Font.hpp:99
raylib::Vector2
Definition: Vector2.hpp:37
raylib::Font::Font
Font(const ::Font &font)
Definition: Font.hpp:40
raylib::Font::Font
Font(const std::string &fileName, int fontSize, int *fontChars, int charCount)
Definition: Font.hpp:48
raylib::Font::set
void set(const Font &font)
Definition: Font.hpp:138
raylib::Color
Definition: Color.hpp:35
raylib::Texture
Definition: Texture.hpp:36
raylib::Font::MeasureText
Vector2 MeasureText(const std::string &text, float fontSize, float spacing) const
Measure string size for Font.
Definition: Font.hpp:110