This file is indexed.

/usr/include/mapnik/svg/svg_path_adapter.hpp is in libmapnik-dev 2.2.0+ds1-6build2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
/*****************************************************************************
 *
 * This file is part of Mapnik (c++ mapping toolkit)
 *
 * Copyright (C) 2011 Artem Pavlenko
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *****************************************************************************/

#ifndef MAPNIK_SVG_PATH_ADAPTER_HPP
#define MAPNIK_SVG_PATH_ADAPTER_HPP

// mapnik
#include <mapnik/noncopyable.hpp>

// agg
#include "agg_math.h"
#include "agg_array.h"
#include "agg_bezier_arc.h"

// stl
#include <cmath>
#include <vector>

namespace mapnik {
namespace svg {

using namespace agg;

template<class VertexContainer> class path_adapter : mapnik::noncopyable
{
public:
    typedef VertexContainer            container_type;
    typedef path_adapter<VertexContainer> self_type;

    //--------------------------------------------------------------------
    path_adapter(VertexContainer & vertices) : vertices_(vertices), iterator_(0) {}
    //void remove_all() { vertices_.remove_all(); iterator_ = 0; }
    //void free_all()   { vertices_.free_all();   iterator_ = 0; }

    // Make path functions
    //--------------------------------------------------------------------
    unsigned start_new_path();

    void move_to(double x, double y);
    void move_rel(double dx, double dy);

    void line_to(double x, double y);
    void line_rel(double dx, double dy);

    void hline_to(double x);
    void hline_rel(double dx);

    void vline_to(double y);
    void vline_rel(double dy);

    void arc_to(double rx, double ry,
                double angle,
                bool large_arc_flag,
                bool sweep_flag,
                double x, double y);

    void arc_rel(double rx, double ry,
                 double angle,
                 bool large_arc_flag,
                 bool sweep_flag,
                 double dx, double dy);

    void curve3(double x_ctrl, double y_ctrl,
                double x_to,   double y_to);

    void curve3_rel(double dx_ctrl, double dy_ctrl,
                    double dx_to,   double dy_to);

    void curve3(double x_to, double y_to);

    void curve3_rel(double dx_to, double dy_to);

    void curve4(double x_ctrl1, double y_ctrl1,
                double x_ctrl2, double y_ctrl2,
                double x_to,    double y_to);

    void curve4_rel(double dx_ctrl1, double dy_ctrl1,
                    double dx_ctrl2, double dy_ctrl2,
                    double dx_to,    double dy_to);

    void curve4(double x_ctrl2, double y_ctrl2,
                double x_to,    double y_to);

    void curve4_rel(double x_ctrl2, double y_ctrl2,
                    double x_to,    double y_to);


    void end_poly(unsigned flags = path_flags_close);
    void close_polygon(unsigned flags = path_flags_none);

    // Accessors
    //--------------------------------------------------------------------
    const container_type& vertices() const { return vertices_; }
    container_type& vertices()       { return vertices_; }

    unsigned total_vertices() const;

    void rel_to_abs(double* x, double* y) const;

    unsigned last_vertex(double* x, double* y) const;
    unsigned prev_vertex(double* x, double* y) const;

    double last_x() const;
    double last_y() const;

    unsigned vertex(unsigned idx, double* x, double* y) const;
    unsigned command(unsigned idx) const;

    void modify_vertex(unsigned idx, double x, double y);
    void modify_vertex(unsigned idx, double x, double y, unsigned cmd);
    void modify_command(unsigned idx, unsigned cmd);

    // VertexSource interface
    //--------------------------------------------------------------------
    void     rewind(unsigned path_id);
    unsigned vertex(double* x, double* y);

    // Arrange the orientation of a polygon, all polygons in a path,
    // or in all paths. After calling arrange_orientations() or
    // arrange_orientations_all_paths(), all the polygons will have
    // the same orientation, i.e. path_flags_cw or path_flags_ccw
    //--------------------------------------------------------------------
    unsigned arrange_polygon_orientation(unsigned start, path_flags_e orientation);
    unsigned arrange_orientations(unsigned path_id, path_flags_e orientation);
    void     arrange_orientations_all_paths(path_flags_e orientation);
    void     invert_polygon(unsigned start);

    // Flip all vertices horizontally or vertically,
    // between x1 and x2, or between y1 and y2 respectively
    //--------------------------------------------------------------------
    void flip_x(double x1, double x2);
    void flip_y(double y1, double y2);

    // Concatenate path. The path is added as is.
    //--------------------------------------------------------------------
    template<class VertexSource>
    void concat_path(VertexSource& vs, unsigned path_id = 0)
    {
        double x(0), y(0);
        unsigned cmd;
        vs.rewind(path_id);
        while(!is_stop(cmd = vs.vertex(&x, &y)))
        {
            vertices_.add_vertex(x, y, cmd);
        }
    }

    //--------------------------------------------------------------------
    // Join path. The path is joined with the existing one, that is,
    // it behaves as if the pen of a plotter was always down (drawing)
    template<class VertexSource>
    void join_path(VertexSource& vs, unsigned path_id = 0)
    {
        double x, y;
        unsigned cmd;
        vs.rewind(path_id);
        cmd = vs.vertex(&x, &y);
        if(!is_stop(cmd))
        {
            if(is_vertex(cmd))
            {
                double x0, y0;
                unsigned cmd0 = last_vertex(&x0, &y0);
                if(is_vertex(cmd0))
                {
                    if(calc_distance(x, y, x0, y0) > vertex_dist_epsilon)
                    {
                        if(is_move_to(cmd)) cmd = path_cmd_line_to;
                        vertices_.add_vertex(x, y, cmd);
                    }
                }
                else
                {
                    if(is_stop(cmd0))
                    {
                        cmd = path_cmd_move_to;
                    }
                    else
                    {
                        if(is_move_to(cmd)) cmd = path_cmd_line_to;
                    }
                    vertices_.add_vertex(x, y, cmd);
                }
            }
            while(!is_stop(cmd = vs.vertex(&x, &y)))
            {
                vertices_.add_vertex(x, y, is_move_to(cmd) ?
                                      unsigned(path_cmd_line_to) :
                                      cmd);
            }
        }
    }

    //--------------------------------------------------------------------
    void translate(double dx, double dy, unsigned path_id=0);
    void translate_all_paths(double dx, double dy);

    //--------------------------------------------------------------------
    template<class Trans>
    void transform(const Trans& trans, unsigned path_id=0)
    {
        unsigned num_ver = vertices_.total_vertices();
        for(; path_id < num_ver; path_id++)
        {
            double x, y;
            unsigned cmd = vertices_.vertex(path_id, &x, &y);
            if(is_stop(cmd)) break;
            if(is_vertex(cmd))
            {
                trans.transform(&x, &y);
                vertices_.modify_vertex(path_id, x, y);
            }
        }
    }

    //--------------------------------------------------------------------
    template<class Trans>
    void transform_all_paths(const Trans& trans)
    {
        unsigned idx;
        unsigned num_ver = vertices_.total_vertices();
        for(idx = 0; idx < num_ver; idx++)
        {
            double x, y;
            if(is_vertex(vertices_.vertex(idx, &x, &y)))
            {
                trans.transform(&x, &y);
                vertices_.modify_vertex(idx, x, y);
            }
        }
    }



private:
    unsigned perceive_polygon_orientation(unsigned start, unsigned end);
    void     invert_polygon(unsigned start, unsigned end);

    VertexContainer & vertices_;
    unsigned          iterator_;
    double start_x_;
    double start_y_;
};

//------------------------------------------------------------------------
template<class VC>
unsigned path_adapter<VC>::start_new_path()
{
    if(!is_stop(vertices_.last_command()))
    {
        vertices_.add_vertex(0.0, 0.0, path_cmd_stop);
    }
    return vertices_.total_vertices();
}


//------------------------------------------------------------------------
template<class VC>
inline void path_adapter<VC>::rel_to_abs(double* x, double* y) const
{
    if(vertices_.total_vertices())
    {
        double x2;
        double y2;
        if(is_vertex(vertices_.last_vertex(&x2, &y2))
           || !is_stop(vertices_.last_command()))
        {
            *x += x2;
            *y += y2;
        }
    }
}

//------------------------------------------------------------------------
template<class VC>
inline void path_adapter<VC>::move_to(double x, double y)
{
    start_x_ = x;
    start_y_ = y;
    vertices_.add_vertex(x, y, path_cmd_move_to);
}

//------------------------------------------------------------------------
template<class VC>
inline void path_adapter<VC>::move_rel(double dx, double dy)
{
    rel_to_abs(&dx, &dy);
    vertices_.add_vertex(dx, dy, path_cmd_move_to);
}

//------------------------------------------------------------------------
template<class VC>
inline void path_adapter<VC>::line_to(double x, double y)
{
    vertices_.add_vertex(x, y, path_cmd_line_to);
}

//------------------------------------------------------------------------
template<class VC>
inline void path_adapter<VC>::line_rel(double dx, double dy)
{
    rel_to_abs(&dx, &dy);
    vertices_.add_vertex(dx, dy, path_cmd_line_to);
}

//------------------------------------------------------------------------
template<class VC>
inline void path_adapter<VC>::hline_to(double x)
{
    vertices_.add_vertex(x, last_y(), path_cmd_line_to);
}

//------------------------------------------------------------------------
template<class VC>
inline void path_adapter<VC>::hline_rel(double dx)
{
    double dy = 0;
    rel_to_abs(&dx, &dy);
    vertices_.add_vertex(dx, dy, path_cmd_line_to);
}

//------------------------------------------------------------------------
template<class VC>
inline void path_adapter<VC>::vline_to(double y)
{
    vertices_.add_vertex(last_x(), y, path_cmd_line_to);
}

//------------------------------------------------------------------------
template<class VC>
inline void path_adapter<VC>::vline_rel(double dy)
{
    double dx = 0;
    rel_to_abs(&dx, &dy);
    vertices_.add_vertex(dx, dy, path_cmd_line_to);
}

//------------------------------------------------------------------------
template<class VC>
void path_adapter<VC>::arc_to(double rx, double ry,
                              double angle,
                              bool large_arc_flag,
                              bool sweep_flag,
                              double x, double y)
{
    if(vertices_.total_vertices() && is_vertex(vertices_.last_command()))
    {
        const double epsilon = 1e-30;
        double x0 = 0.0;
        double y0 = 0.0;
        vertices_.last_vertex(&x0, &y0);

        rx = std::fabs(rx);
        ry = std::fabs(ry);

        // Ensure radii are valid
        //-------------------------
        if(rx < epsilon || ry < epsilon)
        {
            line_to(x, y);
            return;
        }

        if(calc_distance(x0, y0, x, y) < epsilon)
        {
            // If the endpoints (x, y) and (x0, y0) are identical, then this
            // is equivalent to omitting the elliptical arc segment entirely.
            return;
        }
        bezier_arc_svg a(x0, y0, rx, ry, angle, large_arc_flag, sweep_flag, x, y);
        if(a.radii_ok())
        {
            join_path(a);
        }
        else
        {
            line_to(x, y);
        }
    }
    else
    {
        move_to(x, y);
    }
}

//------------------------------------------------------------------------
template<class VC>
void path_adapter<VC>::arc_rel(double rx, double ry,
                               double angle,
                               bool large_arc_flag,
                               bool sweep_flag,
                               double dx, double dy)
{
    rel_to_abs(&dx, &dy);
    arc_to(rx, ry, angle, large_arc_flag, sweep_flag, dx, dy);
}

//------------------------------------------------------------------------
template<class VC>
void path_adapter<VC>::curve3(double x_ctrl, double y_ctrl,
                              double x_to,   double y_to)
{
    vertices_.add_vertex(x_ctrl, y_ctrl, path_cmd_curve3);
    vertices_.add_vertex(x_to,   y_to,   path_cmd_curve3);
}

//------------------------------------------------------------------------
template<class VC>
void path_adapter<VC>::curve3_rel(double dx_ctrl, double dy_ctrl,
                                  double dx_to,   double dy_to)
{
    rel_to_abs(&dx_ctrl, &dy_ctrl);
    rel_to_abs(&dx_to,   &dy_to);
    vertices_.add_vertex(dx_ctrl, dy_ctrl, path_cmd_curve3);
    vertices_.add_vertex(dx_to,   dy_to,   path_cmd_curve3);
}

//------------------------------------------------------------------------
template<class VC>
void path_adapter<VC>::curve3(double x_to, double y_to)
{
    double x0;
    double y0;
    if(is_vertex(vertices_.last_vertex(&x0, &y0)))
    {
        double x_ctrl;
        double y_ctrl;
        unsigned cmd = vertices_.prev_vertex(&x_ctrl, &y_ctrl);
        if(is_curve(cmd))
        {
            x_ctrl = x0 + x0 - x_ctrl;
            y_ctrl = y0 + y0 - y_ctrl;
        }
        else
        {
            x_ctrl = x0;
            y_ctrl = y0;
        }
        curve3(x_ctrl, y_ctrl, x_to, y_to);
    }
}

//------------------------------------------------------------------------
template<class VC>
void path_adapter<VC>::curve3_rel(double dx_to, double dy_to)
{
    rel_to_abs(&dx_to, &dy_to);
    curve3(dx_to, dy_to);
}

//------------------------------------------------------------------------
template<class VC>
void path_adapter<VC>::curve4(double x_ctrl1, double y_ctrl1,
                              double x_ctrl2, double y_ctrl2,
                              double x_to,    double y_to)
{
    vertices_.add_vertex(x_ctrl1, y_ctrl1, path_cmd_curve4);
    vertices_.add_vertex(x_ctrl2, y_ctrl2, path_cmd_curve4);
    vertices_.add_vertex(x_to,    y_to,    path_cmd_curve4);
}

//------------------------------------------------------------------------
template<class VC>
void path_adapter<VC>::curve4_rel(double dx_ctrl1, double dy_ctrl1,
                                  double dx_ctrl2, double dy_ctrl2,
                                  double dx_to,    double dy_to)
{
    rel_to_abs(&dx_ctrl1, &dy_ctrl1);
    rel_to_abs(&dx_ctrl2, &dy_ctrl2);
    rel_to_abs(&dx_to,    &dy_to);
    vertices_.add_vertex(dx_ctrl1, dy_ctrl1, path_cmd_curve4);
    vertices_.add_vertex(dx_ctrl2, dy_ctrl2, path_cmd_curve4);
    vertices_.add_vertex(dx_to,    dy_to,    path_cmd_curve4);
}

//------------------------------------------------------------------------
template<class VC>
void path_adapter<VC>::curve4(double x_ctrl2, double y_ctrl2,
                              double x_to,    double y_to)
{
    double x0;
    double y0;
    if(is_vertex(last_vertex(&x0, &y0)))
    {
        double x_ctrl1;
        double y_ctrl1;
        unsigned cmd = prev_vertex(&x_ctrl1, &y_ctrl1);
        if(is_curve(cmd))
        {
            x_ctrl1 = x0 + x0 - x_ctrl1;
            y_ctrl1 = y0 + y0 - y_ctrl1;
        }
        else
        {
            x_ctrl1 = x0;
            y_ctrl1 = y0;
        }
        curve4(x_ctrl1, y_ctrl1, x_ctrl2, y_ctrl2, x_to, y_to);
    }
}

//------------------------------------------------------------------------
template<class VC>
void path_adapter<VC>::curve4_rel(double dx_ctrl2, double dy_ctrl2,
                                  double dx_to,    double dy_to)
{
    rel_to_abs(&dx_ctrl2, &dy_ctrl2);
    rel_to_abs(&dx_to,    &dy_to);
    curve4(dx_ctrl2, dy_ctrl2, dx_to, dy_to);
}

//------------------------------------------------------------------------
template<class VC>
inline void path_adapter<VC>::end_poly(unsigned flags)
{
    if(is_vertex(vertices_.last_command()))
    {
        vertices_.add_vertex(start_x_, start_y_, path_cmd_end_poly | flags);
    }
}

//------------------------------------------------------------------------
template<class VC>
inline void path_adapter<VC>::close_polygon(unsigned flags)
{
    end_poly(path_flags_close | flags);
}

//------------------------------------------------------------------------
template<class VC>
inline unsigned path_adapter<VC>::total_vertices() const
{
    return vertices_.total_vertices();
}

//------------------------------------------------------------------------
template<class VC>
inline unsigned path_adapter<VC>::last_vertex(double* x, double* y) const
{
    return vertices_.last_vertex(x, y);
}

//------------------------------------------------------------------------
template<class VC>
inline unsigned path_adapter<VC>::prev_vertex(double* x, double* y) const
{
    return vertices_.prev_vertex(x, y);
}

//------------------------------------------------------------------------
template<class VC>
inline double path_adapter<VC>::last_x() const
{
    return vertices_.last_x();
}

//------------------------------------------------------------------------
template<class VC>
inline double path_adapter<VC>::last_y() const
{
    return vertices_.last_y();
}

//------------------------------------------------------------------------
template<class VC>
inline unsigned path_adapter<VC>::vertex(unsigned idx, double* x, double* y) const
{
    return vertices_.vertex(idx, x, y);
}

//------------------------------------------------------------------------
template<class VC>
inline unsigned path_adapter<VC>::command(unsigned idx) const
{
    return vertices_.command(idx);
}

//------------------------------------------------------------------------
template<class VC>
void path_adapter<VC>::modify_vertex(unsigned idx, double x, double y)
{
    vertices_.modify_vertex(idx, x, y);
}

//------------------------------------------------------------------------
template<class VC>
void path_adapter<VC>::modify_vertex(unsigned idx, double x, double y, unsigned cmd)
{
    vertices_.modify_vertex(idx, x, y, cmd);
}

//------------------------------------------------------------------------
template<class VC>
void path_adapter<VC>::modify_command(unsigned idx, unsigned cmd)
{
    vertices_.modify_command(idx, cmd);
}

//------------------------------------------------------------------------
template<class VC>
inline void path_adapter<VC>::rewind(unsigned path_id)
{
    iterator_ = path_id;
}

//------------------------------------------------------------------------
template<class VC>
inline unsigned path_adapter<VC>::vertex(double* x, double* y)
{
    if(iterator_ >= vertices_.total_vertices()) return path_cmd_stop;
    return vertices_.vertex(iterator_++, x, y);
}

//------------------------------------------------------------------------
template<class VC>
unsigned path_adapter<VC>::perceive_polygon_orientation(unsigned start,
                                                        unsigned end)
{
    // Calculate signed area (double area to be exact)
    //---------------------
    unsigned np = end - start;
    double area = 0.0;
    unsigned i;
    for(i = 0; i < np; i++)
    {
        double x1, y1, x2, y2;
        vertices_.vertex(start + i,            &x1, &y1);
        vertices_.vertex(start + (i + 1) % np, &x2, &y2);
        area += x1 * y2 - y1 * x2;
    }
    return (area < 0.0) ? path_flags_cw : path_flags_ccw;
}

//------------------------------------------------------------------------
template<class VC>
void path_adapter<VC>::invert_polygon(unsigned start, unsigned end)
{
    unsigned i;
    unsigned tmp_cmd = vertices_.command(start);

    --end; // Make "end" inclusive

    // Shift all commands to one position
    for(i = start; i < end; i++)
    {
        vertices_.modify_command(i, vertices_.command(i + 1));
    }

    // Assign starting command to the ending command
    vertices_.modify_command(end, tmp_cmd);

    // Reverse the polygon
    while(end > start)
    {
        vertices_.swap_vertices(start++, end--);
    }
}

//------------------------------------------------------------------------
template<class VC>
void path_adapter<VC>::invert_polygon(unsigned start)
{
    // Skip all non-vertices at the beginning
    while(start < vertices_.total_vertices() &&
          !is_vertex(vertices_.command(start))) ++start;

    // Skip all insignificant move_to
    while(start+1 < vertices_.total_vertices() &&
          is_move_to(vertices_.command(start)) &&
          is_move_to(vertices_.command(start+1))) ++start;

    // Find the last vertex
    unsigned end = start + 1;
    while(end < vertices_.total_vertices() &&
          !is_next_poly(vertices_.command(end))) ++end;

    invert_polygon(start, end);
}

//------------------------------------------------------------------------
template<class VC>
unsigned path_adapter<VC>::arrange_polygon_orientation(unsigned start,
                                                       path_flags_e orientation)
{
    if(orientation == path_flags_none) return start;

    // Skip all non-vertices at the beginning
    while(start < vertices_.total_vertices() &&
          !is_vertex(vertices_.command(start))) ++start;

    // Skip all insignificant move_to
    while(start+1 < vertices_.total_vertices() &&
          is_move_to(vertices_.command(start)) &&
          is_move_to(vertices_.command(start+1))) ++start;

    // Find the last vertex
    unsigned end = start + 1;
    while(end < vertices_.total_vertices() &&
          !is_next_poly(vertices_.command(end))) ++end;

    if(end - start > 2)
    {
        if(perceive_polygon_orientation(start, end) != unsigned(orientation))
        {
            // Invert polygon, set orientation flag, and skip all end_poly
            invert_polygon(start, end);
            unsigned cmd;
            while(end < vertices_.total_vertices() &&
                  is_end_poly(cmd = vertices_.command(end)))
            {
                vertices_.modify_command(end++, set_orientation(cmd, orientation));
            }
        }
    }
    return end;
}

//------------------------------------------------------------------------
template<class VC>
unsigned path_adapter<VC>::arrange_orientations(unsigned start,
                                                path_flags_e orientation)
{
    if(orientation != path_flags_none)
    {
        while(start < vertices_.total_vertices())
        {
            start = arrange_polygon_orientation(start, orientation);
            if(is_stop(vertices_.command(start)))
            {
                ++start;
                break;
            }
        }
    }
    return start;
}

//------------------------------------------------------------------------
template<class VC>
void path_adapter<VC>::arrange_orientations_all_paths(path_flags_e orientation)
{
    if(orientation != path_flags_none)
    {
        unsigned start = 0;
        while(start < vertices_.total_vertices())
        {
            start = arrange_orientations(start, orientation);
        }
    }
}

//------------------------------------------------------------------------
template<class VC>
void path_adapter<VC>::flip_x(double x1, double x2)
{
    unsigned i;
    double x, y;
    for(i = 0; i < vertices_.total_vertices(); i++)
    {
        unsigned cmd = vertices_.vertex(i, &x, &y);
        if(is_vertex(cmd))
        {
            vertices_.modify_vertex(i, x2 - x + x1, y);
        }
    }
}

//------------------------------------------------------------------------
template<class VC>
void path_adapter<VC>::flip_y(double y1, double y2)
{
    unsigned i;
    double x, y;
    for(i = 0; i < vertices_.total_vertices(); i++)
    {
        unsigned cmd = vertices_.vertex(i, &x, &y);
        if(is_vertex(cmd))
        {
            vertices_.modify_vertex(i, x, y2 - y + y1);
        }
    }
}

//------------------------------------------------------------------------
template<class VC>
void path_adapter<VC>::translate(double dx, double dy, unsigned path_id)
{
    unsigned num_ver = vertices_.total_vertices();
    for(; path_id < num_ver; path_id++)
    {
        double x, y;
        unsigned cmd = vertices_.vertex(path_id, &x, &y);
        if(is_stop(cmd)) break;
        if(is_vertex(cmd))
        {
            x += dx;
            y += dy;
            vertices_.modify_vertex(path_id, x, y);
        }
    }
}

//------------------------------------------------------------------------
template<class VC>
void path_adapter<VC>::translate_all_paths(double dx, double dy)
{
    unsigned idx;
    unsigned num_ver = vertices_.total_vertices();
    for(idx = 0; idx < num_ver; idx++)
    {
        double x, y;
        if(is_vertex(vertices_.vertex(idx, &x, &y)))
        {
            x += dx;
            y += dy;
            vertices_.modify_vertex(idx, x, y);
        }
    }
}


template<class Container> class vertex_stl_adapter : mapnik::noncopyable
{
public:

    typedef typename Container::value_type vertex_type;
    typedef typename vertex_type::value_type value_type;

    explicit vertex_stl_adapter(Container & vertices)
        : vertices_(vertices) {}

    void add_vertex(double x, double y, unsigned cmd)
    {
        vertices_.push_back(vertex_type(value_type(x),
                                         value_type(y),
                                         int8u(cmd)));
    }

    void modify_vertex(unsigned idx, double x, double y)
    {
        vertex_type& v = vertices_[idx];
        v.x = value_type(x);
        v.y = value_type(y);
    }

    void modify_vertex(unsigned idx, double x, double y, unsigned cmd)
    {
        vertex_type& v = vertices_[idx];
        v.x   = value_type(x);
        v.y   = value_type(y);
        v.cmd = int8u(cmd);
    }

    void modify_command(unsigned idx, unsigned cmd)
    {
        vertices_[idx].cmd = int8u(cmd);
    }

    void swap_vertices(unsigned v1, unsigned v2)
    {
        vertex_type t = vertices_[v1];
        vertices_[v1] = vertices_[v2];
        vertices_[v2] = t;
    }

    unsigned last_command() const
    {
        return vertices_.size() ?
            vertices_[vertices_.size() - 1].cmd :
            (unsigned)path_cmd_stop;
    }

    unsigned last_vertex(double* x, double* y) const
    {
        if(vertices_.size() == 0)
        {
            *x = *y = 0.0;
            return path_cmd_stop;
        }
        return vertex(vertices_.size() - 1, x, y);
    }

    unsigned prev_vertex(double* x, double* y) const
    {
        if(vertices_.size() < 2)
        {
            *x = *y = 0.0;
            return path_cmd_stop;
        }
        return vertex(vertices_.size() - 2, x, y);
    }

    double last_x() const
    {
        return vertices_.size() ? vertices_[vertices_.size() - 1].x : 0.0;
    }

    double last_y() const
    {
        return vertices_.size() ? vertices_[vertices_.size() - 1].y : 0.0;
    }

    unsigned total_vertices() const
    {
        return vertices_.size();
    }

    unsigned vertex(unsigned idx, double* x, double* y) const
    {
        const vertex_type& v = vertices_[idx];
        *x = v.x;
        *y = v.y;
        return v.cmd;
    }

    unsigned command(unsigned idx) const
    {
        return vertices_[idx].cmd;
    }

private:
    Container & vertices_;
};


typedef std::vector<vertex_d> svg_path_storage;

typedef path_adapter<vertex_stl_adapter<svg_path_storage> > svg_path_adapter;

}}

#endif // MAPNIK_SVG_PATH_ADAPTER