Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
michael@0 | 1 | /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | // The possible values of the "align" component of preserveAspectRatio. |
michael@0 | 7 | const ALIGN_VALS = ["none", |
michael@0 | 8 | "xMinYMin", "xMinYMid", "xMinYMax", |
michael@0 | 9 | "xMidYMin", "xMidYMid", "xMidYMax", |
michael@0 | 10 | "xMaxYMin", "xMaxYMid", "xMaxYMax"]; |
michael@0 | 11 | |
michael@0 | 12 | // The possible values of the "meetOrSlice" component of preserveAspectRatio. |
michael@0 | 13 | const MEETORSLICE_VALS = [ "meet", "slice" ]; |
michael@0 | 14 | |
michael@0 | 15 | const SVGNS = "http://www.w3.org/2000/svg"; |
michael@0 | 16 | const XLINKNS = "http://www.w3.org/1999/xlink"; |
michael@0 | 17 | |
michael@0 | 18 | // This is the separation between the x & y values of each <image> in a |
michael@0 | 19 | // generated grid. |
michael@0 | 20 | const IMAGE_OFFSET = 50; |
michael@0 | 21 | |
michael@0 | 22 | function generateBorderRect(aX, aY, aWidth, aHeight) { |
michael@0 | 23 | var rect = document.createElementNS(SVGNS, "rect"); |
michael@0 | 24 | rect.setAttribute("x", aX); |
michael@0 | 25 | rect.setAttribute("y", aY); |
michael@0 | 26 | rect.setAttribute("width", aWidth); |
michael@0 | 27 | rect.setAttribute("height", aHeight); |
michael@0 | 28 | rect.setAttribute("fill", "none"); |
michael@0 | 29 | rect.setAttribute("stroke", "black"); |
michael@0 | 30 | rect.setAttribute("stroke-width", "2"); |
michael@0 | 31 | rect.setAttribute("stroke-dasharray", "3 2"); |
michael@0 | 32 | return rect; |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | // Returns an SVG <image> element with the given xlink:href, width, height, |
michael@0 | 36 | // and preserveAspectRatio=[aAlign aMeetOrSlice] attributes |
michael@0 | 37 | function generateImageElementForParams(aX, aY, aWidth, aHeight, |
michael@0 | 38 | aHref, aAlign, aMeetOrSlice) { |
michael@0 | 39 | var image = document.createElementNS(SVGNS, "image"); |
michael@0 | 40 | image.setAttribute("x", aX); |
michael@0 | 41 | image.setAttribute("y", aY); |
michael@0 | 42 | image.setAttribute("width", aWidth); |
michael@0 | 43 | image.setAttribute("height", aHeight); |
michael@0 | 44 | image.setAttributeNS(XLINKNS, "href", aHref); |
michael@0 | 45 | image.setAttribute("preserveAspectRatio", aAlign + " " + aMeetOrSlice); |
michael@0 | 46 | return image; |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | // Returns a <g> element filled with a grid of <image> elements which each |
michael@0 | 50 | // have the specified aWidth & aHeight and which use all possible values of |
michael@0 | 51 | // preserveAspectRatio. |
michael@0 | 52 | // |
michael@0 | 53 | // The final "aBonusPARVal" argument (if specified) is used as the |
michael@0 | 54 | // preserveAspectRatio value on a bonus <image> element, added at the end. |
michael@0 | 55 | function generateImageGrid(aHref, aWidth, aHeight, aBonusPARVal) { |
michael@0 | 56 | var grid = document.createElementNS(SVGNS, "g"); |
michael@0 | 57 | var y = 0; |
michael@0 | 58 | var x = 0; |
michael@0 | 59 | for (var i = 0; i < ALIGN_VALS.length; i++) { |
michael@0 | 60 | // Jump to next line of grid, for every other "i" value. |
michael@0 | 61 | // (every fourth entry) |
michael@0 | 62 | if (i && i % 2 == 0) { |
michael@0 | 63 | y += IMAGE_OFFSET; |
michael@0 | 64 | x = 0; |
michael@0 | 65 | } |
michael@0 | 66 | var alignVal = ALIGN_VALS[i]; |
michael@0 | 67 | for (var j = 0; j < MEETORSLICE_VALS.length; j++) { |
michael@0 | 68 | var meetorsliceVal = MEETORSLICE_VALS[j]; |
michael@0 | 69 | var border = generateBorderRect(x, y, aWidth, aHeight); |
michael@0 | 70 | var image = generateImageElementForParams(x, y, aWidth, aHeight, |
michael@0 | 71 | aHref, alignVal, |
michael@0 | 72 | meetorsliceVal); |
michael@0 | 73 | grid.appendChild(border); |
michael@0 | 74 | grid.appendChild(image); |
michael@0 | 75 | x += IMAGE_OFFSET; |
michael@0 | 76 | } |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | if (aBonusPARVal) { |
michael@0 | 80 | // Add one final entry with "bonus" pAR value. |
michael@0 | 81 | y += IMAGE_OFFSET; |
michael@0 | 82 | x = 0; |
michael@0 | 83 | var border = generateBorderRect(x, y, aWidth, aHeight); |
michael@0 | 84 | var image = generateImageElementForParams(x, y, aWidth, aHeight, |
michael@0 | 85 | aHref, aBonusPARVal, ""); |
michael@0 | 86 | grid.appendChild(border); |
michael@0 | 87 | grid.appendChild(image); |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | return grid; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | // Returns an SVG <symbol> element that... |
michael@0 | 94 | // (a) has the given ID |
michael@0 | 95 | // (b) contains only a <use> element to the given URI |
michael@0 | 96 | // (c) has a hardcoded viewBox="0 0 10 10" attribute |
michael@0 | 97 | // (d) has the given preserveAspectRatio=[aAlign aMeetOrSlice] attribute |
michael@0 | 98 | function generateSymbolElementForParams(aSymbolID, aHref, |
michael@0 | 99 | aAlign, aMeetOrSlice) { |
michael@0 | 100 | var use = document.createElementNS(SVGNS, "use"); |
michael@0 | 101 | use.setAttributeNS(XLINKNS, "href", aHref); |
michael@0 | 102 | |
michael@0 | 103 | var symbol = document.createElementNS(SVGNS, "symbol"); |
michael@0 | 104 | symbol.setAttribute("id", aSymbolID); |
michael@0 | 105 | symbol.setAttribute("viewBox", "0 0 10 10"); |
michael@0 | 106 | symbol.setAttribute("preserveAspectRatio", aAlign + " " + aMeetOrSlice); |
michael@0 | 107 | |
michael@0 | 108 | symbol.appendChild(use); |
michael@0 | 109 | return symbol; |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | function generateUseElementForParams(aTargetURI, aX, aY, aWidth, aHeight) { |
michael@0 | 113 | var use = document.createElementNS(SVGNS, "use"); |
michael@0 | 114 | use.setAttributeNS(XLINKNS, "href", aTargetURI); |
michael@0 | 115 | use.setAttribute("x", aX); |
michael@0 | 116 | use.setAttribute("y", aY); |
michael@0 | 117 | use.setAttribute("width", aWidth); |
michael@0 | 118 | use.setAttribute("height", aHeight); |
michael@0 | 119 | return use; |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | // Returns a <g> element filled with a grid of <use> elements which each |
michael@0 | 123 | // have the specified aWidth & aHeight and which reference <symbol> elements |
michael@0 | 124 | // with all possible values of preserveAspectRatio. Each <symbol> contains |
michael@0 | 125 | // a <use> that links to the given URI, aHref. |
michael@0 | 126 | // |
michael@0 | 127 | // The final "aBonusPARVal" argument (if specified) is used as the |
michael@0 | 128 | // preserveAspectRatio value on a bonus <symbol> element, added at the end. |
michael@0 | 129 | function generateSymbolGrid(aHref, aWidth, aHeight, aBonusPARVal) { |
michael@0 | 130 | var grid = document.createElementNS(SVGNS, "g"); |
michael@0 | 131 | var y = 0; |
michael@0 | 132 | var x = 0; |
michael@0 | 133 | for (var i = 0; i < ALIGN_VALS.length; i++) { |
michael@0 | 134 | // Jump to next line of grid, for every other "i" value. |
michael@0 | 135 | // (every fourth entry) |
michael@0 | 136 | if (i && i % 2 == 0) { |
michael@0 | 137 | y += IMAGE_OFFSET; |
michael@0 | 138 | x = 0; |
michael@0 | 139 | } |
michael@0 | 140 | var alignVal = ALIGN_VALS[i]; |
michael@0 | 141 | for (var j = 0; j < MEETORSLICE_VALS.length; j++) { |
michael@0 | 142 | var meetorsliceVal = MEETORSLICE_VALS[j]; |
michael@0 | 143 | var border = generateBorderRect(x, y, aWidth, aHeight); |
michael@0 | 144 | |
michael@0 | 145 | var symbolID = "symbol_" + alignVal + "_" + meetorsliceVal; |
michael@0 | 146 | var symbol = generateSymbolElementForParams(symbolID, aHref, |
michael@0 | 147 | alignVal, meetorsliceVal); |
michael@0 | 148 | var use = generateUseElementForParams("#" + symbolID, |
michael@0 | 149 | x, y, aWidth, aHeight); |
michael@0 | 150 | grid.appendChild(symbol); // This isn't painted |
michael@0 | 151 | grid.appendChild(border); |
michael@0 | 152 | grid.appendChild(use); |
michael@0 | 153 | x += IMAGE_OFFSET; |
michael@0 | 154 | } |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | if (aBonusPARVal) { |
michael@0 | 158 | // Add one final entry with "bonus" pAR value. |
michael@0 | 159 | y += IMAGE_OFFSET; |
michael@0 | 160 | x = 0; |
michael@0 | 161 | var border = generateBorderRect(x, y, aWidth, aHeight); |
michael@0 | 162 | var symbolID = "symbol_Bonus"; |
michael@0 | 163 | var symbol = generateSymbolElementForParams(symbolID, aHref, |
michael@0 | 164 | aBonusPARVal, ""); |
michael@0 | 165 | var use = generateUseElementForParams("#" + symbolID, |
michael@0 | 166 | x, y, aWidth, aHeight); |
michael@0 | 167 | grid.appendChild(symbol); // This isn't painted |
michael@0 | 168 | grid.appendChild(border); |
michael@0 | 169 | grid.appendChild(use); |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | return grid; |
michael@0 | 173 | } |