Convert WriteFreely quotes to Instagram / PixelFed posts.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

125 linhas
2.9 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "image/color"
  5. "os"
  6. "path/filepath"
  7. "time"
  8. "github.com/fogleman/gg"
  9. "github.com/writeas/web-core/log"
  10. )
  11. func main() {
  12. log.Info("Starting...")
  13. start := time.Now()
  14. err := run()
  15. if err != nil {
  16. log.Error("%s", err)
  17. os.Exit(1)
  18. }
  19. log.Info("Completed in %s", time.Since(start))
  20. }
  21. func loadFont(dc *gg.Context, bold bool, points float64) error {
  22. fontLoraBoldPath := filepath.Join("fonts", "Lora-Bold.ttf")
  23. fontLoraPath := filepath.Join("fonts", "Lora-Regular.ttf")
  24. path := fontLoraPath
  25. if bold {
  26. path = fontLoraBoldPath
  27. }
  28. err := dc.LoadFontFace(path, points)
  29. if err != nil {
  30. return fmt.Errorf("load bold font: %s", err)
  31. }
  32. return nil
  33. }
  34. func run() error {
  35. w := 900
  36. h := 900
  37. wf := float64(w)
  38. hf := float64(h)
  39. dc := gg.NewContext(w, h)
  40. dc.DrawRectangle(0, 0, wf, hf)
  41. dc.SetRGB(1, 1, 1)
  42. dc.Fill()
  43. // Define margins for footer text
  44. footerFontSize := 32.0
  45. footerMargin := 20.0
  46. x := footerMargin
  47. y := footerMargin
  48. footerMarginY := 20.0
  49. // Content parameters
  50. contentFontSize := 48.0
  51. lineSpacing := 1.8
  52. contentBottomMargin := 100.0
  53. contentRightMargin := 50.0
  54. contentTopMargin := 50.0
  55. contentWidth := wf - contentRightMargin - contentRightMargin
  56. // Create bold instance name
  57. err := loadFont(dc, true, footerFontSize)
  58. if err != nil {
  59. return err
  60. }
  61. instance := "write.as"
  62. baseTextWidth, textHeight := dc.MeasureString(instance)
  63. // Create user path
  64. err = loadFont(dc, false, footerFontSize)
  65. if err != nil {
  66. return err
  67. }
  68. dc.SetColor(color.Black)
  69. userPath := "/matt"
  70. userTextWidth, _ := dc.MeasureString(userPath)
  71. // x = canvas halfway point - total text width halfway point
  72. x = wf/2 - (baseTextWidth+userTextWidth)/2
  73. y = hf - textHeight - footerMarginY
  74. err = loadFont(dc, true, footerFontSize)
  75. if err != nil {
  76. return err
  77. }
  78. dc.DrawString(instance, x, y)
  79. // x = original x coordinate + base text width
  80. x += baseTextWidth
  81. y = hf - textHeight - footerMarginY
  82. err = loadFont(dc, false, footerFontSize)
  83. if err != nil {
  84. return err
  85. }
  86. dc.DrawString(userPath, x, y)
  87. // Draw the content
  88. err = loadFont(dc, false, contentFontSize)
  89. if err != nil {
  90. return err
  91. }
  92. s := "The rest of the travelers in our flying bus napped or stared listlessly at a shiny slab in their lap and the staring yellow orb morphed into a full circle out in the blue. As we banked to the right — a nod to its awakening — it seemed to rest in acknowledgement, hanging for a moment on the invisible horizon."
  93. lines := dc.WordWrap(s, contentWidth)
  94. linesStr := ""
  95. for i, str := range lines {
  96. linesStr += str
  97. if i != len(lines)-1 {
  98. linesStr += "\n"
  99. }
  100. }
  101. _, contentTextHeight := dc.MeasureMultilineString(linesStr, lineSpacing)
  102. x = contentRightMargin
  103. y = contentTopMargin - contentBottomMargin + hf/2 - contentTextHeight/2
  104. dc.DrawStringWrapped(s, x, y, 0, 0, contentWidth, lineSpacing, gg.AlignLeft)
  105. err = dc.SavePNG("out.png")
  106. if err != nil {
  107. return fmt.Errorf("save png: %s", err)
  108. }
  109. return nil
  110. }