Mitsuba Renderer  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
font.h
Go to the documentation of this file.
1 /*
2  This file is part of Mitsuba, a physically based rendering system.
3 
4  Copyright (c) 2007-2014 by Wenzel Jakob and others.
5 
6  Mitsuba is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License Version 3
8  as published by the Free Software Foundation.
9 
10  Mitsuba is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #if !defined(__MITSUBA_HW_FONT_H_)
21 #define __MITSUBA_HW_FONT_H_
22 
23 #include <mitsuba/hw/renderer.h>
24 
26 
27 /** \brief Utility class used to render text inside OpenGL programs
28  * using pre-rasterized TrueType fonts stored as textures.
29  *
30  * A FreeType2-based generation tool is located in the directory
31  * 'tools/linux/fontgen'. Only Latin-1 is supported at the moment.
32  * \ingroup libhw
33  */
34 class MTS_EXPORT_HW Font : public Object {
35 public:
36  /// Glyph metrics data structure
37  struct Glyph {
38  /// Position on the font texture
40 
41  /// Size on the font texture
43 
44  /// Glyph size in pixels
46 
47  /** \brief Horizontal bearing of this glyph
48  *
49  * (# of pixels between the pen position before
50  * having drawn the glyph and the left bounding
51  * box edge)
52  */
54 
55  /** \brief Vertical bearing of this glyph
56  *
57  * (Vertical distance between the baseline and the
58  * top of the glyph bounding box)
59  */
60  int32_t verticalBearing;
61 
62  /** \brief Horizontal advance value of this glyph
63  *
64  * (# of pixels the pen must be advanced
65  * after rendering a glyph)
66  */
68  };
69 
70  /// List of supplied fonts
71  enum EFont {
73  EBitstreamVeraMono14
74  };
75 
76  /// Allocate memory for a certain font
77  Font(EFont font);
78 
79  /// Draw text to the specified bitmap
80  void drawText(Bitmap *dest, Point2i pos, const std::string &text) const;
81 
82  /// Compute the size covered by the given string when rendered using this font
83  Vector2i getSize(const std::string &text) const;
84 
85  /// Upload the font to the GPU
86  void init(Renderer *renderer);
87 
88  /// Free the GPU memory
89  void cleanup();
90 
91  /// Convert the underlying bitmap to a different pixel format
92  void convert(Bitmap::EPixelFormat pixelFormat,
93  Bitmap::EComponentFormat componentFormat, Float gamma);
94 
95  /// Return the name of this font
96  inline const std::string &getName() const { return m_name; }
97 
98  /// Return an entry from the kerning table
99  inline int8_t getKerning(char i, char o) const {
100  return m_kerningMatrix[(uint8_t) i + (uint8_t) o*256];
101  }
102 
103  /// Return the associated texture
104  inline GPUTexture *getTexture() { return m_texture; }
105 
106  /// Return the associated texture (const version)
107  inline const GPUTexture *getTexture() const { return m_texture.get(); }
108 
109  /// Return the glyph data structure for a specified character
110  inline const Glyph &getGlyph(char c) const { return m_glyphs[(uint8_t) c]; }
111 
112  /// Return the max. vertical bearing
113  inline int getMaxVerticalBearing() const { return m_maxVerticalBearing; }
114 
116 protected:
117  /// Virtual destructor
118  virtual ~Font();
119 private:
120  std::string m_name;
121  ref<GPUTexture> m_texture;
122  ref<Bitmap> m_bitmap;
123  Glyph m_glyphs[256];
124  int8_t m_kerningMatrix[256*256];
125  int m_maxVerticalBearing;
126 };
127 
129 
130 #endif /* __MITSUBA_HW_FONT_H_ */
GPUTexture * getTexture()
Return the associated texture.
Definition: font.h:104
General-purpose bitmap class with read and write support for several common file formats.
Definition: bitmap.h:50
Vector2i size
Glyph size in pixels.
Definition: font.h:45
Utility class used to render text inside OpenGL programs using pre-rasterized TrueType fonts stored a...
Definition: font.h:34
Glyph metrics data structure.
Definition: font.h:37
#define MTS_EXPORT_HW
Definition: platform.h:114
EPixelFormat
Definition: bitmap.h:61
const std::string & getName() const
Return the name of this font.
Definition: font.h:96
#define MTS_NAMESPACE_BEGIN
Definition: platform.h:137
const GPUTexture * getTexture() const
Return the associated texture (const version)
Definition: font.h:107
int8_t getKerning(char i, char o) const
Return an entry from the kerning table.
Definition: font.h:99
int32_t verticalBearing
Vertical bearing of this glyph.
Definition: font.h:60
Point2 tx
Position on the font texture.
Definition: font.h:39
Definition: font.h:72
#define MTS_DECLARE_CLASS()
This macro must be used in the initial definition in classes that derive from Object.
Definition: class.h:158
Definition: fwd.h:99
Reference counting helper.
Definition: ref.h:40
int32_t horizontalAdvance
Horizontal advance value of this glyph.
Definition: font.h:67
Abstract renderer implementation.
Definition: renderer.h:79
A data structure for 1/2/3D and cube texture mapping. Also has optional render-to-texture functionali...
Definition: gputexture.h:32
const Glyph & getGlyph(char c) const
Return the glyph data structure for a specified character.
Definition: font.h:110
EFont
List of supplied fonts.
Definition: font.h:71
EComponentFormat
Supported per-component data formats.
Definition: bitmap.h:97
Parent of all Mitsuba classes.
Definition: object.h:38
int getMaxVerticalBearing() const
Return the max. vertical bearing.
Definition: font.h:113
Definition: fwd.h:95
#define MTS_NAMESPACE_END
Definition: platform.h:138
Vector2 ts
Size on the font texture.
Definition: font.h:42
int32_t horizontalBearing
Horizontal bearing of this glyph.
Definition: font.h:53