Gitweb links:
...log
http://git.netsurf-browser.org/libcss.git/shortlog/ae14e8b9dd656a59d1013b...
...commit
http://git.netsurf-browser.org/libcss.git/commit/ae14e8b9dd656a59d1013b97...
...tree
http://git.netsurf-browser.org/libcss.git/tree/ae14e8b9dd656a59d1013b977b...
The branch, lcneves/units has been updated
via ae14e8b9dd656a59d1013b977b8e2057825624b7 (commit)
from 723fba2c05a80274ab50e529d8fcba0310dad1ae (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
commitdiff
http://git.netsurf-browser.org/libcss.git/commit/?id=ae14e8b9dd656a59d101...
commit ae14e8b9dd656a59d1013b977b8e2057825624b7
Author: Lucas Neves <lcneves(a)gmail.com>
Commit: Lucas Neves <lcneves(a)gmail.com>
WIP: Select: Autogen for selection properties.
diff --git a/src/select/__pycache__/assets.cpython-36.pyc
b/src/select/__pycache__/assets.cpython-36.pyc
new file mode 100644
index 0000000..f7b7da8
Binary files /dev/null and b/src/select/__pycache__/assets.cpython-36.pyc differ
diff --git a/src/select/__pycache__/select_config.cpython-36.pyc
b/src/select/__pycache__/select_config.cpython-36.pyc
index 52b37eb..bcda842 100644
Binary files a/src/select/__pycache__/select_config.cpython-36.pyc and
b/src/select/__pycache__/select_config.cpython-36.pyc differ
diff --git a/src/select/assets.py b/src/select/assets.py
index 8151182..1d726a3 100644
--- a/src/select/assets.py
+++ b/src/select/assets.py
@@ -5,8 +5,8 @@
assets = {}
-assets['computed_h'] = {}
-assets['computed_h']['header'] = '''\
+assets['computed.h'] = {}
+assets['computed.h']['header'] = '''\
/*
* This file is part of LibCSS
* Licensed under the MIT License,
@@ -21,7 +21,7 @@ assets['computed_h']['header'] = '''\
#include <libcss/hint.h>
'''
-assets['computed_h']['footer'] = '''\
+assets['computed.h']['footer'] = '''\
/**
* Take a new reference to a computed style
*
diff --git a/src/select/autogenerated_computed.h b/src/select/autogenerated_computed.h
new file mode 100644
index 0000000..76a6b32
--- /dev/null
+++ b/src/select/autogenerated_computed.h
@@ -0,0 +1,162 @@
+/*
+ * This file is part of LibCSS
+ * Licensed under the MIT License,
+ *
http://www.opensource.org/licenses/mit-license.php
+ * Copyright 2017 The NetSurf Project
+ */
+
+#ifndef css_select_computed_h_
+#define css_select_computed_h_
+
+#include <libcss/computed.h>
+#include <libcss/hint.h>
+
+
+
+/*
+ * border_spacing 1 + 10 8
+ * break_after 4
+ * break_before 4
+ * break_inside 4
+ * clip 6
+ * column_count 2 4
+ * column_fill 2
+ * column_gap 2 + 5 4
+ * column_rule_color 2 4
+ * column_rule_style 4
+ * column_rule_width 3 + 5 4
+ * column_span 2
+ * column_width 2 + 5 4
+ * content 2 sizeof(ptr)
+ * counter_increment 1 sizeof(ptr)
+ * counter_reset 1 sizeof(ptr)
+ * cursor 5 sizeof(ptr)
+ * letter_spacing 2 + 5 4
+ * outline_color 2 4
+ * outline_width 3 + 5 4
+ * word_spacing 2 + 5 4
+ */
+
+/*
+ * orphans 1 4
+ * page_break_after 3
+ * page_break_before 3
+ * page_break_inside 2
+ * widows 1 4
+ */
+
+/*
+ * align_content 3
+ * align_items 3
+ * align_self 3
+ * background_attachment 2
+ * background_color 2 4
+ * background_image 1 sizeof(ptr)
+ * background_position 1 + 10 8
+ * background_repeat 3
+ * border_bottom_color 2 4
+ * border_bottom_style 4
+ * border_bottom_width 3 + 5 4
+ * border_collapse 2
+ * border_left_color 2 4
+ * border_left_style 4
+ * border_left_width 3 + 5 4
+ * border_right_color 2 4
+ * border_right_style 4
+ * border_right_width 3 + 5 4
+ * border_top_color 2 4
+ * border_top_style 4
+ * border_top_width 3 + 5 4
+ * bottom 2 + 5 4
+ * box_sizing 2
+ * caption_side 2
+ * clear 3
+ * color 1 4
+ * direction 2
+ * display 5
+ * empty_cells 2
+ * flex_basis 2 + 5 4
+ * flex_direction 3
+ * flex_grow 1 4
+ * flex_shrink 1 4
+ * flex_wrap 2
+ * float 2
+ * font_family 3 sizeof(ptr)
+ * font_size 4 + 5 4
+ * font_style 2
+ * font_variant 2
+ * font_weight 4
+ * height 2 + 5 4
+ * justify_content 3
+ * left 2 + 5 4
+ * line_height 2 + 5 4
+ * list_style_image 1 sizeof(ptr)
+ * list_style_position 2
+ * list_style_type 4
+ * margin_bottom 2 + 5 4
+ * margin_left 2 + 5 4
+ * margin_right 2 + 5 4
+ * margin_top 2 + 5 4
+ * max_height 2 + 5 4
+ * max_width 2 + 5 4
+ * min_height 2 + 5 4
+ * min_width 2 + 5 4
+ * opacity 1 4
+ * order 1 4
+ * outline_style 4
+ * overflow 3
+ * padding_bottom 1 + 5 4
+ * padding_left 1 + 5 4
+ * padding_right 1 + 5 4
+ * padding_top 1 + 5 4
+ * position 3
+ * quotes 1 sizeof(ptr)
+ * right 2 + 5 4
+ * table_layout 2
+ * text_align 4
+ * text_decoration 5
+ * text_indent 1 + 5 4
+ * text_transform 3
+ * top 2 + 5 4
+ * unicode_bidi 2
+ * vertical_align 4 + 5 4
+ * visibility 2
+ * white_space 3
+ * width 2 + 5 4
+ * z_index 2 4
+ */
+/**
+ * Take a new reference to a computed style
+ *
+ * \param style The style to take a new reference to.
+ *
eturn The new computed style reference
+ */
+static inline css_computed_style * css__computed_style_ref(
+ css_computed_style *style)
+{
+ if (style == NULL)
+ return NULL;
+
+ if (style->i.uncommon != NULL) {
+ style->i.uncommon->count++;
+ }
+
+ style->count++;
+ return style;
+}
+
+css_error css__computed_style_create(css_computed_style **result);
+
+css_error css__computed_style_initialise(css_computed_style *style,
+ struct css_select_handler *handler, void *pw);
+
+
+css_error css__computed_uncommon_destroy(css_computed_uncommon *uncommon);
+
+css_error css__compute_absolute_values(const css_computed_style *parent,
+ css_computed_style *style,
+ css_error (*compute_font_size)(void *pw,
+ const css_hint *parent, css_hint *size),
+ void *pw);
+
+#endif
diff --git a/src/select/select_config.py b/src/select/select_config.py
index d5cd525..27b2dc9 100644
--- a/src/select/select_config.py
+++ b/src/select/select_config.py
@@ -98,8 +98,8 @@ style = {
('z_index', 2, 'integer'),
# Style group, arrays
('font_family', 3, 'string_arr', None, None,
- 'Encode font family as an array of string objects, terminated with a'
- '\n' 'blank entry.'),
+ 'Encode font family as an array of string objects, terminated with a '
+ 'blank entry.'),
('quotes', 1, 'string_arr', None, None,
'Encode quotes as an array of string objects, terminated with a '
'blank entry.')
@@ -145,13 +145,13 @@ uncommon = {
'CSS_WORD_SPACING_SET', 'CSS_WORD_SPACING_NORMAL'),
# Uncommon group, arrays
('counter_increment', 1, 'counter', None,
'CSS_COUNTER_INCREMENT_NONE',
- 'Encode counter_increment as an array of name, value pairs,\n'
+ 'Encode counter_increment as an array of name, value pairs, '
'terminated with a blank entry.'),
('counter_reset', 1, 'counter', None,
'CSS_COUNTER_RESET_NONE',
- 'Encode counter_reset as an array of name, value pairs,\n'
+ 'Encode counter_reset as an array of name, value pairs, '
'terminated with a blank entry.'),
('cursor', 5, 'string_arr', 'CSS_CURSOR_AUTO', None,
- 'Encode cursor uri(s) as an array of string objects, terminated\n'
+ 'Encode cursor uri(s) as an array of string objects, terminated '
'with a blank entry'),
('content', 2, 'content_item', 'CSS_CONTENT_NORMAL', None,
'Encode content as an array of content items, terminated with '
diff --git a/src/select/select_generator.py b/src/select/select_generator.py
index c37aa74..3e6744c 100644
--- a/src/select/select_generator.py
+++ b/src/select/select_generator.py
@@ -6,15 +6,6 @@
from select_config import values, groups
from assets import assets
-def get_tuple(from_var):
- 'Accepts tuples and strings, returns a tuple.'
- if type(from_var) is tuple:
- return from_var
- elif type(from_var) is str:
- return (from_var,)
- else:
- return ()
-
class Bin:
def __init__(self, first_object):
self.contents = [ first_object ]
@@ -26,24 +17,7 @@ class Bin:
def push(self, obj):
self.contents.append(obj)
-def make_bit_map(group):
- '''Implements a `best fit first` heuristics for the bin packing of
- property bits in the bits array'''
-
- bin_size = 32
- bit_map = []
- group_props = sorted([ { 'name': p.name, 'size': p.bits_size }
- for p in group['props'] ], key=(lambda x: x['size']),
reverse=True)
- for p in group_props:
- for b in bit_map:
- if b.size + p['size'] <= bin_size:
- b.push(p)
- break
- else:
- bit_map.append(Bin(p))
- bit_map.sort(key=(lambda x: x.size), reverse=True)
-
-class FileText:
+class Text:
def __init__(self):
self._lines = []
self._comment = False
@@ -56,10 +30,21 @@ class FileText:
self._indent += value
def comment(self):
- self.append(' */' if self._comment else '/*')
- self._comment = !self._comment
+ comm = self._comment
+ self._comment = False
+ self.append(' */' if comm else '/*')
+ self._comment = not comm
+
+ def append(self, text=None, pre_formatted=False):
+ if text is None:
+ self._lines.append('\t' * self._indent + ' * '
+ if self._comment else '')
+ return
+
+ if pre_formatted:
+ self._lines.append(text)
+ return
- def append(self, text):
column_max = 80
multiline = False
@@ -75,7 +60,7 @@ class FileText:
break_index = text[:text[:column_max - prefix_size].rfind(' ')]
line += text[:break_index].rstrip()
text = text[break_index:].lstrip()
- self.lines.append(line)
+ self._lines.append(line)
if text and not self._comment and not multiline:
self.indent(2)
multiline = True
@@ -83,12 +68,8 @@ class FileText:
if multiline:
self._indent(-2)
- def write_file(self, filename):
- with open(filename, 'w') as f:
- f.write('\n'.join(self._lines))
-
-def make_computed_h(group, is_main=false):
-
+ def to_string(self):
+ return '\n'.join(self._lines)
class CSSValue:
'Values to be associated with properties.'
@@ -111,22 +92,102 @@ class CSSProperty:
defaults=None, comments=None, override=None):
self.name = name
self.type_size = type_size
- self.values = [ v for v in self.__vals if v.name in get_tuple(values) ]
- self.defaults = get_tuple(defaults)
+ self.values = [ x for v in self.get_tuple(values) for x in self.__vals
+ if x.name is v ]
+ self.defaults = self.get_tuple(defaults)
self.condition = condition
- self.override = get_tuple(override)
+ self.override = self.get_tuple(override)
self.comments = comments
__vals = [ CSSValue(*x) for x in values ]
+ def get_tuple(self, from_var):
+ 'Accepts tuples, strings and None; returns a tuple.'
+ if type(from_var) is tuple:
+ return from_var
+ elif type(from_var) is str:
+ return (from_var,)
+ elif from_var is None:
+ return ()
+ else:
+ raise TypeError('Value should be either tuple, string or None, ' +
+ 'received: ' + type(from_var).__name__)
+
@property
def bits_size(self):
return self.type_size + sum([ v.bits['size'] for v in self.values
if v.bits is not None ])
-groups['main']['props'] = [ CSSProperty(*x) for x in
groups['main']['props'] ]
-groups['main']['map'] = make_bit_map(groups['main'])
-for g in groups['others']:
- g['props'] = [ CSSProperty(*x) for x in g['props'] ]
- g['map'] = make_bit_map(g)
+ @property
+ def size_line(self):
+ name = '{:31}'.format(self.name)
+ type_size = str(self.type_size)
+ extra_size = sum([ v.bits['size'] for v in self.values
+ if v.bits is not None ])
+ bits_size = '{:16}'.format(type_size +
+ (' + ' + str(extra_size) if extra_size else ''))
+ vars_size = sum([ v.size for v in self.values if v.size is not None ])
+ vars_size = str(vars_size) if vars_size else ''
+ ptr = ''
+ for v in self.values:
+ if v.size is None:
+ ptr = 'sizeof(ptr)'
+ break
+
+ return (name + bits_size + vars_size +
+ (' + ' if vars_size and ptr else '') + ptr)
+
+class CSSGroup:
+ def __init__(self, config, is_main=False):
+ self.name = config['name']
+ self.props = [ CSSProperty(*x) for x in config['props'] ]
+ self.is_main = is_main
+
+ @property
+ def bit_map(self):
+ '''Implements a `best fit first` heuristics for the bin packing of
+ property bits in the bits array'''
+
+ bin_size = 32
+ bit_map = []
+ props = sorted([ { 'name': p.name, 'size': p.bits_size }
+ for p in self.props ], key=(lambda x: x['size']), reverse=True)
+
+ for p in props:
+ for b in bit_map:
+ if b.size + p['size'] <= bin_size:
+ b.push(p)
+ break
+ else:
+ bit_map.append(Bin(p))
+ bit_map.sort(key=(lambda x: x.size), reverse=True)
+
+ return bit_map
+
+ def make_computed_h(self):
+ t = Text()
+ t.append()
+ t.comment()
+ for prop in sorted(self.props, key=(lambda x: x.name)):
+ t.append(prop.size_line)
+ t.comment()
+ return t.to_string()
+
+ def make_text(self, filename):
+ if filename == 'computed.h':
+ return self.make_computed_h()
+ else:
+ raise ValueError()
+
+def write_file(self, filename, data):
+ self._lines.append('')
+
+css_groups = [ CSSGroup(g) for g in groups['others'] ]
+css_groups.append(CSSGroup(groups['main'], is_main=True))
+files = ['computed.h']
+for f in files:
+ body = '\n'.join([ x.make_text(f) for x in css_groups ])
+ text = '\n'.join([ assets[f]['header'], body,
assets[f]['footer'] ])
+ with open('autogenerated_' + f, 'w') as file_f:
+ file_f.write(text)
-----------------------------------------------------------------------
Summary of changes:
.../assets.cpython-36.pyc} | Bin 1448 -> 1375 bytes
.../__pycache__/select_config.cpython-36.pyc | Bin 6502 -> 6502 bytes
src/select/assets.py | 6 +-
src/select/autogenerated_computed.h | 162 ++++++++++++++++++++
src/select/select_config.py | 10 +-
src/select/select_generator.py | 151 ++++++++++++------
6 files changed, 276 insertions(+), 53 deletions(-)
copy src/select/{assets.py => __pycache__/assets.cpython-36.pyc} (76%)
create mode 100644 src/select/autogenerated_computed.h
diff --git a/src/select/assets.py b/src/select/__pycache__/assets.cpython-36.pyc
similarity index 76%
copy from src/select/assets.py
copy to src/select/__pycache__/assets.cpython-36.pyc
index 8151182..f7b7da8 100644
Binary files a/src/select/assets.py and b/src/select/__pycache__/assets.cpython-36.pyc
differ
diff --git a/src/select/__pycache__/select_config.cpython-36.pyc
b/src/select/__pycache__/select_config.cpython-36.pyc
index 52b37eb..bcda842 100644
Binary files a/src/select/__pycache__/select_config.cpython-36.pyc and
b/src/select/__pycache__/select_config.cpython-36.pyc differ
diff --git a/src/select/assets.py b/src/select/assets.py
index 8151182..1d726a3 100644
--- a/src/select/assets.py
+++ b/src/select/assets.py
@@ -5,8 +5,8 @@
assets = {}
-assets['computed_h'] = {}
-assets['computed_h']['header'] = '''\
+assets['computed.h'] = {}
+assets['computed.h']['header'] = '''\
/*
* This file is part of LibCSS
* Licensed under the MIT License,
@@ -21,7 +21,7 @@ assets['computed_h']['header'] = '''\
#include <libcss/hint.h>
'''
-assets['computed_h']['footer'] = '''\
+assets['computed.h']['footer'] = '''\
/**
* Take a new reference to a computed style
*
diff --git a/src/select/autogenerated_computed.h b/src/select/autogenerated_computed.h
new file mode 100644
index 0000000..76a6b32
--- /dev/null
+++ b/src/select/autogenerated_computed.h
@@ -0,0 +1,162 @@
+/*
+ * This file is part of LibCSS
+ * Licensed under the MIT License,
+ *
http://www.opensource.org/licenses/mit-license.php
+ * Copyright 2017 The NetSurf Project
+ */
+
+#ifndef css_select_computed_h_
+#define css_select_computed_h_
+
+#include <libcss/computed.h>
+#include <libcss/hint.h>
+
+
+
+/*
+ * border_spacing 1 + 10 8
+ * break_after 4
+ * break_before 4
+ * break_inside 4
+ * clip 6
+ * column_count 2 4
+ * column_fill 2
+ * column_gap 2 + 5 4
+ * column_rule_color 2 4
+ * column_rule_style 4
+ * column_rule_width 3 + 5 4
+ * column_span 2
+ * column_width 2 + 5 4
+ * content 2 sizeof(ptr)
+ * counter_increment 1 sizeof(ptr)
+ * counter_reset 1 sizeof(ptr)
+ * cursor 5 sizeof(ptr)
+ * letter_spacing 2 + 5 4
+ * outline_color 2 4
+ * outline_width 3 + 5 4
+ * word_spacing 2 + 5 4
+ */
+
+/*
+ * orphans 1 4
+ * page_break_after 3
+ * page_break_before 3
+ * page_break_inside 2
+ * widows 1 4
+ */
+
+/*
+ * align_content 3
+ * align_items 3
+ * align_self 3
+ * background_attachment 2
+ * background_color 2 4
+ * background_image 1 sizeof(ptr)
+ * background_position 1 + 10 8
+ * background_repeat 3
+ * border_bottom_color 2 4
+ * border_bottom_style 4
+ * border_bottom_width 3 + 5 4
+ * border_collapse 2
+ * border_left_color 2 4
+ * border_left_style 4
+ * border_left_width 3 + 5 4
+ * border_right_color 2 4
+ * border_right_style 4
+ * border_right_width 3 + 5 4
+ * border_top_color 2 4
+ * border_top_style 4
+ * border_top_width 3 + 5 4
+ * bottom 2 + 5 4
+ * box_sizing 2
+ * caption_side 2
+ * clear 3
+ * color 1 4
+ * direction 2
+ * display 5
+ * empty_cells 2
+ * flex_basis 2 + 5 4
+ * flex_direction 3
+ * flex_grow 1 4
+ * flex_shrink 1 4
+ * flex_wrap 2
+ * float 2
+ * font_family 3 sizeof(ptr)
+ * font_size 4 + 5 4
+ * font_style 2
+ * font_variant 2
+ * font_weight 4
+ * height 2 + 5 4
+ * justify_content 3
+ * left 2 + 5 4
+ * line_height 2 + 5 4
+ * list_style_image 1 sizeof(ptr)
+ * list_style_position 2
+ * list_style_type 4
+ * margin_bottom 2 + 5 4
+ * margin_left 2 + 5 4
+ * margin_right 2 + 5 4
+ * margin_top 2 + 5 4
+ * max_height 2 + 5 4
+ * max_width 2 + 5 4
+ * min_height 2 + 5 4
+ * min_width 2 + 5 4
+ * opacity 1 4
+ * order 1 4
+ * outline_style 4
+ * overflow 3
+ * padding_bottom 1 + 5 4
+ * padding_left 1 + 5 4
+ * padding_right 1 + 5 4
+ * padding_top 1 + 5 4
+ * position 3
+ * quotes 1 sizeof(ptr)
+ * right 2 + 5 4
+ * table_layout 2
+ * text_align 4
+ * text_decoration 5
+ * text_indent 1 + 5 4
+ * text_transform 3
+ * top 2 + 5 4
+ * unicode_bidi 2
+ * vertical_align 4 + 5 4
+ * visibility 2
+ * white_space 3
+ * width 2 + 5 4
+ * z_index 2 4
+ */
+/**
+ * Take a new reference to a computed style
+ *
+ * \param style The style to take a new reference to.
+ *
eturn The new computed style reference
+ */
+static inline css_computed_style * css__computed_style_ref(
+ css_computed_style *style)
+{
+ if (style == NULL)
+ return NULL;
+
+ if (style->i.uncommon != NULL) {
+ style->i.uncommon->count++;
+ }
+
+ style->count++;
+ return style;
+}
+
+css_error css__computed_style_create(css_computed_style **result);
+
+css_error css__computed_style_initialise(css_computed_style *style,
+ struct css_select_handler *handler, void *pw);
+
+
+css_error css__computed_uncommon_destroy(css_computed_uncommon *uncommon);
+
+css_error css__compute_absolute_values(const css_computed_style *parent,
+ css_computed_style *style,
+ css_error (*compute_font_size)(void *pw,
+ const css_hint *parent, css_hint *size),
+ void *pw);
+
+#endif
diff --git a/src/select/select_config.py b/src/select/select_config.py
index d5cd525..27b2dc9 100644
--- a/src/select/select_config.py
+++ b/src/select/select_config.py
@@ -98,8 +98,8 @@ style = {
('z_index', 2, 'integer'),
# Style group, arrays
('font_family', 3, 'string_arr', None, None,
- 'Encode font family as an array of string objects, terminated with a'
- '\n' 'blank entry.'),
+ 'Encode font family as an array of string objects, terminated with a '
+ 'blank entry.'),
('quotes', 1, 'string_arr', None, None,
'Encode quotes as an array of string objects, terminated with a '
'blank entry.')
@@ -145,13 +145,13 @@ uncommon = {
'CSS_WORD_SPACING_SET', 'CSS_WORD_SPACING_NORMAL'),
# Uncommon group, arrays
('counter_increment', 1, 'counter', None,
'CSS_COUNTER_INCREMENT_NONE',
- 'Encode counter_increment as an array of name, value pairs,\n'
+ 'Encode counter_increment as an array of name, value pairs, '
'terminated with a blank entry.'),
('counter_reset', 1, 'counter', None,
'CSS_COUNTER_RESET_NONE',
- 'Encode counter_reset as an array of name, value pairs,\n'
+ 'Encode counter_reset as an array of name, value pairs, '
'terminated with a blank entry.'),
('cursor', 5, 'string_arr', 'CSS_CURSOR_AUTO', None,
- 'Encode cursor uri(s) as an array of string objects, terminated\n'
+ 'Encode cursor uri(s) as an array of string objects, terminated '
'with a blank entry'),
('content', 2, 'content_item', 'CSS_CONTENT_NORMAL', None,
'Encode content as an array of content items, terminated with '
diff --git a/src/select/select_generator.py b/src/select/select_generator.py
index c37aa74..3e6744c 100644
--- a/src/select/select_generator.py
+++ b/src/select/select_generator.py
@@ -6,15 +6,6 @@
from select_config import values, groups
from assets import assets
-def get_tuple(from_var):
- 'Accepts tuples and strings, returns a tuple.'
- if type(from_var) is tuple:
- return from_var
- elif type(from_var) is str:
- return (from_var,)
- else:
- return ()
-
class Bin:
def __init__(self, first_object):
self.contents = [ first_object ]
@@ -26,24 +17,7 @@ class Bin:
def push(self, obj):
self.contents.append(obj)
-def make_bit_map(group):
- '''Implements a `best fit first` heuristics for the bin packing of
- property bits in the bits array'''
-
- bin_size = 32
- bit_map = []
- group_props = sorted([ { 'name': p.name, 'size': p.bits_size }
- for p in group['props'] ], key=(lambda x: x['size']),
reverse=True)
- for p in group_props:
- for b in bit_map:
- if b.size + p['size'] <= bin_size:
- b.push(p)
- break
- else:
- bit_map.append(Bin(p))
- bit_map.sort(key=(lambda x: x.size), reverse=True)
-
-class FileText:
+class Text:
def __init__(self):
self._lines = []
self._comment = False
@@ -56,10 +30,21 @@ class FileText:
self._indent += value
def comment(self):
- self.append(' */' if self._comment else '/*')
- self._comment = !self._comment
+ comm = self._comment
+ self._comment = False
+ self.append(' */' if comm else '/*')
+ self._comment = not comm
+
+ def append(self, text=None, pre_formatted=False):
+ if text is None:
+ self._lines.append('\t' * self._indent + ' * '
+ if self._comment else '')
+ return
+
+ if pre_formatted:
+ self._lines.append(text)
+ return
- def append(self, text):
column_max = 80
multiline = False
@@ -75,7 +60,7 @@ class FileText:
break_index = text[:text[:column_max - prefix_size].rfind(' ')]
line += text[:break_index].rstrip()
text = text[break_index:].lstrip()
- self.lines.append(line)
+ self._lines.append(line)
if text and not self._comment and not multiline:
self.indent(2)
multiline = True
@@ -83,12 +68,8 @@ class FileText:
if multiline:
self._indent(-2)
- def write_file(self, filename):
- with open(filename, 'w') as f:
- f.write('\n'.join(self._lines))
-
-def make_computed_h(group, is_main=false):
-
+ def to_string(self):
+ return '\n'.join(self._lines)
class CSSValue:
'Values to be associated with properties.'
@@ -111,22 +92,102 @@ class CSSProperty:
defaults=None, comments=None, override=None):
self.name = name
self.type_size = type_size
- self.values = [ v for v in self.__vals if v.name in get_tuple(values) ]
- self.defaults = get_tuple(defaults)
+ self.values = [ x for v in self.get_tuple(values) for x in self.__vals
+ if x.name is v ]
+ self.defaults = self.get_tuple(defaults)
self.condition = condition
- self.override = get_tuple(override)
+ self.override = self.get_tuple(override)
self.comments = comments
__vals = [ CSSValue(*x) for x in values ]
+ def get_tuple(self, from_var):
+ 'Accepts tuples, strings and None; returns a tuple.'
+ if type(from_var) is tuple:
+ return from_var
+ elif type(from_var) is str:
+ return (from_var,)
+ elif from_var is None:
+ return ()
+ else:
+ raise TypeError('Value should be either tuple, string or None, ' +
+ 'received: ' + type(from_var).__name__)
+
@property
def bits_size(self):
return self.type_size + sum([ v.bits['size'] for v in self.values
if v.bits is not None ])
-groups['main']['props'] = [ CSSProperty(*x) for x in
groups['main']['props'] ]
-groups['main']['map'] = make_bit_map(groups['main'])
-for g in groups['others']:
- g['props'] = [ CSSProperty(*x) for x in g['props'] ]
- g['map'] = make_bit_map(g)
+ @property
+ def size_line(self):
+ name = '{:31}'.format(self.name)
+ type_size = str(self.type_size)
+ extra_size = sum([ v.bits['size'] for v in self.values
+ if v.bits is not None ])
+ bits_size = '{:16}'.format(type_size +
+ (' + ' + str(extra_size) if extra_size else ''))
+ vars_size = sum([ v.size for v in self.values if v.size is not None ])
+ vars_size = str(vars_size) if vars_size else ''
+ ptr = ''
+ for v in self.values:
+ if v.size is None:
+ ptr = 'sizeof(ptr)'
+ break
+
+ return (name + bits_size + vars_size +
+ (' + ' if vars_size and ptr else '') + ptr)
+
+class CSSGroup:
+ def __init__(self, config, is_main=False):
+ self.name = config['name']
+ self.props = [ CSSProperty(*x) for x in config['props'] ]
+ self.is_main = is_main
+
+ @property
+ def bit_map(self):
+ '''Implements a `best fit first` heuristics for the bin packing of
+ property bits in the bits array'''
+
+ bin_size = 32
+ bit_map = []
+ props = sorted([ { 'name': p.name, 'size': p.bits_size }
+ for p in self.props ], key=(lambda x: x['size']), reverse=True)
+
+ for p in props:
+ for b in bit_map:
+ if b.size + p['size'] <= bin_size:
+ b.push(p)
+ break
+ else:
+ bit_map.append(Bin(p))
+ bit_map.sort(key=(lambda x: x.size), reverse=True)
+
+ return bit_map
+
+ def make_computed_h(self):
+ t = Text()
+ t.append()
+ t.comment()
+ for prop in sorted(self.props, key=(lambda x: x.name)):
+ t.append(prop.size_line)
+ t.comment()
+ return t.to_string()
+
+ def make_text(self, filename):
+ if filename == 'computed.h':
+ return self.make_computed_h()
+ else:
+ raise ValueError()
+
+def write_file(self, filename, data):
+ self._lines.append('')
+
+css_groups = [ CSSGroup(g) for g in groups['others'] ]
+css_groups.append(CSSGroup(groups['main'], is_main=True))
+files = ['computed.h']
+for f in files:
+ body = '\n'.join([ x.make_text(f) for x in css_groups ])
+ text = '\n'.join([ assets[f]['header'], body,
assets[f]['footer'] ])
+ with open('autogenerated_' + f, 'w') as file_f:
+ file_f.write(text)
--
Cascading Style Sheets library