KiCad PCB EDA Suite
ws_data_model_io.cpp
Go to the documentation of this file.
1 
7 /*
8  * This program source code file is part of KiCad, a free EDA CAD application.
9  *
10  * Copyright (C) 2013-2016 CERN
11  * Copyright (C) 2019 Kicad Developers, see AUTHORS.txt for contributors.
12  *
13  * @author Jean-Pierre Charras, jp.charras at wanadoo.fr
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, you may find one here:
27  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
28  * or you may search the http://www.gnu.org website for the version 2 license,
29  * or you may write to the Free Software Foundation, Inc.,
30  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
31  */
32 
33 #include <fctsys.h>
34 #include <base_struct.h>
35 #include <ws_painter.h>
36 #include <ws_draw_item.h>
37 #include <ws_data_model.h>
38 #include <math/vector2d.h>
39 #include <page_layout/page_layout_reader_lexer.h>
40 #include <macros.h>
41 #include <convert_to_biu.h>
42 
43 
44 using namespace TB_READER_T;
45 
46 #define double2Str Double2Str
47 
48 // A helper function to write tokens:
49 static const char* getTokenName( T aTok )
50 {
51  return PAGE_LAYOUT_READER_LEXER::TokenName( aTok );
52 }
53 
54 // A basic helper class to write a page layout description
55 // Not used alone, a file writer or a string writer should be
56 // derived to use it
57 // Therefore the constructor is protected
59 {
60 protected:
62 
63  WS_DATA_MODEL_IO() { m_out = NULL; }
64  virtual ~WS_DATA_MODEL_IO() {}
65 
66 public:
67  void Format( WS_DATA_MODEL* aModel ) const;
68 
69  void Format( WS_DATA_MODEL* aModel, WS_DATA_ITEM* aItem, int aNestLevel ) const;
70 
71 private:
72  void format( WS_DATA_ITEM_TEXT* aItem, int aNestLevel ) const;
73  void format( WS_DATA_MODEL* aModel, WS_DATA_ITEM* aItem, int aNestLevel ) const;
74  void format( WS_DATA_ITEM_POLYGONS* aItem, int aNestLevel )
75  const;
76  void format( WS_DATA_ITEM_BITMAP* aItem, int aNestLevel ) const;
77  void formatCoordinate( const char * aToken, POINT_COORD & aCoord ) const;
78  void formatRepeatParameters( WS_DATA_ITEM* aItem ) const;
79  void formatOptions( WS_DATA_ITEM* aItem ) const;
80 };
81 
82 
83 // A helper class to write a page layout description to a file
85 {
87 
88 public:
89  WS_DATA_MODEL_FILEIO( const wxString& aFilename ):
91  {
92  try
93  {
94  m_fileout = new FILE_OUTPUTFORMATTER( aFilename );
95  m_out = m_fileout;
96  }
97  catch( const IO_ERROR& ioe )
98  {
99  wxMessageBox( ioe.What(), _( "Error writing page layout design file" ) );
100  }
101  }
102 
104  {
105  delete m_fileout;
106  }
107 };
108 
109 
110 // A helper class to write a page layout description to a string
112 {
114  wxString & m_output;
115 
116 public:
117  WS_DATA_MODEL_STRINGIO( wxString& aOutputString ):
118  WS_DATA_MODEL_IO(), m_output( aOutputString )
119  {
120  try
121  {
122  m_writer = new STRING_FORMATTER();
123  m_out = m_writer;
124  }
125  catch( const IO_ERROR& ioe )
126  {
127  wxMessageBox( ioe.What(), _( "Error writing page layout design file" ) );
128  }
129  }
130 
132  {
133  m_output = FROM_UTF8( m_writer->GetString().c_str() );
134  delete m_writer;
135  }
136 };
137 
138 
139 /*
140  * Save the description in a file
141  */
142 void WS_DATA_MODEL::Save( const wxString& aFullFileName )
143 {
144  WS_DATA_MODEL_FILEIO writer( aFullFileName );
145  writer.Format( this );
146 }
147 
148 
149 /* Save the description in a buffer
150  */
151 void WS_DATA_MODEL::SaveInString( wxString& aOutputString )
152 {
153  WS_DATA_MODEL_STRINGIO writer( aOutputString );
154  writer.Format( this );
155 }
156 
157 
158 void WS_DATA_MODEL::SaveInString( std::vector<WS_DATA_ITEM*> aItemsList, wxString& aOutputString )
159 {
160  WS_DATA_MODEL_STRINGIO writer( aOutputString );
161 
162  LOCALE_IO toggle; // switch on/off the locale "C" notation
163 
164  for( WS_DATA_ITEM* item : aItemsList )
165  writer.Format( this, item, 0 );
166 }
167 
168 
169 void WS_DATA_MODEL_IO::Format( WS_DATA_MODEL* aModel, WS_DATA_ITEM* aItem, int aNestLevel ) const
170 {
171  switch( aItem->GetType() )
172  {
174  format( (WS_DATA_ITEM_TEXT*) aItem, aNestLevel );
175  break;
176 
179  format( aModel, aItem, aNestLevel );
180  break;
181 
183  format( (WS_DATA_ITEM_POLYGONS*) aItem, aNestLevel );
184  break;
185 
187  format( (WS_DATA_ITEM_BITMAP*) aItem, aNestLevel );
188  break;
189 
190  default:
191  wxFAIL_MSG( wxT( "Cannot format item" ) );
192  }
193 }
194 
195 
196 void WS_DATA_MODEL_IO::Format( WS_DATA_MODEL* aPageLayout ) const
197 {
198  LOCALE_IO toggle; // switch on/off the locale "C" notation
199 
200  m_out->Print( 0, "(page_layout\n" );
201 
202  // Setup
203  int nestLevel = 1;
204  // Write default values:
205  m_out->Print( nestLevel, "(%s ", getTokenName( T_setup ) );
206  m_out->Print( 0, "(textsize %s %s)",
207  double2Str( aPageLayout->m_DefaultTextSize.x ).c_str(),
208  double2Str( aPageLayout->m_DefaultTextSize.y ).c_str() );
209  m_out->Print( 0, "(linewidth %s)",
210  double2Str( aPageLayout->m_DefaultLineWidth ).c_str() );
211  m_out->Print( 0, "(textlinewidth %s)",
212  double2Str( aPageLayout->m_DefaultTextThickness ).c_str() );
213  m_out->Print( 0, "\n" );
214 
215  // Write margin values
216  m_out->Print( nestLevel, "(%s %s)", getTokenName( T_left_margin ),
217  double2Str( aPageLayout->GetLeftMargin() ).c_str() );
218  m_out->Print( 0, "(%s %s)", getTokenName( T_right_margin ),
219  double2Str( aPageLayout->GetRightMargin() ).c_str() );
220  m_out->Print( 0, "(%s %s)", getTokenName( T_top_margin ),
221  double2Str( aPageLayout->GetTopMargin() ).c_str() );
222  m_out->Print( 0, "(%s %s)", getTokenName( T_bottom_margin ),
223  double2Str( aPageLayout->GetBottomMargin() ).c_str() );
224  m_out->Print( 0, ")\n" );
225 
226  // Save the graphical items on the page layout
227  for( unsigned ii = 0; ii < aPageLayout->GetCount(); ii++ )
228  {
229  WS_DATA_ITEM* item = aPageLayout->GetItem( ii );
230  Format( aPageLayout, item, nestLevel );
231  }
232 
233  m_out->Print( 0, ")\n" );
234 }
235 
236 
237 void WS_DATA_MODEL_IO::format( WS_DATA_ITEM_TEXT* aItem, int aNestLevel ) const
238 {
239  m_out->Print( aNestLevel, "(%s", getTokenName( T_tbtext ) );
240  m_out->Print( 0, " %s", m_out->Quotew( aItem->m_TextBase ).c_str() );
241  m_out->Print( 0, " (%s %s)", getTokenName( T_name ),
242  m_out->Quotew( aItem->m_Name ).c_str() );
243 
244  formatCoordinate( getTokenName( T_pos ), aItem->m_Pos );
245  formatOptions( aItem );
246 
247  if( aItem->m_Orient )
248  m_out->Print( 0, " (%s %s)", getTokenName( T_rotate ),
249  double2Str(aItem->m_Orient ).c_str() );
250 
251  // Write font info, only if it is not the default setup
252  bool write_size = aItem->m_TextSize.x != 0.0 || aItem->m_TextSize.y != 0.0;
253  bool write_thickness = aItem->m_LineWidth != 0.0;
254 
255  if( write_thickness || write_size || aItem->m_Bold || aItem->m_Italic )
256  {
257  m_out->Print( 0, " (%s", getTokenName( T_font ) );
258 
259  if( write_thickness )
260  {
261  m_out->Print( 0, " (%s %s)", getTokenName( T_linewidth ),
262  double2Str(aItem->m_LineWidth ).c_str() );
263  }
264 
265  if( write_size )
266  {
267  m_out->Print( 0, " (%s %s %s)", getTokenName( T_size ),
268  double2Str(aItem->m_TextSize.x ).c_str(),
269  double2Str(aItem->m_TextSize.y ).c_str() );
270  }
271  if( aItem->m_Bold )
272  m_out->Print( 0, " %s", getTokenName( T_bold ) );
273 
274  if( aItem->m_Italic )
275  m_out->Print( 0, " %s", getTokenName( T_italic ) );
276 
277  m_out->Print( 0, ")" );
278  }
279 
280  // Write text justification
281  if( aItem->m_Hjustify != GR_TEXT_HJUSTIFY_LEFT ||
283  {
284  m_out->Print( 0, " (%s", getTokenName( T_justify ) );
285 
286  // Write T_center opt first, because it is
287  // also a center for both m_Hjustify and m_Vjustify
288  if( aItem->m_Hjustify == GR_TEXT_HJUSTIFY_CENTER )
289  m_out->Print( 0, " %s", getTokenName( T_center ) );
290 
291  if( aItem->m_Hjustify == GR_TEXT_HJUSTIFY_RIGHT )
292  m_out->Print( 0, " %s", getTokenName( T_right ) );
293 
294  if( aItem->m_Vjustify == GR_TEXT_VJUSTIFY_TOP )
295  m_out->Print( 0, " %s", getTokenName( T_top ) );
296 
297  if( aItem->m_Vjustify == GR_TEXT_VJUSTIFY_BOTTOM )
298  m_out->Print( 0, " %s", getTokenName( T_bottom ) );
299 
300  m_out->Print( 0, ")" );
301  }
302 
303  // write constraints
304  if( aItem->m_BoundingBoxSize.x )
305  m_out->Print( 0, " (%s %s)", getTokenName( T_maxlen ),
306  double2Str(aItem->m_BoundingBoxSize.x ).c_str() );
307 
308  if( aItem->m_BoundingBoxSize.y )
309  m_out->Print( 0, " (%s %s)", getTokenName( T_maxheight ),
310  double2Str(aItem->m_BoundingBoxSize.y ).c_str() );
311 
312  formatRepeatParameters( aItem );
313 
314  m_out->Print( 0, ")\n" );
315 }
316 
317 void WS_DATA_MODEL_IO::format( WS_DATA_MODEL* aModel, WS_DATA_ITEM* aItem, int aNestLevel ) const
318 {
319  if( aItem->GetType() == WS_DATA_ITEM::WS_RECT )
320  m_out->Print( aNestLevel, "(%s", getTokenName( T_rect ) );
321  else
322  m_out->Print( aNestLevel, "(%s", getTokenName( T_line ) );
323 
324  m_out->Print( 0, " (%s %s)", getTokenName( T_name ),
325  m_out->Quotew( aItem->m_Name ).c_str() );
326 
327  formatCoordinate( getTokenName( T_start ), aItem->m_Pos );
328  formatCoordinate( getTokenName( T_end ), aItem->m_End );
329  formatOptions( aItem );
330 
331  if( aItem->m_LineWidth && aItem->m_LineWidth != aModel->m_DefaultLineWidth )
332  m_out->Print( 0, " (linewidth %s)", double2Str( aItem->m_LineWidth ).c_str() );
333 
334  formatRepeatParameters( aItem );
335 
336  m_out->Print( 0, ")\n" );
337 }
338 
339 
340 void WS_DATA_MODEL_IO::format( WS_DATA_ITEM_POLYGONS* aItem, int aNestLevel ) const
341 {
342  m_out->Print( aNestLevel, "(%s", getTokenName( T_polygon ) );
343  m_out->Print( 0, " (%s %s)", getTokenName( T_name ),
344  m_out->Quotew( aItem->m_Name ).c_str() );
345  formatCoordinate( getTokenName( T_pos ), aItem->m_Pos );
346  formatOptions( aItem );
347 
348  formatRepeatParameters( aItem );
349 
350  if( aItem->m_Orient )
351  m_out->Print( 0, " (%s %s)", getTokenName( T_rotate ),
352  double2Str(aItem->m_Orient ).c_str() );
353 
354  if( aItem->m_LineWidth )
355  m_out->Print( 0, " (linewidth %s)\n", double2Str( aItem->m_LineWidth ).c_str() );
356 
357  // Write polygon corners list
358  for( int kk = 0; kk < aItem->GetPolyCount(); kk++ )
359  {
360  m_out->Print( aNestLevel+1, "(%s", getTokenName( T_pts ) );
361  // Create current polygon corners list
362  unsigned ist = aItem->GetPolyIndexStart( kk );
363  unsigned iend = aItem->GetPolyIndexEnd( kk );
364  int ii = 0;
365 
366  while( ist <= iend )
367  {
368  DPOINT pos = aItem->m_Corners[ist++];
369  int nestLevel = 0;
370 
371  if( ii++ > 4)
372  {
373  m_out->Print( 0, "\n" );
374  nestLevel = aNestLevel+2;
375  ii = 0;
376  }
377 
378  m_out->Print( nestLevel, " (%s %s %s)", getTokenName( T_xy ),
379  double2Str( pos.x ).c_str(),
380  double2Str( pos.y ).c_str() );
381  }
382 
383  m_out->Print( 0, ")\n" );
384  }
385 
386  m_out->Print( aNestLevel, ")\n" );
387 }
388 
389 
390 void WS_DATA_MODEL_IO::format( WS_DATA_ITEM_BITMAP* aItem, int aNestLevel ) const
391 {
392  m_out->Print( aNestLevel, "(%s", getTokenName( T_bitmap ) );
393  m_out->Print( 0, " (%s %s)", getTokenName( T_name ),
394  m_out->Quotew( aItem->m_Name ).c_str() );
395  formatCoordinate( getTokenName( T_pos ), aItem->m_Pos );
396  formatOptions( aItem );
397 
398  m_out->Print( 0, " (%s %s)", getTokenName( T_scale ),
399  double2Str( aItem->m_ImageBitmap->GetScale() ).c_str() );
400 
401  formatRepeatParameters( aItem );
402  m_out->Print( 0,"\n");
403 
404  // Write image in png readable format
405  m_out->Print( aNestLevel, "(%s\n", getTokenName( T_pngdata ) );
406  wxArrayString pngStrings;
407  aItem->m_ImageBitmap->SaveData( pngStrings );
408 
409  for( unsigned ii = 0; ii < pngStrings.GetCount(); ii++ )
410  m_out->Print( aNestLevel+1, "(data \"%s\")\n", TO_UTF8(pngStrings[ii]) );
411 
412  m_out->Print( aNestLevel+1, ")\n" );
413 
414  m_out->Print( aNestLevel, ")\n" );
415 }
416 
417 
418 void WS_DATA_MODEL_IO::formatCoordinate( const char * aToken,
419  POINT_COORD & aCoord ) const
420 {
421  m_out->Print( 0, " (%s %s %s", aToken,
422  double2Str( aCoord.m_Pos.x ).c_str(),
423  double2Str( aCoord.m_Pos.y ).c_str() );
424 
425  switch( aCoord.m_Anchor )
426  {
427  case RB_CORNER: break;
428  case LT_CORNER: m_out->Print( 0, " %s", getTokenName( T_ltcorner ) ); break;
429  case LB_CORNER: m_out->Print( 0, " %s", getTokenName( T_lbcorner ) ); break;
430  case RT_CORNER: m_out->Print( 0, " %s", getTokenName( T_rtcorner ) ); break;
431  }
432 
433  m_out->Print( 0, ")" );
434 }
435 
436 
438 {
439  if( aItem->m_RepeatCount <= 1 )
440  return;
441 
442  m_out->Print( 0, " (repeat %d)", aItem->m_RepeatCount );
443 
444  if( aItem->m_IncrementVector.x )
445  m_out->Print( 0, " (incrx %s)", double2Str(aItem-> m_IncrementVector.x ).c_str() );
446 
447  if( aItem->m_IncrementVector.y )
448  m_out->Print( 0, " (incry %s)", double2Str( aItem->m_IncrementVector.y ).c_str() );
449 
450  if( aItem->m_IncrementLabel != 1 && aItem->GetType() == WS_DATA_ITEM::WS_TEXT )
451  m_out->Print( 0, " (incrlabel %d)", aItem->m_IncrementLabel );
452 }
453 
454 
456 {
457  if( aItem->GetPage1Option() == FIRST_PAGE_ONLY )
458  m_out->Print( 0, " (%s %s)", getTokenName( T_option ), getTokenName(T_page1only ) );
459  else if( aItem->GetPage1Option() == SUBSEQUENT_PAGES )
460  m_out->Print( 0, " (%s %s)", getTokenName( T_option ), getTokenName( T_notonpage1 ) );
461 }
wxString m_Name
Definition: ws_data_item.h:111
WS_DATA_MODEL handles the graphic items list to draw/plot the frame and title block.
Definition: ws_data_model.h:39
WS_DATA_MODEL_FILEIO(const wxString &aFilename)
double GetTopMargin()
Definition: ws_data_model.h:90
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:62
Work sheet structure type definitions.
Definition: ws_data_item.h:93
bool SaveData(FILE *aFile) const
writes the bitmap data to aFile The format is png, in Hexadecimal form: If the hexadecimal data is co...
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: common.h:154
std::vector< DPOINT > m_Corners
Definition: ws_data_item.h:219
FILE_OUTPUTFORMATTER * m_fileout
double GetScale() const
Definition: bitmap_base.h:92
WS_DATA_ITEM * GetItem(unsigned aIdx) const
BITMAP_BASE * m_ImageBitmap
Definition: ws_data_item.h:366
Class OUTPUTFORMATTER is an important interface (abstract class) used to output 8 bit text in a conve...
Definition: richio.h:327
POINT_COORD m_End
Definition: ws_data_item.h:116
unsigned GetPolyIndexStart(unsigned aContour) const
Definition: ws_data_item.h:260
void Format(WS_DATA_MODEL *aModel) const
DSIZE m_DefaultTextSize
Definition: ws_data_model.h:56
unsigned GetPolyIndexEnd(unsigned aContour) const
Definition: ws_data_item.h:272
EDA_TEXT_HJUSTIFY_T m_Hjustify
Definition: ws_data_item.h:304
double GetBottomMargin()
Definition: ws_data_model.h:93
This file contains miscellaneous commonly used macros and functions.
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:48
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
DPOINT m_Pos
Definition: ws_data_item.h:71
double GetRightMargin()
Definition: ws_data_model.h:87
static const char * getTokenName(T aTok)
EDA_TEXT_VJUSTIFY_T m_Vjustify
Definition: ws_data_item.h:305
const std::string & GetString()
Definition: richio.h:475
A coordinate point The position is always relative to the corner anchor Note the coordinate is from t...
Definition: ws_data_item.h:68
#define _(s)
DPOINT m_IncrementVector
Definition: ws_data_item.h:119
void formatCoordinate(const char *aToken, POINT_COORD &aCoord) const
WS_ITEM_TYPE GetType() const
Definition: ws_data_item.h:147
double m_LineWidth
Definition: ws_data_item.h:117
void Save(const wxString &aFullFileName)
Save the description in a file.
void formatOptions(WS_DATA_ITEM *aItem) const
int GetPolyCount() const
Definition: ws_data_item.h:254
void format(WS_DATA_ITEM_TEXT *aItem, int aNestLevel) const
virtual ~WS_DATA_MODEL_IO()
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
double m_DefaultLineWidth
Definition: ws_data_model.h:55
double GetLeftMargin()
Definition: ws_data_model.h:84
void formatRepeatParameters(WS_DATA_ITEM *aItem) const
STRING_FORMATTER * m_writer
WS_DATA_MODEL_STRINGIO(wxString &aOutputString)
double m_DefaultTextThickness
Definition: ws_data_model.h:57
Class FILE_OUTPUTFORMATTER may be used for text file output.
Definition: richio.h:492
int m_IncrementLabel
Definition: ws_data_item.h:121
#define double2Str
POINT_COORD m_Pos
Definition: ws_data_item.h:115
Class STRING_FORMATTER implements OUTPUTFORMATTER to a memory buffer.
Definition: richio.h:445
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76
PAGE_OPTION GetPage1Option() const
Definition: ws_data_item.h:153
void SaveInString(wxString &aOutputString)
Save the description in a buffer.
unsigned GetCount() const
OUTPUTFORMATTER * m_out