KiCad PCB EDA Suite
KIGFX::STROKE_FONT Class Reference

Class STROKE_FONT implements stroke font drawing. More...

#include <stroke_font.h>

Public Member Functions

 STROKE_FONT (GAL *aGal)
 Constructor. More...
 
bool LoadNewStrokeFont (const char *const aNewStrokeFont[], int aNewStrokeFontSize)
 Load the new stroke font. More...
 
void Draw (const UTF8 &aText, const VECTOR2D &aPosition, double aRotationAngle)
 Draw a string. More...
 
void SetGAL (GAL *aGal)
 Function SetGAL Changes Graphics Abstraction Layer used for drawing items for a new one. More...
 
VECTOR2D ComputeStringBoundaryLimits (const UTF8 &aText, const VECTOR2D &aGlyphSize, double aGlyphThickness) const
 Compute the boundary limits of aText (the bounding box of all shapes). More...
 
double ComputeOverbarVerticalPosition (double aGlyphHeight, double aGlyphThickness) const
 Compute the vertical position of an overbar, sometimes used in texts. More...
 

Static Public Member Functions

static double GetInterline (double aGlyphHeight, double aGlyphThickness)
 Compute the distance (interline) between 2 lines of text (for multiline texts). More...
 

Private Member Functions

VECTOR2D computeTextLineSize (const UTF8 &aText) const
 Compute the X and Y size of a given text. More...
 
double computeOverbarVerticalPosition () const
 Compute the vertical position of an overbar, sometimes used in texts. More...
 
int getInterline () const
 Returns a single line height using current settings. More...
 
BOX2D computeBoundingBox (const GLYPH &aGlyph, const VECTOR2D &aGlyphBoundingX) const
 Compute the bounding box of a given glyph. More...
 
void drawSingleLineText (const UTF8 &aText)
 Draws a single line of text. More...
 
unsigned linesCount (const UTF8 &aText) const
 Returns number of lines for a given text. More...
 

Private Attributes

GALm_gal
 Pointer to the GAL. More...
 
GLYPH_LIST m_glyphs
 Glyph list. More...
 
std::vector< BOX2Dm_glyphBoundingBoxes
 Bounding boxes of the glyphs. More...
 

Static Private Attributes

static const double OVERBAR_POSITION_FACTOR = 1.22
 

Factor that determines relative vertical position of the overbar.

More...
 
static const double BOLD_FACTOR = 1.3
 

Factor that determines relative line width for bold text.

More...
 
static const double STROKE_FONT_SCALE = 1.0 / 21.0
 

Scale factor for a glyph

More...
 
static const double ITALIC_TILT = 1.0 / 8
 

Tilt factor for italic style (the is is the scaling factor on dY relative coordinates to give a tilst shape

More...
 
static const double INTERLINE_PITCH_RATIO = 1.5
 

Factor that determines the pitch between 2 lines.

More...
 

Friends

class GAL
 

Detailed Description

Class STROKE_FONT implements stroke font drawing.

A stroke font is composed of lines.

Definition at line 53 of file stroke_font.h.

Constructor & Destructor Documentation

◆ STROKE_FONT()

STROKE_FONT::STROKE_FONT ( GAL aGal)

Constructor.

Definition at line 43 of file stroke_font.cpp.

43  :
44  m_gal( aGal )
45 {
46 }
GAL * m_gal
Pointer to the GAL.
Definition: stroke_font.h:119

Member Function Documentation

◆ computeBoundingBox()

BOX2D STROKE_FONT::computeBoundingBox ( const GLYPH aGlyph,
const VECTOR2D aGlyphBoundingX 
) const
private

Compute the bounding box of a given glyph.

Parameters
aGlyphis the glyph.
aGlyphBoundingXis the x-component of the bounding box size.
Returns
is the complete bounding box size.

Definition at line 139 of file stroke_font.cpp.

140 {
142 
143  std::deque<VECTOR2D> boundingPoints;
144 
145  boundingPoints.emplace_back( VECTOR2D( aGLYPHBoundingX.x, 0 ) );
146  boundingPoints.emplace_back( VECTOR2D( aGLYPHBoundingX.y, 0 ) );
147 
148  for( GLYPH::const_iterator pointListIt = aGLYPH.begin(); pointListIt != aGLYPH.end(); ++pointListIt )
149  {
150  for( std::deque<VECTOR2D>::const_iterator pointIt = pointListIt->begin();
151  pointIt != pointListIt->end(); ++pointIt )
152  {
153  boundingPoints.emplace_back( VECTOR2D( aGLYPHBoundingX.x, pointIt->y ) );
154  }
155  }
156 
157  boundingBox.Compute( boundingPoints );
158 
159  return boundingBox;
160 }
void Compute(const Container &aPointList)
Compute the bounding box from a given list of points.
Definition: box2.h:89
Class BOX2 handles a 2-D bounding box, built on top of an origin point and size vector,...
Definition: box2.h:41
BOX2I boundingBox(T aObject)
boundingBox template method
Definition: shape_index.h:59
VECTOR2< double > VECTOR2D
Definition: vector2d.h:586

References boundingBox(), BOX2< Vec >::Compute(), VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by LoadNewStrokeFont().

◆ ComputeOverbarVerticalPosition()

double STROKE_FONT::ComputeOverbarVerticalPosition ( double  aGlyphHeight,
double  aGlyphThickness 
) const

Compute the vertical position of an overbar, sometimes used in texts.

This is the distance between the text base line and the overbar.

Parameters
aGlyphHeightis the height (vertical size) of the text.
aGlyphThicknessis the thickness of the lines used to draw the text.
Returns
the relative position of the overbar axis.

Definition at line 392 of file stroke_font.cpp.

393 {
394  // Static method.
395  // Compute the Y position of the overbar. This is the distance between
396  // the text base line and the overbar axis.
397  return aGlyphHeight * OVERBAR_POSITION_FACTOR + aGlyphThickness;
398 }
static const double OVERBAR_POSITION_FACTOR
Factor that determines relative vertical position of the overbar.
Definition: stroke_font.h:179

References OVERBAR_POSITION_FACTOR.

Referenced by computeOverbarVerticalPosition(), and EDA_TEXT::GetTextBox().

◆ computeOverbarVerticalPosition()

double STROKE_FONT::computeOverbarVerticalPosition ( ) const
private

Compute the vertical position of an overbar, sometimes used in texts.

This is the distance between the text base line and the overbar.

Returns
the relative position of the overbar axis.

Definition at line 401 of file stroke_font.cpp.

402 {
403  // Compute the Y position of the overbar. This is the distance between
404  // the text base line and the overbar axis.
406 }
float GetLineWidth() const
Get the line width.
const VECTOR2D & GetGlyphSize() const
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:119

References ComputeOverbarVerticalPosition(), KIGFX::GAL::GetGlyphSize(), KIGFX::GAL::GetLineWidth(), m_gal, and VECTOR2< T >::y.

Referenced by drawSingleLineText(), and KIGFX::GAL::GetOverbarVerticalPosition().

◆ ComputeStringBoundaryLimits()

VECTOR2D STROKE_FONT::ComputeStringBoundaryLimits ( const UTF8 aText,
const VECTOR2D aGlyphSize,
double  aGlyphThickness 
) const

Compute the boundary limits of aText (the bounding box of all shapes).

The overbar and alignment are not taken in account, '~' characters are skipped.

Returns
a VECTOR2D giving the width and height of text.

Definition at line 415 of file stroke_font.cpp.

417 {
418  VECTOR2D string_bbox;
419  int line_count = 1;
420  double maxX = 0.0, curX = 0.0;
421 
422  for( UTF8::uni_iter it = aText.ubegin(), end = aText.uend(); it < end; ++it )
423  {
424  if( *it == '\n' )
425  {
426  curX = 0.0;
427  maxX = std::max( maxX, curX );
428  ++line_count;
429  continue;
430  }
431 
432  // If it is double tilda, then it is displayed as a single tilda
433  // If it is single tilda, then it is toggling overbar, so we need to skip it
434  if( *it == '~' )
435  {
436  if( ++it >= end )
437  break;
438  }
439 
440  // Index in the bounding boxes table
441  int dd = *it - ' ';
442 
443  if( dd >= (int) m_glyphBoundingBoxes.size() || dd < 0 )
444  dd = '?' - ' ';
445 
446  const BOX2D& box = m_glyphBoundingBoxes[dd];
447  curX += box.GetEnd().x;
448  }
449 
450  string_bbox.x = std::max( maxX, curX );
451  string_bbox.x *= aGlyphSize.x;
452  string_bbox.x += aGlyphThickness;
453  string_bbox.y = line_count * GetInterline( aGlyphSize.y, aGlyphThickness );
454 
455  // For italic correction, take in account italic tilt
456  if( m_gal->IsFontItalic() )
457  string_bbox.x += string_bbox.y * STROKE_FONT::ITALIC_TILT;
458 
459  return string_bbox;
460 }
static double GetInterline(double aGlyphHeight, double aGlyphThickness)
Compute the distance (interline) between 2 lines of text (for multiline texts).
const Vec GetEnd() const
Definition: box2.h:193
std::vector< BOX2D > m_glyphBoundingBoxes
Bounding boxes of the glyphs.
Definition: stroke_font.h:121
uni_iter uend() const
Function uend returns a uni_iter initialized to the end of "this" UTF8 byte sequence.
Definition: utf8.h:294
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:189
Class BOX2 handles a 2-D bounding box, built on top of an origin point and size vector,...
Definition: box2.h:41
uni_iter ubegin() const
Function ubegin returns a uni_iter initialized to the start of "this" UTF8 byte sequence.
Definition: utf8.h:285
GAL * m_gal
Pointer to the GAL.
Definition: stroke_font.h:119
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
#define max(a, b)
Definition: auxiliary.h:86

References BOX2< Vec >::GetEnd(), GetInterline(), KIGFX::GAL::IsFontItalic(), ITALIC_TILT, m_gal, m_glyphBoundingBoxes, max, UTF8::ubegin(), UTF8::uend(), VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by computeTextLineSize(), and EDA_TEXT::GetTextBox().

◆ computeTextLineSize()

VECTOR2D STROKE_FONT::computeTextLineSize ( const UTF8 aText) const
private

Compute the X and Y size of a given text.

The text is expected to be a only one line text.

Parameters
aTextis the text string (one line).
Returns
the text size.

Definition at line 409 of file stroke_font.cpp.

410 {
412 }
float GetLineWidth() const
Get the line width.
const VECTOR2D & GetGlyphSize() const
GAL * m_gal
Pointer to the GAL.
Definition: stroke_font.h:119
VECTOR2D ComputeStringBoundaryLimits(const UTF8 &aText, const VECTOR2D &aGlyphSize, double aGlyphThickness) const
Compute the boundary limits of aText (the bounding box of all shapes).

References ComputeStringBoundaryLimits(), KIGFX::GAL::GetGlyphSize(), KIGFX::GAL::GetLineWidth(), and m_gal.

Referenced by drawSingleLineText(), and KIGFX::GAL::GetTextLineSize().

◆ Draw()

void STROKE_FONT::Draw ( const UTF8 aText,
const VECTOR2D aPosition,
double  aRotationAngle 
)

Draw a string.

Parameters
aTextis the text to be drawn.
aPositionis the text position in world coordinates.
aRotationAngleis the text rotation angle in radians.

Definition at line 163 of file stroke_font.cpp.

164 {
165  if( aText.empty() )
166  return;
167 
168  // Context needs to be saved before any transformations
169  m_gal->Save();
170 
171  m_gal->Translate( aPosition );
172  m_gal->Rotate( -aRotationAngle );
173 
174  // Single line height
175  int lineHeight = getInterline( );
176  int lineCount = linesCount( aText );
177  const VECTOR2D& glyphSize = m_gal->GetGlyphSize();
178 
179  // align the 1st line of text
180  switch( m_gal->GetVerticalJustify() )
181  {
183  m_gal->Translate( VECTOR2D( 0, glyphSize.y ) );
184  break;
185 
187  m_gal->Translate( VECTOR2D( 0, glyphSize.y / 2.0 ) );
188  break;
189 
191  break;
192 
193  default:
194  break;
195  }
196 
197  if( lineCount > 1 )
198  {
199  switch( m_gal->GetVerticalJustify() )
200  {
202  break;
203 
205  m_gal->Translate( VECTOR2D(0, -( lineCount - 1 ) * lineHeight / 2) );
206  break;
207 
209  m_gal->Translate( VECTOR2D(0, -( lineCount - 1 ) * lineHeight ) );
210  break;
211  }
212  }
213 
214  m_gal->SetIsStroke( true );
215  //m_gal->SetIsFill( false );
216 
217  if( m_gal->IsFontBold() )
219 
220  // Split multiline strings into separate ones and draw them line by line
221  size_t begin = 0;
222  size_t newlinePos = aText.find( '\n' );
223 
224  while( newlinePos != aText.npos )
225  {
226  size_t length = newlinePos - begin;
227 
228  drawSingleLineText( aText.substr( begin, length ) );
229  m_gal->Translate( VECTOR2D( 0.0, lineHeight ) );
230 
231  begin = newlinePos + 1;
232  newlinePos = aText.find( '\n', begin );
233  }
234 
235  // Draw the last (or the only one) line
236  if( !aText.empty() )
237  drawSingleLineText( aText.substr( begin ) );
238 
239  m_gal->Restore();
240 }
static constexpr std::string::size_type npos
Definition: utf8.h:155
float GetLineWidth() const
Get the line width.
static const double BOLD_FACTOR
Factor that determines relative line width for bold text.
Definition: stroke_font.h:182
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.
std::string::size_type find(char c) const
Definition: utf8.h:110
GAL * m_gal
Pointer to the GAL.
Definition: stroke_font.h:119
VECTOR2< double > VECTOR2D
Definition: vector2d.h:586
int getInterline() const
Returns a single line height using current settings.
void drawSingleLineText(const UTF8 &aText)
Draws a single line of text.
virtual void Restore()
Restore the context.
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:169
std::string substr(size_t pos=0, size_t len=npos) const
Definition: utf8.h:182
virtual void Save()
Save the context.
virtual void Translate(const VECTOR2D &aTranslation)
Translate the context.
virtual void SetIsStroke(bool aIsStrokeEnabled)
Enable/disable stroked outlines.
bool empty() const
Definition: utf8.h:108

References BOLD_FACTOR, drawSingleLineText(), UTF8::empty(), UTF8::find(), KIGFX::GAL::GetGlyphSize(), getInterline(), KIGFX::GAL::GetLineWidth(), KIGFX::GAL::GetVerticalJustify(), GR_TEXT_VJUSTIFY_BOTTOM, GR_TEXT_VJUSTIFY_CENTER, GR_TEXT_VJUSTIFY_TOP, KIGFX::GAL::IsFontBold(), linesCount(), m_gal, UTF8::npos, KIGFX::GAL::Restore(), KIGFX::GAL::Rotate(), KIGFX::GAL::Save(), KIGFX::GAL::SetIsStroke(), KIGFX::GAL::SetLineWidth(), UTF8::substr(), KIGFX::GAL::Translate(), and VECTOR2< T >::y.

Referenced by KIGFX::GAL::StrokeText().

◆ drawSingleLineText()

void STROKE_FONT::drawSingleLineText ( const UTF8 aText)
private

Draws a single line of text.

Multiline texts should be split before using the function.

Parameters
aTextis the text to be drawn.

Definition at line 243 of file stroke_font.cpp.

244 {
245  double xOffset;
246  VECTOR2D glyphSize( m_gal->GetGlyphSize() );
247  double overbar_italic_comp = computeOverbarVerticalPosition() * ITALIC_TILT;
248 
249  if( m_gal->IsTextMirrored() )
250  overbar_italic_comp = -overbar_italic_comp;
251 
252  // Compute the text size
253  VECTOR2D textSize = computeTextLineSize( aText );
254  double half_thickness = m_gal->GetLineWidth()/2;
255 
256  // Context needs to be saved before any transformations
257  m_gal->Save();
258 
259  // First adjust: the text X position is corrected by half_thickness
260  // because when the text with thickness is draw, its full size is textSize,
261  // but the position of lines is half_thickness to textSize - half_thickness
262  // so we must translate the coordinates by half_thickness on the X axis
263  // to place the text inside the 0 to textSize X area.
264  m_gal->Translate( VECTOR2D( half_thickness, 0 ) );
265 
266  // Adjust the text position to the given horizontal justification
267  switch( m_gal->GetHorizontalJustify() )
268  {
270  m_gal->Translate( VECTOR2D( -textSize.x / 2.0, 0 ) );
271  break;
272 
274  if( !m_gal->IsTextMirrored() )
275  m_gal->Translate( VECTOR2D( -textSize.x, 0 ) );
276  break;
277 
279  if( m_gal->IsTextMirrored() )
280  m_gal->Translate( VECTOR2D( -textSize.x, 0 ) );
281  break;
282 
283  default:
284  break;
285  }
286 
287  if( m_gal->IsTextMirrored() )
288  {
289  // In case of mirrored text invert the X scale of points and their X direction
290  // (m_glyphSize.x) and start drawing from the position where text normally should end
291  // (textSize.x)
292  xOffset = textSize.x - m_gal->GetLineWidth();
293  glyphSize.x = -glyphSize.x;
294  }
295  else
296  {
297  xOffset = 0.0;
298  }
299 
300  // The overbar is indented inward at the beginning of an italicized section, but
301  // must not be indented on subsequent letters to ensure that the bar segments
302  // overlap.
303  bool last_had_overbar = false;
304  auto processedText = ProcessOverbars( aText );
305  const auto& text = processedText.first;
306  const auto& overbars = processedText.second;
307  int overbar_index = 0;
308 
309  for( UTF8::uni_iter chIt = text.ubegin(), end = text.uend(); chIt < end; ++chIt )
310  {
311  int dd = *chIt - ' ';
312 
313  // Handle tabs as locked to the nearest 4th column (counting in spaces)
314  // The choice of spaces is somewhat arbitrary but sufficient for aligning text
315  if( *chIt == '\t' )
316  {
317  double fourSpaces = 4.0 * glyphSize.x * m_glyphBoundingBoxes[0].GetEnd().x;
318  double addlSpace = fourSpaces - std::fmod( xOffset, fourSpaces );
319 
320  // Add the remaining space (between 0 and 3 spaces)
321  xOffset += addlSpace;
322 
323  // Set the character to ' ' instead of the '?' for tab
324  dd = 0;
325  }
326 
327  if( dd >= (int) m_glyphBoundingBoxes.size() || dd < 0 )
328  dd = '?' - ' ';
329 
330  GLYPH& glyph = m_glyphs[dd];
331  BOX2D& bbox = m_glyphBoundingBoxes[dd];
332 
333  if( overbars[overbar_index] )
334  {
335  double overbar_start_x = xOffset;
336  double overbar_start_y = - computeOverbarVerticalPosition();
337  double overbar_end_x = xOffset + glyphSize.x * bbox.GetEnd().x;
338  double overbar_end_y = overbar_start_y;
339 
340  if( !last_had_overbar )
341  {
342  if( m_gal->IsFontItalic() )
343  overbar_start_x += overbar_italic_comp;
344 
345  last_had_overbar = true;
346  }
347 
348  VECTOR2D startOverbar( overbar_start_x, overbar_start_y );
349  VECTOR2D endOverbar( overbar_end_x, overbar_end_y );
350 
351  m_gal->DrawLine( startOverbar, endOverbar );
352  }
353  else
354  {
355  last_had_overbar = false;
356  }
357 
358  for( GLYPH::iterator pointListIt = glyph.begin(); pointListIt != glyph.end();
359  ++pointListIt )
360  {
361  std::deque<VECTOR2D> pointListScaled;
362 
363  for( std::deque<VECTOR2D>::iterator pointIt = pointListIt->begin();
364  pointIt != pointListIt->end(); ++pointIt )
365  {
366  VECTOR2D pointPos( pointIt->x * glyphSize.x + xOffset, pointIt->y * glyphSize.y );
367 
368  if( m_gal->IsFontItalic() )
369  {
370  // FIXME should be done other way - referring to the lowest Y value of point
371  // because now italic fonts are translated a bit
372  if( m_gal->IsTextMirrored() )
373  pointPos.x += pointPos.y * STROKE_FONT::ITALIC_TILT;
374  else
375  pointPos.x -= pointPos.y * STROKE_FONT::ITALIC_TILT;
376  }
377 
378  pointListScaled.push_back( pointPos );
379  }
380 
381  m_gal->DrawPolyline( pointListScaled );
382  }
383 
384  xOffset += glyphSize.x * bbox.GetEnd().x;
385  ++overbar_index;
386  }
387 
388  m_gal->Restore();
389 }
virtual void DrawPolyline(const std::deque< VECTOR2D > &aPointList)
Draw a polyline.
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
const Vec GetEnd() const
Definition: box2.h:193
bool IsTextMirrored() const
Returns true if text should displayed mirrored.
GLYPH_LIST m_glyphs
Glyph list.
Definition: stroke_font.h:120
std::vector< BOX2D > m_glyphBoundingBoxes
Bounding boxes of the glyphs.
Definition: stroke_font.h:121
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:189
virtual void DrawLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Draw a line.
EDA_TEXT_HJUSTIFY_T GetHorizontalJustify() const
Returns current text horizontal justification setting.
Class BOX2 handles a 2-D bounding box, built on top of an origin point and size vector,...
Definition: box2.h:41
const VECTOR2D & GetGlyphSize() const
GAL * m_gal
Pointer to the GAL.
Definition: stroke_font.h:119
VECTOR2< double > VECTOR2D
Definition: vector2d.h:586
VECTOR2D computeTextLineSize(const UTF8 &aText) const
Compute the X and Y size of a given text.
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
double computeOverbarVerticalPosition() const
Compute the vertical position of an overbar, sometimes used in texts.
virtual void Restore()
Restore the context.
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.

References computeOverbarVerticalPosition(), computeTextLineSize(), KIGFX::GAL::DrawLine(), KIGFX::GAL::DrawPolyline(), BOX2< Vec >::GetEnd(), KIGFX::GAL::GetGlyphSize(), KIGFX::GAL::GetHorizontalJustify(), KIGFX::GAL::GetLineWidth(), GR_TEXT_HJUSTIFY_CENTER, GR_TEXT_HJUSTIFY_LEFT, GR_TEXT_HJUSTIFY_RIGHT, KIGFX::GAL::IsFontItalic(), KIGFX::GAL::IsTextMirrored(), ITALIC_TILT, m_gal, m_glyphBoundingBoxes, m_glyphs, ProcessOverbars(), KIGFX::GAL::Restore(), KIGFX::GAL::Save(), KIGFX::GAL::Translate(), VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by Draw().

◆ GetInterline()

double STROKE_FONT::GetInterline ( double  aGlyphHeight,
double  aGlyphThickness 
)
static

Compute the distance (interline) between 2 lines of text (for multiline texts).

Parameters
aGlyphHeightis the height (vertical size) of the text.
aGlyphThicknessis the thickness of the lines used to draw the text.
Returns
the interline.

Definition at line 127 of file stroke_font.cpp.

128 {
129  return ( aGlyphHeight * INTERLINE_PITCH_RATIO ) + aGlyphThickness;
130 }
static const double INTERLINE_PITCH_RATIO
Factor that determines the pitch between 2 lines.
Definition: stroke_font.h:192

References INTERLINE_PITCH_RATIO.

Referenced by ComputeStringBoundaryLimits(), SCH_GLOBALLABEL::CreateGraphicShape(), getInterline(), and EDA_TEXT::GetInterline().

◆ getInterline()

int STROKE_FONT::getInterline ( ) const
private

Returns a single line height using current settings.

Returns
The line height.

Definition at line 133 of file stroke_font.cpp.

134 {
136 }
float GetLineWidth() const
Get the line width.
static double GetInterline(double aGlyphHeight, double aGlyphThickness)
Compute the distance (interline) between 2 lines of text (for multiline texts).
static int KiROUND(double v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: common.h:118
const VECTOR2D & GetGlyphSize() const
GAL * m_gal
Pointer to the GAL.
Definition: stroke_font.h:119

References KIGFX::GAL::GetGlyphSize(), GetInterline(), KIGFX::GAL::GetLineWidth(), KiROUND(), m_gal, and VECTOR2< T >::y.

Referenced by Draw().

◆ linesCount()

unsigned KIGFX::STROKE_FONT::linesCount ( const UTF8 aText) const
inlineprivate

Returns number of lines for a given text.

Parameters
aTextis the text to be checked.
Returns
unsigned - The number of lines in aText.

Definition at line 169 of file stroke_font.h.

170  {
171  if( aText.empty() )
172  return 0; // std::count does not work well with empty strings
173  else
174  // aText.end() - 1 is to skip a newline character that is potentially at the end
175  return std::count( aText.begin(), aText.end() - 1, '\n' ) + 1;
176  }
std::string::const_iterator end() const
Definition: utf8.h:197
std::string::const_iterator begin() const
Definition: utf8.h:196
bool empty() const
Definition: utf8.h:108

References UTF8::begin(), UTF8::empty(), and UTF8::end().

Referenced by Draw().

◆ LoadNewStrokeFont()

bool STROKE_FONT::LoadNewStrokeFont ( const char *const  aNewStrokeFont[],
int  aNewStrokeFontSize 
)

Load the new stroke font.

Parameters
aNewStrokeFontis the pointer to the font data.
aNewStrokeFontSizeis the size of the font data.
Returns
True, if the font was successfully loaded, else false.

Definition at line 49 of file stroke_font.cpp.

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 }
GLYPH_LIST m_glyphs
Glyph list.
Definition: stroke_font.h:120
std::vector< BOX2D > m_glyphBoundingBoxes
Bounding boxes of the glyphs.
Definition: stroke_font.h:121
BOX2D computeBoundingBox(const GLYPH &aGlyph, const VECTOR2D &aGlyphBoundingX) const
Compute the bounding box of a given glyph.
VECTOR2< double > VECTOR2D
Definition: vector2d.h:586
static const double STROKE_FONT_SCALE
Scale factor for a glyph
Definition: stroke_font.h:185
#define FONT_OFFSET
size_t i
Definition: json11.cpp:597
std::deque< std::deque< VECTOR2D > > GLYPH
Definition: stroke_font.h:43

References computeBoundingBox(), FONT_OFFSET, i, m_glyphBoundingBoxes, m_glyphs, STROKE_FONT_SCALE, VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by KIGFX::GAL::GAL().

◆ SetGAL()

void KIGFX::STROKE_FONT::SetGAL ( GAL aGal)
inline

Function SetGAL Changes Graphics Abstraction Layer used for drawing items for a new one.

Parameters
aGalis the new GAL instance.

Definition at line 84 of file stroke_font.h.

85  {
86  m_gal = aGal;
87  }
GAL * m_gal
Pointer to the GAL.
Definition: stroke_font.h:119

References m_gal.

Friends And Related Function Documentation

◆ GAL

friend class GAL
friend

Definition at line 55 of file stroke_font.h.

Member Data Documentation

◆ BOLD_FACTOR

const double STROKE_FONT::BOLD_FACTOR = 1.3
staticprivate

Factor that determines relative line width for bold text.

Definition at line 182 of file stroke_font.h.

Referenced by Draw().

◆ INTERLINE_PITCH_RATIO

const double STROKE_FONT::INTERLINE_PITCH_RATIO = 1.5
staticprivate

Factor that determines the pitch between 2 lines.

Definition at line 192 of file stroke_font.h.

Referenced by GetInterline().

◆ ITALIC_TILT

const double STROKE_FONT::ITALIC_TILT = 1.0 / 8
staticprivate

Tilt factor for italic style (the is is the scaling factor on dY relative coordinates to give a tilst shape

Definition at line 189 of file stroke_font.h.

Referenced by ComputeStringBoundaryLimits(), and drawSingleLineText().

◆ m_gal

GAL* KIGFX::STROKE_FONT::m_gal
private

◆ m_glyphBoundingBoxes

std::vector<BOX2D> KIGFX::STROKE_FONT::m_glyphBoundingBoxes
private

Bounding boxes of the glyphs.

Definition at line 121 of file stroke_font.h.

Referenced by ComputeStringBoundaryLimits(), drawSingleLineText(), and LoadNewStrokeFont().

◆ m_glyphs

GLYPH_LIST KIGFX::STROKE_FONT::m_glyphs
private

Glyph list.

Definition at line 120 of file stroke_font.h.

Referenced by drawSingleLineText(), and LoadNewStrokeFont().

◆ OVERBAR_POSITION_FACTOR

const double STROKE_FONT::OVERBAR_POSITION_FACTOR = 1.22
staticprivate

Factor that determines relative vertical position of the overbar.

Definition at line 179 of file stroke_font.h.

Referenced by ComputeOverbarVerticalPosition().

◆ STROKE_FONT_SCALE

const double STROKE_FONT::STROKE_FONT_SCALE = 1.0 / 21.0
staticprivate

Scale factor for a glyph

Definition at line 185 of file stroke_font.h.

Referenced by LoadNewStrokeFont().


The documentation for this class was generated from the following files: