KiCad PCB EDA Suite
stroke_font.cpp
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 #include <gal/stroke_font.h>
31 #include <text_utils.h>
32 #include <wx/string.h>
33 
34 
35 using namespace KIGFX;
36 
37 const double STROKE_FONT::INTERLINE_PITCH_RATIO = 1.61;
38 const double STROKE_FONT::OVERBAR_POSITION_FACTOR = 1.22;
39 const double STROKE_FONT::BOLD_FACTOR = 1.3;
40 const double STROKE_FONT::STROKE_FONT_SCALE = 1.0 / 21.0;
41 const double STROKE_FONT::ITALIC_TILT = 1.0 / 8;
42 
44  m_gal( aGal )
45 {
46 }
47 
48 
49 bool STROKE_FONT::LoadNewStrokeFont( const char* const aNewStrokeFont[], int aNewStrokeFontSize )
50 {
51  m_glyphs.clear();
52  m_glyphBoundingBoxes.clear();
53  m_glyphs.resize( aNewStrokeFontSize );
54  m_glyphBoundingBoxes.resize( aNewStrokeFontSize );
55 
56  for( int j = 0; j < aNewStrokeFontSize; j++ )
57  {
58  GLYPH& glyph = m_glyphs[j];
59  double glyphStartX = 0.0;
60  double glyphEndX = 0.0;
61  VECTOR2D glyphBoundingX;
62 
63  std::deque<VECTOR2D>* pointList = nullptr;
64 
65  int i = 0;
66 
67  while( aNewStrokeFont[j][i] )
68  {
69  VECTOR2D point( 0.0, 0.0 );
70  char coordinate[2] = { 0, };
71 
72  for( int k = 0; k < 2; k++ )
73  {
74  coordinate[k] = aNewStrokeFont[j][i + k];
75  }
76 
77  if( i < 2 )
78  {
79  // The first two values contain the width of the char
80  glyphStartX = ( coordinate[0] - 'R' ) * STROKE_FONT_SCALE;
81  glyphEndX = ( coordinate[1] - 'R' ) * STROKE_FONT_SCALE;
82  glyphBoundingX = VECTOR2D( 0, glyphEndX - glyphStartX );
83  }
84  else if( ( coordinate[0] == ' ' ) && ( coordinate[1] == 'R' ) )
85  {
86  // Raise pen
87  pointList = nullptr;
88  }
89  else
90  {
91  // In stroke font, coordinates values are coded as <value> + 'R',
92  // <value> is an ASCII char.
93  // therefore every coordinate description of the Hershey format has an offset,
94  // it has to be subtracted
95  // Note:
96  // * the stroke coordinates are stored in reduced form (-1.0 to +1.0),
97  // and the actual size is stroke coordinate * glyph size
98  // * a few shapes have a height slightly bigger than 1.0 ( like '{' '[' )
99  point.x = (double) ( coordinate[0] - 'R' ) * STROKE_FONT_SCALE - glyphStartX;
100  #define FONT_OFFSET -10
101  // FONT_OFFSET is here for historical reasons, due to the way the stroke font
102  // was built. It allows shapes coordinates like W M ... to be >= 0
103  // Only shapes like j y have coordinates < 0
104  point.y = (double) ( coordinate[1] - 'R' + FONT_OFFSET ) * STROKE_FONT_SCALE;
105 
106  if( !pointList )
107  {
108  glyph.emplace_back( std::deque<VECTOR2D>() );
109  pointList = &glyph.back();
110  }
111 
112  pointList->push_back( point );
113  }
114 
115  i += 2;
116  }
117 
118  // Compute the bounding box of the glyph
119  m_glyphBoundingBoxes[j] = computeBoundingBox( glyph, glyphBoundingX );
120  }
121 
122  return true;
123 }
124 
125 
126 // Static function:
127 double STROKE_FONT::GetInterline( double aGlyphHeight )
128 {
129  // Do not add the glyph thickness to the interline. This makes bold text line-spacing
130  // different from normal text, which is poor typography.
131  return ( aGlyphHeight * INTERLINE_PITCH_RATIO );
132 }
133 
134 
135 BOX2D STROKE_FONT::computeBoundingBox( const GLYPH& aGLYPH, const VECTOR2D& aGLYPHBoundingX ) const
136 {
138 
139  std::deque<VECTOR2D> boundingPoints;
140 
141  boundingPoints.emplace_back( VECTOR2D( aGLYPHBoundingX.x, 0 ) );
142  boundingPoints.emplace_back( VECTOR2D( aGLYPHBoundingX.y, 0 ) );
143 
144  for( GLYPH::const_iterator pointListIt = aGLYPH.begin(); pointListIt != aGLYPH.end(); ++pointListIt )
145  {
146  for( std::deque<VECTOR2D>::const_iterator pointIt = pointListIt->begin();
147  pointIt != pointListIt->end(); ++pointIt )
148  {
149  boundingPoints.emplace_back( VECTOR2D( aGLYPHBoundingX.x, pointIt->y ) );
150  }
151  }
152 
153  boundingBox.Compute( boundingPoints );
154 
155  return boundingBox;
156 }
157 
158 
159 void STROKE_FONT::Draw( const UTF8& aText, const VECTOR2D& aPosition, double aRotationAngle )
160 {
161  if( aText.empty() )
162  return;
163 
164  // Context needs to be saved before any transformations
165  m_gal->Save();
166 
167  m_gal->Translate( aPosition );
168  m_gal->Rotate( -aRotationAngle );
169 
170  // Single line height
171  int lineHeight = KiROUND( GetInterline( m_gal->GetGlyphSize().y ) );
172  int lineCount = linesCount( aText );
173  const VECTOR2D& glyphSize = m_gal->GetGlyphSize();
174 
175  // align the 1st line of text
176  switch( m_gal->GetVerticalJustify() )
177  {
179  m_gal->Translate( VECTOR2D( 0, glyphSize.y ) );
180  break;
181 
183  m_gal->Translate( VECTOR2D( 0, glyphSize.y / 2.0 ) );
184  break;
185 
187  break;
188 
189  default:
190  break;
191  }
192 
193  if( lineCount > 1 )
194  {
195  switch( m_gal->GetVerticalJustify() )
196  {
198  break;
199 
201  m_gal->Translate( VECTOR2D(0, -( lineCount - 1 ) * lineHeight / 2) );
202  break;
203 
205  m_gal->Translate( VECTOR2D(0, -( lineCount - 1 ) * lineHeight ) );
206  break;
207  }
208  }
209 
210  m_gal->SetIsStroke( true );
211  //m_gal->SetIsFill( false );
212 
213  if( m_gal->IsFontBold() )
215 
216  // Split multiline strings into separate ones and draw them line by line
217  size_t begin = 0;
218  size_t newlinePos = aText.find( '\n' );
219 
220  while( newlinePos != aText.npos )
221  {
222  size_t length = newlinePos - begin;
223 
224  drawSingleLineText( aText.substr( begin, length ) );
225  m_gal->Translate( VECTOR2D( 0.0, lineHeight ) );
226 
227  begin = newlinePos + 1;
228  newlinePos = aText.find( '\n', begin );
229  }
230 
231  // Draw the last (or the only one) line
232  if( !aText.empty() )
233  drawSingleLineText( aText.substr( begin ) );
234 
235  m_gal->Restore();
236 }
237 
238 
240 {
241  double xOffset;
242  VECTOR2D glyphSize( m_gal->GetGlyphSize() );
243  double overbar_italic_comp = computeOverbarVerticalPosition() * ITALIC_TILT;
244 
245  if( m_gal->IsTextMirrored() )
246  overbar_italic_comp = -overbar_italic_comp;
247 
248  // Compute the text size
249  VECTOR2D textSize = computeTextLineSize( aText );
250  double half_thickness = m_gal->GetLineWidth()/2;
251 
252  // Context needs to be saved before any transformations
253  m_gal->Save();
254 
255  // First adjust: the text X position is corrected by half_thickness
256  // because when the text with thickness is draw, its full size is textSize,
257  // but the position of lines is half_thickness to textSize - half_thickness
258  // so we must translate the coordinates by half_thickness on the X axis
259  // to place the text inside the 0 to textSize X area.
260  m_gal->Translate( VECTOR2D( half_thickness, 0 ) );
261 
262  // Adjust the text position to the given horizontal justification
263  switch( m_gal->GetHorizontalJustify() )
264  {
266  m_gal->Translate( VECTOR2D( -textSize.x / 2.0, 0 ) );
267  break;
268 
270  if( !m_gal->IsTextMirrored() )
271  m_gal->Translate( VECTOR2D( -textSize.x, 0 ) );
272  break;
273 
275  if( m_gal->IsTextMirrored() )
276  m_gal->Translate( VECTOR2D( -textSize.x, 0 ) );
277  break;
278 
279  default:
280  break;
281  }
282 
283  if( m_gal->IsTextMirrored() )
284  {
285  // In case of mirrored text invert the X scale of points and their X direction
286  // (m_glyphSize.x) and start drawing from the position where text normally should end
287  // (textSize.x)
288  xOffset = textSize.x - m_gal->GetLineWidth();
289  glyphSize.x = -glyphSize.x;
290  }
291  else
292  {
293  xOffset = 0.0;
294  }
295 
296  // The overbar is indented inward at the beginning of an italicized section, but
297  // must not be indented on subsequent letters to ensure that the bar segments
298  // overlap.
299  bool last_had_overbar = false;
300  auto processedText = ProcessOverbars( aText );
301  const auto& text = processedText.first;
302  const auto& overbars = processedText.second;
303  int overbar_index = 0;
304 
305  for( UTF8::uni_iter chIt = text.ubegin(), end = text.uend(); chIt < end; ++chIt )
306  {
307  int dd = *chIt - ' ';
308 
309  // Handle tabs as locked to the nearest 4th column (counting in spaces)
310  // The choice of spaces is somewhat arbitrary but sufficient for aligning text
311  if( *chIt == '\t' )
312  {
313  double fourSpaces = 4.0 * glyphSize.x * m_glyphBoundingBoxes[0].GetEnd().x;
314  double addlSpace = fourSpaces - std::fmod( xOffset, fourSpaces );
315 
316  // Add the remaining space (between 0 and 3 spaces)
317  xOffset += addlSpace;
318 
319  // Set the character to ' ' instead of the '?' for tab
320  dd = 0;
321  }
322 
323  if( dd >= (int) m_glyphBoundingBoxes.size() || dd < 0 )
324  dd = '?' - ' ';
325 
326  GLYPH& glyph = m_glyphs[dd];
327  BOX2D& bbox = m_glyphBoundingBoxes[dd];
328 
329  if( overbars[overbar_index] )
330  {
331  double overbar_start_x = xOffset;
332  double overbar_start_y = - computeOverbarVerticalPosition();
333  double overbar_end_x = xOffset + glyphSize.x * bbox.GetEnd().x;
334  double overbar_end_y = overbar_start_y;
335 
336  if( !last_had_overbar )
337  {
338  if( m_gal->IsFontItalic() )
339  overbar_start_x += overbar_italic_comp;
340 
341  last_had_overbar = true;
342  }
343 
344  VECTOR2D startOverbar( overbar_start_x, overbar_start_y );
345  VECTOR2D endOverbar( overbar_end_x, overbar_end_y );
346 
347  m_gal->DrawLine( startOverbar, endOverbar );
348  }
349  else
350  {
351  last_had_overbar = false;
352  }
353 
354  for( GLYPH::iterator pointListIt = glyph.begin(); pointListIt != glyph.end();
355  ++pointListIt )
356  {
357  std::deque<VECTOR2D> pointListScaled;
358 
359  for( std::deque<VECTOR2D>::iterator pointIt = pointListIt->begin();
360  pointIt != pointListIt->end(); ++pointIt )
361  {
362  VECTOR2D pointPos( pointIt->x * glyphSize.x + xOffset, pointIt->y * glyphSize.y );
363 
364  if( m_gal->IsFontItalic() )
365  {
366  // FIXME should be done other way - referring to the lowest Y value of point
367  // because now italic fonts are translated a bit
368  if( m_gal->IsTextMirrored() )
369  pointPos.x += pointPos.y * STROKE_FONT::ITALIC_TILT;
370  else
371  pointPos.x -= pointPos.y * STROKE_FONT::ITALIC_TILT;
372  }
373 
374  pointListScaled.push_back( pointPos );
375  }
376 
377  m_gal->DrawPolyline( pointListScaled );
378  }
379 
380  xOffset += glyphSize.x * bbox.GetEnd().x;
381  ++overbar_index;
382  }
383 
384  m_gal->Restore();
385 }
386 
387 
388 double STROKE_FONT::ComputeOverbarVerticalPosition( double aGlyphHeight, double aGlyphThickness ) const
389 {
390  // Static method.
391  // Compute the Y position of the overbar. This is the distance between
392  // the text base line and the overbar axis.
393  return aGlyphHeight * OVERBAR_POSITION_FACTOR + aGlyphThickness;
394 }
395 
396 
398 {
399  // Compute the Y position of the overbar. This is the distance between
400  // the text base line and the overbar axis.
402 }
403 
404 
406 {
408 }
409 
410 
412  double aGlyphThickness ) const
413 {
414  VECTOR2D string_bbox;
415  int line_count = 1;
416  double maxX = 0.0, curX = 0.0;
417 
418  for( UTF8::uni_iter it = aText.ubegin(), end = aText.uend(); it < end; ++it )
419  {
420  if( *it == '\n' )
421  {
422  curX = 0.0;
423  maxX = std::max( maxX, curX );
424  ++line_count;
425  continue;
426  }
427 
428  // If it is double tilda, then it is displayed as a single tilda
429  // If it is single tilda, then it is toggling overbar, so we need to skip it
430  if( *it == '~' )
431  {
432  if( ++it >= end )
433  break;
434  }
435 
436  // Index in the bounding boxes table
437  int dd = *it - ' ';
438 
439  if( dd >= (int) m_glyphBoundingBoxes.size() || dd < 0 )
440  dd = '?' - ' ';
441 
442  const BOX2D& box = m_glyphBoundingBoxes[dd];
443  curX += box.GetEnd().x;
444  }
445 
446  string_bbox.x = std::max( maxX, curX );
447  string_bbox.x *= aGlyphSize.x;
448  string_bbox.x += aGlyphThickness;
449  string_bbox.y = line_count * GetInterline( aGlyphSize.y );
450 
451  // For italic correction, take in account italic tilt
452  if( m_gal->IsFontItalic() )
453  string_bbox.x += string_bbox.y * STROKE_FONT::ITALIC_TILT;
454 
455  return string_bbox;
456 }
Class UTF8 is an 8 bit string that is assuredly encoded in UTF8, and supplies special conversion supp...
Definition: utf8.h:73
virtual void DrawPolyline(const std::deque< VECTOR2D > &aPointList)
Draw a polyline.
static constexpr std::string::size_type npos
Definition: utf8.h:155
float GetLineWidth() const
Get the line width.
std::pair< UTF8, std::vector< bool > > ProcessOverbars(const UTF8 &aText)
Processes a text to extract the raw text and overbar flags.
Definition: text_utils.cpp:27
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: class_module.h:57
static int KiROUND(double v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: common.h:115
const Vec GetEnd() const
Definition: box2.h:193
static double GetInterline(double aGlyphHeight)
Compute the distance (interline) between 2 lines of text (for multiline texts).
bool IsTextMirrored() const
Returns true if text should displayed mirrored.
static const double BOLD_FACTOR
Factor that determines relative line width for bold text.
Definition: stroke_font.h:174
GLYPH_LIST m_glyphs
Glyph list.
Definition: stroke_font.h:119
std::vector< BOX2D > m_glyphBoundingBoxes
Bounding boxes of the glyphs.
Definition: stroke_font.h:120
uni_iter uend() const
Function uend returns a uni_iter initialized to the end of "this" UTF8 byte sequence.
Definition: utf8.h:294
void Compute(const Container &aPointList)
Compute the bounding box from a given list of points.
Definition: box2.h:89
bool LoadNewStrokeFont(const char *const aNewStrokeFont[], int aNewStrokeFontSize)
Load the new stroke font.
Definition: stroke_font.cpp:49
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:181
virtual void DrawLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Draw a line.
EDA_TEXT_HJUSTIFY_T GetHorizontalJustify() const
Returns current text horizontal justification setting.
BOX2D computeBoundingBox(const GLYPH &aGlyph, const VECTOR2D &aGlyphBoundingX) const
Compute the bounding box of a given glyph.
virtual void SetLineWidth(float aLineWidth)
Set the line width.
virtual void Rotate(double aAngle)
Rotate the context.
const VECTOR2D & GetGlyphSize() const
bool IsFontBold() const
Returns true if current font has 'bold' attribute enabled.
BOX2I boundingBox(T aObject)
boundingBox template method
Definition: shape_index.h:59
uni_iter ubegin() const
Function ubegin returns a uni_iter initialized to the start of "this" UTF8 byte sequence.
Definition: utf8.h:285
std::string::size_type find(char c) const
Definition: utf8.h:110
double ComputeOverbarVerticalPosition(double aGlyphHeight, double aGlyphThickness) const
Compute the vertical position of an overbar, sometimes used in texts.
GAL * m_gal
Pointer to the GAL.
Definition: stroke_font.h:118
VECTOR2< double > VECTOR2D
Definition: vector2d.h:586
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:184
VECTOR2D ComputeStringBoundaryLimits(const UTF8 &aText, const VECTOR2D &aGlyphSize, double aGlyphThickness) const
Compute the boundary limits of aText (the bounding box of all shapes).
bool IsFontItalic() const
Returns true if current font has 'italic' attribute enabled.
class uni_iter is a non-mutating iterator that walks through unicode code points in the UTF8 encoded ...
Definition: utf8.h:207
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:43
static const double STROKE_FONT_SCALE
Scale factor for a glyph
Definition: stroke_font.h:177
void drawSingleLineText(const UTF8 &aText)
Draws a single line of text.
virtual void Restore()
Restore the context.
#define max(a, b)
Definition: auxiliary.h:86
EDA_TEXT_VJUSTIFY_T GetVerticalJustify() const
Returns current text vertical justification setting.
unsigned linesCount(const UTF8 &aText) const
Returns number of lines for a given text.
Definition: stroke_font.h:161
#define FONT_OFFSET
size_t i
Definition: json11.cpp:597
static const double OVERBAR_POSITION_FACTOR
Factor that determines relative vertical position of the overbar.
Definition: stroke_font.h:171
std::string substr(size_t pos=0, size_t len=npos) const
Definition: utf8.h:182
virtual void Save()
Save the context.
std::deque< std::deque< VECTOR2D > > GLYPH
Definition: stroke_font.h:43
virtual void Translate(const VECTOR2D &aTranslation)
Translate the context.
virtual void SetIsStroke(bool aIsStrokeEnabled)
Enable/disable stroked outlines.
Class GAL is the abstract interface for drawing on a 2D-surface.
bool empty() const
Definition: utf8.h:108