| 1 |
/* |
| 2 |
* $LynxId: HTStyle.c,v 1.18 2025/01/06 15:34:03 tom Exp $ |
| 3 |
* |
| 4 |
* Style Implementation for Hypertext HTStyle.c |
| 5 |
* ================================== |
| 6 |
* |
| 7 |
* Styles allow the translation between a logical property |
| 8 |
* of a piece of text and its physical representation. |
| 9 |
* |
| 10 |
* A StyleSheet is a collection of styles, defining the |
| 11 |
* translation necessary to |
| 12 |
* represent a document. It is a linked list of styles. |
| 13 |
*/ |
| 14 |
|
| 15 |
#include <HTUtils.h> |
| 16 |
#include <HTStyle.h> |
| 17 |
|
| 18 |
#include <LYLeaks.h> |
| 19 |
|
| 20 |
/* Create a new style |
| 21 |
*/ |
| 22 |
HTStyle *HTStyleNew(void) |
| 23 |
{ |
| 24 |
HTStyle *self = typecalloc(HTStyle); |
| 25 |
|
| 26 |
if (self == NULL) |
| 27 |
outofmem(__FILE__, "HTStyleNew"); |
| 28 |
return self; |
| 29 |
} |
| 30 |
|
| 31 |
/* Create a new style with a name |
| 32 |
*/ |
| 33 |
HTStyle *HTStyleNewNamed(const char *name) |
| 34 |
{ |
| 35 |
HTStyle *self = HTStyleNew(); |
| 36 |
|
| 37 |
StrAllocCopy(self->w_name, name); |
| 38 |
self->id = -1; /* <0 */ |
| 39 |
return self; |
| 40 |
} |
| 41 |
|
| 42 |
/* Free a style |
| 43 |
*/ |
| 44 |
HTStyle *HTStyleFree(HTStyle *self) |
| 45 |
{ |
| 46 |
FREE(self->w_name); |
| 47 |
FREE(self->w_SGMLTag); |
| 48 |
FREE(self); |
| 49 |
return NULL; |
| 50 |
} |
| 51 |
|
| 52 |
/* StyleSheet Functions |
| 53 |
* ==================== |
| 54 |
*/ |
| 55 |
|
| 56 |
/* Searching for styles: |
| 57 |
*/ |
| 58 |
HTStyle *HTStyleNamed(HTStyleSheet *self, const char *name) |
| 59 |
{ |
| 60 |
HTStyle *scan; |
| 61 |
|
| 62 |
for (scan = self->styles; scan; scan = scan->next) |
| 63 |
if (0 == strcmp(GetHTStyleName(scan), name)) |
| 64 |
return scan; |
| 65 |
CTRACE((tfp, "StyleSheet: No style named `%s'\n", name)); |
| 66 |
return NULL; |
| 67 |
} |
| 68 |
|
| 69 |
/* Add a style to a sheet |
| 70 |
* ---------------------- |
| 71 |
*/ |
| 72 |
HTStyleSheet *HTStyleSheetAddStyle(HTStyleSheet *self, HTStyle *style) |
| 73 |
{ |
| 74 |
style->next = NULL; /* The style will go on the end */ |
| 75 |
if (!self->styles) { |
| 76 |
self->styles = style; |
| 77 |
} else { |
| 78 |
HTStyle *scan; |
| 79 |
|
| 80 |
for (scan = self->styles; scan->next; scan = scan->next) ; /* Find end */ |
| 81 |
scan->next = style; |
| 82 |
} |
| 83 |
return self; |
| 84 |
} |
| 85 |
|
| 86 |
/* Remove the given object from a style sheet if it exists |
| 87 |
*/ |
| 88 |
HTStyleSheet *HTStyleSheetRemoveStyle(HTStyleSheet *self, HTStyle *style) |
| 89 |
{ |
| 90 |
if (self->styles == style) { |
| 91 |
self->styles = style->next; |
| 92 |
return self; |
| 93 |
} else { |
| 94 |
HTStyle *scan; |
| 95 |
|
| 96 |
for (scan = self->styles; scan; scan = scan->next) { |
| 97 |
if (scan->next == style) { |
| 98 |
scan->next = style->next; |
| 99 |
return self; |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
return NULL; |
| 104 |
} |
| 105 |
|
| 106 |
/* Create new style sheet |
| 107 |
*/ |
| 108 |
|
| 109 |
HTStyleSheet *HTStyleSheetNew(void) |
| 110 |
{ |
| 111 |
HTStyleSheet *self = typecalloc(HTStyleSheet); |
| 112 |
|
| 113 |
if (self == NULL) |
| 114 |
outofmem(__FILE__, "HTStyleSheetNew"); |
| 115 |
return self; |
| 116 |
} |
| 117 |
|
| 118 |
/* Free off a style sheet pointer |
| 119 |
*/ |
| 120 |
HTStyleSheet *HTStyleSheetFree(HTStyleSheet *self) |
| 121 |
{ |
| 122 |
HTStyle *style; |
| 123 |
|
| 124 |
while ((style = self->styles) != NULL) { |
| 125 |
self->styles = style->next; |
| 126 |
HTStyleFree(style); |
| 127 |
} |
| 128 |
FREE(self); |
| 129 |
return NULL; |
| 130 |
} |