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 std::string& fileName) {
41  set(::LoadFont(fileName.c_str()));
42  }
43 
44  Font(const std::string& fileName, int fontSize, int* fontChars, int charCount) {
45  set(::LoadFontEx(fileName.c_str(), fontSize, fontChars, charCount));
46  }
47 
48  Font(const ::Image& image, ::Color key, int firstChar) {
49  set(::LoadFontFromImage(image, key, firstChar));
50  }
51 
52  Font(const std::string& fileType, const unsigned char *fileData, int dataSize, int fontSize,
53  int *fontChars, int charsCount) {
54  set(::LoadFontFromMemory(fileType.c_str(), fileData, dataSize, fontSize, fontChars,
55  charsCount));
56  }
57 
58  ~Font() {
59  Unload();
60  }
61 
62  void Unload() {
63  UnloadFont(*this);
64  }
65 
66  GETTERSETTER(int, BaseSize, baseSize)
67  GETTERSETTER(int, CharsCount, charsCount)
68  GETTERSETTER(int, CharsPadding, charsPadding)
69  GETTERSETTER(::Texture2D, Texture, texture)
70  GETTERSETTER(::Rectangle*, Recs, recs)
71  GETTERSETTER(::CharInfo*, Chars, chars)
72 
73  Font& operator=(const ::Font& font) {
74  set(font);
75  return *this;
76  }
77 
78  Font& operator=(const Font& font) {
79  set(font);
80  return *this;
81  }
82 
83  inline Font& DrawText(const std::string& text, ::Vector2 position, float fontSize,
84  float spacing, ::Color tint = WHITE) {
85  ::DrawTextEx(*this, text.c_str(), position, fontSize, spacing, tint);
86  return *this;
87  }
88 
89  inline Font& DrawText(const std::string& text, ::Rectangle rec, float fontSize, float spacing,
90  bool wordWrap, ::Color tint = WHITE) {
91  ::DrawTextRec(*this, text.c_str(), rec, fontSize, spacing, wordWrap, tint);
92  return *this;
93  }
94 
95  inline Font& DrawText(const std::string& text, ::Rectangle rec, float fontSize, float spacing,
96  bool wordWrap, Color tint, int selectStart, int selectLength, ::Color selectText,
97  ::Color selectBack) {
98  ::DrawTextRecEx(*this, text.c_str(), rec, fontSize, spacing, wordWrap, tint,
99  selectStart, selectLength, selectText, selectBack);
100  return *this;
101  }
102 
106  inline Vector2 MeasureText(const std::string& text, float fontSize, float spacing) const {
107  return ::MeasureTextEx(*this, text.c_str(), fontSize, spacing);
108  }
109 
113  inline int GetGlyphIndex(int character) const {
114  return ::GetGlyphIndex(*this, character);
115  }
116 
120  inline Image ImageText(const std::string& text, float fontSize, float spacing, ::Color tint) const {
121  return ::ImageTextEx(*this, text.c_str(), fontSize, spacing, tint);
122  }
123 
124  protected:
125  void set(const ::Font font) {
126  baseSize = font.baseSize;
127  charsCount = font.charsCount;
128  texture = font.texture;
129  recs = font.recs;
130  chars = font.chars;
131  }
132 
133  void set(const Font& font) {
134  baseSize = font.baseSize;
135  charsCount = font.charsCount;
136  texture = font.texture;
137  recs = font.recs;
138  chars = font.chars;
139  }
140 };
141 } // namespace raylib
142 
143 #endif // RAYLIB_CPP_INCLUDE_FONT_HPP_
raylib::Font::Font
Font(const std::string &fileName)
Definition: Font.hpp:40
raylib
Provides all the classes associated with raylib-cpp.
Definition: AudioDevice.hpp:31
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:120
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:83
raylib::Font::Unload
void Unload()
Definition: Font.hpp:62
raylib::Font::operator=
Font & operator=(const Font &font)
Definition: Font.hpp:78
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:89
raylib::Font::~Font
~Font()
Definition: Font.hpp:58
raylib::Font::Font
Font(const ::Image &image, ::Color key, int firstChar)
Definition: Font.hpp:48
raylib::Font::set
void set(const ::Font font)
Definition: Font.hpp:125
raylib::Font::Font
Font(const std::string &fileType, const unsigned char *fileData, int dataSize, int fontSize, int *fontChars, int charsCount)
Definition: Font.hpp:52
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:113
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:95
raylib::Vector2
Definition: Vector2.hpp:37
raylib::Font::Font
Font(const std::string &fileName, int fontSize, int *fontChars, int charCount)
Definition: Font.hpp:44
raylib::Font::set
void set(const Font &font)
Definition: Font.hpp:133
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:106