X-Git-Url: https://git.sommitrealweird.co.uk/cssbattle.git/blobdiff_plain/8b446ce74ff54596f0bf4403c9fb11501780a1ed..41532a15fb5b3644714b1cc7028ef04c2d43f87a:/unit_replacement.js?ds=sidebyside diff --git a/unit_replacement.js b/unit_replacement.js index 2f83635..c033c00 100644 --- a/unit_replacement.js +++ b/unit_replacement.js @@ -1,3 +1,16 @@ +/* +Permission to use, copy, modify, and/or distribute this software for +any purpose with or without fee is hereby granted. + + THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL +WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE +FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY +DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN +AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT +OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + // 1ch -> 8px // 1pc -> 12pt -> 1/6in -> 16px // 1vw -> 4px @@ -5,21 +18,29 @@ // 1in -> 96px // 1ex -> 7.16px (useful, right?!) // 1em -> 16px - we'll just use pc for this instead -// 1q -> 100/106 px <-- this is a bad bad number, so, if we've already got q, we probably won't do anything with 'em' +// 1mm -> (25.4/96)px +// 1q -> (4*25.4 / 96)px - as there are 25.4mm in 1in, and q is 1/4 of 1mm pixel_lengths = { "ch": 8, + "pt":(4.0/3.0), "pc": 16, "vw":4, "vh":3, "in": 96, "ex":7.16, "em":16, + "mm":(25.4/96), + "q":(96/101.6), "px":1 }; +do_experimental_q = false; +pixel_diff = 0.25; + function getShortestUnit(length,unit) { cur_string = `${length}${unit}`; + console.log("Checking:",cur_string); pixel_length = 0; if (pixel_lengths.hasOwnProperty(unit)) { pixel_length = length * pixel_lengths[unit]; @@ -30,22 +51,41 @@ function getShortestUnit(length,unit) { for (unit in pixel_lengths) { if (pixel_lengths.hasOwnProperty(unit)) { new_len = pixel_length / pixel_lengths[unit]; + if (unit=="q" && do_experimental_q) { + console.log("Trying q fuzzyness"); + new_len_test = Math.round(new_len); + diff = Math.abs(pixel_length - new_len_test * pixel_lengths[unit]); + console.log("Diff is:", diff, "for",pixel_length+"px (",new_len_test,"q) pixel_diff to compare: ",pixel_diff); + if (diff < pixel_diff) { + new_len = new_len_test; + } + } new_string = `${new_len}${unit}`; if (new_string.length < cur_string.length) { cur_string = new_string; } } } + console.log("Using:",cur_string); return cur_string; } // REQUIRED: A `run` function thats takes in current code and returns processed code. function run(code) { + console.log("Doing a unit replacement run...") + let temp = prompt("When calculating q values, maximum difference in pixel length to take in to account (cancel to not use)","0.5"); + if (temp != null) { + pixel_diff = Math.abs(temp); + console.log("Will attempt q fuzzyness with pixel_diff of",temp); + do_experimental_q = true; + } // `code` is your current code in the editor - var processedCode = code.replace(/(\d+)(vw|vh|pc|px|in|ex|em)/g, (match, $1,$2) => { + var processedCode = code.replace(/(\d+)(ch|pt|pc|vw|vh|in|ex|em|mm|q|px)/g, (match, $1,$2) => { return getShortestUnit($1,$2); }); + + console.log("... Finished Unit Replacement Run"); // Return the final code return processedCode; -} \ No newline at end of file +}