KiCad PCB EDA Suite
drawtxt.cpp File Reference

Functions to draw and plot text on screen. More...

#include <fctsys.h>
#include <gr_basic.h>
#include <common.h>
#include <plot_common.h>
#include <eda_text.h>
#include <trigo.h>
#include <macros.h>
#include <class_drawpanel.h>
#include <class_base_screen.h>
#include <basic_gal.h>

Go to the source code of this file.

Functions

int GetPenSizeForBold (int aTextSize)
 Function GetPensizeForBold. More...
 
int Clamp_Text_PenSize (int aPenSize, int aSize, bool aBold)
 Function Clamp_Text_PenSize As a rule, pen width should not be >1/4em, otherwise the character will be cluttered up in its own fatness so pen width max is aSize/4 for bold text, and aSize/6 for normal text The "best" pen width is aSize/5 for bold texts, so the clamp is consistant with bold option. More...
 
int Clamp_Text_PenSize (int aPenSize, wxSize aSize, bool aBold)
 
int GraphicTextWidth (const wxString &aText, const wxSize &aSize, bool aItalic, bool aBold)
 Function GraphicTextWidth. More...
 
void DrawGraphicText (EDA_RECT *aClipBox, wxDC *aDC, const wxPoint &aPos, COLOR4D aColor, const wxString &aText, double aOrient, const wxSize &aSize, enum EDA_TEXT_HJUSTIFY_T aH_justify, enum EDA_TEXT_VJUSTIFY_T aV_justify, int aWidth, bool aItalic, bool aBold, void(*aCallback)(int x0, int y0, int xf, int yf), PLOTTER *aPlotter)
 Function DrawGraphicText Draw a graphic text (like module texts) More...
 
void DrawGraphicHaloText (EDA_RECT *aClipBox, wxDC *aDC, const wxPoint &aPos, const COLOR4D aBgColor, COLOR4D aColor1, COLOR4D aColor2, const wxString &aText, double aOrient, const wxSize &aSize, enum EDA_TEXT_HJUSTIFY_T aH_justify, enum EDA_TEXT_VJUSTIFY_T aV_justify, int aWidth, bool aItalic, bool aBold, void(*aCallback)(int x0, int y0, int xf, int yf), PLOTTER *aPlotter)
 Draw graphic text with a border, so that it can be read on different backgrounds. More...
 

Detailed Description

Functions to draw and plot text on screen.

Definition in file drawtxt.cpp.

Function Documentation

int Clamp_Text_PenSize ( int  aPenSize,
int  aSize,
bool  aBold 
)

Function Clamp_Text_PenSize As a rule, pen width should not be >1/4em, otherwise the character will be cluttered up in its own fatness so pen width max is aSize/4 for bold text, and aSize/6 for normal text The "best" pen width is aSize/5 for bold texts, so the clamp is consistant with bold option.

Function Clamp_Text_PenSize As a rule, pen width should not be >1/4em, otherwise the character will be cluttered up in its own fatness The pen width max is aSize/4 for bold texts, and aSize/6 for normal texts The "best" pen width is aSize/5 for bold texts, so the clamp is consistant with bold option.

Parameters
aPenSize= the pen size to clamp
aSizethe char size (height or width)
aBold= true if text accept bold pen size
Returns
the max pen size allowed

Definition at line 67 of file drawtxt.cpp.

References abs, KiROUND(), and scale.

Referenced by Clamp_Text_PenSize(), SCH_GLOBALLABEL::CreateGraphicShape(), FOOTPRINT_EDIT_FRAME::CreateTextModule(), SCH_TEXT::Draw(), SCH_FIELD::Draw(), SCH_GLOBALLABEL::Draw(), SCH_HIERLABEL::Draw(), DRAWING_TOOL::DrawDimension(), LIB_FIELD::drawGraphic(), LIB_PIN::DrawPinTexts(), PCB_EDIT_FRAME::EditDimension(), SCH_FIELD::GetBoundingBox(), SCH_TEXT::GetBoundingBox(), LIB_TEXT::GetPenSize(), SCH_FIELD::GetPenSize(), SCH_TEXT::GetPenSize(), SCH_GLOBALLABEL::GetSchematicTextOffset(), EAGLE_PLUGIN::loadPlain(), DIALOG_DIMENSION_EDITOR::OnOKClick(), SCH_SHEET::Plot(), LIB_PIN::PlotPinTexts(), PLOTTER::Text(), DialogEditModuleText::TransferDataFromWindow(), and DIALOG_PCB_TEXT_PROPERTIES::TransferDataFromWindow().

68 {
69  int penSize = aPenSize;
70  double scale = aBold ? 4.0 : 6.0;
71  int maxWidth = KiROUND( std::abs( aSize ) / scale );
72 
73  if( penSize > maxWidth )
74  penSize = maxWidth;
75 
76  return penSize;
77 }
static int KiROUND(double v)
KiROUND rounds a floating point number to an int using "round halfway cases away from zero"...
Definition: common.h:107
#define abs(a)
Definition: auxiliary.h:84
const int scale
int Clamp_Text_PenSize ( int  aPenSize,
wxSize  aSize,
bool  aBold 
)

Definition at line 80 of file drawtxt.cpp.

References abs, Clamp_Text_PenSize(), and min.

81 {
82  int size = std::min( std::abs( aSize.x ), std::abs( aSize.y ) );
83 
84  return Clamp_Text_PenSize( aPenSize, size, aBold );
85 }
#define abs(a)
Definition: auxiliary.h:84
int Clamp_Text_PenSize(int aPenSize, int aSize, bool aBold)
Function Clamp_Text_PenSize As a rule, pen width should not be >1/4em, otherwise the character will b...
Definition: drawtxt.cpp:67
#define min(a, b)
Definition: auxiliary.h:85
void DrawGraphicHaloText ( EDA_RECT aClipBox,
wxDC *  aDC,
const wxPoint aPos,
const COLOR4D  aBgColor,
COLOR4D  aColor1,
COLOR4D  aColor2,
const wxString &  aText,
double  aOrient,
const wxSize &  aSize,
enum EDA_TEXT_HJUSTIFY_T  aH_justify,
enum EDA_TEXT_VJUSTIFY_T  aV_justify,
int  aWidth,
bool  aItalic,
bool  aBold,
void(*)(int x0, int y0, int xf, int yf)  aCallback = NULL,
PLOTTER aPlotter = NULL 
)

Draw graphic text with a border, so that it can be read on different backgrounds.

See DrawGraphicText for most of the parameters. If aBgColor is a dark color text is drawn in aColor2 with aColor1 border; otherwise colors are swapped.

Definition at line 176 of file drawtxt.cpp.

References DrawGraphicText(), and KIGFX::COLOR4D::GetBrightness().

Referenced by VIA::Draw(), D_PAD::DrawShape(), and TRACK::DrawShortNetname().

189 {
190  // Swap color if contrast would be better
191  // TODO: Maybe calculate contrast some way other than brightness
192  if( aBgColor.GetBrightness() > 0.5 )
193  {
194  COLOR4D c = aColor1;
195  aColor1 = aColor2;
196  aColor2 = c;
197  }
198 
199  // Draw the background
200  DrawGraphicText( aClipBox, aDC, aPos, aColor1, aText, aOrient, aSize,
201  aH_justify, aV_justify, aWidth, aItalic, aBold,
202  aCallback, aPlotter );
203 
204  // Draw the text
205  DrawGraphicText( aClipBox, aDC, aPos, aColor2, aText, aOrient, aSize,
206  aH_justify, aV_justify, aWidth/4, aItalic, aBold,
207  aCallback, aPlotter );
208 }
void DrawGraphicText(EDA_RECT *aClipBox, wxDC *aDC, const wxPoint &aPos, COLOR4D aColor, const wxString &aText, double aOrient, const wxSize &aSize, enum EDA_TEXT_HJUSTIFY_T aH_justify, enum EDA_TEXT_VJUSTIFY_T aV_justify, int aWidth, bool aItalic, bool aBold, void(*aCallback)(int x0, int y0, int xf, int yf), PLOTTER *aPlotter)
Function DrawGraphicText Draw a graphic text (like module texts)
Definition: drawtxt.cpp:122
double GetBrightness() const
Function GetBrightness Returns the brightness value of the color ranged from 0.0 to 1...
Definition: color4d.h:254
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39
void DrawGraphicText ( EDA_RECT aClipBox,
wxDC *  aDC,
const wxPoint aPos,
COLOR4D  aColor,
const wxString &  aText,
double  aOrient,
const wxSize &  aSize,
enum EDA_TEXT_HJUSTIFY_T  aH_justify,
enum EDA_TEXT_VJUSTIFY_T  aV_justify,
int  aWidth,
bool  aItalic,
bool  aBold,
void(*)(int x0, int y0, int xf, int yf)  aCallback,
PLOTTER aPlotter 
)

Function DrawGraphicText Draw a graphic text (like module texts)

Parameters
aClipBox= the clipping rect, or NULL if no clipping
aDC= the current Device Context. NULL if draw within a 3D GL Canvas
aPos= text position (according to h_justify, v_justify)
aColor(COLOR4D) = text color
aText= text to draw
aOrient= angle in 0.1 degree
aSize= text size (size.x or size.y can be < 0 for mirrored texts)
aH_justify= horizontal justification (Left, center, right)
aV_justify= vertical justification (bottom, center, top)
aWidth= line width (pen width) (use default width if aWidth = 0) if width < 0 : draw segments in sketch mode, width = abs(width) Use a value min(aSize.x, aSize.y) / 5 for a bold text
aItalic= true to simulate an italic font
aBold= true to use a bold font. Useful only with default width value (aWidth = 0)
aCallback()= function called (if non null) to draw each segment. used to draw 3D texts or for plotting, NULL for normal drawings
aPlotter= a pointer to a PLOTTER instance, when this function is used to plot the text. NULL to draw this text.

Definition at line 122 of file drawtxt.cpp.

References basic_gal, dummy(), GetPenSizeForBold(), BASIC_GAL::m_Color, BASIC_GAL::m_DC, min, EDA_TEXT::SetBold(), BASIC_GAL::SetCallback(), BASIC_GAL::SetClipBox(), EDA_TEXT::SetHorizJustify(), KIGFX::GAL::SetIsFill(), EDA_TEXT::SetItalic(), KIGFX::GAL::SetLineWidth(), EDA_TEXT::SetMirrored(), BASIC_GAL::SetPlotter(), KIGFX::GAL::SetTextAttributes(), EDA_TEXT::SetTextSize(), EDA_TEXT::SetVertJustify(), and KIGFX::GAL::StrokeText().

Referenced by CINFO3D_VISU::AddGraphicsShapesWithClearanceToContainer(), CINFO3D_VISU::AddShapeWithClearanceToContainer(), SCH_FIELD::Draw(), TEXTE_MODULE::Draw(), SCH_SHEET::Draw(), LIB_TEXT::drawGraphic(), LIB_FIELD::drawGraphic(), DrawGraphicHaloText(), GBR_LAYOUT::DrawItemsDCodeID(), EDA_TEXT::drawOneLineOfText(), LIB_PIN::DrawPinElectricalTypeName(), LIB_PIN::DrawPinTexts(), export_vrml_pcbtext(), export_vrml_text_module(), PLOTTER::Text(), MODULE::TransformGraphicShapesWithClearanceToPolygonSet(), MODULE::TransformGraphicTextWithClearanceToPolygonSet(), TEXTE_PCB::TransformShapeWithClearanceToPolygonSet(), and EDA_TEXT::TransformTextShapeToSegmentList().

136 {
137  bool fill_mode = true;
138 
139  if( aWidth == 0 && aBold ) // Use default values if aWidth == 0
140  aWidth = GetPenSizeForBold( std::min( aSize.x, aSize.y ) );
141 
142  if( aWidth < 0 )
143  {
144  aWidth = -aWidth;
145  fill_mode = false;
146  }
147 
148  basic_gal.SetIsFill( fill_mode );
149  basic_gal.SetLineWidth( aWidth );
150 
151  EDA_TEXT dummy;
152  dummy.SetItalic( aItalic );
153  dummy.SetBold( aBold );
154  dummy.SetHorizJustify( aH_justify );
155  dummy.SetVertJustify( aV_justify );
156 
157  wxSize size = aSize;
158  dummy.SetMirrored( size.x < 0 );
159 
160  if( size.x < 0 )
161  size.x = - size.x;
162 
163  dummy.SetTextSize( size );
164 
165  basic_gal.SetTextAttributes( &dummy );
166  basic_gal.SetPlotter( aPlotter );
167  basic_gal.SetCallback( aCallback );
168  basic_gal.m_DC = aDC;
169  basic_gal.m_Color = aColor;
170  basic_gal.SetClipBox( aClipBox );
171 
172  basic_gal.StrokeText( aText, VECTOR2D( aPos ), aOrient * M_PI/1800 );
173 }
void SetMirrored(bool isMirrored)
Definition: eda_text.h:178
virtual void SetTextAttributes(const EDA_TEXT *aText)
Loads attributes of the given text (bold/italic/underline/mirrored and so on).
void SetItalic(bool isItalic)
Definition: eda_text.h:169
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:214
Class EDA_TEXT is a mix-in class (via multiple inheritance) that handles texts such as labels...
Definition: eda_text.h:114
virtual void SetLineWidth(double aLineWidth)
Set the line width.
VECTOR2< double > VECTOR2D
Definition: vector2d.h:589
virtual void SetIsFill(bool aIsFillEnabled)
Enable/disable fill.
COLOR4D m_Color
Definition: basic_gal.h:63
virtual void StrokeText(const wxString &aText, const VECTOR2D &aPosition, double aRotationAngle)
Draws a vector type text using preloaded Newstroke font.
void SetVertJustify(EDA_TEXT_VJUSTIFY_T aType)
Definition: eda_text.h:194
void SetPlotter(PLOTTER *aPlotter)
Definition: basic_gal.h:80
void SetCallback(void(*aCallback)(int x0, int y0, int xf, int yf))
Definition: basic_gal.h:85
int GetPenSizeForBold(int aTextSize)
Function GetPensizeForBold.
Definition: drawtxt.cpp:49
wxDC * m_DC
Definition: basic_gal.h:62
static LIB_PART * dummy()
Used when a LIB_PART is not found in library to draw a dummy shape This component is a 400 mils squar...
void SetHorizJustify(EDA_TEXT_HJUSTIFY_T aType)
Definition: eda_text.h:193
BASIC_GAL basic_gal(basic_displayOptions)
void SetBold(bool aBold)
Definition: eda_text.h:172
void SetClipBox(EDA_RECT *aClipBox)
Set a clip box for drawings If NULL, no clip will be made.
Definition: basic_gal.h:92
#define min(a, b)
Definition: auxiliary.h:85
int GetPenSizeForBold ( int  aTextSize)

Function GetPensizeForBold.

Returns
the "best" value for a pen size to draw/plot a bold text
Parameters
aTextSize= the char size (height or width)

Definition at line 49 of file drawtxt.cpp.

References KiROUND().

Referenced by WS_DRAW_ITEM_LIST::BuildWorkSheetGraphicList(), SCH_FIELD::Draw(), LIB_FIELD::drawGraphic(), DrawGraphicText(), LIB_TEXT::GetPenSize(), SCH_FIELD::GetPenSize(), SCH_TEXT::GetPenSize(), SCH_TEXT::Load(), SCH_LABEL::Load(), SCH_GLOBALLABEL::Load(), SCH_HIERLABEL::Load(), SCH_LEGACY_PLUGIN::loadText(), PLOTTER::Text(), and DIALOG_LABEL_EDITOR::TextPropertiesAccept().

50 {
51  return KiROUND( aTextSize / 5.0 );
52 }
static int KiROUND(double v)
KiROUND rounds a floating point number to an int using "round halfway cases away from zero"...
Definition: common.h:107
int GraphicTextWidth ( const wxString &  aText,
const wxSize &  aSize,
bool  italic,
bool  bold 
)

Function GraphicTextWidth.

Returns
the X size of the graphic text the full X size is GraphicTextWidth + the thickness of graphic lines

Definition at line 88 of file drawtxt.cpp.

References basic_gal, KIGFX::GAL::GetTextLineSize(), KiROUND(), KIGFX::GAL::SetFontBold(), KIGFX::GAL::SetFontItalic(), KIGFX::GAL::SetGlyphSize(), and VECTOR2< T >::x.

Referenced by SCH_SHEET::GetBoundingBox().

89 {
90  basic_gal.SetFontItalic( aItalic );
91  basic_gal.SetFontBold( aBold );
92  basic_gal.SetGlyphSize( VECTOR2D( aSize ) );
93 
94  VECTOR2D tsize = basic_gal.GetTextLineSize( aText );
95 
96  return KiROUND( tsize.x );
97 }
void SetFontItalic(const bool aItalic)
Set italic property of current font.
static int KiROUND(double v)
KiROUND rounds a floating point number to an int using "round halfway cases away from zero"...
Definition: common.h:107
void SetFontBold(const bool aBold)
Set bold property of current font.
VECTOR2< double > VECTOR2D
Definition: vector2d.h:589
void SetGlyphSize(const VECTOR2D aGlyphSize)
Set the font glyph size.
BASIC_GAL basic_gal(basic_displayOptions)
VECTOR2D GetTextLineSize(const UTF8 &aText) const
Compute the X and Y size of a given text.