Skip to content

Commit a07a386

Browse files
committed
Add configuration for registering font
1 parent 7bd0fe7 commit a07a386

2 files changed

Lines changed: 75 additions & 0 deletions

File tree

lib/thinreports/config.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def self.config
1515
class Configuration
1616
def initialize
1717
@fallback_fonts = []
18+
@fonts = {}
1819
end
1920

2021
# @return [Array<String>]
@@ -32,5 +33,37 @@ def fallback_fonts
3233
def fallback_fonts=(font_names)
3334
@fallback_fonts = font_names.is_a?(Array) ? font_names : [font_names]
3435
end
36+
37+
# @param [String] name
38+
# @param [String] normal (required) Path for normal style font
39+
# @param [String] bold Path for bold style font. Set :normal by default.
40+
# @param [String] italic Path for italic style font. Set :normal by default.
41+
# @param [String] bold_italic Path for bold+italic style font. Set :normal by default.
42+
# @example
43+
# config.register_font('Foo Font',
44+
# normal: '/path/to/foo.ttf',
45+
# bold: '/path/to/foo_bold.ttf',
46+
# italic: '/path/to/foo_italic.ttf',
47+
# bold_italic: '/path/to/foo_bold_italic.ttf'
48+
# )
49+
#
50+
# # For fonts that have no style, such as Japanese:
51+
# config.register_font('Bar Font', normal: '/path/to/bar.tlf')
52+
# => {
53+
# normal: '/path/to/bar.tlf',
54+
# bold: '/path/to/bar.tlf,'
55+
# italic: '/path/to/bar.tlf,'
56+
# bold_italic: '/path/to/bar.tlf,'
57+
# }
58+
def register_font(name, normal:, bold: normal, italic: normal, bold_italic: normal)
59+
@fonts[name] = {
60+
normal: normal,
61+
bold: bold,
62+
italic: italic,
63+
bold_italic: bold_italic
64+
}
65+
end
66+
67+
attr_reader :fonts
3568
end
3669
end

test/unit/test_config.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,46 @@ def test_fallback_fonts
3333
config.fallback_fonts.unshift 'Times New Roman'
3434
assert_equal config.fallback_fonts, ['Times New Roman', 'Helvetica', 'IPAMincho']
3535
end
36+
37+
def test_register_font
38+
config = Thinreports::Configuration.new
39+
40+
assert_equal({}, config.fonts)
41+
42+
config.register_font('Foo',
43+
normal: 'foo.ttf',
44+
bold: 'foo_bold.ttf',
45+
italic: 'foo_italic.ttf',
46+
bold_italic: 'foo_bold_italic.ttf'
47+
)
48+
assert_equal({
49+
normal: 'foo.ttf',
50+
bold: 'foo_bold.ttf',
51+
italic: 'foo_italic.ttf',
52+
bold_italic: 'foo_bold_italic.ttf'
53+
}, config.fonts['Foo'])
54+
55+
config.register_font('Bar', normal: 'bar.ttf')
56+
assert_equal({
57+
normal: 'bar.ttf',
58+
bold: 'bar.ttf',
59+
italic: 'bar.ttf',
60+
bold_italic: 'bar.ttf'
61+
}, config.fonts['Bar'])
62+
63+
assert_equal({
64+
'Foo' => {
65+
normal: 'foo.ttf',
66+
bold: 'foo_bold.ttf',
67+
italic: 'foo_italic.ttf',
68+
bold_italic: 'foo_bold_italic.ttf'
69+
},
70+
'Bar' => {
71+
normal: 'bar.ttf',
72+
bold: 'bar.ttf',
73+
italic: 'bar.ttf',
74+
bold_italic: 'bar.ttf'
75+
}
76+
}, config.fonts)
77+
end
3678
end

0 commit comments

Comments
 (0)