// 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];
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;