Skip to content

Commit

Permalink
Merge pull request #5002 from camptocamp/svg-more-flexible
Browse files Browse the repository at this point in the history
Try to be more flexible and less magic by default
  • Loading branch information
sbrunner authored Jul 11, 2019
2 parents 8dc116a + f22239e commit 54c1112
Show file tree
Hide file tree
Showing 18 changed files with 150 additions and 114 deletions.
81 changes: 43 additions & 38 deletions buildtools/svg-viewbox-loader.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
/**
* Webpack loader to be used after `svg-inline-loader`.
*
* It will:
* * Fill the `viewBox` if it's missing (to be able to change the `width` and the `height`).
* * Fill the `width` and `height` (required by OpenLayers).
* * Allows to change the `width` and `height` (with `width` or `height` arguments).
* * Remove the unneeded `x` and `y` property.
*
* See also the `svg` example.
*/

const simpleHTMLTokenizer = require('simple-html-tokenizer');
const querystring = require('querystring');
const generate = require('./generate-xml-from-tokens.js');
const parseUnit = require('parse-absolute-css-unit')

module.exports = function(source) {
this.cacheable(true);
Expand All @@ -9,66 +23,57 @@ module.exports = function(source) {
if (tag.type === 'StartTag' && tag.tagName === 'svg') {
let width = undefined;
let height = undefined;
let x = 0;
let y = 0;
let viewBox = undefined;
for (const attribute of tag.attributes) {
if (attribute[0] === 'width') {
try {
width = parseFloat(attribute[1]);
width = parseUnit(attribute[1]);
} catch (e) {
console.warn('Unable to read width: ' + attribute[1]);
}
}
if (attribute[0] === 'height') {
try {
height = parseFloat(attribute[1]);
height = parseUnit(attribute[1]);
} catch (e) {
console.warn('Unable to read height: ' + attribute[1]);
}
}
if (attribute[0] === 'x') {
try {
x = parseFloat(attribute[1]);
} catch (e) {
console.warn('Unable to read x: ' + attribute[1]);
}
}
if (attribute[0] === 'y') {
try {
y = parseFloat(attribute[1]);
} catch (e) {
console.warn('Unable to read y: ' + attribute[1]);
}
}
if (attribute[0] === 'viewBox') {
try {
const attrs = attribute[1].split(' ');
x = parseFloat(attrs[0]);
y = parseFloat(attrs[1]);
width = parseFloat(attrs[2]);
height = parseFloat(attrs[3]);
} catch (e) {
console.warn('Unable to read viewbox: ' + attribute[1]);
}
viewBox = attribute[1];
}
}
if (viewBox !== undefined && width === undefined && height === undefined) {
try {
const attrs = viewBox.split(' ');
width = parseFloat(attrs[2]);
height = parseFloat(attrs[3]);
} catch (e) {
console.warn('Unable to read viewBox: ' + viewBox);
}
}
if (width !== undefined && height != undefined) {
tag.attributes = tag.attributes.filter((attribute) => {
return attribute[0] !== 'width' && attribute[0] != 'height' && attribute[0] != 'viewBox';
return attribute[0] !== 'width' && attribute[0] !== 'height' &&
attribute[0] !== 'x' && attribute[0] !== 'y';
});
if (x) {
tag.attributes.push(['x', x, true]);
}
if (y) {
tag.attributes.push(['y', y, true]);
if (viewBox === undefined) {
tag.attributes.push(['viewBox', `0 0 ${width} ${height}`, true]);
}
if (this.resourceQuery.search(/inline/) >= 0) {
const queryString = this.resourceQuery.startsWith('?') ? this.resourceQuery.substring(1) :
this.resourceQuery;
const query = querystring.decode(queryString);
if (query.width !== undefined) {
const parsed = query.width.match(/^([0-9]+)([a-z]*)$/);
tag.attributes.push(['width', query.width, true]);
tag.attributes.push(['height', `${height / width * parseFloat(parsed[1])}${parsed[2]}`, true]);
} else if (query.height !== undefined) {
const parsed = query.height.match(/^([0-9]+)([a-z]*)$/);
tag.attributes.push(['width', `${width / height * parseFloat(parsed[1])}${parsed[2]}`, true]);
tag.attributes.push(['height', query.height, true]);
} else {
tag.attributes.push(['width', width, true]);
tag.attributes.push(['height', height, true]);
} else {
tag.attributes.push(['viewBox', `0 0 ${width} ${height}`, true]);
tag.attributes.push(['height', '1em', true]);
tag.attributes.push(['width', `${width / height}em`, true]);
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions buildtools/webpack.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const svgRule = {
'svgo-loader',
]
}, {
resourceQuery: /viewbox/,
use: [
{
loader: 'svg-inline-loader',
Expand All @@ -36,6 +37,16 @@ const svgRule = {
'./buildtools/svg-viewbox-loader',
'svgo-loader',
]
}, {
use: [
{
loader: 'svg-inline-loader',
options: {
removeSVGTagAttrs: false,
},
},
'svgo-loader',
]
}]
};

Expand Down
4 changes: 2 additions & 2 deletions buildtools/webpack.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ const svgRule = {
'svgo-loader',
]
}, {
resourceQuery: /inline/,
resourceQuery: /viewbox/,
use: [
{
loader: 'svg-inline-loader',
options: {
removeSVGTagAttrs: false,
},
},
'./buildtools/svg-viewbox-loader',
'svgo-loader',
]
}, {
Expand All @@ -44,7 +45,6 @@ const svgRule = {
removeSVGTagAttrs: false,
},
},
'./buildtools/svg-viewbox-loader',
'svgo-loader',
]
}]
Expand Down
4 changes: 1 addition & 3 deletions contribs/gmf/apps/desktop_alt/Controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,7 @@ module.value('gmfPermalinkOptions', /** @type {import('gmf/permalink/Permalink.j
crosshairStyle: [
new Style({
image: new Icon({
src: 'data:image/svg+xml;base64,' + btoa(require('./image/crosshair.svg?inline')),
// Also working
// src: require('./image/crosshair.svg?url'),
src: 'data:image/svg+xml;base64,' + btoa(require('./image/crosshair.svg?viewbox')),
imgSize: [22, 22],
})
})
Expand Down
4 changes: 2 additions & 2 deletions contribs/gmf/apps/desktop_alt/image/crosshair.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion contribs/gmf/apps/desktop_alt/index.html.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</div>
<header>
<div class="logo">
<%=require("./image/logo.svg?inline")%>
<%=require("./image/logo.svg")%>
<span>by Camptocamp</span>
</div>
</header>
Expand Down
12 changes: 6 additions & 6 deletions contribs/gmf/src/drawing/drawFeatureComponent.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
class="btn btn-default ngeo-drawfeature-point"
ng-class="{active: dfCtrl.drawPoint.active}"
ng-model="dfCtrl.drawPoint.active">
<%=require('gmf/icons/point.svg')%>
<%=require('gmf/icons/point.svg?viewbox&height=1em')%>
</a>
<a
data-toggle="tooltip"
Expand All @@ -24,7 +24,7 @@
class="btn btn-default ngeo-drawfeature-linestring"
ng-class="{active: dfCtrl.measureLength.active}"
ng-model="dfCtrl.measureLength.active">
<%=require('gmf/icons/line.svg')%>
<%=require('gmf/icons/line.svg?viewbox&height=1em')%>
</a>
<a
data-toggle="tooltip"
Expand All @@ -35,7 +35,7 @@
class="btn btn-default ngeo-drawfeature-polygon"
ng-class="{active: dfCtrl.measureArea.active}"
ng-model="dfCtrl.measureArea.active">
<%=require('gmf/icons/polygon.svg')%>
<%=require('gmf/icons/polygon.svg?viewbox&height=1em')%>
</a>
<a
data-toggle="tooltip"
Expand All @@ -46,7 +46,7 @@
class="btn btn-default ngeo-drawfeature-circle"
ng-class="{active: dfCtrl.measureAzimut.active}"
ng-model="dfCtrl.measureAzimut.active">
<%=require('gmf/icons/circle.svg')%>
<%=require('gmf/icons/circle.svg?viewbox&height=1em')%>
</a>
<a
data-toggle="tooltip"
Expand All @@ -57,7 +57,7 @@
class="btn btn-default ngeo-drawfeature-rectangle"
ng-class="{active: dfCtrl.drawRectangle.active}"
ng-model="dfCtrl.drawRectangle.active">
<%=require('gmf/icons/rectangle.svg')%>
<%=require('gmf/icons/rectangle.svg?viewbox&height=1em')%>
</a>
<a
data-toggle="tooltip"
Expand All @@ -68,7 +68,7 @@
class="btn btn-default ngeo-drawfeature-text"
ng-class="{active: dfCtrl.drawText.active}"
ng-model="dfCtrl.drawText.active">
<%=require('gmf/icons/text.svg')%>
<%=require('gmf/icons/text.svg?viewbox&height=1em')%>
</a>
</ngeo-drawfeature>

Expand Down
12 changes: 6 additions & 6 deletions contribs/gmf/src/editing/editFeatureComponent.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@
ng-model="efCtrl.createActive">
<span ng-switch="::efCtrl.geomType">
<span ng-switch-when="Point">
<%=require('gmf/icons/point.svg')%>
<%=require('gmf/icons/point.svg?viewbox&height=1em')%>
{{'Draw new point' | translate}}
</span>
<span ng-switch-when="MultiPoint">
<%=require('gmf/icons/point.svg')%>
<%=require('gmf/icons/point.svg?viewbox&height=1em')%>
{{'Draw new point' | translate}}
</span>
<span ng-switch-when="LineString">
<%=require('gmf/icons/line.svg')%>
<%=require('gmf/icons/line.svg?viewbox&height=1em')%>
{{'Draw new linestring' | translate}}
</span>
<span ng-switch-when="MultiLineString">
<%=require('gmf/icons/line.svg')%>
<%=require('gmf/icons/line.svg?viewbox&height=1em')%>
{{'Draw new linestring' | translate}}
</span>
<span ng-switch-when="Polygon">
<%=require('gmf/icons/polygon.svg')%>
<%=require('gmf/icons/polygon.svg?viewbox&height=1em')%>
{{'Draw new polygon' | translate}}
</span>
<span ng-switch-when="MultiPolygon">
<%=require('gmf/icons/polygon.svg')%>
<%=require('gmf/icons/polygon.svg?viewbox&height=1em')%>
{{'Draw new polygon' | translate}}
</span>
</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
<span
ng-if="::(layer.Layer !== undefined && layer.Layer.length)"
class="fa fa-fw gmf-wmscapabilitylayertreenode-group"
><%=require('gmf/icons/layers.svg')%></span>
><%=require('gmf/icons/layers.svg?viewbox&height=1em')%></span>

<a
class="fa fa-circle-thin gmf-wmscapabilitylayertreenode-no-icon fa-fw "
Expand Down
8 changes: 4 additions & 4 deletions contribs/gmf/src/layertree/component.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
ng-click="::gmfLayertreeCtrl.toggleActive(layertreeCtrl)"
ng-if="::!layertreeCtrl.node.children && !gmfLayertreeCtrl.getLegendIconURL(layertreeCtrl)"
class="gmf-layertree-layer-icon gmf-layertree-no-layer-icon"
><%=require('gmf/icons/dot.svg')%></a>
><%=require('gmf/icons/dot.svg?viewbox&height=1em')%></a>

<a
href
Expand All @@ -58,13 +58,13 @@
ng-click="::gmfLayertreeCtrl.toggleActive(layertreeCtrl)"
ng-if="layertreeCtrl.node.children && !layertreeCtrl.layer.loading"
class="gmf-layertree-state gmf-not-intermediate"
><%=require('gmf/icons/layers.svg')%></a>
><%=require('gmf/icons/layers.svg?viewbox&height=1em')%></a>
<a
href
ng-click="::gmfLayertreeCtrl.toggleActive(layertreeCtrl)"
ng-if="layertreeCtrl.node.children && !layertreeCtrl.layer.loading"
class="gmf-layertree-state gmf-intermediate"
><%=require('gmf/icons/stack-indeterminate.svg')%></a>
><%=require('gmf/icons/stack-indeterminate.svg?viewbox&height=1em')%></a>

<a
href
Expand All @@ -83,7 +83,7 @@
data-title="{{'Not visible at current scale. Click to zoom.'|translate}}"
ng-click="::gmfLayertreeCtrl.zoomToResolution(layertreeCtrl); $event.preventDefault(); $event.stopPropagation();"
ng-if="gmfLayertreeCtrl.getNodeState(layertreeCtrl) == 'on'"
><%=require('gmf/icons/search-go.svg')%></i>
><%=require('gmf/icons/search-go.svg?viewbox&height=1em')%></i>

<span
ngeo-popover
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class="fa fa-fw gmf-layertree-state"
href
ng-click="$ctrl.toggle()"
><%=require('gmf/icons/layers.svg')%></a>
><%=require('gmf/icons/layers.svg?viewbox&height=1em')%></a>

<a
class="gmf-layertree-name"
Expand Down
2 changes: 1 addition & 1 deletion contribs/gmf/src/permalink/Permalink.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ export function PermalinkService(
this.crosshairStyle_ = [new olStyleStyle({
image: new olStyleIcon({
// @ts-ignore: webpack
src: require('!!file-loader!gmf/permalink/crosshair.svg')
src: 'data:image/svg+xml;base64,' + btoa(require('gmf/permalink/crosshair.svg'))
})
})];
}
Expand Down
Loading

0 comments on commit 54c1112

Please sign in to comment.