KiCad PCB EDA Suite
pcad2kicad_common.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) 2007, 2008 Lubo Racko <developer@lura.sk>
5  * Copyright (C) 2008, 2012 Alexander Lunev <al.lunev@yahoo.com>
6  * Copyright (C) 2012 KiCad Developers, see CHANGELOG.TXT for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
30 #include <wx/wx.h>
31 #include <wx/config.h>
32 #include <wx/regex.h>
33 
34 #include <common.h>
35 #include <convert_to_biu.h>
36 #include <math/util.h> // for KiROUND
37 
38 #include <pcad2kicad_common.h>
39 
40 namespace PCAD2KICAD {
41 
42 // PCAD stroke font average ratio of width to size
43 const double TEXT_WIDTH_TO_SIZE_AVERAGE = 0.5;
44 // PCAD proportions of stroke font
45 const double STROKE_HEIGHT_TO_SIZE = 0.656;
46 const double STROKE_WIDTH_TO_SIZE = 0.69;
47 // TrueType font
48 const double TRUETYPE_HEIGHT_TO_SIZE = 0.585;
49 const double TRUETYPE_WIDTH_TO_SIZE = 0.585;
50 const double TRUETYPE_THICK_PER_HEIGHT = 0.073;
51 const double TRUETYPE_BOLD_THICK_MUL = 1.6;
52 const long TRUETYPE_BOLD_MIN_WEIGHT = 700;
53 
54 wxString GetWord( wxString* aStr )
55 {
56  wxString result = wxEmptyString;
57 
58  *aStr = aStr->Trim( false );
59 
60  if( aStr->Len() == 0 )
61  return result;
62 
63  if( (*aStr)[0] == wxT( '"' ) )
64  {
65  result += (*aStr)[0];
66  *aStr = aStr->Mid( 1 ); // remove Frot apostrofe
67 
68  while( aStr->Len() > 0 && (*aStr)[0] != wxT( '"' ) )
69  {
70  result += (*aStr)[0];
71  *aStr = aStr->Mid( 1 );
72  }
73 
74  if( aStr->Len() > 0 && (*aStr)[0] == wxT( '"' ) )
75  {
76  result += (*aStr)[0];
77  *aStr = aStr->Mid( 1 ); // remove ending apostrophe
78  }
79  }
80  else
81  {
82  while( aStr->Len() > 0
83  && !( (*aStr)[0] == wxT( ' ' )
84  || (*aStr)[0] == wxT( '(' )
85  || (*aStr)[0] == wxT( ')' ) ) )
86  {
87  result += (*aStr)[0];
88  *aStr = aStr->Mid( 1 );
89  }
90  }
91 
92  result.Trim( true );
93  result.Trim( false );
94 
95  return result;
96 }
97 
98 
100 {
101  XNODE* result, * lNode;
102 
103  result = NULL;
104  lNode = FindNode( aNode, wxT( "attachedPattern" ) );
105 
106  if( lNode )
107  result = FindNode( lNode, wxT( "padPinMap" ) );
108 
109  return result;
110 }
111 
112 
113 double StrToDoublePrecisionUnits( const wxString& aStr, char aAxe, const wxString& aActualConversion )
114 {
115  wxString ls;
116  double i;
117  char u;
118 
119  ls = aStr;
120  ls.Trim( true );
121  ls.Trim( false );
122 
123  if( ls.Len() > 0 )
124  {
125  u = ls[ls.Len() - 1];
126 
127  while( ls.Len() > 0
128  && !( ls[ls.Len() - 1] == wxT( '.' )
129  || ls[ls.Len() - 1] == wxT( ',' )
130  || (ls[ls.Len() - 1] >= wxT( '0' ) && ls[ls.Len() - 1] <= wxT( '9' ) ) ) )
131  {
132  ls = ls.Left( ls.Len() - 1 );
133  }
134 
135  while( ls.Len() > 0
136  && !( ls[0] == wxT( '-' )
137  || ls[0] == wxT( '+' )
138  || ls[0] == wxT( '.' )
139  || ls[0] == wxT( ',' )
140  || (ls[0] >= wxT( '0' ) && ls[0] <= wxT( '9' ) ) ) )
141  {
142  ls = ls.Mid( 1 );
143  }
144 
145  if( u == wxT( 'm' ) )
146  {
147  ls.ToDouble( &i );
148 #ifdef PCAD2KICAD_SCALE_SCH_TO_INCH_GRID
149  if( aActualConversion == wxT( "SCH" )
150  || aActualConversion == wxT( "SCHLIB" ) )
151  i = i * (0.0254 / 0.025);
152 #endif
153  i = Millimeter2iu( i );
154  }
155  else
156  {
157  ls.ToDouble( &i );
158  i = Mils2iu( i );
159  }
160  }
161  else
162  i = 0.0;
163 
164  if( ( aActualConversion == wxT( "PCB" ) || aActualConversion == wxT( "SCH" ) )
165  && aAxe == wxT( 'Y' ) )
166  return -i;
167  else
168  return i; // Y axe is mirrored compared to P-Cad
169 }
170 
171 
172 int StrToIntUnits( const wxString& aStr, char aAxe, const wxString& aActualConversion )
173 {
174  return KiROUND( StrToDoublePrecisionUnits( aStr, aAxe, aActualConversion ) );
175 }
176 
177 
178 wxString GetAndCutWordWithMeasureUnits( wxString* aStr, const wxString& aDefaultMeasurementUnit )
179 {
180  wxString result;
181 
182  aStr->Trim( false );
183  result = wxEmptyString;
184 
185  // value
186  while( aStr->Len() > 0 && (*aStr)[0] != wxT( ' ' ) )
187  {
188  result += (*aStr)[0];
189  *aStr = aStr->Mid( 1 );
190  }
191 
192  aStr->Trim( false );
193 
194  // if there is also measurement unit
195  while( aStr->Len() > 0
196  && ( ( (*aStr)[0] >= wxT( 'a' ) && (*aStr)[0] <= wxT( 'z' ) )
197  || ( (*aStr)[0] >= wxT( 'A' ) && (*aStr)[0] <= wxT( 'Z' ) ) ) )
198  {
199  result += (*aStr)[0];
200  *aStr = aStr->Mid( 1 );
201  }
202 
203  // and if not, add default....
204  if( result.Len() > 0
205  && ( result[result.Len() - 1] == wxT( '.' )
206  || result[result.Len() - 1] == wxT( ',' )
207  || (result[result.Len() - 1] >= wxT( '0' )
208  && result[result.Len() - 1] <= wxT( '9' ) ) ) )
209  {
210  result += aDefaultMeasurementUnit;
211  }
212 
213  return result;
214 }
215 
216 
217 int StrToInt1Units( const wxString& aStr )
218 {
219  double num, precision = 10;
220 
221  aStr.ToDouble( &num );
222  return KiROUND( num * precision );
223 }
224 
225 
226 wxString ValidateName( wxString aName )
227 {
228  aName.Replace( wxT( " " ), wxT( "_" ) );
229 
230  return aName;
231 }
232 
233 
234 wxString ValidateReference( wxString aRef )
235 {
236  wxRegEx reRef;
237  reRef.Compile( wxT( "^[[:digit:]][[:digit:]]*$" ) );
238 
239  if( reRef.Matches( aRef ) )
240  aRef.Prepend( wxT( '.' ) );
241 
242  return aRef;
243 }
244 
245 
246 void SetWidth( wxString aStr,
247  const wxString& aDefaultMeasurementUnit,
248  int* aWidth,
249  const wxString& aActualConversion )
250 {
252  aDefaultMeasurementUnit ), wxT( ' ' ),
253  aActualConversion );
254 }
255 
256 
257 void SetHeight( wxString aStr,
258  const wxString& aDefaultMeasurementUnit,
259  int* aHeight,
260  const wxString& aActualConversion )
261 {
262  *aHeight = StrToIntUnits( GetAndCutWordWithMeasureUnits( &aStr,
263  aDefaultMeasurementUnit ), wxT( ' ' ),
264  aActualConversion );
265 }
266 
267 
268 void SetPosition( wxString aStr,
269  const wxString& aDefaultMeasurementUnit,
270  int* aX,
271  int* aY,
272  const wxString& aActualConversion )
273 {
275  aDefaultMeasurementUnit ), wxT( 'X' ),
276  aActualConversion );
278  aDefaultMeasurementUnit ), wxT( 'Y' ),
279  aActualConversion );
280 }
281 
282 
283 void SetDoublePrecisionPosition( wxString aStr,
284  const wxString& aDefaultMeasurementUnit,
285  double* aX,
286  double* aY,
287  const wxString& aActualConversion )
288 {
290  aDefaultMeasurementUnit ), wxT( 'X' ),
291  aActualConversion );
293  aDefaultMeasurementUnit ), wxT( 'Y' ),
294  aActualConversion );
295 }
296 
297 
298 TTEXT_JUSTIFY GetJustifyIdentificator( const wxString& aJustify )
299 {
300  TTEXT_JUSTIFY id;
301 
302  if( aJustify == wxT( "LowerCenter" ) )
303  id = LowerCenter;
304  else if( aJustify == wxT( "LowerRight" ) )
305  id = LowerRight;
306  else if( aJustify == wxT( "UpperLeft" ) )
307  id = UpperLeft;
308  else if( aJustify == wxT( "UpperCenter" ) )
309  id = UpperCenter;
310  else if( aJustify == wxT( "UpperRight" ) )
311  id = UpperRight;
312  else if( aJustify == wxT( "Left" ) )
313  id = Left;
314  else if( aJustify == wxT( "Center" ) )
315  id = Center;
316  else if( aJustify == wxT( "Right" ) )
317  id = Right;
318  else
319  id = LowerLeft;
320 
321  return id;
322 }
323 
324 
326  TTEXTVALUE* aTextValue,
327  const wxString& aDefaultMeasurementUnit,
328  const wxString& aActualConversion )
329 {
330  XNODE* tNode;
331  wxString str;
332 
333  tNode = FindNode( aNode, wxT( "pt" ) );
334 
335  if( tNode )
336  SetPosition( tNode->GetNodeContent(),
337  aDefaultMeasurementUnit,
338  &aTextValue->textPositionX,
339  &aTextValue->textPositionY,
340  aActualConversion );
341 
342  tNode = FindNode( aNode, wxT( "rotation" ) );
343 
344  if( tNode )
345  {
346  str = tNode->GetNodeContent();
347  str.Trim( false );
348  aTextValue->textRotation = StrToInt1Units( str );
349  }
350  else
351  {
352  aTextValue->textRotation = 0;
353  }
354 
355  str = FindNodeGetContent( aNode, wxT( "isVisible" ) );
356 
357  if( str == wxT( "True" ) )
358  aTextValue->textIsVisible = 1;
359  else
360  aTextValue->textIsVisible = 0;
361 
362  str = FindNodeGetContent( aNode, wxT( "justify" ) );
363  aTextValue->justify = GetJustifyIdentificator( str );
364 
365  str = FindNodeGetContent( aNode, wxT( "isFlipped" ) );
366 
367  if( str == wxT( "True" ) )
368  aTextValue->mirror = 1;
369  else
370  aTextValue->mirror = 0;
371 
372  tNode = FindNode( aNode, wxT( "textStyleRef" ) );
373 
374  if( tNode )
375  SetFontProperty( tNode, aTextValue, aDefaultMeasurementUnit, aActualConversion );
376 }
377 
378 
379 void SetFontProperty( XNODE* aNode,
380  TTEXTVALUE* aTextValue,
381  const wxString& aDefaultMeasurementUnit,
382  const wxString& aActualConversion )
383 {
384  wxString n, propValue;
385 
386  aNode->GetAttribute( wxT( "Name" ), &n );
387 
388  while( aNode->GetName() != wxT( "www.lura.sk" ) )
389  aNode = aNode->GetParent();
390 
391  aNode = FindNode( aNode, wxT( "library" ) );
392  if( aNode )
393  aNode = FindNode( aNode, wxT( "textStyleDef" ) );
394 
395  while( aNode )
396  {
397  aNode->GetAttribute( wxT( "Name" ), &propValue );
398  propValue.Trim( false );
399  propValue.Trim( true );
400 
401  if( propValue == n )
402  break;
403 
404  aNode = aNode->GetNext();
405  }
406 
407  if( aNode )
408  {
409  wxString fontType;
410 
411  propValue = FindNodeGetContent( aNode, wxT( "textStyleDisplayTType" ) );
412  aTextValue->isTrueType = ( propValue == wxT( "True" ) );
413 
414  aNode = FindNode( aNode, wxT( "font" ) );
415  fontType = FindNodeGetContent( aNode, wxT( "fontType" ) );
416  if( ( aTextValue->isTrueType && ( fontType != wxT( "TrueType" ) ) ) ||
417  ( !aTextValue->isTrueType && ( fontType != wxT( "Stroke" ) ) ) )
418  aNode = aNode->GetNext();
419 
420  if( aNode )
421  {
422  if( aTextValue->isTrueType )
423  {
424  propValue = FindNodeGetContent( aNode, wxT( "fontItalic" ) );
425  aTextValue->isItalic = ( propValue == wxT( "True" ) );
426 
427  propValue = FindNodeGetContent( aNode, wxT( "fontWeight" ) );
428  if( propValue != wxEmptyString )
429  {
430  long fontWeight;
431 
432  propValue.ToLong( &fontWeight );
433  aTextValue->isBold = ( fontWeight >= TRUETYPE_BOLD_MIN_WEIGHT );
434  }
435  }
436 
437  XNODE* lNode;
438 
439  lNode = FindNode( aNode, wxT( "fontHeight" ) );
440  if( lNode )
441  SetHeight( lNode->GetNodeContent(), aDefaultMeasurementUnit,
442  &aTextValue->textHeight, aActualConversion );
443 
444  if( aTextValue->isTrueType )
445  {
446  aTextValue->textstrokeWidth = TRUETYPE_THICK_PER_HEIGHT * aTextValue->textHeight;
447  if( aTextValue->isBold )
449  }
450  else
451  {
452  lNode = FindNode( aNode, wxT( "strokeWidth" ) );
453  if( lNode )
454  SetWidth( lNode->GetNodeContent(), aDefaultMeasurementUnit,
455  &aTextValue->textstrokeWidth, aActualConversion );
456  }
457  }
458  }
459 }
460 
461 
462 void SetTextJustify( EDA_TEXT* aText, TTEXT_JUSTIFY aJustify )
463 {
464  switch( aJustify )
465  {
466  case LowerLeft:
469  break;
470  case LowerCenter:
473  break;
474  case LowerRight:
477  break;
478  case UpperLeft:
481  break;
482  case UpperCenter:
485  break;
486  case UpperRight:
489  break;
490  case Left:
493  break;
494  case Center:
497  break;
498  case Right:
501  break;
502  }
503 }
504 
505 
507 {
508  return KiROUND( (double) aText->text.Len() *
509  (double) aText->textHeight * TEXT_WIDTH_TO_SIZE_AVERAGE );
510 }
511 
512 
514 {
515  int cm = aValue->mirror ? -1 : 1;
516  int cl = KiROUND( (double) CalculateTextLengthSize( aValue ) / 2.0 );
517  int ch = KiROUND( (double) aValue->textHeight / 2.0 );
518  int posX = 0;
519  int posY = 0;
520 
521  if( aValue->justify == LowerLeft ||
522  aValue->justify == Left ||
523  aValue->justify == UpperLeft )
524  posX += cl * cm;
525  else if( aValue->justify == LowerRight ||
526  aValue->justify == Right ||
527  aValue->justify == UpperRight )
528  posX -= cl * cm;
529 
530  if( aValue->justify == LowerLeft ||
531  aValue->justify == LowerCenter ||
532  aValue->justify == LowerRight )
533  posY -= ch;
534  else if( aValue->justify == UpperLeft ||
535  aValue->justify == UpperCenter ||
536  aValue->justify == UpperRight )
537  posY += ch;
538 
539  RotatePoint( &posX, &posY, aValue->textRotation );
540 
541  aValue->correctedPositionX = aValue->textPositionX + posX;
542  aValue->correctedPositionY = aValue->textPositionY + posY;
543 }
544 
545 
546 void SetTextSizeFromStrokeFontHeight( EDA_TEXT* aText, int aTextHeight )
547 {
548  aText->SetTextSize( wxSize( KiROUND( aTextHeight * STROKE_WIDTH_TO_SIZE ),
549  KiROUND( aTextHeight * STROKE_HEIGHT_TO_SIZE ) ) );
550 }
551 
552 
553 void SetTextSizeFromTrueTypeFontHeight( EDA_TEXT* aText, int aTextHeight )
554 {
555  aText->SetTextSize( wxSize( KiROUND( aTextHeight * TRUETYPE_WIDTH_TO_SIZE ),
556  KiROUND( aTextHeight * TRUETYPE_HEIGHT_TO_SIZE ) ) );
557 }
558 
559 
560 XNODE* FindNode( XNODE* aChild, const wxString& aTag )
561 {
562  aChild = aChild->GetChildren();
563 
564  while( aChild )
565  {
566  if( aChild->GetName() == aTag )
567  return aChild;
568 
569  aChild = aChild->GetNext();
570  }
571 
572  return NULL;
573 }
574 
575 wxString FindNodeGetContent( XNODE* aChild, const wxString& aTag )
576 {
577  wxString str = wxEmptyString;
578 
579  aChild = FindNode( aChild, aTag );
580 
581  if( aChild )
582  {
583  str = aChild->GetNodeContent();
584  str.Trim( false );
585  str.Trim( true );
586  }
587 
588  return str;
589 }
590 
591 void InitTTextValue( TTEXTVALUE* aTextValue )
592 {
593  aTextValue->text = wxEmptyString;
594  aTextValue->textPositionX = 0;
595  aTextValue->textPositionY = 0;
596  aTextValue->textRotation = 0;
597  aTextValue->textHeight = 0;
598  aTextValue->textstrokeWidth = 0;
599  aTextValue->textIsVisible = 0;
600  aTextValue->mirror = 0;
601  aTextValue->textUnit = 0;
602  aTextValue->correctedPositionX = 0;
603  aTextValue->correctedPositionY = 0;
604  aTextValue->justify = LowerLeft;
605  aTextValue->isBold = false;
606  aTextValue->isItalic = false;
607  aTextValue->isTrueType = false;
608 }
609 
610 } // namespace PCAD2KICAD
const long TRUETYPE_BOLD_MIN_WEIGHT
wxString GetWord(wxString *aStr)
void SetFontProperty(XNODE *aNode, TTEXTVALUE *aTextValue, const wxString &aDefaultMeasurementUnit, const wxString &aActualConversion)
void SetPosition(wxString aStr, const wxString &aDefaultMeasurementUnit, int *aX, int *aY, const wxString &aActualConversion)
double StrToDoublePrecisionUnits(const wxString &aStr, char aAxe, const wxString &aActualConversion)
const double TRUETYPE_BOLD_THICK_MUL
const double TEXT_WIDTH_TO_SIZE_AVERAGE
const double TRUETYPE_THICK_PER_HEIGHT
wxString GetAndCutWordWithMeasureUnits(wxString *aStr, const wxString &aDefaultMeasurementUnit)
void SetWidth(wxString aStr, const wxString &aDefaultMeasurementUnit, int *aWidth, const wxString &aActualConversion)
void SetHeight(wxString aStr, const wxString &aDefaultMeasurementUnit, int *aHeight, const wxString &aActualConversion)
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:222
const double TRUETYPE_WIDTH_TO_SIZE
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:208
void SetTextSizeFromTrueTypeFontHeight(EDA_TEXT *aText, int aTextHeight)
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:109
wxString ValidateName(wxString aName)
int CalculateTextLengthSize(TTEXTVALUE *aText)
XNODE * GetChildren() const
Definition: xnode.h:63
#define NULL
const double STROKE_WIDTH_TO_SIZE
XNODE * GetParent() const
Definition: xnode.h:73
wxString FindNodeGetContent(XNODE *aChild, const wxString &aTag)
void SetVertJustify(EDA_TEXT_VJUSTIFY_T aType)
Definition: eda_text.h:187
void SetTextParameters(XNODE *aNode, TTEXTVALUE *aTextValue, const wxString &aDefaultMeasurementUnit, const wxString &aActualConversion)
wxString ValidateReference(wxString aRef)
const double STROKE_HEIGHT_TO_SIZE
void SetDoublePrecisionPosition(wxString aStr, const wxString &aDefaultMeasurementUnit, double *aX, double *aY, const wxString &aActualConversion)
int StrToIntUnits(const wxString &aStr, char aAxe, const wxString &aActualConversion)
XNODE holds an XML or S-expression element.
Definition: xnode.h:43
void SetTextJustify(EDA_TEXT *aText, TTEXT_JUSTIFY aJustify)
const double TRUETYPE_HEIGHT_TO_SIZE
XNODE * GetNext() const
Definition: xnode.h:68
void SetHorizJustify(EDA_TEXT_HJUSTIFY_T aType)
Definition: eda_text.h:186
void CorrectTextPosition(TTEXTVALUE *aValue)
TTEXT_JUSTIFY GetJustifyIdentificator(const wxString &aJustify)
XNODE * FindNode(XNODE *aChild, const wxString &aTag)
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 InitTTextValue(TTEXTVALUE *aTextValue)
The common library.
void SetTextSizeFromStrokeFontHeight(EDA_TEXT *aText, int aTextHeight)
XNODE * FindPinMap(XNODE *aNode)
int StrToInt1Units(const wxString &aStr)