xyc
2025-02-21 664db98c9e8595ce4dd636a27f480e3a08b81ff5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/**
 * Partial polyfill of `SVGTransformList`
 * @module SVGTransformList
 *
 * @license MIT
 *
 * @copyright 2010 Alexis Deveria, 2010 Jeff Schiller
 */
 
import {NS} from './namespaces.js';
import {supportsNativeTransformLists} from './browser.js';
 
const svgroot = document.createElementNS(NS.SVG, 'svg');
 
// Helper function.
function transformToString (xform) {
  const m = xform.matrix;
  let text = '';
  switch (xform.type) {
  case 1: // MATRIX
    text = 'matrix(' + [m.a, m.b, m.c, m.d, m.e, m.f].join(',') + ')';
    break;
  case 2: // TRANSLATE
    text = 'translate(' + m.e + ',' + m.f + ')';
    break;
  case 3: // SCALE
    if (m.a === m.d) {
      text = 'scale(' + m.a + ')';
    } else {
      text = 'scale(' + m.a + ',' + m.d + ')';
    }
    break;
  case 4: { // ROTATE
    let cx = 0;
    let cy = 0;
    // this prevents divide by zero
    if (xform.angle !== 0) {
      const K = 1 - m.a;
      cy = (K * m.f + m.b * m.e) / (K * K + m.b * m.b);
      cx = (m.e - m.b * cy) / K;
    }
    text = 'rotate(' + xform.angle + ' ' + cx + ',' + cy + ')';
    break;
  }
  }
  return text;
}
 
/**
 * Map of SVGTransformList objects.
 */
let listMap_ = {};
 
/**
* @interface module:SVGTransformList.SVGEditTransformList
* @property {Integer} numberOfItems unsigned long
*/
/**
* @function module:SVGTransformList.SVGEditTransformList#clear
* @returns {undefined}
*/
/**
* @function module:SVGTransformList.SVGEditTransformList#initialize
* @param {SVGTransform} newItem
* @returns {SVGTransform}
*/
/**
* (DOES NOT THROW DOMException, INDEX_SIZE_ERR)
* @function module:SVGTransformList.SVGEditTransformList#getItem
* @param {Integer} index unsigned long
* @returns {SVGTransform}
*/
/**
* (DOES NOT THROW DOMException, INDEX_SIZE_ERR)
* @function module:SVGTransformList.SVGEditTransformList#insertItemBefore
* @param {SVGTransform} newItem
* @param {Integer} index unsigned long
* @returns {SVGTransform}
*/
/**
* (DOES NOT THROW DOMException, INDEX_SIZE_ERR)
* @function module:SVGTransformList.SVGEditTransformList#replaceItem
* @param {SVGTransform} newItem
* @param {Integer} index unsigned long
* @returns {SVGTransform}
*/
/**
* (DOES NOT THROW DOMException, INDEX_SIZE_ERR)
* @function module:SVGTransformList.SVGEditTransformList#removeItem
* @param {Integer} index unsigned long
* @returns {SVGTransform}
*/
/**
* @function module:SVGTransformList.SVGEditTransformList#appendItem
* @param {SVGTransform} newItem
* @returns {SVGTransform}
*/
/**
* NOT IMPLEMENTED
* @ignore
* @function module:SVGTransformList.SVGEditTransformList#createSVGTransformFromMatrix
* @param {SVGMatrix} matrix
* @returns {SVGTransform}
*/
/**
* NOT IMPLEMENTED
* @ignore
* @function module:SVGTransformList.SVGEditTransformList#consolidate
* @returns {SVGTransform}
*/
 
/**
* SVGTransformList implementation for Webkit.
* These methods do not currently raise any exceptions.
* These methods also do not check that transforms are being inserted.  This is basically
* implementing as much of SVGTransformList that we need to get the job done.
*/
export class SVGTransformList {
  /**
  * @param {Element} elem
  */
  constructor (elem) {
    this._elem = elem || null;
    this._xforms = [];
    // TODO: how do we capture the undo-ability in the changed transform list?
    this._update = function () {
      let tstr = '';
      // /* const concatMatrix = */ svgroot.createSVGMatrix();
      for (let i = 0; i < this.numberOfItems; ++i) {
        const xform = this._list.getItem(i);
        tstr += transformToString(xform) + ' ';
      }
      this._elem.setAttribute('transform', tstr);
    };
    this._list = this;
    this._init = function () {
      // Transform attribute parser
      let str = this._elem.getAttribute('transform');
      if (!str) { return; }
 
      // TODO: Add skew support in future
      const re = /\s*((scale|matrix|rotate|translate)\s*\(.*?\))\s*,?\s*/;
      let m = true;
      while (m) {
        m = str.match(re);
        str = str.replace(re, '');
        if (m && m[1]) {
          const x = m[1];
          const bits = x.split(/\s*\(/);
          const name = bits[0];
          const valBits = bits[1].match(/\s*(.*?)\s*\)/);
          valBits[1] = valBits[1].replace(/(\d)-/g, '$1 -');
          const valArr = valBits[1].split(/[, ]+/);
          const letters = 'abcdef'.split('');
          const mtx = svgroot.createSVGMatrix();
          Object.values(valArr).forEach(function (item, i) {
            valArr[i] = parseFloat(item);
            if (name === 'matrix') {
              mtx[letters[i]] = valArr[i];
            }
          });
          const xform = svgroot.createSVGTransform();
          const fname = 'set' + name.charAt(0).toUpperCase() + name.slice(1);
          const values = name === 'matrix' ? [mtx] : valArr;
 
          if (name === 'scale' && values.length === 1) {
            values.push(values[0]);
          } else if (name === 'translate' && values.length === 1) {
            values.push(0);
          } else if (name === 'rotate' && values.length === 1) {
            values.push(0, 0);
          }
          xform[fname].apply(xform, values);
          this._list.appendItem(xform);
        }
      }
    };
    this._removeFromOtherLists = function (item) {
      if (item) {
        // Check if this transform is already in a transformlist, and
        // remove it if so.
        let found = false;
        for (const id in listMap_) {
          const tl = listMap_[id];
          for (let i = 0, len = tl._xforms.length; i < len; ++i) {
            if (tl._xforms[i] === item) {
              found = true;
              tl.removeItem(i);
              break;
            }
          }
          if (found) {
            break;
          }
        }
      }
    };
 
    this.numberOfItems = 0;
  }
  /**
  * @returns {undefined}
  */
  clear () {
    this.numberOfItems = 0;
    this._xforms = [];
  }
 
  /**
  * @param {SVGTransform} newItem
  * @returns {SVGTransform}
  */
  initialize (newItem) {
    this.numberOfItems = 1;
    this._removeFromOtherLists(newItem);
    this._xforms = [newItem];
  }
 
  /**
  * @param {Integer} index unsigned long
  * @throws {Error}
  * @returns {SVGTransform}
  */
  getItem (index) {
    if (index < this.numberOfItems && index >= 0) {
      return this._xforms[index];
    }
    const err = new Error('DOMException with code=INDEX_SIZE_ERR');
    err.code = 1;
    throw err;
  }
 
  /**
  * @param {SVGTransform} newItem
  * @param {Integer} index unsigned long
  * @returns {SVGTransform}
  */
  insertItemBefore (newItem, index) {
    let retValue = null;
    if (index >= 0) {
      if (index < this.numberOfItems) {
        this._removeFromOtherLists(newItem);
        const newxforms = new Array(this.numberOfItems + 1);
        // TODO: use array copying and slicing
        let i;
        for (i = 0; i < index; ++i) {
          newxforms[i] = this._xforms[i];
        }
        newxforms[i] = newItem;
        for (let j = i + 1; i < this.numberOfItems; ++j, ++i) {
          newxforms[j] = this._xforms[i];
        }
        this.numberOfItems++;
        this._xforms = newxforms;
        retValue = newItem;
        this._list._update();
      } else {
        retValue = this._list.appendItem(newItem);
      }
    }
    return retValue;
  }
 
  /**
  * @param {SVGTransform} newItem
  * @param {Integer} index unsigned long
  * @returns {SVGTransform}
  */
  replaceItem (newItem, index) {
    let retValue = null;
    if (index < this.numberOfItems && index >= 0) {
      this._removeFromOtherLists(newItem);
      this._xforms[index] = newItem;
      retValue = newItem;
      this._list._update();
    }
    return retValue;
  }
 
  /**
  * @param {Integer} index unsigned long
  * @throws {Error}
  * @returns {SVGTransform}
  */
  removeItem (index) {
    if (index < this.numberOfItems && index >= 0) {
      const retValue = this._xforms[index];
      const newxforms = new Array(this.numberOfItems - 1);
      let i;
      for (i = 0; i < index; ++i) {
        newxforms[i] = this._xforms[i];
      }
      for (let j = i; j < this.numberOfItems - 1; ++j, ++i) {
        newxforms[j] = this._xforms[i + 1];
      }
      this.numberOfItems--;
      this._xforms = newxforms;
      this._list._update();
      return retValue;
    }
    const err = new Error('DOMException with code=INDEX_SIZE_ERR');
    err.code = 1;
    throw err;
  }
 
  /**
  * @param {SVGTransform} newItem
  * @returns {SVGTransform}
  */
  appendItem (newItem) {
    this._removeFromOtherLists(newItem);
    this._xforms.push(newItem);
    this.numberOfItems++;
    this._list._update();
    return newItem;
  }
}
 
/**
* @function module:SVGTransformList.resetListMap
* @returns {undefined}
*/
export const resetListMap = function () {
  listMap_ = {};
};
 
/**
 * Removes transforms of the given element from the map.
 * @function module:SVGTransformList.removeElementFromListMap
 * @param {Element} elem - a DOM Element
 * @returns {undefined}
 */
export let removeElementFromListMap = function (elem) {
  if (elem.id && listMap_[elem.id]) {
    delete listMap_[elem.id];
  }
};
 
/**
* Returns an object that behaves like a `SVGTransformList` for the given DOM element.
* @function module:SVGTransformList.getTransformList
* @param {Element} elem - DOM element to get a transformlist from
* @todo The polyfill should have `SVGAnimatedTransformList` and this should use it
* @returns {SVGAnimatedTransformList|SVGTransformList}
*/
export const getTransformList = function (elem) {
  if (!supportsNativeTransformLists()) {
    const id = elem.id || 'temp';
    let t = listMap_[id];
    if (!t || id === 'temp') {
      listMap_[id] = new SVGTransformList(elem);
      listMap_[id]._init();
      t = listMap_[id];
    }
    return t;
  }
  if (elem.transform) {
    return elem.transform.baseVal;
  }
  if (elem.gradientTransform) {
    return elem.gradientTransform.baseVal;
  }
  if (elem.patternTransform) {
    return elem.patternTransform.baseVal;
  }
 
  return null;
};
 
/**
* @callback module:SVGTransformList.removeElementFromListMap
* @param {Element} elem
*/
/**
* Replace `removeElementFromListMap` for unit-testing.
* @function module:SVGTransformList.changeRemoveElementFromListMap
* @param {module:SVGTransformList.removeElementFromListMap} cb Passed a single argument `elem`
* @returns {undefined}
*/
export const changeRemoveElementFromListMap = function (cb) {
  removeElementFromListMap = cb;
};