Stringify public name types
[platform/upstream/libxkbcommon.git] / src / geom.c
1 /************************************************************
2 Copyright (c) 1993 by Silicon Graphics Computer Systems, Inc.
3
4 Permission to use, copy, modify, and distribute this
5 software and its documentation for any purpose and without
6 fee is hereby granted, provided that the above copyright
7 notice appear in all copies and that both that copyright
8 notice and this permission notice appear in supporting
9 documentation, and that the name of Silicon Graphics not be
10 used in advertising or publicity pertaining to distribution
11 of the software without specific prior written permission.
12 Silicon Graphics makes no representation about the suitability
13 of this software for any purpose. It is provided "as is"
14 without any express or implied warranty.
15
16 SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17 SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18 AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19 GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
20 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22 OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
23 THE USE OR PERFORMANCE OF THIS SOFTWARE.
24
25 ********************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30
31 #include <limits.h>
32 #include "xkbgeom.h"
33 #include "xkbcommon/xkbcommon.h"
34 #include "XKBcommonint.h"
35
36 static void
37 _XkbCheckBounds(struct xkb_bounds * bounds, int x, int y)
38 {
39     if (x < bounds->x1)
40         bounds->x1 = x;
41     if (x > bounds->x2)
42         bounds->x2 = x;
43     if (y < bounds->y1)
44         bounds->y1 = y;
45     if (y > bounds->y2)
46         bounds->y2 = y;
47 }
48
49 Bool
50 XkbcComputeShapeBounds(struct xkb_shape * shape)
51 {
52     int o, p;
53     struct xkb_outline * outline;
54     struct xkb_point * pt;
55
56     if ((!shape) || (shape->num_outlines < 1))
57         return False;
58
59     shape->bounds.x1 = shape->bounds.y1 = SHRT_MAX;
60     shape->bounds.x2 = shape->bounds.y2 = SHRT_MIN;
61
62     for (outline = shape->outlines, o = 0; o < shape->num_outlines;
63          o++, outline++)
64     {
65         for (pt = outline->points, p = 0; p < outline->num_points; p++, pt++)
66             _XkbCheckBounds(&shape->bounds, pt->x, pt->y);
67         if (outline->num_points < 2)
68             _XkbCheckBounds(&shape->bounds, 0, 0);
69     }
70     return True;
71 }
72
73 static Bool
74 XkbcComputeRowBounds(struct xkb_geometry * geom, struct xkb_section * section, struct xkb_row * row)
75 {
76     int k, pos;
77     struct xkb_key * key;
78     struct xkb_bounds *bounds, *sbounds;
79
80     if (!geom || !section || !row)
81         return False;
82
83     bounds = &row->bounds;
84     memset(bounds, 0, sizeof(struct xkb_bounds));
85
86     for (key = row->keys, pos = k = 0; k < row->num_keys; k++, key++) {
87         sbounds = &XkbKeyShape(geom, key)->bounds;
88         _XkbCheckBounds(bounds, pos, 0);
89
90         if (!row->vertical) {
91             if (key->gap != 0) {
92                 pos += key->gap;
93                 _XkbCheckBounds(bounds, pos, 0);
94             }
95             _XkbCheckBounds(bounds, pos + sbounds->x1, sbounds->y1);
96             _XkbCheckBounds(bounds, pos + sbounds->x2, sbounds->y2);
97             pos += sbounds->x2;
98         }
99         else {
100             if (key->gap != 0) {
101                 pos += key->gap;
102                 _XkbCheckBounds(bounds, 0, pos);
103             }
104             _XkbCheckBounds(bounds,pos + sbounds->x1, sbounds->y1);
105             _XkbCheckBounds(bounds,pos + sbounds->x2, sbounds->y2);
106             pos += sbounds->y2;
107         }
108     }
109
110     return True;
111 }
112
113 Bool
114 XkbcComputeSectionBounds(struct xkb_geometry * geom, struct xkb_section * section)
115 {
116     int i;
117     struct xkb_shape * shape;
118     struct xkb_row * row;
119     union xkb_doodad * doodad;
120     struct xkb_bounds * bounds, *rbounds = NULL;
121
122     if (!geom || !section)
123         return False;
124
125     bounds = &section->bounds;
126     memset(bounds, 0, sizeof(struct xkb_bounds));
127
128     for (i = 0, row = section->rows; i < section->num_rows; i++, row++) {
129         if (!XkbcComputeRowBounds(geom, section, row))
130             return False;
131         rbounds = &row->bounds;
132         _XkbCheckBounds(bounds, row->left + rbounds->x1,
133                         row->top + rbounds->y1);
134         _XkbCheckBounds(bounds, row->left + rbounds->x2,
135                         row->top + rbounds->y2);
136     }
137
138     for (i = 0, doodad = section->doodads; i < section->num_doodads;
139          i++, doodad++)
140     {
141         static struct xkb_bounds tbounds;
142
143         switch (doodad->any.type) {
144         case XkbOutlineDoodad:
145         case XkbSolidDoodad:
146             shape = XkbShapeDoodadShape(geom, &doodad->shape);
147             rbounds = &shape->bounds;
148             break;
149         case XkbTextDoodad:
150             tbounds.x1 = doodad->text.left;
151             tbounds.y1 = doodad->text.top;
152             tbounds.x2 = tbounds.x1 + doodad->text.width;
153             tbounds.y2 = tbounds.y1 + doodad->text.height;
154             rbounds = &tbounds;
155             break;
156         case XkbIndicatorDoodad:
157             shape = XkbIndicatorDoodadShape(geom, &doodad->indicator);
158             rbounds = &shape->bounds;
159             break;
160         case XkbLogoDoodad:
161             shape = XkbLogoDoodadShape(geom, &doodad->logo);
162             rbounds = &shape->bounds;
163             break;
164         default:
165             tbounds.x1 = tbounds.x2 = doodad->any.left;
166             tbounds.y1 = tbounds.y2 = doodad->any.top;
167             rbounds = &tbounds;
168             break;
169         }
170
171         _XkbCheckBounds(bounds, rbounds->x1, rbounds->y1);
172         _XkbCheckBounds(bounds, rbounds->x2, rbounds->y2);
173     }
174
175     return True;
176 }