freetype icon indicating copy to clipboard operation
freetype copied to clipboard

About the `done` input parameter of `Paint` functions

Open Megidd opened this issue 1 year ago • 3 comments

I investigated the done input parameter of func (r *RGBAPainter) Paint(ss []Span, done bool) {} here:

https://github.com/golang/freetype/blob/e2365dfdc4a05e4b8299a783240d4a7d5a65d4e4/raster/paint.go#L127

My debugger indicates that the done input parameter passed to the function is true multiple times. It is not what I expected. I expected that it should be true once and only for the last span to be drawn. I mean, it should indicate whether the drawing is done. Right?

Why is the done parameter not behaving as I assumed? Am I missing something about it? Thanks.

Megidd avatar Apr 14 '25 05:04 Megidd

For example, while drawing a single image, it's observed that multiple calls to Paint function would pass the done parameter of true value:

Image Image Image

Megidd avatar Apr 14 '25 06:04 Megidd

It occurred to me that maybe the done parameter will be true whenever a LineTo call finishes. So, I investigated like below.

I added this log to the source code:

	if done {
		fmt.Printf("done is %v\n", done)
	}

Image

The debugger indicates that done is true is logged 4 times.

These are my drawing calls:

import (
	"image"
	"image/color"
	"image/png"
	"log"
	"os"
	"testing"

	draw2dimg_external "github.com/llgcode/draw2d/draw2dimg"
)

func TestCanvas(t *testing.T) {
	var tests = []struct {
		skip        bool
		name        string
		startX      int
		startY      int
		endX        int
		endY        int
		fillShade   uint8   // Any integer from 0 to 255
		strokeShade uint8   // Any integer from 0 to 255
		lineWidth   float64 // Stroke thickness
	}{
		{false, "test1", 0, 0, 312, 210.0, 0xdd, 0x88, 2.0},
	}

	for _, tt := range tests {
		if tt.skip == true {
			continue
		}
		t.Run(tt.name, func(t *testing.T) {

			{
				// Initialize the graphic context on an RGBA image
				dest := image.NewRGBA(image.Rect(tt.startX, tt.startY, tt.endX, tt.endY))
				gc := draw2dimg_external.NewGraphicContext(dest)

				// Set some properties
				gc.SetFillColor(color.RGBA{tt.fillShade, tt.fillShade, tt.fillShade, 0xff})

				gc.SetLineWidth(tt.lineWidth)
				gc.SetStrokeColor(color.RGBA{tt.strokeShade, 0x00, 0x00, 0xff})

				// Draw a closed shape
				gc.BeginPath() // Initialize a new path
				gc.MoveTo(0, 0)
				gc.LineTo(100, 0)
				gc.LineTo(100, 100)
				gc.LineTo(50, 150)
				gc.LineTo(0, 100)
				gc.Close()
				gc.FillStroke()

				// Empty path
				gc.SetFillColor(color.RGBA{0x00, 0x00, 0x00, 0xff}) // Any integer from 0 to 255

				// Draw a closed shape
				gc.BeginPath() // Initialize a new path
				gc.MoveTo(0+20, 0+20)
				gc.LineTo(100-20, 0+20)
				gc.LineTo(100-20, 100-20)
				gc.LineTo(50, 150-20)
				gc.LineTo(0+20, 100-20)
				gc.Close()
				gc.FillStroke()

				// Save to file
				img.SavePNG(tt.name+"-upstream.png", dest)
			}
		})
	}
}

Since I'm calling LineTo exactly 8 times, I was expecting to have 8 instances of done is true log. But I receive only 4.

Why is the done parameter behaving like this?

By the way, the final test1-upstream.png image is this:

Image

Megidd avatar Apr 16 '25 06:04 Megidd

Samples of AI chatbot help with the done parameter:

https://poe.com/s/NIGl710jmRGgDpLWbr5V

https://poe.com/s/jys0CecKKIb106cl69VQ

But I need a human developer feedback on this 🙄

Megidd avatar Apr 16 '25 07:04 Megidd