-
Notifications
You must be signed in to change notification settings - Fork 204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
vg/vgimg: migrate to fogleman/gg #471
Conversation
it's a WIP:
|
c761757
to
4d37799
Compare
PTAL. it seems the rendering of fonts is somewhat different (a bit fuzzier) than what we had with draw2d... |
@fogleman do you know why text would appear fuzzier when using see:
|
I suspect you are translating some non-integer amount before drawing the text. |
(That said, it should be possible to draw nice looking text even in that case, so I will look into this.) |
thx for looking into it! I think I made progress: it seems to come from different default DPIs. it seems |
here is a simpler reproducer: package main
import (
"image"
"image/color"
"image/draw"
"log"
"os"
"github.com/fogleman/gg"
"gonum.org/v1/plot/vg"
"gonum.org/v1/plot/vg/vgimg"
)
const (
fontName = "Times-Roman"
)
func main() {
f72()
f96()
}
func f72() {
c := vgimg.NewWith(
vgimg.UseDPI(72),
vgimg.UseWH(200, 200),
vgimg.UseBackgroundColor(color.Transparent),
)
ft, err := vg.MakeFont(fontName, 12)
if err != nil {
log.Fatal(err)
}
log.Printf("f2: %v", c.Image().Bounds())
c.FillString(ft, vg.Point{X: 50, Y: 100}, "Hello 72")
pp := vgimg.PngCanvas{c}
o, err := os.Create("o-f2-72.png")
if err != nil {
log.Fatal(err)
}
pp.WriteTo(o)
o.Close()
}
func f96() {
c := vgimg.NewWith(
vgimg.UseDPI(96),
vgimg.UseWH(200, 200),
vgimg.UseBackgroundColor(color.Transparent),
)
ft, err := vg.MakeFont(fontName, 12)
if err != nil {
log.Fatal(err)
}
log.Printf("f2: %v", c.Image().Bounds())
c.FillString(ft, vg.Point{X: 50, Y: 100}, "Hello 96")
pp := vgimg.PngCanvas{c}
o, err := os.Create("o-f2-96.png")
if err != nil {
log.Fatal(err)
}
pp.WriteTo(o)
o.Close()
} |
and an even simpler one: package main
import (
"log"
"github.com/fogleman/gg"
"gonum.org/v1/plot/vg"
)
var (
pt = vg.Point{X: 20, Y: 60}
)
func main() {
ctx := gg.NewContext(100, 100)
ctx.SetRGB(1, 1, 1)
ctx.Clear()
ctx.InvertY()
ctx.SetRGB(0, 0, 0)
ft, err := vg.MakeFont("Times-Roman", 12)
if err != nil {
log.Fatal(err)
}
ctx.SetFontFace(ft.FontFace(96))
x1 := pt.X.Dots(96) // == 26.666666666666668
y1 := 20.0
x2 := pt.X.Dots(96)
y2 := 10.0
{
ctx.Push()
ctx.InvertY()
ctx.DrawString("Hello+96", x1, y1)
ctx.Pop()
}
{
ctx.Push()
ctx.Translate(x2, y2)
ctx.Scale(1, -1)
ctx.DrawString("Hello-96", 0, 0)
ctx.Pop()
}
ctx.SavePNG("out.png")
} so, indeed something related with non-integer-ness. I've modified the diff --git a/vg/vgimg/vgimg.go b/vg/vgimg/vgimg.go
index a12a464..098b6a4 100644
--- a/vg/vgimg/vgimg.go
+++ b/vg/vgimg/vgimg.go
@@ -297,9 +297,12 @@ func (c *Canvas) FillString(font vg.Font, pt vg.Point, str string) {
c.ctx.SetFontFace(font.FontFace(c.DPI()))
- c.ctx.Translate(pt.X.Dots(c.DPI()), pt.Y.Dots(c.DPI()))
- c.ctx.Scale(1, -1)
- c.ctx.DrawString(str, 0, 0)
+ x := pt.X.Dots(c.DPI())
+ y := pt.Y.Dots(c.DPI())
+ h := c.h.Dots(c.DPI())
+
+ c.ctx.InvertY()
+ c.ctx.DrawString(str, x, h-y)
}
// DrawImage implements the vg.Canvas.DrawImage method. it seems to give better results... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this fix #231?
I guess I can add |
actually, as I wrote in the last CL:
so we don't even need to modify |
PTAL. |
#231 talks about dashed line strokes. The gg example doesn't have any dashing AFAICS. Does it support it? |
Thanks, @fogleman. |
and here is how I converted to the https://github.com/gonum/plot/pull/471/files#diff-3fc76b9160b7dbc0faf71918da9adea0R192 |
OK, the offset doesn't have a meaning in the gg @fogleman Is there a way of specify an offset into the dash sequence? |
There isn't. 😢 But I should be able to add that. |
Thanks. So, @sbinet, in the first instance just drop the |
6b3cac9
to
b4d6742
Compare
ok. except for:
differences are now somewhat "in the noise." I've created fogleman/gg#64 to track the dash-offset feature. |
@ctessum would have to comment on whether the new output of the |
I think the polygon_holes change is fine. As of now, the winding order is backend-dependent, but if this makes all of the backends agree, that is probably an improvement. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small query then LGTM.
The rotated text in https://github.com/gonum/plot/pull/471/files?short_path=be63a62#diff-be63a62daebdea2ba8d92058785fcbf8 is a bit fuzzy. |
Ah, yes. Presumably upstream will eventually fix that. I prefer fuzz though to what we have at the moment. |
PTAL |
You may want to rebase onto master to avoid the coveralls failure that will otherwise happen. |
This CL adds consistent handling of DPI for the fogleman/gg backend. The old backend required to be communicated the current DPI for the image. This new backend does not, as the DPI is only needed for the font. gg handles fonts via golang.org/x/image/font.Face that embeds the DPI upon creation of that font. The vg/vgimg.Canvas carries its own DPI information and propagates it correctly upon font registration and creation. This evacuates the need to communicate the current DPI to gg.
Drop llgcode/draw2d, add fogleman/gg.
done. |
could you remind me of the rule for "large-ish" PRs like that (that consist of many commits) ? |
This is all fairly obviously one thing. So I would squash it and tidy up the commit message body and add any notes you think are relevant. |
Fixes #470.
Please take a look.