KiCad PCB EDA Suite
stroke_font.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KICAD, a free EDA CAD application.
3  *
4  * Copyright (C) 2012 Torsten Hueter, torstenhtr <at> gmx.de
5  * Copyright (C) 2013 CERN
6  * @author Maciej Suminski <maciej.suminski@cern.ch>
7  * Copyright (C) 2016 Kicad Developers, see change_log.txt for contributors.
8  *
9  * Stroke font class
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, you may find one here:
23  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
24  * or you may search the http://www.gnu.org website for the version 2 license,
25  * or you may write to the Free Software Foundation, Inc.,
26  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27  */
28 
29 #ifndef STROKE_FONT_H_
30 #define STROKE_FONT_H_
31 
32 #include <deque>
33 #include <algorithm>
34 
35 #include <utf8.h>
36 
37 #include <eda_text.h>
38 
39 #include <math/box2.h>
40 
41 namespace KIGFX
42 {
43 class GAL;
44 
45 typedef std::vector<std::vector<VECTOR2D>*> GLYPH;
46 typedef std::vector<GLYPH*> GLYPH_LIST;
47 
54 {
55  friend class GAL;
56 
57 public:
59  STROKE_FONT( GAL* aGal );
60 
68  bool LoadNewStrokeFont( const char* const aNewStrokeFont[], int aNewStrokeFontSize );
69 
77  void Draw( const UTF8& aText, const VECTOR2D& aPosition, double aRotationAngle );
78 
84  void SetGAL( GAL* aGal )
85  {
86  m_gal = aGal;
87  }
88 
94  VECTOR2D ComputeStringBoundaryLimits( const UTF8& aText, const VECTOR2D& aGlyphSize,
95  double aGlyphThickness ) const;
96 
104  double ComputeOverbarVerticalPosition( double aGlyphHeight, double aGlyphThickness ) const;
105 
112  static double GetInterline( double aGlyphHeight );
113 
114 
115 
116 private:
119  const std::vector<BOX2D>* m_glyphBoundingBoxes;
120 
128  VECTOR2D computeTextLineSize( const UTF8& aText ) const;
129 
135  double computeOverbarVerticalPosition() const;
136 
144  BOX2D computeBoundingBox( const GLYPH* aGlyph, double aGlyphWidth ) const;
145 
152  void drawSingleLineText( const UTF8& aText );
153 
160  inline unsigned linesCount( const UTF8& aText ) const
161  {
162  if( aText.empty() )
163  return 0; // std::count does not work well with empty strings
164  else
165  // aText.end() - 1 is to skip a newline character that is potentially at the end
166  return std::count( aText.begin(), aText.end() - 1, '\n' ) + 1;
167  }
168 
170  static const double OVERBAR_POSITION_FACTOR;
171 
173  static const double BOLD_FACTOR;
174 
176  static const double STROKE_FONT_SCALE;
177 
180  static const double ITALIC_TILT;
181 
183  static const double INTERLINE_PITCH_RATIO;
184 };
185 } // namespace KIGFX
186 
187 #endif // STROKE_FONT_H_
std::vector< std::vector< VECTOR2D > * > GLYPH
Definition: stroke_font.h:43
UTF8 is an 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to...
Definition: utf8.h:73
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:131
static double GetInterline(double aGlyphHeight)
Compute the distance (interline) between 2 lines of text (for multiline texts).
static const double BOLD_FACTOR
Factor that determines relative line width for bold text.
Definition: stroke_font.h:173
bool LoadNewStrokeFont(const char *const aNewStrokeFont[], int aNewStrokeFontSize)
Load the new stroke font.
Definition: stroke_font.cpp:55
static const double ITALIC_TILT
Tilt factor for italic style (the is is the scaling factor on dY relative coordinates to give a tilst...
Definition: stroke_font.h:180
double ComputeOverbarVerticalPosition(double aGlyphHeight, double aGlyphThickness) const
Compute the vertical position of an overbar, sometimes used in texts.
void SetGAL(GAL *aGal)
Function SetGAL Changes Graphics Abstraction Layer used for drawing items for a new one.
Definition: stroke_font.h:84
GAL * m_gal
Pointer to the GAL.
Definition: stroke_font.h:117
VECTOR2D computeTextLineSize(const UTF8 &aText) const
Compute the X and Y size of a given text.
static const double INTERLINE_PITCH_RATIO
Factor that determines the pitch between 2 lines.
Definition: stroke_font.h:183
VECTOR2D ComputeStringBoundaryLimits(const UTF8 &aText, const VECTOR2D &aGlyphSize, double aGlyphThickness) const
Compute the boundary limits of aText (the bounding box of all shapes).
std::vector< GLYPH * > GLYPH_LIST
Definition: stroke_font.h:46
void Draw(const UTF8 &aText, const VECTOR2D &aPosition, double aRotationAngle)
Draw a string.
double computeOverbarVerticalPosition() const
Compute the vertical position of an overbar, sometimes used in texts.
STROKE_FONT(GAL *aGal)
Constructor.
Definition: stroke_font.cpp:49
static const double STROKE_FONT_SCALE
Scale factor for a glyph
Definition: stroke_font.h:176
void drawSingleLineText(const UTF8 &aText)
Draws a single line of text.
unsigned linesCount(const UTF8 &aText) const
Returns number of lines for a given text.
Definition: stroke_font.h:160
std::string::const_iterator end() const
Definition: utf8.h:197
static const double OVERBAR_POSITION_FACTOR
Factor that determines relative vertical position of the overbar.
Definition: stroke_font.h:170
const std::vector< BOX2D > * m_glyphBoundingBoxes
Bounding boxes of the glyphs.
Definition: stroke_font.h:119
std::string::const_iterator begin() const
Definition: utf8.h:196
const GLYPH_LIST * m_glyphs
Glyph list.
Definition: stroke_font.h:118
BOX2D computeBoundingBox(const GLYPH *aGlyph, double aGlyphWidth) const
Compute the bounding box of a given glyph.
Class STROKE_FONT implements stroke font drawing.
Definition: stroke_font.h:53
Class GAL is the abstract interface for drawing on a 2D-surface.
bool empty() const
Definition: utf8.h:108