KiCad PCB EDA Suite
gr_text.cpp File Reference
#include <fctsys.h>
#include <gr_basic.h>
#include <common.h>
#include <plotter.h>
#include <eda_text.h>
#include <trigo.h>
#include <macros.h>
#include <base_screen.h>
#include <gr_text.h>
#include <math/util.h>
#include <basic_gal.h>

Go to the source code of this file.

Functions

void SetTextMarkupFlags (int aMarkupFlags)
 
int GetTextMarkupFlags ()
 
int GetPenSizeForBold (int aTextSize)
 Function GetPensizeForBold. More...
 
int Clamp_Text_PenSize (int aPenSize, int aSize, bool aBold)
 Function Clamp_Text_PenSize Don't allow text to become cluttered up in its own fatness. More...
 
float Clamp_Text_PenSize (float aPenSize, int aSize, bool aBold)
 
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 GRText (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, void *aData), void *aCallbackData, PLOTTER *aPlotter)
 Function GRText Draw a graphic text (like module texts) More...
 
void GRHaloText (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, void *aData), void *aCallbackData, PLOTTER *aPlotter)
 Draw graphic text with a border, so that it can be read on different backgrounds. More...
 

Variables

static int s_textMarkupFlags = 0
 

Function Documentation

◆ Clamp_Text_PenSize() [1/3]

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

Function Clamp_Text_PenSize Don't allow text to become cluttered up in its own fatness.

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.

Bold fonts are generally around aSize/5 in width, so we limit them to aSize/4, and normal text to aSize/6.

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 81 of file gr_text.cpp.

82 {
83  double scale = aBold ? 4.0 : 6.0;
84  int maxWidth = KiROUND( (double) aSize / scale );
85 
86  return std::min( aPenSize, maxWidth );
87 }
const int scale
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:61

References KiROUND(), and scale.

Referenced by Clamp_Text_PenSize(), SCH_GLOBALLABEL::CreateGraphicShape(), KIGFX::SCH_PAINTER::draw(), SCH_FIELD::GetBoundingBox(), SCH_TEXT::GetBoundingBox(), LIB_TEXT::GetPenSize(), LIB_FIELD::GetPenSize(), SCH_FIELD::GetPenSize(), SCH_TEXT::GetPenSize(), SCH_GLOBALLABEL::GetSchematicTextOffset(), LIB_PIN::PlotPinTexts(), SCH_FIELD::Print(), SCH_TEXT::Print(), SCH_GLOBALLABEL::Print(), SCH_HIERLABEL::Print(), LIB_PIN::PrintPinTexts(), PLOTTER::Text(), and DIALOG_TEXT_PROPERTIES::TransferDataFromWindow().

◆ Clamp_Text_PenSize() [2/3]

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

Definition at line 90 of file gr_text.cpp.

91 {
92  float scale = aBold ? 4.0 : 6.0;
93  float maxWidth = (float) aSize / scale;
94 
95  return std::min( aPenSize, maxWidth );
96 }
const int scale

References scale.

◆ Clamp_Text_PenSize() [3/3]

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

Definition at line 99 of file gr_text.cpp.

100 {
101  int size = std::min( std::abs( aSize.x ), std::abs( aSize.y ) );
102 
103  return Clamp_Text_PenSize( aPenSize, size, aBold );
104 }
int Clamp_Text_PenSize(int aPenSize, int aSize, bool aBold)
Function Clamp_Text_PenSize Don't allow text to become cluttered up in its own fatness.
Definition: gr_text.cpp:81

References Clamp_Text_PenSize().

◆ GetPenSizeForBold()

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 66 of file gr_text.cpp.

67 {
68  return KiROUND( aTextSize / 5.0 );
69 }
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:61

References KiROUND().

Referenced by LIB_TEXT::GetPenSize(), LIB_FIELD::GetPenSize(), SCH_FIELD::GetPenSize(), SCH_TEXT::GetPenSize(), GRText(), SCH_LEGACY_PLUGIN::loadText(), SCH_EAGLE_PLUGIN::loadTextAttributes(), SCH_FIELD::Print(), WS_DATA_ITEM_TEXT::SyncDrawItems(), PLOTTER::Text(), SVG_PLOTTER::Text(), and DIALOG_LABEL_EDITOR::TransferDataFromWindow().

◆ GetTextMarkupFlags()

◆ GraphicTextWidth()

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 107 of file gr_text.cpp.

108 {
109  basic_gal.SetFontItalic( aItalic );
110  basic_gal.SetFontBold( aBold );
111  basic_gal.SetGlyphSize( VECTOR2D( aSize ) );
112 
113  VECTOR2D tsize = basic_gal.GetTextLineSize( aText );
114 
115  return KiROUND( tsize.x );
116 }
void SetFontItalic(const bool aItalic)
Set italic property of current font.
void SetFontBold(const bool aBold)
Set bold property of current font.
VECTOR2< double > VECTOR2D
Definition: vector2d.h:593
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:61
void SetGlyphSize(const VECTOR2D aGlyphSize)
Set the font glyph size.
BASIC_GAL basic_gal(basic_displayOptions)
VECTOR2D GetTextLineSize(const UTF8 &aText, int aMarkupFlags=0) const
Compute the X and Y size of a given text.

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

Referenced by SVG_PLOTTER::Text().

◆ GRHaloText()

void GRHaloText ( 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, void *aData)  aCallback = nullptr,
void *  aCallbackData = nullptr,
PLOTTER aPlotter = nullptr 
)

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

See GRText 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 188 of file gr_text.cpp.

194 {
195  // Swap color if contrast would be better
196  // TODO: Maybe calculate contrast some way other than brightness
197  if( aBgColor.GetBrightness() > 0.5 )
198  {
199  COLOR4D c = aColor1;
200  aColor1 = aColor2;
201  aColor2 = c;
202  }
203 
204  // Draw the background
205  GRText( aDC, aPos, aColor1, aText, aOrient, aSize, aH_justify, aV_justify, aWidth, aItalic,
206  aBold, aCallback, aCallbackData, aPlotter );
207 
208  // Draw the text
209  GRText( aDC, aPos, aColor2, aText, aOrient, aSize, aH_justify, aV_justify, aWidth/4, aItalic,
210  aBold, aCallback, aCallbackData, aPlotter );
211 }
double GetBrightness() const
Function GetBrightness Returns the brightness value of the color ranged from 0.0 to 1....
Definition: color4d.h:270
void GRText(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, void *aData), void *aCallbackData, PLOTTER *aPlotter)
Function GRText Draw a graphic text (like module texts)
Definition: gr_text.cpp:143
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:40

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

Referenced by VIA::Print(), and D_PAD::PrintShape().

◆ GRText()

void GRText ( 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, void *aData)  aCallback,
void *  aCallbackData,
PLOTTER aPlotter 
)

Function GRText Draw a graphic text (like module texts)

Parameters
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(int x0, int y0, int xf, int yf, void* aData ) is a function called (if non null) to draw each segment. used to draw 3D texts or for plotting. NULL for normal drawings
aCallbackData= is the auxiliary parameter aData for the callback function. can be nullptr if no auxiliary parameter is needed
aPlotter= a pointer to a PLOTTER instance, when this function is used to plot the text. NULL to draw this text.

Definition at line 143 of file gr_text.cpp.

148 {
149  bool fill_mode = true;
150 
151  if( aWidth == 0 && aBold ) // Use default values if aWidth == 0
152  aWidth = GetPenSizeForBold( std::min( aSize.x, aSize.y ) );
153 
154  if( aWidth < 0 )
155  {
156  aWidth = -aWidth;
157  fill_mode = false;
158  }
159 
160  basic_gal.SetIsFill( fill_mode );
161  basic_gal.SetLineWidth( aWidth );
162 
163  EDA_TEXT dummy;
164  dummy.SetItalic( aItalic );
165  dummy.SetBold( aBold );
166  dummy.SetHorizJustify( aH_justify );
167  dummy.SetVertJustify( aV_justify );
168 
169  wxSize size = aSize;
170  dummy.SetMirrored( size.x < 0 );
171 
172  if( size.x < 0 )
173  size.x = - size.x;
174 
175  dummy.SetTextSize( size );
176 
178  basic_gal.SetPlotter( aPlotter );
179  basic_gal.SetCallback( aCallback, aCallbackData );
180  basic_gal.m_DC = aDC;
181  basic_gal.m_Color = aColor;
182  basic_gal.SetClipBox( nullptr );
183 
184  basic_gal.StrokeText( aText, VECTOR2D( aPos ), aOrient * M_PI/1800, GetTextMarkupFlags() );
185 }
int GetPenSizeForBold(int aTextSize)
Function GetPensizeForBold.
Definition: gr_text.cpp:66
void SetCallback(void(*aCallback)(int x0, int y0, int xf, int yf, void *aData), void *aData)
Definition: basic_gal.h:86
virtual void SetTextAttributes(const EDA_TEXT *aText)
Loads attributes of the given text (bold/italic/underline/mirrored and so on).
virtual void SetLineWidth(float aLineWidth)
Set the line width.
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:109
int GetTextMarkupFlags()
Definition: gr_text.cpp:55
VECTOR2< double > VECTOR2D
Definition: vector2d.h:593
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, int aMarkupFlags=0)
Draws a vector type text using preloaded Newstroke font.
void SetPlotter(PLOTTER *aPlotter)
Definition: basic_gal.h:81
wxDC * m_DC
Definition: basic_gal.h:62
static LIB_PART * dummy()
Used to draw a dummy shape when a LIB_PART is not found in library.
BASIC_GAL basic_gal(basic_displayOptions)
void SetClipBox(EDA_RECT *aClipBox)
Set a clip box for drawings If NULL, no clip will be made.
Definition: basic_gal.h:94

References basic_gal, dummy(), GetPenSizeForBold(), GetTextMarkupFlags(), BASIC_GAL::m_Color, BASIC_GAL::m_DC, BASIC_GAL::SetCallback(), BASIC_GAL::SetClipBox(), KIGFX::GAL::SetIsFill(), KIGFX::GAL::SetLineWidth(), BASIC_GAL::SetPlotter(), KIGFX::GAL::SetTextAttributes(), and KIGFX::GAL::StrokeText().

Referenced by CINFO3D_VISU::AddGraphicsShapesWithClearanceToContainer(), CINFO3D_VISU::AddShapeWithClearanceToContainer(), export_vrml_pcbtext(), export_vrml_text_module(), GRHaloText(), LIB_TEXT::print(), LIB_FIELD::print(), SCH_FIELD::Print(), TEXTE_MODULE::Print(), EDA_TEXT::printOneLineOfText(), LIB_PIN::PrintPinElectricalTypeName(), LIB_PIN::PrintPinTexts(), PLOTTER::Text(), MODULE::TransformGraphicShapesWithClearanceToPolygonSet(), MODULE::TransformGraphicTextWithClearanceToPolygonSet(), TEXTE_PCB::TransformShapeWithClearanceToPolygonSet(), and EDA_TEXT::TransformTextShapeToSegmentList().

◆ SetTextMarkupFlags()

void SetTextMarkupFlags ( int  aMarkupFlags)

Definition at line 49 of file gr_text.cpp.

50 {
51  s_textMarkupFlags = aMarkupFlags;
52 }
static int s_textMarkupFlags
Definition: gr_text.cpp:46

References s_textMarkupFlags.

Referenced by PANEL_EESCHEMA_DISPLAY_OPTIONS::TransferDataFromWindow(), and PANEL_SETUP_FORMATTING::TransferDataFromWindow().

Variable Documentation

◆ s_textMarkupFlags

int s_textMarkupFlags = 0
static

Definition at line 46 of file gr_text.cpp.

Referenced by GetTextMarkupFlags(), and SetTextMarkupFlags().