ObjectivelyMVC
Object oriented MVC framework for SDL3 and GNU C
Loading...
Searching...
No Matches
Style.c File Reference
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <Objectively.h>
#include "Style.h"

Go to the source code of this file.

Macros

#define _Class   _Style
 

Functions

Class * _Style (void)
 
static void addAttribute (Style *self, const char *attr, ident value)
 
static void addAttributes (Style *self, const Dictionary *attributes)
 
static void addBoolAttribute (Style *self, const char *attr, bool value)
 
static void addCharactersAttribute (Style *self, const char *attr, const char *value)
 
static void addColorAttribute (Style *self, const char *attr, const SDL_Color *color)
 
static void addDoubleAttribute (Style *self, const char *attr, double value)
 
static void addEnumAttribute (Style *self, const char *attr, const EnumName *names, int value)
 
static void addFloatAttribute (Style *self, const char *attr, float value)
 
static void addIntegerAttribute (Style *self, const char *attr, int value)
 
static void addPointAttribute (Style *self, const char *attr, const SDL_Point *value)
 
static void addRectangleAttribute (Style *self, const char *attr, const SDL_Rect *value)
 
static void addSelector (Style *self, Selector *selector)
 
static void addSizeAttribute (Style *self, const char *attr, const SDL_Size *value)
 
static ident attributeValue (const Style *self, const char *attr)
 
static void dealloc (Object *self)
 
static String * description (const Object *self)
 
static int hash (const Object *self)
 
static void initialize (Class *clazz)
 
static StyleinitWithAttributes (Style *self, const Dictionary *attributes)
 
static StyleinitWithRules (Style *self, const char *rules)
 
static bool isComputedEqual (const Style *self, const Style *other)
 
static bool isEqual (const Object *self, const Object *other)
 
static Array * parse (const char *css)
 
static ident parseValue (String *string)
 
static void removeAllAttributes (Style *self)
 
static void removeAttribute (Style *self, const char *attr)
 

Macro Definition Documentation

◆ _Class

#define _Class   _Style

Definition at line 32 of file Style.c.

Function Documentation

◆ _Style()

Class * _Style ( void  )

Definition at line 539 of file Style.c.

539 {
540 static Class *clazz;
541 static Once once;
542
543 do_once(&once, {
544 clazz = _initialize(&(const ClassDef) {
545 .name = "Style",
546 .superclass = _Object(),
547 .instanceSize = sizeof(Style),
548 .interfaceOffset = offsetof(Style, interface),
549 .interfaceSize = sizeof(StyleInterface),
551 });
552 });
553
554 return clazz;
555}
static void initialize(Class *clazz)
Definition Style.c:506
The Style type.
Definition Style.h:43

◆ addAttribute()

static void addAttribute ( Style self,
const char *  attr,
ident  value 
)
static

Definition at line 102 of file Style.c.

102 {
103
104 assert(attr);
105 assert(value);
106
107 char *key = strtrim(attr);
108 assert(key);
109
110 $((Dictionary *) self->attributes, setObjectForKeyPath, value, key);
111
112 free(key);
113}
Dictionary * attributes
Definition Style.h:59

◆ addAttributes()

static void addAttributes ( Style self,
const Dictionary *  attributes 
)
static

Definition at line 119 of file Style.c.

119 {
120
121 assert(attributes);
122
123 $((Dictionary *) self->attributes, addEntriesFromDictionary, attributes);
124}

◆ addBoolAttribute()

static void addBoolAttribute ( Style self,
const char *  attr,
bool  value 
)
static

Definition at line 130 of file Style.c.

130 {
131 $(self, addAttribute, attr, $$(Boole, valueof, value));
132}
static void addAttribute(Style *self, const char *attr, ident value)
Definition Style.c:102

◆ addCharactersAttribute()

static void addCharactersAttribute ( Style self,
const char *  attr,
const char *  value 
)
static

Definition at line 138 of file Style.c.

138 {
139
140 String *string = $$(String, stringWithCharacters, value);
141
142 $(self, addAttribute, attr, string);
143
144 release(string);
145}

◆ addColorAttribute()

static void addColorAttribute ( Style self,
const char *  attr,
const SDL_Color *  color 
)
static

Definition at line 151 of file Style.c.

151 {
152
153 Number *r = $$(Number, numberWithValue, color->r);
154 Number *g = $$(Number, numberWithValue, color->g);
155 Number *b = $$(Number, numberWithValue, color->b);
156 Number *a = $$(Number, numberWithValue, color->a);
157
158 Array *array = $$(Array, arrayWithObjects, r, g, b, a, NULL);
159
160 $(self, addAttribute, attr, array);
161
162 release(array);
163 release(r);
164 release(g);
165 release(b);
166 release(a);
167}

◆ addDoubleAttribute()

static void addDoubleAttribute ( Style self,
const char *  attr,
double  value 
)
static

Definition at line 173 of file Style.c.

173 {
174
175 Number *number = $$(Number, numberWithValue, value);
176
177 $(self, addAttribute, attr, number);
178
179 release(number);
180}

◆ addEnumAttribute()

static void addEnumAttribute ( Style self,
const char *  attr,
const EnumName *  names,
int  value 
)
static

Definition at line 186 of file Style.c.

186 {
187
188 String *string = nameof(names, value);
189
190 $(self, addAttribute, attr, string);
191
192 release(string);
193}

◆ addFloatAttribute()

static void addFloatAttribute ( Style self,
const char *  attr,
float  value 
)
static

Definition at line 199 of file Style.c.

199 {
200 $(self, addDoubleAttribute, attr, value);
201}
static void addDoubleAttribute(Style *self, const char *attr, double value)
Definition Style.c:173

◆ addIntegerAttribute()

static void addIntegerAttribute ( Style self,
const char *  attr,
int  value 
)
static

Definition at line 207 of file Style.c.

207 {
208 $(self, addDoubleAttribute, attr, value);
209}

◆ addPointAttribute()

static void addPointAttribute ( Style self,
const char *  attr,
const SDL_Point *  value 
)
static

Definition at line 215 of file Style.c.

215 {
216
217 Number *x = $$(Number, numberWithValue, value->x);
218 Number *y = $$(Number, numberWithValue, value->y);
219
220 Array *array = $$(Array, arrayWithObjects, x, y, NULL);
221
222 $(self, addAttribute, attr, array);
223
224 release(array);
225 release(x);
226 release(y);
227}

◆ addRectangleAttribute()

static void addRectangleAttribute ( Style self,
const char *  attr,
const SDL_Rect *  value 
)
static

Definition at line 233 of file Style.c.

233 {
234
235 Number *x = $$(Number, numberWithValue, value->x);
236 Number *y = $$(Number, numberWithValue, value->y);
237 Number *w = $$(Number, numberWithValue, value->w);
238 Number *h = $$(Number, numberWithValue, value->h);
239
240 Array *array = $$(Array, arrayWithObjects, x, y, w, h, NULL);
241
242 $(self, addAttribute, attr, array);
243
244 release(array);
245 release(x);
246 release(y);
247 release(w);
248 release(h);
249}

◆ addSelector()

static void addSelector ( Style self,
Selector selector 
)
static

Definition at line 255 of file Style.c.

255 {
256
257 assert(selector);
258
259 $((Array *) self->selectors, addObject, selector);
260}
Array * selectors
The Selectors.
Definition Style.h:64

◆ addSizeAttribute()

static void addSizeAttribute ( Style self,
const char *  attr,
const SDL_Size *  value 
)
static

Definition at line 266 of file Style.c.

266 {
267
268 Number *w = $$(Number, numberWithValue, value->w);
269 Number *h = $$(Number, numberWithValue, value->h);
270
271 Array *array = $$(Array, arrayWithObjects, w, h, NULL);
272
273 $(self, addAttribute, attr, array);
274
275 release(array);
276 release(w);
277 release(h);
278}

◆ attributeValue()

static ident attributeValue ( const Style self,
const char *  attr 
)
static

Definition at line 284 of file Style.c.

284 {
285 return $((Dictionary *) self->attributes, objectForKeyPath, attr);
286}

◆ dealloc()

static void dealloc ( Object *  self)
static
See also
Object::dealloc(Object *)

Definition at line 39 of file Style.c.

39 {
40
41 Style *this = (Style *) self;
42
43 release(this->attributes);
44 release(this->selectors);
45
46 super(Object, self, dealloc);
47}
static void dealloc(Object *self)
Definition Style.c:39

◆ description()

static String * description ( const Object *  self)
static
See also
Object::description(const Object *)

Definition at line 52 of file Style.c.

52 {
53
54 const Style *this = (Style *) self;
55
56 return $((Array *) this->selectors, componentsJoinedByCharacters, ", ");
57}

◆ hash()

static int hash ( const Object *  self)
static
See also
Object::hash(const Object *)

Definition at line 62 of file Style.c.

62 {
63
64 Style *this = (Style *) self;
65
66 int hash = HASH_SEED;
67
68 hash = HashForObject(hash, this->attributes);
69 hash = HashForObject(hash, this->selectors);
70
71 return hash;
72}
static int hash(const Object *self)
Definition Style.c:62

◆ initialize()

static void initialize ( Class *  clazz)
static
See also
Class::initialize(Class *)

Definition at line 506 of file Style.c.

506 {
507
508 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
509 ((ObjectInterface *) clazz->interface)->description = description;
510 ((ObjectInterface *) clazz->interface)->hash = hash;
511 ((ObjectInterface *) clazz->interface)->isEqual = isEqual;
512
513 ((StyleInterface *) clazz->interface)->addAttribute = addAttribute;
514 ((StyleInterface *) clazz->interface)->addAttributes = addAttributes;
515 ((StyleInterface *) clazz->interface)->addBoolAttribute = addBoolAttribute;
516 ((StyleInterface *) clazz->interface)->addCharactersAttribute = addCharactersAttribute;
517 ((StyleInterface *) clazz->interface)->addColorAttribute = addColorAttribute;
518 ((StyleInterface *) clazz->interface)->addDoubleAttribute = addDoubleAttribute;
519 ((StyleInterface *) clazz->interface)->addEnumAttribute = addEnumAttribute;
520 ((StyleInterface *) clazz->interface)->addFloatAttribute = addFloatAttribute;
521 ((StyleInterface *) clazz->interface)->addIntegerAttribute = addIntegerAttribute;
522 ((StyleInterface *) clazz->interface)->addPointAttribute = addPointAttribute;
523 ((StyleInterface *) clazz->interface)->addRectangleAttribute = addRectangleAttribute;
524 ((StyleInterface *) clazz->interface)->addSelector = addSelector;
525 ((StyleInterface *) clazz->interface)->addSizeAttribute = addSizeAttribute;
526 ((StyleInterface *) clazz->interface)->attributeValue = attributeValue;
527 ((StyleInterface *) clazz->interface)->initWithAttributes = initWithAttributes;
528 ((StyleInterface *) clazz->interface)->initWithRules = initWithRules;
529 ((StyleInterface *) clazz->interface)->isComputedEqual = isComputedEqual;
530 ((StyleInterface *) clazz->interface)->parse = parse;
531 ((StyleInterface *) clazz->interface)->removeAllAttributes = removeAllAttributes;
532 ((StyleInterface *) clazz->interface)->removeAttribute = removeAttribute;
533}
static bool isComputedEqual(const Style *self, const Style *other)
Definition Style.c:331
static void addIntegerAttribute(Style *self, const char *attr, int value)
Definition Style.c:207
static void addEnumAttribute(Style *self, const char *attr, const EnumName *names, int value)
Definition Style.c:186
static bool isEqual(const Object *self, const Object *other)
Definition Style.c:77
static Style * initWithRules(Style *self, const char *rules)
Definition Style.c:308
static Array * parse(const char *css)
Definition Style.c:422
static String * description(const Object *self)
Definition Style.c:52
static void addCharactersAttribute(Style *self, const char *attr, const char *value)
Definition Style.c:138
static void removeAttribute(Style *self, const char *attr)
Definition Style.c:489
static void addSizeAttribute(Style *self, const char *attr, const SDL_Size *value)
Definition Style.c:266
static void removeAllAttributes(Style *self)
Definition Style.c:497
static void addColorAttribute(Style *self, const char *attr, const SDL_Color *color)
Definition Style.c:151
static Style * initWithAttributes(Style *self, const Dictionary *attributes)
Definition Style.c:292
static ident attributeValue(const Style *self, const char *attr)
Definition Style.c:284
static void addRectangleAttribute(Style *self, const char *attr, const SDL_Rect *value)
Definition Style.c:233
static void addSelector(Style *self, Selector *selector)
Definition Style.c:255
static void addFloatAttribute(Style *self, const char *attr, float value)
Definition Style.c:199
static void addBoolAttribute(Style *self, const char *attr, bool value)
Definition Style.c:130
static void addPointAttribute(Style *self, const char *attr, const SDL_Point *value)
Definition Style.c:215
static void addAttributes(Style *self, const Dictionary *attributes)
Definition Style.c:119
Style * initWithRules(Style *self, const char *rules)
Initializes this Style with the given CSS selector rules.
Definition Style.c:308
bool isComputedEqual(const Style *self, const Style *other)
Performs a fast, rule-based comparison of this Style to the given Style.
Definition Style.c:331

◆ initWithAttributes()

static Style * initWithAttributes ( Style self,
const Dictionary *  attributes 
)
static

Definition at line 292 of file Style.c.

292 {
293
294 self = $(self, initWithRules, NULL);
295 if (self) {
296 if (attributes) {
297 $(self, addAttributes, attributes);
298 }
299 }
300
301 return self;
302}

◆ initWithRules()

static Style * initWithRules ( Style self,
const char *  rules 
)
static

Definition at line 308 of file Style.c.

308 {
309
310 self = (Style *) super(Object, self, init);
311 if (self) {
312
313 self->selectors = $$(Selector, parse, rules);
314 assert(self->selectors);
315
316 for (size_t i = 0; i < self->selectors->count; i++) {
317 ((Selector *) self->selectors->elements[i])->style = self;
318 }
319
320 self->attributes = (Dictionary *) $$(Dictionary, dictionaryWithCapacity, 4);
321 assert(self->attributes);
322 }
323
324 return self;
325}
static View * init(View *self)
Definition Box.c:67
Selectors are comprised of one or more SelectorSequences.
Definition Selector.h:49

◆ isComputedEqual()

static bool isComputedEqual ( const Style self,
const Style other 
)
static

Definition at line 331 of file Style.c.

331 {
332
333 assert(other);
334
335 if (self->selectors->count == other->selectors->count) {
336
337 for (size_t i = 0; i < self->selectors->count; i++) {
338
339 const Selector *this = $(self->selectors, objectAtIndex, i);
340 const Selector *that = $(other->selectors, objectAtIndex, i);
341
342 if (strcmp(this->rule, that->rule)) {
343 return false;
344 }
345 }
346
347 return true;
348 }
349
350 return false;
351}
char * rule
The rule, as provided by the user.
Definition Selector.h:70

◆ isEqual()

static bool isEqual ( const Object *  self,
const Object *  other 
)
static
See also
Object::isEqual(const Object *, const Object *)

Definition at line 77 of file Style.c.

77 {
78
79 if (super(Object, self, isEqual, other)) {
80 return true;
81 }
82
83 if (other && $(other, isKindOfClass, _Style())) {
84
85 const Style *this = (Style *) self;
86 const Style *that = (Style *) other;
87
88 if ($((Object *) this->selectors, isEqual, (Object *) that->selectors)) {
89 return $((Object *) this->attributes, isEqual, (Object *) that->attributes);
90 }
91 }
92
93 return false;
94}
Class * _Style(void)
Definition Style.c:539

◆ parse()

static Array * parse ( const char *  css)
static

Definition at line 422 of file Style.c.

422 {
423
424 Array *styles = $$(Array, array);
425 assert(styles);
426
427 if (css) {
428
429 StringReader *reader = $(alloc(StringReader), initWithCharacters, css);
430 assert(reader);
431
432 Style *style = NULL;
433 char *attr = NULL;
434
435 while (true) {
436
437 const Unicode *charset = style ? L"{:;}" : L"{}";
438 Unicode stop;
439
440 String *token = $(reader, readToken, charset, &stop);
441 if (token) {
442 switch (stop) {
443 case '{':
444 style = $(alloc(Style), initWithRules, token->chars);
445 assert(style);
446 break;
447 case ':':
448 if (style) {
449 attr = strtrim(token->chars);
450 assert(attr);
451 }
452 break;
453 case ';':
454 if (style && attr) {
455
456 ident value = parseValue(token);
457 $(style, addAttribute, attr, value);
458
459 free(attr);
460 attr = NULL;
461
462 release(value);
463 }
464 break;
465 case '}':
466 if (style) {
467 $(styles, addObject, style);
468 style = release(style);
469 }
470 break;
471 }
472
473 release(token);
474 } else {
475 break;
476 }
477 }
478
479 release(reader);
480 }
481
482 return (Array *) styles;
483}
static ident parseValue(String *string)
Definition Style.c:356
static Stylesheet * initWithCharacters(Stylesheet *self, const char *chars)
Definition Stylesheet.c:124

◆ parseValue()

static ident parseValue ( String *  string)
static
Returns
The value Object parsed from the given String.

Definition at line 356 of file Style.c.

356 {
357
358 ident value = NULL;
359
360 assert(string);
361
362 String *trimmed = $(string, trimmedString);
363 assert(trimmed);
364
365 StringReader *reader = $(alloc(StringReader), initWithString, trimmed);
366 assert(reader);
367
368 const Unicode *charset = L", \n\t";
369 Unicode stop;
370
371 String *token = $(reader, readToken, charset, &stop);
372 if (token) {
373
374 NumberFormatter *formatter = $(alloc(NumberFormatter), initWithFormat, NULL);
375 assert(formatter);
376
377 Number *number = $(formatter, numberFromString, token);
378
379 if (stop == READER_EOF) {
380
381 if (number) {
382 value = retain(number);
383 } else if (strcmp("true", token->chars) == 0) {
384 value = retain($$(Boole, True));
385 } else if (strcmp("false", token->chars) == 0) {
386 value = retain($$(Boole, False));
387 } else {
388 value = $((Object *) token, copy);
389 }
390
391 } else if (number) {
392 value = $$(Array, arrayWithCapacity, 4);
393
394 while (token) {
395
396 Object *obj = parseValue(token);
397 $((Array *) value, addObject, obj);
398 release(obj);
399 release(token);
400
401 token = $(reader, readToken, charset, NULL);
402 }
403 } else {
404 value = retain(trimmed);
405 }
406
407 release(number);
408 release(formatter);
409 release(token);
410 }
411
412 release(reader);
413 release(trimmed);
414
415 return value;
416}
static Stylesheet * initWithString(Stylesheet *self, const String *string)
Definition Stylesheet.c:195
static Warning * initWithFormat(Warning *self, WarningType type, const char *fmt,...)
Definition Warning.c:50

◆ removeAllAttributes()

static void removeAllAttributes ( Style self)
static

Definition at line 497 of file Style.c.

497 {
498 $((Dictionary *) self->attributes, removeAllObjects);
499}

◆ removeAttribute()

static void removeAttribute ( Style self,
const char *  attr 
)
static

Definition at line 489 of file Style.c.

489 {
490 $((Dictionary *) self->attributes, removeObjectForKeyPath, attr);
491}