forked from dk/Prima
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrawable.c
1548 lines (1402 loc) · 42.5 KB
/
Drawable.c
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) 1997-2002 The Protein Laboratory, University of Copenhagen
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* --------------------------------------------------------------------
* Parabolic spline procedures taken from TclTk's tkTrig.c
*
* Copyright (c) 1992-1994 The Regents of the University of California.
* Copyright (c) 1994 Sun Microsystems, Inc.
*
* See the file "license.terms" in TclTk distribution
* for information on usage and redistribution
* of this code, and for a DISCLAIMER OF ALL WARRANTIES.
* ---------------------------------------------------------------------
*
* $Id$
*/
#include "apricot.h"
#include "Drawable.h"
#include "Image.h"
#include <Drawable.inc>
#ifdef __cplusplus
extern "C" {
#endif
#undef my
#define inherited CComponent->
#define my ((( PDrawable) self)-> self)
#define var (( PDrawable) self)
#define gpARGS Bool inPaint = opt_InPaint
#define gpENTER(fail) if ( !inPaint) if ( !my-> begin_paint_info( self)) return (fail)
#define gpLEAVE if ( !inPaint) my-> end_paint_info( self)
void
Drawable_init( Handle self, HV * profile)
{
dPROFILE;
inherited init( self, profile);
apc_gp_init( self);
var-> w = var-> h = 0;
my-> set_color ( self, pget_i ( color));
my-> set_backColor ( self, pget_i ( backColor));
my-> set_fillWinding ( self, pget_B ( fillWinding));
my-> set_fillPattern ( self, pget_sv( fillPattern));
my-> set_lineEnd ( self, pget_i ( lineEnd));
my-> set_lineJoin ( self, pget_i ( lineJoin));
my-> set_linePattern ( self, pget_sv( linePattern));
my-> set_lineWidth ( self, pget_i ( lineWidth));
my-> set_region ( self, pget_H ( region));
my-> set_rop ( self, pget_i ( rop));
my-> set_rop2 ( self, pget_i ( rop2));
my-> set_textOpaque ( self, pget_B ( textOpaque));
my-> set_textOutBaseline( self, pget_B ( textOutBaseline));
my-> set_splinePrecision( self, pget_i ( splinePrecision));
if ( pexist( translate))
{
AV * av = ( AV *) SvRV( pget_sv( translate));
Point tr = {0,0};
SV ** holder = av_fetch( av, 0, 0);
if ( holder) tr.x = SvIV( *holder); else warn("Array panic on 'translate'");
holder = av_fetch( av, 1, 0);
if ( holder) tr.y = SvIV( *holder); else warn("Array panic on 'translate'");
my-> set_translate( self, tr);
}
SvHV_Font( pget_sv( font), &Font_buffer, "Drawable::init");
my-> set_font( self, Font_buffer);
my-> set_palette( self, pget_sv( palette));
CORE_INIT_TRANSIENT(Drawable);
}
static void
clear_font_abc_caches( Handle self)
{
PList u;
if (( u = var-> font_abc_unicode)) {
int i;
for ( i = 0; i < u-> count; i += 2)
free(( void*) u-> items[ i + 1]);
plist_destroy( u);
var-> font_abc_unicode = nil;
}
if ( var-> font_abc_ascii) {
free( var-> font_abc_ascii);
var-> font_abc_ascii = nil;
}
}
void
Drawable_done( Handle self)
{
clear_font_abc_caches( self);
apc_gp_done( self);
inherited done( self);
}
void
Drawable_cleanup( Handle self)
{
if ( is_opt( optInDrawInfo))
my-> end_paint_info( self);
if ( is_opt( optInDraw))
my-> end_paint( self);
inherited cleanup( self);
}
Bool
Drawable_begin_paint( Handle self)
{
if ( var-> stage > csFrozen) return false;
if ( is_opt( optInDrawInfo)) my-> end_paint_info( self);
opt_set( optInDraw);
var-> splinePrecision_saved = var-> splinePrecision;
return true;
}
void
Drawable_end_paint( Handle self)
{
clear_font_abc_caches( self);
opt_clear( optInDraw);
var-> splinePrecision = var-> splinePrecision_saved;
}
Bool
Drawable_begin_paint_info( Handle self)
{
if ( var-> stage > csFrozen) return false;
if ( is_opt( optInDraw)) return true;
if ( is_opt( optInDrawInfo)) return false;
opt_set( optInDrawInfo);
var-> splinePrecision_saved = var-> splinePrecision;
return true;
}
void
Drawable_end_paint_info( Handle self)
{
clear_font_abc_caches( self);
opt_clear( optInDrawInfo);
var-> splinePrecision = var-> splinePrecision_saved;
}
void
Drawable_set( Handle self, HV * profile)
{
dPROFILE;
if ( pexist( font))
{
SvHV_Font( pget_sv( font), &Font_buffer, "Drawable::set");
my-> set_font( self, Font_buffer);
pdelete( font);
}
if ( pexist( translate))
{
AV * av = ( AV *) SvRV( pget_sv( translate));
Point tr = {0,0};
SV ** holder = av_fetch( av, 0, 0);
if ( holder) tr.x = SvIV( *holder); else warn("Array panic on 'translate'");
holder = av_fetch( av, 1, 0);
if ( holder) tr.y = SvIV( *holder); else warn("Array panic on 'translate'");
my-> set_translate( self, tr);
pdelete( translate);
}
if ( pexist( width) && pexist( height)) {
Point size;
size. x = pget_i( width);
size. y = pget_i( height);
my-> set_size( self, size);
pdelete( width);
pdelete( height);
}
inherited set( self, profile);
}
Font *
Drawable_font_match( char * dummy, Font * source, Font * dest, Bool pick)
{
if ( pick)
apc_font_pick( nilHandle, source, dest);
else
Drawable_font_add( nilHandle, source, dest);
return dest;
}
Bool
Drawable_font_add( Handle self, Font * source, Font * dest)
{
Bool useHeight = source-> height != C_NUMERIC_UNDEF;
Bool useWidth = source-> width != C_NUMERIC_UNDEF;
Bool useSize = source-> size != C_NUMERIC_UNDEF;
Bool usePitch = source-> pitch != C_NUMERIC_UNDEF;
Bool useStyle = source-> style != C_NUMERIC_UNDEF;
Bool useDir = source-> direction != C_NUMERIC_UNDEF;
Bool useName = strcmp( source-> name, C_STRING_UNDEF) != 0;
Bool useEnc = strcmp( source-> encoding, C_STRING_UNDEF) != 0;
/* assignning values */
if ( dest != source) {
if ( useHeight) dest-> height = source-> height;
if ( useWidth ) dest-> width = source-> width;
if ( useDir ) dest-> direction = source-> direction;
if ( useStyle ) dest-> style = source-> style;
if ( usePitch ) dest-> pitch = source-> pitch;
if ( useSize ) dest-> size = source-> size;
if ( useName ) strcpy( dest-> name, source-> name);
if ( useEnc ) strcpy( dest-> encoding, source-> encoding);
}
/* nulling dependencies */
if ( !useHeight && useSize)
dest-> height = 0;
if ( !useWidth && ( usePitch || useHeight || useName || useSize || useDir || useStyle))
dest-> width = 0;
if ( !usePitch && ( useStyle || useName || useDir || useWidth))
dest-> pitch = fpDefault;
if ( useHeight)
dest-> size = 0;
if ( !useHeight && !useSize && ( dest-> height <= 0 || dest-> height > 16383))
useSize = 1;
/* validating entries */
if ( dest-> height <= 0) dest-> height = 1;
else if ( dest-> height > 16383 ) dest-> height = 16383;
if ( dest-> width < 0) dest-> width = 1;
else if ( dest-> width > 16383 ) dest-> width = 16383;
if ( dest-> size <= 0) dest-> size = 1;
else if ( dest-> size > 16383 ) dest-> size = 16383;
if ( dest-> name[0] == 0)
strcpy( dest-> name, "Default");
if ( dest-> pitch < fpDefault || dest-> pitch > fpFixed)
dest-> pitch = fpDefault;
if ( dest-> direction == C_NUMERIC_UNDEF)
dest-> direction = 0;
if ( dest-> style == C_NUMERIC_UNDEF)
dest-> style = 0;
return useSize && !useHeight;
}
int
Drawable_get_paint_state( Handle self)
{
if ( is_opt( optInDraw))
return psEnabled;
else if ( is_opt( optInDrawInfo))
return psInformation;
else
return psDisabled;
}
int
Drawable_get_bpp( Handle self)
{
gpARGS;
int ret;
gpENTER(0);
ret = apc_gp_get_bpp( self);
gpLEAVE;
return ret;
}
SV *
Drawable_linePattern( Handle self, Bool set, SV * pattern)
{
if ( set) {
STRLEN len;
unsigned char *pat = ( unsigned char *) SvPV( pattern, len);
if ( len > 255) len = 255;
apc_gp_set_line_pattern( self, pat, len);
} else {
unsigned char ret[ 256];
int len = apc_gp_get_line_pattern( self, ret);
return newSVpvn((char*) ret, len);
}
return nilSV;
}
Color
Drawable_get_nearest_color( Handle self, Color color)
{
gpARGS;
gpENTER(clInvalid);
color = apc_gp_get_nearest_color( self, color);
gpLEAVE;
return color;
}
Point
Drawable_resolution( Handle self, Bool set, Point resolution)
{
if ( set)
croak("Attempt to write read-only property %s", "Drawable::resolution");
resolution = apc_gp_get_resolution( self);
return resolution;
}
SV *
Drawable_get_physical_palette( Handle self)
{
gpARGS;
int i, nCol;
AV * av = newAV();
PRGBColor r;
gpENTER(newRV_noinc(( SV *) av));
r = apc_gp_get_physical_palette( self, &nCol);
gpLEAVE;
for ( i = 0; i < nCol; i++) {
av_push( av, newSViv( r[ i].b));
av_push( av, newSViv( r[ i].g));
av_push( av, newSViv( r[ i].r));
}
free( r);
return newRV_noinc(( SV *) av);
}
SV *
Drawable_get_font_abc( Handle self, int first, int last, Bool unicode)
{
int i;
AV * av;
PFontABC abc;
if ( first < 0) first = 0;
if ( last < 0) last = 255;
if ( !unicode) {
if ( first > 255) first = 255;
if ( last > 255) last = 255;
}
if ( first > last)
abc = nil;
else {
gpARGS;
gpENTER( newRV_noinc(( SV *) newAV()));
abc = apc_gp_get_font_abc( self, first, last, unicode );
gpLEAVE;
}
av = newAV();
if ( abc != nil) {
for ( i = 0; i <= last - first; i++) {
av_push( av, newSVnv( abc[ i]. a));
av_push( av, newSVnv( abc[ i]. b));
av_push( av, newSVnv( abc[ i]. c));
}
free( abc);
}
return newRV_noinc(( SV *) av);
}
SV *
Drawable_get_font_ranges( Handle self)
{
int count = 0;
unsigned long * ret;
AV * av = newAV();
gpARGS;
gpENTER( newRV_noinc(( SV *) av));
ret = apc_gp_get_font_ranges( self, &count);
gpLEAVE;
if ( ret) {
int i;
for ( i = 0; i < count; i++)
av_push( av, newSViv( ret[i]));
free( ret);
}
return newRV_noinc(( SV *) av);
}
SV *
Drawable_get_handle( Handle self)
{
char buf[ 256];
snprintf( buf, 256, "0x%08lx", apc_gp_get_handle( self));
return newSVpv( buf, 0);
}
int
Drawable_height( Handle self, Bool set, int height)
{
Point p = my-> get_size( self);
if ( !set)
return p. y;
p. y = height;
my-> set_size( self, p);
return height;
}
Point
Drawable_size ( Handle self, Bool set, Point size)
{
if ( set)
croak("Attempt to write read-only property %s", "Drawable::size");
size. x = var-> w;
size. y = var-> h;
return size;
}
int
Drawable_width( Handle self, Bool set, int width)
{
Point p = my-> get_size( self);
if ( !set)
return p. x;
p. x = width;
my-> set_size( self, p);
return width;
}
Bool
Drawable_put_image_indirect( Handle self, Handle image, int x, int y, int xFrom, int yFrom, int xDestLen, int yDestLen, int xLen, int yLen, int rop)
{
Bool ok;
if ( image == nilHandle) return false;
if ( xLen == xDestLen && yLen == yDestLen)
ok = apc_gp_put_image( self, image, x, y, xFrom, yFrom, xLen, yLen, rop);
else
ok = apc_gp_stretch_image( self, image, x, y, xFrom, yFrom, xDestLen, yDestLen, xLen, yLen, rop);
if ( !ok) perl_error();
return ok;
}
Bool
Drawable_text_out( Handle self, SV * text, int x, int y)
{
Bool ok;
STRLEN dlen;
char * c_text = SvPV( text, dlen);
Bool utf8 = prima_is_utf8_sv( text);
if ( utf8) dlen = utf8_length(( U8*) c_text, ( U8*) c_text + dlen);
ok = apc_gp_text_out( self, c_text, x, y, dlen, utf8);
if ( !ok) perl_error();
return ok;
}
Point *
Drawable_polypoints( SV * points, char * procName, int mod, int * n_points)
{
AV * av;
int i, count;
Point * p;
if ( !SvROK( points) || ( SvTYPE( SvRV( points)) != SVt_PVAV)) {
warn("Invalid array reference passed to %s", procName);
return nil;
}
av = ( AV *) SvRV( points);
count = av_len( av) + 1;
if ( count % mod) {
warn("Drawable::%s: Number of elements in an array must be a multiple of %d",
procName, mod);
return nil;
}
count /= 2;
if ( count < 2) return nil;
if (!( p = allocn( Point, count))) return false;
for ( i = 0; i < count; i++)
{
SV** psvx = av_fetch( av, i * 2, 0);
SV** psvy = av_fetch( av, i * 2 + 1, 0);
if (( psvx == nil) || ( psvy == nil)) {
free( p);
warn("Array panic on item pair %d on Drawable::%s", i, procName);
return nil;
}
p[ i]. x = SvIV( *psvx);
p[ i]. y = SvIV( *psvy);
}
*n_points = count;
return p;
}
static Bool
polypoints( Handle self, SV * points, char * procName, int mod, Bool (*procPtr)(Handle,int,Point*))
{
int count;
Point * p;
Bool ret = false;
if (( p = Drawable_polypoints( points, procName, mod, &count))) {
ret = procPtr( self, count, p);
if ( !ret) perl_error();
free( p);
}
return ret;
}
Bool
Drawable_polyline( Handle self, SV * points)
{
return polypoints( self, points, "Drawable::polyline", 2, apc_gp_draw_poly);
}
Bool
Drawable_lines( Handle self, SV * points)
{
return polypoints( self, points, "Drawable::lines", 4, apc_gp_draw_poly2);
}
Bool
Drawable_fillpoly( Handle self, SV * points)
{
return polypoints( self, points, "Drawable::fillpoly", 2, apc_gp_fill_poly);
}
/*
*--------------------------------------------------------------
*
* TkBezierScreenPoints --
*
* Given four control points, create a larger set of XPoints
* for a Bezier spline based on the points.
*
* Results:
* The array at *xPointPtr gets filled in with numSteps XPoints
* corresponding to the Bezier spline defined by the four
* control points. Note: no output point is generated for the
* first input point, but an output point *is* generated for
* the last input point.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
static void
TkBezierScreenPoints(
double control[], /* Array of coordinates for four
* control points: x0, y0, x1, y1,
* ... x3 y3. */
int numSteps, /* Number of curve points to
* generate. */
register Point *xPointPtr) /* Where to put new points. */
{
int i;
double u, u2, u3, t, t2, t3;
for (i = 1; i <= numSteps; i++, xPointPtr++) {
t = ((double) i)/((double) numSteps);
t2 = t*t;
t3 = t2*t;
u = 1.0 - t;
u2 = u*u;
u3 = u2*u;
xPointPtr-> x = control[0]*u3 + 3.0 * (control[2]*t*u2 + control[4]*t2*u) + control[6]*t3;
xPointPtr-> y = control[1]*u3 + 3.0 * (control[3]*t*u2 + control[5]*t2*u) + control[7]*t3;
}
}
/*
*--------------------------------------------------------------
*
* TkMakeBezierCurve --
*
* Given a set of points, create a new set of points that fit
* parabolic splines to the line segments connecting the original
* points.
*
* Note: in spite of this procedure's name, it does *not* generate
* Bezier curves. Since only three control points are used for
* each curve segment, not four, the curves are actually just
* parabolic.
*
* Results:
* xPoints array is always filled
* in. The return value is the number of points placed in the
* array. Note: if the first and last points are the same, then
* a closed curve is generated.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
static int
TkMakeBezierCurve(
int *pointPtr, /* Array of input coordinates: x0,
* y0, x1, y1, etc.. */
int numPoints, /* Number of points at pointPtr. */
int numSteps, /* Number of steps to use for each
* spline segments (determines
* smoothness of curve). */
Point xPoints[]) /* Array of Points to fill in (e.g.
* for display. NULL means don't
* fill in any Points. */
{
int closed, outputPoints, i;
int numCoords = numPoints*2;
double control[8];
/*
* If the curve is a closed one then generate a special spline
* that spans the last points and the first ones. Otherwise
* just put the first point into the output.
*/
if (!pointPtr) {
/* Of pointPtr == NULL, this function returns an upper limit.
* of the array size to store the coordinates. This can be
* used to allocate storage, before the actual coordinates
* are calculated. */
return 1 + numPoints * numSteps;
}
outputPoints = 0;
if ((pointPtr[0] == pointPtr[numCoords-2])
&& (pointPtr[1] == pointPtr[numCoords-1])) {
closed = 1;
control[0] = 0.5*pointPtr[numCoords-4] + 0.5*pointPtr[0];
control[1] = 0.5*pointPtr[numCoords-3] + 0.5*pointPtr[1];
control[2] = 0.167*pointPtr[numCoords-4] + 0.833*pointPtr[0];
control[3] = 0.167*pointPtr[numCoords-3] + 0.833*pointPtr[1];
control[4] = 0.833*pointPtr[0] + 0.167*pointPtr[2];
control[5] = 0.833*pointPtr[1] + 0.167*pointPtr[3];
control[6] = 0.5*pointPtr[0] + 0.5*pointPtr[2];
control[7] = 0.5*pointPtr[1] + 0.5*pointPtr[3];
if (xPoints != NULL) {
xPoints-> x = control[0];
xPoints-> y = control[1];
TkBezierScreenPoints( control, numSteps, xPoints+1);
xPoints += numSteps+1;
}
outputPoints += numSteps+1;
} else {
closed = 0;
if (xPoints != NULL) {
xPoints->x = pointPtr[0];
xPoints->y = pointPtr[1];
xPoints += 1;
}
outputPoints += 1;
}
for (i = 2; i < numPoints; i++, pointPtr += 2) {
/*
* Set up the first two control points. This is done
* differently for the first spline of an open curve
* than for other cases.
*/
if ((i == 2) && !closed) {
control[0] = pointPtr[0];
control[1] = pointPtr[1];
control[2] = 0.333*pointPtr[0] + 0.667*pointPtr[2];
control[3] = 0.333*pointPtr[1] + 0.667*pointPtr[3];
} else {
control[0] = 0.5*pointPtr[0] + 0.5*pointPtr[2];
control[1] = 0.5*pointPtr[1] + 0.5*pointPtr[3];
control[2] = 0.167*pointPtr[0] + 0.833*pointPtr[2];
control[3] = 0.167*pointPtr[1] + 0.833*pointPtr[3];
}
/*
* Set up the last two control points. This is done
* differently for the last spline of an open curve
* than for other cases.
*/
if ((i == (numPoints-1)) && !closed) {
control[4] = .667*pointPtr[2] + .333*pointPtr[4];
control[5] = .667*pointPtr[3] + .333*pointPtr[5];
control[6] = pointPtr[4];
control[7] = pointPtr[5];
} else {
control[4] = .833*pointPtr[2] + .167*pointPtr[4];
control[5] = .833*pointPtr[3] + .167*pointPtr[5];
control[6] = 0.5*pointPtr[2] + 0.5*pointPtr[4];
control[7] = 0.5*pointPtr[3] + 0.5*pointPtr[5];
}
/*
* If the first two points coincide, or if the last
* two points coincide, then generate a single
* straight-line segment by outputting the last control
* point.
*/
if (((pointPtr[0] == pointPtr[2]) && (pointPtr[1] == pointPtr[3]))
|| ((pointPtr[2] == pointPtr[4])
&& (pointPtr[3] == pointPtr[5]))) {
if (xPoints != NULL) {
xPoints[0].x = control[6];
xPoints[0].y = control[7];
xPoints++;
}
outputPoints += 1;
continue;
}
/*
* Generate a Bezier spline using the control points.
*/
if (xPoints != NULL) {
TkBezierScreenPoints(control, numSteps, xPoints);
xPoints += numSteps;
}
outputPoints += numSteps;
}
return outputPoints;
}
#define STATIC_ARRAY_SIZE 200
static Bool
plot_spline( Handle self, int count, Point * points, Bool fill)
{
Bool ret;
int array_size;
Point static_array[STATIC_ARRAY_SIZE], *array;
array_size = TkMakeBezierCurve( NULL, count, var-> splinePrecision, NULL);
if ( array_size >= STATIC_ARRAY_SIZE) {
if ( !( array = malloc( array_size * sizeof( Point)))) {
warn("Not enough memory");
return false;
}
} else
array = static_array;
array_size = TkMakeBezierCurve((int*) points, count, var-> splinePrecision, array);
if ( fill && ( my-> fillpoly == Drawable_fillpoly)) {
ret = apc_gp_fill_poly( self, array_size, array);
if ( !ret) perl_error();
} else if ( !fill && ( my-> polyline == Drawable_polyline)) {
ret = apc_gp_draw_poly( self, array_size, array);
if ( !ret) perl_error();
} else {
int i;
AV * av = newAV();
SV * sv = newRV(( SV*) av);
for ( i = 0; i < array_size; i++) {
av_push( av, newSViv( array[i]. x));
av_push( av, newSViv( array[i]. y));
}
ret = fill ?
my-> fillpoly( self, sv) :
my-> polyline( self, sv);
sv_free( sv);
}
if ( array != static_array) free( array);
return ret;
}
static Bool
spline( Handle self, int count, Point * points)
{
return plot_spline( self, count, points, false);
}
static Bool
fill_spline( Handle self, int count, Point * points)
{
return plot_spline( self, count, points, true);
}
Bool
Drawable_spline( Handle self, SV * points)
{
return polypoints( self, points, "Drawable::spline", 2, spline);
}
Bool
Drawable_fill_spline( Handle self, SV * points)
{
return polypoints( self, points, "Drawable::fill_spline", 2, fill_spline);
}
SV *
Drawable_render_spline( SV * obj, SV * points, int precision)
{
int i, n_p, array_size;
Point static_array[STATIC_ARRAY_SIZE], *array, *p;
AV * av;
if ( precision < 0) {
Handle self;
self = gimme_the_mate( obj);
precision = self ? var-> splinePrecision : 24;
}
av = newAV();
p = Drawable_polypoints( points, "Drawable::render_spline", 2, &n_p);
if ( p) {
array_size = TkMakeBezierCurve( NULL, n_p, precision, NULL);
if ( array_size >= STATIC_ARRAY_SIZE) {
if ( !( array = malloc( array_size * sizeof( Point)))) {
warn("Not enough memory");
free( p);
return newRV_noinc(( SV *) av);
}
} else
array = static_array;
array_size = TkMakeBezierCurve((int*) p, n_p, precision, array);
for ( i = 0; i < array_size; i++) {
av_push( av, newSViv( array[i]. x));
av_push( av, newSViv( array[i]. y));
}
if ( array != static_array) free( array);
free( p);
}
return newRV_noinc(( SV *) av);
}
int
Drawable_get_text_width( Handle self, SV * text, Bool addOverhang)
{
gpARGS;
int res;
STRLEN dlen;
char * c_text = SvPV( text, dlen);
Bool utf8 = prima_is_utf8_sv( text);
if ( utf8) dlen = utf8_length(( U8*) c_text, ( U8*) c_text + dlen);
gpENTER(0);
res = apc_gp_get_text_width( self, c_text, dlen, addOverhang, utf8);
gpLEAVE;
return res;
}
SV *
Drawable_get_text_box( Handle self, SV * text)
{
gpARGS;
Point * p;
AV * av;
int i;
STRLEN dlen;
char * c_text = SvPV( text, dlen);
Bool utf8 = prima_is_utf8_sv( text);
if ( utf8) dlen = utf8_length(( U8*) c_text, ( U8*) c_text + dlen);
gpENTER( newRV_noinc(( SV *) newAV()));
p = apc_gp_get_text_box( self, c_text, dlen, utf8);
gpLEAVE;
av = newAV();
if ( p) {
for ( i = 0; i < 5; i++) {
av_push( av, newSViv( p[ i]. x));
av_push( av, newSViv( p[ i]. y));
};
free( p);
}
return newRV_noinc(( SV *) av);
}
static PFontABC
query_abc_range( Handle self, TextWrapRec * t, unsigned int base)
{
PFontABC abc;
/* find if present in cache */
if ( t-> utf8_text) {
if ( *(t-> unicode)) {
int i;
PList p;
if (( p = *(t-> unicode)))
for ( i = 0; i < p-> count; i += 2)
if (( unsigned int) p-> items[ i] == base)
return ( PFontABC) p-> items[i + 1];
}
} else
if ( *( t-> ascii)) return *(t-> ascii);
/* query ABC information */
if ( !self) {
abc = apc_gp_get_font_abc( self, base * 256, base * 256 + 255, t-> utf8_text);
if ( !abc) return nil;
} else if ( my-> get_font_abc == Drawable_get_font_abc) {
gpARGS;
gpENTER(nil);
abc = apc_gp_get_font_abc( self, base * 256, base * 256 + 255, t-> utf8_text);
gpLEAVE;
if ( !abc) return nil;
} else {
SV * sv;
if ( !( abc = malloc( 256 * sizeof( FontABC)))) return nil;
sv = my-> get_font_abc( self, base * 256, base * 256 + 255, t-> utf8_text);
if ( SvOK( sv) && SvROK( sv) && SvTYPE( SvRV( sv)) == SVt_PVAV) {
AV * av = ( AV*) SvRV( sv);
int i, j = 0, n = av_len( av) + 1;
if ( n > 256 * 3) n = 256 * 3;
n = ( n / 3) * 3;
if ( n < 256) memset( abc, 0, 256 * sizeof( FontABC));
for ( i = 0; i < n; i += 3) {
SV ** holder = av_fetch( av, i, 0);
if ( holder) abc[j]. a = ( float) SvNV( *holder);
holder = av_fetch( av, i + 1, 0);
if ( holder) abc[j]. b = ( float) SvNV( *holder);
holder = av_fetch( av, i + 2, 0);
if ( holder) abc[j]. c = ( float) SvNV( *holder);
j++;
}
} else
memset( abc, 0, 256 * sizeof( FontABC));
sv_free( sv);
}
/* store in cache */
if ( t-> utf8_text) {
PList p;
if ( !*(t-> unicode))
*(t-> unicode) = plist_create( 8, 8);
if (( p = *(t-> unicode))) {
list_add( p, ( Handle) base);
list_add( p, ( Handle) abc);
} else {
free( abc);
return nil;
}
} else
*(t-> ascii) = abc;
return abc;
}
static Bool
precalc_abc_buffer( PFontABC src, float * width, PFontABC dest)
{
int i;
if ( !dest) return false;
for ( i = 0; i < 256; i++) {
width[i] = src[i]. a + src[i]. b + src[i]. c;
dest[i]. a = ( src[i]. a < 0) ? - src[i]. a : 0;
dest[i]. b = src[i]. b;
dest[i]. c = ( src[i]. c < 0) ? - src[i]. c : 0;
}
return true;
}
static Bool
add_wrapped_text( TextWrapRec * t, int start, int utfstart, int end, int utfend,
int tildeIndex, int * tildePos, int * tildeLPos, int * tildeLine,
char *** lArray, int * lSize)
{
int l = end - start;
char *c = nil;
if (!( t-> options & twReturnChunks)) {
if ( !( c = allocs( l + 1))) return false;
memcpy( c, t-> text + start, l);
c[ l] = 0;
}
if ( tildeIndex >= 0 && tildeIndex >= start && tildeIndex < end) {
*tildeLine = t-> t_line = t-> count;
*tildePos = *tildeLPos = tildeIndex - start;
if ( tildeIndex == end - 1) {
t-> t_line++;
tildeLPos = 0;
}
}
if ( t-> count == *lSize) {
char ** n = allocn( char*, *lSize + 16);
if ( !n) return false;
memcpy( n, *lArray, sizeof( char*) * (*lSize));
*lSize += 16;
free( *lArray);
*lArray = n;
}
if ( t-> options & twReturnChunks) {
(*lArray)[ t-> count++] = INT2PTR(char*,utfstart);
(*lArray)[ t-> count++] = INT2PTR(char*,utfend - utfstart);
} else
(*lArray)[ t-> count++] = c;