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
17 */
18
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include "glyphy-common.hh"
24
25
26 void
glyphy_extents_clear(glyphy_extents_t * extents)27 glyphy_extents_clear (glyphy_extents_t *extents)
28 {
29 extents->min_x = GLYPHY_INFINITY;
30 extents->min_y = GLYPHY_INFINITY;
31 extents->max_x = -GLYPHY_INFINITY;
32 extents->max_y = -GLYPHY_INFINITY;
33 }
34
35 glyphy_bool_t
glyphy_extents_is_empty(const glyphy_extents_t * extents)36 glyphy_extents_is_empty (const glyphy_extents_t *extents)
37 {
38 return isinf (extents->min_x);
39 }
40
41 void
glyphy_extents_add(glyphy_extents_t * extents,const glyphy_point_t * p)42 glyphy_extents_add (glyphy_extents_t *extents,
43 const glyphy_point_t *p)
44 {
45 if (glyphy_extents_is_empty (extents)) {
46 extents->min_x = extents->max_x = p->x;
47 extents->min_y = extents->max_y = p->y;
48 return;
49 }
50 extents->min_x = std::min (extents->min_x, p->x);
51 extents->min_y = std::min (extents->min_y, p->y);
52 extents->max_x = std::max (extents->max_x, p->x);
53 extents->max_y = std::max (extents->max_y, p->y);
54 }
55
56 void
glyphy_extents_extend(glyphy_extents_t * extents,const glyphy_extents_t * other)57 glyphy_extents_extend (glyphy_extents_t *extents,
58 const glyphy_extents_t *other)
59 {
60 if (glyphy_extents_is_empty (other))
61 return;
62 if (glyphy_extents_is_empty (extents)) {
63 *extents = *other;
64 return;
65 }
66 extents->min_x = std::min (extents->min_x, other->min_x);
67 extents->min_y = std::min (extents->min_y, other->min_y);
68 extents->max_x = std::max (extents->max_x, other->max_x);
69 extents->max_y = std::max (extents->max_y, other->max_y);
70 }
71
72 glyphy_bool_t
glyphy_extents_includes(const glyphy_extents_t * extents,const glyphy_point_t * p)73 glyphy_extents_includes (const glyphy_extents_t *extents,
74 const glyphy_point_t *p)
75 {
76 return extents->min_x <= p->x && p->x <= extents->max_x &&
77 extents->min_y <= p->y && p->y <= extents->max_y;
78 }
79
80 void
glyphy_extents_scale(glyphy_extents_t * extents,double x_scale,double y_scale)81 glyphy_extents_scale (glyphy_extents_t *extents,
82 double x_scale,
83 double y_scale)
84 {
85 extents->min_x *= x_scale;
86 extents->max_x *= x_scale;
87 extents->min_y *= y_scale;
88 extents->max_y *= y_scale;
89 }
90