Now I write python functions which adds text to a image. Modify the function so that if the input text is too long, the function can automatically shrink it to a size that would allow the text to fit within the specified area. But save all existing feautres like different color for random words etc.
Code:
def draw_text(draw, text, font, default_text_color, text_area_width, text_area_height, start_x, start_y, shadow_color, shadow_offset, line_spacing, color_ratio=0.2, alternate_color='orange'):
  text = text.upper()
  words = text.split()

  # Determine the number of words to color differently, based on color_ratio
  num_words_to_color = max(1, int(len(words) * color_ratio))
  # Randomly pick unique words to color differently
  words_to_color = set(random.sample(words, num_words_to_color))

  lines = textwrap.wrap(text, width=text_area_width // (font.size // 2))

  # Font metrics
  ascent, descent = font.getmetrics()

  # Calculate the total height of the text block with additional line spacing
  total_text_height = len(lines) * (ascent + descent) + (line_spacing * (len(lines) - 1))

  # Initial y offset - start at the vertical center with consistent line spacing
  y_offset = start_y + (text_area_height - total_text_height) // 2

  for line in lines:
      # Calculate the line width for horizontal centering
      line_width = sum(draw.textbbox((0, 0), word + ' ', font=font)[2] for word in line.split())
      x_offset = start_x + (text_area_width - line_width) // 2

      for word in line.split():
          # Determine coordinates for the text and shadow
          shadow_x = x_offset + shadow_offset
          shadow_y = y_offset + shadow_offset
          word_color = alternate_color if word in words_to_color else default_text_color

          # Draw shadow first
          if shadow_offset:
              draw.text((shadow_x, shadow_y), word, font=font, fill=shadow_color)

          # Draw the word itself
          draw.text(
              (x_offset, y_offset),
              word,
              font=font,
              fill=word_color
          )

          # Increment x_offset by the width of the word
          word_width = draw.textbbox((x_offset, y_offset), word, font=font)[2] - draw.textbbox((x_offset, y_offset), word, font=font)[0]
          space_width = draw.textbbox((0, 0), ' ', font=font)[2]
          x_offset += word_width + space_width

      # Increment y_offset by the line height (ascent + descent) plus the additional line spacing
      y_offset += ascent + descent + line_spacing

      if len(lines) > 1:
          y_offset += line_spacing  # add additional space between lines if there's more than one line

def add_image_and_text_to_background_with_mask6(
  background_path, overlay_image_path, mask_path, output_path,
  overlay_area_width, overlay_area_height, 
  text_area_width, text_area_height, text_start_x, text_start_y, 
  text, font_path, initial_font_size, text_color, shadow_color,
  shadow_offset, line_spacing, alternate_color='orange', color_ratio=0.5):

  # Load background and images
  background = Image.open(background_path)
  overlay_image = Image.open(overlay_image_path).resize((overlay_area_width, overlay_area_height), resample=Image.Resampling.LANCZOS)
  mask_image = Image.open(mask_path).resize((overlay_area_width, overlay_area_height), resample=Image.Resampling.LANCZOS)

  # Ensure the background is the correct size (1080x1350)
  assert background.size == (1080, 1350)

  # Paste the overlay image onto the background using the mask
  background.paste(overlay_image, (50, 50), mask_image)

  # Initialize the drawing
  font = ImageFont.truetype(font_path, initial_font_size)
  draw = ImageDraw.Draw(background)

  # Set the seed for reproducibility, if needed
  # random.seed(1)  
  
  draw_text(draw, text, font, initial_font_size, text_color, text_area_width, text_area_height, text_start_x, text_start_y, shadow_color, shadow_offset, line_spacing, color_ratio, alternate_color, min_font_size)

  # Save the resulting image
  background.save(output_path)


# Example usage
add_image_and_text_to_background_with_mask6(
    background_path='BlackBackground.jpg',
    overlay_image_path='OverlayImage.png',
    mask_path='Mask.png',
    output_path='output.jpg',
    overlay_area_width=970,
    overlay_area_height=600,
    text_area_width=820,
    text_area_height=500,
    text_start_x=130,
    text_start_y=740,
    text='Алези: ШумаД,хер бы не хоЙтел такоДЩго напарника, как Хэмилтон Здарова Ну че там как А понял ну норм',
    font_path='PFDinItalic.ttf',
    initial_font_size=50,
    text_color='white',
    shadow_color='black',
    shadow_offset=2,
    line_spacing=5,
    alternate_color='orange',  # Color for randomly selected words
    color_ratio=0.35 # Percentage of words to randomly colo
)