]> git.sommitrealweird.co.uk Git - cssbattle.git/blobdiff - unit_replacement.js
Add q unit fuzzy matching for unit replacement
[cssbattle.git] / unit_replacement.js
index 2f8363586dcae75c8ead3d88f7475cb230bd9786..7df05ae20271c71bee51cceab77b99c2aa0c4d56 100644 (file)
@@ -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
 // 1ch -> 8px
 // 1pc -> 12pt -> 1/6in -> 16px
 // 1vw -> 4px
 // 1in -> 96px
 // 1ex -> 7.16px (useful, right?!)
 // 1em -> 16px - we'll just use pc for this instead
 // 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,
 
 pixel_lengths = {
   "ch": 8,
+  "pt":(4.0/3.0),
   "pc": 16,
   "vw":4,
   "vh":3,
   "in": 96,
   "ex":7.16,
   "em":16,
   "pc": 16,
   "vw":4,
   "vh":3,
   "in": 96,
   "ex":7.16,
   "em":16,
+  "mm":(25.4/96),
+  "q":(96/101.6),
   "px":1
 };
 
   "px":1
 };
 
+do_experimental_q = false;
+pixel_diff = 0.5;
+
 function getShortestUnit(length,unit) {
   cur_string = `${length}${unit}`;
 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];
   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];
     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;
         }
       }
     }
         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) {
   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
   // `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);
   });
    return getShortestUnit($1,$2);
   });
+
+  console.log("... Finished Unit Replacement Run");
                                    
   // Return the final code
   return processedCode;
                                    
   // Return the final code
   return processedCode;
-}
\ No newline at end of file
+}