cursetree/cursetree/dims.c

88 lines
2.5 KiB
C

#include <assert.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include "dims.h"
#include "node.h"
#include "util.h"
struct ct_dims *new_dims(const int x, const int y, const int width, const int height) {
struct ct_dims *dims;
dims = (struct ct_dims *)malloc(sizeof(struct ct_dims));
*dims = (struct ct_dims){
.x = x,
.y = y,
.width = width,
.height = height,
};
return dims;
}
struct ct_dims *dup_dims(const struct ct_dims *const dims) {
struct ct_dims *dup;
dup = (struct ct_dims *)malloc(sizeof(struct ct_dims));
memcpy(dup, dims, sizeof(struct ct_dims));
return dup;
}
static struct ct_bounds *__bounds(const enum ct_boundtype type, const int wmin,
const int wmax, const int hmin,
const int hmax) {
struct ct_bounds *bounds;
bounds = (struct ct_bounds *)malloc(sizeof(struct ct_bounds));
*bounds = (struct ct_bounds){
.type = type,
.wmin = wmin,
.wmax = wmax,
.hmin = hmin,
.hmax = hmax,
};
return bounds;
}
static inline void __clamp(int *const val, const int min, const int max,
const int do_ceiling) {
if (*val == __BOUND_UNLIMITED)
*val = do_ceiling ? max : min;
else
*val = clampi(*val, min, max);
}
#define CLAMP_ABS(val, is_max) \
(__clamp(&val, __BOUND_ABS_MIN, __BOUND_ABS_MAX, is_max))
#define CLAMP_REL(val, is_max) \
(__clamp(&val, __BOUND_REL_MIN, __BOUND_REL_MAX, is_max))
struct ct_bounds *bounds_none(void) {
return __bounds(BOUND_NONE, __BOUND_ABS_MIN, __BOUND_ABS_MAX, __BOUND_ABS_MIN,
__BOUND_ABS_MAX);
}
struct ct_bounds *bounds_absolute(int wmin, int wmax,
int hmin, int hmax) {
CLAMP_ABS(wmin, false);
CLAMP_ABS(wmax, true);
CLAMP_ABS(hmin, false);
CLAMP_ABS(hmax, true);
return __bounds(BOUND_ABSOLUTE, wmin, wmax, hmin, hmax);
}
struct ct_bounds *bounds_relative(int wmin, int wmax,
int hmin, int hmax) {
CLAMP_REL(wmin, false);
CLAMP_REL(wmax, true);
CLAMP_REL(hmin, false);
CLAMP_REL(hmax, true);
return __bounds(BOUND_RELATIVE, wmin, wmax, hmin, hmax);
}
struct ct_bounds *dup_bounds(const struct ct_bounds *const bounds) {
struct ct_bounds *dup;
dup = (struct ct_bounds *)malloc(sizeof(struct ct_bounds));
memcpy(dup, bounds, sizeof(struct ct_bounds));
return dup;
}