xref: /aosp_15_r20/external/pdfium/third_party/agg23/agg_shorten_path.h (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 
2 //----------------------------------------------------------------------------
3 // Anti-Grain Geometry - Version 2.3
4 // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
5 //
6 // Permission to copy, use, modify, sell and distribute this software
7 // is granted provided this copyright notice appears in all copies.
8 // This software is provided "as is" without express or implied
9 // warranty, and with no claim as to its suitability for any purpose.
10 //
11 //----------------------------------------------------------------------------
12 // Contact: [email protected]
13 //          [email protected]
14 //          http://www.antigrain.com
15 //----------------------------------------------------------------------------
16 #ifndef AGG_SHORTEN_PATH_INCLUDED
17 #define AGG_SHORTEN_PATH_INCLUDED
18 #include "agg_basics.h"
19 #include "agg_vertex_sequence.h"
20 namespace pdfium
21 {
22 namespace agg
23 {
24 template<class VertexSequence>
25 void shorten_path(VertexSequence& vs, float s, unsigned closed = 0)
26 {
27     typedef typename VertexSequence::value_type vertex_type;
28     if(s > 0 && vs.size() > 1) {
29         float d;
30         int n = int(vs.size() - 2);
31         while(n) {
32             d = vs[n].dist;
33             if(d > s) {
34                 break;
35             }
36             vs.remove_last();
37             s -= d;
38             --n;
39         }
40         if(vs.size() < 2) {
41             vs.remove_all();
42         } else {
43             n = vs.size() - 1;
44             vertex_type& prev = vs[n - 1];
45             vertex_type& last = vs[n];
46             d = (prev.dist - s) / prev.dist;
47             float x = prev.x + (last.x - prev.x) * d;
48             float y = prev.y + (last.y - prev.y) * d;
49             last.x = x;
50             last.y = y;
51             if(!prev(last)) {
52                 vs.remove_last();
53             }
54             vs.close(closed != 0);
55         }
56     }
57 }
58 }
59 }  // namespace pdfium
60 #endif
61