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::deque< std::deque<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 
96  VECTOR2D ComputeStringBoundaryLimits( const UTF8& aText, VECTOR2D aGlyphSize,
97  double aGlyphThickness,
98  double* aTopLimit = NULL, double* aBottomLimit = NULL ) const;
99 
107  VECTOR2D ComputeTextLineSize( const UTF8& aText ) const;
108 
116  double ComputeOverbarVerticalPosition( double aGlyphHeight, double aGlyphThickness ) const;
117 
125  static double GetInterline( double aGlyphHeight, double aGlyphThickness );
126 
127 
128 
129 private:
131  GLYPH_LIST m_glyphs;
132  std::vector<BOX2D> m_glyphBoundingBoxes;
133 
141  VECTOR2D computeTextLineSize( const UTF8& aText ) const;
142 
148  double computeOverbarVerticalPosition() const;
149 
155  int getInterline() const;
156 
164  BOX2D computeBoundingBox( const GLYPH& aGlyph, const VECTOR2D& aGlyphBoundingX ) const;
165 
172  void drawSingleLineText( const UTF8& aText );
173 
180  inline unsigned linesCount( const UTF8& aText ) const
181  {
182  if( aText.empty() )
183  return 0; // std::count does not work well with empty strings
184  else
185  // aText.end() - 1 is to skip a newline character that is potentially at the end
186  return std::count( aText.begin(), aText.end() - 1, '\n' ) + 1;
187  }
188 
190  static const double OVERBAR_POSITION_FACTOR;
191 
193  static const double BOLD_FACTOR;
194 
196  static const double STROKE_FONT_SCALE;
197 
200  static const double ITALIC_TILT;
201 
203  static const double INTERLINE_PITCH_RATIO;
204 };
205 } // namespace KIGFX
206 
207 #endif // STROKE_FONT_H_
Class UTF8 is an 8 bit string that is assuredly encoded in UTF8, and supplies special conversion supp...
Definition: utf8.h:73
static double GetInterline(double aGlyphHeight, double aGlyphThickness)
Compute the distance (interline) between 2 lines of text (for multiline texts).
double computeOverbarVerticalPosition() const
Compute the vertical position of an overbar, sometimes used in texts.
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: class_module.h:57
static const double BOLD_FACTOR
Factor that determines relative line width for bold text.
Definition: stroke_font.h:193
GLYPH_LIST m_glyphs
Glyph list.
Definition: stroke_font.h:131
std::vector< BOX2D > m_glyphBoundingBoxes
Bounding boxes of the glyphs.
Definition: stroke_font.h:132
std::vector< GLYPH > GLYPH_LIST
Definition: stroke_font.h:46
bool LoadNewStrokeFont(const char *const aNewStrokeFont[], int aNewStrokeFontSize)
Load the new stroke font.
Definition: stroke_font.cpp:47
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:200
Definition of base KiCad text object.
Class BOX2 handles a 2-D bounding box, built on top of an origin point and size vector, both of templated class Vec.
Definition: box2.h:41
VECTOR2D ComputeStringBoundaryLimits(const UTF8 &aText, VECTOR2D aGlyphSize, double aGlyphThickness, double *aTopLimit=NULL, double *aBottomLimit=NULL) const
Compute the boundary limits of aText (the bbox of all shapes).
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:130
bool empty() const
Definition: utf8.h:108
BOX2D computeBoundingBox(const GLYPH &aGlyph, const VECTOR2D &aGlyphBoundingX) const
Compute the bounding box of a given glyph.
VECTOR2D computeTextLineSize(const UTF8 &aText) const
Compute the X and Y size of a given text.
std::string::const_iterator end() const
Definition: utf8.h:190
static const double INTERLINE_PITCH_RATIO
Factor that determines the pitch between 2 lines.
Definition: stroke_font.h:203
unsigned linesCount(const UTF8 &aText) const
Returns number of lines for a given text.
Definition: stroke_font.h:180
void Draw(const UTF8 &aText, const VECTOR2D &aPosition, double aRotationAngle)
Draw a string.
std::string::const_iterator begin() const
Definition: utf8.h:189
STROKE_FONT(GAL *aGal)
Constructor.
Definition: stroke_font.cpp:41
static const double STROKE_FONT_SCALE
Scale factor for a glyph
Definition: stroke_font.h:196
void drawSingleLineText(const UTF8 &aText)
Draws a single line of text.
double ComputeOverbarVerticalPosition(double aGlyphHeight, double aGlyphThickness) const
Compute the vertical position of an overbar, sometimes used in texts.
static const double OVERBAR_POSITION_FACTOR
Factor that determines relative vertical position of the overbar.
Definition: stroke_font.h:190
VECTOR2D ComputeTextLineSize(const UTF8 &aText) const
Compute the X and Y size of a given text.
std::deque< std::deque< VECTOR2D > > GLYPH
Definition: stroke_font.h:43
Class STROKE_FONT implements stroke font drawing.
Definition: stroke_font.h:53
int getInterline() const
Returns a single line height using current settings.
Class GAL is the abstract interface for drawing on a 2D-surface.