fixed: let the material fill the override chunk block
[opensg.git] / Source / System / Text / OSGTextTXFFace.h
blob244ec0cb127d5647c63dfb0198ef20b382b0f275
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 #ifndef _OSGTEXTTXFFACE_H_
40 #define _OSGTEXTTXFFACE_H_
42 #ifdef _MSC_VER
43 # pragma once
44 #endif
47 #include "OSGConfig.h"
48 #include "OSGTextDef.h"
49 #include "OSGBaseTypes.h"
50 #include "OSGGeometry.h"
51 #include "OSGNode.h"
52 #include "OSGImage.h"
54 #include "OSGTextFace.h"
55 #include "OSGTextGlyph.h"
56 #include "OSGTextTXFParam.h"
58 #include <string>
59 #include <map>
60 #include <iosfwd>
63 OSG_BEGIN_NAMESPACE
66 class TextTXFGlyph;
67 class TextLayoutParam;
68 class TextLayoutResult;
71 /**
72 * Represents a TXF face. A TXF face is a texture containing a set of
73 * glyphs. It allows to take geometries consisting of small rectangles
74 * onto which one glyph of the texture is mapped, respectively. You
75 * can load TXF faces from TXF files, or you can create TXF faces from
76 * the faces installed on the system. The following piece of code
77 * demonstrates how to create and use %TextTXFFace objects.
79 * @code
80 * // Includes
81 * #include "OSGTextTXFFace.h"
82 * #include "OSGTextLayoutParam.h"
83 * #include "OSGTextLayoutResult.h"
85 * // Try to create a new %TextTXFFace object. The create
86 * // method returns 0 in case of an error.
87 * // Make sure to use a RefPtr for the face as they are cached and there may
88 * // be more than one user.
89 * TextTXFFaceRefPtr face = TextTXFFace::create("SANS");
90 * if (face == 0)
91 * ; // error handling
93 * // Lay out a single line of text. There are lots of parameters
94 * // you can set in the layoutParam object, but for now we are
95 * // satisfied with the default values. See the documentation
96 * // of the TextLayoutParam class for more information.
97 * TextLayoutParam layoutParam;
98 * TextLayoutResult layoutResult;
99 * face->layout("Hello World!", layoutParam, layoutResult);
101 * // Create the geometry using the layout information returned
102 * // from the previous call to the layout method.
103 * Real32 scale = 2.f; // This is the height of the glyphs
104 * GeometryRefPtr geo = face->makeGeo(layoutResult, scale);
106 * // Get the texture. You have to map this texture onto the
107 * // geometry we created above.
108 * ImageRefPtr img = face->getTexture();
110 * // We do not need the TXF face anymore, so clear the pointer or let it go
111 * // out of scope.
112 * face = NULL;
113 * @endcode
115 * @author Patrick D&auml;hne
117 * \ingroup GrpTextFaces
118 * \ingroup GrpLibOSGText
121 class OSG_TEXT_DLLMAPPING TextTXFFace: public TextFace
123 /*========================== PUBLIC =================================*/
124 public:
125 /*---------------------------------------------------------------------*/
126 /*! \name Types */
127 /*! \{ */
129 typedef TextFace Inherited;
130 typedef TextTXFFace Self;
132 typedef TransitPtr <Self > ObjTransitPtr;
133 typedef RefCountPtr<Self, MemObjRefCountPolicy> ObjRefPtr;
135 /*! \} */
136 /*---------------------------------------------------------------------*/
139 * Returns the scaling factor.
140 * @return The scaling factor
142 inline Real32 getScale() const;
145 * Returns the parameters of the face.
146 * @return The parameters.
148 inline const TextTXFParam &getParam() const;
151 * Returns the texture that contains all glyphs.
152 * @return The texture. Do not modify this texture in
153 * any way!
155 inline Image *getTexture() const;
158 * Returns information about a glyph.
159 * @param glyphIndex The index of the glyph. Use the layout method
160 * to get the glyph indices corresponding to a character string.
161 * @return A glyph object containing information about the glyph.
163 virtual const TextGlyph &getGlyph(TextGlyph::Index glyphIndex);
166 * Returns information about a glyph.
167 * @param glyphIndex The index of the glyph. Use the layout method
168 * to get the glyph indices corresponding to a character string.
169 * @return A glyph object containing information about the glyph.
171 const TextTXFGlyph &getTXFGlyph(TextGlyph::Index glyphIndex);
174 * Lays out one line of text.
175 * @param utf8Text The UTF8 encoded text.
176 * @param param Contains parameters that affect the layout process.
177 * @param result Gets filled with the layout results.
179 virtual void layout(const std::string &utf8Text,
180 const TextLayoutParam &param,
181 TextLayoutResult &result);
184 * Lays out one line of text.
185 * @param text The text.
186 * @param param Contains parameters that affect the layout process.
187 * @param result Gets filled with the layout results.
189 virtual void layout(const std::wstring &text,
190 const TextLayoutParam &param,
191 TextLayoutResult &result);
194 * Lays out multiple lines of text.
195 * @param lines The vector of UTF8 encoded lines.
196 * @param param Contains parameters that affect the layout process.
197 * @param result Gets filled with the layout results.
199 virtual void layout(const std::vector<std::string> &lines,
200 const TextLayoutParam &param,
201 TextLayoutResult &result);
204 * Lays out multiple lines of text.
205 * @param lines The vector of text lines.
206 * @param param Contains parameters that affect the layout process.
207 * @param result Gets filled with the layout results.
209 virtual void layout(const std::vector<std::wstring> &lines,
210 const TextLayoutParam &param,
211 TextLayoutResult &result);
214 * Fills a geometry with a new text.
215 * @param geoPtr The geometry that gets filled with the new text.
216 * @param layoutResult The result of a layout operation.
217 * @param scale The size of the glyphs.
218 * @param offset Amount to offset the positions in the layout.
219 * @param color The color to use for the text. If not specified, then we will not add color container.
221 void fillGeo(Geometry *geoPtr, const TextLayoutResult &layoutResult, Real32 scale = 1.f,
222 Vec2f offset = Vec2f(0,0), Color3f color = Color3f(-1,-1,-1));
225 * Adds geometry for new text to an existing text geometry.
226 * @param geoPtr The geometry that gets filled with the new text.
227 * @param layoutResult The result of a layout operation.
228 * @param scale The size of the glyphs.
229 * @param offset Amount to offset the positions in the layout.
230 * @param color The color to use for the text.
231 * @note Iff initial fill or add call used non-default color, the color parameter will be used.
233 void addToGeom(Geometry *geoPtr, const TextLayoutResult &layoutResult, Real32 scale = 1.f,
234 Vec2f offset = Vec2f(0,0), Color3f color = Color3f(-1,-1,-1));
237 * Creates a new text geometry.
238 * @param layoutResult The result of a layout operation.
239 * @param scale The size of the glyphs.
240 * @param offset Amount to offset the positions in the layout.
241 * @param color The color to use for the text.
242 * @return A new text geometry.
244 GeometryTransitPtr makeGeo(const TextLayoutResult &layoutResult, Real32 scale = 1.f,
245 Vec2f offset = Vec2f(0,0), Color3f color = Color3f(-1,-1,-1));
248 * Creates a new node with a text geometry.
249 * @param layoutResult The result of a layout operation.
250 * @param scale The size of the glyphs.
251 * @param offset Amount to offset the positions in the layout.
252 * @param color The color to use for the text.
253 * @return A new node containing a text geometry.
255 NodeTransitPtr makeNode(const TextLayoutResult &layoutResult, Real32 scale = 1.f,
256 Vec2f offset = Vec2f(0,0), Color3f color = Color3f(-1,-1,-1));
259 * Helper method to draw the results of a layout using this face.
260 * @param layoutResult The results of a previous layout
261 * @pre The texture for this face *must* be active before calling this method
263 void drawCharacters(const TextLayoutResult &layoutResult);
266 * Tries to create a TXF face.
267 * @param family The font family of the face (Arial, Courier etc.)
268 * @param style The style of the face (bold, italic etc.)
269 * @param param Parameters that affect the creation of the
270 * TXF face.
271 * @return The TXF face object or 0 in case of an error.
273 static ObjTransitPtr create(
274 const std::string &family,
275 Style style = STYLE_PLAIN,
276 const TextTXFParam &param = TextTXFParam());
279 * Reads a TXF face from an input stream.
280 * @param is The input stream.
281 * @param family The font family
282 * @param style The font style
283 * @return The TXF face or 0 in case of an error.
285 static ObjTransitPtr createFromStream(
286 std::istream &is,
287 const std::string &family = std::string(),
288 Style style = STYLE_PLAIN );
291 * Reads a TXF face from a file.
292 * @param filename The name of the TXF file.
293 * @return The TXF face or 0 in case of an error.
295 static ObjTransitPtr createFromFile(const std::string &filename);
298 * Writes a TXF face to an output stream.
299 * @param os The output stream.
300 * @return false in case of an error.
302 bool writeToStream(std::ostream &os) const;
305 * Writes a TXF face to a TXF file.
306 * @param filename The name of the TXF file.
307 * @return false in case of an error.
309 bool writeToFile(const std::string &filename) const;
311 /*========================= PROTECTED ===============================*/
312 protected:
314 /** Creates a new %TextTXFFace object. */
315 inline TextTXFFace();
317 /** Destroys the %TextTXFFace object. */
318 virtual ~TextTXFFace();
320 /** Calculates the positions of the glyphs on the texture */
321 void prepareTexture(const TextTXFParam &param);
323 /** The scale factor used to scale font metrics */
324 Real32 _scale;
326 /** The parameters of the face */
327 TextTXFParam _param;
329 /** The texture that contains all glyphs */
330 ImageRefPtr _texture;
332 /** Defines a map of glyphs */
333 typedef std::map<TextGlyph::Index, TextTXFGlyph*> GlyphMap;
335 /** The map of glyphs */
336 GlyphMap _glyphMap;
338 /*========================== PRIVATE ================================*/
339 private:
341 /** Copy constructor (not implemented!) */
342 TextTXFFace(const TextTXFFace &);
344 /** Copy operator (not implemented!) */
345 const TextTXFFace &operator=(const TextTXFFace &);
347 /** An empty glyph */
348 static TextTXFGlyph _emptyGlyph;
351 typedef TextTXFFace::ObjTransitPtr TextTXFFaceTransitPtr;
352 typedef TextTXFFace::ObjRefPtr TextTXFFaceRefPtr;
354 OSG_END_NAMESPACE
357 #include "OSGTextTXFFace.inl"
359 #endif /* _OSGTEXTTXFFACE_H_ */