-
Notifications
You must be signed in to change notification settings - Fork 1
/
IO.ts
346 lines (287 loc) · 10.5 KB
/
IO.ts
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
import { Geom } from "./Geom";
import { AnyShape, ShapeCollection, Sketch } from "./internal";
import parseSVG from "svg-path-parser"; // https://github.com/hughsk/svg-path-parser
const makeAbsolute = parseSVG.makeAbsolute; //
import * as txml from 'txml'; // for XML parsing because DOMParser is not available in a WebWorker: https://github.com/tobiasnickel/tXml
interface Asset
{
source:string
valid:boolean
locationType: 'url'|'local'; // on the web or on AY
url:string
localId:string
formatType: 'data'|'geodata'|'vector'|'bitmap'|'api'
format: string // TODO: more formats possible: jpg
content?:any // raw content
fetchError?:string
imported?:any // Shape or data ready for Archiyou
}
interface SvgTransform
{
x:number
y:number
}
export class IO
{
_oc:any;
_geom:Geom;
source:string;
asset:Asset // last asset
cache:{[key:string]:Asset} = {}; // save all assets
// SETTINGS
FORMAT_TO_TYPE = {
svg: 'vector',
json: 'data',
geojson: 'geodata',
jpg: 'bitmap',
gif: 'bitmap',
png: 'bitmap',
}
constructor(geom:Geom)
{
console.log('IO::constructor');
this._geom = geom;
this._oc = this?._geom?._oc;
}
async load(source:string, forceFetch:boolean=false, onlyCache:boolean=false)
{
console.log(`IO::load: ${source}`);
this.source = source;
let cachedAsset = await this._getAssetFromCache(source);
if (cachedAsset && !forceFetch)
{
return cachedAsset;
}
else if (!onlyCache)
{
// fetch
let asset = this._parse(this.source);
let fetchedAsset = await this._fetch(asset);
let importedAsset = this._importAsset(fetchedAsset); // set in cache automatically
return importedAsset;
}
else {
console.error(`IO:load: onlyCached forced, but nothing in cache under source ${source}`)
return null;
}
}
/** Load directly from hot cache (no async) */
loadFromCache(source:string):AnyShape|ShapeCollection
{
let cachedAsset = this.cache[source];
return cachedAsset?.imported || null;
}
_parse(source:string):Asset
{
const locationType = (source.includes('https')) ? 'url' : null;
const url = (locationType === 'url') ? source : null;
const localId = null; // TODO if we have local asset management
const format = url.split('.').slice(-1)[0];
const formatCheck = Object.keys(this.FORMAT_TO_TYPE).includes(format);
const formatType = this.FORMAT_TO_TYPE[format];
// TODO: api
// basic checks
const valid = (url && formatCheck && format && formatType) != null;
// AssetInfo
return {
source: source,
valid: valid,
locationType: locationType,
url: url,
localId: localId,
formatType: formatType,
format: format,
}
}
async _getAssetFromCache(source:string)
{
// to have it consistent with fetch we return a promise
let compScope = this;
let promise = new Promise((resolve, reject) => {
resolve(compScope.cache[source]);
});
return promise as any; // avoid TS error
}
async _fetch(asset:Asset)
{
let fetchedAsset = await this._fetchAsset(asset);
return fetchedAsset;
}
async _fetchAsset(asset:Asset)
{
if(asset.locationType === 'url')
{
switch(asset.format)
{
// TODO: more cases
default:
try
{
let r = await fetch(asset.source, {
method : 'GET',
headers: {
'Content-type': `text/plain`,
},
})
let loadedAsset = await this._handleFetchedAsset(asset,r);
return loadedAsset
}
catch(e)
{
this._handleFetchedAssetError(asset)
return null;
}
}
}
// TODO: fetch from API
}
async _handleFetchedAsset(asset:Asset, response:any)
{
const content = await response.text();
let fetchedAsset = { ...asset, content:content };
// NOTE: we only set asset in cache when also content is imported to Shapes or data
return fetchedAsset;
}
_handleFetchedAssetError(asset:Asset)
{
throw new Error(`IO::_fetch: Failed to fetch asset ${asset.source}: Check if it exists or the server allowes downloading from other domains!`);
}
/** Convert and import fetched asset to Archiyou entity: either some Shape or raw data like a text or json data structure */
_importAsset(asset:Asset):any // TODO: shape, string or Object
{
if(!asset.content)
{
console.error(`IO::_importAsset: Skipped asset "${asset.source}" because it has no content (probably an invalid source)`);
return null;
}
let imported;
switch(asset.format)
{
case 'svg':
imported = this._importSVG(asset);
break;
default:
// TODO: give error
imported = null;
}
// place asset (incuding imported ) cache
let importedAsset = { ...asset, imported: imported };
this.cache[asset.source] = importedAsset;
return importedAsset
}
_importSVG(asset:Asset)
{
// see for overview: https://developer.mozilla.org/en-US/docs/Web/SVG
if(!asset)
{
console.log('IO::_importSVG: Skipped invalid SVG')
return null;
}
let svgNodesWithTransform;
try
{
let svgTree = txml.parse(asset.content);
svgNodesWithTransform = this._getSvgNodesRecursive(svgTree); // [{ node: { tagName, attributes }, transform: {x,y} }]
}
catch(e)
{
console.log(`IO::_importSVG: parse error. Check if valid SVG!`);
return null;
}
if (!this._oc)
{
throw new Error(`IO:_importSVG: Cannot import SVG without reference to geom in constructor!`)
}
let svgShapeCollection = new ShapeCollection();
svgShapeCollection.name('importedSVG');
// parse SVG nodes
svgNodesWithTransform.forEach( nodeAndTransform =>
{
let node = nodeAndTransform.node;
let transform = nodeAndTransform.transform;
let nodeName = node.tagName;
let shapeOrShapes;
switch(nodeName)
{
case 'path':
shapeOrShapes = this._importSVGPath(node.attributes.d, transform);
shapeOrShapes.name(this._geom.getNextObjName('ImportedSVGPath'));
break;
}
svgShapeCollection.add(shapeOrShapes)
});
// because SVG has a y-axis that points down we mirror the imcoming shapes
//svgShapeCollection = svgShapeCollection._mirroredX(); // mirror around center of ShapeCollection
return svgShapeCollection;
}
_getSvgNodesRecursive(svgTree, svgNodesWithTransform = [], parentTransform:SvgTransform = { x: 0, y: 0 })
{
/* svgTree structure
[elem,elem] with elem { tagName, attributes : {}, children: [elem,elem..]}
*/
const TRAVERSE_DEEPER_TAGS = ['svg','g'];
const VALID_SVG_TAGS = [
'path',
'polygon',
'circle',
'line',
'polyline',
'rect',
// text
]
svgTree.forEach(elem =>
{
// traverse deeper
if (TRAVERSE_DEEPER_TAGS.includes(elem.tagName))
{
// traverse deeper, but do take transform with if in attributes
const transform = elem.attributes.transform ? this._parseTransformString(elem.attributes.transform) : parentTransform;
svgNodesWithTransform = this._getSvgNodesRecursive(elem.children, svgNodesWithTransform, transform)
}
// a valid graphic element
else if (VALID_SVG_TAGS.includes(elem.tagName))
{
let localNodeTransform = elem.attributes.transform ? this._parseTransformString(elem.attributes.transform) : { x: 0, y: 0 };
let nodeTransform = { x : parentTransform.x + localNodeTransform.x, y: parentTransform.y + localNodeTransform.y };
svgNodesWithTransform.push( { node: elem, transform: nodeTransform })
}
})
return svgNodesWithTransform;
}
_parseTransformString(t:string)
{
const PARSE_RE = /translate\(([-\d\.]+),([-\d\.]+)/
let m = t.match(PARSE_RE);
return m ? { x: parseFloat(m[1]), y: parseFloat(m[2]) } : null;
}
_importSVGPath(pathData:string, transform:SvgTransform = { x : 0, y: 0}):AnyShape|ShapeCollection
{
// path command to sketch command
const PATH_CMD_TO_SKETCH_CMD = {
'moveto' : { func: 'moveTo', args: (cmd) => [ [cmd.x + transform.x, cmd.y + transform.y]] },
'lineto' : { func: 'lineTo', args: (cmd) => [ [cmd.x + transform.x, cmd.y + transform.y]] },
'closepath' : { func: 'close', args: (cmd) => [] },
}
let commands = parseSVG(pathData);
let absCommands = makeAbsolute(commands);
let sketch = new Sketch('xy');
absCommands.every( (cmd) =>
{
let sketchCmd = PATH_CMD_TO_SKETCH_CMD[cmd.command];
if (!sketchCmd)
{
console.error(`IO::_importSVGPath: Not yet supported command "${cmd.command}. Skipped entire SVG Path!`)
// TODO: just skip?
return false;
}
// supported SVG Path command: apply to Sketch
let args = sketchCmd.args(cmd);
sketch[sketchCmd.func](...args);
return true
})
let shapeOrShapes = sketch.getShapes(); // return Shape of this SVG Path
sketch._removeSketchLayer(); // remove original sketch layer
this._geom.resetLayers();
return shapeOrShapes;
}
}