Skip to content

Commit

Permalink
Add hex-bifilar-coil-diagram example
Browse files Browse the repository at this point in the history
  • Loading branch information
gmlewis committed Feb 9, 2019
1 parent 5ad6313 commit 6c9c03a
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 28 deletions.
30 changes: 2 additions & 28 deletions cmd/render-fonts/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"sort"
"strings"

"github.com/fogleman/gg"
. "github.com/gmlewis/go-fonts/fonts"
_ "github.com/gmlewis/go-fonts/fonts/latoregular"
)
Expand Down Expand Up @@ -71,34 +70,9 @@ func main() {
log.Fatal(err)
}

scale := float64(*width) / (render.MBB.Max[0] - render.MBB.Min[0])
if yScale := float64(*height) / (render.MBB.Max[1] - render.MBB.Min[1]); yScale < scale {
scale = yScale
*width = int(0.5 + scale*(render.MBB.Max[0]-render.MBB.Min[0]))
} else {
*height = int(0.5 + scale*(render.MBB.Max[1]-render.MBB.Min[1]))
}
log.Printf("MBB: %v, scale=%.2f, size=(%v,%v)", render.MBB, scale, *width, *height)

dc := gg.NewContext(*width, *height)
dc.SetRGB(1, 1, 1)
dc.Clear()
for _, poly := range render.Polygons {
if poly.Dark {
dc.SetRGB(0, 0, 0)
} else {
dc.SetRGB(1, 1, 1)
}
for i, pt := range poly.Pts {
if i == 0 {
dc.MoveTo(scale*(pt[0]-render.MBB.Min[0]), float64(*height)-scale*(pt[1]-render.MBB.Min[1]))
} else {
dc.LineTo(scale*(pt[0]-render.MBB.Min[0]), float64(*height)-scale*(pt[1]-render.MBB.Min[1]))
}
}
dc.Fill()
if err := render.SavePNG(*out, *width, *height); err != nil {
log.Fatal(err)
}
dc.SavePNG(*out)
}

fmt.Println("Done.")
Expand Down
86 changes: 86 additions & 0 deletions examples/hex-bifilar-coil-diagram/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// hex-bifilar-coil-diagram renders a schematic diagram
// representing the wiring of the hex bifilar coil design
// from https://github.com/gmlewis/go-gerber/tree/master/examples/hex-bifilar-coil .
package main

import (
"flag"
"fmt"
"log"
"math"

. "github.com/gmlewis/go-fonts/fonts"
_ "github.com/gmlewis/go-fonts/fonts/carrelectronicdingbats"
_ "github.com/gmlewis/go-fonts/fonts/latoregular"
)

var (
width = flag.Int("width", 800, "Image width")
height = flag.Int("height", 800, "Image height")
out = flag.String("out", "hex-bifilar-coil-diagram.png", "Output image filename")
)

const (
carrFont = "carrelectronicdingbats"
textFont = "latoregular"

dx = 0.02
ts = 0.25
)

func main() {
flag.Parse()

opts := &TextOpts{Rotate: math.Pi / 2}
coil1, err := Text(0, 0, 1.0, 1.0, "}", carrFont, opts)
check(err)
log.Printf("coil1=%v", coil1.MBB)
coil2, err := Text(coil1.MBB.Max[0]-coil1.MBB.Min[0]-dx, 0, 1.0, 1.0, "}", carrFont, opts)
check(err)
log.Printf("coil2=%v", coil2.MBB)
coil3, err := Text(coil2.MBB.Max[0]-coil1.MBB.Min[0]-dx, 0, 1.0, 1.0, "}", carrFont, opts)
check(err)
log.Printf("coil3=%v", coil3.MBB)
coil4, err := Text(coil3.MBB.Max[0]-coil1.MBB.Min[0]-dx, 0, 1.0, 1.0, "}", carrFont, opts)
check(err)
log.Printf("coil4=%v", coil4.MBB)
coil5, err := Text(coil4.MBB.Max[0]-coil1.MBB.Min[0]-dx, 0, 1.0, 1.0, "}", carrFont, opts)
check(err)
log.Printf("coil5=%v", coil5.MBB)
coil6, err := Text(coil5.MBB.Max[0]-coil1.MBB.Min[0]-dx, 0, 1.0, 1.0, "}", carrFont, opts)
check(err)
log.Printf("coil6=%v", coil6.MBB)

opts = &TopCenter
text3L, err := Text(coil1.MBB.Min[0]+dx, coil1.MBB.Min[1], ts, ts, "3L", textFont, opts)
check(err)
textBL, err := Text(coil2.MBB.Min[0]+dx, coil2.MBB.Min[1], ts, ts, "BL", textFont, opts)
check(err)
text5L, err := Text(coil3.MBB.Min[0]+dx, coil3.MBB.Min[1], ts, ts, "5L", textFont, opts)
check(err)
text3R, err := Text(coil4.MBB.Min[0]+dx, coil4.MBB.Min[1], ts, ts, "3R", textFont, opts)
check(err)
textBR, err := Text(coil5.MBB.Min[0]+dx, coil5.MBB.Min[1], ts, ts, "BR", textFont, opts)
check(err)
text5R, err := Text(coil6.MBB.Min[0]+dx, coil6.MBB.Min[1], ts, ts, "5R", textFont, opts)
check(err)
text2R, err := Text(coil6.MBB.Max[0]+dx, coil6.MBB.Min[1], ts, ts, "2R", textFont, opts)
check(err)

coils := Merge(
coil1, coil2, coil3, coil4, coil5, coil6,
text3L, textBL, text5L, text3R, textBR, text5R, text2R,
)

if err := coils.SavePNG(*out, *width, *height); err != nil {
log.Fatal(err)
}

fmt.Println("Done.")
}

func check(err error) {
if err != nil {
log.Fatal(err)
}
}
Binary file modified fonts/latoregular/latoregular.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions fonts/merge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package fonts

func Merge(renders ...*Render) *Render {
merged := &Render{}
for i, r := range renders {
if i == 0 {
merged.MBB = r.MBB
} else {
merged.MBB.Join(&r.MBB)
}
merged.Polygons = append(merged.Polygons, r.Polygons...)
merged.Info = append(merged.Info, r.Info...)
}
return merged
}
38 changes: 38 additions & 0 deletions fonts/png.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package fonts

import (
"log"

"github.com/fogleman/gg"
)

func (r *Render) SavePNG(filename string, width, height int) error {
scale := float64(width) / (r.MBB.Max[0] - r.MBB.Min[0])
if yScale := float64(height) / (r.MBB.Max[1] - r.MBB.Min[1]); yScale < scale {
scale = yScale
width = int(0.5 + scale*(r.MBB.Max[0]-r.MBB.Min[0]))
} else {
height = int(0.5 + scale*(r.MBB.Max[1]-r.MBB.Min[1]))
}
log.Printf("MBB: %v, scale=%.2f, size=(%v,%v)", r.MBB, scale, width, height)

dc := gg.NewContext(width, height)
dc.SetRGB(1, 1, 1)
dc.Clear()
for _, poly := range r.Polygons {
if poly.Dark {
dc.SetRGB(0, 0, 0)
} else {
dc.SetRGB(1, 1, 1)
}
for i, pt := range poly.Pts {
if i == 0 {
dc.MoveTo(scale*(pt[0]-r.MBB.Min[0]), float64(height)-scale*(pt[1]-r.MBB.Min[1]))
} else {
dc.LineTo(scale*(pt[0]-r.MBB.Min[0]), float64(height)-scale*(pt[1]-r.MBB.Min[1]))
}
}
dc.Fill()
}
return dc.SavePNG(filename)
}

0 comments on commit 6c9c03a

Please sign in to comment.