Skip to content

Commit

Permalink
fix: apply multiplier (scale) to PNG for higher quality
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed Feb 5, 2024
1 parent d21ec9d commit 8f70231
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 21 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ require (
github.com/alecthomas/kong v0.8.2-0.20240122082144-30e84613fe34
github.com/beevik/etree v1.3.0
github.com/charmbracelet/huh v0.3.1-0.20240205173616-7d77334f08bd
github.com/charmbracelet/huh/spinner v0.0.0-20240205173616-7d77334f08bd
github.com/charmbracelet/lipgloss v0.9.2-0.20240203000443-de4601232b79
github.com/charmbracelet/log v0.3.2-0.20240104164151-fd1729ad8eb8
github.com/kanrichan/resvg-go v0.0.2-0.20231001163256-63db194ca9f5
github.com/mattn/go-isatty v0.0.20
)

Expand All @@ -24,7 +26,6 @@ require (
github.com/dlclark/regexp2 v1.10.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/kanrichan/resvg-go v0.0.2-0.20231001163256-63db194ca9f5 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ github.com/charmbracelet/bubbletea v0.25.0 h1:bAfwk7jRz7FKFl9RzlIULPkStffg5k6pNt
github.com/charmbracelet/bubbletea v0.25.0/go.mod h1:EN3QDR1T5ZdWmdfDzYcqOCAps45+QIJbLOBxmVNWNNg=
github.com/charmbracelet/huh v0.3.1-0.20240205173616-7d77334f08bd h1:0H9+MSRLp2GLVwBelCYOUPhI8kToVZg3ZCFj4nFRGBg=
github.com/charmbracelet/huh v0.3.1-0.20240205173616-7d77334f08bd/go.mod h1:ll3nYmrqcRLde+NKubJp0up1t3R274dlXyR8AKBIrDo=
github.com/charmbracelet/huh/spinner v0.0.0-20240205173616-7d77334f08bd h1:YtNPk23kqwXIVVmR8XvXaoR9Q9zvNFth6LL7GLdD4UY=
github.com/charmbracelet/huh/spinner v0.0.0-20240205173616-7d77334f08bd/go.mod h1:2l0nupcBRhdSZQqIiaV2hKwctrYbBbOr9Dn6Smox3f4=
github.com/charmbracelet/lipgloss v0.9.2-0.20240203000443-de4601232b79 h1:GwoEGZobTI/Hj0SL4CDLUqgHwWRVHvYYDUMNxnqpxxI=
github.com/charmbracelet/lipgloss v0.9.2-0.20240203000443-de4601232b79/go.mod h1:Y7ZVUws+m6C01H1UrhTyeGT0/bdHvnDIVfpV+BunAE8=
github.com/charmbracelet/log v0.3.2-0.20240104164151-fd1729ad8eb8 h1:b0ztGgJo6EgjcyfKRST1TPqzfhfqCEfMUVpv5280fhM=
Expand All @@ -42,8 +44,6 @@ github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi
github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/kanrichan/resvg-go v0.0.1 h1:qXt/ffAcybitiGxELLm40SQ58IW137Fv8WbN/kaooHY=
github.com/kanrichan/resvg-go v0.0.1/go.mod h1:8duvQiA+s19COisrVUOxxjqNBUvB5y1OUs6P1ujarO0=
github.com/kanrichan/resvg-go v0.0.2-0.20231001163256-63db194ca9f5 h1:BXnB1Gz4y/zwQh+ZFNy7rgd+ZfMOrwRr4uZSHEI+ieY=
github.com/kanrichan/resvg-go v0.0.2-0.20231001163256-63db194ca9f5/go.mod h1:c9+VS9GaommgIOzNWb5ze4lYwfT8BZ2UDyGiuQTT7yc=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
Expand Down Expand Up @@ -73,8 +73,6 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tetratelabs/wazero v1.4.0 h1:9/MirYvmkJ/zSUOygKY/ia3t+e+RqIZXKbylIby1WYk=
github.com/tetratelabs/wazero v1.4.0/go.mod h1:0U0G41+ochRKoPKCJlh0jMg1CHkyfK8kDqiirMmKY8A=
github.com/tetratelabs/wazero v1.5.0 h1:Yz3fZHivfDiZFUXnWMPUoiW7s8tC1sjdBtlJn08qYa0=
github.com/tetratelabs/wazero v1.5.0/go.mod h1:0U0G41+ochRKoPKCJlh0jMg1CHkyfK8kDqiirMmKY8A=
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a h1:Q8/wZp0KX97QFTc2ywcOE0YRjZPVIx+MXInMzdvQqcA=
Expand Down
70 changes: 58 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/mattn/go-isatty"
)

const pngExportMultiplier = 3
const pngExportMultiplier = 2

func main() {
var (
Expand Down Expand Up @@ -77,6 +77,11 @@ func main() {
}
}

multiplier := 1
if strings.HasSuffix(config.Output, ".png") {
multiplier = pngExportMultiplier
}

config.Margin = expandMargin(config.Margin)
config.Padding = expandPadding(config.Padding)

Expand Down Expand Up @@ -146,16 +151,37 @@ func main() {
w, h := svg.GetDimensions(image)

rect := image.SelectElement("rect")

// apply multiplier
w *= multiplier
h *= multiplier
config.Font.Size *= float64(multiplier)

for i := range config.Padding {
config.Padding[i] *= multiplier
}
for i := range config.Margin {
config.Margin[i] *= multiplier
}

w += config.Padding[left] + config.Padding[right]
h += config.Padding[top] + config.Padding[bottom]

config.Shadow.Blur *= multiplier
config.Shadow.X *= multiplier
config.Shadow.Y *= multiplier

config.Border.Radius *= multiplier
config.Border.Width *= multiplier

svg.SetDimensions(rect, w, h)
svg.Move(rect, float64(config.Margin[left]), float64(config.Margin[top]))

if config.Window {
windowControls := svg.NewWindowControls()
windowControls := svg.NewWindowControls(5.5*float64(multiplier), 19*multiplier, 12*multiplier)
svg.Move(windowControls, float64(config.Margin[left]), float64(config.Margin[top]))
image.AddChild(windowControls)
config.Padding[top] += 15
config.Padding[top] += (15 * multiplier)
}

if config.Border.Radius > 0 {
Expand All @@ -182,7 +208,9 @@ func main() {
rect.CreateAttr("filter", fmt.Sprintf("url(#%s)", id))
}

lines := image.SelectElement("g").SelectElements("text")
g := image.SelectElement("g")
g.CreateAttr("font-size", fmt.Sprintf("%.2fpx", config.Font.Size))
lines := g.SelectElements("text")
for i, line := range lines {
// Offset the text by padding...
// (x, y) -> (x+p, y+p)
Expand Down Expand Up @@ -212,29 +240,47 @@ func main() {
}
fontdb.LoadFontData(font.JetBrainsMonoTTF)

pixmap, err := worker.NewPixmap(uint32(w+config.Margin[left]+config.Margin[right]), uint32(h+config.Margin[top]+config.Margin[bottom]))
pixmap, err := worker.NewPixmap(uint32((w + config.Margin[left] + config.Margin[right])), uint32(h+config.Margin[top]+config.Margin[bottom]))
defer pixmap.Close()
if err != nil {
printErrorFatal("Unable to write output", err)
printError("Unable to write output", err)
os.Exit(1)
}

tree, err := worker.NewTreeFromData(svg, &resvg.Options{})
tree, err := worker.NewTreeFromData(svg, &resvg.Options{
Dpi: 288.0,
ShapeRenderingMode: resvg.ShapeRenderingModeGeometricPrecision,
TextRenderingMode: resvg.TextRenderingModeGeometricPrecision,
ImageRenderingMode: resvg.ImageRenderingModeOptimizeQuality,
FontSize: float32(config.Font.Size),
})
defer tree.Close()
if err != nil {
printErrorFatal("Unable to write output", err)
printError("Unable to write output", err)
os.Exit(1)
}

err = tree.ConvertText(fontdb)
if err != nil {
printErrorFatal("Unable to write output", err)
printError("Unable to render text", err)
os.Exit(1)
}
err = tree.Render(resvg.TransformIdentity(), pixmap)
if err != nil {
printError("Unable to render PNG", err)
os.Exit(1)
}
tree.Render(resvg.TransformIdentity(), pixmap)
png, err := pixmap.EncodePNG()
if err != nil {
printErrorFatal("Unable to write output", err)
printError("Unable to encode PNG", err)
os.Exit(1)
}

os.WriteFile(config.Output, png, 0644)
err = os.WriteFile(config.Output, png, 0644)
if err != nil {
printError("Unable to write output", err)
os.Exit(1)
}

case strings.HasSuffix(config.Output, ".svg"):
if istty {
Expand Down
8 changes: 4 additions & 4 deletions svg/svg.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ const (
)

// NewWindowControls returns a colorful window bar element.
func NewWindowControls() *etree.Element {
func NewWindowControls(r float64, x, y int) *etree.Element {
bar := etree.NewElement("svg")
for i, color := range []string{red, yellow, green} {
circle := etree.NewElement("circle")
circle.CreateAttr("cx", fmt.Sprintf("%d", (i+1)*19+-6))
circle.CreateAttr("cy", fmt.Sprintf("%d", 12))
circle.CreateAttr("r", "5.5")
circle.CreateAttr("cx", fmt.Sprintf("%.2f", float64(i+1)*float64(x)-float64(r)))
circle.CreateAttr("cy", fmt.Sprintf("%d", y))
circle.CreateAttr("r", fmt.Sprintf("%.2f", r))
circle.CreateAttr("fill", color)
bar.AddChild(circle)
}
Expand Down

0 comments on commit 8f70231

Please sign in to comment.