summaryrefslogtreecommitdiffstats
path: root/src/extension/internal/javafx-out.cpp
blob: 8399d602f3ed297da3b59131b7c0ca17ad4827ef (plain)
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
/*
 * A simple utility for exporting Inkscape svg Shapes as JavaFX paths.
 *
 *  For information on the JavaFX file format, see:
 *      https://openjfx.dev.java.net/
 *
 * Authors:
 *   Bob Jamison <ishmal@inkscape.org>
 *   Silveira Neto <silveiraneto@gmail.com>
 *   Jim Clarke <Jim.Clarke@sun.com>
 *   Jon A. Cruz <jon@joncruz.org>
 *   Abhishek Sharma
 *
 * Copyright (C) 2008,2009 Authors
 *
 * Released under GNU GPL, read the file 'COPYING' for more information
 */


#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "javafx-out.h"
#include <inkscape.h>
#include <inkscape-version.h>
#include <sp-path.h>
#include <sp-linear-gradient.h>
#include <sp-radial-gradient.h>
#include <style.h>
#include <display/curve.h>
#include <display/canvas-bpath.h>
#include <svg/svg.h>
#include <extension/system.h>
#include <2geom/pathvector.h>
#include <2geom/rect.h>
#include <2geom/bezier-curve.h>
#include <2geom/hvlinesegment.h>
#include "helper/geom.h"
#include "helper/geom-curves.h"
#include <io/sys.h>
#include "sp-root.h"

#include <string>
#include <stdio.h>
#include <stdarg.h>


namespace Inkscape
{
namespace Extension
{
namespace Internal
{




//########################################################################
//# M E S S A G E S
//########################################################################

static void err(const char *fmt, ...)
{
    va_list args;
    g_log(NULL,  G_LOG_LEVEL_WARNING, "javafx-out err: ");
    va_start(args, fmt);
    g_logv(NULL, G_LOG_LEVEL_WARNING, fmt, args);
    va_end(args);
    g_log(NULL,  G_LOG_LEVEL_WARNING, "\n");
}


//########################################################################
//# U T I L I T Y
//########################################################################

/**
 * Got this method from Bulia, and modified it a bit.  It basically
 * starts with this style, gets its SPObject parent, walks up the object
 * tree and finds all of the opacities and multiplies them.
 *
 * We use this for our "flat" object output.  If the code is modified
 * to reflect a tree of <groups>, then this will be unneccessary.
 */
static double effective_opacity(const SPStyle *style)
{
    double val = 1.0;
    for (SPObject const *obj = style->object; obj ; obj = obj->parent)
    {
        if (obj->style) {
            val *= SP_SCALE24_TO_FLOAT(obj->style->opacity.value);
        }
    }
    return val;
}

//########################################################################
//# OUTPUT FORMATTING
//########################################################################


/**
 * We want to control floating output format.
 * Especially to avoid localization. (decimal ',' for example)
 */
static JavaFXOutput::String dstr(double d)
{
    char dbuf[G_ASCII_DTOSTR_BUF_SIZE+1];
    g_ascii_formatd(dbuf, G_ASCII_DTOSTR_BUF_SIZE,
                  "%.8f", (gdouble)d);
    JavaFXOutput::String s = dbuf;
    return s;
}

#define DSTR(d) (dstr(d).c_str())


/**
 * Format a double as an integer
 */
static JavaFXOutput::String istr(double d)
{
    char dbuf[G_ASCII_DTOSTR_BUF_SIZE+1];
    g_ascii_formatd(dbuf, G_ASCII_DTOSTR_BUF_SIZE,
                  "%.0f", (gdouble)d);
    JavaFXOutput::String s = dbuf;
    return s;
}

#define ISTR(d) (istr(d).c_str())


/**
 * Format an rgba() string
 */
static JavaFXOutput::String rgba(guint32 rgba)
{
    unsigned int r = SP_RGBA32_R_U(rgba);
    unsigned int g = SP_RGBA32_G_U(rgba);
    unsigned int b = SP_RGBA32_B_U(rgba);
    unsigned int a = SP_RGBA32_A_U(rgba);
    char buf[80];
    snprintf(buf, 79, "Color.rgb(0x%02x, 0x%02x, 0x%02x, %s)",
                           r, g, b, DSTR((double)a/255.0));
    JavaFXOutput::String s = buf;
    return s;
}


/**
 * Format an rgba() string for a color and a 0.0-1.0 alpha
 */
static JavaFXOutput::String rgba(SPColor color, gdouble alpha)
{
    return rgba(color.toRGBA32(alpha));
}

/**
 * Map Inkscape linecap styles to JavaFX
 */
static JavaFXOutput::String getStrokeLineCap(unsigned value) {
    switch(value) {
        case SP_STROKE_LINECAP_BUTT:
            return "StrokeLineCap.BUTT";
        case SP_STROKE_LINECAP_ROUND:
            return "StrokeLineCap.ROUND";
        case SP_STROKE_LINECAP_SQUARE:
            return "StrokeLineCap.SQUARE";
        default:
            return "INVALID LINE CAP";
    }
}


/**
 * Map Inkscape linejoin styles to JavaFX
 */
static JavaFXOutput::String getStrokeLineJoin(unsigned value) {
    switch(value) {
        case SP_STROKE_LINEJOIN_MITER:
            return "StrokeLineJoin.MITER";
        case SP_STROKE_LINEJOIN_ROUND:
            return "StrokeLineJoin.ROUND";
        case SP_STROKE_LINEJOIN_BEVEL:
            return "StrokeLineJoin.BEVEL";
        default:
            return "INVALID LINE JOIN";
    }
}


/**
 * Replace illegal characters for JavaFX for a underscore.
 */
static JavaFXOutput::String sanatize(const JavaFXOutput::String &badstr){
    JavaFXOutput::String good(badstr);
    for (int pos = 0; pos < static_cast<int>(badstr.length()); ++pos )
        if ((badstr.at(pos)=='-')||(badstr.at(pos)==' ')) {
            good.replace(pos, 1, "_");
        }
    return good;
}

/**
 *  Output data to the buffer, printf()-style
 */
void JavaFXOutput::out(const char *fmt, ...)
{
    va_list args;
    va_start(args, fmt);
    gchar *output = g_strdup_vprintf(fmt, args);
    va_end(args);
    outbuf.append(output);
    g_free(output);
}



/**
 * Output the file header
 */
bool JavaFXOutput::doHeader()
{
    time_t tim = time(NULL);
    out("/*###################################################################\n");
    out("### This JavaFX document was generated by Inkscape\n");
    out("### http://www.inkscape.org\n");
    out("### Created: %s",   ctime(&tim));
    out("### Version: %s\n", Inkscape::version_string);
    out("#####################################################################\n");
    out("### NOTES:\n");
    out("### ============\n");
    out("### JavaFX information can be found at\n");
    out("### http://www.javafx.com/\n");
    out("###\n");
    out("### If you have any problems with this output, please see the\n");
    out("### Inkscape project at http://www.inkscape.org, or visit\n");
    out("### the #inkscape channel on irc.freenode.net . \n");
    out("###\n");
    out("###################################################################*/\n");
    out("\n\n");
    out("/*###################################################################\n");
    out("##   Exports in this file\n");
    out("##==========================\n");
    out("##    Shapes   : %d\n", nrShapes);
    out("##    Nodes    : %d\n", nrNodes);
    out("###################################################################*/\n");
    out("\n\n");

    // import javafx libraries we can need
    out("import javafx.scene.*;\n");
    out("import javafx.scene.shape.*;\n");
    out("import javafx.scene.transform.*;\n");
    out("import javafx.scene.paint.*;\n");
    out("\n");

    out("\n\n");

    // Creates a class extended from CustomNode
    out("public class %s extends CustomNode {\n", name.c_str());

    return true;
}



/**
 *  Output the file footer
 */
bool JavaFXOutput::doTail()
{
    float border = 25.0;

    // Write the tail of CustomNode
    out("           ] // content\n");
    out("           transforms: Translate { x : %s, y : %s }\n",
        DSTR((-minx) + border), DSTR((-miny) + border) );
    out("       } // Group\n");
    out("   } // function create()\n");
    out("} // class %s\n", name.c_str());
    out("\n");

    // Stage
//     out("    stage: Stage {\n");
//     out("        content: %s{}\n", name.c_str());
//     out("    } // Stage\n");


    out("\n");

    out("/*###################################################################\n");
    out("### E N D   C L A S S    %s\n", name.c_str());
    out("###################################################################*/\n");
    out("\n\n");
    return true;
}



/**
 *  Output gradient information to the buffer
 */
bool JavaFXOutput::doGradient(SPGradient *grad, const String &id)
{
    String jfxid = sanatize(id);

    if (SP_IS_LINEARGRADIENT(grad))
        {
        SPLinearGradient *g = SP_LINEARGRADIENT(grad);
        out("    /* create LinearGradient for %s */\n", jfxid.c_str());
        out("    function %s(): LinearGradient {\n",  jfxid.c_str());
        out("        LinearGradient {\n");
        std::vector<SPGradientStop> stops = g->vector.stops;
        if (stops.size() > 0)
            {
            out("            stops:\n");
            out("                [\n");
            for (unsigned int i = 0 ; i<stops.size() ; i++)
                {
                SPGradientStop stop = stops[i];
                out("                Stop {\n");
                out("                    offset: %s\n", DSTR(stop.offset));
                out("                    color: %s\n",  rgba(stop.color, stop.opacity).c_str());
                out("                },\n");
                }
            out("            ]\n");
            }
        out("        };\n");
        out("    } // end LinearGradient: %s\n", jfxid.c_str());
        out("\n\n");
        }
    else if (SP_IS_RADIALGRADIENT(grad))
        {
        SPRadialGradient *g = SP_RADIALGRADIENT(grad);
        out("    /* create RadialGradient for %s */\n", jfxid.c_str());
        out("    function %s() {\n", jfxid.c_str());
        out("        RadialGradient {\n");
        out("            centerX: %s\n", DSTR(g->cx.value));
        out("            centerY: %s\n", DSTR(g->cy.value));
        out("            focusX: %s\n",  DSTR(g->fx.value));
        out("            focusY: %s\n",  DSTR(g->fy.value));
        out("            radius: %s\n",  DSTR(g->r.value ));
        std::vector<SPGradientStop> stops = g->vector.stops;
        if (stops.size() > 0)
            {
            out("            stops:\n");
            out("            [\n");
            for (unsigned int i = 0 ; i<stops.size() ; i++)
                {
                SPGradientStop stop = stops[i];
                out("                Stop {\n");
                out("                    offset: %s\n", DSTR(stop.offset));
                out("                    color: %s\n",  rgba(stop.color, stop.opacity).c_str());
                out("                },\n");
                }
            out("            ]\n");
            }
        out("        };\n");
        out("    } // end RadialGradient: %s\n", jfxid.c_str());
        out("\n\n");
        }
    else
        {
        err("Unknown gradient type for '%s'\n", jfxid.c_str());
        return false;
        }


    return true;
}




/**
 *  Output an element's style attribute
 */
bool JavaFXOutput::doStyle(SPStyle *style)
{
    if (!style) {
        return true;
    }

    out("            opacity: %s\n", DSTR(effective_opacity(style)));

    /**
     * Fill
     */
    SPIPaint const &fill = style->fill;
    if (fill.isColor())
        {
        // see color.h for how to parse SPColor
        out("            fill: %s\n",
            rgba(fill.value.color, SP_SCALE24_TO_FLOAT(style->fill_opacity.value)).c_str());
        }
    else if (fill.isPaintserver()){
        if (fill.value.href && fill.value.href->getURI() ){
            String uri = fill.value.href->getURI()->toString();
            /* trim the anchor '#' from the front */
            if (uri.size() > 0 && uri[0]=='#') {
                uri = uri.substr(1);
            }
            out("            fill: %s()\n", sanatize(uri).c_str());
        }
    }


    /**
     * Stroke
     */
    /**
     *NOTE:  Things in style we can use:
     * SPIPaint stroke;
     * SPILength stroke_width;
     * SPIEnum stroke_linecap;
     * SPIEnum stroke_linejoin;
     * SPIFloat stroke_miterlimit;
     * NRVpathDash stroke_dash;
     * unsigned stroke_dasharray_set : 1;
     * unsigned stroke_dasharray_inherit : 1;
     * unsigned stroke_dashoffset_set : 1;
     * SPIScale24 stroke_opacity;
     */
    if (style->stroke_opacity.value > 0)
        {
        SPIPaint const &stroke = style->stroke;
        out("            stroke: %s\n",
            rgba(stroke.value.color, SP_SCALE24_TO_FLOAT(style->stroke_opacity.value)).c_str());
        double strokewidth = style->stroke_width.value;
        unsigned linecap   = style->stroke_linecap.value;
        unsigned linejoin  = style->stroke_linejoin.value;
        out("            strokeWidth: %s\n",      DSTR(strokewidth));
        out("            strokeLineCap: %s\n",    getStrokeLineCap(linecap).c_str());
        out("            strokeLineJoin: %s\n",   getStrokeLineJoin(linejoin).c_str());
        out("            strokeMiterLimit: %s\n", DSTR(style->stroke_miterlimit.value));
        if (style->stroke_dasharray_set) {
           if (style->stroke_dashoffset_set) {
               out("            strokeDashOffset: %s\n", DSTR(style->stroke_dash.offset));
           }
           out("            strokeDashArray: [ ");
           for(int i = 0; i < style->stroke_dash.n_dash; i++ ) {
               if (i > 0) {
                   out(", %.2lf", style->stroke_dash.dash[i]);
               }else {
                   out(" %.2lf", style->stroke_dash.dash[i]);
               }
           }
           out(" ]\n");
        }

        }

    return true;
}


#if 1

/**
 *  Output the curve data to buffer
 */
bool JavaFXOutput::doCurve(SPItem *item, const String &id)
{
    using Geom::X;
    using Geom::Y;

    String jfxid = sanatize(id);

    //### Get the Shape
    if (!SP_IS_SHAPE(item)) { //Bulia's suggestion.  Allow all shapes
        return true;
    }

    SPShape *shape = SP_SHAPE(item);
    SPCurve *curve = shape->curve;
    if (curve->is_empty()) {
        return true;
    }

    nrShapes++;

    out("    /** path %s */\n", jfxid.c_str());
    out("    function %s() : Path {\n",jfxid.c_str());
    out("        Path {\n");
    out("            id: \"%s\"\n", jfxid.c_str());

    /**
     * Output the style information
     */
    if (!doStyle(shape->style)) {
        return false;
    }

    // convert the path to only lineto's and cubic curveto's:
    Geom::Scale yflip(1.0, -1.0);
    Geom::Affine tf = item->i2d_affine() * yflip;
    Geom::PathVector pathv = pathv_to_linear_and_cubic_beziers( curve->get_pathvector() * tf );

    //Count the NR_CURVETOs/LINETOs (including closing line segment)
    guint segmentCount = 0;
    for(Geom::PathVector::const_iterator it = pathv.begin(); it != pathv.end(); ++it) {
        segmentCount += (*it).size();
        if (it->closed()) {
            segmentCount += 1;
        }
    }

    out("            elements: [\n");

    unsigned int segmentNr = 0;

    nrNodes += segmentCount;

    Geom::Rect cminmax( pathv.front().initialPoint(), pathv.front().initialPoint() );

    /**
     * For all Subpaths in the <path>
     */
    for (Geom::PathVector::const_iterator pit = pathv.begin(); pit != pathv.end(); ++pit)
        {
        Geom::Point p = pit->front().initialPoint();
        cminmax.expandTo(p);
        out("                MoveTo {\n");
        out("                    x: %s\n", DSTR(p[X]));
        out("                    y: %s\n", DSTR(p[Y]));
        out("                },\n");

        /**
         * For all segments in the subpath
         */
        for (Geom::Path::const_iterator cit = pit->begin(); cit != pit->end_closed(); ++cit)
            {
            //### LINE
            if ( dynamic_cast<Geom::LineSegment  const *> (&*cit) ||
                dynamic_cast<Geom::HLineSegment const *> (&*cit) ||
                dynamic_cast<Geom::VLineSegment const *> (&*cit) )
                {
                Geom::Point p = cit->finalPoint();
                out("                LineTo {\n");
                out("                    x: %s\n", DSTR(p[X]));
                out("                    y: %s\n", DSTR(p[Y]));
                out("                },\n");
                nrNodes++;
                }
            //### BEZIER
            else if (Geom::CubicBezier const *cubic = dynamic_cast<Geom::CubicBezier const*>(&*cit))
                {
                std::vector<Geom::Point> points = cubic->points();
                Geom::Point p1 = points[1];
                Geom::Point p2 = points[2];
                Geom::Point p3 = points[3];
                out("                CubicCurveTo {\n");
                out("                    controlX1: %s\n", DSTR(p1[X]));
                out("                    controlY1: %s\n", DSTR(p1[Y]));
                out("                    controlX2: %s\n", DSTR(p2[X]));
                out("                    controlY2: %s\n", DSTR(p2[Y]));
                out("                    x: %s\n",         DSTR(p3[X]));
                out("                    y: %s\n",         DSTR(p3[Y]));
                out("                },\n");
                nrNodes++;
                }
            else
                {
                g_error ("logical error, because pathv_to_linear_and_cubic_beziers was used");
                }
            segmentNr++;
            cminmax.expandTo(cit->finalPoint());
            }
        if (pit->closed())
            {
            out("                ClosePath {},\n");
            }
        }

    out("            ] // elements\n");
    out("        }; // Path\n");
    out("    } // end path %s\n\n", jfxid.c_str());

    double cminx = cminmax.min()[X];
    double cmaxx = cminmax.max()[X];
    double cminy = cminmax.min()[Y];
    double cmaxy = cminmax.max()[Y];

    if (cminx < minx) {
        minx = cminx;
    }
    if (cmaxx > maxx) {
        maxx = cmaxx;
    }
    if (cminy < miny) {
        miny = cminy;
    }
    if (cmaxy > maxy) {
        maxy = cmaxy;
    }

    return true;
}



#else

/**
 *  Output the curve data to buffer
 */
bool JavaFXOutput::doCurve(SPItem *item, const String &id)
{
    using Geom::X;
    using Geom::Y;

    //### Get the Shape
    if (!SP_IS_SHAPE(item)) { //Bulia's suggestion.  Allow all shapes
        return true;
    }

    SPShape *shape = SP_SHAPE(item);
    SPCurve *curve = shape->curve;
    if (curve->is_empty()) {
        return true;
    }

    nrShapes++;

    out("        SVGPath \n");
    out("        {\n");
    out("        id: \"%s\"\n", id.c_str());

    /**
     * Output the style information
     */
    if (!doStyle(shape->style)) {
        return false;
    }

    // convert the path to only lineto's and cubic curveto's:
    Geom::Scale yflip(1.0, -1.0);
    Geom::Affine tf = item->i2d_affine() * yflip;
    Geom::PathVector pathv = pathv_to_linear_and_cubic_beziers( curve->get_pathvector() * tf );

    //Count the NR_CURVETOs/LINETOs (including closing line segment)
    nrNodes = 0;
    for(Geom::PathVector::const_iterator it = pathv.begin(); it != pathv.end(); ++it) {
        nrNodes += (*it).size();
        if (it->closed()) {
            nrNodes += 1;
        }
    }

    char *dataStr = sp_svg_write_path(pathv);
    out("        content: \"%s\"\n", dataStr);
    free(dataStr);

    Geom::Rect cminmax( pathv.front().initialPoint(), pathv.front().initialPoint() );

    /**
     * Get the Min and Max X and Y extends for the Path.
     * ....For all Subpaths in the <path>
     */
    for (Geom::PathVector::const_iterator pit = pathv.begin(); pit != pathv.end(); ++pit)
        {
        cminmax.expandTo(pit->front().initialPoint());
        /**
         * For all segments in the subpath
         */
        for (Geom::Path::const_iterator cit = pit->begin(); cit != pit->end_closed(); ++cit)
            {
            cminmax.expandTo(cit->finalPoint());
            }
        }

    out("        },\n");

    double cminx = cminmax.min()[X];
    double cmaxx = cminmax.max()[X];
    double cminy = cminmax.min()[Y];
    double cmaxy = cminmax.max()[Y];

    if (cminx < minx) {
        minx = cminx;
    }
    if (cmaxx > maxx) {
        maxx = cmaxx;
    }
    if (cminy < miny) {
        miny = cminy;
    }
    if (cmaxy > maxy) {
        maxy = cmaxy;
    }

    return true;
}



#endif  /* #if o */



/**
 *  Output the tree data to buffer
 */
bool JavaFXOutput::doTreeRecursive(SPDocument *doc, SPObject *obj)
{
    /**
     * Check the type of node and process
     */
    String id;
    if (!obj->getId())
        {
        char buf[16];
        sprintf(buf, "id%d", idindex++);
        id = buf;
        }
    else
        {
            id = obj->getId();
        }
    if (SP_IS_ITEM(obj))
        {
        SPItem *item = SP_ITEM(obj);
        if (!doCurve(item, id)) {
            return false;
        }
        }
    else if (SP_IS_GRADIENT(obj))
        {
        SPGradient *grad = SP_GRADIENT(obj);
        if (!doGradient(grad, id)) {
            return false;
        }
        }

    /**
     * Descend into children
     */
    for (SPObject *child = obj->firstChild() ; child ; child = child->next)
        {
            if (!doTreeRecursive(doc, child)) {
                return false;
            }
        }

    return true;
}


/**
 *  Output the curve data to buffer
 */
bool JavaFXOutput::doTree(SPDocument *doc)
{

    double bignum = 1000000.0;
    minx  =  bignum;
    maxx  = -bignum;
    miny  =  bignum;
    maxy  = -bignum;

    if (!doTreeRecursive(doc, doc->getRoot())) {
        return false;
    }

    return true;

}


bool JavaFXOutput::doBody(SPDocument *doc, SPObject *obj)
{
    /**
     * Check the type of node and process
     */
    String id;
    if (!obj->getId())
        {
        char buf[16];
        sprintf(buf, "id%d", idindex++);
        id = buf;
        }
    else
        {
            id = obj->getId();
        }

    if (SP_IS_ITEM(obj)) {
        SPItem *item = SP_ITEM(obj);
        //### Get the Shape
        if (SP_IS_SHAPE(item)) {//Bulia's suggestion.  Allow all shapes
            SPShape *shape = SP_SHAPE(item);
            SPCurve *curve = shape->curve;
            if (!curve->is_empty()) {
                String jfxid = sanatize(id);
                out("               %s(),\n", jfxid.c_str());
            }
        }
    }
    else if (SP_IS_GRADIENT(obj)) {
        //TODO: what to do with Gradient within body?????
        //SPGradient *grad = SP_GRADIENT(reprobj);
        //if (!doGradient(grad, id)) {
        //    return false;
        //}
    }

    /**
     * Descend into children
     */
    for (SPObject *child = obj->firstChild() ; child ; child = child->next)
        {
            if (!doBody(doc, child)) {
                return false;
            }
        }

    return true;
}



//########################################################################
//# M A I N    O U T P U T
//########################################################################



/**
 *  Set values back to initial state
 */
void JavaFXOutput::reset()
{
    nrNodes    = 0;
    nrShapes   = 0;
    idindex    = 0;
    name.clear();
    outbuf.clear();
    foutbuf.clear();
}



/**
 * Saves the <paths> of an Inkscape SVG file as JavaFX spline definitions
 */
bool JavaFXOutput::saveDocument(SPDocument *doc, gchar const *filename_utf8)
{
    reset();


    name = Glib::path_get_basename(filename_utf8);
    int pos = name.find('.');
    if (pos > 0) {
        name = name.substr(0, pos);
    }


    //###### SAVE IN JAVAFX FORMAT TO BUFFER
    //# Lets do the curves first, to get the stats

    if (!doTree(doc)) {
        return false;
    }
    String curveBuf = outbuf;
    outbuf.clear();

    if (!doHeader()) {
        return false;
    }

    outbuf.append(curveBuf);

    out("   override function create(): Node {\n");
    out("       Group {\n");
    out("           content: [\n");
    idindex    = 0;

    doBody(doc, doc->getRoot());

    if (!doTail()) {
        return false;
    }



    //###### WRITE TO FILE
    FILE *f = Inkscape::IO::fopen_utf8name(filename_utf8, "w");
    if (!f)
        {
        err("Could open JavaFX file '%s' for writing", filename_utf8);
        return false;
        }

    for (String::iterator iter = outbuf.begin() ; iter!=outbuf.end(); iter++)
        {
        fputc(*iter, f);
        }

    fclose(f);

    return true;
}




//########################################################################
//# EXTENSION API
//########################################################################



#include "clear-n_.h"



/**
 * API call to save document
*/
void
JavaFXOutput::save(Inkscape::Extension::Output */*mod*/,
                        SPDocument *doc, gchar const *filename_utf8)
{
    /* N.B. The name `filename_utf8' represents the fact that we want it to be in utf8; whereas in
     * fact we know that some callers of Extension::save pass something in the filesystem's
     * encoding, while others do g_filename_to_utf8 before calling.
     *
     * In terms of safety, it's best to make all callers pass actual filenames, since in general
     * one can't round-trip from filename to utf8 back to the same filename.  Whereas the argument
     * for passing utf8 filenames is one of convenience: we often want to pass to g_warning or
     * store as a string (rather than a byte stream) in XML, or the like. */
    if (!saveDocument(doc, filename_utf8))
        {
        g_warning("Could not save JavaFX file '%s'", filename_utf8);
        }
}



/**
 * Make sure that we are in the database
 */
bool JavaFXOutput::check (Inkscape::Extension::Extension */*module*/)
{
    /* We don't need a Key
    if (NULL == Inkscape::Extension::db.get(SP_MODULE_KEY_OUTPUT_JFX)) {
        return FALSE;
    }
    */

    return true;
}



/**
 * This is the definition of JavaFX output.  This function just
 * calls the extension system with the memory allocated XML that
 * describes the data.
*/
void
JavaFXOutput::init()
{
    Inkscape::Extension::build_from_mem(
        "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
            "<name>" N_("JavaFX Output") "</name>\n"
            "<id>org.inkscape.output.jfx</id>\n"
            "<output>\n"
                "<extension>.fx</extension>\n"
                "<mimetype>text/x-javafx-script</mimetype>\n"
                "<filetypename>" N_("JavaFX (*.fx)") "</filetypename>\n"
                "<filetypetooltip>" N_("JavaFX Raytracer File") "</filetypetooltip>\n"
            "</output>\n"
        "</inkscape-extension>",
        new JavaFXOutput());
}





}  // namespace Internal
}  // namespace Extension
}  // namespace Inkscape


/*
  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:fileencoding=utf-8:textwidth=99 :