xref: /aosp_15_r20/external/pdfium/third_party/agg23/agg_clip_liang_barsky.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 //
17 // Liang-Barsky clipping
18 //
19 //----------------------------------------------------------------------------
20 #ifndef AGG_CLIP_LIANG_BARSKY_INCLUDED
21 #define AGG_CLIP_LIANG_BARSKY_INCLUDED
22 #include "agg_basics.h"
23 #include "third_party/base/numerics/safe_math.h"
24 namespace pdfium
25 {
26 namespace agg
27 {
28 template<class T>
clipping_flags(T x,T y,const rect_base<T> & clip_box)29 inline unsigned clipping_flags(T x, T y, const rect_base<T>& clip_box)
30 {
31     return  (x > clip_box.x2) |
32             ((y > clip_box.y2) << 1) |
33             ((x < clip_box.x1) << 2) |
34             ((y < clip_box.y1) << 3);
35 }
36 template<class T>
clip_liang_barsky(T x1,T y1,T x2,T y2,const rect_base<T> & clip_box,T * x,T * y)37 inline unsigned clip_liang_barsky(T x1, T y1, T x2, T y2,
38                                   const rect_base<T>& clip_box,
39                                   T* x, T* y)
40 {
41     const float nearzero = 1e-30f;
42 
43     pdfium::base::CheckedNumeric<float> width = x2;
44     width -= x1;
45     if (!width.IsValid())
46         return 0;
47     pdfium::base::CheckedNumeric<float> height = y2;
48     height -= y1;
49     if (!height.IsValid())
50         return 0;
51 
52     float deltax = width.ValueOrDefault(0);
53     float deltay = height.ValueOrDefault(0);
54     unsigned np = 0;
55     if(deltax == 0) {
56         deltax = (x1 > clip_box.x1) ? -nearzero : nearzero;
57     }
58     float xin, xout;
59     if(deltax > 0) {
60         xin  = (float)clip_box.x1;
61         xout = (float)clip_box.x2;
62     } else {
63         xin  = (float)clip_box.x2;
64         xout = (float)clip_box.x1;
65     }
66     float tinx = (xin - x1) / deltax;
67     if(deltay == 0) {
68         deltay = (y1 > clip_box.y1) ? -nearzero : nearzero;
69     }
70     float yin, yout;
71     if(deltay > 0) {
72         yin  = (float)clip_box.y1;
73         yout = (float)clip_box.y2;
74     } else {
75         yin  = (float)clip_box.y2;
76         yout = (float)clip_box.y1;
77     }
78     float tiny = (yin - y1) / deltay;
79     float tin1, tin2;
80     if (tinx < tiny) {
81         tin1 = tinx;
82         tin2 = tiny;
83     } else {
84         tin1 = tiny;
85         tin2 = tinx;
86     }
87     if(tin1 <= 1.0f) {
88         if(0 < tin1) {
89             *x++ = (T)xin;
90             *y++ = (T)yin;
91             ++np;
92         }
93         if(tin2 <= 1.0f) {
94           float toutx = (xout - x1) / deltax;
95           float touty = (yout - y1) / deltay;
96           float tout1 = (toutx < touty) ? toutx : touty;
97           if (tin2 > 0 || tout1 > 0) {
98                 if(tin2 <= tout1) {
99                     if(tin2 > 0) {
100                         if(tinx > tiny) {
101                           *x++ = (T)xin;
102                           *y++ = (T)(y1 + (deltay * tinx));
103                         } else {
104                           *x++ = (T)(x1 + (deltax * tiny));
105                           *y++ = (T)yin;
106                         }
107                         ++np;
108                     }
109                     if(tout1 < 1.0f) {
110                         if(toutx < touty) {
111                           *x++ = (T)xout;
112                           *y++ = (T)(y1 + (deltay * toutx));
113                         } else {
114                           *x++ = (T)(x1 + (deltax * touty));
115                           *y++ = (T)yout;
116                         }
117                     } else {
118                         *x++ = x2;
119                         *y++ = y2;
120                     }
121                     ++np;
122                 } else {
123                     if(tinx > tiny) {
124                         *x++ = (T)xin;
125                         *y++ = (T)yout;
126                     } else {
127                         *x++ = (T)xout;
128                         *y++ = (T)yin;
129                     }
130                     ++np;
131                 }
132           }
133         }
134     }
135     return np;
136 }
137 }
138 }  // namespace pdfium
139 #endif
140