shithub: mycel

Download patch

ref: 7e48ef89b6b053c7566d90312ae7f758a5fba147
parent: 905263542c6f19b3d66f94e323995af7880c9355
author: Philip Silva <philip.silva@protonmail.com>
date: Sat Apr 3 06:00:39 EDT 2021

Font handling, remove placeholders, style attribute parsing

- check which font sizes are actually available (unix)

--- a/browser/browser.go
+++ b/browser/browser.go
@@ -820,8 +820,6 @@
 		}
 
 		return &Box{
-			Padding: duit.SpaceXY(6, 4),
-			Margin:  duit.SpaceXY(6, 4),
 			Kids:    duit.NewKids(finalUis...),
 		}
 	} else {
--- a/style/fonts_plan9.go
+++ b/style/fonts_plan9.go
@@ -9,15 +9,6 @@
 
 func initFontserver() {}
 
-func matchClosestFontSize(desired float64, available []int) (closest int) {
-	for _, a := range available {
-		if closest == 0 || math.Abs(float64(a)-desired) < math.Abs(float64(closest)-desired) {
-			closest = a
-		}
-	}
-	return
-}
-
 func (cs Map) FontFilename() string {
 	fontSize := matchClosestFontSize(cs.FontSize(), []int{5,6,7,8,9,10,12,14,16,18,20,24,28,32})
 	return fmt.Sprintf("/lib/font/bit/lucida/unicode.%v.font", fontSize)
--- a/style/fonts_unix.go
+++ b/style/fonts_unix.go
@@ -4,11 +4,14 @@
 
 import (
 	"fmt"
-	"math"
 	"os/exec"
+	"regexp"
+	"strconv"
 	"strings"
 )
 
+var availableFontSizes = make(map[string][]int)
+
 func initFontserver() {
 	buf, err := exec.Command("fontsrv", "-p", ".").Output()
 	if err == nil {
@@ -18,9 +21,40 @@
 	}
 }
 
+func fontSizes(fontName string) (fss []int, err error) {
+	re := regexp.MustCompile(`^(\d+)$`)
+	fss = make([]int, 0, 20)
+
+	buf, err := exec.Command("fontsrv", "-p", fontName).Output()
+	if err != nil {
+		return
+	}
+	for _, s := range strings.Split(string(buf), "\n") {
+		s = strings.TrimSpace(s)
+		s = strings.TrimSuffix(s, "/")
+		if !re.MatchString(s) {
+			continue
+		}
+		fs, err := strconv.Atoi(s)
+		if err != nil {
+			log.Errorf("%v: %v", fs, err)
+		}
+		fss = append(fss, fs)
+	}
+
+	return
+}
+
 func (cs Map) FontFilename() string {
-	pref := cs.preferedFontName([]string{"HelveticaNeue", "Helvetica"})
-	fontSize := 2 * /*dui.Scale(*/int(math.RoundToEven(cs.FontSize()))/*)*/
+	f := cs.preferedFontName([]string{"HelveticaNeue", "Helvetica"})
+	if _, ok := availableFontSizes[f]; !ok {
+		fss, err := fontSizes(f)
+		if err != nil {
+			log.Errorf("font sizes %v: %v", f, err)
+		}
+		availableFontSizes[f] = fss
+	}
+	s := matchClosestFontSize(2*cs.FontSize(), availableFontSizes[f])
 
-	return fmt.Sprintf("/mnt/font/"+pref+"%va/font", fontSize)
-}
\ No newline at end of file
+	return fmt.Sprintf("/mnt/font/"+f+"%va/font", s)
+}
--- a/style/stylesheets.go
+++ b/style/stylesheets.go
@@ -10,6 +10,7 @@
 	"golang.org/x/image/colornames"
 	"golang.org/x/net/html"
 	"github.com/psilva261/opossum/logger"
+	"math"
 	"regexp"
 	"strconv"
 	"strings"
@@ -210,7 +211,11 @@
 
 	for _, a := range n.Attr {
 		if a.Key == "style" {
-			decls, err := parser.ParseDeclarations(a.Val)
+			v := strings.TrimSpace(a.Val)
+			if !strings.HasSuffix(v, ";") {
+				v += ";"
+			}
+			decls, err := parser.ParseDeclarations(v)
 
 			if err != nil {
 				log.Printf("could not parse '%v'", a.Val)
@@ -302,6 +307,15 @@
 	}
 
 	return avails[0]
+}
+
+func matchClosestFontSize(desired float64, available []int) (closest int) {
+	for _, a := range available {
+		if closest == 0 || math.Abs(float64(a)-desired) < math.Abs(float64(closest)-desired) {
+			closest = a
+		}
+	}
+	return
 }
 
 func (cs Map) FontSize() float64 {
--- a/style/stylesheets_test.go
+++ b/style/stylesheets_test.go
@@ -10,8 +10,8 @@
 )
 
 func init() {
-	quiet := true
-	logger.Quiet = &quiet
+	f := false
+	logger.Quiet = &f
 	logger.Init()
 	log = &logger.Logger{Debug: true}
 }
@@ -183,6 +183,42 @@
 		t.Fail()
 	}
 	t.Logf("m=%+v", m)
+}
+
+func TestNewMapStyle(t *testing.T) {
+	htms := []string{
+		`<h2 style="color: green;">a header</h2>`,
+		`<h2 style="color: green">a header</h2>`,
+	}
+	for _, htm := range htms {
+		doc, err := html.Parse(strings.NewReader(htm))
+		if err != nil {
+			t.Fail()
+		}
+
+		h2 := grep(doc, "h2")
+		m := NewMap(h2)
+
+		if m.Declarations["color"].Value != "green" {
+			t.Errorf("%+v", m)
+		}
+	}
+}
+
+func grep(nn *html.Node, tag string) *html.Node {
+	var f func(n *html.Node) *html.Node
+	f = func(n *html.Node) *html.Node {
+		if n.Data == tag {
+			return n
+		}
+		for c := n.FirstChild; c != nil; c = c.NextSibling {
+			if m := f(c); m != nil {
+				return m
+			}
+		}
+		return nil
+	}
+	return f(nn)
 }
 
 func TestSmaller(t *testing.T) {