<commit_before>module Gruff
  module Mini
    module Legend
      
      attr_accessor :hide_mini_legend
      
      ##
      # The canvas needs to be bigger so we can put the legend beneath it.

      def expand_canvas_for_vertical_legend
        return if @hide_mini_legend
        
        @original_rows = @raw_rows
        @rows += @data.length * calculate_caps_height(scale_fontsize(@legend_font_size)) * 1.7
        render_background
      end
      
      ##
      # Draw the legend beneath the existing graph.

      def draw_vertical_legend
        return if @hide_mini_legend
        
        @legend_labels = @data.collect {|item| item[Gruff::Base::DATA_LABEL_INDEX] }
        
        legend_square_width = 40.0 # small square with color of this item
        legend_square_margin = 10.0
        @legend_left_margin = 100.0
        legend_top_margin = 40.0

        # May fix legend drawing problem at small sizes
        @d.font = @font if @font
        @d.pointsize = @legend_font_size

        current_x_offset = @legend_left_margin
        current_y_offset = @original_rows + legend_top_margin

        debug { @d.line 0.0, current_y_offset, @raw_columns, current_y_offset }

        @legend_labels.each_with_index do |legend_label, index|        

          # Draw label
          @d.fill = @font_color
          @d.font = @font if @font
          @d.pointsize = scale_fontsize(@legend_font_size)
          @d.stroke = 'transparent'
          @d.font_weight = Magick::NormalWeight
          @d.gravity = Magick::WestGravity
          @d = @d.annotate_scaled( @base_image, 
                            @raw_columns, 1.0,
                            current_x_offset + (legend_square_width * 1.7), current_y_offset, 
                            truncate_legend_label(legend_label), @scale)

          # Now draw box with color of this dataset
          @d = @d.stroke 'transparent'
          @d = @d.fill @data[index][Gruff::Base::DATA_COLOR_INDEX]
          @d = @d.rectangle(current_x_offset, 
                            current_y_offset - legend_square_width / 2.0, 
                            current_x_offset + legend_square_width, 
                            current_y_offset + legend_square_width / 2.0)
          
          current_y_offset += calculate_caps_height(@legend_font_size) * 1.7
        end
        @color_index = 0
      end

      ##
      # Shorten long labels so they will fit on the canvas.
      #
      #   Department of Hu...
      
      def truncate_legend_label(label)
        truncated_label = label.to_s
        while calculate_width(scale_fontsize(@legend_font_size), truncated_label) > (@columns - @legend_left_margin - @right_margin) && (truncated_label.length > 1)
          truncated_label = truncated_label[0..truncated_label.length-2]
        end
        truncated_label + (truncated_label.length < label.to_s.length ? "…" : '')
      end
      
    end
  end
end<commit_msg>Removed Unicode ellipsis for Ruby 1.9 compatibility [<NAME>]<commit_after>module Gruff
  module Mini
    module Legend
      
      attr_accessor :hide_mini_legend
      
      ##
      # The canvas needs to be bigger so we can put the legend beneath it.

      def expand_canvas_for_vertical_legend
        return if @hide_mini_legend
        
        @original_rows = @raw_rows
        @rows += @data.length * calculate_caps_height(scale_fontsize(@legend_font_size)) * 1.7
        render_background
      end
      
      ##
      # Draw the legend beneath the existing graph.

      def draw_vertical_legend
        return if @hide_mini_legend
        
        @legend_labels = @data.collect {|item| item[Gruff::Base::DATA_LABEL_INDEX] }
        
        legend_square_width = 40.0 # small square with color of this item
        legend_square_margin = 10.0
        @legend_left_margin = 100.0
        legend_top_margin = 40.0

        # May fix legend drawing problem at small sizes
        @d.font = @font if @font
        @d.pointsize = @legend_font_size

        current_x_offset = @legend_left_margin
        current_y_offset = @original_rows + legend_top_margin

        debug { @d.line 0.0, current_y_offset, @raw_columns, current_y_offset }

        @legend_labels.each_with_index do |legend_label, index|        

          # Draw label
          @d.fill = @font_color
          @d.font = @font if @font
          @d.pointsize = scale_fontsize(@legend_font_size)
          @d.stroke = 'transparent'
          @d.font_weight = Magick::NormalWeight
          @d.gravity = Magick::WestGravity
          @d = @d.annotate_scaled( @base_image, 
                            @raw_columns, 1.0,
                            current_x_offset + (legend_square_width * 1.7), current_y_offset, 
                            truncate_legend_label(legend_label), @scale)

          # Now draw box with color of this dataset
          @d = @d.stroke 'transparent'
          @d = @d.fill @data[index][Gruff::Base::DATA_COLOR_INDEX]
          @d = @d.rectangle(current_x_offset, 
                            current_y_offset - legend_square_width / 2.0, 
                            current_x_offset + legend_square_width, 
                            current_y_offset + legend_square_width / 2.0)
          
          current_y_offset += calculate_caps_height(@legend_font_size) * 1.7
        end
        @color_index = 0
      end

      ##
      # Shorten long labels so they will fit on the canvas.
      #
      #   Department of Hu...
      
      def truncate_legend_label(label)
        truncated_label = label.to_s
        while calculate_width(scale_fontsize(@legend_font_size), truncated_label) > (@columns - @legend_left_margin - @right_margin) && (truncated_label.length > 1)
          truncated_label = truncated_label[0..truncated_label.length-2]
        end
        truncated_label + (truncated_label.length < label.to_s.length ? "..." : '')
      end
      
    end
  end
end
{'fraction_non_alphanumeric': 0.08095318573601487, 'fraction_numerical': 0.010816292039885076, 'mean_word_length': 1.9798590130916416, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 8, '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': '2193220', 'n_tokens_mistral': 1857, 'n_tokens_neox': 1729, 'n_words': 383}