Skip to content

Commit

Permalink
Merge pull request #17 from felixbuenemann/custom-fonts
Browse files Browse the repository at this point in the history
Support customizing font name, size, family
  • Loading branch information
felixbuenemann authored Oct 30, 2017
2 parents a06ba36 + bb2cb30 commit 18495c0
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
3 changes: 3 additions & 0 deletions lib/xlsxtream/errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Xlsxtream
class Error < StandardError; end
end
24 changes: 21 additions & 3 deletions lib/xlsxtream/workbook.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# encoding: utf-8
require "stringio"
require "xlsxtream/errors"
require "xlsxtream/xml"
require "xlsxtream/shared_string_table"
require "xlsxtream/workbook"
Expand All @@ -10,6 +11,15 @@
module Xlsxtream
class Workbook

FONT_FAMILY_IDS = {
'' => 0,
'roman' => 1,
'swiss' => 2,
'modern' => 3,
'script' => 4,
'decorative' => 5
}.freeze

class << self

def open(data = nil, options = {})
Expand Down Expand Up @@ -98,6 +108,14 @@ def write_workbook
end

def write_styles
font_options = @options.fetch(:font, {})
font_size = font_options.fetch(:size, 12).to_s
font_name = font_options.fetch(:name, 'Calibri').to_s
font_family = font_options.fetch(:family, 'Swiss').to_s.downcase
font_family_id = FONT_FAMILY_IDS[font_family] or fail Error,
"Invalid font family #{font_family}, must be one of "\
+ FONT_FAMILY_IDS.keys.map(&:inspect).join(', ')

@io.add_file "xl/styles.xml"
@io << XML.header
@io << XML.strip(<<-XML)
Expand All @@ -108,9 +126,9 @@ def write_styles
</numFmts>
<fonts count="1">
<font>
<sz val="12"/>
<name val="Calibri"/>
<family val="2"/>
<sz val="#{XML.escape_attr font_size}"/>
<name val="#{XML.escape_attr font_name}"/>
<family val="#{font_family_id}"/>
</font>
</fonts>
<fills count="2">
Expand Down
57 changes: 57 additions & 0 deletions test/xlsxtream/workbook_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,63 @@ def test_styles_content
assert_equal expected, actual
end

def test_custom_font_size
iow_spy = io_wrapper_spy
font_options = { :size => 23 }
Workbook.open(nil, :io_wrapper => iow_spy, :font => font_options) {}
expected = '<sz val="23"/>'
actual = iow_spy['xl/styles.xml'][/<sz [^>]+>/]
assert_equal expected, actual
end

def test_custom_font_name
iow_spy = io_wrapper_spy
font_options = { :name => 'Comic Sans' }
Workbook.open(nil, :io_wrapper => iow_spy, :font => font_options) {}
expected = '<name val="Comic Sans"/>'
actual = iow_spy['xl/styles.xml'][/<name [^>]+>/]
assert_equal expected, actual
end

def test_custom_font_family
iow_spy = io_wrapper_spy
font_options = { :family => 'Script' }
Workbook.open(nil, :io_wrapper => iow_spy, :font => font_options) {}
expected = '<family val="4"/>'
actual = iow_spy['xl/styles.xml'][/<family [^>]+>/]
assert_equal expected, actual
end

def test_font_family_mapping
tests = {
nil => 0,
'' => 0,
'ROMAN' => 1,
:roman => 1,
'Roman' => 1,
:swiss => 2,
:modern => 3,
:script => 4,
:decorative => 5
}
tests.each do |value, id|
iow_spy = io_wrapper_spy
font_options = { :family => value }
Workbook.open(nil, :io_wrapper => iow_spy, :font => font_options) {}
expected = "<family val=\"#{id}\"/>"
actual = iow_spy['xl/styles.xml'][/<family [^>]+>/]
assert_equal expected, actual
end
end

def test_invalid_font_family
iow_spy = io_wrapper_spy
font_options = { :family => 'Foo' }
assert_raises Xlsxtream::Error do
Workbook.open(nil, :io_wrapper => iow_spy, :font => font_options) {}
end
end

def test_tempfile_is_not_closed
tempfile = Tempfile.new('workbook')
Workbook.open(tempfile) {}
Expand Down

0 comments on commit 18495c0

Please sign in to comment.