TextMate is a graphical text editor for OS X 10.7+
Revision | 18eecca2ab8de6b12ae1c797527abbbb9163dcc1 (tree) |
---|---|
Zeit | 2012-08-18 23:28:47 |
Autor | Joachim Mårtensson <joachimmartensson@gmai...> |
Commiter | Allan Odgaard |
Represent font size using a CGFloat
This applies to scoped font size settings and should be faster than the previous std::string based approach.
@@ -2,6 +2,7 @@ | ||
2 | 2 | #include <cf/cf.h> |
3 | 3 | |
4 | 4 | static theme_t::color_info_t read_color (std::string const& str_color); |
5 | +static CGFloat read_font_size (std::string const& str_font_size); | |
5 | 6 | |
6 | 7 | static void get_key_path (plist::dictionary_t const& plist, std::string const& setting, theme_t::color_info_t& color) |
7 | 8 | { |
@@ -10,6 +11,13 @@ static void get_key_path (plist::dictionary_t const& plist, std::string const& s | ||
10 | 11 | color = read_color(temp_str); |
11 | 12 | } |
12 | 13 | |
14 | +static void get_key_path(plist::dictionary_t const& plist, std::string const& setting, CGFloat &font_size) | |
15 | +{ | |
16 | + std::string temp_str = NULL_STR; | |
17 | + plist::get_key_path(plist, setting, temp_str); | |
18 | + font_size = read_font_size(temp_str); | |
19 | +} | |
20 | + | |
13 | 21 | theme_t::decomposed_style_t theme_t::parse_styles (plist::dictionary_t const& plist) |
14 | 22 | { |
15 | 23 | decomposed_style_t res; |
@@ -19,7 +27,7 @@ theme_t::decomposed_style_t theme_t::parse_styles (plist::dictionary_t const& pl | ||
19 | 27 | res.scope_selector = scopeSelector; |
20 | 28 | |
21 | 29 | plist::get_key_path(plist, "settings.fontName", res.font_name); |
22 | - plist::get_key_path(plist, "settings.fontSize", res.font_size); | |
30 | + get_key_path(plist, "settings.fontSize", res.absolute_font_size); | |
23 | 31 | get_key_path(plist, "settings.foreground", res.foreground); |
24 | 32 | get_key_path(plist, "settings.background", res.background); |
25 | 33 | get_key_path(plist, "settings.caret", res.caret); |
@@ -61,8 +69,12 @@ std::vector<theme_t::decomposed_style_t> theme_t::global_styles (scope::context_ | ||
61 | 69 | |
62 | 70 | static struct { std::string name; std::string decomposed_style_t::*field; } const stringKeys[] = |
63 | 71 | { |
64 | - { "fontName", &decomposed_style_t::font_name }, | |
65 | - { "fontSize", &decomposed_style_t::font_size }, | |
72 | + { "fontName", &decomposed_style_t::font_name }, | |
73 | + }; | |
74 | + | |
75 | + static struct { std::string name; CGFloat decomposed_style_t::*field; } const doubleKeys[] = | |
76 | + { | |
77 | + { "fontSize", &decomposed_style_t::absolute_font_size }, | |
66 | 78 | }; |
67 | 79 | |
68 | 80 | static struct { std::string name; bool_t decomposed_style_t::*field; } const booleanKeys[] = |
@@ -97,6 +109,17 @@ std::vector<theme_t::decomposed_style_t> theme_t::global_styles (scope::context_ | ||
97 | 109 | } |
98 | 110 | } |
99 | 111 | |
112 | + for(size_t i = 0; i < sizeofA(doubleKeys); ++i) | |
113 | + { | |
114 | + bundles::item_ptr item; | |
115 | + plist::any_t const& value = bundles::value_for_setting(doubleKeys[i].name, scope, &item); | |
116 | + if(item) | |
117 | + { | |
118 | + res.push_back(decomposed_style_t(item->scope_selector())); | |
119 | + res.back().*(doubleKeys[i].field) = read_font_size(plist::get<std::string>(value)); | |
120 | + } | |
121 | + } | |
122 | + | |
100 | 123 | for(size_t i = 0; i < sizeofA(booleanKeys); ++i) |
101 | 124 | { |
102 | 125 | bundles::item_ptr item; |
@@ -237,6 +260,13 @@ static void alpha_blend (theme_t::color_info_t& lhs, theme_t::color_info_t const | ||
237 | 260 | } |
238 | 261 | } |
239 | 262 | |
263 | +static void calculate_font_size (CGFloat& absolute_font_size, CGFloat font_size) | |
264 | +{ | |
265 | + if(font_size > 0) | |
266 | + absolute_font_size = font_size; | |
267 | + else absolute_font_size = absolute_font_size * fabs(font_size); | |
268 | +} | |
269 | + | |
240 | 270 | static double my_strtod (char const* str, char const** last) // problem with strtod() is that it uses LC_NUMERIC for point separator. |
241 | 271 | { |
242 | 272 | double res = atof(str); |
@@ -249,13 +279,13 @@ static double my_strtod (char const* str, char const** last) // problem with str | ||
249 | 279 | return res; |
250 | 280 | } |
251 | 281 | |
252 | -theme_t::decomposed_style_t& theme_t::decomposed_style_t::operator+= (theme_t::decomposed_style_t const& rhs) | |
282 | +static CGFloat read_font_size (std::string const& str_font_size) | |
253 | 283 | { |
254 | - font_name = rhs.font_name == NULL_STR ? font_name : rhs.font_name; | |
255 | - | |
256 | - if(rhs.font_size != NULL_STR) | |
284 | + // Treat positive values as absolute font | |
285 | + // and negative as relative, that way we don't have to use a bool as a flag :) | |
286 | + if(str_font_size != NULL_STR) | |
257 | 287 | { |
258 | - char const* first = rhs.font_size.c_str(); | |
288 | + char const* first = str_font_size.c_str(); | |
259 | 289 | char const* last; |
260 | 290 | double size = my_strtod(first, &last); |
261 | 291 | if(first != last) |
@@ -264,19 +294,27 @@ theme_t::decomposed_style_t& theme_t::decomposed_style_t::operator+= (theme_t::d | ||
264 | 294 | ++last; |
265 | 295 | |
266 | 296 | if(strcmp(last, "pt") == 0 || *last == '\0') |
267 | - absolute_font_size = size; | |
297 | + return size; | |
268 | 298 | else if(strcmp(last, "em") == 0) |
269 | - absolute_font_size = absolute_font_size * size; | |
299 | + return -size; | |
270 | 300 | else if(strcmp(last, "%") == 0) |
271 | - absolute_font_size = absolute_font_size * size / 100; | |
301 | + return -size / 100; | |
272 | 302 | else |
273 | - fprintf(stderr, "*** unsupported font size unit: %s (%s)\n", last, rhs.font_size.c_str()); | |
303 | + fprintf(stderr, "*** unsupported font size unit: %s (%s)\n", last, str_font_size.c_str()); | |
274 | 304 | } |
275 | 305 | else |
276 | 306 | { |
277 | - fprintf(stderr, "*** unsupported font size format: %s\n", rhs.font_size.c_str()); | |
307 | + fprintf(stderr, "*** unsupported font size format: %s\n", str_font_size.c_str()); | |
278 | 308 | } |
279 | 309 | } |
310 | + return -1; | |
311 | +} | |
312 | + | |
313 | +theme_t::decomposed_style_t& theme_t::decomposed_style_t::operator+= (theme_t::decomposed_style_t const& rhs) | |
314 | +{ | |
315 | + font_name = rhs.font_name == NULL_STR ? font_name : rhs.font_name; | |
316 | + | |
317 | + calculate_font_size(absolute_font_size, rhs.absolute_font_size); | |
280 | 318 | |
281 | 319 | alpha_blend(foreground, rhs.foreground); |
282 | 320 | alpha_blend(background, rhs.background); |
@@ -55,13 +55,12 @@ private: | ||
55 | 55 | |
56 | 56 | struct decomposed_style_t |
57 | 57 | { |
58 | - decomposed_style_t (scope::selector_t const& scopeSelector = scope::selector_t(), std::string const& fontName = NULL_STR, CGFloat fontSize = 0) : scope_selector(scopeSelector), font_name(fontName), font_size(NULL_STR), bold(bool_unset), italic(bool_unset), underlined(bool_unset), misspelled(bool_unset), absolute_font_size(fontSize) { } | |
58 | + decomposed_style_t (scope::selector_t const& scopeSelector = scope::selector_t(), std::string const& fontName = NULL_STR, CGFloat fontSize = 0) : scope_selector(scopeSelector), font_name(fontName), bold(bool_unset), italic(bool_unset), underlined(bool_unset), misspelled(bool_unset), absolute_font_size(fontSize) { } | |
59 | 59 | decomposed_style_t& operator+= (decomposed_style_t const& rhs); |
60 | 60 | |
61 | 61 | scope::selector_t scope_selector; |
62 | 62 | |
63 | 63 | std::string font_name; |
64 | - std::string font_size; | |
65 | 64 | color_info_t foreground; |
66 | 65 | color_info_t background; |
67 | 66 | color_info_t caret; |