summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRandy Morgan <[email protected]>2012-04-16 11:07:45 +0900
committerRandy Morgan <[email protected]>2012-04-16 11:07:45 +0900
commite03b79e8f83f943f7caa01dd15fa6c5c87bcc4a2 (patch)
treec75eff8b0a5b71b8b1c94352def4b5263294169f
parentd7010e4c748b682ba756149e54db04d296db6d1a (diff)
downloadcaxlsx-e03b79e8f83f943f7caa01dd15fa6c5c87bcc4a2.tar.gz
caxlsx-e03b79e8f83f943f7caa01dd15fa6c5c87bcc4a2.zip
The beginning of the end of RMagick! Still needs lots of testing but seems comparable to the current autowidth calculations, is much faster than RMagick and more than anything - it removes a dependency on a Gem that is a huge pain in the ass.
-rw-r--r--README.md5
-rw-r--r--axlsx.gemspec2
-rw-r--r--lib/axlsx/workbook/worksheet/worksheet.rb22
3 files changed, 10 insertions, 19 deletions
diff --git a/README.md b/README.md
index 80c177ee..8d2b6e91 100644
--- a/README.md
+++ b/README.md
@@ -34,10 +34,6 @@ http://github.com/randym/acts_as_xlsx
There are guides for using axlsx and acts_as_xlsx here:
[http://axlsx.blogspot.com](http://axlsx.blogspot.com)
-Help Wanted
------------
-
-I'd really like to get rid of the dependency on RMagick in this gem. RMagic is being used to calculate the column widths in a worksheet based on the content the user specified. If there happens to be anyone out there with the background and c skills to write an extension that can determine the width of a single character rendered with a specific font at a specific font size please give me a shout.
Feature List
------------
@@ -473,6 +469,7 @@ This gem has 100% test coverage using test/unit. To execute tests for this gem,
---------
- ** April.?.12**: 1.1.1 release
- bugfix for autowidth calculations across multiple rows
+ - REMOVED RMAGICK dependency WOOT!
- ** April.3.12**: 1.1.0 release
- bugfix patch name_to_indecies to properly handle extended ranges.
- bugfix properly serialize chart title.
diff --git a/axlsx.gemspec b/axlsx.gemspec
index 7a3844b3..5e59195f 100644
--- a/axlsx.gemspec
+++ b/axlsx.gemspec
@@ -19,8 +19,6 @@ Gem::Specification.new do |s|
s.test_files = Dir.glob("{test/**/*}")
s.add_runtime_dependency 'nokogiri', '>= 1.4.1'
- s.add_runtime_dependency 'rmagick', '>= 2.12.2' unless Object.const_defined? :JRUBY_VERSION
- s.add_runtime_dependency 'rmagick4j', '>= 0.3.7' if Object.const_defined? :JRUBY_VERSION
s.add_runtime_dependency 'rubyzip', '>= 0.9.5'
# REQUIRED by Travis-ci please do not alter these lines
diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb
index d52ec1da..f185c9e7 100644
--- a/lib/axlsx/workbook/worksheet/worksheet.rb
+++ b/lib/axlsx/workbook/worksheet/worksheet.rb
@@ -88,6 +88,7 @@ module Axlsx
# @option options [Hash] page_margins A hash containing page margins for this worksheet. @see PageMargins
# @option options [Boolean] show_gridlines indicates if gridlines should be shown for this sheet.
def initialize(wb, options={})
+ @thin_chars = ".acefijklrstxyzFIJL()-"
self.workbook = wb
@workbook.worksheets << self
@@ -105,17 +106,9 @@ module Axlsx
# @cols = SimpleTypedList.new Cell
@tables = SimpleTypedList.new Table
- if self.workbook.use_autowidth
- require 'RMagick' unless defined?(Magick)
- @magick_draw = Magick::Draw.new
- else
- @magick_draw = nil
- end
-
options.each do |o|
self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
end
-
end
# convinience method to access all cells in this worksheet
@@ -466,7 +459,6 @@ module Axlsx
width = widths[index]
col.width = width if [Integer, Float, Fixnum].include?(width.class)
c_style = style[index] if [Integer, Fixnum].include?(style[index].class)
- #BUG - col.width wil only be nil the first time the column object is created. Subsequent row adds will not update the width of the column! col.width ||
next if width == :ignore || (cell.value.is_a?(String) && cell.value.start_with?('=') || cell.value == nil)
if self.workbook.use_autowidth
cell_xf = cellXfs[(c_style || 0)]
@@ -477,11 +469,15 @@ module Axlsx
end
end
+
def calculate_width(text, sz)
- mdw_count, font_scale, mdw = 0, sz/11.0, 6.0
- mdw_count = text.scan(/./mu).reduce(0) do | count, char |
- count +=1 if @magick_draw.get_type_metrics(char).max_advance >= mdw
- count
+ mdw_count = 0
+ mdw = 1.78
+ font_scale = sz/10.0
+ text.scan(/./mu).each do |char|
+ #TODO generate < mdw lists for other fonts and calculate widths based on the font in use.
+ # this is all arial for now. I need to workout the width of all characters in 10px font and remove any characters that are equal to or greater than the largest of 0 thru 9
+ mdw_count +=1 unless @thin_chars.include?(char)
end
((mdw_count * mdw + 5) / mdw * 256) / 256.0 * font_scale
end