<commit_before>module Slapme
  module Utils
    module RmagickCroppedText
      # cropped rendering courtesy of http://blog.choonkeat.com/weblog/2007/02/rmagick-renderi.html
      # untest though :(
      def render_cropped_text(caption_text, width_constraint, height_constraint, &block)
        image = render_text(caption_text, width_constraint, &block)
        if height_constraint < image.rows
          percent = height_constraint.to_f / image.rows.to_f
          end_index = (caption_text.size * percent).to_i  # takes a leap into cropping
          image = render_text(caption_text[0..end_index] + "...", width_constraint, &block)
          while height_constraint < image.rows && end_index > 0 # reduce in small chunks until within range
            end_index -= 80
            image = render_text(caption_text[0..end_index] + "...", width_constraint, &block)
          end
          while height_constraint > image.rows                  # lengthen in smaller steps until exceed
            end_index += 10
            image = render_text(caption_text[0..end_index] + "...", width_constraint, &block)
          end
          while height_constraint < image.rows && end_index > 0 # reduce in baby steps until fit
            end_index -= 1
            image = render_text(caption_text[0..end_index] + "...", width_constraint, &block)
          end
        end
        image
      end

      def render_text(caption_text, width_constraint, &block)
        Magick::Image.read("caption:#{caption_text.to_s}") {
          self.size = width_constraint
          block.call(self) if block_given?
        }.first
      end
    end
  end
end<commit_msg>use quite smaller steps to avoid breaking loops earlier<commit_after>module Slapme
  module Utils
    module RmagickCroppedText
      # cropped rendering courtesy of http://blog.choonkeat.com/weblog/2007/02/rmagick-renderi.html
      # untest though :(
      def render_cropped_text(caption_text, width_constraint, height_constraint, &block)
        image = render_text(caption_text, width_constraint, &block)
        if height_constraint < image.rows
          percent = height_constraint.to_f / image.rows.to_f
          end_index = (caption_text.size * percent).to_i  # takes a leap into cropping
          image = render_text(caption_text[0..end_index] + "...", width_constraint, &block)
          while height_constraint < image.rows && end_index > 0 # reduce in small chunks until within range
            end_index -= 5
            image = render_text(caption_text[0..end_index] + "...", width_constraint, &block)
          end
          while height_constraint > image.rows                  # lengthen in smaller steps until exceed
            end_index += 3
            image = render_text(caption_text[0..end_index] + "...", width_constraint, &block)
          end
          while height_constraint < image.rows && end_index > 0 # reduce in baby steps until fit
            end_index -= 1
            image = render_text(caption_text[0..end_index] + "...", width_constraint, &block)
          end
        end
        image
      end

      def render_text(caption_text, width_constraint, &block)
        Magick::Image.read("caption:#{caption_text.to_s}") {
          self.size = width_constraint
          block.call(self) if block_given?
        }.first
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.09270334928229665, 'fraction_numerical': 0.009569377990430622, 'mean_word_length': 2.375378405650858, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6188735', 'n_tokens_mistral': 969, 'n_tokens_neox': 936, 'n_words': 229}