Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | /* * Paper.js - The Swiss Army Knife of Vector Graphics Scripting. * http://paperjs.org/ * * Copyright (c) 2011 - 2020, Jürg Lehni & Jonathan Puckey * http://juerglehni.com/ & https://puckey.studio/ * * Distributed under the MIT license. See LICENSE file for details. * * All rights reserved. */ // TODO: remove eslint-disable comment and deal with errors over time /* eslint-disable */ import { ref } from '~/globals'; import { Base } from '~/straps'; import { Point } from '~/basic/Point'; import { Size } from '~/basic/Size'; import { DomEvent } from '~/dom/DomEvent'; import { Color } from '~/style/Color'; import { Tool } from '~/tool/Tool'; import { PaperScope } from './PaperScope'; import { Http } from '~/net/Http'; /** * @name PaperScript * @namespace */ Base.exports.PaperScript = async function () { // `this` == global scope, as the function is called with `.call(this);` var global = this, // See if there is a global Acorn in the browser already. acorn = global.acorn; // // Also try importing an outside version of Acorn. // if (!acorn && typeof require !== "undefined") { // try { // acorn = require("acorn"); // } catch (e) {} // } // If no Acorn was found, load the bundled version. if (!acorn) { // Provide our own local exports and module object so that Acorn gets // assigned to it and ends up in the local acorn object. var exports, module; acorn = exports = module = {}; // /*#*/ include("../../node_modules/acorn/acorn.js", { exports: false }); acorn = await import('acorn'); // Clear object again if it wasn't loaded here; for load.js, see below. if (!acorn.version) acorn = null; } function parse(code, options) { // https://www.npmjs.com/package/acorn#interface options.ecmaVersion = 2022; // NOTE: When using load.js, Acorn will end up in global.acorn and will // not be immediately available, so we need to check for it here again. // We also give global.acorn the preference over the bundled one, so // people can load their own preferred version in sketch.paperjs.org return (global.acorn || acorn).parse(code, options); } // Operators to overload var binaryOperators = { // The hidden math methods are to be injected specifically, see below. '+': '__add', '-': '__subtract', '*': '__multiply', '/': '__divide', '%': '__modulo', '==': '__equals', '!=': '__equals', }; var unaryOperators = { '-': '__negate', '+': '__self', }; // Inject underscored math methods as aliases to Point, Size and Color. var fields = Base.each( ['add', 'subtract', 'multiply', 'divide', 'modulo', 'equals', 'negate'], function (name) { // Create an alias for each math method to be injected into the // classes using Straps.js' #inject() this['__' + name] = '#' + name; }, { // Needed for '+' unary operator: __self: function () { return this; }, } ); Point.inject(fields); Size.inject(fields); Color.inject(fields); // Use very short name for the binary operator (__$__) as well as the // unary operator ($__), as operations will be replaced with then. // The underscores stands for the values, and the $ for the operators. // Binary Operator Handler function __$__(left, operator, right) { var handler = binaryOperators[operator]; if (left && left[handler]) { var res = left[handler](right); return operator === '!=' ? !res : res; } switch (operator) { case '+': return left + right; case '-': return left - right; case '*': return left * right; case '/': return left / right; case '%': return left % right; case '==': return left == right; case '!=': return left != right; } } // Unary Operator Handler function $__(operator, value) { var handler = unaryOperators[operator]; if (value && value[handler]) return value[handler](); switch (operator) { case '+': return +value; case '-': return -value; } } // AST Helpers /** * Compiles PaperScript code into JavaScript code. * * @name PaperScript.compile * @function * * @option options.url {String} the url of the source, for source-map * generation * @option options.source {String} the source to be used for the source- * mapping, in case the code that's passed in has already been mingled. * * @param {String} code the PaperScript code * @param {Object} [options] the compilation options * @return {Object} an object holding the compiled PaperScript translated * into JavaScript code along with source-maps and other information. */ function compile(code, options) { if (!code) return ''; options = options || {}; // Use Acorn or Esprima to translate the code into an AST structure // which is then walked and parsed for operators to overload. Instead of // modifying the AST and translating it back to code, we directly change // the source code based on the parser's range information, to preserve // line-numbers in syntax errors and remove the need for Escodegen. // Track code insertions so their differences can be added to the // original offsets. var insertions = []; // Converts an original offset to the one in the current state of the // modified code. function getOffset(offset) { // Add all insertions before this location together to calculate // the current offset for (var i = 0, l = insertions.length; i < l; i++) { var insertion = insertions[i]; if (insertion[0] >= offset) break; offset += insertion[1]; } return offset; } // Returns the node's code as a string, taking insertions into account. function getCode(node) { return code.substring(getOffset(node.range[0]), getOffset(node.range[1])); } // Returns the code between two nodes, e.g. an operator and white-space. function getBetween(left, right) { return code.substring(getOffset(left.range[1]), getOffset(right.range[0])); } // Replaces the node's code with a new version and keeps insertions // information up-to-date. function replaceCode(node, str) { var start = getOffset(node.range[0]), end = getOffset(node.range[1]), insert = 0; // Sort insertions by their offset, so getOffset() can do its thing for (var i = insertions.length - 1; i >= 0; i--) { if (start > insertions[i][0]) { insert = i + 1; break; } } insertions.splice(insert, 0, [start, str.length - end + start]); code = code.substring(0, start) + str + code.substring(end); } function handleOverloading(node, parent) { switch (node.type) { case 'UnaryExpression': // -a if (node.operator in unaryOperators && node.argument.type !== 'Literal') { var arg = getCode(node.argument); replaceCode(node, '$__("' + node.operator + '", ' + arg + ')'); } break; case 'BinaryExpression': // a + b, a - b, a / b, a * b, a == b, ... if (node.operator in binaryOperators && node.left.type !== 'Literal') { var left = getCode(node.left), right = getCode(node.right), between = getBetween(node.left, node.right), operator = node.operator; replaceCode( node, '__$__(' + left + ',' + // To preserve line-breaks, get the code in between // left & right, and replace the occurrence of the // operator with its string counterpart: between.replace(new RegExp('\\' + operator), '"' + operator + '"') + ', ' + right + ')' ); } break; case 'UpdateExpression': // a++, a--, ++a, --a case 'AssignmentExpression': /// a += b, a -= b var parentType = parent && parent.type; if ( !( // Filter out for statements to allow loop increments // to perform well ( parentType === 'ForStatement' || // We need to filter out parents that are comparison // operators, e.g. for situations like `if (++i < 1)`, // as we can't replace that with // `if (__$__(i, "+", 1) < 1)` // Match any operator beginning with =, !, < and >. (parentType === 'BinaryExpression' && /^[=!<>]/.test(parent.operator)) || // array[i++] is a MemberExpression with computed = true // We can't replace that with array[__$__(i, "+", 1)]. (parentType === 'MemberExpression' && parent.computed) ) ) ) { if (node.type === 'UpdateExpression') { var arg = getCode(node.argument), exp = '__$__(' + arg + ', "' + node.operator[0] + '", 1)', str = arg + ' = ' + exp; if (node.prefix) { // A prefixed update expression (++a / --a), // wrap expression in paranthesis. See #1611 str = '(' + str + ')'; } else if ( // A suffixed update expression (a++, a--), // assign the old value before updating it. // See #691, #1450 parentType === 'AssignmentExpression' || parentType === 'VariableDeclarator' || parentType === 'BinaryExpression' ) { // Handle special case where the old value is // assigned to itself, and the expression is just // executed after, e.g.: `var x = ***; x = x++;` if (getCode(parent.left || parent.id) === arg) str = exp; str = arg + '; ' + str; } replaceCode(node, str); } else { // AssignmentExpression if (/^.=$/.test(node.operator) && node.left.type !== 'Literal') { var left = getCode(node.left), right = getCode(node.right), exp = left + ' = __$__(' + left + ', "' + node.operator[0] + '", ' + right + ')'; // If the original expression is wrapped in // parenthesis, do the same with the replacement: replaceCode(node, /^\(.*\)$/.test(getCode(node)) ? '(' + exp + ')' : exp); } } } break; } } function handleExports(node) { switch (node.type) { case 'ExportDefaultDeclaration': // Convert `export default` to `module.exports = ` statements: replaceCode( { range: [node.start, node.declaration.start], }, 'module.exports = ' ); break; case 'ExportNamedDeclaration': // Convert named exports to `module.exports.NAME = NAME;` // statements both for new declarations and existing specifiers: var declaration = node.declaration; var specifiers = node.specifiers; if (declaration) { var declarations = declaration.declarations; if (declarations) { declarations.forEach(function (dec) { replaceCode(dec, 'module.exports.' + getCode(dec)); }); replaceCode( { range: [node.start, declaration.start + declaration.kind.length], }, '' ); } } else if (specifiers) { var exports = specifiers .map(function (specifier) { var name = getCode(specifier); return 'module.exports.' + name + ' = ' + name + '; '; }) .join(''); if (exports) { replaceCode(node, exports); } } break; } } // Recursively walks the AST and replaces the code of certain nodes function walkAST(node, parent, paperFeatures) { if (node) { // The easiest way to walk through the whole AST is to simply // loop over each property of the node and filter out fields we // don't need to consider... for (var key in node) { if (key !== 'range' && key !== 'loc') { var value = node[key]; if (Array.isArray(value)) { for (var i = 0, l = value.length; i < l; i++) { walkAST(value[i], node, paperFeatures); } } else if (value && typeof value === 'object') { // Don't use Base.isPlainObject() for these since // Acorn.js uses its own internal prototypes now. walkAST(value, node, paperFeatures); } } } if (paperFeatures.operatorOverloading !== false) { handleOverloading(node, parent); } if (paperFeatures.moduleExports !== false) { handleExports(node); } } } // Source-map support: // Encodes a Variable Length Quantity as a Base64 string. // See: https://www.html5rocks.com/en/tutorials/developertools/sourcemaps/ function encodeVLQ(value) { var res = '', base64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; value = (Math.abs(value) << 1) + (value < 0 ? 1 : 0); while (value || !res) { var next = value & (32 - 1); value >>= 5; if (value) next |= 32; res += base64[next]; } return res; } var url = options.url || '', sourceMaps = options.sourceMaps, paperFeatures = options.paperFeatures || {}, // Include the original code in the sourceMap if there is no linked // source file so the debugger can still display it correctly. source = options.source || code, offset = options.offset || 0, agent = ref.paper.agent, version = agent.versionNumber, offsetCode = false, lineBreaks = /\r\n|\n|\r/gm, map; // TODO: Verify these browser versions for source map support, and check // other browsers. if ( sourceMaps && ((agent.chrome && version >= 30) || (agent.webkit && version >= 537.76) || // >= Safari 7.0.4 (agent.firefox && version >= 23) || agent.node) ) { if (agent.node) { // -2 required to remove function header: // https://code.google.com/p/chromium/issues/detail?id=331655 offset -= 2; } else if (globalThis.window && url && !globalThis.window.location.href.indexOf(url)) { // If the code stems from the actual html page, determine the // offset of inlined code. var html = globalThis.document.getElementsByTagName('html')[0].innerHTML; // Count the amount of line breaks in the html before this code // to determine the offset. offset = html.substr(0, html.indexOf(code) + 1).match(lineBreaks).length + 1; } // A hack required by older versions of browsers to align inlined // code: Instead of starting the mappings at the given offset, we // have to shift the actual code down to the place in the original // file, as source-map support seems incomplete in these browsers. offsetCode = offset > 0 && !( (agent.chrome && version >= 36) || (agent.safari && version >= 600) || (agent.firefox && version >= 40) || agent.node ); var mappings = ['AA' + encodeVLQ(offsetCode ? 0 : offset) + 'A']; // Create empty entries by the amount of lines + 1, so join can be // used below to produce the actual instructions that many times. mappings.length = (code.match(lineBreaks) || []).length + 1 + (offsetCode ? offset : 0); map = { version: 3, file: url, names: [], // Since PaperScript doesn't actually change the offsets between // the lines of the original code, all that is required is a // mappings string that increments by one between each line. // AACA is the instruction to increment the line by one. // TODO: Add support for column offsets! mappings: mappings.join(';AACA'), sourceRoot: '', sources: [url], sourcesContent: [source], }; } if (paperFeatures.operatorOverloading !== false || paperFeatures.moduleExports !== false) { // Now do the parsing magic walkAST( parse(code, { ranges: true, preserveParens: true, sourceType: 'module', }), null, paperFeatures ); } if (map) { if (offsetCode) { // Adjust the line offset of the resulting code if required. // This is part of a browser hack, see above. code = new Array(offset + 1).join('\n') + code; } if (/^(inline|both)$/.test(sourceMaps)) { code += '\n//# sourceMappingURL=data:application/json;base64,' + globalThis.btoa(unescape(encodeURIComponent(JSON.stringify(map)))); } code += '\n//# sourceURL=' + (url || 'paperscript'); } return { url: url, source: source, code: code, map: map, }; } /** * Compiles the PaperScript code into a compiled function and executes it. * The compiled function receives all properties of the passed {@link * PaperScope} as arguments, to emulate a global scope with unaffected * performance. It also installs global view and tool handlers automatically * on the respective objects. * * @name PaperScript.execute * @function * * @option options.url {String} the url of the source, for source-map * generation * @option options.source {String} the source to be used for the source- * mapping, in case the code that's passed in has already been mingled. * * @param {String} code the PaperScript code * @param {PaperScope} scope the scope for which the code is executed * @param {Object} [options] the compilation options * @return {Object} the exports defined in the executed code */ function execute(code, scope, options) { // Set currently active scope. ref.paper = scope; var view = scope.getView(), // Only create a tool if the tool object is accessed or something // resembling a global tool handler is contained in the code, but // no tool objects are actually created. tool = /\btool\.\w+|\s+on(?:Key|Mouse)(?:Up|Down|Move|Drag)\b/.test(code) && !/\bnew\s+Tool\b/.test(code) ? new Tool() : null, toolHandlers = tool ? tool._events : [], // Compile a list of all handlers that can be defined globally // inside the PaperScript. These are passed on to the function as // undefined arguments, so that their name exists, rather than // injecting a code line that defines them as variables. They are // exported again at the end of the function. handlers = ['onFrame', 'onResize'].concat(toolHandlers), // compile a list of parameter names for all variables that need to // appear as globals inside the script. At the same time, also // collect their values, so we can pass them on as arguments in the // function call. params = [], args = [], func, compiled = typeof code === 'object' ? code : compile(code, options); code = compiled.code; function expose(scope, hidden) { // Look through all enumerable properties on the scope and expose // these too as pseudo-globals, but only if they seem to be in use. for (var key in scope) { // Next to \b well also need to match \s and \W in the beginning // of $__, since $ is not part of \w. And that causes \b to not // match ^ longer, so include that specifically too. if ( (hidden || !/^_/.test(key)) && new RegExp('([\\b\\s\\W]|^)' + key.replace(/\$/g, '\\$') + '\\b').test(code) ) { params.push(key); args.push(scope[key]); } } } expose({ __$__: __$__, $__: $__, paper: scope, tool: tool }, true); // @ts-expect-error expose(scope); // Add a fake `module.exports` object so PaperScripts can export things. code = 'var module = { exports: {} }; ' + code; // Finally define the handler variable names as parameters and compose // the string describing the properties for the returned exports object // at the end of the code execution, so we can retrieve their values // from the function call. var exports = Base.each( handlers, function (key) { // Check for each handler explicitly and only export them if they // seem to exist. if (new RegExp('\\s+' + key + '\\b').test(code)) { params.push(key); this.push('module.exports.' + key + ' = ' + key + ';'); } }, [] ).join('\n'); // Add the setting of the exported handlers to the end of the code. if (exports) { code += '\n' + exports; } // End by returning `module.exports` at the end of the generated code: code += '\nreturn module.exports;'; var agent = ref.paper.agent; if (globalThis.document && (agent.chrome || (agent.firefox && agent.versionNumber < 40))) { // On older Firefox, all error numbers inside dynamically compiled // code are relative to the line where the eval / compilation // happened. To fix this issue, we're temporarily inserting a new // script tag. // We also use this on Chrome to fix issues with compiled functions: // https://code.google.com/p/chromium/issues/detail?id=331655 var script = globalThis.document.createElement('script'), head = globalThis.document.head || globalThis.document.getElementsByTagName('head')[0]; // Add a new-line before the code on Firefox since the error // messages appear to be aligned to line number 0... if (agent.firefox) code = '\n' + code; script.appendChild( globalThis.document.createTextNode('document.__paperscript__ = function(' + params + ') {' + code + '\n}') ); head.appendChild(script); // @ts-expect-error = does not exist func = globalThis.document.__paperscript__; // @ts-expect-error = does not exist delete globalThis.document.__paperscript__; head.removeChild(script); } else { // @ts-expect-error = cannot assign func = Function(params, code); } var exports = func && func.apply(scope, args); var obj = exports || {}; // Now install the 'global' tool and view handlers, and we're done! Base.each(toolHandlers, function (key) { var value = obj[key]; if (value) tool[key] = value; }); if (view) { if (obj.onResize) view.setOnResize(obj.onResize); // Emit resize event directly, so any user // defined resize handlers are called. view.emit('resize', { size: view.size, delta: new Point(), }); if (obj.onFrame) view.setOnFrame(obj.onFrame); // Automatically request an update at the end. This is only needed // if the script does not actually produce anything yet, and the // used canvas contains previous content. view.requestUpdate(); } return exports; } function loadScript(script) { // Only load this script if it not loaded already. // Support both text/paperscript and text/x-paperscript: if (/^text\/(?:x-|)paperscript$/.test(script.type) && PaperScope.getAttribute(script, 'ignore') !== 'true') { // Produce a new PaperScope for this script now. Scopes are cheap so // let's not worry about the initial one that was already created. // Define an id for each PaperScript, so its scope can be retrieved // through PaperScope.get(). // If a canvas id is provided, pass it on to the PaperScope so a // project is created for it now. var canvasId = PaperScope.getAttribute(script, 'canvas'), canvas = globalThis.document.getElementById(canvasId), // To avoid possible duplicate browser requests for PaperScript // files, support the data-src attribute as well as src: // TODO: Consider switching from data-paper- to data- prefix // in PaperScope.getAttribute() and use it here too: src = script.src || script.getAttribute('data-src'), async = PaperScope.hasAttribute(script, 'async'), scopeAttribute = 'data-paper-scope'; if (!canvas) throw new Error('Unable to find canvas with id "' + canvasId + '"'); // See if there already is a scope for this canvas and reuse it, to // support multiple scripts per canvas. Otherwise create a new one. var scope = PaperScope.get(canvas.getAttribute(scopeAttribute)) || new PaperScope().setup(canvas); // Link the element to this scope, so we can reuse the scope when // compiling multiple scripts for the same element. canvas.setAttribute(scopeAttribute, scope._id); if (src) { // If we're loading from a source, request the source // synchronously to guarantee code is executed in the // same order the script tags appear. // If the async attribute is specified on the script element, // request the source asynchronously and execute as soon as // it is retrieved. Http.request({ url: src, async: async, mimeType: 'text/plain', onLoad: function (code) { execute(code, scope, src); }, }); } else { // We can simply get the code form the script tag. execute(script.innerHTML, scope, script.baseURI); } // Mark script as loaded now. script.setAttribute('data-paper-ignore', 'true'); return scope; } } function loadAll() { Base.each(globalThis.document && globalThis.document.getElementsByTagName('script'), loadScript); } /** * Loads, compiles and executes PaperScript code in the HTML document. Note * that this method is executed automatically for all scripts in the * document through a window load event. You can optionally call it earlier * (e.g. from a DOM ready event), or you can mark scripts to be ignored by * setting the attribute `ignore="true"` or `data-paper-ignore="true"`, and * call the `PaperScript.load(script)` method for each script separately * when needed. * * @name PaperScript.load * @function * @param {HTMLScriptElement} [script=null] the script to load. If none is * provided, all scripts of the HTML document are iterated over and * loaded * @return {PaperScope} the scope produced for the passed `script`, or * `undefined` of multiple scripts area loaded */ function load(script) { return script ? loadScript(script) : loadAll(); } if (globalThis.window) { // Catch cases where paper.js is loaded after the browser event has // already occurred. if (globalThis.document.readyState === 'complete') { // Handle it asynchronously setTimeout(loadAll); } else { DomEvent.add(globalThis.window, { load: loadAll }); } } return { compile: compile, execute: execute, load: load, parse: parse, calculateBinary: __$__, calculateUnary: $__, }; // Pass on `this` as the binding object, so we can reference Acorn both in // development and in the built library. }.call(this); |