Skip to content

Commit 6869683

Browse files
committedOct 7, 2019
fix(css): setting font style parser's style_handler doesn't work
1 parent 811585b commit 6869683

File tree

3 files changed

+220
-190
lines changed

3 files changed

+220
-190
lines changed
 

‎include/LCUI/gui/css_parser.h

+50-43
Original file line numberDiff line numberDiff line change
@@ -36,38 +36,40 @@
3636
LCUI_BEGIN_HEADER
3737

3838
#define CASE_WHITE_SPACE \
39-
case ' ':\
40-
case '\n':\
41-
case '\r':\
39+
case ' ': \
40+
case '\n': \
41+
case '\r': \
4242
case '\t'
4343

44-
#define CSSParser_GetChar( CTX ) do {\
45-
(CTX)->buffer[(ctx)->pos++] = *((CTX)->cur);\
46-
} while( 0 );
44+
#define CSSParser_GetChar(CTX) \
45+
do { \
46+
(CTX)->buffer[(ctx)->pos++] = *((CTX)->cur); \
47+
} while (0);
4748

48-
#define CSSParser_EndBuffer( CTX ) do {\
49-
(CTX)->buffer[(CTX)->pos] = 0;\
50-
(CTX)->pos = 0;\
51-
} while( 0 );
49+
#define CSSParser_EndBuffer(CTX) \
50+
do { \
51+
(CTX)->buffer[(CTX)->pos] = 0; \
52+
(CTX)->pos = 0; \
53+
} while (0);
5254

5355
#define CSSParser_GetRuleParser(CTX) &ctx->rule.parsers[CSS_RULE_FONT_FACE]
5456

5557
typedef enum LCUI_CSSParserTarget {
56-
CSS_TARGET_NONE, /**< 无 */
57-
CSS_TARGET_RULE_NAME, /**< 规则名称 */
58-
CSS_TARGET_RULE_DATA, /**< 规则数据 */
59-
CSS_TARGET_SELECTOR, /**< 选择器 */
60-
CSS_TARGET_KEY, /**< 属性名 */
61-
CSS_TARGET_VALUE, /**< 属性值 */
62-
CSS_TARGET_COMMENT, /**< 注释 */
58+
CSS_TARGET_NONE, /**< 无 */
59+
CSS_TARGET_RULE_NAME, /**< 规则名称 */
60+
CSS_TARGET_RULE_DATA, /**< 规则数据 */
61+
CSS_TARGET_SELECTOR, /**< 选择器 */
62+
CSS_TARGET_KEY, /**< 属性名 */
63+
CSS_TARGET_VALUE, /**< 属性值 */
64+
CSS_TARGET_COMMENT, /**< 注释 */
6365
CSS_TARGET_TOTAL_NUM
6466
} LCUI_CSSParserTarget;
6567

6668
typedef enum LCUI_CSSRule {
6769
CSS_RULE_NONE,
68-
CSS_RULE_FONT_FACE, /**< @font-face */
69-
CSS_RULE_IMPORT, /**< @import */
70-
CSS_RULE_MEDIA, /**< @media */
70+
CSS_RULE_FONT_FACE, /**< @font-face */
71+
CSS_RULE_IMPORT, /**< @import */
72+
CSS_RULE_MEDIA, /**< @media */
7173
CSS_RULE_TOTAL_NUM
7274
} LCUI_CSSRule;
7375

@@ -91,7 +93,7 @@ typedef struct LCUI_CSSParserCommentContextRec_ LCUI_CSSParserCommentContextRec;
9193
typedef struct LCUI_CSSParserCommentContextRec_ *LCUI_CSSParserCommentContext;
9294
typedef struct LCUI_CSSParserRuleContextRec_ LCUI_CSSParserRuleContextRec;
9395
typedef struct LCUI_CSSParserRuleContextRec_ *LCUI_CSSParserRuleContext;
94-
typedef int(*LCUI_CSSParserFunction)(LCUI_CSSParserContext ctx);
96+
typedef int (*LCUI_CSSParserFunction)(LCUI_CSSParserContext ctx);
9597

9698
struct LCUI_CSSParserRec_ {
9799
LCUI_CSSParserFunction parse;
@@ -109,44 +111,45 @@ typedef LCUI_CSSRuleParserRec LCUI_CSSRuleParsers[CSS_RULE_TOTAL_NUM];
109111

110112
/** 样式属性的解析器 */
111113
struct LCUI_CSSPropertyParserRec_ {
112-
int key; /**< 标识,在解析数据时可以使用它访问样式表中的自定义属性 */
113-
char *name; /**< 名称,对应 CSS 样式属性名称 */
114-
int(*parse)(LCUI_CSSParserStyleContext, const char*);
114+
int key; /**< 标识,在解析数据时可以使用它访问样式表中的自定义属性 */
115+
char *name; /**< 名称,对应 CSS 样式属性名称 */
116+
int (*parse)(LCUI_CSSParserStyleContext, const char *);
115117
};
116118

117119
struct LCUI_CSSParserStyleContextRec_ {
118-
char *dirname; /**< 当前所在的目录 */
119-
const char *space; /**< 样式记录所属的空间 */
120+
char *dirname; /**< 当前所在的目录 */
121+
const char *space; /**< 样式记录所属的空间 */
120122

121-
void (*style_handler)(int, LCUI_Style, void*);
123+
void (*style_handler)(int, LCUI_Style, void *);
122124
void *style_handler_arg;
123125

124-
LinkedList selectors; /**< 当前匹配到的选择器列表 */
125-
LCUI_StyleSheet sheet; /**< 当前缓存的样式表 */
126-
LCUI_CSSPropertyParser parser; /**< 当前找到的样式属性解析器 */
126+
LinkedList selectors; /**< 当前匹配到的选择器列表 */
127+
LCUI_StyleSheet sheet; /**< 当前缓存的样式表 */
128+
LCUI_CSSPropertyParser parser; /**< 当前找到的样式属性解析器 */
127129
};
128130

129131
struct LCUI_CSSParserCommentContextRec_ {
130-
LCUI_BOOL is_line_comment; /**< 是否为单行注释 */
131-
LCUI_CSSParserTarget prev_target; /**< 保存的上一个目标,解析完注释后将还原成该目标 */
132+
LCUI_BOOL is_line_comment; /**< 是否为单行注释 */
133+
LCUI_CSSParserTarget
134+
prev_target; /**< 保存的上一个目标,解析完注释后将还原成该目标 */
132135
};
133136

134137
struct LCUI_CSSParserRuleContextRec_ {
135-
int state; /**< 规则解析器的状态 */
136-
LCUI_CSSRule rule; /**< 当前规则 */
137-
LCUI_CSSRuleParsers parsers; /**< 规则解析器列表 */
138+
int state; /**< 规则解析器的状态 */
139+
LCUI_CSSRule rule; /**< 当前规则 */
140+
LCUI_CSSRuleParsers parsers; /**< 规则解析器列表 */
138141
};
139142

140143
/** CSS 代码解析器的环境参数(上下文数据) */
141144
struct LCUI_CSSParserContextRec_ {
142-
int pos; /**< 缓存中的字符串的下标位置 */
143-
const char *cur; /**< 用于遍历字符串的指针 */
144-
char *space; /**< 样式记录所属的空间 */
145-
char *buffer; /**< 缓存中的字符串 */
146-
size_t buffer_size; /**< 缓存区大小 */
145+
int pos; /**< 缓存中的字符串的下标位置 */
146+
const char *cur; /**< 用于遍历字符串的指针 */
147+
char *space; /**< 样式记录所属的空间 */
148+
char *buffer; /**< 缓存中的字符串 */
149+
size_t buffer_size; /**< 缓存区大小 */
147150

148-
LCUI_CSSParserTarget target; /**< 当前解析目标 */
149-
LCUI_CSSParsers parsers; /**< 可供使用的解析器列表 */
151+
LCUI_CSSParserTarget target; /**< 当前解析目标 */
152+
LCUI_CSSParsers parsers; /**< 可供使用的解析器列表 */
150153

151154
LCUI_CSSParserRuleContextRec rule;
152155
LCUI_CSSParserStyleContextRec style;
@@ -162,6 +165,9 @@ LCUI_API const char *LCUI_GetStyleName(int key);
162165
/** 初始化 LCUI 的 CSS 代码解析功能 */
163166
LCUI_API void LCUI_InitCSSParser(void);
164167

168+
LCUI_API void CSSStyleParser_SetCSSProperty(LCUI_CSSParserStyleContext ctx,
169+
int key, LCUI_Style s);
170+
165171
LCUI_API LCUI_CSSPropertyParser LCUI_GetCSSPropertyParser(const char *name);
166172

167173
/** 从文件中载入CSS样式数据,并导入至样式库中 */
@@ -170,7 +176,8 @@ LCUI_API int LCUI_LoadCSSFile(const char *filepath);
170176
/** 从字符串中载入CSS样式数据,并导入至样式库中 */
171177
LCUI_API size_t LCUI_LoadCSSString(const char *str, const char *space);
172178

173-
LCUI_API LCUI_CSSParserContext CSSParser_Begin(size_t buffer_size, const char *space);
179+
LCUI_API LCUI_CSSParserContext CSSParser_Begin(size_t buffer_size,
180+
const char *space);
174181

175182
LCUI_API void CSSParser_EndParseRuleData(LCUI_CSSParserContext ctx);
176183

‎src/gui/css_fontstyle.c

+55-38
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,9 @@
4949
/* clang-format on */
5050

5151
#define ComputeActual LCUIMetrics_ComputeActual
52-
#define GetFontStyle(CTX) &(CTX)->sheet->sheet[self.keys[(CTX)->parser->key]]
53-
#define SetFontStyle(CTX, V, T) \
54-
do { \
55-
SetStyle((CTX)->sheet, self.keys[(CTX)->parser->key], V, T); \
56-
} while (0)
52+
#define GetFontStyleKey(CTX) self.keys[(CTX)->parser->key]
53+
#define SetFontStyleProperty(CTX, S) \
54+
CSSStyleParser_SetCSSProperty(CTX, GetFontStyleKey(CTX), S);
5755

5856
enum FontStyleType { FS_NORMAL, FS_ITALIC, FS_OBLIQUE };
5957

@@ -100,100 +98,119 @@ static size_t unescape(const wchar_t *instr, wchar_t *outstr)
10098
static int OnParseContent(LCUI_CSSParserStyleContext ctx, const char *str)
10199
{
102100
size_t len;
103-
char *content;
101+
LCUI_StyleRec s;
104102

105103
len = strlen(str);
106104
if (len < 1 || (str[0] == '"' && str[len - 1] != '"')) {
107105
return -1;
108106
}
109-
content = malloc(sizeof(char) * (len + 1));
110-
if (!content) {
107+
s.is_valid = TRUE;
108+
s.type = LCUI_STYPE_STRING;
109+
s.val_string = malloc(sizeof(char) * (len + 1));
110+
if (!s.val_string) {
111111
return -1;
112112
}
113-
strcpy(content, str);
114-
SetFontStyle(ctx, content, string);
113+
strcpy(s.val_string, str);
114+
SetFontStyleProperty(ctx, &s);
115115
return 0;
116116
}
117117

118118
static int OnParseColor(LCUI_CSSParserStyleContext ctx, const char *str)
119119
{
120-
LCUI_Style s = GetFontStyle(ctx);
121-
if (ParseColor(s, str)) {
120+
LCUI_StyleRec s;
121+
122+
if (ParseColor(&s, str)) {
123+
SetFontStyleProperty(ctx, &s);
122124
return 0;
123125
}
124126
return -1;
125127
}
126128

127129
static int OnParseFontSize(LCUI_CSSParserStyleContext ctx, const char *str)
128130
{
129-
LCUI_Style s = GetFontStyle(ctx);
130-
if (ParseNumber(s, str)) {
131+
LCUI_StyleRec s;
132+
133+
if (ParseNumber(&s, str)) {
134+
SetFontStyleProperty(ctx, &s);
131135
return 0;
132136
}
133137
return -1;
134138
}
135139

136140
static int OnParseFontFamily(LCUI_CSSParserStyleContext ctx, const char *str)
137141
{
138-
char *name = strdup2(str);
139-
LCUI_Style s = GetFontStyle(ctx);
140-
if (s->is_valid && s->string) {
141-
free(s->string);
142-
}
143-
SetFontStyle(ctx, name, string);
142+
LCUI_StyleRec s;
143+
144+
s.is_valid = TRUE;
145+
s.type = LCUI_STYPE_STRING;
146+
s.val_string = strdup2(str);
147+
SetFontStyleProperty(ctx, &s);
144148
return 0;
145149
}
146150

147151
static int OnParseFontStyle(LCUI_CSSParserStyleContext ctx, const char *str)
148152
{
149-
int style;
150-
if (ParseFontStyle(str, &style)) {
151-
SetFontStyle(ctx, style, int);
153+
LCUI_StyleRec s;
154+
155+
if (ParseFontStyle(str, &s.val_int)) {
156+
s.is_valid = TRUE;
157+
s.type = LCUI_STYPE_INT;
158+
SetFontStyleProperty(ctx, &s);
152159
return 0;
153160
}
154161
return -1;
155162
}
156163

157164
static int OnParseFontWeight(LCUI_CSSParserStyleContext ctx, const char *str)
158165
{
159-
int weight;
160-
if (ParseFontWeight(str, &weight)) {
161-
SetFontStyle(ctx, weight, int);
166+
LCUI_StyleRec s;
167+
168+
if (ParseFontWeight(str, &s.val_int)) {
169+
s.is_valid = TRUE;
170+
s.type = LCUI_STYPE_INT;
171+
SetFontStyleProperty(ctx, &s);
162172
return 0;
163173
}
164174
return -1;
165175
}
166176

167177
static int OnParseTextAlign(LCUI_CSSParserStyleContext ctx, const char *str)
168178
{
169-
int val = LCUI_GetStyleValue(str);
170-
if (val < 0) {
179+
LCUI_StyleRec s;
180+
181+
s.is_valid = TRUE;
182+
s.type = LCUI_STYPE_STYLE;
183+
s.val_style = LCUI_GetStyleValue(str);
184+
185+
if (s.val_style < 0) {
171186
return -1;
172187
}
173-
SetFontStyle(ctx, val, style);
188+
SetFontStyleProperty(ctx, &s);
174189
return 0;
175190
}
176191

177192
static int OnParseLineHeight(LCUI_CSSParserStyleContext ctx, const char *str)
178193
{
179-
LCUI_StyleRec sv;
180-
if (!ParseNumber(&sv, str)) {
194+
LCUI_StyleRec s;
195+
196+
if (!ParseNumber(&s, str)) {
181197
return -1;
182198
}
183-
ctx->sheet->sheet[self.keys[ctx->parser->key]] = sv;
199+
SetFontStyleProperty(ctx, &s);
184200
return 0;
185201
}
186202

187203
static int OnParseStyleOption(LCUI_CSSParserStyleContext ctx, const char *str)
188204
{
189-
LCUI_Style s = GetFontStyle(ctx);
190-
int v = LCUI_GetStyleValue(str);
191-
if (v < 0) {
205+
LCUI_StyleRec s;
206+
207+
s.is_valid = TRUE;
208+
s.type = LCUI_STYPE_STYLE;
209+
s.val_style = LCUI_GetStyleValue(str);
210+
if (s.val_style < 0) {
192211
return -1;
193212
}
194-
s->style = v;
195-
s->type = LCUI_STYPE_STYLE;
196-
s->is_valid = TRUE;
213+
SetFontStyleProperty(ctx, &s);
197214
return 0;
198215
}
199216

0 commit comments

Comments
 (0)