Compare commits

...

2 Commits

2 changed files with 59 additions and 8 deletions

View File

@ -91,8 +91,7 @@ func getTextBounds(text []string, size float64) (width, height int) {
} }
func rectsOverlap(r1, r2 struct{ x, y, w, h int }) bool { func rectsOverlap(r1, r2 struct{ x, y, w, h int }) bool {
return !(r1.x+r1.w <= r2.x || r2.x+r2.w <= r1.x || return !(r1.x+r1.w <= r2.x || r2.x+r2.w <= r1.x || r1.y+r1.h <= r2.y || r2.y+r2.h <= r1.y)
r1.y+r1.h <= r2.y || r2.y+r2.h <= r1.y)
} }
func drawTextWithOutline(canvas *image.RGBA, text []string, x, y int, textColor, outlineColor color.RGBA, size float64) { func drawTextWithOutline(canvas *image.RGBA, text []string, x, y int, textColor, outlineColor color.RGBA, size float64) {
@ -144,7 +143,18 @@ func drawTextWithOutline(canvas *image.RGBA, text []string, x, y int, textColor,
} }
} }
func abs(a int) int {
if a < 0 {
return -a
}
return a
}
func findTextPositions(textData []*TextData, filename string) { func findTextPositions(textData []*TextData, filename string) {
type attemptedPos struct {
x, y int
}
padding := 10 padding := 10
for i, data := range textData { for i, data := range textData {
@ -172,12 +182,33 @@ func findTextPositions(textData []*TextData, filename string) {
continue continue
} }
// track attempted positions to avoid redundancy
attempts := make([]attemptedPos, 0, maxPositioningAttempts)
// min. distance between attempts (1/5 of text height)
minAttemptDistance := max(h/5, 10)
isTooCloseToAttempted := func(x, y int) bool {
for _, pos := range attempts {
if abs(x-pos.x) < minAttemptDistance && abs(y-pos.y) < minAttemptDistance {
return true
}
}
return false
}
// try random positions // try random positions
for attempt := 0; attempt < maxPositioningAttempts && !placed; attempt++ { for attempt := 0; attempt < maxPositioningAttempts && !placed; attempt++ {
// gen. random pos. // gen. random pos.
x := rand.IntN(maxX-minX+1) + minX x := rand.IntN(maxX-minX+1) + minX
y := rand.IntN(maxY-minY+1) + minY y := rand.IntN(maxY-minY+1) + minY
// skip if too close to a previously attempt pos.
if isTooCloseToAttempted(x, y) {
continue
}
attempts = append(attempts, attemptedPos{x: x, y: y})
// bounding box for cur. text // bounding box for cur. text
curRect := struct{ x, y, w, h int }{ curRect := struct{ x, y, w, h int }{
x: x, x: x,
@ -188,7 +219,7 @@ func findTextPositions(textData []*TextData, filename string) {
// checks of overlaps with already placed texts // checks of overlaps with already placed texts
overlaps := false overlaps := false
for j := 0; j < i; j++ { for j := range i {
other := textData[j] other := textData[j]
if !other.Positioned { if !other.Positioned {
continue continue
@ -245,7 +276,7 @@ func findTextPositions(textData []*TextData, filename string) {
totalOverlapArea := 0 totalOverlapArea := 0
// calculate total overlap area with existing texts // calculate total overlap area with existing texts
for j := 0; j < i; j++ { for j := range i {
other := textData[j] other := textData[j]
if !other.Positioned { if !other.Positioned {
continue continue

28
main.go
View File

@ -38,9 +38,29 @@ type BannerInfo struct {
func init() { func init() {
config = Config{ config = Config{
ClearwebURL: "https://example.com", ClearwebURL: "https://example.com",
OnionURL: "http://example.onion", OnionURL: "http://example.onion",
ColorThemes: []string{"vibrant", "sunset", "ocean", "neon", "forest", "cosmic"}, ColorThemes: []string{
"vibrant",
"sunset",
"ocean",
"neon",
"cosmic",
"midnight",
"fire",
"pastel",
"earth",
"ice",
"autumn",
"cyberpunk",
"retro",
"monochrome",
"tropical",
"volcanic",
"aurora",
"desert",
"matrix",
},
MaxBanners: 50, MaxBanners: 50,
BannerWidth: 600, BannerWidth: 600,
BannerHeight: 120, BannerHeight: 120,
@ -87,7 +107,7 @@ func main() {
NewTextData(config.ClearwebURL, defaultFontSize+4.0, false), NewTextData(config.ClearwebURL, defaultFontSize+4.0, false),
} }
for range 10 { for range 25 {
if err := genBanner(textData); err != nil { if err := genBanner(textData); err != nil {
log.Errorf("Error generating banner: %s", err) log.Errorf("Error generating banner: %s", err)
} }