]> git.sommitrealweird.co.uk Git - cssbattle.git/blob - unit_replacement.js
7df05ae20271c71bee51cceab77b99c2aa0c4d56
[cssbattle.git] / unit_replacement.js
1 /*
2 Permission to use, copy, modify, and/or distribute this software for
3 any purpose with or without fee is hereby granted.
4
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.
12 */
13
14 // 1ch -> 8px
15 // 1pc -> 12pt -> 1/6in -> 16px
16 // 1vw -> 4px
17 // 1vh -> 3px
18 // 1in -> 96px
19 // 1ex -> 7.16px (useful, right?!)
20 // 1em -> 16px - we'll just use pc for this instead
21 // 1mm -> (25.4/96)px
22 // 1q -> (4*25.4 / 96)px - as there are 25.4mm in 1in, and q is 1/4 of 1mm
23
24 pixel_lengths = {
25   "ch": 8,
26   "pt":(4.0/3.0),
27   "pc": 16,
28   "vw":4,
29   "vh":3,
30   "in": 96,
31   "ex":7.16,
32   "em":16,
33   "mm":(25.4/96),
34   "q":(96/101.6),
35   "px":1
36 };
37
38 do_experimental_q = false;
39 pixel_diff = 0.5;
40
41 function getShortestUnit(length,unit) {
42   cur_string = `${length}${unit}`;
43   console.log("Checking:",cur_string);
44   pixel_length = 0;
45     if (pixel_lengths.hasOwnProperty(unit)) {
46       pixel_length = length * pixel_lengths[unit];
47     }
48     else {
49       return cur_string;
50     }
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;
61           }
62         }
63         new_string = `${new_len}${unit}`;
64         if (new_string.length < cur_string.length) {
65           cur_string = new_string;
66         }
67       }
68     }
69   console.log("Using:",cur_string);
70   return cur_string;
71 }
72
73 // REQUIRED: A `run` function thats takes in current code and returns processed code.
74 function run(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");
77   if (temp != null) {
78       pixel_diff = Math.abs(temp);
79       console.log("Will attempt q fuzzyness with pixel_diff of",temp);
80       do_experimental_q = true;
81   }
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);
85   });
86
87   console.log("... Finished Unit Replacement Run");
88                                    
89   // Return the final code
90   return processedCode;
91 }