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 <utf8.h>
34 
35 #include <eda_text.h>
36 
37 #include <math/box2.h>
38 
39 namespace KIGFX
40 {
41 class GAL;
42 
43 typedef std::deque< std::deque<VECTOR2D> > GLYPH;
44 typedef std::vector<GLYPH> GLYPH_LIST;
45 
52 {
53  friend class GAL;
54 
55 public:
57  STROKE_FONT( GAL* aGal );
58 
66  bool LoadNewStrokeFont( const char* const aNewStrokeFont[], int aNewStrokeFontSize );
67 
75  void Draw( const UTF8& aText, const VECTOR2D& aPosition, double aRotationAngle );
76 
82  void SetGAL( GAL* aGal )
83  {
84  m_gal = aGal;
85  }
86 
94  VECTOR2D ComputeStringBoundaryLimits( const UTF8& aText, VECTOR2D aGlyphSize,
95  double aGlyphThickness,
96  double* aTopLimit = NULL, double* aBottomLimit = NULL ) const;
97 
105  VECTOR2D ComputeTextLineSize( const UTF8& aText ) const;
106 
114  double ComputeOverbarVerticalPosition( double aGlyphHeight, double aGlyphThickness ) const;
115 
123  static double GetInterline( double aGlyphHeight, double aGlyphThickness );
124 
125 
126 
127 private:
129  GLYPH_LIST m_glyphs;
130  std::vector<BOX2D> m_glyphBoundingBoxes;
131 
139  VECTOR2D computeTextLineSize( const UTF8& aText ) const;
140 
146  double computeOverbarVerticalPosition() const;
147 
153  int getInterline() const;
154 
162  BOX2D computeBoundingBox( const GLYPH& aGlyph, const VECTOR2D& aGlyphBoundingX ) const;
163 
170  void drawSingleLineText( const UTF8& aText );
171 
178  inline unsigned linesCount( const UTF8& aText ) const
179  {
180  if( aText.empty() )
181  return 0; // std::count does not work well with empty strings
182  else
183  // aText.end() - 1 is to skip a newline character that is potentially at the end
184  return std::count( aText.begin(), aText.end() - 1, '\n' ) + 1;
185  }
186 
188  static const double OVERBAR_POSITION_FACTOR;
189 
191  static const double BOLD_FACTOR;
192 
194  static const double STROKE_FONT_SCALE;
195 
198  static const double ITALIC_TILT;
199 
201  static const double INTERLINE_PITCH_RATIO;
202 };
203 } // namespace KIGFX
204 
205 #endif // STROKE_FONT_H_
Class UTF8 is an 8 bit std::string that is assuredly encoded in UTF8, and supplies special conversion...
Definition: utf8.h:53
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:58
static const double BOLD_FACTOR
Factor that determines relative line width for bold text.
Definition: stroke_font.h:191
GLYPH_LIST m_glyphs
Glyph list.
Definition: stroke_font.h:129
std::vector< BOX2D > m_glyphBoundingBoxes
Bounding boxes of the glyphs.
Definition: stroke_font.h:130
std::vector< GLYPH > GLYPH_LIST
Definition: stroke_font.h:44
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:198
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:82
GAL * m_gal
Pointer to the GAL.
Definition: stroke_font.h:128
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.
static const double INTERLINE_PITCH_RATIO
Factor that determines the pitch between 2 lines.
Definition: stroke_font.h:201
unsigned linesCount(const UTF8 &aText) const
Returns number of lines for a given text.
Definition: stroke_font.h:178
void Draw(const UTF8 &aText, const VECTOR2D &aPosition, double aRotationAngle)
Draw a string.
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:194
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:188
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:41
Class STROKE_FONT implements stroke font drawing.
Definition: stroke_font.h:51
int getInterline() const
Returns a single line height using current settings.
Class GAL is the abstract interface for drawing on a 2D-surface.