2022-11-28 11:46:21AM
This commit is contained in:
parent
33b7fbaa9e
commit
922d9c0ca7
2 changed files with 9 additions and 5 deletions
|
|
@ -13,6 +13,8 @@ import (
|
||||||
v8 "rogchap.com/v8go"
|
v8 "rogchap.com/v8go"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var pxPerEx = 8
|
||||||
|
|
||||||
//go:embed polyfills.js
|
//go:embed polyfills.js
|
||||||
var polyfillsJS string
|
var polyfillsJS string
|
||||||
|
|
||||||
|
|
@ -22,6 +24,8 @@ var setupJS string
|
||||||
//go:embed mathjax.js
|
//go:embed mathjax.js
|
||||||
var mathjaxJS string
|
var mathjaxJS string
|
||||||
|
|
||||||
|
// Matches this
|
||||||
|
// <svg style="background: white;" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="563" height="326" viewBox="-100 -100 563 326"><style type="text/css">
|
||||||
var svgRe = regexp.MustCompile(`<svg[^>]+width="([0-9\.]+)ex" height="([0-9\.]+)ex"[^>]+>`)
|
var svgRe = regexp.MustCompile(`<svg[^>]+width="([0-9\.]+)ex" height="([0-9\.]+)ex"[^>]+>`)
|
||||||
|
|
||||||
func Render(s string) (_ string, err error) {
|
func Render(s string) (_ string, err error) {
|
||||||
|
|
@ -41,9 +45,9 @@ func Render(s string) (_ string, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
val, err := v8ctx.RunScript(fmt.Sprintf(`adaptor.innerHTML(html.convert("%s", {
|
val, err := v8ctx.RunScript(fmt.Sprintf(`adaptor.innerHTML(html.convert("%s", {
|
||||||
em: 16,
|
em: %d,
|
||||||
ex: 8,
|
ex: %d,
|
||||||
}))`, s), "value.js")
|
}))`, s, pxPerEx*2, pxPerEx), "value.js")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
@ -75,5 +79,5 @@ func Measure(s string) (width, height int, err error) {
|
||||||
return 0, 0, fmt.Errorf("svg parsing failed for latex: %v", svg)
|
return 0, 0, fmt.Errorf("svg parsing failed for latex: %v", svg)
|
||||||
}
|
}
|
||||||
|
|
||||||
return int(math.Ceil(wf * 8)), int(math.Ceil(hf * 8)), nil
|
return int(math.Ceil(wf * float64(pxPerEx))), int(math.Ceil(hf * float64(pxPerEx))), nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,6 @@ func TestRender(t *testing.T) {
|
||||||
func TestRenderError(t *testing.T) {
|
func TestRenderError(t *testing.T) {
|
||||||
_, err := Render(`\frac{1}{2}`)
|
_, err := Render(`\frac{1}{2}`)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal(err)
|
t.Fatal("expected to error on invalid latex syntax")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue