2 Permission to use, copy, modify, and/or distribute this software for
3 any purpose with or without fee is hereby granted.
5 THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL
6 WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
7 OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE
8 FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
9 DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
10 AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
11 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 // 1pc -> 12pt -> 1/6in -> 16px
19 // 1ex -> 7.16px (useful, right?!)
20 // 1em -> 16px - we'll just use pc for this instead
22 // 1q -> (4*25.4 / 96)px - as there are 25.4mm in 1in, and q is 1/4 of 1mm
38 do_experimental_q = false;
41 function getShortestUnit(length,unit) {
42 cur_string = `${length}${unit}`;
43 console.log("Checking:",cur_string);
45 if (pixel_lengths.hasOwnProperty(unit)) {
46 pixel_length = length * pixel_lengths[unit];
51 for (unit in pixel_lengths) {
52 if (pixel_lengths.hasOwnProperty(unit)) {
53 new_len = pixel_length / pixel_lengths[unit];
54 if (unit=="q" && do_experimental_q) {
55 console.log("Trying q fuzzyness");
56 new_len_test = Math.round(new_len);
57 diff = Math.abs(pixel_length - new_len_test * pixel_lengths[unit]);
58 console.log("Diff is:", diff, "for",pixel_length+"px (",new_len_test,"q) pixel_diff to compare: ",pixel_diff);
59 if (diff < pixel_diff) {
60 new_len = new_len_test;
63 new_string = `${new_len}${unit}`;
64 if (new_string.length < cur_string.length) {
65 cur_string = new_string;
69 console.log("Using:",cur_string);
73 // REQUIRED: A `run` function thats takes in current code and returns processed code.
75 console.log("Doing a unit replacement run...")
76 let temp = prompt("When calculating q values, maximum difference in pixel length to take in to account (cancel to not use)","0.5");
78 pixel_diff = Math.abs(temp);
79 console.log("Will attempt q fuzzyness with pixel_diff of",temp);
80 do_experimental_q = true;
82 // `code` is your current code in the editor
83 var processedCode = code.replace(/(\d+)(ch|pt|pc|vw|vh|in|ex|em|mm|q|px)/g, (match, $1,$2) => {
84 return getShortestUnit($1,$2);
87 console.log("... Finished Unit Replacement Run");
89 // Return the final code