classic_song_template.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 .song_template import SongTemplate
  19. from utils import get_empty_image
  20. try:
  21. import config.config as const # pyright: ignore [reportMissingImports]
  22. except ModuleNotFoundError:
  23. import config.default_config as const
  24. class ClassicSongTemplate(SongTemplate):
  25. def __init__(self) -> None:
  26. self.song_template = ""
  27. def get_base_image(self) -> Image:
  28. with Image(
  29. width=const.WIDTH,
  30. height=const.HEIGHT,
  31. background=Color(const.BG_COLOR),
  32. ) as img:
  33. return img.clone()
  34. def get_titlebar_rectangle(self, text: str) -> Image:
  35. font_size = const.MAX_TITLE_FONT_SIZE
  36. while font_size >= const.MIN_TITLE_FONT_SIZE:
  37. with Image(
  38. width=const.WIDTH,
  39. height=const.TITLE_HEIGHT,
  40. background=Color(const.FG_COLOR),
  41. ) as img:
  42. img.caption(
  43. text,
  44. font=Font(
  45. const.BOLD_FONT_PATH,
  46. size=font_size,
  47. color=Color(const.TITLE_COLOR),
  48. ),
  49. )
  50. img.trim()
  51. img.border(color=Color(const.FG_COLOR), width=30, height=0)
  52. trimmed_img_width = img.width
  53. trimmed_img_height = img.height
  54. concat_height = int(
  55. (const.TITLE_HEIGHT - trimmed_img_height) / 2
  56. )
  57. correction_heigt = (
  58. const.TRIANGLE_HEIGTH
  59. - trimmed_img_height
  60. - (2 * concat_height)
  61. )
  62. concatenated_img = Image(
  63. width=trimmed_img_width,
  64. height=concat_height,
  65. background=Color(const.FG_COLOR),
  66. )
  67. concatenated_img.sequence.append(img)
  68. concatenated_img.sequence.append(
  69. Image(
  70. width=trimmed_img_width,
  71. height=concat_height + correction_heigt,
  72. background=Color(const.FG_COLOR),
  73. )
  74. )
  75. concatenated_img.concat(stacked=True)
  76. if concatenated_img.width > (
  77. const.WIDTH - const.PLAYER_WIDTH - const.TRIANGLE_WIDTH
  78. ):
  79. font_size -= const.TITLE_FONT_SIZE_STEP
  80. continue
  81. return concatenated_img.clone()
  82. return get_empty_image()
  83. def get_template(self, title: str) -> Image:
  84. titlebar_rectangle = self.get_titlebar_rectangle(title)
  85. titlebar_rectangle.sequence.append(self.get_titlebar_triangle())
  86. titlebar_rectangle.concat(stacked=False)
  87. base_img = self.get_base_image()
  88. base_img.composite(titlebar_rectangle, top=const.TITLEBAR_Y)
  89. self.song_template = base_img.clone()
  90. return base_img.clone()
  91. def get_titlebar_triangle(self) -> Image:
  92. with Drawing() as draw:
  93. draw.fill_color = Color(const.FG_COLOR)
  94. draw.path_start()
  95. draw.path_move(to=(const.TRIANGLE_WIDTH, 0))
  96. draw.path_line(to=(0, 0))
  97. draw.path_line(to=(0, const.TRIANGLE_HEIGTH))
  98. draw.path_close()
  99. draw.path_finish()
  100. with Image(
  101. width=const.TRIANGLE_WIDTH,
  102. height=const.TRIANGLE_HEIGTH,
  103. background=Color(const.BG_COLOR),
  104. ) as img:
  105. draw(img)
  106. return img.clone()