Allolib  1.0
C++ Components For Interactive Multimedia
al_Font.hpp
1 #ifndef INCLUDE_AL_FONT_HPP
2 #define INCLUDE_AL_FONT_HPP
3 
4 /* Allocore --
5  Multimedia / virtual environment application class library
6 
7  Copyright (C) 2009. AlloSphere Research Group, Media Arts & Technology,
8  UCSB. Copyright (C) 2012. The Regents of the University of California. All
9  rights reserved.
10 
11  Redistribution and use in source and binary forms, with or without
12  modification, are permitted provided that the following conditions are met:
13 
14  Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16 
17  Redistributions in binary form must reproduce the above copyright
18  notice, this list of conditions and the following disclaimer in the
19  documentation and/or other materials provided with the distribution.
20 
21  Neither the name of the University of California nor the names of its
22  contributors may be used to endorse or promote products derived from
23  this software without specific prior written permission.
24 
25  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
29  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  POSSIBILITY OF SUCH DAMAGE.
36 
37 
38  File description:
39  (previous) Abstraction over the FreeType library
40  Abstraction over the stb font library
41 
42  File author(s):
43  Graham Wakefield, 2010, grrrwaaa@gmail.com
44  Keehong Youn, 2019, younkeehong@gmail.com
45 */
46 
47 #include <map>
48 
49 #include "al/graphics/al_Graphics.hpp"
50 #include "al/graphics/al_Mesh.hpp"
51 #include "al/graphics/al_Texture.hpp"
52 
53 namespace al {
54 
60 struct Font {
61  struct Impl;
62  Impl *impl;
63  Texture tex;
64  float alignFactorX = 0.0f;
65 
66  Font();
67 
68  // no copy
69  Font(const Font &) = delete;
70  Font &operator=(const Font &) = delete;
71 
72  // but movable
73  Font(Font &&other) noexcept;
74  Font &operator=(Font &&other) noexcept;
75 
76  ~Font();
77 
83  bool load(const char *filename, int fontSize, int bitmapSize);
84 
102  void write(Mesh &mesh, const char *text, float worldHeight);
103 
105  // float width(const char* text) const;
106 
108  // float width(int c) const;
109 
111  // float ascender() const;
112 
114  // float descender() const;
115 
117  // float size() const { return (float)mFontSize; }
118 
120 
125  // void align(float xfrac, float yfrac);
126 
127  void alignLeft() { alignFactorX = 0.0f; }
128  void alignCenter() { alignFactorX = -0.5f; }
129  void alignRight() { alignFactorX = -1.0f; }
130 
131  static std::string defaultFont();
132 };
133 
138 struct FontRenderer : public Font {
139  Mesh fontMesh;
140 
141  void write(const char *text, float worldHeight = 1.0f) {
142  Font::write(fontMesh, text, worldHeight);
143  }
144 
145  static void render(Graphics &g, const char *text, Vec3d position,
146  float worldHeight = 1.0, int fontSize = 24,
147  const char *filename = Font::defaultFont().c_str(),
148  int bitmapSize = 1024) {
149  static std::map<std::string, FontRenderer> renderers;
150  if (renderers.find(std::string(filename)) == renderers.end()) {
151  renderers[std::string(filename)] = FontRenderer();
152  renderers[filename].load(filename, fontSize, bitmapSize);
153  }
154  // FIXME check bitmap size for case when font is reused with different
155  // resolution
156  FontRenderer &renderer = renderers[filename];
157  renderer.write(text, worldHeight);
158  renderer.renderAt(g, position);
159  }
160 
161  void render(Graphics &g) {
162  g.blending(true);
163  g.blendTrans();
164  g.texture();
165  tex.bind();
166  g.draw(fontMesh);
167  tex.unbind();
168  }
169 
170  void renderAt(Graphics &g, Vec3d position) {
171  g.pushMatrix();
172  g.translate(position);
173  render(g);
174  g.popMatrix();
175  }
176 }; // namespace al
177 
178 } // namespace al
179 
180 #endif
Interface for loading fonts and rendering text.
Definition: al_Graphics.hpp:63
void blending(bool doBlend)
Turn blending on/off.
Definition: al_Graphics.hpp:81
void blendTrans()
Set blend mode to transparent (asymmetric)
Stores buffers related to rendering graphical objects.
Definition: al_Mesh.hpp:62
void translate(float x, float y, float z=0.)
Translate current matrix.
void popMatrix()
Pop current matrix stack.
void pushMatrix()
Push current matrix stack.
void unbind(int binding_point=0)
Unbind the texture (from a multitexture unit)
void bind(int binding_point=0)
Definition: al_App.hpp:23
Interface for loading fonts and rendering text.
Definition: al_Font.hpp:60
void write(Mesh &mesh, const char *text, float worldHeight)
void alignLeft()
Returns the width of a text string, in pixels.
Definition: al_Font.hpp:127
bool load(const char *filename, int fontSize, int bitmapSize)
Convenience class to simplify rendering text.
Definition: al_Font.hpp:138