classic_song_template.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # Copyright © 2024 Noah Vogt <noah@noahvogt.com>
  2. # This program is free software: you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation, either version 3 of the License, or
  5. # (at your option) any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License
  11. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. from wand.image import Image
  13. from wand.drawing import Drawing
  14. from wand.color import Color
  15. from wand.font import Font
  16. from utils import get_empty_image
  17. import config as const
  18. from .song_template import SongTemplate
  19. class ClassicSongTemplate(SongTemplate):
  20. def __init__(self) -> None:
  21. self.song_template = ""
  22. def get_base_image(self) -> Image:
  23. with Image(
  24. width=const.WIDTH,
  25. height=const.HEIGHT,
  26. background=Color(const.BG_COLOR),
  27. ) as img:
  28. return img.clone()
  29. def get_titlebar_rectangle(self, text: str) -> Image:
  30. font_size = const.MAX_TITLE_FONT_SIZE
  31. while font_size >= const.MIN_TITLE_FONT_SIZE:
  32. with Image(
  33. width=const.WIDTH,
  34. height=const.TITLE_HEIGHT,
  35. background=Color(const.FG_COLOR),
  36. ) as img:
  37. img.caption(
  38. text,
  39. font=Font(
  40. const.BOLD_FONT_PATH,
  41. size=font_size,
  42. color=Color(const.TITLE_COLOR),
  43. ),
  44. )
  45. img.trim()
  46. img.border(color=Color(const.FG_COLOR), width=30, height=0)
  47. trimmed_img_width = img.width
  48. trimmed_img_height = img.height
  49. concat_height = int(
  50. (const.TITLE_HEIGHT - trimmed_img_height) / 2
  51. )
  52. correction_heigt = (
  53. const.TITLEBAR_TRIANGLE_HEIGTH
  54. - trimmed_img_height
  55. - (2 * concat_height)
  56. )
  57. concatenated_img = Image(
  58. width=trimmed_img_width,
  59. height=concat_height,
  60. background=Color(const.FG_COLOR),
  61. )
  62. concatenated_img.sequence.append(img)
  63. concatenated_img.sequence.append(
  64. Image(
  65. width=trimmed_img_width,
  66. height=concat_height + correction_heigt,
  67. background=Color(const.FG_COLOR),
  68. )
  69. )
  70. concatenated_img.concat(stacked=True)
  71. if concatenated_img.width > (
  72. const.WIDTH
  73. - const.PLAYER_WIDTH
  74. - const.TITLEBAR_TRIANGLE_WIDTH
  75. ):
  76. font_size -= const.TITLE_FONT_SIZE_STEP
  77. continue
  78. return concatenated_img.clone()
  79. return get_empty_image()
  80. def get_template(self, title: str) -> Image:
  81. titlebar_rectangle = self.get_titlebar_rectangle(title)
  82. titlebar_rectangle.sequence.append(self.get_titlebar_triangle())
  83. titlebar_rectangle.concat(stacked=False)
  84. base_img = self.get_base_image()
  85. base_img.composite(titlebar_rectangle, top=const.TITLEBAR_Y)
  86. self.song_template = base_img.clone()
  87. return base_img.clone()
  88. def get_titlebar_triangle(self) -> Image:
  89. with Drawing() as draw:
  90. draw.fill_color = Color(const.FG_COLOR)
  91. draw.path_start()
  92. draw.path_move(to=(const.TITLEBAR_TRIANGLE_WIDTH, 0))
  93. draw.path_line(to=(0, 0))
  94. draw.path_line(to=(0, const.TITLEBAR_TRIANGLE_HEIGTH))
  95. draw.path_close()
  96. draw.path_finish()
  97. with Image(
  98. width=const.TITLEBAR_TRIANGLE_WIDTH,
  99. height=const.TITLEBAR_TRIANGLE_HEIGTH,
  100. background=Color(const.BG_COLOR),
  101. ) as img:
  102. draw(img)
  103. return img.clone()