summaryrefslogtreecommitdiff
path: root/gsk/gl/glutilsprivate.h
blob: fb2c67d11bfc87e77cfb8ce5e700f886c4b1b54f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287

#pragma once

#define SANITY_CHECKS 0


enum {
  NINE_SLICE_TOP_LEFT      = 0,
  NINE_SLICE_TOP_CENTER    = 1,
  NINE_SLICE_TOP_RIGHT     = 2,
  NINE_SLICE_LEFT_CENTER   = 3,
  NINE_SLICE_CENTER        = 4,
  NINE_SLICE_RIGHT_CENTER  = 5,
  NINE_SLICE_BOTTOM_LEFT   = 6,
  NINE_SLICE_BOTTOM_CENTER = 7,
  NINE_SLICE_BOTTOM_RIGHT  = 8,
};
#define NINE_SLICE_SIZE 9 /* Hah. */

typedef struct
{
  int texture_id;
  float x;
  float y;
  float x2;
  float y2;
} TextureRegion;

static inline bool G_GNUC_PURE
slice_is_visible (const cairo_rectangle_int_t *r)
{
  return (r->width > 0 && r->height > 0);
}

static inline void
nine_slice_rounded_rect (const GskRoundedRect  *rect,
                         cairo_rectangle_int_t *out_rects)
{
  const graphene_point_t *origin = &rect->bounds.origin;
  const graphene_size_t *size = &rect->bounds.size;
  const int top_height = ceilf (MAX (rect->corner[GSK_CORNER_TOP_LEFT].height,
                                     rect->corner[GSK_CORNER_TOP_RIGHT].height));
  const int bottom_height = ceilf (MAX (rect->corner[GSK_CORNER_BOTTOM_LEFT].height,
                                        rect->corner[GSK_CORNER_BOTTOM_RIGHT].height));
  const int right_width = ceilf (MAX (rect->corner[GSK_CORNER_TOP_RIGHT].width,
                                      rect->corner[GSK_CORNER_BOTTOM_RIGHT].width));
  const int left_width = ceilf (MAX (rect->corner[GSK_CORNER_TOP_LEFT].width,
                                     rect->corner[GSK_CORNER_BOTTOM_LEFT].width));

  /* Top left */
  out_rects[0] = (cairo_rectangle_int_t) {
                   origin->x, origin->y,
                   left_width, top_height,
                 };

  /* Top center */
  out_rects[1] = (cairo_rectangle_int_t) {
                   origin->x + size->width / 2.0 - 1, origin->y,
                   1, top_height,
                 };

  /* Top right */
  out_rects[2] = (cairo_rectangle_int_t) {
                   origin->x + size->width - right_width, origin->y,
                   right_width, top_height
                 };

  /* Left center */
  out_rects[3] = (cairo_rectangle_int_t) {
                   origin->x, origin->y + size->height / 2,
                   left_width, 1,
                 };

  /* center */
  out_rects[4] = (cairo_rectangle_int_t) {
                   origin->x + size->width / 2.0,
                   origin->y + size->height / 2.0,
                   1, 1
                 };

  /* Right center */
  out_rects[5] = (cairo_rectangle_int_t) {
                   origin->x + size->width - right_width,
                   origin->y + (size->height / 2.0),
                   right_width,
                   1,
                 };

  /* Bottom Left */
  out_rects[6] = (cairo_rectangle_int_t) {
                   origin->x, origin->y + size->height - bottom_height,
                   left_width, bottom_height,
                 };

  /* Bottom center */
  out_rects[7] = (cairo_rectangle_int_t) {
                   origin->x + (size->width / 2.0), origin->y + size->height - bottom_height,
                   1, bottom_height,
                 };

  /* Bottom right */
  out_rects[8] = (cairo_rectangle_int_t) {
                   origin->x + size->width - right_width,
                   origin->y + size->height - bottom_height,
                   right_width, bottom_height,
                 };

#if SANITY_CHECKS
  g_assert_cmpfloat (size->width, >=, left_width + right_width);
  g_assert_cmpfloat (size->height, >=, top_height + bottom_height);
#endif
}

static inline void
nine_slice_grow (cairo_rectangle_int_t *slices,
                 const int              amount)
{
  /* top left */
  slices[0].x -= amount;
  slices[0].y -= amount;
  if (amount > slices[0].width)
    slices[0].width += amount * 2;
  else
    slices[0].width += amount;

  if (amount > slices[0].height)
    slices[0].height += amount * 2;
  else
    slices[0].height += amount;


  /* Top center */
  slices[1].y -= amount;
  if (amount > slices[1].height)
    slices[1].height += amount * 2;
  else
    slices[1].height += amount;


  /* top right */
  slices[2].y -= amount;
  if (amount > slices[2].width)
    {
      slices[2].x -= amount;
      slices[2].width += amount * 2;
    }
  else
    {
     slices[2].width += amount;
    }

  if (amount > slices[2].height)
    slices[2].height += amount * 2;
  else
    slices[2].height += amount;



  slices[3].x -= amount;
  if (amount > slices[3].width)
    slices[3].width += amount * 2;
  else
    slices[3].width += amount;

  /* Leave Britney^Wcenter alone */

  if (amount > slices[5].width)
    {
      slices[5].x -= amount;
      slices[5].width += amount * 2;
    }
  else
    {
      slices[5].width += amount;
    }


  /* Bottom left */
  slices[6].x -= amount;
  if (amount > slices[6].width)
    {
      slices[6].width += amount * 2;
    }
  else
    {
      slices[6].width += amount;
    }

  if (amount > slices[6].height)
    {
      slices[6].y -= amount;
      slices[6].height += amount * 2;
    }
  else
    {
      slices[6].height += amount;
    }


  /* Bottom center */
  if (amount > slices[7].height)
    {
      slices[7].y -= amount;
      slices[7].height += amount * 2;
    }
  else
    {
      slices[7].height += amount;
    }

  if (amount > slices[8].width)
    {
      slices[8].x -= amount;
      slices[8].width += amount * 2;
    }
  else
    {
      slices[8].width += amount;
    }

  if (amount > slices[8].height)
    {
      slices[8].y -= amount;
      slices[8].height += amount * 2;
    }
  else
    {
      slices[8].height += amount;
    }

#if SANITY_CHECKS
  {
    for (int i = 0; i < 9; i ++)
      {
        g_assert_cmpint (slices[i].x, >=, 0);
        g_assert_cmpint (slices[i].y, >=, 0);
        g_assert_cmpint (slices[i].width, >=, 0);
        g_assert_cmpint (slices[i].height, >=, 0);
      }

    /* Rows don't overlap */
    for (int i = 0; i < 3; i++)
      {
        g_assert_cmpint (slices[i * 3 + 0].x + slices[i * 3 + 0].width, <, slices[i * 3 + 1].x);
      }
  }
#endif

}

static inline void
nine_slice_to_texture_coords (const cairo_rectangle_int_t *slices,
                              const int                    texture_width,
                              const int                    texture_height,
                              TextureRegion               *out_regions)
{
  const float fw = (float)texture_width;
  const float fh = (float)texture_height;
  int i;

  for (i = 0; i < 9; i++)
    {
      out_regions[i] = (TextureRegion) {
        0, /* Texture id */
        slices[i].x / fw,
        1.0 - ((slices[i].y + slices[i].height) / fh),
        (slices[i].x + slices[i].width)  / fw,
        1.0 - (slices[i].y / fh),
      };
    }

#if SANITY_CHECKS
  {
    for (i = 0; i < 9; i++)
      {
        const TextureRegion *r = &out_regions[i];
        g_assert_cmpfloat (r->x, >=, 0);
        g_assert_cmpfloat (r->x, <=, 1);
        g_assert_cmpfloat (r->y, >=, 0);
        g_assert_cmpfloat (r->y, <=, 1);

        g_assert_cmpfloat (r->x, <, r->x2);
        g_assert_cmpfloat (r->y, <, r->y2);
      }
  }
#endif
}