From 060628cac024abbb0577188a7ffb497c7f50a2f9 Mon Sep 17 00:00:00 2001 From: Liu Date: Sat, 28 Sep 2024 15:52:44 +0800 Subject: [PATCH 1/3] support otf format fonts --- go.mod | 11 +++++++++++ go.sum | 8 ++++++++ util.go | 21 +++++++++++++++------ 3 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 go.mod create mode 100644 go.sum diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..23cfdcf --- /dev/null +++ b/go.mod @@ -0,0 +1,11 @@ +module github.com/aa88kk/gg + +go 1.23.0 + +require ( + github.com/fogleman/gg v1.3.0 + github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 + golang.org/x/image v0.20.0 +) + +require golang.org/x/text v0.18.0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..5264f29 --- /dev/null +++ b/go.sum @@ -0,0 +1,8 @@ +github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8= +github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k= +github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g= +github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= +golang.org/x/image v0.20.0 h1:7cVCUjQwfL18gyBJOmYvptfSHS8Fb3YUDtfLIZ7Nbpw= +golang.org/x/image v0.20.0/go.mod h1:0a88To4CYVBAHp5FXJm8o7QbUl37Vd85ply1vyD8auM= +golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224= +golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= diff --git a/util.go b/util.go index 4497b7c..08ee9eb 100644 --- a/util.go +++ b/util.go @@ -12,9 +12,8 @@ import ( "os" "strings" - "github.com/golang/freetype/truetype" - "golang.org/x/image/font" + "golang.org/x/image/font/opentype" "golang.org/x/image/math/fixed" ) @@ -134,13 +133,23 @@ func LoadFontFace(path string, points float64) (font.Face, error) { if err != nil { return nil, err } - f, err := truetype.Parse(fontBytes) + f, err := opentype.Parse(fontBytes) + // f, err := truetype.Parse(fontBytes) if err != nil { return nil, err } - face := truetype.NewFace(f, &truetype.Options{ - Size: points, - // Hinting: font.HintingFull, + face, err := opentype.NewFace(f, &opentype.FaceOptions{ + Size: points, + DPI: 72, + Hinting: font.HintingNone, }) + if err != nil { + return nil, err + } + + // face := truetype.NewFace(f, &truetype.Options{ + // Size: points, + // // Hinting: font.HintingFull, + // }) return face, nil } From 5c287dac867ca842c0ef09d33cd4f36e850b4809 Mon Sep 17 00:00:00 2001 From: Liu Date: Sat, 28 Sep 2024 16:06:14 +0800 Subject: [PATCH 2/3] update README.md --- README.md | 221 +----------------------------------------------------- 1 file changed, 2 insertions(+), 219 deletions(-) diff --git a/README.md b/README.md index 8fdcabd..1677543 100644 --- a/README.md +++ b/README.md @@ -1,223 +1,6 @@ -# Go Graphics +This is a fork of [grit] (https://github.com/mojombo/grit), with : -`gg` is a library for rendering 2D graphics in pure Go. -![Stars](http://i.imgur.com/CylQIJt.png) +support opentype fonts(otf). -## Installation - go get -u github.com/fogleman/gg - -Alternatively, you may use gopkg.in to grab a specific major-version: - - go get -u gopkg.in/fogleman/gg.v1 - -## Documentation - -- godoc: https://godoc.org/github.com/fogleman/gg -- pkg.go.dev: https://pkg.go.dev/github.com/fogleman/gg?tab=doc - -## Hello, Circle! - -Look how easy! - -```go -package main - -import "github.com/fogleman/gg" - -func main() { - dc := gg.NewContext(1000, 1000) - dc.DrawCircle(500, 500, 400) - dc.SetRGB(0, 0, 0) - dc.Fill() - dc.SavePNG("out.png") -} -``` - -## Examples - -There are [lots of examples](https://github.com/fogleman/gg/tree/master/examples) included. They're mostly for testing the code, but they're good for learning, too. - -![Examples](http://i.imgur.com/tMFoyzu.png) - -## Creating Contexts - -There are a few ways of creating a context. - -```go -NewContext(width, height int) *Context -NewContextForImage(im image.Image) *Context -NewContextForRGBA(im *image.RGBA) *Context -``` - -## Drawing Functions - -Ever used a graphics library that didn't have functions for drawing rectangles -or circles? What a pain! - -```go -DrawPoint(x, y, r float64) -DrawLine(x1, y1, x2, y2 float64) -DrawRectangle(x, y, w, h float64) -DrawRoundedRectangle(x, y, w, h, r float64) -DrawCircle(x, y, r float64) -DrawArc(x, y, r, angle1, angle2 float64) -DrawEllipse(x, y, rx, ry float64) -DrawEllipticalArc(x, y, rx, ry, angle1, angle2 float64) -DrawRegularPolygon(n int, x, y, r, rotation float64) -DrawImage(im image.Image, x, y int) -DrawImageAnchored(im image.Image, x, y int, ax, ay float64) -SetPixel(x, y int) - -MoveTo(x, y float64) -LineTo(x, y float64) -QuadraticTo(x1, y1, x2, y2 float64) -CubicTo(x1, y1, x2, y2, x3, y3 float64) -ClosePath() -ClearPath() -NewSubPath() - -Clear() -Stroke() -Fill() -StrokePreserve() -FillPreserve() -``` - -It is often desired to center an image at a point. Use `DrawImageAnchored` with `ax` and `ay` set to 0.5 to do this. Use 0 to left or top align. Use 1 to right or bottom align. `DrawStringAnchored` does the same for text, so you don't need to call `MeasureString` yourself. - -## Text Functions - -It will even do word wrap for you! - -```go -DrawString(s string, x, y float64) -DrawStringAnchored(s string, x, y, ax, ay float64) -DrawStringWrapped(s string, x, y, ax, ay, width, lineSpacing float64, align Align) -MeasureString(s string) (w, h float64) -MeasureMultilineString(s string, lineSpacing float64) (w, h float64) -WordWrap(s string, w float64) []string -SetFontFace(fontFace font.Face) -LoadFontFace(path string, points float64) error -``` - -## Color Functions - -Colors can be set in several different ways for your convenience. - -```go -SetRGB(r, g, b float64) -SetRGBA(r, g, b, a float64) -SetRGB255(r, g, b int) -SetRGBA255(r, g, b, a int) -SetColor(c color.Color) -SetHexColor(x string) -``` - -## Stroke & Fill Options - -```go -SetLineWidth(lineWidth float64) -SetLineCap(lineCap LineCap) -SetLineJoin(lineJoin LineJoin) -SetDash(dashes ...float64) -SetDashOffset(offset float64) -SetFillRule(fillRule FillRule) -``` - -## Gradients & Patterns - -`gg` supports linear, radial and conic gradients and surface patterns. You can also implement your own patterns. - -```go -SetFillStyle(pattern Pattern) -SetStrokeStyle(pattern Pattern) -NewSolidPattern(color color.Color) -NewLinearGradient(x0, y0, x1, y1 float64) -NewRadialGradient(x0, y0, r0, x1, y1, r1 float64) -NewConicGradient(cx, cy, deg float64) -NewSurfacePattern(im image.Image, op RepeatOp) -``` - -## Transformation Functions - -```go -Identity() -Translate(x, y float64) -Scale(x, y float64) -Rotate(angle float64) -Shear(x, y float64) -ScaleAbout(sx, sy, x, y float64) -RotateAbout(angle, x, y float64) -ShearAbout(sx, sy, x, y float64) -TransformPoint(x, y float64) (tx, ty float64) -InvertY() -``` - -It is often desired to rotate or scale about a point that is not the origin. The functions `RotateAbout`, `ScaleAbout`, `ShearAbout` are provided as a convenience. - -`InvertY` is provided in case Y should increase from bottom to top vs. the default top to bottom. - -## Stack Functions - -Save and restore the state of the context. These can be nested. - -```go -Push() -Pop() -``` - -## Clipping Functions - -Use clipping regions to restrict drawing operations to an area that you -defined using paths. - -```go -Clip() -ClipPreserve() -ResetClip() -AsMask() *image.Alpha -SetMask(mask *image.Alpha) -InvertMask() -``` - -## Helper Functions - -Sometimes you just don't want to write these yourself. - -```go -Radians(degrees float64) float64 -Degrees(radians float64) float64 -LoadImage(path string) (image.Image, error) -LoadPNG(path string) (image.Image, error) -SavePNG(path string, im image.Image) error -``` - -![Separator](http://i.imgur.com/fsUvnPB.png) - -## Another Example - -See the output of this example below. - -```go -package main - -import "github.com/fogleman/gg" - -func main() { - const S = 1024 - dc := gg.NewContext(S, S) - dc.SetRGBA(0, 0, 0, 0.1) - for i := 0; i < 360; i += 15 { - dc.Push() - dc.RotateAbout(gg.Radians(float64(i)), S/2, S/2) - dc.DrawEllipse(S/2, S/2, S*7/16, S/8) - dc.Fill() - dc.Pop() - } - dc.SavePNG("out.png") -} -``` - -![Ellipses](http://i.imgur.com/J9CBZef.png) From 9f6a265a62553d4b725f47a0344c3f8cbe1b2890 Mon Sep 17 00:00:00 2001 From: Liu Date: Sat, 28 Sep 2024 16:06:14 +0800 Subject: [PATCH 3/3] update README.md --- README.md | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/README.md b/README.md index 1677543..4480258 100644 --- a/README.md +++ b/README.md @@ -1,6 +1 @@ -This is a fork of [grit] (https://github.com/mojombo/grit), with : - - -support opentype fonts(otf). - - +This is a fork of [grit] (https://github.com/fogleman/gg), with support opentype fonts(otf).