generadorPortadasLibro/portada.go

217 lines
7.1 KiB
Go

package main
import (
"github.com/fogleman/gg"
"image"; "image/png"; "image/color"
"fmt"; "os"
"bufio"; "strings"
"math/rand"; "time"
)
// Pagina A4 (2480 x 3508)
var pagAncho = 2480
var pagAlto = 3508
var portada = gg.NewContext(pagAncho, pagAlto) // Tamaño de Portada A4 para la Imagen (Ancho x Alto)
var AltoAreaTitulo = 920 // Altura en pixeles del Area de Titulo
// Colores/Temas
var colorTema = color.RGBA{60, 92, 153, 255} // Color del Tema del Libro (defectoa es Azul)
var colorRojo = color.RGBA{202, 31, 0, 255}
var colorVerde = color.RGBA{29, 149, 27, 255}
var colorAzul = color.RGBA{60, 92, 153, 255} // Azul con transparencia
var colorAmarillo = color.RGBA{223, 217, 26, 255}
var colorBlanco = color.RGBA{255, 255, 255, 255} // Blanco con transparencia
var colorNegro = color.RGBA{0, 0, 0, 255} // Negro con transparencia
// Texto
var colorTextoTitulo = (color.RGBA{255, 255, 255, 230}) // Blanco con transparencia reducida
var colorTextoResumen = (color.RGBA{0, 0, 0, 230}) // Negro con transparencia reducida
func main() {
// Generador de Portadas de Libro
fmt.Println("GENERADOR DE PORTADAS PARA LIBROS")
// Pedir datos
leer := bufio.NewReader(os.Stdin)
fmt.Println("Título: ")
inTitulo, _ := leer.ReadString('\n')
inTitulo = strings.Replace(inTitulo, "\n", "", -1)
fmt.Println("SubTítulo: ")
inSubtitulo, _ := leer.ReadString('\n')
inSubtitulo = strings.Replace(inSubtitulo, "\n", "", -1)
fmt.Println("Autor: ")
inAutor, _ := leer.ReadString('\n')
inAutor = strings.Replace(inAutor, "\n", "", -1)
fmt.Println("Editorial: ")
inEditorial, _ := leer.ReadString('\n')
inEditorial = strings.Replace(inEditorial, "\n", "", -1)
// Color Portada
fmt.Println("Color de Portada [ N:Negro, R:Rojo, V:Verde, A:Azul, M:Amarillo ] ")
inColor, _ := leer.ReadString('\n')
inColor = strings.Replace(inColor, "\n", "", -1)
switch inColor {
case "N": // Negro
colorTema = colorNegro
//colorTema = color.RGBA{0, 0, 0, 255}
case "R": // Rojo
colorTema = colorRojo
//colorTema = color.RGBA{202, 31, 0, 255}
case "V": // Verde
colorTema = colorVerde
//colorTema = color.RGBA{29, 149, 27, 255}
case "A": // Azul
colorTema = colorAzul
//colorTema = color.RGBA{60, 92, 153, 255}
case "M": // Amarillo
colorTema = colorAmarillo
//colorTema = color.RGBA{223, 217, 26, 255}
default: // defecto es Azul
colorTema = colorAzul
}
fmt.Println("Generando Portada. Espere por favor....")
// Fondo portada (x, y, ancho/width, alto/height)
DibujaRectanguloColor(colorBlanco, 0, 0, portada.Width(), portada.Height())
DibujaRectanguloColor(colorTema, 0, 200, portada.Width(), AltoAreaTitulo)
// Localizar imagen deseada: imagen.png -es un link de imagen en ruta ./imagen/
// Si no existe escoger imagen al azar
if _, err := os.Stat("./imagen.png"); err == nil {
LeerIncluirImagen("./imagen.png", 200, AltoAreaTitulo+400)
} else {
// imagen random
f, err := os.Open("./imagenes")
if err != nil {
fmt.Println(err)
return
}
files, err := f.Readdir(0)
if err != nil {
fmt.Println(err)
return
}
// Random
rand.Seed(time.Now().UnixNano())
min := 0
max := len(files)-1
posRandom := rand.Intn(max - min) + min
LeerIncluirImagen("./imagenes/"+files[posRandom].Name(), 200, AltoAreaTitulo+400)
}
PonerTextoTitulo(inTitulo, 150, 135)
PonerTextoResumen(inSubtitulo, 150, 72)
PonerTextoEditorial(inEditorial, 100)
PonerTextoAutor(inAutor, 72)
// Banda superior
DibujaRectanguloColor(colorTema, 60, 0, portada.Width()-120, 20)
// Banda inferior
DibujaRectanguloColor(colorTema, 60, pagAlto-20, portada.Width()-120, 20)
portada.SavePNG("portada.png")
fmt.Println("Se ha generado 'portada.png")
}
func DibujaRectanguloColor(c color.Color, x, y, w, h int) {
portada.SetColor(c)
portada.DrawRectangle(float64(x), float64(y), float64(w), float64(h))
portada.Fill()
}
func LeerIncluirImagen(fichImagen string, x, y int) {
backgroundImage, err := gg.LoadImage(fichImagen)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
portada.DrawImage(backgroundImage, x, y)
}
func PonerTextoTitulo(titulo string, margin int, size int) {
// Leer el fichero de la fuente ttf y fijar tamaño fuente
if err := portada.LoadFontFace("./fuentes/Roboto.ttf", float64(size)); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
// Fijar color del texto y mostrar el titulo
// usando un margen definido - centrando el texto mostrado
// area con el Area del Titulo
portada.SetColor(colorTextoTitulo)
portada.DrawStringWrapped(titulo,
float64(portada.Width())/2, float64(AltoAreaTitulo)/2+200, // x, y
0.5, 0.5,
float64(portada.Width()-(margin*2)),
1.5, // alto linea
gg.AlignLeft)
}
func PonerTextoResumen(resumen string, margin int, size int) {
// Leer el fichero de la fuente ttf y fijar tamaño fuente
if err := portada.LoadFontFace("./fuentes/Roboto.ttf", float64(size)); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
// Fijar color del texto y mostrar el titulo
// usando un margen definido - centrando el texto mostrado
// area con el Area del Titulo
portada.SetColor(colorTextoResumen)
portada.DrawStringWrapped(resumen,
float64(portada.Width())/2, float64(80), // x, y
0.5, 0.5,
float64(portada.Width()-(margin*2)),
1.5, // alto linea
gg.AlignLeft)
}
func PonerTextoAutor(autor string, size int) {
// Leer el fichero de la fuente ttf y fijar tamaño fuente
if err := portada.LoadFontFace("./fuentes/Bebas.ttf", float64(size)); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
// Fijar color y situarlo (si amarillo autor en negro)
if colorTema == colorAmarillo {
portada.SetColor(colorNegro)
} else {
portada.SetColor(colorTema)
}
portada.DrawString(autor, float64(pagAncho/3*2), float64(pagAlto-130))
}
func PonerTextoEditorial(editorial string, size int) {
// Leer el fichero de la fuente ttf y fijar tamaño fuente
if err := portada.LoadFontFace("./fuentes/Sketch.ttf", float64(size)); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
// Fijar color y situarlo
portada.SetColor(colorTema)
portada.DrawString(editorial, float64(140), float64(pagAlto-130))
}
func SavePNG(imagen image.Image, fichero string) error {
f, err := os.Create(fichero)
if err != nil {
return err
}
defer f.Close()
if err := png.Encode(f, imagen); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
return nil
}