shithub: mycel

Download patch

ref: 35e034e341b0794b327eabb3f25392e3b27dd99b
parent: 762eed4298a43fa920a29e75e08710cbc754ac6f
author: Philip Silva <philip.silva@protonmail.com>
date: Tue Sep 14 15:08:21 EDT 2021

(improved) support for em, ex and mm

--- a/style/stylesheets.go
+++ b/style/stylesheets.go
@@ -622,7 +622,7 @@
 		return calc(cs, l)
 	}
 
-	for _, suffix := range []string{"px", "%", "rem", "em", "vw", "vh"} {
+	for _, suffix := range []string{"px", "%", "rem", "em", "ex", "vw", "vh", "mm"} {
 		if strings.HasSuffix(l, suffix) {
 			if s = strings.TrimSuffix(l, suffix); s != "" {
 				f, err = strconv.ParseFloat(s, 64)
@@ -637,9 +637,16 @@
 
 	switch unit {
 	case "px":
-	case "em", "rem":
-		// TODO: distinguish between em and rem
+	case "rem":
+		// TODO: use font size from root element
 		f *= FontBaseSize
+	case "em", "ex":
+		// TODO: distinguish between em and ex
+		if cs == nil {
+			f *= FontBaseSize
+		} else {
+			f *= cs.FontHeight()
+		}
 	case "vw":
 		f *= float64(WindowWidth) / 100.0
 	case "vh":
@@ -655,6 +662,12 @@
 			log.Printf("%% unit used in root element")
 		}
 		f *= 0.01 * float64(wp)
+	case "mm":
+		dpi := 100
+		if dui != nil && dui.Display != nil && dui.Display.DPI != 0 {
+			dpi = dui.Display.DPI
+		}
+		f *= float64(dpi) / 25.4
 	default:
 		return f, unit, fmt.Errorf("unknown suffix: %v", l)
 	}
--- a/style/stylesheets_test.go
+++ b/style/stylesheets_test.go
@@ -313,9 +313,11 @@
 		"inherit": 0.0,
 		"17px": 17.0,
 		"10em": 110.0,
+		"10ex": 110.0,
 		"10vw": 128.0,
 		"10vh": 108.0,
 		"10%": 0,
+		"101.6mm": 400,
 	}
 	for l, px := range lpx {
 		f, _, err := length(nil, l)