Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(73)

Side by Side Diff: third_party/WebKit/Source/devtools/scripts/spritesheet_assembler/SVGSpriteSheet.js

Issue 2671413004: DevTools: introduce spritesheet assembler. (Closed)
Patch Set: rebaseline tests Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 var postsvg = require('postsvg');
2 var renameId = require('postsvg-rename-id');
3
4 class SVGSpriteSheet {
5 /**
6 * @param {number} width
7 * @param {number} height
8 * @param {!Map<!SVGSprite, !{x: number, y: number}>} spritePositions
9 */
10 constructor(width, height, spritePositions) {
11 this._width = width;
12 this._height = height;
13 this._sprites = Array.from(spritePositions.keys());
14 this._positions = spritePositions;
15 }
16
17 /**
18 * @return {number}
19 */
20 width() {
21 return this._width;
22 }
23
24 /**
25 * @return {number}
26 */
27 height() {
28 return this._height;
29 }
30
31 /**
32 * @return {!Array<!SVGSprite>}
33 */
34 sprites() {
35 return this._sprites;
36 }
37
38 /**
39 * @param {!SVGSprite} sprite
40 * @return {!{x: number, y: number}}
41 */
42 spritePosition(sprite) {
43 console.assert(this._positions.has(sprite), 'sprite ' + sprite.filePath + ' has undefined position!');
44 return this._positions.get(sprite);
45 }
46
47 /**
48 * @return {string}
49 */
50 svgText() {
51 if (this._svgText)
52 return this._svgText;
53
54 var svgBody = '';
55 var spriteId = 0;
56 for (var sprite of this._sprites) {
57 var spriteSVG = sprite.content.trim();
58
59 // Strip of <?xml> header, if any.
60 if (spriteSVG.startsWith('<?xml')) {
61 var headerIndexEnd = spriteSVG.indexOf('?>');
62 spriteSVG = spriteSVG.substring(headerIndexEnd + 2);
63 }
64
65 // Rename sprite ids to avoid clashing.
66 spriteSVG = postsvg().use(renameId({pattern: 'sprite' + (++spriteId) + '_[ id]'})).process(spriteSVG);
67
68 // Wrap sprite to SVG container with proper position.
69 var position = this._positions.get(sprite);
70 var prefix = `<svg x="${position.x}" y="${position.y}" width="${sprite.wid th}" height="${sprite.height}">`;
71 var suffix = '</svg>';
72 spriteSVG = prefix + spriteSVG + suffix;
73
74 svgBody += spriteSVG;
75 }
76
77 var xmlHeader = '<?xml version="1.0" encoding="utf-8"?>';
78 var spriteSheetHeader = `<svg width="${this._width}" height="${this._height} " xmlns="http://www.w3.org/2000/svg">`;
79 return xmlHeader + spriteSheetHeader + svgBody + '</svg>';
80 }
81 };
82
83 module.exports = SVGSpriteSheet;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698