Open WillianBR opened 1 year ago
you need to translate to the centre of the rotation
(below works, i think, but has changes other than just bug fix.)
package main
import (
"flag"
//"fmt"
"github.com/fogleman/gg"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font/gofont/goregular"
)
func main() {
xPtr := flag.Int("x", 250, "Valor de X")
yPtr := flag.Int("y", 250, "Valor de Y")
rotatePtr := flag.Int("rotate", -45, "Angulo de rotação em graus")
fontSizePtr := flag.Int("fontSize", 32, "Tamanho da fonte")
sizePtr := flag.Int("size", 500, "Tamanho do Canvas (size x size)")
txtPtr := flag.String("text", "Willian & Simone", "Texto para imprimir")
imageFilePtr := flag.String("file", "rotated-text.go.png", "Arquivo para salvar a imagem")
flag.Parse()
dc := gg.NewContext(*sizePtr, *sizePtr)
dc.SetRGB(1, 1, 1)
dc.Clear()
font, err := truetype.Parse(goregular.TTF)
if err != nil {
panic("truetype.Parse()")
}
face := truetype.NewFace(font, &truetype.Options{
Size: float64(*fontSizePtr),
})
dc.SetFontFace(face)
dc.SetRGB(0, 0, 0)
// border inset 10px
dc.DrawRectangle(10, 10, float64(*sizePtr)-20, float64(*sizePtr)-20)
dc.Stroke()
dc.Translate(float64(*xPtr), float64(*yPtr))
dc.Rotate(gg.Radians(float64(*rotatePtr))) // Left to Right and Up
// does its own centring
dc.DrawStringAnchored(*txtPtr, 0, 0,.5,.5)
w, h := dc.MeasureString(*txtPtr)
// added border
dc.DrawRectangle(-w/2-10,-h/2, w+20, h+10)
dc.Stroke()
dc.SavePNG(*imageFilePtr)
}
also use alpha color...
dc.SetRGBA(1, 1, 1,100)
... for a transparent watermark
I'll check it later, after I get home!
Thank you!
Hi @splace,
I tested it and work!
I looks like a missed the detail in the documentation about the function DrawStringAnchored()
.
I started some test and now I'll look for a function group to save the canvas status (colors, etc.) and restore it after.
And ofcourse, add the code to read a existing image and apply the watermark at it center without compromised the original image & text.
Watermark with "fogleman/gg"
I stumbled into "fogleman/gg" yesterday and immediately seen it as a nice way to add watermark into my images. But I couldn't make ir work.
I started using "fogleman/gg/blob/master/examples/rotated-text.go" as boilerplate, but it seeans the dc.Rotate() do it with all the canvas and I couldn't center the text!
If course the idea is draw a diagonal text over the hypotenuse of the canvas. Exactly into the center.
When I draw the texto with 0 degrees rotarion it is on center. But if I only rotate 45 degrees, it's a mess.
Does anybody can help with that?
The intended steps are:
Get the image bounds and compute hypotenuse, and fit the most large text possible without get clipped out of canvas;
Create a canvas with transparent backgroud and draw the rotate (45º) text;
Save the new picture with a another name;
The step 2 is the tricky one at the moment!
@fogleman ?
Canvas rotated at 45 degress:
Canvas at 0 degress:
Draft souce code: