1 /* Copyright (C) 2019 by Skef Iterum */
2 /*
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions are met:
5 
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer.
8 
9  * Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12 
13  * The name of the author may not be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15 
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef FONTFORGE_UTANVEC_H
29 #define FONTFORGE_UTANVEC_H
30 
31 #include <fontforge-config.h>
32 
33 #include "splinefont.h"
34 
35 #include <math.h>
36 
37 #define BPUNINIT ((BasePoint) { -INFINITY, INFINITY })
38 #define UTZERO ((BasePoint) { 0.0, 1.0 })
39 #define UTMIN ((BasePoint) { -1, -DBL_MIN })
40 
BPLenSq(BasePoint v)41 static inline bigreal BPLenSq(BasePoint v) {
42     return v.x * v.x + v.y * v.y;
43 }
44 
BPNorm(BasePoint v)45 static inline bigreal BPNorm(BasePoint v) {
46     return sqrt(v.x * v.x + v.y * v.y);
47 }
48 
BPDist(BasePoint p1,BasePoint p2)49 static inline bigreal BPDist(BasePoint p1, BasePoint p2) {
50     bigreal dx = p2.x - p1.x, dy = p2.y - p1.y;
51     return sqrt(dx * dx + dy * dy);
52 }
53 
BPRev(BasePoint v)54 static inline BasePoint BPRev(BasePoint v) {
55     return (BasePoint) { -v.x, -v.y };
56 }
57 
BPRevIf(int t,BasePoint v)58 static inline BasePoint BPRevIf(int t, BasePoint v) {
59     return t ? (BasePoint) { -v.x, -v.y } : v;
60 }
61 
BPAdd(BasePoint v1,BasePoint v2)62 static inline BasePoint BPAdd(BasePoint v1, BasePoint v2) {
63     return (BasePoint) { v1.x + v2.x, v1.y + v2.y };
64 }
65 
BPSub(BasePoint v1,BasePoint v2)66 static inline BasePoint BPSub(BasePoint v1, BasePoint v2) {
67     return (BasePoint) { v1.x - v2.x, v1.y - v2.y };
68 }
69 
BPScale(BasePoint v,bigreal f)70 static inline BasePoint BPScale(BasePoint v, bigreal f) {
71     return (BasePoint) { f * v.x, f * v.y };
72 }
73 
BPAvg(BasePoint v1,BasePoint v2)74 static inline BasePoint BPAvg(BasePoint v1, BasePoint v2) {
75     return (BasePoint) { (v1.x + v2.x) / 2, (v1.y + v2.y)/2 };
76 }
77 
BPDot(BasePoint v1,BasePoint v2)78 static inline bigreal BPDot(BasePoint v1, BasePoint v2) {
79     return v1.x * v2.x + v1.y * v2.y;
80 }
81 
BPCross(BasePoint v1,BasePoint v2)82 static inline bigreal BPCross(BasePoint v1, BasePoint v2) {
83     return v1.x * v2.y - v1.y * v2.x;
84 }
85 
BPIsUninit(BasePoint bp)86 static inline int BPIsUninit(BasePoint bp) {
87     return bp.x == -INFINITY && bp.y == INFINITY;
88 }
89 
BPRot(BasePoint v,BasePoint ut)90 static inline BasePoint BPRot(BasePoint v, BasePoint ut) {
91     return (BasePoint) { ut.x * v.x - ut.y * v.y, ut.y * v.x + ut.x * v.y };
92 }
93 
BPEq(BasePoint bp1,BasePoint bp2)94 static inline int BPEq(BasePoint bp1, BasePoint bp2) {
95     return bp1.x == bp2.x && bp1.y == bp2.y;
96 }
97 
BP90CCW(BasePoint v)98 static inline BasePoint BP90CCW(BasePoint v) {
99     return (BasePoint) { -v.y, v.x };
100 }
101 
BP90CW(BasePoint v)102 static inline BasePoint BP90CW(BasePoint v) {
103     return (BasePoint) { v.y, -v.x };
104 }
105 
BPNeg(BasePoint v)106 static inline BasePoint BPNeg(BasePoint v) {
107     return (BasePoint) { v.x, -v.y };
108 }
109 
110 extern BasePoint MakeUTanVec(bigreal x, bigreal y);
111 extern BasePoint NormVec(BasePoint v);
112 extern int UTanVecGreater(BasePoint uta, BasePoint utb);
113 extern int UTanVecsSequent(BasePoint ut1, BasePoint ut2, BasePoint ut3,
114                            int ccw);
115 extern int JointBendsCW(BasePoint ut_ref, BasePoint ut_vec);
116 extern BasePoint SplineUTanVecAt(Spline *s, bigreal t);
117 extern bigreal SplineSolveForUTanVec(Spline *spl, BasePoint ut, bigreal min_t,
118                                      bool picky);
119 extern void UTanVecTests();
120 
121 #endif // FONTFORGE_UTANVEC_H
122