Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
Added style folding optimization
Browse files Browse the repository at this point in the history
This adds an optimization that removes Mapnik style rules that can never
be applied.
  • Loading branch information
c0nk committed May 17, 2015
1 parent a1744d8 commit 09cb77c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/carto/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ carto.Renderer.prototype.render = function render(m) {
sorted = sortStyles(rules, env);

for (var k = 0, rule, style_name; k < sorted.length; k++) {
rule = sorted[k];
rule = foldStyle(sorted[k]);
style_name = l.name + (rule.attachment !== '__default__' ? '-' + rule.attachment : '');

// env.effects can be modified by this call
Expand Down Expand Up @@ -355,6 +355,19 @@ function sortStyles(styles, env) {
return result;
}

// Removes dead style definitions that can never be reached
// when filter-mode="first". The style is modified in-place
// and returned. The style must be sorted.
function foldStyle(style) {
for (var i = 0; i < style.length; i++) {
for (var j = style.length - 1; j > i; j--) {
if (style[j].filters.cloneWith(style[i].filters) === null)
style.splice(j, 1);
}
}
return style;
}

/**
* Find a rule like Map { background-color: #fff; },
* if any, and return a list of properties to be inserted
Expand Down
11 changes: 11 additions & 0 deletions test/rendering/style_fold.mml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"Layer": [
{
"name": "a",
"id": "a"
}
],
"Stylesheet": [
"style_fold.mss"
]
}
14 changes: 14 additions & 0 deletions test/rendering/style_fold.mss
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Map { }
#a {
[x = 'X'] {
#a {
line-cap: round;
}
}
[y = 'Y'] {
line-width: 2;
#a {
line-cap: round;
}
}
}
20 changes: 20 additions & 0 deletions test/rendering/style_fold.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE Map[]>
<Map>


<Style name="a" filter-mode="first">
<Rule>
<Filter>([y] = 'Y')</Filter>
<LineSymbolizer stroke-linecap="round" stroke-width="2" />
</Rule>
<Rule>
<Filter>([x] = 'X')</Filter>
<LineSymbolizer stroke-linecap="round" />
</Rule>
</Style>
<Layer name="a"
>
<StyleName>a</StyleName> </Layer>

</Map>

0 comments on commit 09cb77c

Please sign in to comment.