Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Font Class Reference

#include <font.h>

Public Member Functions

float size (void) const
 
void setSize (float s)
 
float strokeWeight (void) const
 
void setStrokeWeight (float w)
 
float drawChar (char c) const
 
double charWidth (char c) const
 
double stringWidth (const std::string &str) const
 
double verticalScaling (void) const
 
double horizontalScaling (void) const
 
 Font (float newsize=.1, float weight=NORMAL_STROKE)
 
double strokeWidth (void) const
 
bool operator== (const Font &other)
 
bool operator!= (const Font &other)
 

Protected Member Functions

void calcMetrics (void)
 
void calcMetricsIfNeeded (void) const
 

Protected Attributes

float m_size
 
float m_strokeWeight
 
bool m_needMetrics
 
double m_strokeWidth
 
double m_extraCharWidth
 
double m_spaceCharFixup
 
double m_verticalScaling
 
double m_horizontalScaling
 

Detailed Description

Definition at line 49 of file font.h.

Constructor & Destructor Documentation

Font::Font ( float  newsize = .1,
float  weight = NORMAL_STROKE 
)
inline

Definition at line 90 of file font.h.

90  :
91  m_size( newsize )
92  , m_strokeWeight( weight )
93  , m_needMetrics( true )
94  , m_strokeWidth( 1.0 )
95  , m_extraCharWidth( 0.5 )
96  , m_spaceCharFixup( 10.0 )
97  , m_verticalScaling( 1.0 )
98  , m_horizontalScaling( 1.0 )
99  {}

Member Function Documentation

void Font::calcMetrics ( void  )
protected

Definition at line 50 of file font.cpp.

References g_game, guiMax, m_extraCharWidth, m_horizontalScaling, m_needMetrics, m_spaceCharFixup, m_strokeWidth, m_verticalScaling, REFERENCE_LINE_SPACING, size(), SPACE_CHAR, strokeWeight(), useStroke(), game_data_t::x_resolution, and game_data_t::y_resolution.

Referenced by calcMetricsIfNeeded().

51 {
52  //This is a hack to adjust the font stroke width as the font size goes up.
53  //Since the stroke width is in pixels, we scale the width up as the screen resolution gets
54  //higher. (Currently, this is linear, which doesn't work well at small sizes.)
55  //We also make sure the stroke width is at least 1.0. Otherwise antialiasing causes
56  //some font features to fade out.
57  //My OpenGL (Windows XP) has a max line width of 10. So we get stroke width truncation for font
58  //size over 0.5. This is OK because the curves look really ugly that big anyway.
59  const double referenceStrokeWidth = 20.0; //Best width for a font size of 1.0 (big).
60  const double referenceStrokeWidthResolution = 800; //X-resolution stroke width measured in.
61  const double minimumStrokeWidth = 1.0;
62 
63  const double nonClippedStrokeWidth = size()*referenceStrokeWidth*strokeWeight()
64  *(g_game.x_resolution/referenceStrokeWidthResolution);
65 
66  m_strokeWidth = guiMax( minimumStrokeWidth, nonClippedStrokeWidth );
67  m_needMetrics = false;
68 
69  //Vertical scaling factor:
71  //Horizontal scaling factor. Same as vertical, except we need to make the coord system
72  //the same distance in all directions, so we need to apply the ratio of vert / horiz
73  //resolution. Otherwise the fonts are slightly stretched horizontally -- there
74  //are more pixels horizontally than vertically per unit in the identity coord space.
75  if ( useStroke() )
77  else
78  //Calculation above seems broken... this seems to work for most sizes with bitmap.
80  //The size of a horizontal pixel in reference space.
81  const double horizPixelInRefSpace = REFERENCE_LINE_SPACING/(g_game.x_resolution/2)/size();
82 
83  //Recalculate the extra char width.
84  m_extraCharWidth = horizPixelInRefSpace*m_strokeWidth;
85 
86  //Space character width should be the same as a number.
87  //Numbers are generally all the same width so you can assume they will go into columns.
88  //We use '8' because if the numbers aren't all the same width, '8' is a good, wide number.
89  //What we calculate here is the horizontal translation to get from the actual outline font
90  //space char width to the space char width we want.
91  const double eightWidth = glutStrokeWidth( GLUT_STROKE_ROMAN, '8' )+m_extraCharWidth;
92  m_spaceCharFixup = eightWidth-glutStrokeWidth( GLUT_STROKE_ROMAN, SPACE_CHAR );
93 }
void Font::calcMetricsIfNeeded ( void  ) const
protected

Definition at line 96 of file font.cpp.

References calcMetrics(), and m_needMetrics.

Referenced by charWidth(), drawChar(), horizontalScaling(), stringWidth(), strokeWidth(), and verticalScaling().

97 {
98  if (m_needMetrics) {
99  //calcmetrics is a cache. Doesn't change the "real" state of the object.
100  Font *s = const_cast< Font* > (this);
101  s->calcMetrics();
102  }
103 }
double Font::charWidth ( char  c) const

Definition at line 124 of file font.cpp.

References calcMetricsIfNeeded(), GLUT_WIDTH_HACK, m_extraCharWidth, m_spaceCharFixup, size(), SPACE_CHAR, and useStroke().

Referenced by PaintText::addFragment(), PaintText::parseFragmentsWithCharBreak(), PaintText::parseFragmentsWithWordBreak(), and stringWidth().

125 {
127  if ( useStroke() ) {
128  if (c == SPACE_CHAR) {
129  //Spaces have a special width.
130  const double spaceCharWidth =
131  glutStrokeWidth( GLUT_STROKE_ROMAN, SPACE_CHAR )+m_spaceCharFixup;
132  return spaceCharWidth+GLUT_WIDTH_HACK;
133  }
134  const double charWidth = glutStrokeWidth( GLUT_STROKE_ROMAN, c );
135  return charWidth+m_extraCharWidth+GLUT_WIDTH_HACK;
136  } else {
137  return glutBitmapWidth( GLUT_BITMAP_HELVETICA_12, c )/(size()*2);
138  }
139 }
float Font::drawChar ( char  c) const

Definition at line 106 of file font.cpp.

References calcMetricsIfNeeded(), m_extraCharWidth, m_spaceCharFixup, SPACE_CHAR, and useStroke().

Referenced by drawChars().

107 {
109  if ( useStroke() ) {
110  glutStrokeCharacter( GLUT_STROKE_ROMAN, c );
111  if (c == SPACE_CHAR)
112  //Need to translate back a bit -- the width of the space is too big.
113  glTranslated( m_spaceCharFixup, 0.0, 0.0 );
114  else
115  glTranslated( m_extraCharWidth, 0.0, 0.0 );
116  return 0;
117  } else {
118  glutBitmapCharacter( GLUT_BITMAP_HELVETICA_12, c );
119  return glutBitmapWidth( GLUT_BITMAP_HELVETICA_12, c );
120  }
121 }
double Font::horizontalScaling ( void  ) const

Definition at line 170 of file font.cpp.

References calcMetricsIfNeeded(), and m_horizontalScaling.

Referenced by PaintText::calcLayout().

171 {
173 
174  return m_horizontalScaling;
175 }
bool Font::operator!= ( const Font other)
inline

Definition at line 109 of file font.h.

110  {
111  return !(*this == other);
112  }
bool Font::operator== ( const Font other)
inline

Definition at line 105 of file font.h.

References m_size, and m_strokeWeight.

106  {
107  return m_size == other.m_size && m_strokeWeight == other.m_strokeWeight;
108  }
void Font::setSize ( float  s)
inline

Definition at line 57 of file font.h.

References m_needMetrics, and m_size.

58  {
59  m_size = s;
60  m_needMetrics = true;
61  }
void Font::setStrokeWeight ( float  w)
inline

Definition at line 68 of file font.h.

References m_needMetrics, and m_strokeWeight.

69  {
70  m_strokeWeight = w;
71  m_needMetrics = true;
72  }
float Font::size ( void  ) const
inline

Definition at line 53 of file font.h.

References m_size.

Referenced by PaintText::calcLayout(), calcMetrics(), charWidth(), and Picker::totalCellHeight().

54  {
55  return m_size * 0.5;
56  }
double Font::stringWidth ( const std::string &  str) const

Definition at line 142 of file font.cpp.

References calcMetricsIfNeeded(), charWidth(), and i.

Referenced by PaintText::parseFragmentsWithCharBreak().

143 {
145 
146  double result = 0.0;
147  for (string::const_iterator i = str.begin(); i != str.end(); i++)
148  result += charWidth( *i );
149  return result;
150 }
float Font::strokeWeight ( void  ) const
inline

Definition at line 64 of file font.h.

References m_strokeWeight.

Referenced by calcMetrics().

65  {
66  return m_strokeWeight;
67  }
double Font::strokeWidth ( void  ) const

Definition at line 154 of file font.cpp.

References calcMetricsIfNeeded(), and m_strokeWidth.

Referenced by drawChars().

155 {
157 
158  return m_strokeWidth;
159 }
double Font::verticalScaling ( void  ) const

Definition at line 162 of file font.cpp.

References calcMetricsIfNeeded(), and m_verticalScaling.

Referenced by PaintText::calcLayout().

163 {
165 
166  return m_verticalScaling;
167 }

Member Data Documentation

double Font::m_extraCharWidth
protected

Definition at line 132 of file font.h.

Referenced by calcMetrics(), charWidth(), and drawChar().

double Font::m_horizontalScaling
protected

Definition at line 135 of file font.h.

Referenced by calcMetrics(), and horizontalScaling().

bool Font::m_needMetrics
protected

Definition at line 130 of file font.h.

Referenced by calcMetrics(), calcMetricsIfNeeded(), setSize(), and setStrokeWeight().

float Font::m_size
protected

Definition at line 126 of file font.h.

Referenced by operator==(), setSize(), and size().

double Font::m_spaceCharFixup
protected

Definition at line 133 of file font.h.

Referenced by calcMetrics(), charWidth(), and drawChar().

float Font::m_strokeWeight
protected

Definition at line 127 of file font.h.

Referenced by operator==(), setStrokeWeight(), and strokeWeight().

double Font::m_strokeWidth
protected

Definition at line 131 of file font.h.

Referenced by calcMetrics(), and strokeWidth().

double Font::m_verticalScaling
protected

Definition at line 134 of file font.h.

Referenced by calcMetrics(), and verticalScaling().


The documentation for this class was generated from the following files: