classic_song_template.py 4.3 KB

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