-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathwxwidgets_dev.cpp
More file actions
1894 lines (1737 loc) · 73.4 KB
/
Copy pathwxwidgets_dev.cpp
File metadata and controls
1894 lines (1737 loc) · 73.4 KB
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
// Copyright (C) 2015-2017 Phil Rosenberg
// Copyright (C) 2017-2018 Alan W. Irwin
// Copyright (C) 2005 Werner Smekal, Sjaak Verdoold
// Copyright (C) 2005 Germain Carrera Corraleche
// Copyright (C) 1999 Frank Huebner
//
// This file is part of PLplot.
//
// PLplot is free software; you can redistribute it and/or modify
// it under the terms of the GNU Library General Public License as published
// by the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// PLplot 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 Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public License
// along with PLplot; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
#define DEBUG
#define NEED_PLDEBUG
// Set this to help when debugging wxPLViewer issues. It uses a memory
// map name without random characters and does not execute the viewer,
// allowing the user to execute the viewer in a debugger
//#define WXPLVIEWER_DEBUG
// Headers needed for Rand
#ifdef _WIN32
// This include must occur before any other include of stdlib.h due to
// the #define _CRT_RAND_S
#define _CRT_RAND_S
#include <stdlib.h>
#else
#include <fstream>
#endif
// PLplot headers
#include "plDevs.h"
#include "wxwidgets.h" // includes wx/wx.h
// wxwidgets headers
#include <wx/dir.h>
#include <wx/ustring.h>
// std and driver headers
#include <cmath>
#include <limits>
// Needed for cerr, etc.
#if defined ( WXPLVIEWER_DEBUG ) || defined ( PLPLOT_WX_DEBUG_OUTPUT )
#include <iostream>
#endif
//--------------------------------------------------------------------------
// PlDevice::PlDevice()
//
// Constructor for wxPLDevice
//--------------------------------------------------------------------------
PlDevice::PlDevice()
{
m_prevSymbol = 0;
m_prevBaseFontSize = 0.;
m_prevLevel = 0;
m_prevFci = 0;
m_prevSymbolWidth = 0;
m_prevSymbolHeight = 0;
}
//--------------------------------------------------------------------------
// void wxPLDevice::DrawText( PLStream* pls, EscText* args )
//
// This is the main function which processes the unicode text strings.
// Font size, rotation and color are set, width and height of the
// text string is determined and then the string is drawn to the canvas.
//--------------------------------------------------------------------------
void PlDevice::drawText( PLStream* pls, EscText* args )
{
// Split the text into lines separated by forced linebreak '\n' characters
// inserted by the user.
typedef std::pair< PLUNICODE *, PLUNICODE *> uniIterPair;
PLUNICODE *textEnd = args->unicode_array + args->unicode_array_len;
PLUNICODE lf = PLUNICODE( '\n' );
std::vector< uniIterPair > lines( 1, uniIterPair( args->unicode_array, textEnd ) );
for ( PLUNICODE * uni = args->unicode_array; uni != textEnd; ++uni )
{
if ( *uni == lf )
{
lines.back().second = uni;
lines.push_back( uniIterPair( uni + 1, textEnd ) );
}
}
// Check that we got unicode, warning message and return if not
if ( args->unicode_array_len == 0 )
{
printf( "Non unicode string passed to the wxWidgets driver, ignoring\n" );
return;
}
// Check that unicode string isn't longer then the max we allow
if ( args->unicode_array_len >= 500 )
{
printf( "Sorry, the wxWidgets drivers only handles strings of length < %d\n", 500 );
return;
}
// Calculate the font size (in pt)
// PLplot saves it in mm (bizarre units!)
PLFLT baseFontSize = pls->chrht * PLPLOT_POINTS_PER_INCH / PLPLOT_MM_PER_INCH;
//initialize the text state
PLUNICODE currentFci;
plgfci( ¤tFci );
bool currentUnderlined = false;
//Get the size of each line. Even for left aligned text
//we still need the text height to vertically align text
std::vector<wxCoord> lineWidths( lines.size() );
std::vector<wxCoord> lineHeights( lines.size() );
std::vector<wxCoord> lineDepths( lines.size() );
{
// Get the text length without drawing it. Also, determine
// lineWidths, lineHeights, and lineDepths arrays that are required
// for the actual draw.
wxCoord paraWidth = 0;
wxCoord paraHeight = 0;
PLUNICODE testFci = currentFci;
bool testUnderlined = currentUnderlined = false;
PLFLT identityMatrix[6];
plP_affine_identity( identityMatrix );
for ( size_t i = 0; i < lines.size(); ++i )
{
DrawTextLine( lines[i].first, lines[i].second - lines[i].first, 0, 0, 0, 0, identityMatrix, baseFontSize, false,
testUnderlined, testFci,
0, 0, 0, 0.0, lineWidths[i], lineHeights[i], lineDepths[i] );
paraWidth = MAX( paraWidth, lineWidths[i] );
paraHeight += lineHeights[i] + lineDepths[i];
}
pls->string_length = paraWidth / pls->xpmm;
}
if ( !pls->get_string_length )
{
// Draw the text string if requested by PLplot. The needed lineWidths,
// lineHeights, and lineDepths arrays are determined above.
wxCoord cumSumHeight = 0;
// Plplot doesn't include plot orientation in args->xform, so we must
// rotate the text if needed;
PLFLT textTransform[6];
PLFLT diorot = pls->diorot - 4.0 * floor( pls->diorot / 4.0 ); //put diorot in range 0-4
textTransform[0] = args->xform[0];
textTransform[2] = args->xform[1];
textTransform[1] = args->xform[2];
textTransform[3] = args->xform[3];
textTransform[4] = 0.0;
textTransform[5] = 0.0;
PLFLT diorotTransform[6];
if ( diorot == 0.0 )
{
diorotTransform[0] = 1;
diorotTransform[1] = 0;
diorotTransform[2] = 0;
diorotTransform[3] = 1;
}
else if ( diorot == 1.0 )
{
diorotTransform[0] = 0;
diorotTransform[1] = -1;
diorotTransform[2] = 1;
diorotTransform[3] = 0;
}
else if ( diorot == 2.0 )
{
diorotTransform[0] = -1;
diorotTransform[1] = 0;
diorotTransform[2] = 0;
diorotTransform[3] = -1;
}
else if ( diorot == 3.0 )
{
diorotTransform[0] = 0;
diorotTransform[1] = 1;
diorotTransform[2] = -1;
diorotTransform[3] = 0;
}
else
{
PLFLT angle = diorot * M_PI / 2.0;
PLFLT cosAngle = cos( angle );
PLFLT sinAngle = sin( angle );
diorotTransform[0] = cosAngle;
diorotTransform[1] = -sinAngle;
diorotTransform[2] = sinAngle;
diorotTransform[3] = cosAngle;
}
diorotTransform[4] = 0;
diorotTransform[5] = 0;
PLFLT finalTransform[6];
memcpy( finalTransform, textTransform, sizeof ( PLFLT ) * 6 );
plP_affine_multiply( finalTransform, textTransform, diorotTransform );
std::vector<wxCoord> lineWidths_ignored( lines.size() );
std::vector<wxCoord> lineHeights_ignored( lines.size() );
std::vector<wxCoord> lineDepths_ignored( lines.size() );
for ( size_t i = 0; i < lines.size(); ++i )
{
DrawTextLine( lines[i].first, lines[i].second - lines[i].first,
args->x,
args->y,
-lineWidths[i] * args->just, 0.5 * ( lineHeights[i] ) - cumSumHeight,
finalTransform, baseFontSize, true,
currentUnderlined,
currentFci, pls->curcolor.r, pls->curcolor.g, pls->curcolor.b, pls->curcolor.a, lineWidths_ignored[i],
lineHeights_ignored[i], lineDepths_ignored[i] );
// Ignore the ignored versions even though gdb tells me
// (AWI) they are the same as the unignored versions
// determined above for the DrawText false case (as
// expected from inspection of the DrawTextLine code).
cumSumHeight += lineHeights[i] + lineDepths[i];
}
}
}
// This function will draw a line of text given by ucs4 with ucs4Len
// characters. The ucs4 argument must not contain any newline characters.
// basefontSize is the size of a full size character in points. Pass
// in underlined flag and fci for the beginning of the line. On
// return they will be filled with the values at the end of the line.
// On return textWidth, textHeigth and textDepth will be filled with
// the width, ascender height and descender depth of the text string.
// If drawText is true the text will actually be drawn. If it is
// false the size will be calculated but the text will not be
// rendered.
void PlDevice::DrawTextLine( PLUNICODE* ucs4, int ucs4Len, wxCoord xOrigin, wxCoord yOrigin, wxCoord x, wxCoord y, PLFLT *transform, PLFLT baseFontSize, bool drawText, bool &underlined, PLUNICODE &fci, unsigned char red, unsigned char green, unsigned char blue, PLFLT alpha, wxCoord &textWidth, wxCoord &textHeight, wxCoord &textDepth )
{
PLINT level = 0;
PLFLT oldScale;
PLFLT Scale = 1.;
PLFLT scaledFontSize = baseFontSize;
PLFLT oldOffset;
PLFLT Offset = 0.;
PLFLT yScale;
PLFLT scaledOffset = 0.;
// Factor of 1.2 is an empirical correction to work around a bug
// where the calculated symmetrical subscript and superscript
// offset arguments of DrawTextSection are rendered in that
// routine in an asymmetical way (with subscript levels having a
// differently rendered offset than the corresponding superscript
// level). Of course, fixing this DrawTextSection bug is far
// preferable to this workaround, but I have been unable to find
// that bug in DrawTextSection so I am leaving this ultimate fix
// until later.
PLFLT empiricalSymmetricFactor = 1.2;
wxCoord sectionWidth;
wxCoord sectionHeight;
wxCoord sectionDepth;
// check if we have the same symbol as last time - only do this for single characters
// (e.g., typical plstring use).
if ( !drawText
&& ucs4Len == 1
&& ucs4[0] == m_prevSymbol
&& baseFontSize == m_prevBaseFontSize
&& level == m_prevLevel
&& fci == m_prevFci )
{
textWidth = m_prevSymbolWidth;
textHeight = m_prevSymbolHeight;
textDepth = m_prevSymbolDepth;
return;
}
wxString section;
PLFLT sectionTransform[6];
memcpy( sectionTransform, transform, sizeof ( sectionTransform ) );
// Get PLplot escape character
char plplotEsc;
plgesc( &plplotEsc );
// Reset the size metrics
textWidth = 0;
textHeight = 0;
textDepth = 0;
int i = 0;
while ( i < ucs4Len )
{
if ( ucs4[i] == (PLUNICODE) plplotEsc )
{
// We found an escape character. Move to the next character to see what we need to do next
++i;
if ( ucs4[i] == (PLUNICODE) plplotEsc )
{
// Add the actual escape character to the string
section += wxUString( (wxChar32) ucs4[i] );
}
else
{
// We have a change of state. Output the string so far
DrawTextSection( section, xOrigin, yOrigin, x + textWidth, y + scaledOffset, transform,
scaledFontSize, drawText, underlined, fci, red, green, blue, alpha, yScale, sectionWidth, sectionHeight, sectionDepth );
textWidth += sectionWidth;
textHeight = MAX( textHeight, sectionHeight + scaledOffset );
textDepth = MAX( textDepth, sectionDepth - scaledOffset );
section = wxEmptyString;
// Act on the escape character
if ( ucs4[i] == (PLUNICODE) 'u' )
{
// Superscript escape
// y, textHeight, and textDepth are all scaled
// quantities so any offset-related variable that
// is linearly combined with them such as
// scaledOffset must be scaled as well. Offset is
// always positive so the last factor is to give
// scaledOffset the correct sign depending on
// level.
plP_script_scale( TRUE, &level, &oldScale, &Scale, &oldOffset, &Offset );
scaledFontSize = baseFontSize * Scale;
scaledOffset = yScale * Offset * baseFontSize * ( level > 0 ? 1.0 / empiricalSymmetricFactor : -1.0 * empiricalSymmetricFactor );
}
else if ( ucs4[i] == (PLUNICODE) 'd' )
{
// Subscript escape
// y, textHeight, and textDepth are all scaled
// quantities so any offset-related variable that
// is linearly combined with them such as
// scaledOffset must be scaled as well. Offset is
// always positive so the last factor is to give
// scaledOffset the correct sign depending on
// level.
plP_script_scale( FALSE, &level, &oldScale, &Scale, &oldOffset, &Offset );
scaledFontSize = baseFontSize * Scale;
scaledOffset = yScale * Offset * baseFontSize * ( level > 0 ? 1.0 / empiricalSymmetricFactor : -1.0 * empiricalSymmetricFactor );
}
else if ( ucs4[i] == (PLUNICODE) '-' ) // underline
underlined = !underlined;
else if ( ucs4[i] == (PLUNICODE) '+' ) // overline
{ // not implemented yet
}
}
}
else if ( ucs4[i] >= PL_FCI_MARK )
{
// A font change
// draw string so far
DrawTextSection( section, xOrigin, yOrigin, x + textWidth, y + scaledOffset, transform,
scaledFontSize, drawText, underlined, fci, red, green, blue, alpha, yScale, sectionWidth, sectionHeight, sectionDepth );
textWidth += sectionWidth;
textHeight = MAX( textHeight, sectionHeight + scaledOffset );
textDepth = MAX( textDepth, sectionDepth - scaledOffset );
section = wxEmptyString;
// Get new fci
fci = ucs4[i];
}
else
{
// Just a regular character - add it to the string
section += wxUString( (wxChar32) ucs4[i] );
}
++i;
}
// We have reached the end of the string. Draw the last section.
DrawTextSection( section, xOrigin, yOrigin, x + textWidth, y + scaledOffset, transform,
scaledFontSize, drawText, underlined, fci, red, green, blue, alpha, yScale, sectionWidth, sectionHeight, sectionDepth );
textWidth += sectionWidth;
textHeight = MAX( textHeight, sectionHeight + scaledOffset );
textDepth = MAX( textDepth, sectionDepth - scaledOffset );
// If this was a single character remember its size as it is
// likely to be requested repeatedly (e.g., typical plstring use).
if ( ucs4Len == 1 )
{
m_prevSymbol = ucs4[0];
m_prevBaseFontSize = baseFontSize;
m_prevLevel = level;
m_prevFci = fci;
m_prevSymbolWidth = textWidth;
m_prevSymbolHeight = textHeight;
m_prevSymbolDepth = textDepth;
}
}
//--------------------------------------------------------------------------
// Scaler class
// This class changes the logical scale of a dc on construction and resets
// it to its original value on destruction. It is ideal for making temporary
// changes to the scale and guaranteeing that the scale gets set back.
//--------------------------------------------------------------------------
class Scaler
{
public:
Scaler( wxDC * dc, double xScale, double yScale )
{
m_dc = dc;
if ( m_dc )
{
dc->GetUserScale( &m_xScaleOld, &m_yScaleOld );
dc->SetUserScale( xScale, yScale );
}
}
~Scaler( )
{
if ( m_dc )
m_dc->SetUserScale( m_xScaleOld, m_yScaleOld );
}
private:
wxDC *m_dc;
double m_xScaleOld;
double m_yScaleOld;
Scaler & operator=( const Scaler & );
Scaler ( const Scaler & );
};
//--------------------------------------------------------------------------
// OriginChanger class
// This class changes the logical origin of a dc on construction and resets
// it to its original value on destruction. It is ideal for making temporary
// changes to the origin and guaranteeing that the scale gets set back.
//--------------------------------------------------------------------------
class OriginChanger
{
public:
OriginChanger( wxDC * dc, wxCoord xOrigin, wxCoord yOrigin )
{
m_dc = dc;
if ( m_dc )
{
dc->GetLogicalOrigin( &m_xOriginOld, &m_yOriginOld );
dc->SetLogicalOrigin( xOrigin, yOrigin );
}
}
~OriginChanger( )
{
if ( m_dc )
m_dc->SetLogicalOrigin( m_xOriginOld, m_yOriginOld );
}
private:
wxDC *m_dc;
wxCoord m_xOriginOld;
wxCoord m_yOriginOld;
OriginChanger & operator=( const OriginChanger & );
OriginChanger ( const OriginChanger & );
};
//--------------------------------------------------------------------------
// DrawingObjectsChanger class
// This class changes the pen and brush of a dc on construction and resets
// them to their original values on destruction. It is ideal for making temporary
// changes to the pen and brush and guaranteeing that they get set back.
//--------------------------------------------------------------------------
class DrawingObjectsChanger
{
public:
DrawingObjectsChanger( wxDC *dc, const wxPen &pen, const wxBrush &brush )
{
m_dc = dc;
if ( m_dc )
{
m_pen = dc->GetPen();
m_brush = dc->GetBrush();
dc->SetPen( pen );
dc->SetBrush( brush );
}
}
~DrawingObjectsChanger()
{
if ( m_dc )
{
m_dc->SetPen( m_pen );
m_dc->SetBrush( m_brush );
}
}
private:
wxDC *m_dc;
wxPen m_pen;
wxBrush m_brush;
DrawingObjectsChanger & operator=( const DrawingObjectsChanger & );
DrawingObjectsChanger ( const DrawingObjectsChanger & );
};
//--------------------------------------------------------------------------
//TextObjectsSaver class
//This class saves the text rendering details of a dc on construction and
//resets them to their original values on destruction. It can be used to
//ensure the restoration of state when a scope is exited
//--------------------------------------------------------------------------
class TextObjectsSaver
{
public:
TextObjectsSaver( wxDC *dc )
{
m_dc = dc;
if ( m_dc )
{
m_font = dc->GetFont();
m_textForeground = dc->GetTextForeground();
m_textBackground = dc->GetTextBackground();
}
}
~TextObjectsSaver()
{
if ( m_dc )
{
m_dc->SetTextForeground( m_textForeground );
m_dc->SetTextBackground( m_textBackground );
m_dc->SetFont( m_font );
}
}
private:
wxDC *m_dc;
wxFont m_font;
wxColour m_textForeground;
wxColour m_textBackground;
TextObjectsSaver & operator=( const TextObjectsSaver & );
TextObjectsSaver ( const TextObjectsSaver & );
};
//--------------------------------------------------------------------------
// TextObjectsChanger class
// This class changes the font and text colours of a dc on construction and resets
// them to their original values on destruction. It is ideal for making temporary
// changes to the text and guaranteeing that they get set back.
//--------------------------------------------------------------------------
class TextObjectsChanger
{
public:
TextObjectsChanger( wxDC *dc, const wxFont &font, const wxColour &textForeground, const wxColour &textBackground )
: m_saver( dc )
{
if ( dc )
{
dc->SetTextForeground( textForeground );
dc->SetTextBackground( textBackground );
dc->SetFont( font );
}
}
TextObjectsChanger( wxDC *dc, const wxFont &font )
: m_saver( dc )
{
if ( dc )
dc->SetFont( font );
}
TextObjectsChanger( wxDC *dc, FontGrabber &fontGrabber, PLUNICODE fci, PLFLT size, bool underlined, const wxColour &textForeground, const wxColour &textBackground )
: m_saver( dc )
{
if ( dc )
{
wxFont font = fontGrabber.GetFont( fci, size, underlined ).getWxFont();
dc->SetTextForeground( textForeground );
dc->SetTextBackground( textBackground );
dc->SetFont( font );
}
}
private:
TextObjectsSaver m_saver;
TextObjectsChanger & operator=( const TextObjectsChanger & );
TextObjectsChanger ( const TextObjectsChanger & );
};
//--------------------------------------------------------------------------
// Clipper class
// This class changes the clipping region of a dc on construction and restores
// it to its previous region on destruction. It is ideal for making temporary
// changes to the clip region and guaranteeing that the scale gets set back.
//
// It turns out that clipping is mostly broken for wxGCDC - see
// http://trac.wxwidgets.org/ticket/17013. So there are a lot of things in
// this class to work around those bugs. In particular you should check
// isEveryThingClipped before drawing as I'm not sure if non-overlapping
//clip regions behave properly.
//--------------------------------------------------------------------------
class Clipper
{
public:
Clipper( wxDC * dc, const wxRect &rect )
{
m_dc = dc;
m_clipEverything = true;
if ( m_dc )
{
dc->GetClippingBox( m_boxOld );
wxRect newRect = rect;
m_clipEverything = !( newRect.Intersects( m_boxOld )
|| ( m_boxOld.width == 0 && m_boxOld.height == 0 ) );
if ( m_clipEverything )
dc->SetClippingRegion( wxRect( -1, -1, 1, 1 ) ); //not sure if this works
else
dc->SetClippingRegion( rect );
}
}
~Clipper( )
{
if ( m_dc )
{
m_dc->DestroyClippingRegion();
m_dc->SetClippingRegion( wxRect( 0, 0, 0, 0 ) );
m_dc->DestroyClippingRegion();
if ( m_boxOld.width != 0 && m_boxOld.height != 0 )
m_dc->SetClippingRegion( m_boxOld );
}
}
bool isEverythingClipped()
{
return m_clipEverything;
}
private:
wxDC *m_dc;
wxRect m_boxOld;
bool m_clipEverything;
Clipper & operator=( const Clipper & );
Clipper ( const Clipper & );
};
//--------------------------------------------------------------------------
// class Rand
// This is a simple random number generator class, created solely so that
// random numbers can be generated in this file without "contaminating" the
// global series of random numbers with a new seed.
// It uses an algorithm that apparently used to be used in gcc rand()
// provided under GNU LGPL v2.1.
//--------------------------------------------------------------------------
class Rand
{
public:
Rand()
{
#ifdef _WIN32
rand_s( &m_seed );
#else
std::fstream fin( "/dev/urandom", std::ios::in );
if ( fin.is_open() )
fin.read( (char *) ( &m_seed ), sizeof ( m_seed ) );
else
{
fin.clear();
fin.open( "/dev/random", std::ios::in );
if ( fin.is_open() )
fin.read( (char *) ( &m_seed ), sizeof ( m_seed ) );
else
m_seed = 0;
}
fin.close();
#endif
}
Rand( unsigned int seed )
{
m_seed = seed;
}
unsigned int operator()()
{
unsigned int next = m_seed;
int result;
next *= 1103515245;
next += 12345;
result = (unsigned int) ( next / max ) % 2048;
next *= 1103515245;
next += 12345;
result <<= 10;
result ^= (unsigned int) ( next / max ) % 1024;
next *= 1103515245;
next += 12345;
result <<= 10;
result ^= (unsigned int) ( next / max ) % 1024;
m_seed = next;
return result;
}
static const unsigned int max = 65536;
private:
unsigned int m_seed;
};
void plFontToWxFontParameters( PLUNICODE fci, PLFLT scaledFontSize, wxFontFamily &family, int &style, int &weight, int &pt )
{
unsigned char plFontFamily, plFontStyle, plFontWeight;
plP_fci2hex( fci, &plFontFamily, PL_FCI_FAMILY );
plP_fci2hex( fci, &plFontStyle, PL_FCI_STYLE );
plP_fci2hex( fci, &plFontWeight, PL_FCI_WEIGHT );
family = fontFamilyLookup[plFontFamily];
style = fontStyleLookup[plFontStyle];
weight = fontWeightLookup[plFontWeight];
pt = ROUND( scaledFontSize );
}
Font::Font ( )
{
m_fci = 0;
m_size = std::numeric_limits<PLFLT>::quiet_NaN();
m_underlined = false;
m_hasFont = false;
}
Font::Font( PLUNICODE fci, PLFLT size, bool underlined, bool createFontOnConstruction )
{
m_fci = fci;
m_size = size;
m_underlined = underlined;
m_hasFont = false;
if ( createFontOnConstruction )
createFont();
}
void Font::createFont()
{
wxFontFamily family;
int style;
int weight;
int pt;
plFontToWxFontParameters( m_fci, m_size, family, style, weight, pt );
m_font = wxFont( pt, family, style, weight, m_underlined, wxEmptyString, wxFONTENCODING_DEFAULT );
//wxWidgets has a feature where wxDEFAULT can be passed in as the size in the constructor
//which gives the default size for the system. Annoyingly wxDEFAULT is 70 which can get used
//as an actual size. The workaround as per http://trac.wxwidgets.org/ticket/12315 is to call
//wxFont::SetPointSize after construction.
if ( pt == wxDEFAULT )
m_font.SetPointSize( pt );
m_hasFont = true;
}
wxFont Font::getWxFont()
{
if ( !m_hasFont )
createFont();
return m_font;
}
bool operator ==( const Font &lhs, const Font &rhs )
{
return lhs.getFci() == rhs.getFci()
&& lhs.getSize() == rhs.getSize()
&& lhs.getUnderlined() == rhs.getUnderlined();
}
//--------------------------------------------------------------------------
// FontGrabber::FontGrabber( )
//
// Default constructor
//--------------------------------------------------------------------------
FontGrabber::FontGrabber( )
{
m_lastWasCached = false;
}
//--------------------------------------------------------------------------
// Font FontGrabber::GetFont( PLUNICODE fci )
//
// Get the requested font either fresh or from the cache.
//--------------------------------------------------------------------------
Font FontGrabber::GetFont( PLUNICODE fci, PLFLT scaledFontSize, bool underlined )
{
Font newFont( fci, scaledFontSize, underlined );
if ( m_prevFont == newFont )
{
m_lastWasCached = true;
return m_prevFont;
}
m_lastWasCached = false;
return m_prevFont = newFont;
}
//--------------------------------------------------------------------------
// wxPLDevice::wxPLDevice( void )
//
// Constructor of the standard wxWidgets device based on the wxPLDevBase
// class. Only some initialisations are done.
//--------------------------------------------------------------------------
wxPLDevice::wxPLDevice( PLStream *pls, char * mfo, PLINT text, PLINT hrshsym )
: m_plplotEdgeLength( PLFLT( SHRT_MAX ) ), m_interactiveTextImage( 1, 1 )
{
PLPLOT_wxLogDebug( "wxPLDevice(): enter" );
m_fixedAspect = false;
m_lineSpacing = 1.0;
m_dc = NULL;
wxGraphicsContext *gc = wxGraphicsContext::Create( m_interactiveTextImage );
PLPLOT_wxLogDebug( "wxPLDevice(): gc done" );
try
{
m_interactiveTextGcdc = new wxGCDC( gc );
}
catch ( ... )
{
delete gc;
throw( "wxPLDevice::wxPLDevice: unknown failure in new wxGCDC( gc )" );
}
PLPLOT_wxLogDebug( "wxPLDevice(): m_interactiveTextGcdc done" );
if ( mfo )
strcpy( m_mfo, mfo );
else
//assume we will be outputting to the default
//memory map until we are given a dc to draw to
#ifdef WXPLVIEWER_DEBUG
strcpy( m_mfo, "plplotMemoryMap" );
#else
strcpy( m_mfo, "plplotMemoryMap??????????" );
#endif
// be verbose and write out debug messages
#ifdef _DEBUG
pls->verbose = 1;
pls->debug = 1;
#endif
pls->color = 1; // Is a color device
pls->dev_flush = 1; // Handles flushes
pls->dev_fill0 = 1; // Can handle solid fills
pls->dev_fill1 = 0; // Can't handle pattern fills
pls->dev_dash = 0;
pls->dev_clear = 1; // driver supports clear
pls->plbuf_write = 1; // use the plot buffer!
pls->termin = ( strlen( m_mfo ) ) > 0 ? 0 : 1; // interactive device unless we are writing to memory - pretty sure this is an unused option though
pls->graphx = GRAPHICS_MODE; // This indicates this is a graphics driver. PLplot will therefore call pltext() before outputting text, however we currently have not implemented catching that text.
if ( text )
{
pls->dev_text = 1; // want to draw text
pls->dev_unicode = 1; // want unicode
if ( hrshsym )
pls->dev_hrshsym = 1;
}
// Set up physical limits of plotting device in plplot internal units
// we just tell plplot we are the maximum plint in both dimensions
//which gives us the best resolution
plP_setphy( (PLINT) 0, (PLINT) SHRT_MAX,
(PLINT) 0, (PLINT) SHRT_MAX );
// set dpi and page size defaults if the user has not already set
// these with -dpi or -geometry command line options or with
// plspage.
if ( pls->xdpi <= 0. || pls->ydpi <= 0. )
{
// Use recommended default pixels per inch.
plspage( PLPLOT_DEFAULT_PIXELS_PER_INCH, PLPLOT_DEFAULT_PIXELS_PER_INCH, 0, 0, 0, 0 );
}
if ( pls->xlength == 0 || pls->ylength == 0 )
{
// Use recommended default pixel width and height.
plspage( 0., 0., PLPLOT_DEFAULT_WIDTH_PIXELS, PLPLOT_DEFAULT_HEIGHT_PIXELS, 0, 0 );
}
m_localBufferPosition = 0;
SetSize( pls, plsc->xlength, plsc->ylength );
if ( pls->dev_data )
{
SetDC( pls, (wxDC *) pls->dev_data );
PLPLOT_wxLogDebug( "wxPLDevice(): SetDC done" );
}
else
{
SetupMemoryMap();
}
PLPLOT_wxLogDebug( "wxPLDevice(): leave" );
//this must be the absolute last thing that is done
//so that if an exception is thrown pls->dev remains as NULL
pls->dev = (void *) this;
}
//--------------------------------------------------------------------------
// wxPLDevice::~wxPLDevice( void )
//
// The destructor frees memory allocated by the device.
//--------------------------------------------------------------------------
wxPLDevice::~wxPLDevice()
{
if ( m_outputMemoryMap.isValid() )
{
#ifdef PL_WXWIDGETS_IPC3
m_header.completeFlag = 1;
TransmitBuffer( NULL, transmissionComplete );
#else
MemoryMapHeader *header = (MemoryMapHeader *) ( m_outputMemoryMap.getBuffer() );
header->completeFlag = 1;
#endif
}
delete m_interactiveTextGcdc;
}
//--------------------------------------------------------------------------
// void wxPLDevice::PreDestructorTidy( PLStream *pls )
//
// This function performs any tidying up that requires a PLStream. should
// be called before the destructor obviously
//--------------------------------------------------------------------------
void wxPLDevice::PreDestructorTidy( PLStream *pls )
{
if ( !m_dc && pls->nopause )
TransmitBuffer( pls, transmissionClose );
}
//--------------------------------------------------------------------------
// void wxPLDevice::DrawLine( short x1a, short y1a, short x2a, short y2a )
//
// Draw a line from (x1a, y1a) to (x2a, y2a).
//--------------------------------------------------------------------------
void wxPLDevice::DrawLine( short x1a, short y1a, short x2a, short y2a )
{
if ( !m_dc )
return;
Clipper clipper( m_dc, GetClipRegion().GetBox() );
Scaler scaler( m_dc, 1.0 / m_scale, 1.0 / m_scale );
DrawingObjectsChanger drawingObjectsChanger( m_dc, m_pen, m_brush );
m_dc->DrawLine( (wxCoord) ( m_xAspect * x1a ), (wxCoord) ( m_yAspect * ( m_plplotEdgeLength - y1a ) ),
(wxCoord) ( m_xAspect * x2a ), (wxCoord) ( m_yAspect * ( m_plplotEdgeLength - y2a ) ) );
}
//--------------------------------------------------------------------------
// void wxPLDevice::DrawPolyline( short *xa, short *ya, PLINT npts )
//
// Draw a poly line - coordinates are in the xa and ya arrays.
//--------------------------------------------------------------------------
void wxPLDevice::DrawPolyline( short *xa, short *ya, PLINT npts )
{
if ( !m_dc )
return;
Clipper clipper( m_dc, GetClipRegion().GetBox() );
Scaler scaler( m_dc, 1.0 / m_scale, 1.0 / m_scale );
DrawingObjectsChanger drawingObjectsChanger( m_dc, m_pen, m_brush );
for ( PLINT i = 1; i < npts; i++ )
m_dc->DrawLine( m_xAspect * xa[i - 1], m_yAspect * ( m_plplotEdgeLength - ya[i - 1] ),
m_xAspect * xa[i], m_yAspect * ( m_plplotEdgeLength - ya[i] ) );
}
//--------------------------------------------------------------------------
// void wxPLDevice::ClearBackground( PLStream* pls, PLINT bgr, PLINT bgg, PLINT bgb,
// PLINT x1, PLINT y1, PLINT x2, PLINT y2 )
//
// Clear parts ((x1,y1) to (x2,y2)) of the background in color (bgr,bgg,bgb).
//--------------------------------------------------------------------------
void wxPLDevice::ClearBackground( PLStream* pls, PLINT x1, PLINT y1, PLINT x2, PLINT y2 )
{
if ( !m_dc )
return;
x1 = x1 < 0 ? 0 : x1;
x2 = x2 < 0 ? m_plplotEdgeLength : x2;
y1 = y1 < 0 ? 0 : y1;
y2 = y2 < 0 ? m_plplotEdgeLength : y2;
PLINT x = MIN( x1, x2 ) * m_xAspect;
PLINT y = ( m_plplotEdgeLength - MAX( y1, y2 ) ) * m_yAspect;
PLINT width = abs( x1 - x2 ) * m_xAspect;
PLINT height = abs( y1 - y2 ) * m_yAspect;
if ( width > 0 && height > 0 )
{
PLINT r, g, b;
PLFLT a;
plgcolbga( &r, &g, &b, &a );
wxColour bgColour( r, g, b, a * 255 );
DrawingObjectsChanger changer( m_dc, wxPen( bgColour, 0 ), wxBrush( bgColour ) );
Scaler scaler( m_dc, 1.0 / m_scale, 1.0 / m_scale );
m_dc->DrawRectangle( x, y, width, height );
}
}
//--------------------------------------------------------------------------
// void wxPLDevice::FillPolygon( PLStream *pls )
//