1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
|
#define __CURVE_C__
/** \file
* Routines for SPCurve and for NArtBpath arrays in general.
*/
/*
* Author:
* Lauris Kaplinski <lauris@kaplinski.com>
*
* Copyright (C) 2000 Lauris Kaplinski
* Copyright (C) 2000-2001 Ximian, Inc.
* Copyright (C) 2002 Lauris Kaplinski
*
* Released under GNU GPL
*/
#include <glib/gmem.h>
#include <display/curve.h>
#include <libnr/n-art-bpath.h>
#include <libnr/nr-point-matrix-ops.h>
#include <libnr/nr-translate-ops.h>
#define SP_CURVE_LENSTEP 32
static bool sp_bpath_good(NArtBpath const bpath[]);
static NArtBpath *sp_bpath_clean(NArtBpath const bpath[]);
static NArtBpath const *sp_bpath_check_subpath(NArtBpath const bpath[]);
static unsigned sp_bpath_length(NArtBpath const bpath[]);
static bool sp_bpath_closed(NArtBpath const bpath[]);
/* Constructors */
/**
* The returned curve's state is as if sp_curve_reset has just been called on it.
*/
SPCurve *
sp_curve_new()
{
return sp_curve_new_sized(SP_CURVE_LENSTEP);
}
/**
* Like sp_curve_new, but overriding the default initial capacity.
*
* The returned curve's state is as if sp_curve_reset has just been called on it.
*
* \param length Initial number of NArtBpath elements allocated for bpath (including NR_END
* element).
*/
SPCurve *
sp_curve_new_sized(gint length)
{
g_return_val_if_fail(length > 0, NULL);
SPCurve *curve = g_new(SPCurve, 1);
curve->refcount = 1;
curve->_bpath = g_new(NArtBpath, length);
curve->_bpath->code = NR_END;
curve->end = 0;
curve->length = length;
curve->substart = 0;
curve->hascpt = false;
curve->posSet = false;
curve->moving = false;
curve->closed = false;
return curve;
}
/**
* Convert NArtBpath object to SPCurve object.
*
* \return new SPCurve, or NULL if the curve was not created for some reason.
*/
SPCurve *
sp_curve_new_from_bpath(NArtBpath *bpath)
{
g_return_val_if_fail(bpath != NULL, NULL);
SPCurve *curve = sp_curve_new_from_foreign_bpath(bpath);
g_free(bpath);
return curve;
}
/**
* Convert const NArtBpath array to SPCurve.
*
* \return new SPCurve, or NULL if the curve was not created for some reason.
*/
SPCurve *sp_curve_new_from_foreign_bpath(NArtBpath const bpath[])
{
g_return_val_if_fail(bpath != NULL, NULL);
NArtBpath *new_bpath;
if (!sp_bpath_good(bpath)) {
new_bpath = sp_bpath_clean(bpath);
g_return_val_if_fail(new_bpath != NULL, NULL);
} else {
unsigned const len = sp_bpath_length(bpath);
new_bpath = g_new(NArtBpath, len);
memcpy(new_bpath, bpath, len * sizeof(NArtBpath));
}
SPCurve *curve = g_new(SPCurve, 1);
curve->refcount = 1;
curve->_bpath = new_bpath;
curve->length = sp_bpath_length(new_bpath);
curve->end = curve->length - 1;
gint i = curve->end;
for (; i > 0; i--)
if ((curve->_bpath[i].code == NR_MOVETO) ||
(curve->_bpath[i].code == NR_MOVETO_OPEN))
break;
curve->substart = i;
curve->hascpt = false;
curve->posSet = false;
curve->moving = false;
curve->closed = sp_bpath_closed(new_bpath);
return curve;
}
SPCurve *sp_curve_new_from_rect(NR::Maybe<NR::Rect> const &rect)
{
g_return_val_if_fail(rect, NULL);
SPCurve *c = sp_curve_new();
NR::Point p = rect->corner(0);
sp_curve_moveto(c, p);
for (int i=3; i>=0; i--) {
sp_curve_lineto(c, rect->corner(i));
}
sp_curve_closepath_current(c);
return c;
}
/**
* Increase refcount of curve.
*
* \todo should this be shared with other refcounting code?
*/
SPCurve *
sp_curve_ref(SPCurve *curve)
{
g_return_val_if_fail(curve != NULL, NULL);
curve->refcount += 1;
return curve;
}
/**
* Decrease refcount of curve, with possible destruction.
*
* \todo should this be shared with other refcounting code?
*/
SPCurve *
sp_curve_unref(SPCurve *curve)
{
g_return_val_if_fail(curve != NULL, NULL);
curve->refcount -= 1;
if (curve->refcount < 1) {
if (curve->_bpath) {
g_free(curve->_bpath);
}
g_free(curve);
}
return NULL;
}
/**
* Add space for more paths in curve.
*/
static void
sp_curve_ensure_space(SPCurve *curve, gint space)
{
g_return_if_fail(curve != NULL);
g_return_if_fail(space > 0);
if (curve->end + space < curve->length)
return;
if (space < SP_CURVE_LENSTEP)
space = SP_CURVE_LENSTEP;
curve->_bpath = g_renew(NArtBpath, curve->_bpath, curve->length + space);
curve->length += space;
}
/**
* Create new curve from its own bpath array.
*/
SPCurve *
sp_curve_copy(SPCurve *curve)
{
g_return_val_if_fail(curve != NULL, NULL);
return sp_curve_new_from_foreign_bpath(curve->_bpath);
}
/**
* Return new curve that is the concatenation of all curves in list.
*/
SPCurve *
sp_curve_concat(GSList const *list)
{
g_return_val_if_fail(list != NULL, NULL);
gint length = 0;
for (GSList const *l = list; l != NULL; l = l->next) {
SPCurve *c = (SPCurve *) l->data;
length += c->end;
}
SPCurve *new_curve = sp_curve_new_sized(length + 1);
NArtBpath *bp = new_curve->_bpath;
for (GSList const *l = list; l != NULL; l = l->next) {
SPCurve *c = (SPCurve *) l->data;
memcpy(bp, c->_bpath, c->end * sizeof(NArtBpath));
bp += c->end;
}
bp->code = NR_END;
new_curve->end = length;
gint i;
for (i = new_curve->end; i > 0; i--) {
if ((new_curve->_bpath[i].code == NR_MOVETO) ||
(new_curve->_bpath[i].code == NR_MOVETO_OPEN) )
break;
}
new_curve->substart = i;
return new_curve;
}
/**
* Returns a list of new curves corresponding to the subpaths in \a curve.
*/
GSList *
sp_curve_split(SPCurve const *curve)
{
g_return_val_if_fail(curve != NULL, NULL);
gint p = 0;
GSList *l = NULL;
while (p < curve->end) {
gint i = 1;
while ((curve->_bpath[p + i].code == NR_LINETO) ||
(curve->_bpath[p + i].code == NR_CURVETO))
i++;
SPCurve *new_curve = sp_curve_new_sized(i + 1);
memcpy(new_curve->_bpath, curve->_bpath + p, i * sizeof(NArtBpath));
new_curve->end = i;
new_curve->_bpath[i].code = NR_END;
new_curve->substart = 0;
new_curve->closed = (new_curve->_bpath->code == NR_MOVETO);
new_curve->hascpt = (new_curve->_bpath->code == NR_MOVETO_OPEN);
l = g_slist_prepend(l, new_curve);
p += i;
}
return l;
}
/**
* Transform all paths in curve, template helper.
*/
template<class M>
static void
tmpl_curve_transform(SPCurve *const curve, M const &m)
{
g_return_if_fail(curve != NULL);
for (gint i = 0; i < curve->end; i++) {
NArtBpath *p = curve->_bpath + i;
switch (p->code) {
case NR_MOVETO:
case NR_MOVETO_OPEN:
case NR_LINETO: {
p->setC(3, p->c(3) * m);
break;
}
case NR_CURVETO:
for (unsigned i = 1; i <= 3; ++i) {
p->setC(i, p->c(i) * m);
}
break;
default:
g_warning("Illegal pathcode %d", p->code);
break;
}
}
}
/**
* Transform all paths in curve using matrix.
*/
void
sp_curve_transform(SPCurve *const curve, NR::Matrix const &m)
{
tmpl_curve_transform<NR::Matrix>(curve, m);
}
/**
* Transform all paths in curve using NR::translate.
*/
void
sp_curve_transform(SPCurve *const curve, NR::translate const &m)
{
tmpl_curve_transform<NR::translate>(curve, m);
}
/* Methods */
/**
* Set curve to empty curve.
*/
void
sp_curve_reset(SPCurve *curve)
{
g_return_if_fail(curve != NULL);
curve->_bpath->code = NR_END;
curve->end = 0;
curve->substart = 0;
curve->hascpt = false;
curve->posSet = false;
curve->moving = false;
curve->closed = false;
}
/* Several consecutive movetos are ALLOWED */
/**
* Calls sp_curve_moveto() with point made of given coordinates.
*/
void
sp_curve_moveto(SPCurve *curve, gdouble x, gdouble y)
{
sp_curve_moveto(curve, NR::Point(x, y));
}
/**
* Perform a moveto to a point, thus starting a new subpath.
*/
void
sp_curve_moveto(SPCurve *curve, NR::Point const &p)
{
g_return_if_fail(curve != NULL);
g_return_if_fail(!curve->moving);
curve->substart = curve->end;
curve->hascpt = true;
curve->posSet = true;
curve->movePos = p;
}
/**
* Calls sp_curve_lineto() with a point's coordinates.
*/
void
sp_curve_lineto(SPCurve *curve, NR::Point const &p)
{
sp_curve_lineto(curve, p[NR::X], p[NR::Y]);
}
/**
* Adds a line to the current subpath.
*/
void
sp_curve_lineto(SPCurve *curve, gdouble x, gdouble y)
{
g_return_if_fail(curve != NULL);
g_return_if_fail(curve->hascpt);
if (curve->moving) {
/* fix endpoint */
g_return_if_fail(!curve->posSet);
g_return_if_fail(curve->end > 1);
NArtBpath *bp = curve->_bpath + curve->end - 1;
g_return_if_fail(bp->code == NR_LINETO);
bp->x3 = x;
bp->y3 = y;
curve->moving = false;
return;
}
if (curve->posSet) {
/* start a new segment */
sp_curve_ensure_space(curve, 2);
NArtBpath *bp = curve->_bpath + curve->end;
bp->code = NR_MOVETO_OPEN;
bp->setC(3, curve->movePos);
bp++;
bp->code = NR_LINETO;
bp->x3 = x;
bp->y3 = y;
bp++;
bp->code = NR_END;
curve->end += 2;
curve->posSet = false;
curve->closed = false;
return;
}
/* add line */
g_return_if_fail(curve->end > 1);
sp_curve_ensure_space(curve, 1);
NArtBpath *bp = curve->_bpath + curve->end;
bp->code = NR_LINETO;
bp->x3 = x;
bp->y3 = y;
bp++;
bp->code = NR_END;
curve->end++;
}
/// Unused
void
sp_curve_lineto_moving(SPCurve *curve, gdouble x, gdouble y)
{
g_return_if_fail(curve != NULL);
g_return_if_fail(curve->hascpt);
if (curve->moving) {
/* change endpoint */
g_return_if_fail(!curve->posSet);
g_return_if_fail(curve->end > 1);
NArtBpath *bp = curve->_bpath + curve->end - 1;
g_return_if_fail(bp->code == NR_LINETO);
bp->x3 = x;
bp->y3 = y;
return;
}
if (curve->posSet) {
/* start a new segment */
sp_curve_ensure_space(curve, 2);
NArtBpath *bp = curve->_bpath + curve->end;
bp->code = NR_MOVETO_OPEN;
bp->setC(3, curve->movePos);
bp++;
bp->code = NR_LINETO;
bp->x3 = x;
bp->y3 = y;
bp++;
bp->code = NR_END;
curve->end += 2;
curve->posSet = false;
curve->moving = true;
curve->closed = false;
return;
}
/* add line */
g_return_if_fail(curve->end > 1);
sp_curve_ensure_space(curve, 1);
NArtBpath *bp = curve->_bpath + curve->end;
bp->code = NR_LINETO;
bp->x3 = x;
bp->y3 = y;
bp++;
bp->code = NR_END;
curve->end++;
curve->moving = true;
}
/**
* Calls sp_curve_curveto() with coordinates of three points.
*/
void
sp_curve_curveto(SPCurve *curve, NR::Point const &p0, NR::Point const &p1, NR::Point const &p2)
{
using NR::X;
using NR::Y;
sp_curve_curveto(curve,
p0[X], p0[Y],
p1[X], p1[Y],
p2[X], p2[Y]);
}
/**
* Adds a bezier segment to the current subpath.
*/
void
sp_curve_curveto(SPCurve *curve, gdouble x0, gdouble y0, gdouble x1, gdouble y1, gdouble x2, gdouble y2)
{
g_return_if_fail(curve != NULL);
g_return_if_fail(curve->hascpt);
g_return_if_fail(!curve->moving);
if (curve->posSet) {
/* start a new segment */
sp_curve_ensure_space(curve, 2);
NArtBpath *bp = curve->_bpath + curve->end;
bp->code = NR_MOVETO_OPEN;
bp->setC(3, curve->movePos);
bp++;
bp->code = NR_CURVETO;
bp->x1 = x0;
bp->y1 = y0;
bp->x2 = x1;
bp->y2 = y1;
bp->x3 = x2;
bp->y3 = y2;
bp++;
bp->code = NR_END;
curve->end += 2;
curve->posSet = false;
curve->closed = false;
return;
}
/* add curve */
g_return_if_fail(curve->end > 1);
sp_curve_ensure_space(curve, 1);
NArtBpath *bp = curve->_bpath + curve->end;
bp->code = NR_CURVETO;
bp->x1 = x0;
bp->y1 = y0;
bp->x2 = x1;
bp->y2 = y1;
bp->x3 = x2;
bp->y3 = y2;
bp++;
bp->code = NR_END;
curve->end++;
}
/**
* Close current subpath by possibly adding a line between start and end.
*/
void
sp_curve_closepath(SPCurve *curve)
{
g_return_if_fail(curve != NULL);
g_return_if_fail(curve->hascpt);
g_return_if_fail(!curve->posSet);
g_return_if_fail(!curve->moving);
g_return_if_fail(!curve->closed);
/* We need at least moveto, curveto, end. */
g_return_if_fail(curve->end - curve->substart > 1);
{
NArtBpath *bs = curve->_bpath + curve->substart;
NArtBpath *be = curve->_bpath + curve->end - 1;
if (bs->c(3) != be->c(3)) {
sp_curve_lineto(curve, bs->c(3));
bs = curve->_bpath + curve->substart;
}
bs->code = NR_MOVETO;
}
curve->closed = true;
for (NArtBpath const *bp = curve->_bpath; bp->code != NR_END; bp++) {
/** \todo
* effic: Maintain a count of NR_MOVETO_OPEN's (e.g. instead of
* the closed boolean).
*/
if (bp->code == NR_MOVETO_OPEN) {
curve->closed = false;
break;
}
}
curve->hascpt = false;
}
/** Like sp_curve_closepath() but sets the end point of the current
command to the subpath start point instead of adding a new lineto.
Used for freehand drawing when the user draws back to the start point.
**/
void
sp_curve_closepath_current(SPCurve *curve)
{
g_return_if_fail(curve != NULL);
g_return_if_fail(curve->hascpt);
g_return_if_fail(!curve->posSet);
g_return_if_fail(!curve->closed);
/* We need at least moveto, curveto, end. */
g_return_if_fail(curve->end - curve->substart > 1);
{
NArtBpath *bs = curve->_bpath + curve->substart;
NArtBpath *be = curve->_bpath + curve->end - 1;
be->x3 = bs->x3;
be->y3 = bs->y3;
bs->code = NR_MOVETO;
}
curve->closed = true;
for (NArtBpath const *bp = curve->_bpath; bp->code != NR_END; bp++) {
/** \todo
* effic: Maintain a count of NR_MOVETO_OPEN's (e.g. instead of
* the closed boolean).
*/
if (bp->code == NR_MOVETO_OPEN) {
curve->closed = false;
break;
}
}
curve->hascpt = false;
curve->moving = false;
}
/**
* True if no paths are in curve.
*/
bool
sp_curve_empty(SPCurve *curve)
{
g_return_val_if_fail(curve != NULL, TRUE);
return (curve->_bpath->code == NR_END);
}
/**
* Return last subpath or NULL.
*/
NArtBpath *
sp_curve_last_bpath(SPCurve const *curve)
{
g_return_val_if_fail(curve != NULL, NULL);
if (curve->end == 0) {
return NULL;
}
return curve->_bpath + curve->end - 1;
}
/**
* Return first subpath or NULL.
*/
NArtBpath *
sp_curve_first_bpath(SPCurve const *curve)
{
g_return_val_if_fail(curve != NULL, NULL);
if (curve->end == 0) {
return NULL;
}
return curve->_bpath;
}
/**
* Return first point of first subpath or (0,0).
*/
NR::Point
sp_curve_first_point(SPCurve const *const curve)
{
NArtBpath *const bpath = sp_curve_first_bpath(curve);
g_return_val_if_fail(bpath != NULL, NR::Point(0, 0));
return bpath->c(3);
}
/**
* Return the second point of first subpath or curve->movePos if curve too short.
*/
NR::Point
sp_curve_second_point(SPCurve const *const curve)
{
g_return_val_if_fail(curve != NULL, NR::Point(0, 0));
if (curve->end < 1) {
return curve->movePos;
}
NArtBpath *bpath = NULL;
if (curve->end < 2) {
bpath = curve->_bpath;
} else {
bpath = curve->_bpath + 1;
}
g_return_val_if_fail(bpath != NULL, NR::Point(0, 0));
return bpath->c(3);
}
/**
* Return the second-last point of last subpath or curve->movePos if curve too short.
*/
NR::Point
sp_curve_penultimate_point(SPCurve const *const curve)
{
g_return_val_if_fail(curve != NULL, NR::Point(0, 0));
if (curve->end < 2) {
return curve->movePos;
}
NArtBpath *const bpath = curve->_bpath + curve->end - 2;
g_return_val_if_fail(bpath != NULL, NR::Point(0, 0));
return bpath->c(3);
}
/**
* Return last point of last subpath or (0,0).
*/
NR::Point
sp_curve_last_point(SPCurve const *const curve)
{
NArtBpath *const bpath = sp_curve_last_bpath(curve);
g_return_val_if_fail(bpath != NULL, NR::Point(0, 0));
return bpath->c(3);
}
inline static bool
is_moveto(NRPathcode const c)
{
return c == NR_MOVETO || c == NR_MOVETO_OPEN;
}
/**
* Returns \a curve but drawn in the opposite direction.
* Should result in the same shape, but
* with all its markers drawn facing the other direction.
**/
SPCurve *
sp_curve_reverse(SPCurve const *curve)
{
/* We need at least moveto, curveto, end. */
g_return_val_if_fail(curve->end - curve->substart > 1, NULL);
NArtBpath const *be = curve->_bpath + curve->end - 1;
g_assert(is_moveto(curve->_bpath[curve->substart].code));
g_assert(is_moveto(curve->_bpath[0].code));
g_assert((be+1)->code == NR_END);
SPCurve *new_curve = sp_curve_new_sized(curve->length);
sp_curve_moveto(new_curve, be->c(3));
for (NArtBpath const *bp = be; ; --bp) {
switch (bp->code) {
case NR_MOVETO:
g_assert(new_curve->_bpath[new_curve->substart].code == NR_MOVETO_OPEN);
new_curve->_bpath[new_curve->substart].code = NR_MOVETO;
/* FALL-THROUGH */
case NR_MOVETO_OPEN:
if (bp == curve->_bpath) {
return new_curve;
}
sp_curve_moveto(new_curve, (bp-1)->c(3));
break;
case NR_LINETO:
sp_curve_lineto(new_curve, (bp-1)->c(3));
break;
case NR_CURVETO:
sp_curve_curveto(new_curve, bp->c(2), bp->c(1), (bp-1)->c(3));
break;
default:
g_assert_not_reached();
}
}
}
/**
* Append \a curve2 to \a curve.
*/
void
sp_curve_append(SPCurve *curve,
SPCurve const *curve2,
bool use_lineto)
{
g_return_if_fail(curve != NULL);
g_return_if_fail(curve2 != NULL);
if (curve2->end < 1)
return;
NArtBpath const *bs = curve2->_bpath;
bool closed = curve->closed;
for (NArtBpath const *bp = bs; bp->code != NR_END; bp++) {
switch (bp->code) {
case NR_MOVETO_OPEN:
if (use_lineto && curve->hascpt) {
sp_curve_lineto(curve, bp->x3, bp->y3);
use_lineto = FALSE;
} else {
if (closed) sp_curve_closepath(curve);
sp_curve_moveto(curve, bp->x3, bp->y3);
}
closed = false;
break;
case NR_MOVETO:
if (use_lineto && curve->hascpt) {
sp_curve_lineto(curve, bp->x3, bp->y3);
use_lineto = FALSE;
} else {
if (closed) sp_curve_closepath(curve);
sp_curve_moveto(curve, bp->x3, bp->y3);
}
closed = true;
break;
case NR_LINETO:
sp_curve_lineto(curve, bp->x3, bp->y3);
break;
case NR_CURVETO:
sp_curve_curveto(curve, bp->x1, bp->y1, bp->x2, bp->y2, bp->x3, bp->y3);
break;
case NR_END:
g_assert_not_reached();
}
}
if (closed) {
sp_curve_closepath(curve);
}
}
/**
* Append \a c1 to \a c0 with possible fusing of close endpoints.
*/
SPCurve *
sp_curve_append_continuous(SPCurve *c0, SPCurve const *c1, gdouble tolerance)
{
g_return_val_if_fail(c0 != NULL, NULL);
g_return_val_if_fail(c1 != NULL, NULL);
g_return_val_if_fail(!c0->closed, NULL);
g_return_val_if_fail(!c1->closed, NULL);
if (c1->end < 1) {
return c0;
}
NArtBpath *be = sp_curve_last_bpath(c0);
if (be) {
NArtBpath const *bs = sp_curve_first_bpath(c1);
if ( bs
&& ( fabs( bs->x3 - be->x3 ) <= tolerance )
&& ( fabs( bs->y3 - be->y3 ) <= tolerance ) )
{
/** \todo
* fixme: Strictly we mess in case of multisegment mixed
* open/close curves
*/
bool closed = false;
for (bs = bs + 1; bs->code != NR_END; bs++) {
switch (bs->code) {
case NR_MOVETO_OPEN:
if (closed) sp_curve_closepath(c0);
sp_curve_moveto(c0, bs->x3, bs->y3);
closed = false;
break;
case NR_MOVETO:
if (closed) sp_curve_closepath(c0);
sp_curve_moveto(c0, bs->x3, bs->y3);
closed = true;
break;
case NR_LINETO:
sp_curve_lineto(c0, bs->x3, bs->y3);
break;
case NR_CURVETO:
sp_curve_curveto(c0, bs->x1, bs->y1, bs->x2, bs->y2, bs->x3, bs->y3);
break;
case NR_END:
g_assert_not_reached();
}
}
} else {
sp_curve_append(c0, c1, TRUE);
}
} else {
sp_curve_append(c0, c1, TRUE);
}
return c0;
}
/**
* Remove last segment of curve.
*/
void
sp_curve_backspace(SPCurve *curve)
{
g_return_if_fail(curve != NULL);
if (curve->end > 0) {
curve->end -= 1;
if (curve->end > 0) {
NArtBpath *bp = curve->_bpath + curve->end - 1;
if ((bp->code == NR_MOVETO) ||
(bp->code == NR_MOVETO_OPEN) )
{
curve->hascpt = true;
curve->posSet = true;
curve->closed = false;
curve->movePos = bp->c(3);
curve->end -= 1;
}
}
curve->_bpath[curve->end].code = NR_END;
}
}
/* Private methods */
/**
* True if all subpaths in bpath array pass consistency check.
*/
static bool sp_bpath_good(NArtBpath const bpath[])
{
g_return_val_if_fail(bpath != NULL, FALSE);
NArtBpath const *bp = bpath;
while (bp->code != NR_END) {
bp = sp_bpath_check_subpath(bp);
if (bp == NULL)
return false;
}
return true;
}
/**
* Return copy of a bpath array, discarding any inconsistencies.
*/
static NArtBpath *sp_bpath_clean(NArtBpath const bpath[])
{
NArtBpath *new_bpath = g_new(NArtBpath, sp_bpath_length(bpath));
NArtBpath const *bp = bpath;
NArtBpath *np = new_bpath;
while (bp->code != NR_END) {
if (sp_bpath_check_subpath(bp)) {
*np++ = *bp++;
while ((bp->code == NR_LINETO) ||
(bp->code == NR_CURVETO))
*np++ = *bp++;
} else {
bp++;
while ((bp->code == NR_LINETO) ||
(bp->code == NR_CURVETO))
bp++;
}
}
if (np == new_bpath) {
g_free(new_bpath);
return NULL;
}
np->code = NR_END;
np += 1;
new_bpath = g_renew(NArtBpath, new_bpath, np - new_bpath);
return new_bpath;
}
/**
* Perform consistency check of bpath array.
* \return Address of NR_END node or NULL.
*/
static NArtBpath const *sp_bpath_check_subpath(NArtBpath const bpath[])
{
g_return_val_if_fail(bpath != NULL, NULL);
bool closed;
if (bpath->code == NR_MOVETO) {
closed = true;
} else if (bpath->code == NR_MOVETO_OPEN) {
closed = false;
} else {
return NULL;
}
gint len = 0;
gint i;
/** \todo
* effic: consider checking for END/MOVE/MOVETO inside switch block
*/
for (i = 1; (bpath[i].code != NR_END) && (bpath[i].code != NR_MOVETO) && (bpath[i].code != NR_MOVETO_OPEN); i++) {
switch (bpath[i].code) {
case NR_LINETO:
case NR_CURVETO:
len++;
break;
default:
return NULL;
}
}
if (closed) {
if (len < 1)
return NULL;
if ((bpath->x3 != bpath[i-1].x3) || (bpath->y3 != bpath[i-1].y3))
return NULL;
} else {
if (len < 1)
return NULL;
}
return bpath + i;
}
/**
* Returns index of first NR_END bpath in array.
*/
static unsigned sp_bpath_length(NArtBpath const bpath[])
{
g_return_val_if_fail(bpath != NULL, FALSE);
unsigned ret = 0;
while ( bpath[ret].code != NR_END ) {
++ret;
}
++ret;
return ret;
}
/**
* \brief
*
* \todo
* fixme: this is bogus -- it doesn't check for nr_moveto, which will indicate
* a closing of the subpath it's nonsense to talk about a path as a whole
* being closed, although maybe someone would want that for some other reason?
* Oh, also, if the bpath just ends, then it's *open*. I hope nobody is using
* this code for anything.
*/
static bool sp_bpath_closed(NArtBpath const bpath[])
{
g_return_val_if_fail(bpath != NULL, FALSE);
for (NArtBpath const *bp = bpath; bp->code != NR_END; bp++) {
if (bp->code == NR_MOVETO_OPEN) {
return false;
}
}
return true;
}
/**
* Returns length of bezier segment.
*/
static double
bezier_len(NR::Point const &c0,
NR::Point const &c1,
NR::Point const &c2,
NR::Point const &c3,
double const threshold)
{
/** \todo
* The SVG spec claims that a closed form exists, but for the moment I'll
* use a stupid algorithm.
*/
double const lbound = L2( c3 - c0 );
double const ubound = L2( c1 - c0 ) + L2( c2 - c1 ) + L2( c3 - c2 );
double ret;
if ( ubound - lbound <= threshold ) {
ret = .5 * ( lbound + ubound );
} else {
NR::Point const a1( .5 * ( c0 + c1 ) );
NR::Point const b2( .5 * ( c2 + c3 ) );
NR::Point const c12( .5 * ( c1 + c2 ) );
NR::Point const a2( .5 * ( a1 + c12 ) );
NR::Point const b1( .5 * ( c12 + b2 ) );
NR::Point const midpoint( .5 * ( a2 + b1 ) );
double const rec_threshold = .625 * threshold;
ret = bezier_len(c0, a1, a2, midpoint, rec_threshold) + bezier_len(midpoint, b1, b2, c3, rec_threshold);
if (!(lbound - 1e-2 <= ret && ret <= ubound + 1e-2)) {
using NR::X; using NR::Y;
g_warning("ret=%f outside of expected bounds [%f, %f] for {(%.0f %.0f) (%.0f %.0f) (%.0f %.0f) (%.0f %.0f)}",
ret, lbound, ubound, c0[X], c0[Y], c1[X], c1[Y], c2[X], c2[Y], c3[X], c3[Y]);
}
}
return ret;
}
/**
* Returns total length of curve, excluding length of closepath segments.
*/
static double
sp_curve_distance_including_space(SPCurve const *const curve, double seg2len[])
{
g_return_val_if_fail(curve != NULL, 0.);
double ret = 0.0;
if ( curve->_bpath->code == NR_END ) {
return ret;
}
NR::Point prev(curve->_bpath->c(3));
for (gint i = 1; i < curve->end; ++i) {
NArtBpath &p = curve->_bpath[i];
double seg_len = 0;
switch (p.code) {
case NR_MOVETO_OPEN:
case NR_MOVETO:
case NR_LINETO:
seg_len = L2(p.c(3) - prev);
break;
case NR_CURVETO:
seg_len = bezier_len(prev, p.c(1), p.c(2), p.c(3), 1.);
break;
case NR_END:
return ret;
}
seg2len[i - 1] = seg_len;
ret += seg_len;
prev = p.c(3);
}
g_assert(!(ret < 0));
return ret;
}
/**
* Like sp_curve_distance_including_space(), but ensures that the
* result >= 1e-18: uses 1 per segment if necessary.
*/
static double
sp_curve_nonzero_distance_including_space(SPCurve const *const curve, double seg2len[])
{
double const real_dist(sp_curve_distance_including_space(curve, seg2len));
if (real_dist >= 1e-18) {
return real_dist;
} else {
unsigned const nSegs = SP_CURVE_LENGTH(curve) - 1;
for (unsigned i = 0; i < nSegs; ++i) {
seg2len[i] = 1.;
}
return (double) nSegs;
}
}
void
sp_curve_stretch_endpoints(SPCurve *curve, NR::Point const &new_p0, NR::Point const &new_p1)
{
if (sp_curve_empty(curve)) {
return;
}
g_assert(unsigned(SP_CURVE_LENGTH(curve)) + 1 == sp_bpath_length(curve->_bpath));
unsigned const nSegs = SP_CURVE_LENGTH(curve) - 1;
g_assert(nSegs != 0);
double *const seg2len = new double[nSegs];
double const tot_len = sp_curve_nonzero_distance_including_space(curve, seg2len);
NR::Point const offset0( new_p0 - sp_curve_first_point(curve) );
NR::Point const offset1( new_p1 - sp_curve_last_point(curve) );
curve->_bpath->setC(3, new_p0);
double begin_dist = 0.;
for (unsigned si = 0; si < nSegs; ++si) {
double const end_dist = begin_dist + seg2len[si];
NArtBpath &p = curve->_bpath[1 + si];
switch (p.code) {
case NR_LINETO:
case NR_MOVETO:
case NR_MOVETO_OPEN:
p.setC(3, p.c(3) + NR::Lerp(end_dist / tot_len, offset0, offset1));
break;
case NR_CURVETO:
for (unsigned ci = 1; ci <= 3; ++ci) {
p.setC(ci, p.c(ci) + Lerp((begin_dist + ci * seg2len[si] / 3.) / tot_len, offset0, offset1));
}
break;
default:
g_assert_not_reached();
}
begin_dist = end_dist;
}
g_assert(L1(curve->_bpath[nSegs].c(3) - new_p1) < 1.);
/* Explicit set for better numerical properties. */
curve->_bpath[nSegs].setC(3, new_p1);
delete [] seg2len;
}
void
sp_curve_move_endpoints(SPCurve *curve, NR::Point const &new_p0,
NR::Point const &new_p1)
{
if (sp_curve_empty(curve)) {
return;
}
unsigned const nSegs = SP_CURVE_LENGTH(curve) - 1;
g_assert(nSegs != 0);
curve->_bpath->setC(3, new_p0);
curve->_bpath[nSegs].setC(3, new_p1);
}
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
|