libmoldeo (Moldeo 1.0 Core)  1.0
libmoldeo is the group of objects and functions that executes the basic operations of Moldeo 1.0 Platform.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
moFontManager.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 
3  moFontManager.cpp
4 
5  ****************************************************************************
6  * *
7  * This source is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This code is distributed in the hope that it will be useful, but *
13  * WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
15  * General Public License for more details. *
16  * *
17  * A copy of the GNU General Public License is available on the World *
18  * Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
19  * obtain it by writing to the Free Software Foundation, *
20  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  * *
22  ****************************************************************************
23 
24  Copyright(C) 2006 Fabricio Costa
25 
26  Authors:
27  Fabricio Costa
28 
29 
30 *******************************************************************************/
31 
32 #include "moFontManager.h"
33 #include <moDataManager.h>
34 #include <moFileManager.h>
35 #include <moTextureManager.h>
36 
37 #include "FTGL/ftgl.h"
38 
39 #include "moArray.h"
41 
42 //===========================================
43 //
44 // moFontManager
45 //
46 //===========================================
47 
49  SetType(MO_OBJECT_RESOURCE);
50  SetResourceType(MO_RESOURCETYPE_FONT);
51 
52  SetName("fontmanager");
53  SetLabelName("fontmanager");
54 }
55 
57 
58 }
59 
62 
63  m_Fonts.Init(0, NULL);
64 
65 
66 
67  moFont *pFont = NULL;
68 
69  pFont = new moFont();
70 
71  if (pFont) {
72 
73  moText completepath;
74 
75  //completepath = m_pResourceManager->GetDataMan()->GetDataPath() + moText("/");
76  completepath = m_pResourceManager->GetDataMan()->GetAppDataPath() + "/fonts/Tuffy.ttf";
77 
78  pFont->Init( MO_FONT_OUTLINE, completepath, 16 );
79 
80 
81  //pFont->Init( MO_FONT_SOLID, completepath, 16 );
82 
83  }
84 
85  AddFont(pFont);
86 
87  return true;
88 
89 }
90 
93  m_Fonts.Empty();
94  return true;
95 }
96 
97 moFonts*
99  return &m_Fonts;
100 }
101 
102 void
104  m_Fonts.Add( p_pFont );
105 }
106 
107 moFont*
108 moFontManager::AddFont( moText p_fontname, moText p_fonttype, MOfloat p_fontsize ) {
109  return AddFont( p_fontname, GetFontType(p_fonttype), p_fontsize );
110 }
111 
112 moFont*
113 moFontManager::AddFont( moText p_fontname, moFontType p_fonttype, MOfloat p_fontsize ) {
114 
115  moFont* pFont = NULL;
116  if (! (p_fontname.Trim() == moText(""))) {
117  pFont = GetFont( p_fontname, true, p_fonttype, p_fontsize );
118  }
119  return pFont;
120 }
121 
122 moFont*
123 moFontManager::GetFont( moText p_fontname, bool create, moFontType p_fonttype, MOfloat p_fontsize) {
124 
125  moFont* pFont;
126  pFont = NULL;
127 
128  if (! (p_fontname.Trim().Length() == 0)) {
129 
130  if (p_fontname.Trim()==moText("Default") || p_fontname.Trim()==moText("default") || p_fontname.Trim()==moText("DEFAULT") ) {
131  return m_Fonts.GetRef(0);
132  }
133 
134  for(int i=0; i< (int)m_Fonts.Count(); i++) {
135 
136  pFont = m_Fonts.GetRef(i);
137  if (pFont) {
138  if (pFont->GetName() == (moText)p_fontname ) {
139  return pFont;
140  }
141  }
142 
143  }
144 
145  if (create) {
146 
147  moText completepath;
148 
149  completepath = m_pResourceManager->GetDataMan()->GetDataPath() + moSlash;
150  completepath+= moText(p_fontname);
151 
152  moFile fname( completepath );
153  if (!fname.Exists()) {
154  MODebug2->Error(moText("moFontManager: filename does't exists: ") + (moText)completepath );
155  completepath = moText("../../art/fonts/") + moText(p_fontname);
156  }
157 
158  pFont = LoadFont( completepath, p_fonttype, p_fontsize );
159  }
160  } else MODebug2->Error(moText("FontManager:: empty string"));
161  return pFont;
162 }
163 
164 moFont*
166 
167  moFont* pFont = NULL;
168 
169  if( 0<=p_fontid && p_fontid<(int)m_Fonts.Count())
170  return m_Fonts[p_fontid];
171 
172  return pFont;
173 }
174 
175 moFont*
176 moFontManager::GetFont( moValue& p_value, bool create ) {
177 
178  moText fontname;
179  moFontType fonttype;
180  MOint fontsize;
181 
182  switch(p_value.GetSubValueCount()) {
183 
184  case 0:
185  return NULL;
186  break;
187  case 1:
188  fontname = p_value.GetSubValue(0).Text();
189  return GetFont( fontname, create );
190  break;
191  case 2:
192  fontname = p_value.GetSubValue(0).Text();
193  fonttype = (moFontType)p_value.GetSubValue(1).Int();
194  return GetFont( fontname, create, fonttype );
195  break;
196  case 3:
197  fontname = p_value.GetSubValue(0).Text();
198  fonttype = (moFontType)p_value.GetSubValue(1).Int();
199  fontsize = p_value.GetSubValue(2).Int();
200  return GetFont( fontname, create, fonttype, fontsize );
201  break;
202  }
203 
204  return NULL;
205 
206 }
207 
208 moFont*
209 moFontManager::LoadFont( moText p_fontname_path, moFontType p_fonttype, MOfloat p_fontsize ) {
210 
211  moFont *pFont = NULL;
212  moTexture* p_Texture = NULL;
213  int idx = -1;
214 
215  pFont = new moFont();
216 
217  if (pFont) {
218 
219  if ( p_fonttype == MO_FONT_GLBUILD ) {
220 
221  idx = m_pResourceManager->GetTextureMan()->GetTextureMOId( p_fontname_path, true );
222  if (idx>-1) p_Texture = (moTexture*) m_pResourceManager->GetTextureMan()->GetTexture(idx);
223  if (p_Texture)
224  pFont->Init( p_fonttype, p_fontname_path, p_fontsize, p_Texture->GetGLId() );
225  MODebug2->Push( moText("Loaded Bitmap Font: ") + (moText)p_fontname_path );
226 
227  } else if ( pFont->Init( p_fonttype, p_fontname_path, p_fontsize) ) {
228 
229  MODebug2->Push( moText("Loaded FreeType Font: ") + (moText)p_fontname_path );
230 
231  } else {
232 
233  MODebug2->Push( moText("Error: font: ") + (moText)p_fontname_path );
234 
235  return NULL;
236  }
237 
238  AddFont(pFont);
239 
240  }
241 
242  return pFont;
243 }
244 
247 
248  if ( fonttype==moText("TRANSLUCENT") ) {
249  return MO_FONT_TRANSLUCENT;
250  } else if ( fonttype==moText("FILLED") ) {
251  return MO_FONT_FILLED;
252  } else if ( fonttype==moText("GRAYSCALE") ) {
253  return MO_FONT_GRAYSCALE;
254  } else if ( fonttype==moText("MONOCHROME") ) {
255  return MO_FONT_MONOCHROME;
256  } else if ( fonttype==moText("OUTLINE") ) {
257  return MO_FONT_OUTLINE;
258  } else if ( fonttype==moText("SOLID") ) {
259  return MO_FONT_SOLID;
260  } else if ( fonttype==moText("TRANSLUCENTTEXTURE") ) {
262  } else if ( fonttype==moText("UNDEFINED") ) {
263  return MO_FONT_UNDEFINED;
264  } else if ( fonttype==moText("GLBUILD") ) {
265  return MO_FONT_GLBUILD;
266  }
267  return MO_FONT_UNDEFINED;
268 }
269 
270 //===========================================
271 //
272 // moFont
273 //
274 //===========================================
275 
277 
278  m_pFace = NULL;
279  m_FontGLId = -1;
280  m_FontSize = 12;
281 
282 }
283 
285 }
286 
287 
288 MOboolean
290  return true;
291 }
292 
293 MOboolean
294 moFont::Init( moFontType p_Type, moText p_fontname, MOint p_size, MOuint glid ) {
295 
296  glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
297 
298  moFile file(p_fontname);
299 
300  if (!file.Exists()) {
301  MODebug2->Error("moFont:Init filename "+p_fontname+" not found.");
302  return false;
303  }
304 
305  switch( (int)p_Type ) {
306  case MO_FONT_OUTLINE://3d
307  m_pFace = (FTFont*)new FTGLOutlineFont( p_fontname );
308  break;
309  case MO_FONT_TRANSLUCENT://2d
310  m_pFace = (FTFont*)new FTGLBitmapFont( p_fontname );
311  break;
313  m_pFace = (FTFont*)new FTGLTextureFont( p_fontname );
314  break;
315  case MO_FONT_GRAYSCALE://2d
316  m_pFace = (FTFont*)new FTGLPixmapFont( p_fontname );
317  break;
318  case MO_FONT_MONOCHROME://2d
319  m_pFace = (FTFont*)new FTGLPixmapFont( p_fontname );
320  break;
321  case MO_FONT_SOLID://3d extruded (depth)
322  m_pFace = (FTFont*)new FTGLExtrdFont( p_fontname );
323  break;
324  case MO_FONT_FILLED://3d
325  m_pFace = (FTFont*)new FTGLPolygonFont( p_fontname );
326  break;
327  case MO_FONT_GLBUILD:
328  m_FontGLId = glid;
329  BuildFont();
330  break;
331  case MO_FONT_UNDEFINED:
332  MODebug2->Error(moText(" FontManager:: UNDEFINED font type"));
333  m_pFace = NULL;
334  break;
335 
336  }
337 
338 
339  FTFont* FF = (FTFont*) m_pFace;
340 
341  FT_Error FontError;
342 
343  if (FF) FontError = FF->Error();
344 
345  //if (FontError==FT_Er)
346 
347  if ( ( p_Type!=MO_FONT_GLBUILD && ( FF == NULL || FontError!=0 ) ) ||
348  ( p_Type==MO_FONT_UNDEFINED ) || (p_Type==MO_FONT_GLBUILD && (int)m_FontGLId==-1)) {
349 
350  MODebug2->Error(moText("moFont::Init > Could not construct face from ")+(moText)p_fontname);
351 
352  return false;
353 
354  } else {
355 
356  m_Name = p_fontname;
357 
358  MODebug2->Push(moText("moFont::Init > ")+(moText)m_Name);
359 
360  if (FF) {
361 
362  SetSize(p_size);
363 
364  FF->Depth(20);
365 
366  //if (!FF->CharMap(ft_encoding_latin_1)) {
367 
368  //MODebug2->Error(moText("moFont::Init > Could not set charmap to ft_encoding_latin_1 for ")+(moText)m_Name);
369  //if (!FF->CharMap(ft_encoding_latin_2)) {
370  //MODebug2->Error(moText("moFont::Init > Could not set charmap to ft_encoding_latin_2 for ")+(moText)m_Name);
371  if (!FF->CharMap(ft_encoding_unicode)) {
372  MODebug2->Error(moText("moFont::Init > Could not set charmap to ft_encoding_unicode for ")+(moText)m_Name);
373  return false;
374  }
375 
376  //}
377  //}
378 
379  }
380 
381  return true;
382  }
383 
384  return false;
385 }
386 
387 MOboolean
389 
390  FTFont* FF = (FTFont*) m_pFace;
391  if (FF) {
392  delete FF;
393  FF = NULL;
394  }
395 
396  return true;
397 
398 }
399 
400 void
402 
403  m_FontSize = size;
404 
405  FTFont* FF = (FTFont*) m_pFace;
406  if (FF) FF->FaceSize(m_FontSize);
407 
408 }
409 
410 void
412  glColor3f( p_r, p_g, p_b );
413 }
414 
415 void
416 moFont::SetHorizontalJustification( int p_horizontal_justification) {
417 
418  //FTFont* FF = (FTFont*) m_pFace;
419  //if (FF) FF->
420  MODebug2->Message("moFont::SetHorizontalJustification > p_horizontal_justification NOT IMPLEMENTED : "
421  + IntToStr( p_horizontal_justification ) );
422 }
423 
424 void
425 moFont::SetStringRotation( MOfloat p_string_rotation ) {
426 
427  //FTFont* FF = (FTFont*) m_pFace;
428  //if (FF) FF->
429  MODebug2->Message("moFont::SetStringRotation > p_string_rotation NOT IMPLEMENTED: "
430  + FloatToStr( p_string_rotation ) );
431 }
432 
433 void
435  Draw( x, y, text, m_FontSize );
436 }
448 void
449 moFont::Draw( MOfloat x, MOfloat y, moText& text, moFontSize p_fontsize, MOint set, MOfloat sx, MOfloat sy, MOfloat rt ) {
450 
451  FTFont* FF = (FTFont*) m_pFace;
452  if (FF) {
453  SetSize(p_fontsize);
454 
455  FF->Render( text, text.Length(), FTPoint(x,y) );
456  //FF->Render( GetWC( text ), text.Length(), FTPoint(x,y) );
457  }
458 
459  else {
460  //if (m_FontGLId>=0) {
461  this->glPrint( (int)x, (int)y, text, set, sx, sy, rt );
462  //}
463  }
464 }
465 
466 moText
468 
469  return m_Name;
470 
471 }
472 
473 void
474 moFont::BuildFont() // Build Our Font Display List
475 {
476  float cx; // Holds Our X Character Coord
477  float cy; // Holds Our Y Character Coord
478  GLint loop;
479 #ifndef OPENGLESV2
480  m_GLBaseList = glGenLists(256); // Creating 256 Display Lists
481  glBindTexture( GL_TEXTURE_2D, m_FontGLId );
482  glEnable(GL_BLEND);
483  //glBlendFunc(GL_SRC_ALPHA,GL_ONE);
484  glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
485  for(loop=0; loop<256; loop++) // Loop Through All 256 Lists
486  {
487  cx=float((int)(loop%16))/16.0f; // X Position Of Current Character
488  cy=float((int)(loop/16))/16.0f; // Y Position Of Current Character
489 
490  glNewList(m_GLBaseList+loop,GL_COMPILE); // Start Building A List
491  glBegin(GL_QUADS); // Use A Quad For Each Character
492  glTexCoord2f(cx,1.0f-cy-0.0625f); // Texture Coord(Bottom Left)
493  glVertex2i(0,0); // Vertex Coord(Bottom Left)
494  glTexCoord2f(cx+0.0625f,1.0f-cy-0.0625f); // Texture Coord(Bottom Right)
495  glVertex2i(16,0); // Vertex Coord(Bottom Right)
496  glTexCoord2f(cx+0.0625f,1.0f-cy); // Texture Coord(Top Right)
497  glVertex2i(16,16); // Vertex Coord(Top Right)
498  glTexCoord2f(cx,1.0f-cy); // Texture Coord(Top Left)
499  glVertex2i(0,16); // Vertex Coord(Top Left)
500  glEnd(); // Done Building Our Quad(Character)
501  glTranslated(10,0,0); // Move To The Right Of The Character
502  glEndList(); // Done Building The Display List
503  }
504 #endif // Loop Until All 256 Are Built
505 }
506 
507 
508 void
509 moFont::glPrint( GLint x, GLint y, char *string, int set, float scx, float scy, float rt ) // Where The Printing Happens
510 {
511 #ifndef OPENGLESV2
512  if(set>1)
513  {
514  set=1;
515  }
516  glBindTexture( GL_TEXTURE_2D, m_FontGLId );
517  glDisable(GL_DEPTH_TEST); // Disables Depth Testing
518 
519  glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
520  glPushMatrix(); // Store The Projection Matrix
521  glLoadIdentity(); // Reset The Projection Matrix
522  glOrtho(0,800,0,600,-1,1); // Set Up An Ortho Screen
523 
524  glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
525  glPushMatrix(); // Store The Modelview Matrix
526  glLoadIdentity(); // Reset The Modelview Matrix
527  glRotated(rt,0.0,0.0,1.0);
528  glTranslated(x,y,0); // Position The Text(0,0 - Bottom Left)
529  glScalef(scx,scy,1.0f);
530  glListBase( m_GLBaseList -32+(128*set)); // Choose The Font Set(0 or 1)
531  glCallLists(strlen(string),GL_BYTE,string); // Write The Text To The Screen
532  glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
533  glPopMatrix(); // Restore The Old Projection Matrix
534  glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
535  glPopMatrix(); // Restore The Old Projection Matrix
536  //glEnable(GL_DEPTH_TEST); // Enables Depth Testing
537 #endif
538 }
539 
540 void
541 moFont::KillFont() // Delete The Font From Memory
542 {
543 #ifndef OPENGLESV2
544  glDeleteLists( m_GLBaseList, 256); // Delete All 256 Display Lists
545 #endif
546 }
Valor de un Parámetro.
Definition: moValue.h:501
virtual MOboolean Init()
Inicializa el objeto.
void SetHorizontalJustification(int p_horizontal_justification)
void Error(moText p_text)
Anuncia y registra un error.
Definition: moAbstract.cpp:79
clase base para una fuente 3d o 2d
Definition: moFontManager.h:63
moValueBase & GetSubValue(MOint p_indexsubvalue=0)
Definition: moValue.h:539
void glPrint(GLint x, GLint y, char *string, int set, float scx, float scy, float rt)
LIBMOLDEO_API moText0 FloatToStr(double a)
Definition: moText.cpp:1134
virtual MOboolean Finish()
#define MOboolean
Definition: moTypes.h:385
virtual ~moFont()
moText m_Name
Definition: moFontManager.h:98
administrator de fuentes
moFont * GetFont(moText p_fontname, bool create=false, moFontType p_fonttype=MO_FONT_OUTLINE, MOfloat p_fontsize=12)
MOboolean Exists()
Definition: moFile.cpp:436
moDefineDynamicArray(moFonts) moFontManager
#define MOfloat
Definition: moTypes.h:403
GLuint m_FontGLId
void BuildFont()
clase de para manejar textos
Definition: moText.h:75
moTexture * GetTexture(MOuint p_moid)
moResourceManager * m_pResourceManager
Puntero al administrador de recursos.
MOfloat moFontSize
Definition: moFontManager.h:43
Dispositivo de entrada/salida, típicamente, interfaces humanas de IO y datos ( teclado, mouse, tableta, tcp, udp, serial )
Definition: moTypes.h:532
void SetStringRotation(MOfloat p_string_rotation)
moText0 & Trim()
Definition: moText.cpp:604
moText0 moText
Definition: moText.h:291
#define MOint
Definition: moTypes.h:388
moText GetName()
virtual MOboolean Finish()
Finaliza el objeto, libera recursos.
void KillFont()
GLuint m_GLBaseList
moFontType
Definition: moFontManager.h:45
MOuint GetGLId() const
Definition: moTexture.h:224
MOint GetTextureMOId(moParam *param, MOboolean p_create_tex)
clase base para el manejo de una textura
Definition: moTexture.h:78
moFonts * GetFonts()
MOuint Length() const
Definition: moText.cpp:347
void SetForegroundColor(MOfloat p_r, MOfloat p_g, MOfloat p_b)
void Draw(MOfloat x, MOfloat y, moText &text)
static moDebug * MODebug2
Clase de impresión de errores para depuración
Definition: moAbstract.h:225
moFont * AddFont(moText p_fontname, moText p_fonttype="OUTLINE", MOfloat p_fontsize=12)
moDataManager * GetDataMan()
moText Text()
Definition: moValue.cpp:539
virtual ~moFontManager()
void Push(moText p_text)
Apila el mensaje dentro de la pila de mensajes.
Definition: moAbstract.h:115
virtual MOboolean Init()
moText GetAppDataPath()
moText GetDataPath()
#define MOuint
Definition: moTypes.h:387
LIBMOLDEO_API moText0 IntToStr(int a)
Definition: moText.cpp:1070
void SetSize(MOfloat size)
MOint Int() const
Definition: moValue.cpp:773
moFontType GetFontType(moText fonttype)
MOuint GetSubValueCount()
Definition: moValue.h:545
moFontSize m_FontSize
Definition: moFontManager.h:99
void Message(moText p_text)
Anuncia un mensaje al usuario además de guardarlo en el log de texto.
Definition: moAbstract.cpp:114
moFontFace * m_pFace
moTextureManager * GetTextureMan()