Spinning Topp Logo BlackTopp Studios
inc
font.cpp
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 #ifndef _uifont_cpp
41 #define _uifont_cpp
42 
43 #include "UI/font.h"
44 #include "UI/glyph.h"
45 #include "UI/textureatlas.h"
46 
47 namespace Mezzanine
48 {
49  namespace UI
50  {
52  Atlas(ParentAtlas),
53  SpaceLength(0),
54  LineHeight(0),
55  BaseLine(0),
56  LetterSpacing(0)
57  { }
58 
60  {
61  for( GlyphIterator GlyphIt = this->Glyphs.begin() ; GlyphIt != this->Glyphs.end() ; ++GlyphIt )
62  {
63  delete (*GlyphIt).second;
64  }
65  this->Glyphs.clear();
66  }
67 
69  {
70  Whitespace->AtlasCoords[UI::QC_TopLeft] = Atlas->GetWhitePixel();
71  Whitespace->AtlasCoords[UI::QC_TopRight] = Atlas->GetWhitePixel();
72  Whitespace->AtlasCoords[UI::QC_BottomLeft] = Atlas->GetWhitePixel();
73  Whitespace->AtlasCoords[UI::QC_BottomRight] = Atlas->GetWhitePixel();
74  }
75 
76  ///////////////////////////////////////////////////////////////////////////////
77  // Utility Methods
78 
80  { return this->SpaceLength; }
81 
83  { return this->LineHeight; }
84 
86  { return this->BaseLine; }
87 
89  { return this->LetterSpacing; }
90 
91  const String& FontData::GetName() const
92  { return this->FontName; }
93 
95  { return this->Atlas; }
96 
98  { return this->Atlas->GetName(); }
99 
100  Glyph* FontData::GetGlyph(const UInt32& GlyphID) const
101  {
102  ConstGlyphIterator GlyphIt = this->Glyphs.find(GlyphID);
103  if( GlyphIt != this->Glyphs.end() ) return (*GlyphIt).second;
104  else return NULL;
105  }
106 
107  ///////////////////////////////////////////////////////////////////////////////
108  // Internal Methods
109 
111  { this->SpaceLength = SL; }
112 
114  { this->LineHeight = LH; }
115 
117  { this->BaseLine = BL; }
118 
120  { this->LetterSpacing = LS; }
121 
122  void FontData::_SetName(const String& Name)
123  { this->FontName = Name; }
124 
125  void FontData::_AddGlyph(Glyph* NewGlyph)
126  {
127  GlyphIterator GlyphIt = this->Glyphs.find(NewGlyph->GlyphID);
128  if( GlyphIt == this->Glyphs.end() ) { this->Glyphs.insert(std::pair<UInt32,Glyph*>(NewGlyph->GlyphID,NewGlyph)); }
129  else
130  {
131  StringStream ExceptionStream;
132  ExceptionStream << "Glyph of ID: " << NewGlyph->GlyphID << " already exists in Font: \"" << this->FontName << "\".";
134  }
135  }
136 
138  {
139  Glyph* WhiteSpace = NULL;
140  // Create a glyph for a space
141  WhiteSpace = new Glyph(this,this->Atlas,Glyph::Space,this->SpaceLength,0);
142  this->SetWhitespaceAtlasCoords(WhiteSpace);
143  this->_AddGlyph(WhiteSpace);
144  // Create a glyph for a tab
145  WhiteSpace = new Glyph(this,this->Atlas,Glyph::HT,this->SpaceLength * 4,0);
146  this->SetWhitespaceAtlasCoords(WhiteSpace);
147  this->_AddGlyph(WhiteSpace);
148  // Create a glyph for a vertical tab
149  WhiteSpace = new Glyph(this,this->Atlas,Glyph::VT,this->SpaceLength * 4,0);
150  this->SetWhitespaceAtlasCoords(WhiteSpace);
151  this->_AddGlyph(WhiteSpace);
152  // Create a glyph for a line feed
153  WhiteSpace = new Glyph(this,this->Atlas,Glyph::LF,0,0);
154  this->SetWhitespaceAtlasCoords(WhiteSpace);
155  this->_AddGlyph(WhiteSpace);
156  // Create a glyph for carriage return
157  WhiteSpace = new Glyph(this,this->Atlas,Glyph::CR,0,0);
158  this->SetWhitespaceAtlasCoords(WhiteSpace);
159  this->_AddGlyph(WhiteSpace);
160  // Create a glyph for next line
161  WhiteSpace = new Glyph(this,this->Atlas,Glyph::NEL,0,0);
162  this->SetWhitespaceAtlasCoords(WhiteSpace);
163  this->_AddGlyph(WhiteSpace);
164  }
165  }//UI
166 }//Mezzanine
167 
168 #endif
void _SetSpaceLength(const Real SL)
Sets the length of a space for this font.
Definition: font.cpp:110
Thrown when duplicates of teh same identity string exist.
Definition: exception.h:95
Next Line/Newline.
Definition: glyph.h:78
Real GetLineHeight() const
Gets the height of a line of text in this font.
Definition: font.cpp:82
const String & GetAtlasName() const
Gets the name of the TextureAtlas this font belongs to.
Definition: font.cpp:97
const String & GetName() const
Gets the name of this Texture Atlas.
Class used to describe a single glyph or character available for text operations. ...
Definition: glyph.h:59
~FontData()
Class destructor.
Definition: font.cpp:59
void _SetLetterSpacing(const Real LS)
Sets the LetterSpacing of this font.
Definition: font.cpp:119
void _GenerateWhitespaceGlyphs()
Generates Whitespace Glyphs from this font's data for this font.
Definition: font.cpp:137
void _SetName(const String &Name)
Sets the name of this font.
Definition: font.cpp:122
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
Real GetSpaceLength() const
Gets the length of a space in this font.
Definition: font.cpp:79
GlyphContainer::iterator GlyphIterator
Iterator type for Glyphs stored by this class.
Definition: font.h:61
Real SpaceLength
The width in pixels of a space in this font.
Definition: font.h:76
This is a collection of smaller textures packed into a larger texture, intended to increase UI perfor...
Definition: textureatlas.h:71
Vertical Tab.
Definition: glyph.h:74
FontData(TextureAtlas *ParentAtlas)
Class constructor.
Definition: font.cpp:51
std::stringstream StringStream
A Datatype used for streaming operations with strings.
Definition: datatypes.h:176
Carriage Return.
Definition: glyph.h:75
void SetWhitespaceAtlasCoords(Glyph *Whitespace)
Sets the dummy coordinates for a generated whitespace glyph.
Definition: font.cpp:68
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
Real BaseLine
The height of the largest glyph in this font.
Definition: font.h:82
Horizontal Tab.
Definition: glyph.h:72
GlyphContainer::const_iterator ConstGlyphIterator
Const Iterator type for Glyphs stored by this class.
Definition: font.h:63
Real LetterSpacing
I don't know.
Definition: font.h:85
GlyphContainer Glyphs
Container storing all the Glyphs contained in this font.
Definition: font.h:67
uint32_t UInt32
An 32-bit unsigned integer.
Definition: datatypes.h:126
const String & GetName() const
Gets the name of this font.
Definition: font.cpp:91
void _SetBaseLine(const Real BL)
Sets the height of the largest glyph in this font.
Definition: font.cpp:116
Vector2 AtlasCoords[4]
The 4 corner coordinates on the Texture.
Definition: glyph.h:111
TextureAtlas * GetAtlas() const
Gets the TextureAtlas this font belongs to.
Definition: font.cpp:94
Line Feed/Newline.
Definition: glyph.h:73
void _SetLineHeight(const Real LH)
Sets the height of a single line of text in this font.
Definition: font.cpp:113
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
Real GetBaseLine() const
Gets the height of the largest glyph in this font.
Definition: font.cpp:85
Vector2 GetWhitePixel() const
Gets the location of the WhitePixel on the TextureAtlas.
String FontName
The name of this font.
Definition: font.h:70
TextureAtlas * Atlas
The Atlas that this GlyphData belongs to.
Definition: font.h:73
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
void _AddGlyph(Glyph *NewGlyph)
Adds a new glyph to this Font. exception will be thrown if a glyph with the same ID is already taken...
Definition: font.cpp:125
UInt32 GlyphID
The Character this glyph information represents.
Definition: glyph.h:105
Glyph * GetGlyph(const UInt32 &GlyphID) const
Gets the glyph corresponding to the provided characters UTF-8 code.
Definition: font.cpp:100
Real GetLetterSpacing() const
Gets the spacing to apply between letters in this font.
Definition: font.cpp:88
Real LineHeight
The height of a line of text in this font.
Definition: font.h:79