module BerkeleyLibrary::TIND::Export::ColumnWidthCalculator

Calculates approximate column widths for cell values, based on Arial average character widths ()in units of 1/1000 point size) per {www.math.utah.edu/~beebe/fonts/afm-widths.html this table}. (LibreOffice default is Liberation Sans, which should match Arial.)

CJK and fullwidth characters will probably be mapped to another font, but it's probably going to be roughly square.

Non-Western, non-CJK characters will hopefully not be much wider than their Western counterparts.

Constants

WIDTHS
WIDTH_CJK

Measured empirically in LibreOffice 6.4.7.2

WIDTH_DEFAULT

See {WIDTHS}

WIDTH_DIGIT
WIDTH_LOWER
WIDTH_UNIT
WIDTH_UPPER

Public Instance Methods

width_inches(str, font_size_points = font_size_pt) click to toggle source
# File lib/berkeley_library/tind/export/column_width_calculator.rb, line 53
def width_inches(str, font_size_points = font_size_pt)
  return 0 if str.nil? || str.empty?

  width_points(str, font_size_points) / 72.0
end
width_points(str, font_size_points = font_size_pt) click to toggle source
# File lib/berkeley_library/tind/export/column_width_calculator.rb, line 49
def width_points(str, font_size_points = font_size_pt)
  width_per_point(str) * font_size_points
end
width_ps_units(str) click to toggle source
# File lib/berkeley_library/tind/export/column_width_calculator.rb, line 42
def width_ps_units(str)
  return 0 if str.nil? || str.empty?

  chars = str.unicode_normalize.chars
  chars.inject(0) { |total, c| total + width_for_char(c) }
end

Private Instance Methods

width_for_char(c) click to toggle source
# File lib/berkeley_library/tind/export/column_width_calculator.rb, line 65
def width_for_char(c)
  WIDTHS.each { |re, w| return w if c =~ re }
  WIDTH_DEFAULT
end
width_per_point(str) click to toggle source
# File lib/berkeley_library/tind/export/column_width_calculator.rb, line 61
def width_per_point(str)
  width_ps_units(str) / WIDTH_UNIT
end