lib/write_xlsx/workbook.rb in write_xlsx-1.02.0 vs lib/write_xlsx/workbook.rb in write_xlsx-1.04.0

- old
+ new

@@ -130,10 +130,11 @@ @image_types = {} @images = [] @strings_to_urls = (options[:strings_to_urls].nil? || options[:strings_to_urls]) ? true : false @max_url_length = 2079 + @has_comments = false if options[:max_url_length] @max_url_length = options[:max_url_length] @max_url_length = 2079 if @max_url_length < 250 end @@ -1113,11 +1114,12 @@ @font_count, @num_format_count, @border_count, @fill_count, @custom_colors, - @dxf_formats + @dxf_formats, + @has_comments ] end def num_vml_files @worksheets.select { |sheet| sheet.has_vml? || sheet.has_header_vml? }.count @@ -1709,15 +1711,18 @@ next if !sheet.has_vml? && !sheet.has_header_vml? if sheet.has_vml? if sheet.has_comments? comment_files += 1 comment_id += 1 + @has_comments = true end vml_drawing_id += 1 - sheet.prepare_vml_objects(vml_data_id, vml_shape_id, - vml_drawing_id, comment_id) + sheet.prepare_vml_objects( + vml_data_id, vml_shape_id, + vml_drawing_id, comment_id + ) # Each VML file should start with a shape id incremented by 1024. vml_data_id += 1 * ( 1 + sheet.num_comments_block ) vml_shape_id += 1024 * ( 1 + sheet.num_comments_block ) end @@ -1736,12 +1741,10 @@ sheet.set_vba_name end end end - add_font_format_for_cell_comments if num_comment_files > 0 - # Set the workbook vba_codename if one of the sheets has a button and # the workbook has a vbaProject binary. if has_button && @vba_project && !@vba_codename set_vba_name end @@ -1757,23 +1760,10 @@ sheets.each do |sheet| table_id += sheet.prepare_tables(table_id + 1, seen) end end - def add_font_format_for_cell_comments - format = Format.new( - @formats, - :font => 'Tahoma', - :size => 8, - :color_indexed => 81, - :font_only => 1 - ) - - format.get_xf_index - @formats.formats << format - end - # # Add "cached" data to charts to provide the numCache and strCache data for # series and title/axis ranges. # def add_chart_data #:nodoc: @@ -1920,13 +1910,16 @@ # # Iterate through the worksheets and set up any chart or image drawings. # def prepare_drawings #:nodoc: - chart_ref_id = 0 - image_ref_id = 0 - drawing_id = 0 + chart_ref_id = 0 + image_ref_id = 0 + drawing_id = 0 + ref_id = 0 + image_ids = {} + header_image_ids = {} @worksheets.each do |sheet| chart_count = sheet.charts.size image_count = sheet.images.size shape_count = sheet.shapes.size header_image_count = sheet.header_images.size @@ -1942,13 +1935,23 @@ has_drawings = true end # Prepare the worksheet images. sheet.images.each_with_index do |image, index| - type, width, height, name, x_dpi, y_dpi = get_image_properties(image[2]) - image_ref_id += 1 - sheet.prepare_image(index, image_ref_id, drawing_id, width, height, name, type, x_dpi, y_dpi) + filename = image[2] + type, width, height, name, x_dpi, y_dpi, md5 = get_image_properties(image[2]) + if image_ids[md5] + ref_id = image_ids[md5] + else + image_ref_id += 1 + image_ids[md5] = ref_id = image_ref_id + @images << [filename, type] + end + sheet.prepare_image( + index, ref_id, drawing_id, width, height, + name, type, x_dpi, y_dpi, md5 + ) end # Prepare the worksheet charts. sheet.charts.each_with_index do |chart, index| chart_ref_id += 1 @@ -1963,31 +1966,47 @@ # Prepare the header images. header_image_count.times do |index| filename = sheet.header_images[index][0] position = sheet.header_images[index][1] - type, width, height, name, x_dpi, y_dpi = + type, width, height, name, x_dpi, y_dpi, md5 = get_image_properties(filename) - image_ref_id += 1 + if header_image_ids[md5] + ref_id = header_image_ids[md5] + else + image_ref_id += 1 + header_image_ids[md5] = ref_id = image_ref_id + @images << [filename, type] + end - sheet.prepare_header_image(image_ref_id, width, height, - name, type, position, x_dpi, y_dpi) + sheet.prepare_header_image( + ref_id, width, height, name, type, + position, x_dpi, y_dpi, md5 + ) end # Prepare the footer images. footer_image_count.times do |index| filename = sheet.footer_images[index][0] position = sheet.footer_images[index][1] - type, width, height, name, x_dpi, y_dpi = + type, width, height, name, x_dpi, y_dpi, md5 = get_image_properties(filename) - image_ref_id += 1 + if header_image_ids[md5] + ref_id = header_image_ids[md5] + else + image_ref_id += 1 + header_image_ids[md5] = ref_id = image_ref_id + @images << [filename, type] + end - sheet.prepare_header_image(image_ref_id, width, height, - name, type, position, x_dpi, y_dpi) + sheet.prepare_header_image( + ref_id, width, height, name, type, + position, x_dpi, y_dpi, md5 + ) end if has_drawings drawings = sheet.drawings @drawings << drawings @@ -2012,10 +2031,11 @@ x_dpi = 96 y_dpi = 96 # Open the image file and import the data. data = File.binread(filename) + md5 = Digest::MD5.hexdigest(data) if data.unpack('x A3')[0] == 'PNG' # Test for PNGs. type, width, height, x_dpi, y_dpi = process_png(data) @image_types[:png] = 1 elsif data.unpack('n')[0] == 0xFFD8 @@ -2029,16 +2049,14 @@ else # TODO. Add Image::Size to support other types. raise "Unsupported image format for file: #{filename}\n" end - @images << [filename, type] - # Set a default dpi for images with 0 dpi. x_dpi = 96 if x_dpi == 0 y_dpi = 96 if y_dpi == 0 - [type, width, height, File.basename(filename), x_dpi, y_dpi] + [type, width, height, File.basename(filename), x_dpi, y_dpi, md5] end # # Extract width and height information from a PNG file. #