CMake for MS Windows command line
[platform/core/uifw/dali-adaptor.git] / third-party / glyphy / glyphy-sdf.cc
1 /*
2  * Copyright 2012 Google, Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * Google Author(s): Behdad Esfahbod, Maysum Panju, Wojciech Baranowski
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include "glyphy-common.hh"
24 #include "glyphy-geometry.hh"
25
26 using namespace GLyphy::Geometry;
27
28 double
29 glyphy_sdf_from_arc_list (const glyphy_arc_endpoint_t *endpoints,
30                           unsigned int                 num_endpoints,
31                           const glyphy_point_t        *p,
32                           glyphy_point_t              *closest_p /* may be NULL; TBD not implemented yet */)
33 {
34   Point c = *p;
35   Point p0 (0, 0);
36   Arc closest_arc (p0, p0, 0);
37   double min_dist = GLYPHY_INFINITY;
38   int side = 0;
39   for (unsigned int i = 0; i < num_endpoints; i++) {
40     const glyphy_arc_endpoint_t &endpoint = endpoints[i];
41     if (endpoint.d == GLYPHY_INFINITY) {
42       p0 = endpoint.p;
43       continue;
44     }
45     Arc arc (p0, endpoint.p, endpoint.d);
46     p0 = endpoint.p;
47
48     if (arc.wedge_contains_point (c)) {
49       double sdist = arc.distance_to_point (c); /* TODO This distance has the wrong sign.  Fix */
50       double udist = fabs (sdist) * (1 - GLYPHY_EPSILON);
51       if (udist <= min_dist) {
52         min_dist = udist;
53         side = sdist >= 0 ? -1 : +1;
54       }
55     } else {
56       double udist = std::min ((arc.p0 - c).len (), (arc.p1 - c).len ());
57       if (udist < min_dist) {
58         min_dist = udist;
59         side = 0; /* unsure */
60         closest_arc = arc;
61       } else if (side == 0 && udist == min_dist) {
62         /* If this new distance is the same as the current minimum,
63          * compare extended distances.  Take the sign from the arc
64          * with larger extended distance. */
65         double old_ext_dist = closest_arc.extended_dist (c);
66         double new_ext_dist = arc.extended_dist (c);
67
68         double ext_dist = fabs (new_ext_dist) <= fabs (old_ext_dist) ?
69                           old_ext_dist : new_ext_dist;
70
71         /* For emboldening and stuff: */
72         // min_dist = fabs (ext_dist);
73         side = ext_dist >= 0 ? +1 : -1;
74       }
75     }
76   }
77
78   if (side == 0) {
79     // Technically speaking this should not happen, but it does.  So try to fix it.
80     double ext_dist = closest_arc.extended_dist (c);
81     side = ext_dist >= 0 ? +1 : -1;
82   }
83
84   return side * min_dist;
85 }