classic_song_slide.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.font import Font
  15. from wand.color import Color
  16. import config as const
  17. from utils import get_empty_image
  18. from .song_slide import SongSlide
  19. class ClassicSongSlide(SongSlide):
  20. def get_slide(
  21. self,
  22. template_img: Image,
  23. slide_text: str,
  24. song_structure: list,
  25. index: int,
  26. use_arrow: bool,
  27. ) -> Image:
  28. canvas_img, font_size = self.get_text_canvas(slide_text)
  29. verse_or_chorus = song_structure[index]
  30. bg_img = template_img.clone()
  31. if "R" not in verse_or_chorus:
  32. bg_img.composite(
  33. self.get_index(verse_or_chorus, font_size),
  34. top=const.STRUCTURE_ELEMENT_Y,
  35. left=const.STRUCTURE_ELEMENT_X,
  36. )
  37. bg_img.composite(
  38. canvas_img, top=const.TEXT_CANVAS_Y, left=const.TEXT_CANVAS_X
  39. )
  40. if use_arrow:
  41. bg_img.composite(
  42. self.get_arrow(), top=const.ARROW_Y, left=const.ARROW_X
  43. )
  44. bg_img.composite(
  45. self.get_structure_info_display(song_structure, index),
  46. top=const.INFODISPLAY_Y,
  47. left=const.INFODISPLAY_X,
  48. )
  49. return bg_img.clone()
  50. def get_arrow(self) -> Image:
  51. with Drawing() as draw:
  52. draw.stroke_width = 1
  53. draw.stroke_color = Color(const.ARROW_COLOR)
  54. draw.fill_color = Color(const.ARROW_COLOR)
  55. arrow_width = const.ARROW_HEIGHT * 3 // 2
  56. draw.path_start()
  57. draw.path_move(
  58. to=(0, const.ARROW_HEIGHT / 2 - const.ARROW_HEIGHT / 10)
  59. )
  60. draw.path_line(
  61. to=(0, const.ARROW_HEIGHT / 2 + const.ARROW_HEIGHT / 10)
  62. )
  63. draw.path_line(
  64. to=(
  65. arrow_width / 3 * 2,
  66. const.ARROW_HEIGHT / 2 + const.ARROW_HEIGHT / 10,
  67. )
  68. )
  69. draw.path_line(to=(arrow_width / 3 * 2, const.ARROW_HEIGHT))
  70. draw.path_line(to=(arrow_width, const.ARROW_HEIGHT / 2))
  71. draw.path_line(to=(arrow_width / 3 * 2, 0))
  72. draw.path_line(
  73. to=(
  74. arrow_width / 3 * 2,
  75. const.ARROW_HEIGHT / 2 - const.ARROW_HEIGHT / 10,
  76. )
  77. )
  78. draw.path_close()
  79. draw.path_finish()
  80. with Image(
  81. width=arrow_width,
  82. height=const.ARROW_HEIGHT,
  83. background=Color(const.BG_COLOR),
  84. ) as image:
  85. draw(image)
  86. return image.clone()
  87. def get_text_canvas(self, slide_text: str) -> tuple[Image, int]:
  88. font_size = const.MAX_CANVAS_FONT_SIZE
  89. while font_size >= const.MIN_CANVAS_FONT_SIZE:
  90. with Drawing() as draw:
  91. draw.fill_color = Color(const.TEXT_COLOR)
  92. draw.text_interline_spacing = const.INTERLINE_SPACING
  93. draw.font_size = font_size
  94. draw.font = const.FONT
  95. draw.text(0, font_size, slide_text)
  96. with Image(
  97. width=const.WIDTH,
  98. height=const.HEIGHT,
  99. background=Color(const.BG_COLOR),
  100. ) as img:
  101. draw(img)
  102. img.trim()
  103. if (
  104. img.width > const.TEXT_CANVAS_WIDTH
  105. or img.height > const.TEXT_CANVAS_HEIGHT
  106. ):
  107. font_size -= const.CANVAS_FONT_SIZE_STEP
  108. else:
  109. return img.clone(), font_size
  110. return get_empty_image(), 0
  111. def get_structure_info_display(self, structure: list, index: int) -> Image:
  112. with Drawing() as draw:
  113. draw.fill_color = Color(const.TEXT_COLOR)
  114. draw.font_size = const.INFODISPLAY_FONT_SIZE
  115. draw.font = const.FONT
  116. for current_index, item in enumerate(structure):
  117. if current_index == index:
  118. draw.font = const.BOLD_FONT
  119. draw.text(
  120. current_index * const.INFODISPLAY_ITEM_WIDTH,
  121. const.INFODISPLAY_FONT_SIZE,
  122. item,
  123. )
  124. draw.font = const.FONT
  125. else:
  126. draw.text(
  127. current_index * const.INFODISPLAY_ITEM_WIDTH,
  128. const.INFODISPLAY_FONT_SIZE,
  129. item,
  130. )
  131. with Image(
  132. width=const.WIDTH,
  133. height=const.HEIGHT,
  134. background=Color(const.BG_COLOR),
  135. ) as img:
  136. draw(img)
  137. img.trim()
  138. return img.clone()
  139. def get_index(self, verse: str, font_size: int) -> Image:
  140. with Image(
  141. width=const.WIDTH,
  142. height=const.HEIGHT,
  143. background=Color(const.BG_COLOR),
  144. ) as img:
  145. img.caption(
  146. verse + ".",
  147. font=Font(
  148. const.FONT_PATH,
  149. size=font_size,
  150. color=Color(const.TEXT_COLOR),
  151. ),
  152. )
  153. img.trim()
  154. return img.clone()