Add support for pixel based font sizes (XF 2)
authorTim Düsterhus <tim@bastelstu.be>
Thu, 6 Feb 2020 12:08:22 +0000 (13:08 +0100)
committerTim Düsterhus <tim@bastelstu.be>
Thu, 5 Mar 2020 16:40:33 +0000 (17:40 +0100)
files/lib/system/exporter/XF2xExporter.class.php

index bf73570a8d69b499db855e733fa59c018d752725..327a59010dd679b8d8e5b9fd7eb05325f2833934 100644 (file)
@@ -1724,28 +1724,28 @@ class XF2xExporter extends AbstractExporter {
                $message = $quoteRegex->replace($message, $quoteCallback);
                
                // fix size bbcodes
-               $message = preg_replace_callback('/\[size=\'?(\d+)\'?\]/i', function ($matches) {
-                       $size = 36;
+               $message = preg_replace_callback('/\[size=\'?(\d+)(px)?\'?\]/i', function ($matches) {
+                       $unit = 'scalar';
+                       if (!empty($matches[2])) $unit = $matches[2];
                        
-                       switch ($matches[1]) {
-                               case 1:
-                                       $size = 8;
-                                       break;
-                               case 2:
-                                       $size = 10;
-                                       break;
-                               case 3:
-                                       $size = 12;
-                                       break;
-                               case 4:
-                                       $size = 14;
-                                       break;
-                               case 5:
-                                       $size = 18;
-                                       break;
-                               case 6:
-                                       $size = 24;
-                                       break;
+                       $validSizes = [8, 10, 12, 14, 18, 24, 36];
+                       $size = 36;
+                       switch ($unit) {
+                               case 'px':
+                                       foreach ($validSizes as $pt) {
+                                               // 1 Point equals roughly 4/3 Pixels
+                                               if ($pt >= ($matches[1] / 4 * 3)) {
+                                                       $size = $pt;
+                                                       break;
+                                               }
+                                       }
+                               break;
+                               case 'scalar':
+                               default:
+                                       if ($matches[1] >= 1 && $matches[1] <= 6) {
+                                               $size = $validSizes[$matches[1] - 1];
+                                       }
+                               break;
                        }
                        
                        return '[size='.$size.']';