1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 1992-2013 Jean-Pierre Charras <jp.charras at wanadoo.fr>.
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <charconv>
#include <fmt/format.h>
#include <wx/base64.h>
#include <wx/ffile.h>
#include <wx/log.h>
#include <eda_item.h>
#include <locale_io.h>
#include <string_utils.h>
#include <drawing_sheet/ds_data_item.h>
#include <drawing_sheet/ds_data_model.h>
#include <drawing_sheet/ds_painter.h>
#include <drawing_sheet/drawing_sheet_lexer.h>
#include <drawing_sheet/ds_file_versions.h>
#include <font/font.h>
using namespace DRAWINGSHEET_T;
/**
* Hold data and functions pertinent to parsing a S-expression file for a #DS_DATA_MODEL.
*/
class DRAWING_SHEET_PARSER : public DRAWING_SHEET_LEXER
{
public:
DRAWING_SHEET_PARSER( const char* aLine, const wxString& aSource );
void Parse( DS_DATA_MODEL* aLayout );
private:
int m_requiredVersion;
wxString m_generatorVersion;
/**
* Parse the data specified at the very beginning of the file, like version and the
* application used to create this file.
*/
void parseHeader( T aHeaderType );
/**
* Parse an integer.
*/
int parseInt();
/**
* Parse an integer and constrain it between two values.
*
* @param aMin is the smallest return value.
* @param aMax is the largest return value.
* @return int - the parsed integer.
*/
int parseInt( int aMin, int aMax );
/**
* Parse a double.
*
* @return double - the parsed double.
*/
double parseDouble();
void parseSetup( DS_DATA_MODEL* aLayout );
/**
* Parse a graphic item starting by "(line" or "(rect" and read parameters.
*/
void parseGraphic( DS_DATA_ITEM * aItem );
/**
* Parse a text item starting by "(tbtext" and read parameters.
*/
void parseText( DS_DATA_ITEM_TEXT * aItem );
/**
* Parse a polygon item starting by "( polygon" and read parameters.
* the list of corners included in this description is read by parsePolyOutline.
*/
void parsePolygon( DS_DATA_ITEM_POLYGONS * aItem );
/**
* Parse a list of corners starting by "( pts" and read coordinates.
*/
void parsePolyOutline( DS_DATA_ITEM_POLYGONS * aItem );
/**
* Parse a bitmap item starting by "( bitmap" and read parameters.
*/
void parseBitmap( DS_DATA_ITEM_BITMAP * aItem );
void parseCoordinate( POINT_COORD& aCoord);
void readOption( DS_DATA_ITEM * aItem );
void readPngdata( DS_DATA_ITEM_BITMAP * aItem );
};
DRAWING_SHEET_PARSER::DRAWING_SHEET_PARSER( const char* aLine,
const wxString& aSource ) :
DRAWING_SHEET_LEXER( aLine, aSource ),
m_requiredVersion( 0 )
{
}
wxString convertLegacyVariableRefs( const wxString& aTextbase )
{
wxString msg;
/*
* Legacy formats
* %% = replaced by %
* %K = Kicad version
* %Z = paper format name (A4, USLetter)
* %Y = company name
* %D = date
* %R = revision
* %S = sheet number
* %N = number of sheets
* %L = layer name
* %Cx = comment (x = 0 to 9 to identify the comment)
* %F = filename
* %P = sheet path (sheet full name)
* %T = title
*/
for( unsigned ii = 0; ii < aTextbase.Len(); ii++ )
{
if( aTextbase[ii] != '%' )
{
msg << aTextbase[ii];
continue;
}
if( ++ii >= aTextbase.Len() )
break;
wxChar format = aTextbase[ii];
switch( format )
{
case '%': msg += '%'; break;
case 'D': msg += wxT( "${ISSUE_DATE}" ); break;
case 'R': msg += wxT( "${REVISION}" ); break;
case 'K': msg += wxT( "${KICAD_VERSION}" ); break;
case 'Z': msg += wxT( "${PAPER}" ); break;
case 'S': msg += wxT( "${#}" ); break;
case 'N': msg += wxT( "${##}" ); break;
case 'F': msg += wxT( "${FILENAME}" ); break;
case 'L': msg += wxT( "${LAYER}" ); break;
case 'P': msg += wxT( "${SHEETPATH}" ); break;
case 'Y': msg += wxT( "${COMPANY}" ); break;
case 'T': msg += wxT( "${TITLE}" ); break;
case 'C':
format = aTextbase[++ii];
switch( format )
{
case '0': msg += wxT( "${COMMENT1}" ); break;
case '1': msg += wxT( "${COMMENT2}" ); break;
case '2': msg += wxT( "${COMMENT3}" ); break;
case '3': msg += wxT( "${COMMENT4}" ); break;
case '4': msg += wxT( "${COMMENT5}" ); break;
case '5': msg += wxT( "${COMMENT6}" ); break;
case '6': msg += wxT( "${COMMENT7}" ); break;
case '7': msg += wxT( "${COMMENT8}" ); break;
case '8': msg += wxT( "${COMMENT9}" ); break;
}
break;
default:
break;
}
}
return msg;
}
void DRAWING_SHEET_PARSER::Parse( DS_DATA_MODEL* aLayout )
{
DS_DATA_ITEM* item;
LOCALE_IO toggle;
NeedLEFT();
T token = NextTok();
parseHeader( token );
aLayout->SetFileFormatVersionAtLoad( m_requiredVersion );
auto checkVersion =
[&]()
{
if( m_requiredVersion > SEXPR_WORKSHEET_FILE_VERSION )
{
throw FUTURE_FORMAT_ERROR( fmt::format( "{}", m_requiredVersion ),
m_generatorVersion );
}
};
for( token = NextTok(); token != T_RIGHT && token != EOF; token = NextTok() )
{
if( token == T_LEFT )
token = NextTok();
switch( token )
{
case T_generator:
// (generator "genname"); we don't care about it at the moment.
NeedSYMBOL();
NeedRIGHT();
break;
case T_generator_version:
NextTok();
m_generatorVersion = FromUTF8();
NeedRIGHT();
break;
case T_setup: // Defines default values for graphic items
// Check the version here, because the generator and generator_version (if available)
// will have been parsed by now given the order the formatter writes them in
checkVersion();
parseSetup( aLayout );
break;
case T_line:
item = new DS_DATA_ITEM( DS_DATA_ITEM::DS_SEGMENT );
parseGraphic( item );
aLayout->Append( item );
break;
case T_rect:
item = new DS_DATA_ITEM( DS_DATA_ITEM::DS_RECT );
parseGraphic( item );
aLayout->Append( item );
break;
case T_polygon:
item = new DS_DATA_ITEM_POLYGONS();
parsePolygon( (DS_DATA_ITEM_POLYGONS*) item );
aLayout->Append( item );
break;
case T_bitmap:
item = new DS_DATA_ITEM_BITMAP( NULL );
parseBitmap( (DS_DATA_ITEM_BITMAP*) item );
// Drop invalid bitmaps
if( static_cast<DS_DATA_ITEM_BITMAP*>( item )->m_ImageBitmap->GetOriginalImageData() )
{
aLayout->Append( item );
}
else
{
delete static_cast<DS_DATA_ITEM_BITMAP*>( item )->m_ImageBitmap;
delete item;
}
break;
case T_tbtext:
NeedSYMBOLorNUMBER();
item = new DS_DATA_ITEM_TEXT( convertLegacyVariableRefs( FromUTF8() ) );
parseText( (DS_DATA_ITEM_TEXT*) item );
aLayout->Append( item );
break;
default:
Unexpected( CurText() );
break;
}
}
}
void DRAWING_SHEET_PARSER::parseHeader( T aHeaderType )
{
// The older files had no versioning and their first token after the initial left parenthesis
// was a `page_layout` or `drawing_sheet` token. The newer files have versions and have a
// `kicad_wks` token instead.
if( aHeaderType == T_kicad_wks || aHeaderType == T_drawing_sheet )
{
NeedLEFT();
T tok = NextTok();
if( tok == T_version )
{
m_requiredVersion = parseInt();
NeedRIGHT();
}
else
{
Expecting( T_version );
}
}
else
{
// We assign version 0 to files that were created before there was any versioning of
// worksheets. The below line is not strictly necessary, as `m_requiredVersion` is already
// initialized to 0 in the constructor.
m_requiredVersion = 0;
}
}
void DRAWING_SHEET_PARSER::parseSetup( DS_DATA_MODEL* aLayout )
{
for( T token = NextTok(); token != T_RIGHT && token != EOF; token = NextTok() )
{
switch( token )
{
case T_LEFT:
break;
case T_linewidth:
aLayout->m_DefaultLineWidth = parseDouble();
NeedRIGHT();
break;
case T_textsize:
aLayout->m_DefaultTextSize.x = parseDouble();
aLayout->m_DefaultTextSize.y = parseDouble();
NeedRIGHT();
break;
case T_textlinewidth:
aLayout->m_DefaultTextThickness = parseDouble();
NeedRIGHT();
break;
case T_left_margin:
aLayout->SetLeftMargin( parseDouble() );
NeedRIGHT();
break;
case T_right_margin:
aLayout->SetRightMargin( parseDouble() );
NeedRIGHT();
break;
case T_top_margin:
aLayout->SetTopMargin( parseDouble() );
NeedRIGHT();
break;
case T_bottom_margin:
aLayout->SetBottomMargin( parseDouble() );
NeedRIGHT();
break;
default:
Unexpected( CurText() );
break;
}
}
// The file is well-formed. If it has no further items, then that's the way the
// user wants it.
aLayout->AllowVoidList( true );
}
void DRAWING_SHEET_PARSER::parsePolygon( DS_DATA_ITEM_POLYGONS * aItem )
{
for( T token = NextTok(); token != T_RIGHT && token != EOF; token = NextTok() )
{
if( token == T_LEFT )
token = NextTok();
switch( token )
{
case T_comment:
NeedSYMBOLorNUMBER();
aItem->m_Info = FromUTF8();
NeedRIGHT();
break;
case T_pos:
parseCoordinate( aItem->m_Pos );
break;
case T_name:
NeedSYMBOLorNUMBER();
aItem->m_Name = FromUTF8();
NeedRIGHT();
break;
case T_option:
readOption( aItem );
break;
case T_pts:
parsePolyOutline( aItem );
aItem->CloseContour();
break;
case T_rotate:
aItem->m_Orient = EDA_ANGLE( parseDouble(), DEGREES_T );
NeedRIGHT();
break;
case T_repeat:
aItem->m_RepeatCount = parseInt( 1, 100 );
NeedRIGHT();
break;
case T_incrx:
aItem->m_IncrementVector.x = parseDouble();
NeedRIGHT();
break;
case T_incry:
aItem->m_IncrementVector.y = parseDouble();
NeedRIGHT();
break;
case T_linewidth:
aItem->m_LineWidth = parseDouble();
NeedRIGHT();
break;
default:
Unexpected( CurText() );
break;
}
}
aItem->SetBoundingBox();
}
void DRAWING_SHEET_PARSER::parsePolyOutline( DS_DATA_ITEM_POLYGONS * aItem )
{
VECTOR2D corner;
for( T token = NextTok(); token != T_RIGHT && token != EOF; token = NextTok() )
{
if( token == T_LEFT )
token = NextTok();
switch( token )
{
case T_xy:
corner.x = parseDouble();
corner.y = parseDouble();
aItem->AppendCorner( corner );
NeedRIGHT();
break;
default:
Unexpected( CurText() );
break;
}
}
}
void DRAWING_SHEET_PARSER::parseBitmap( DS_DATA_ITEM_BITMAP * aItem )
{
BITMAP_BASE* image = new BITMAP_BASE;
aItem->m_ImageBitmap = image;
for( T token = NextTok(); token != T_RIGHT && token != EOF; token = NextTok() )
{
if( token == T_LEFT )
token = NextTok();
switch( token )
{
case T_name:
NeedSYMBOLorNUMBER();
aItem->m_Name = FromUTF8();
NeedRIGHT();
break;
case T_pos:
parseCoordinate( aItem->m_Pos );
break;
case T_repeat:
aItem->m_RepeatCount = parseInt( 1, 100 );
NeedRIGHT();
break;
case T_incrx:
aItem->m_IncrementVector.x = parseDouble();
NeedRIGHT();
break;
case T_incry:
aItem->m_IncrementVector.y = parseDouble();
NeedRIGHT();
break;
case T_linewidth:
aItem->m_LineWidth = parseDouble();
NeedRIGHT();
break;
case T_scale:
aItem->m_ImageBitmap->SetScale( parseDouble() );
NeedRIGHT();
break;
case T_comment:
NeedSYMBOLorNUMBER();
aItem->m_Info = FromUTF8();
NeedRIGHT();
break;
case T_data:
{
token = NextTok();
wxString data;
// Reserve 512K because most image files are going to be larger than the default
// 1K that wxString reserves.
data.reserve( 1 << 19 );
while( token != T_RIGHT )
{
if( !IsSymbol( token ) )
Expecting( "base64 image data" );
data += FromUTF8();
token = NextTok();
}
wxMemoryBuffer buffer = wxBase64Decode( data );
if( !aItem->m_ImageBitmap->ReadImageFile( buffer ) )
THROW_IO_ERROR( _( "Failed to read image data." ) );
break;
}
case T_pngdata:
readPngdata( aItem );
break;
case T_option:
readOption( aItem );
break;
default:
Unexpected( CurText() );
break;
}
}
}
void DRAWING_SHEET_PARSER::readPngdata( DS_DATA_ITEM_BITMAP * aItem )
{
std::string tmp;
for( T token = NextTok(); token != T_RIGHT && token != EOF; token = NextTok() )
{
if( token == T_LEFT )
token = NextTok();
switch( token )
{
case T_data:
NeedSYMBOLorNUMBER();
tmp += CurStr();
tmp += "\n";
NeedRIGHT();
break;
default:
Unexpected( CurText() );
break;
}
}
tmp += "EndData";
wxString msg;
STRING_LINE_READER str_reader( tmp, wxT("Png kicad_wks data") );
aItem->m_ImageBitmap->LoadLegacyData( str_reader, msg );
}
void DRAWING_SHEET_PARSER::readOption( DS_DATA_ITEM * aItem )
{
for( T token = NextTok(); token != T_RIGHT && token != EOF; token = NextTok() )
{
switch( token )
{
case T_page1only: aItem->SetPage1Option( FIRST_PAGE_ONLY ); break;
case T_notonpage1: aItem->SetPage1Option( SUBSEQUENT_PAGES ); break;
default: Unexpected( CurText() ); break;
}
}
}
void DRAWING_SHEET_PARSER::parseGraphic( DS_DATA_ITEM * aItem )
{
for( T token = NextTok(); token != T_RIGHT && token != EOF; token = NextTok() )
{
if( token == T_LEFT )
{
token = NextTok();
}
else
{
// If another token than T_LEFT is read here, this is an error
// however, due to a old bug in kicad, the token T_end can be found
// without T_LEFT in a very few .wks files (perhaps only one in a demo).
// So this ugly hack disables the error detection.
if( token != T_end )
Unexpected( CurText() );
}
switch( token )
{
case T_comment:
NeedSYMBOLorNUMBER();
aItem->m_Info = FromUTF8();
NeedRIGHT();
break;
case T_option:
readOption( aItem );
break;
case T_name:
NeedSYMBOLorNUMBER();
aItem->m_Name = FromUTF8();
NeedRIGHT();
break;
case T_start:
parseCoordinate( aItem->m_Pos );
break;
case T_end:
parseCoordinate( aItem->m_End );
break;
case T_repeat:
aItem->m_RepeatCount = parseInt( 1, 100 );
NeedRIGHT();
break;
case T_incrx:
aItem->m_IncrementVector.x = parseDouble();
NeedRIGHT();
break;
case T_incry:
aItem->m_IncrementVector.y = parseDouble();
NeedRIGHT();
break;
case T_linewidth:
aItem->m_LineWidth = parseDouble();
NeedRIGHT();
break;
default:
Unexpected( CurText() );
break;
}
}
}
void DRAWING_SHEET_PARSER::parseText( DS_DATA_ITEM_TEXT* aItem )
{
if( m_requiredVersion < 20210606 )
aItem->m_TextBase = ConvertToNewOverbarNotation( aItem->m_TextBase );
for( T token = NextTok(); token != T_RIGHT && token != EOF; token = NextTok() )
{
if( token == T_LEFT )
token = NextTok();
switch( token )
{
case T_comment:
NeedSYMBOLorNUMBER();
aItem->m_Info = FromUTF8();
NeedRIGHT();
break;
case T_option:
readOption( aItem );
break;
case T_name:
NeedSYMBOLorNUMBER();
aItem->m_Name = FromUTF8();
NeedRIGHT();
break;
case T_pos:
parseCoordinate( aItem->m_Pos );
break;
case T_repeat:
aItem->m_RepeatCount = parseInt( 1, 100 );
NeedRIGHT();
break;
case T_incrx:
aItem->m_IncrementVector.x = parseDouble();
NeedRIGHT();
break;
case T_incry:
aItem->m_IncrementVector.y = parseDouble();
NeedRIGHT();
break;
case T_incrlabel:
aItem->m_IncrementLabel = parseInt(INT_MIN, INT_MAX);
NeedRIGHT();
break;
case T_maxlen:
aItem->m_BoundingBoxSize.x = parseDouble();
NeedRIGHT();
break;
case T_maxheight:
aItem->m_BoundingBoxSize.y = parseDouble();
NeedRIGHT();
break;
case T_font:
{
wxString faceName;
aItem->m_TextColor = COLOR4D::UNSPECIFIED;
for( token = NextTok(); token != T_RIGHT && token != EOF; token = NextTok() )
{
switch( token )
{
case T_LEFT:
break;
case T_face:
NeedSYMBOL();
faceName = FromUTF8();
NeedRIGHT();
break;
case T_bold:
aItem->m_Bold = true;
break;
case T_italic:
aItem->m_Italic = true;
break;
case T_size:
aItem->m_TextSize.x = parseDouble();
aItem->m_TextSize.y = parseDouble();
NeedRIGHT();
break;
case T_color:
aItem->m_TextColor.r = parseInt( 0, 255 ) / 255.0;
aItem->m_TextColor.g = parseInt( 0, 255 ) / 255.0;
aItem->m_TextColor.b = parseInt( 0, 255 ) / 255.0;
aItem->m_TextColor.a = std::clamp( parseDouble(), 0.0, 1.0 );
NeedRIGHT();
break;
case T_linewidth:
aItem->m_LineWidth = parseDouble();
NeedRIGHT();
break;
default:
Unexpected( CurText() );
break;
}
}
if( !faceName.IsEmpty() )
aItem->m_Font = KIFONT::FONT::GetFont( faceName, aItem->m_Bold, aItem->m_Italic );
break;
}
case T_justify:
for( token = NextTok(); token != T_RIGHT && token != EOF; token = NextTok() )
{
switch( token )
{
case T_center:
aItem->m_Hjustify = GR_TEXT_H_ALIGN_CENTER;
aItem->m_Vjustify = GR_TEXT_V_ALIGN_CENTER;
break;
case T_left:
aItem->m_Hjustify = GR_TEXT_H_ALIGN_LEFT;
break;
case T_right:
aItem->m_Hjustify = GR_TEXT_H_ALIGN_RIGHT;
break;
case T_top:
aItem->m_Vjustify = GR_TEXT_V_ALIGN_TOP;
break;
case T_bottom:
aItem->m_Vjustify = GR_TEXT_V_ALIGN_BOTTOM;
break;
default:
Unexpected( CurText() );
break;
}
}
break;
case T_rotate:
aItem->m_Orient = parseDouble();
NeedRIGHT();
break;
default:
Unexpected( CurText() );
break;
}
}
}
void DRAWING_SHEET_PARSER::parseCoordinate( POINT_COORD& aCoord)
{
aCoord.m_Pos.x = parseDouble();
aCoord.m_Pos.y = parseDouble();
for( T token = NextTok(); token != T_RIGHT && token != EOF; token = NextTok() )
{
switch( token )
{
case T_ltcorner: aCoord.m_Anchor = LT_CORNER; break;
case T_lbcorner: aCoord.m_Anchor = LB_CORNER; break;
case T_rbcorner: aCoord.m_Anchor = RB_CORNER; break;
case T_rtcorner: aCoord.m_Anchor = RT_CORNER; break;
default: Unexpected( CurText() ); break;
}
}
}
int DRAWING_SHEET_PARSER::parseInt()
{
T token = NextTok();
if( token != T_NUMBER )
Expecting( T_NUMBER );
return atoi( CurText() );
}
int DRAWING_SHEET_PARSER::parseInt( int aMin, int aMax )
{
int val = parseInt();
if( val < aMin )
val = aMin;
else if( val > aMax )
val = aMax;
return val;
}
double DRAWING_SHEET_PARSER::parseDouble()
{
T token = NextTok();
if( token != T_NUMBER )
Expecting( T_NUMBER );
return DSNLEXER::parseDouble();
}
// defaultDrawingSheet is the default drawing sheet using the S expr.
extern const char defaultDrawingSheet[];
void DS_DATA_MODEL::SetDefaultLayout()
{
SetPageLayout( defaultDrawingSheet, false, wxT( "default page" ) );
}
wxString DS_DATA_MODEL::DefaultLayout()
{
return wxString( defaultDrawingSheet );
}
// emptyDrawingSheet is a "empty" drawing sheet using the S expr.
// there is a 0 length line to fool something somewhere.
extern const char emptyDrawingSheet[];
void DS_DATA_MODEL::SetEmptyLayout()
{
SetPageLayout( emptyDrawingSheet, false, wxT( "empty page" ) );
}
wxString DS_DATA_MODEL::EmptyLayout()
{
return wxString( emptyDrawingSheet );
}
void DS_DATA_MODEL::SetPageLayout( const char* aPageLayout, bool Append, const wxString& aSource )
{
if( ! Append )
ClearList();
DRAWING_SHEET_PARSER parser( aPageLayout, wxT( "Sexpr_string" ) );
try
{
parser.Parse( this );
}
catch( ... )
{
// best efforts
}
}
bool DS_DATA_MODEL::LoadDrawingSheet( const wxString& aFullFileName, wxString* aMsg, bool aAppend )
{
if( !aAppend )
{
if( aFullFileName.IsEmpty() )
{
SetDefaultLayout();
return true; // we assume its fine / default init
}
if( !wxFileExists( aFullFileName ) )
{
if( aMsg )
*aMsg = _( "File not found." );
SetDefaultLayout();
return false;
}
}
wxFFile wksFile( aFullFileName, wxS( "rb" ) );
if( ! wksFile.IsOpened() )
{
if( aMsg )
*aMsg = _( "File could not be opened." );
if( !aAppend )
SetDefaultLayout();
return false;
}
size_t filelen = wksFile.Length();
std::unique_ptr<char[]> buffer = std::make_unique<char[]>(filelen+10);
if( wksFile.Read( buffer.get(), filelen ) != filelen )
{
if( aMsg )
*aMsg = _( "Drawing sheet was not fully read." );
return false;
}
else
{
buffer[filelen]=0;
if( ! aAppend )
ClearList();
DRAWING_SHEET_PARSER parser( buffer.get(), aFullFileName );
try
{
parser.Parse( this );
}
catch( const IO_ERROR& ioe )
{
if( aMsg )
*aMsg = ioe.What();
return false;
}
catch( const std::bad_alloc& )
{
if( aMsg )
*aMsg = _( "Ran out of memory." );
return false;
}
}
return true;
}
|