diff --git a/imagick.go b/imagick.go index a70d1a9..6d4fb74 100644 --- a/imagick.go +++ b/imagick.go @@ -3,8 +3,8 @@ package main import ( "bytes" + "fmt" "image" - "image/color" "log" "math" "os/exec" @@ -79,6 +79,10 @@ func (cl *cell) getImage(width, height int) (img *image.RGBA) { } mw = resizeImage(mw, width, height, true, true) + mw1 := cl.imgtext(width, height) + mw.CompositeImage(mw1, imagick.COMPOSITE_OP_OVER, 0, 0) + mw1.Destroy() + img = image.NewRGBA(image.Rect(0, 0, int(width), int(height))) if img.Stride != img.Rect.Size().X*4 { panic("unsupported stride") @@ -91,9 +95,9 @@ func (cl *cell) getImage(width, height int) (img *image.RGBA) { } // adding text to image copied from example -func (cl *cell) imgtext(x, y int) { +func (cl *cell) imgtext(width, height int) *imagick.MagickWand { mw := imagick.NewMagickWand() - defer mw.Destroy() + //defer mw.Destroy() dw := imagick.NewDrawingWand() defer dw.Destroy() pw := imagick.NewPixelWand() @@ -101,30 +105,35 @@ func (cl *cell) imgtext(x, y int) { pw.SetColor("none") // Create a new transparent image - mw.NewImage(0, 0, pw) + mw.NewImage(uint(width), uint(height), pw) // Set up a 72 point white font - pw.SetColor(font.textColor) + r, g, b, _ := cl.font.color.RGBA() + pw.SetColor(fmt.Sprintf("rgb(%d,%d,%d)", r, g, b)) dw.SetFillColor(pw) - dw.SetFont(font.name) - dw.SetFontSize(font.size) + dw.SetFont(cl.font.name) + dw.SetFontSize(cl.font.size) + otlne := "none" // Add a black outline to the text - pw.SetColor(font.OutlineColor) + r, g, b, _ = cl.font.outlineColor.RGBA() + if cl.font.outline { + otlne = fmt.Sprintf("rgb(%d,%d,%d)", r, g, b) + } + + pw.SetColor(otlne) dw.SetStrokeColor(pw) + dw.SetStrokeWidth(cl.font.outlineSize) // Turn antialias on - not sure this makes a difference - dw.SetTextAntialias(true) + //dw.SetTextAntialias(true) // Now draw the text - dw.Annotation(25, 65, cl.text) + dw.Annotation(cl.font.x, cl.font.y, cl.text) // Draw the image on to the mw mw.DrawImage(dw) - // Trim the image down to include only the text - mw.TrimImage(0) - // equivalent to the command line +repage mw.ResetImagePage("") @@ -141,6 +150,7 @@ func (cl *cell) imgtext(x, y int) { // Composite the text on top of the shadow mw.CompositeImage(cw, imagick.COMPOSITE_OP_OVER, 5, 5) cw.Destroy() + return mw } func findfonts() { diff --git a/main.go b/main.go index 357327f..6b7f9fe 100644 --- a/main.go +++ b/main.go @@ -23,11 +23,11 @@ type cell struct { qmlcell qml.Object index int font struct { - name string - size int - color color.RGBA - outline float32 - outlineColor color.RGBA + name string + outlineSize, size, x, y float64 + color color.RGBA + outlineColor color.RGBA + outline Bool } } type slide []*cell @@ -52,6 +52,7 @@ func main() { func run() error { imagick.Initialize() + findfonts() engine = qml.NewEngine() engine.AddImageProvider("images", imgProvider) @@ -104,6 +105,7 @@ func run() error { //Adds a new cell func (sl *slide) add( /*cl *cell*/ ) { var cl cell + cl.Init() //gets the length so that the index is valid cl.index = len(*sl) @@ -114,19 +116,11 @@ func (sl *slide) add( /*cl *cell*/ ) { cl.qmlcell.Set("parent", window.ObjectByName("data1")) cl.qmlcell.Set("index", cl.index) - //load image - cl.img = imagick.NewMagickWand() - cl.img.ReadImage("logo:") - - //give QML the text - cl.qmlcell.ObjectByName("cellText").Set("text", cl.text) - //keep the pointer/dereference (i'm not sure which it is) //problems occur otherwise *sl = append(*sl, &cl) //seperate image object in QML - cl.qmlimg = qimg.Create(engine.Context()) cl.qmlimg.Set("objectName", fmt.Sprintf("cellImg%d", cl.index)) cl.qmlimg.Set("source", fmt.Sprintf("image://images/%d"+`;`+"0", cl.index)) cl.qmlimg.Set("parent", window.ObjectByName("data2")) @@ -135,6 +129,27 @@ func (sl *slide) add( /*cl *cell*/ ) { } +func (cl *cell) Init() { + cl.text = "Testing 1... 2... 3... :-P" + cl.index = -1 + cl.font.color, cl.font.outlineColor = color.RGBA{0, 0, 0, 1}, color.RGBA{1, 1, 1, 1} + cl.font.name = fontlst[1] + cl.font.outline = false + cl.font.outlineSize = 1 + cl.font.size = 35 + cl.font.x, cl.font.y = 10, 30 + + cl.qmlcell = cellQml.Create(engine.Context()) + cl.qmlimg = qimg.Create(engine.Context()) + + //load image + cl.img = imagick.NewMagickWand() + cl.img.ReadImage("logo:") + + //give QML the text + cl.qmlcell.ObjectByName("cellText").Set("text", cl.text) +} + //(cell) remove() should destroy everything for this cell func (cl *cell) remove() { cl.text = ""