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 
85  inline Font& DrawText(const std::string& text, ::Vector2 position, float fontSize,
86  float spacing, ::Color tint = WHITE) {
87  ::DrawTextEx(*this, text.c_str(), position, fontSize, spacing, tint);
88  return *this;
89  }
90 
91  inline Font& DrawText(const std::string& text, ::Rectangle rec, float fontSize, float spacing,
92  bool wordWrap = false, ::Color tint = WHITE) {
93  ::DrawTextRec(*this, text.c_str(), rec, fontSize, spacing, wordWrap, tint);
94  return *this;
95  }
96 
100  inline Font& DrawText(const std::string& text, ::Rectangle rec, float fontSize, float spacing,
101  bool wordWrap, ::Color tint, int selectStart, int selectLength, ::Color selectText,
102  ::Color selectBack) {
103  ::DrawTextRecEx(*this, text.c_str(), rec, fontSize, spacing, wordWrap, tint,
104  selectStart, selectLength, selectText, selectBack);
105  return *this;
106  }
107 
111  inline Font& DrawText(int codepoint,
112  ::Vector2 position,
113  float fontSize,
114  ::Color tint = { 255, 255, 255, 255 }) {
115  ::DrawTextCodepoint(*this, codepoint, position, fontSize, tint);
116  return *this;
117  }
118 
122  inline Vector2 MeasureText(const std::string& text, float fontSize, float spacing) const {
123  return ::MeasureTextEx(*this, text.c_str(), fontSize, spacing);
124  }
125 
129  inline int GetGlyphIndex(int character) const {
130  return ::GetGlyphIndex(*this, character);
131  }
132 
136  inline ::Image ImageText(const std::string& text, float fontSize,
137  float spacing, ::Color tint) const {
138  return ::ImageTextEx(*this, text.c_str(), fontSize, spacing, tint);
139  }
140 
141  protected:
142  void set(const ::Font& font) {
143  baseSize = font.baseSize;
144  charsCount = font.charsCount;
145  charsPadding = font.charsPadding;
146  texture = font.texture;
147  recs = font.recs;
148  chars = font.chars;
149  }
150 };
151 } // namespace raylib
152 
153 #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:142
raylib::Font::DrawText
Font & DrawText(const std::string &text, ::Vector2 position, float fontSize, float spacing, ::Color tint=WHITE)
Draw text using font and additional parameters.
Definition: Font.hpp:85
raylib::Font::Unload
void Unload()
Definition: Font.hpp:66
raylib::Font::DrawText
Font & DrawText(const std::string &text, ::Rectangle rec, float fontSize, float spacing, bool wordWrap=false, ::Color tint=WHITE)
Definition: Font.hpp:91
raylib::Font::Font
Font()
Definition: Font.hpp:36
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::DrawText
Font & DrawText(int codepoint, ::Vector2 position, float fontSize, ::Color tint={ 255, 255, 255, 255 })
Draw one character (codepoint)
Definition: Font.hpp:111
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:129
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::Color
Definition: Color.hpp:35
raylib::Font::ImageText
inline ::Image ImageText(const std::string &text, float fontSize, float spacing, ::Color tint) const
Create an image from text (custom sprite font)
Definition: Font.hpp:136
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:122
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)
Draw text using font inside rectangle limits with support for text selection.
Definition: Font.hpp:100