Module: BerkeleyLibrary::TIND::Export::ColumnWidthCalculator

Extended by:
ColumnWidthCalculator
Includes:
Config
Included in:
ColumnWidthCalculator
Defined in:
lib/berkeley_library/tind/export/column_width_calculator.rb

Overview

Calculates approximate column widths for cell values, based on Arial average character widths ()in units of 1/1000 point size) per 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.

Constant Summary collapse

WIDTH_UNIT =
1000.0
WIDTH_LOWER =
489.46
WIDTH_UPPER =
677.42
WIDTH_DIGIT =
556.0
WIDTH_CJK =

Measured empirically in LibreOffice 6.4.7.2

970.0
WIDTHS =
{
  /[\u4e00-\u9fff]/ => WIDTH_CJK, # CJK (excluding half-width forms)
  /[\uff01-\uff65\uffe0-\uffee]/ => WIDTH_CJK, # Fullwidth forms
  /[[:digit:]]/ => WIDTH_DIGIT,
  /[[:upper:]]/ => WIDTH_UPPER,
  /[[:lower:]]/ => WIDTH_LOWER,
  /[[:space:]]/ => 2 * WIDTH_LOWER / 3 # empirical
}.freeze
WIDTH_DEFAULT =

See WIDTHS

WIDTH_DIGIT

Constants included from Config

BerkeleyLibrary::TIND::Export::Config::FONT_SIZE_DEFAULT, BerkeleyLibrary::TIND::Export::Config::FORMAT_DIGITS_DEFAULT, BerkeleyLibrary::TIND::Export::Config::HEIGHT_INCREMENT_DEFAULT_POINTS, BerkeleyLibrary::TIND::Export::Config::LINE_HEIGHT_DEFAULT_EM, BerkeleyLibrary::TIND::Export::Config::MAX_COLUMN_WIDTH_INCHES, BerkeleyLibrary::TIND::Export::Config::WIDTH_INCREMENT_DEFAULT_INCHES

Instance Method Summary collapse

Methods included from Config

#font_size_pt, font_size_pt, font_size_pt=, #format_digits, format_digits, format_digits=, #h_incr_pt, h_incr_pt, h_incr_pt=, #line_height_em, line_height_em, line_height_em=, #max_col_width_in, max_col_width_in, max_col_width_in=, w_incr_in, #w_incr_in, w_incr_in=

Instance Method Details

#width_inches(str, font_size_points = font_size_pt) ⇒ Object



53
54
55
56
57
# 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) ⇒ Object



49
50
51
# 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) ⇒ Object

Fallback to digit width for other characters



42
43
44
45
46
47
# 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