Brandon Goddard | 7ceb501 | 2020-07-09 19:36:57 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // Copyright (C) 2012 Google Inc. All rights reserved. |
| 6 | |
| 7 | // Redistribution and use in source and binary forms, with or without |
| 8 | // modification, are permitted provided that the following conditions |
| 9 | // are met: |
| 10 | |
| 11 | // 1. Redistributions of source code must retain the above copyright |
| 12 | // notice, this list of conditions and the following disclaimer. |
| 13 | // 2. Redistributions in binary form must reproduce the above copyright |
| 14 | // notice, this list of conditions and the following disclaimer in the |
| 15 | // documentation and/or other materials provided with the distribution. |
| 16 | // 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of |
| 17 | // its contributors may be used to endorse or promote products derived |
| 18 | // from this software without specific prior written permission. |
| 19 | |
| 20 | // THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
| 21 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 22 | // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 23 | // DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 24 | // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 25 | // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 26 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 27 | // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 29 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | |
| 32 | // @ts-nocheck |
| 33 | // TODO(crbug.com/1011811): Enable TypeScript compiler checks |
| 34 | |
Patrick Brosset | 8eafacc | 2020-08-05 08:18:41 | [diff] [blame] | 35 | export const DEFAULT_RULER_COLOR = 'rgba(128, 128, 128, 0.3)'; |
Patrick Brosset | 112a132 | 2020-08-04 12:30:37 | [diff] [blame] | 36 | |
Brandon Goddard | 7ceb501 | 2020-07-09 19:36:57 | [diff] [blame] | 37 | export function drawRulers(context, bounds, rulerAtRight, rulerAtBottom, color, dash) { |
| 38 | context.save(); |
| 39 | const width = canvasWidth; |
Patrick Brosset | 8eafacc | 2020-08-05 08:18:41 | [diff] [blame] | 40 | const height = canvasHeight; |
| 41 | context.strokeStyle = color || DEFAULT_RULER_COLOR; |
Brandon Goddard | 7ceb501 | 2020-07-09 19:36:57 | [diff] [blame] | 42 | context.lineWidth = 1; |
| 43 | context.translate(0.5, 0.5); |
| 44 | if (dash) { |
| 45 | context.setLineDash([3, 3]); |
| 46 | } |
| 47 | |
| 48 | if (rulerAtRight) { |
| 49 | for (const y in bounds.rightmostXForY) { |
| 50 | context.beginPath(); |
| 51 | context.moveTo(width, y); |
| 52 | context.lineTo(bounds.rightmostXForY[y], y); |
| 53 | context.stroke(); |
| 54 | } |
| 55 | } else { |
| 56 | for (const y in bounds.leftmostXForY) { |
| 57 | context.beginPath(); |
| 58 | context.moveTo(0, y); |
| 59 | context.lineTo(bounds.leftmostXForY[y], y); |
| 60 | context.stroke(); |
| 61 | } |
| 62 | } |
| 63 | |
Brandon Goddard | 7ceb501 | 2020-07-09 19:36:57 | [diff] [blame] | 64 | if (rulerAtBottom) { |
| 65 | for (const x in bounds.bottommostYForX) { |
| 66 | context.beginPath(); |
| 67 | context.moveTo(x, height); |
| 68 | context.lineTo(x, bounds.topmostYForX[x]); |
| 69 | context.stroke(); |
| 70 | } |
| 71 | } else { |
| 72 | for (const x in bounds.topmostYForX) { |
| 73 | context.beginPath(); |
| 74 | context.moveTo(x, 0); |
| 75 | context.lineTo(x, bounds.topmostYForX[x]); |
| 76 | context.stroke(); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | context.restore(); |
| 81 | } |
| 82 | |
| 83 | export function buildPath(commands, bounds) { |
| 84 | let commandsIndex = 0; |
| 85 | |
| 86 | function extractPoints(count) { |
| 87 | const points = []; |
| 88 | |
| 89 | for (let i = 0; i < count; ++i) { |
| 90 | const x = Math.round(commands[commandsIndex++] * emulationScaleFactor); |
| 91 | bounds.maxX = Math.max(bounds.maxX, x); |
| 92 | bounds.minX = Math.min(bounds.minX, x); |
| 93 | |
| 94 | const y = Math.round(commands[commandsIndex++] * emulationScaleFactor); |
| 95 | bounds.maxY = Math.max(bounds.maxY, y); |
| 96 | bounds.minY = Math.min(bounds.minY, y); |
| 97 | |
| 98 | bounds.leftmostXForY[y] = Math.min(bounds.leftmostXForY[y] || Number.MAX_VALUE, x); |
| 99 | bounds.rightmostXForY[y] = Math.max(bounds.rightmostXForY[y] || Number.MIN_VALUE, x); |
| 100 | bounds.topmostYForX[x] = Math.min(bounds.topmostYForX[x] || Number.MAX_VALUE, y); |
| 101 | bounds.bottommostYForX[x] = Math.max(bounds.bottommostYForX[x] || Number.MIN_VALUE, y); |
Patrick Brosset | 8eafacc | 2020-08-05 08:18:41 | [diff] [blame] | 102 | |
| 103 | bounds.allPoints.push({x, y}); |
| 104 | |
Brandon Goddard | 7ceb501 | 2020-07-09 19:36:57 | [diff] [blame] | 105 | points.push(x, y); |
| 106 | } |
| 107 | return points; |
| 108 | } |
| 109 | |
| 110 | const commandsLength = commands.length; |
| 111 | const path = new Path2D(); |
| 112 | while (commandsIndex < commandsLength) { |
| 113 | switch (commands[commandsIndex++]) { |
| 114 | case 'M': |
| 115 | path.moveTo.apply(path, extractPoints(1)); |
| 116 | break; |
| 117 | case 'L': |
| 118 | path.lineTo.apply(path, extractPoints(1)); |
| 119 | break; |
| 120 | case 'C': |
| 121 | path.bezierCurveTo.apply(path, extractPoints(3)); |
| 122 | break; |
| 123 | case 'Q': |
| 124 | path.quadraticCurveTo.apply(path, extractPoints(2)); |
| 125 | break; |
| 126 | case 'Z': |
| 127 | path.closePath(); |
| 128 | break; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return path; |
| 133 | } |
| 134 | |
| 135 | export function emptyBounds() { |
| 136 | const bounds = { |
| 137 | minX: Number.MAX_VALUE, |
| 138 | minY: Number.MAX_VALUE, |
| 139 | maxX: Number.MIN_VALUE, |
| 140 | maxY: Number.MIN_VALUE, |
| 141 | leftmostXForY: {}, |
| 142 | rightmostXForY: {}, |
| 143 | topmostYForX: {}, |
Patrick Brosset | 8eafacc | 2020-08-05 08:18:41 | [diff] [blame] | 144 | bottommostYForX: {}, |
| 145 | allPoints: [] |
Brandon Goddard | 7ceb501 | 2020-07-09 19:36:57 | [diff] [blame] | 146 | }; |
| 147 | return bounds; |
| 148 | } |