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  * Copyright (C) 2016-2021 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * @author Maciej Suminski <maciej.suminski@cern.ch>
9  *
10  * Stroke font class
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, you may find one here:
24  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
25  * or you may search the http://www.gnu.org website for the version 2 license,
26  * or you may write to the Free Software Foundation, Inc.,
27  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
28  */
29 
30 #ifndef STROKE_FONT_H_
31 #define STROKE_FONT_H_
32 
33 #include <deque>
34 #include <algorithm>
35 
36 #include <utf8.h>
37 
38 #include <eda_text.h>
39 
40 #include <math/box2.h>
41 
42 namespace KIGFX
43 {
44 class GAL;
45 
46 typedef std::vector<std::vector<VECTOR2D>*> GLYPH;
47 typedef std::vector<GLYPH*> GLYPH_LIST;
48 
55 {
56  friend class GAL;
57 
58 public:
60  STROKE_FONT( GAL* aGal );
61 
69  bool LoadNewStrokeFont( const char* const aNewStrokeFont[], int aNewStrokeFontSize );
70 
78  void Draw( const UTF8& aText, const VECTOR2D& aPosition, double aRotationAngle );
79 
85  void SetGAL( GAL* aGal )
86  {
87  m_gal = aGal;
88  }
89 
97  VECTOR2D ComputeStringBoundaryLimits( const UTF8& aText, const VECTOR2D& aGlyphSize,
98  double aGlyphThickness ) const;
99 
108  double ComputeOverbarVerticalPosition( double aGlyphHeight ) const;
109 
116  static double GetInterline( double aGlyphHeight );
117 
118 private:
126  VECTOR2D computeTextLineSize( const UTF8& aText ) const;
127 
133  double computeOverbarVerticalPosition() const;
134  double computeUnderlineVerticalPosition() const;
135 
143  BOX2D computeBoundingBox( const GLYPH* aGlyph, double aGlyphWidth ) const;
144 
151  void drawSingleLineText( const UTF8& aText );
152 
159  inline unsigned linesCount( const UTF8& aText ) const
160  {
161  if( aText.empty() )
162  return 0; // std::count does not work well with empty strings
163  else
164  // aText.end() - 1 is to skip a newline character that is potentially at the end
165  return std::count( aText.begin(), aText.end() - 1, '\n' ) + 1;
166  }
167 
170  const std::vector<BOX2D>* m_glyphBoundingBoxes;
171 
173  static const double OVERBAR_POSITION_FACTOR;
174  static const double UNDERLINE_POSITION_FACTOR;
175 
177  static const double BOLD_FACTOR;
178 
180  static const double STROKE_FONT_SCALE;
181 
184  static const double ITALIC_TILT;
185 
187  static const double INTERLINE_PITCH_RATIO;
188 };
189 } // namespace KIGFX
190 
191 #endif // STROKE_FONT_H_
std::vector< std::vector< VECTOR2D > * > GLYPH
Definition: stroke_font.h:44
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition: utf8.h:70
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:236
static double GetInterline(double aGlyphHeight)
Compute the distance (interline) between 2 lines of text (for multiline texts).
static const double BOLD_FACTOR
Scale factor for a glyph.
Definition: stroke_font.h:177
bool LoadNewStrokeFont(const char *const aNewStrokeFont[], int aNewStrokeFontSize)
Load the new stroke font.
Definition: stroke_font.cpp:58
static const double ITALIC_TILT
Factor that determines the pitch between 2 lines.
Definition: stroke_font.h:184
void SetGAL(GAL *aGal)
Changes Graphics Abstraction Layer used for drawing items for a new one.
Definition: stroke_font.h:85
GAL * m_gal
Pointer to the GAL.
Definition: stroke_font.h:168
VECTOR2D computeTextLineSize(const UTF8 &aText) const
Compute the X and Y size of a given text.
static const double UNDERLINE_POSITION_FACTOR
Factor that determines relative line width for bold text.
Definition: stroke_font.h:174
double computeUnderlineVerticalPosition() const
static const double INTERLINE_PITCH_RATIO
Definition: stroke_font.h:187
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:47
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.
double ComputeOverbarVerticalPosition(double aGlyphHeight) const
Compute the vertical position of an overbar, sometimes used in texts.
STROKE_FONT(GAL *aGal)
Constructor.
Definition: stroke_font.cpp:50
static const double STROKE_FONT_SCALE
Tilt factor for italic style (the is is the scaling factor on dY relative coordinates to give a tilt ...
Definition: stroke_font.h:180
void drawSingleLineText(const UTF8 &aText)
Draw a single line of text.
unsigned linesCount(const UTF8 &aText) const
Returns number of lines for a given text.
Definition: stroke_font.h:159
std::string::const_iterator end() const
Definition: utf8.h:193
static const double OVERBAR_POSITION_FACTOR
Definition: stroke_font.h:173
const std::vector< BOX2D > * m_glyphBoundingBoxes
Bounding boxes of the glyphs.
Definition: stroke_font.h:170
std::string::const_iterator begin() const
Definition: utf8.h:192
const GLYPH_LIST * m_glyphs
Glyph list.
Definition: stroke_font.h:169
BOX2D computeBoundingBox(const GLYPH *aGlyph, double aGlyphWidth) const
Compute the bounding box of a given glyph.
Implement a stroke font drawing.
Definition: stroke_font.h:54
Abstract interface for drawing on a 2D-surface.
bool empty() const
Definition: utf8.h:103