forked from mbk-lab/rui_orig
Bug fixing
This commit is contained in:
parent
21ee77eb26
commit
d3c36e38bd
40
canvas.go
40
canvas.go
|
@ -645,26 +645,34 @@ func (canvas *canvasData) SetFontWithParams(name string, size SizeUnit, params F
|
||||||
}
|
}
|
||||||
|
|
||||||
func (canvas *canvasData) TextWidth(text string, fontName string, fontSize SizeUnit) float64 {
|
func (canvas *canvasData) TextWidth(text string, fontName string, fontSize SizeUnit) float64 {
|
||||||
script := allocStringBuilder()
|
buffer := allocStringBuilder()
|
||||||
defer freeStringBuilder(script)
|
defer freeStringBuilder(buffer)
|
||||||
|
|
||||||
script.WriteString(`const canvas = document.getElementById('`)
|
canvas.setFontWithParams(fontName, fontSize, FontParams{}, buffer)
|
||||||
script.WriteString(canvas.View().htmlID())
|
fontParams := buffer.String()
|
||||||
script.WriteString(`');
|
|
||||||
|
buffer.Reset()
|
||||||
|
canvas.writeStringArgs(text, buffer)
|
||||||
|
str := buffer.String()
|
||||||
|
|
||||||
|
script := fmt.Sprintf(`
|
||||||
|
var w = 0;
|
||||||
|
const canvas = document.getElementById('%s');
|
||||||
|
if (canvas) {
|
||||||
const ctx = canvas.getContext('2d');
|
const ctx = canvas.getContext('2d');
|
||||||
const dpr = window.devicePixelRatio || 1;
|
if (ctx) {
|
||||||
var gradient;
|
|
||||||
var path;
|
|
||||||
ctx.save()
|
ctx.save()
|
||||||
ctx.scale(dpr, dpr);`)
|
const dpr = window.devicePixelRatio || 1;
|
||||||
|
ctx.scale(dpr, dpr);
|
||||||
|
%s;
|
||||||
|
w = ctx.measureText('%s').width;
|
||||||
|
ctx.restore();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sendMessage('answer{width=' + w + ', answerID=' + answerID + '}');
|
||||||
|
`, canvas.View().htmlID(), fontParams, str)
|
||||||
|
|
||||||
canvas.setFontWithParams(fontName, fontSize, FontParams{}, script)
|
result := canvas.View().Session().runGetterScript(script)
|
||||||
script.WriteString("\nvar w = ctx.measureText('")
|
|
||||||
canvas.writeStringArgs(text, script)
|
|
||||||
script.WriteString(`').width;
|
|
||||||
sendMessage('answer{width=' + w + ', answerID=' + answerID + '}')`)
|
|
||||||
|
|
||||||
result := canvas.View().Session().runGetterScript(script.String())
|
|
||||||
switch result.Tag() {
|
switch result.Tag() {
|
||||||
case "answer":
|
case "answer":
|
||||||
if value, ok := result.PropertyValue("width"); ok {
|
if value, ok := result.PropertyValue("width"); ok {
|
||||||
|
|
Loading…
Reference in New Issue