Last active
April 25, 2023 09:21
-
-
Save ernierasta/dd5a497fbb8a71484ef61381dbb115c7 to your computer and use it in GitHub Desktop.
golang github.com/tdewolff/canvas example of text overflowing TextBox
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"github.com/tdewolff/canvas" | |
"github.com/tdewolff/canvas/renderers" | |
) | |
func main() { | |
w := 20.0 | |
h := 10.0 | |
s := "Uncopyrightable" | |
canvW := w + 20 | |
canvH := h + 10 | |
c := canvas.New(canvW, canvH) | |
ctx := canvas.NewContext(c) | |
// background | |
ctx.SetFillColor(canvas.White) | |
ctx.DrawPath(0, 0, canvas.Rectangle(canvW, canvH)) | |
// rectangle | |
ctx.SetFillColor(canvas.Salmon) | |
ctx.SetStrokeColor(canvas.Pink) | |
ctx.SetStrokeWidth(0.3) | |
r := canvas.Rectangle(w, h) | |
ctx.DrawPath(5, 5, r) | |
// text | |
t := text(w, h, s) | |
ctx.DrawText(5, 5+h, t) | |
renderers.Write("output.svg", c) | |
} | |
func text(w, h float64, s string) *canvas.Text { | |
return canvas.NewTextBox(LoadArial(), s, w, h, canvas.Center, canvas.Center, 0.0, 0.0) | |
} | |
// LoadArial will load Arial font. If not present, alternative match will be loaded. | |
// So this should work on all OSes. | |
func LoadArial() *canvas.FontFace { | |
arial := canvas.NewFontFamily("arial") | |
if err := arial.LoadLocalFont("arial", canvas.FontRegular); err != nil { | |
log.Panic("error loading Arial (or alternative) font,", err) | |
} | |
return arial.Face(12) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment