summaryrefslogtreecommitdiffhomepage
path: root/lib
diff options
context:
space:
mode:
authorTobias Egli <[email protected]>2022-02-02 16:00:32 +0100
committerJosef Šimánek <[email protected]>2022-02-06 00:37:27 +0100
commitd3cf7bca5728b166c7643ad839efeba60ed0e256 (patch)
tree9175590f74a43cd394d22b03da1788eef7a7d2af /lib
parent059c9d38c8860775a260711f78222b69535e6163 (diff)
downloadcaxlsx-d3cf7bca5728b166c7643ad839efeba60ed0e256.tar.gz
caxlsx-d3cf7bca5728b166c7643ad839efeba60ed0e256.zip
Autowidth cell calculation is now configurable
On the workbook you can now configure the font_scale_divisor and the bold_font_multiplier to get better results for the automatic cell width calculationb
Diffstat (limited to 'lib')
-rw-r--r--lib/axlsx/workbook/workbook.rb25
-rw-r--r--lib/axlsx/workbook/worksheet/cell.rb5
-rw-r--r--lib/axlsx/workbook/worksheet/col.rb8
-rw-r--r--lib/axlsx/workbook/worksheet/worksheet.rb3
4 files changed, 34 insertions, 7 deletions
diff --git a/lib/axlsx/workbook/workbook.rb b/lib/axlsx/workbook/workbook.rb
index 48b7f882..edf719d1 100644
--- a/lib/axlsx/workbook/workbook.rb
+++ b/lib/axlsx/workbook/workbook.rb
@@ -85,6 +85,9 @@ require 'axlsx/workbook/worksheet/selection.rb'
# *workbookPr is only supported to the extend of date1904
class Workbook
+ BOLD_FONT_MULTIPLIER = 1.5
+ FONT_SCALE_DIVISOR = 10.0
+
# When true, the Package will be generated with a shared string table. This may be required by some OOXML processors that do not
# adhere to the ECMA specification that dictates string may be inline in the sheet.
# Using this option will increase the time required to serialize the document as every string in every cell must be analzed and referenced.
@@ -213,6 +216,8 @@ require 'axlsx/workbook/worksheet/selection.rb'
@use_autowidth = true
+ @bold_font_multiplier = BOLD_FONT_MULTIPLIER
+ @font_scale_divisor = FONT_SCALE_DIVISOR
self.date1904= !options[:date1904].nil? && options[:date1904]
yield self if block_given?
@@ -243,6 +248,26 @@ require 'axlsx/workbook/worksheet/selection.rb'
# see @use_autowidth
def use_autowidth=(v=true) Axlsx::validate_boolean v; @use_autowidth = v; end
+ # Font size of bold fonts is multiplied with this
+ # Used for automatic calculation of cell widths with bold text
+ # @return [Float]
+ attr_reader :bold_font_multiplier
+
+ def bold_font_multiplier=(v)
+ Axlsx::validate_float v
+ @bold_font_multiplier = v
+ end
+
+ # Font scale is calculated with this value (font_size / font_scale_divisor)
+ # Used for automatic calculation of cell widths
+ # @return [Float]
+ attr_reader :font_scale_divisor
+
+ def font_scale_divisor=(v)
+ Axlsx::validate_float v
+ @font_scale_divisor = v
+ end
+
# inserts a worksheet into this workbook at the position specified.
# It the index specified is out of range, the worksheet will be added to the end of the
# worksheets collection
diff --git a/lib/axlsx/workbook/worksheet/cell.rb b/lib/axlsx/workbook/worksheet/cell.rb
index 51a14494..3277b8c5 100644
--- a/lib/axlsx/workbook/worksheet/cell.rb
+++ b/lib/axlsx/workbook/worksheet/cell.rb
@@ -409,7 +409,7 @@ module Axlsx
# This is still not perfect...
# - scaling is not linear as font sizes increase
def string_width(string, font_size)
- font_scale = font_size / 10.0
+ font_scale = font_size / row.worksheet.workbook.font_scale_divisor
(string.to_s.size + 3) * font_scale
end
@@ -418,8 +418,9 @@ module Axlsx
# imagemagick and loading metrics for every character.
def font_size
return sz if sz
+
font = styles.fonts[styles.cellXfs[style].fontId] || styles.fonts[0]
- (font.b || (defined?(@b) && @b)) ? (font.sz * 1.5) : font.sz
+ font.b || (defined?(@b) && @b) ? (font.sz * row.worksheet.workbook.bold_font_multiplier) : font.sz
end
# Utility method for setting inline style attributes
diff --git a/lib/axlsx/workbook/worksheet/col.rb b/lib/axlsx/workbook/worksheet/col.rb
index 95204028..3b3775c4 100644
--- a/lib/axlsx/workbook/worksheet/col.rb
+++ b/lib/axlsx/workbook/worksheet/col.rb
@@ -125,12 +125,12 @@ module Axlsx
# to this value and the cell's attributes are ignored.
# @param [Boolean] use_autowidth If this is false, the cell's
# autowidth value will be ignored.
- def update_width(cell, fixed_width=nil, use_autowidth=true)
+ def update_width(cell, fixed_width = nil, use_autowidth = true)
if fixed_width.is_a? Numeric
- self.width = fixed_width
+ self.width = fixed_width
elsif use_autowidth
- cell_width = cell.autowidth
- self.width = cell_width unless (width || 0) > (cell_width || 0)
+ cell_width = cell.autowidth
+ self.width = cell_width unless (width || 0) > (cell_width || 0)
end
end
diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb
index cc0e56ae..adb9839f 100644
--- a/lib/axlsx/workbook/worksheet/worksheet.rb
+++ b/lib/axlsx/workbook/worksheet/worksheet.rb
@@ -762,11 +762,12 @@ module Axlsx
def workbook=(v) DataTypeValidator.validate "Worksheet.workbook", Workbook, v; @workbook = v; end
- def update_column_info(cells, widths=nil)
+ def update_column_info(cells, widths = nil)
cells.each_with_index do |cell, index|
width = widths ? widths[index] : nil
col = find_or_create_column_info(index)
next if width == :ignore
+
col.update_width(cell, width, workbook.use_autowidth)
end
end