-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathwxwidgets_dev.cpp
More file actions
1558 lines (1405 loc) · 58.9 KB
/
Copy pathwxwidgets_dev.cpp
File metadata and controls
1558 lines (1405 loc) · 58.9 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 Phil Rosenberg
// 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 do
//it 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>
// std and driver headers
#include <cmath>
#include <limits>
//--------------------------------------------------------------------------
// 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 guarenteeing that the scale gets set back.
//--------------------------------------------------------------------------
class Scaler
{
public:
Scaler( wxDC * dc, double xScale, double yScale )
{
m_dc = dc;
if ( m_dc )
{
dc->GetLogicalScale( &m_xScaleOld, &m_yScaleOld );
dc->SetLogicalScale( xScale, yScale );
}
}
~Scaler( )
{
if ( m_dc )
m_dc->SetLogicalScale( m_xScaleOld, m_yScaleOld );
}
private:
wxDC *m_dc;
double m_xScaleOld;
double m_yScaleOld;
};
//--------------------------------------------------------------------------
// 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 guarenteeing 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;
};
//--------------------------------------------------------------------------
// 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 guarenteeing 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;
};
//--------------------------------------------------------------------------
// 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 guarenteeing that they get set back.
//--------------------------------------------------------------------------
class TextObjectsChanger
{
public:
TextObjectsChanger( wxDC *dc, const wxFont &font, const wxColour &textForeground, const wxColour &textBackground )
{
m_dc = dc;
if ( m_dc )
{
m_font = dc->GetFont();
m_textForeground = dc->GetTextForeground();
m_textBackground = dc->GetTextBackground();
dc->SetTextForeground( textForeground );
dc->SetTextBackground( textBackground );
dc->SetFont( font );
}
}
TextObjectsChanger( wxDC *dc, const wxFont &font )
{
m_dc = dc;
if ( m_dc )
{
m_font = dc->GetFont();
m_textForeground = dc->GetTextForeground();
m_textBackground = dc->GetTextBackground();
dc->SetFont( font );
}
}
~TextObjectsChanger()
{
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;
};
//--------------------------------------------------------------------------
// 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 guarenteeing 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 aound 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;
};
//--------------------------------------------------------------------------
// class Rand
// This is a simple random number generator class, created soley 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/random", std::ios::in );
fin.read( (char *) ( &m_seed ), sizeof ( m_seed ) );
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 );
}
//--------------------------------------------------------------------------
// FontGrabber::FontGrabber( )
//
// Default constructor
//--------------------------------------------------------------------------
FontGrabber::FontGrabber( )
{
//set m_prevScaledFontSize to a nan to ensure testing the prev values
//always gives false
m_prevFci = 0;
m_prevScaledFontSize = std::numeric_limits<PLFLT>::quiet_NaN();
m_prevUnderlined = false;
m_lastWasCached = false;
}
//--------------------------------------------------------------------------
// wxFont FontGrabber::GetFont( PLUNICODE fci )
//
// Get the requested font either fresh of from the cache.
//--------------------------------------------------------------------------
wxFont FontGrabber::GetFont( PLUNICODE fci, PLFLT scaledFontSize, bool underlined )
{
if ( m_prevFci == fci && m_prevScaledFontSize == scaledFontSize
&& m_prevUnderlined == underlined )
{
m_lastWasCached = true;
return m_prevFont;
}
m_prevFci = fci;
m_prevScaledFontSize = scaledFontSize;
m_prevUnderlined = underlined;
m_lastWasCached = false;
wxFontFamily family;
int style;
int weight;
int pt;
plFontToWxFontParameters( fci, scaledFontSize, family, style, weight, pt );
return m_prevFont = wxFont( pt, family, style, weight, underlined, wxEmptyString, wxFONTENCODING_DEFAULT );
}
//--------------------------------------------------------------------------
// 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_fixedAspect = false;
m_lineSpacing = 1.0;
m_dc = NULL;
m_prevSingleCharString = 0;
m_prevSingleCharStringWidth = 0;
m_prevSingleCharStringHeight = 0;
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 with therefore cal 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
// the user might have already set this with plspage
// so check first
if ( !plsc->pageset )
c_plspage( 90, 90, 900, 675, 0, 0 );
//check dpi is non-zero otherwise we get infinities in some calcualtions
//and ridiculous numbers in others
if ( pls->xdpi == 0.0 || pls->ydpi == 0 )
{
if ( pls->xdpi == 0.0 && pls->ydpi == 0 )
c_plspage( 90, 90, 0, 0, 0, 0 );
else
{
PLFLT dpi = MAX( pls->xdpi, pls->ydpi );
pls->xdpi = dpi;
pls->ydpi = dpi;
}
}
m_localBufferPosition = 0;
SetSize( pls, plsc->xlength, plsc->ylength );
if ( pls->dev_data )
SetDC( pls, (wxDC *) pls->dev_data );
else
SetupMemoryMap();
//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 deconstructor frees memory allocated by the device.
//--------------------------------------------------------------------------
wxPLDevice::~wxPLDevice()
{
if ( m_outputMemoryMap.isValid() )
{
MemoryMapHeader *header = (MemoryMapHeader *) ( m_outputMemoryMap.getBuffer() );
header->completeFlag = 1;
}
}
//--------------------------------------------------------------------------
// 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 );
PLINT y = MIN( y1, y2 );
PLINT width = abs( x1 - x2 );
PLINT height = abs( y1 - y2 );
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 ) );
m_dc->DrawRectangle( x, y, width, height );
}
}
//--------------------------------------------------------------------------
// void wxPLDevice::FillPolygon( PLStream *pls )
//
// Draw a filled polygon.
//--------------------------------------------------------------------------
void wxPLDevice::FillPolygon( PLStream *pls )
{
if ( !m_dc )
return;
//edge the polygon with a 0.5 pixel line to avoid seams. This is a
//bit of a bodge really but this is a difficult problem
wxPen edgePen( m_brush.GetColour(), m_scale, wxSOLID );
DrawingObjectsChanger changer( m_dc, edgePen, m_brush );
//DrawingObjectsChanger changer(m_dc, wxNullPen, m_brush );
Scaler scaler( m_dc, 1.0 / m_scale, 1.0 / m_scale );
wxPoint *points = new wxPoint[pls->dev_npts];
wxCoord xoffset = 0;
wxCoord yoffset = 0;
for ( int i = 0; i < pls->dev_npts; i++ )
{
points[i].x = (int) ( m_xAspect * pls->dev_x[i] );
points[i].y = (int) ( m_yAspect * ( m_plplotEdgeLength - pls->dev_y[i] ) );
}
if ( pls->dev_eofill )
{
m_dc->DrawPolygon( pls->dev_npts, points, xoffset, yoffset, wxODDEVEN_RULE );
}
else
{
m_dc->DrawPolygon( pls->dev_npts, points, xoffset, yoffset, wxWINDING_RULE );
}
delete[] points;
}
//--------------------------------------------------------------------------
// void wxPLDevice::SetWidth( PLStream *pls )
//
// Set the width of the drawing pen.
//--------------------------------------------------------------------------
void wxPLDevice::SetWidth( PLStream *pls )
{
PLFLT width = ( pls->width > 0.0 ? pls->width : 1.0 ) * m_scale;
m_pen = wxPen( wxColour( pls->curcolor.r, pls->curcolor.g, pls->curcolor.b,
pls->curcolor.a * 255 ), width, wxSOLID );
}
//--------------------------------------------------------------------------
// void wxPLDevice::SetColor( PLStream *pls )
//
// Set color from PLStream.
//--------------------------------------------------------------------------
void wxPLDevice::SetColor( PLStream *pls )
{
PLFLT width = ( pls->width > 0.0 ? pls->width : 1.0 ) * m_scale;
m_pen = wxPen( wxColour( pls->curcolor.r, pls->curcolor.g, pls->curcolor.b,
pls->curcolor.a * 255 ), width, wxSOLID );
m_brush = wxBrush( wxColour( pls->curcolor.r, pls->curcolor.g, pls->curcolor.b,
pls->curcolor.a * 255 ) );
}
//--------------------------------------------------------------------------
// void wxPLDevice::SetDC( PLStream *pls, void* dc )
//
// Adds a dc to the device. In that case, the drivers doesn't provide
// a GUI.
//--------------------------------------------------------------------------
void wxPLDevice::SetDC( PLStream *pls, wxDC* dc )
{
if ( m_outputMemoryMap.isValid() )
throw( "wxPLDevice::SetDC The DC must be set before initialisation. The device is outputting to a separate viewer" );
m_dc = dc; // Add the dc to the device
m_useDcTextTransform = false;
m_gc = NULL;
if ( m_dc )
{
#if wxVERSION_NUMBER >= 2902
m_useDcTextTransform = m_dc->CanUseTransformMatrix();
#endif
//If we don't have wxDC tranforms we can use the
//underlying wxGCDC if this is a wxGCDC
if ( !m_useDcTextTransform )
{
//check to see if m_dc is a wxGCDC by using RTTI
wxGCDC *gcdc = NULL;
try
{
//put this in a try block as I'm not sure if it will throw if
//RTTI is switched off
gcdc = dynamic_cast< wxGCDC* >( m_dc );
}
catch ( ... )
{
}
if ( gcdc )
m_gc = gcdc->GetGraphicsContext();
}
strcpy( m_mfo, "" );
SetSize( pls, m_width, m_height ); //call with our current size to set the scaling
pls->has_string_length = 1; // Driver supports string length calculations, if we have a dc to draw on
}
else
{
pls->has_string_length = 0; //if we have no device to draw on we cannot check string size
}
}
PLFLT getTextOffset( PLINT superscriptLevel, PLFLT baseFontSize )
{
if ( superscriptLevel == 0 )
return 0;
else
{
PLFLT fontScale = pow( 0.8, abs( superscriptLevel ) );
if ( superscriptLevel > 0 )
return getTextOffset( superscriptLevel - 1, baseFontSize ) + baseFontSize * fontScale / 2.;
else
return getTextOffset( superscriptLevel + 1, baseFontSize ) - baseFontSize * fontScale * 0.8 / 2.;
}
}
//--------------------------------------------------------------------------
// void wxPLDevice::DrawText( PLUNICODE* ucs4, int ucs4Len, bool drawText )
//
// This is the function to draw text. Pass in a unicde string and its
// length and set drawText to true to actually draw the text or false to
// just get text metrics. This function will process the string for inline
// style change escapes and newlines.
//--------------------------------------------------------------------------
void wxPLDevice::DrawTextLine( PLUNICODE* ucs4, int ucs4Len, PLFLT baseFontSize, bool drawText, PLINT &superscriptLevel, bool &underlined )
{
if ( !m_dc && drawText )
return;
char utf8_string[m_max_string_length];
char utf8[5];
memset( utf8_string, '\0', m_max_string_length );
// Get PLplot escape character
char plplotEsc;
plgesc( &plplotEsc );
//Reset the size metrics
m_textWidth = 0;
m_textHeight = 0;
m_superscriptHeight = 0;
m_subscriptDepth = 0;
int i = 0;
while ( i < ucs4Len )
{
if ( ucs4[i] < PL_FCI_MARK ) // not a font change
{
if ( ucs4[i] != (PLUNICODE) plplotEsc ) // a character to display
{
ucs4_to_utf8( ucs4[i], utf8 );
strncat( utf8_string, utf8,
sizeof ( utf8_string ) - strlen( utf8_string ) - 1 );
i++;
continue;
}
i++;
if ( ucs4[i] == (PLUNICODE) plplotEsc ) // a escape character to display
{
ucs4_to_utf8( ucs4[i], utf8 );
strncat( utf8_string, utf8,
sizeof ( utf8_string ) - strlen( utf8_string ) - 1 );
i++;
continue;
}
else
{
if ( ucs4[i] == (PLUNICODE) 'u' ) // Superscript
{ // draw string so far
PLFLT fontScale = pow( 0.8, abs( superscriptLevel ) );
PLFLT yOffset = getTextOffset( superscriptLevel, baseFontSize ) * m_yScale;
DrawTextSection( utf8_string, baseFontSize * fontScale, yOffset, underlined, drawText );
++superscriptLevel;
fontScale = pow( 0.8, abs( superscriptLevel ) );
if ( m_dc )
m_dc->SetFont( m_fontGrabber.GetFont( m_fci, baseFontSize * fontScale, underlined ) );
}
if ( ucs4[i] == (PLUNICODE) 'd' ) // Subscript
{ // draw string so far
PLFLT fontScale = pow( 0.8, abs( superscriptLevel ) );
PLFLT yOffset = getTextOffset( superscriptLevel, baseFontSize ) * m_yScale;
DrawTextSection( utf8_string, baseFontSize * fontScale, yOffset, underlined, drawText );
--superscriptLevel;
fontScale = pow( 0.8, abs( superscriptLevel ) );
if ( m_dc )
m_dc->SetFont( m_fontGrabber.GetFont( m_fci, baseFontSize * fontScale, underlined ) );
}
if ( ucs4[i] == (PLUNICODE) '-' ) // underline
{ // draw string so far
PLFLT fontScale = pow( 0.8, abs( superscriptLevel ) );
PLFLT yOffset = getTextOffset( superscriptLevel, baseFontSize ) * m_yScale;
DrawTextSection( utf8_string, baseFontSize * fontScale, yOffset, underlined, drawText );
underlined = !underlined;
if ( m_dc )
m_dc->SetFont( m_fontGrabber.GetFont( m_fci, baseFontSize * fontScale, underlined ) );
}
if ( ucs4[i] == (PLUNICODE) '+' ) // overline
{ // not implemented yet
}
i++;
}
}
else // a font change
{
// draw string so far
PLFLT fontScale = pow( 0.8, abs( superscriptLevel ) );
PLFLT yOffset = getTextOffset( superscriptLevel, baseFontSize ) * m_yScale;
DrawTextSection( utf8_string, baseFontSize * fontScale, yOffset, underlined, drawText );
// get new font
m_fci = ucs4[i];
if ( m_dc )
m_dc->SetFont( m_fontGrabber.GetFont( m_fci, baseFontSize * fontScale, underlined ) );
i++;
}
}
//we have reached the end of the string. Draw the remainder.
PLFLT fontScale = pow( 0.8, abs( superscriptLevel ) );
PLFLT yOffset = getTextOffset( superscriptLevel, baseFontSize ) * m_yScale;
DrawTextSection( utf8_string, baseFontSize * fontScale, yOffset, underlined, drawText );
}
//--------------------------------------------------------------------------
// void wxPLDevice::DrawTextSection( char* utf8_string, bool drawText )
//
// Draw a section of text. This should already have been processed and
// split into sections so that the text passed in has no style changes or
// newines.
//--------------------------------------------------------------------------
void wxPLDevice::DrawTextSection( char* utf8_string, PLFLT scaledFontSize, PLFLT yOffset, bool underlined, bool drawText )
{
if ( !m_dc && drawText )
return;
wxCoord w, h, d, l;
wxString str = wxString::FromUTF8( utf8_string );
//wxString str( wxConvUTF8.cMB2WC( utf8_string ), *wxConvCurrent );
if ( m_dc )
m_dc->GetTextExtent( str, &w, &h, &d, &l );
if ( !m_dc && m_outputMemoryMap.isValid() )
{
MemoryMapHeader *header = (MemoryMapHeader *) ( m_outputMemoryMap.getBuffer() );
header->textSizeInfo.written = false;
plFontToWxFontParameters( m_fci, scaledFontSize, header->textSizeInfo.family, header->textSizeInfo.style,
header->textSizeInfo.weight, header->textSizeInfo.pt );
header->textSizeInfo.underlined = underlined;
bool gotResponse = false;
TransmitBuffer( NULL, transmissionRequestTextSize );
size_t counter = 0;
while ( !gotResponse && counter < 1000 )
{
gotResponse = header->textSizeInfo.written;
++counter;
wxMilliSleep( 1 );
}
if ( counter == 1000 )
plwarn( "Failed to get text size from wxPLViewer - timeout" );
w = header->textSizeInfo.width;
h = header->textSizeInfo.height;
}
if ( drawText )
{
//if we are using wxDC transforms or the wxGC, then the transformations
//have already been applied
if ( m_gc )
m_gc->DrawText( str, m_textWidth, -yOffset / m_yScale );
else if ( m_useDcTextTransform )
m_dc->DrawText( str, m_textWidth, -yOffset / m_yScale );
else
{
//If we are stuck with a wxDC that has no transformation abilities then
// all we can really do is rotate the text - this is a bit of a poor state
// really, but to be honest it is better than defaulting to hershey for all
// text
if ( m_rotation == 0 )
m_dc->DrawRotatedText( str, (wxCoord) ( m_posX + m_textWidth ),
(wxCoord) ( m_height - (wxCoord) ( m_posY + yOffset / m_yScale ) ),
m_rotation * 180.0 / M_PI );
else
m_dc->DrawRotatedText( str,
(wxCoord) ( m_posX - yOffset / m_yScale * sin( m_rotation ) + m_textWidth * cos( m_rotation ) ),
(wxCoord) ( m_height - (wxCoord) ( m_posY + yOffset * cos( m_rotation ) / m_yScale ) - m_textWidth * sin( m_rotation ) ),
m_rotation * 180.0 / M_PI );
}
}
m_textWidth += w;
//keep track of the height of superscript text, the depth of subscript
//text and the height of regular text
if ( yOffset > 0.0001 )
{
//determine the height the text would have if it were full size
double currentOffset = yOffset;
double currentHeight = h;
while ( currentOffset > 0.0001 )
{
currentOffset -= m_yScale * scaledFontSize / 2.;
currentHeight *= 1.25;
}
m_textHeight = (wxCoord) m_textHeight > ( currentHeight )
? m_textHeight
: currentHeight;
//work out the height including superscript
m_superscriptHeight = m_superscriptHeight > ( currentHeight + yOffset / m_yScale )
? m_superscriptHeight
: static_cast<int>( ( currentHeight + yOffset / m_yScale ) );
}
else if ( yOffset < -0.0001 )
{
//determine the height the text would have if it were full size
double currentOffset = yOffset;
double currentHeight = h;
double currentDepth = d;
while ( currentOffset < -0.0001 )
{
currentOffset += m_yScale * scaledFontSize * 1.25 / 2.;
currentHeight *= 1.25;
currentDepth *= 1.25;
}
m_textHeight = (wxCoord) m_textHeight > currentHeight ? m_textHeight : currentHeight;
//work out the additional depth for subscript note an assumption has been made
//that the font size of (non-superscript and non-subscript) text is the same
//along a line. Currently there is no escape to change font size mid string
//so this should be fine
m_subscriptDepth = (wxCoord) m_subscriptDepth > ( ( -yOffset / m_yScale + h + d ) - ( currentDepth + m_textHeight ) )
? m_subscriptDepth
: ( ( -yOffset / m_yScale + h + d ) - ( currentDepth + m_textHeight ) );
m_subscriptDepth = m_subscriptDepth > 0 ? m_subscriptDepth : 0;
}
else
m_textHeight = (wxCoord) m_textHeight > ( h ) ? m_textHeight : h;
memset( utf8_string, '\0', m_max_string_length );
}
//--------------------------------------------------------------------------
// void wxPLDevice::ProcessString( 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 wxPLDevice::ProcessString( PLStream* pls, EscText* args )
{
if ( !m_dc && !m_outputMemoryMap.isValid() )
return;
m_textWidth = 0;
m_textHeight = 0;
m_textDescent = 0;
m_textLeading = 0;
//for text, work in native coordinates, partly to avoid rewriting existing code
//but also because we should get better text hinting for screen display I think.
//The scaler object sets the scale to the new value until it is destroyed
//when this function exits.
Scaler scaler( m_dc, 1.0, 1.0 );
//Also move the origin so the text region buts up to the dc top, not the bottom
OriginChanger originChanger( m_dc, 0, wxCoord( m_height - m_plplotEdgeLength / m_yScale ) );
// 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 * 72.0 / 25.4;
// Use PLplot core routine to get the corners of the clipping rectangle
PLINT rcx[4], rcy[4];
difilt_clip( rcx, rcy );
wxPoint cpoints[4];
for ( int i = 0; i < 4; i++ )
{
cpoints[i].x = rcx[i] / m_xScale;
cpoints[i].y = m_height - rcy[i] / m_yScale;
}
Clipper clipper( m_dc, wxRegion( 4, cpoints ).GetBox() );
PLUNICODE *lineStart = args->unicode_array;
int lineLen = 0;
bool lineFeed = false;
bool carriageReturn = false;
wxCoord paraHeight = 0;
// Get the curent font
PLINT superscriptLevel = 0;
bool underlined = 0;
plgfci( &m_fci );
//set the font up, we use a textObjectChanger here so that the font returns
//to its original value on exit
TextObjectsChanger textObjectsChanger( m_dc, m_fontGrabber.GetFont( m_fci, baseFontSize, underlined ),
wxColour( pls->curcolor.r, pls->curcolor.g, pls->curcolor.b, pls->curcolor.a * 255 ),
wxColour( pls->curcolor.r, pls->curcolor.g, pls->curcolor.b, pls->curcolor.a * 255 ) );
bool lastFontWasCached = m_fontGrabber.lastWasCached();
//draw each line of text individually and record the width of the paragraph
wxCoord paragraphWidth = 0;
while ( lineStart != args->unicode_array + args->unicode_array_len )
{
//remember the text parameters so they can be restored
double lineStartSuperscriptLevel = superscriptLevel;
PLUNICODE lineStartFci = m_fci;
bool lineStartUnderlined = underlined;
while ( lineStart + lineLen != args->unicode_array + args->unicode_array_len
&& *( lineStart + lineLen ) != (PLUNICODE) '\n' )
{
lineLen++;
}
//set line feed for the beginning of this line and
//carriage return for the end
lineFeed = carriageReturn;
carriageReturn = lineStart + lineLen != args->unicode_array + args->unicode_array_len
&& *( lineStart + lineLen ) == (PLUNICODE) ( '\n' );
if ( lineFeed )