Skip to content

Commit

Permalink
resolves asciidoctor#1157 correctly map legacy Font Awesome icon name…
Browse files Browse the repository at this point in the history
…s when icon set is not specified
  • Loading branch information
mojavelinux committed Jul 16, 2019
1 parent ce070cb commit 8b5faf4
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ For a detailed view of what has changed, refer to the {uri-repo}/commits/master[

* fix crash when image_width is defined in theme (#995)
* fix crash when toc is enabled and toc-title attribute is unset
* correctly map legacy Font Awesome icon names when icon set is not specified (#1157)
* coerce color values in theme that contain uppercase letters (#1149)
* ensure base font color is set
* use more robust mechanism to detect an empty page; tare content stream after adding page background color or image
Expand Down
29 changes: 18 additions & 11 deletions lib/asciidoctor-pdf/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2248,22 +2248,29 @@ def convert_inline_icon node
else
size_attr = ''
end
begin
if icon_set == 'fa'
if icon_set == 'fa'
# legacy name from Font Awesome < 5
if (remapped_icon_name = resolve_legacy_icon_name icon_name)
requested_icon_name = icon_name
icon_set, icon_name = remapped_icon_name.split '-', 2
glyph = (icon_font_data icon_set).unicode icon_name
logger.info { %(#{requested_icon_name} icon found in deprecated fa icon set; using #{icon_name} from #{icon_set} icon set instead) }
# new name in Font Awesome >= 5 (but document is configured to use fa icon set)
else
font_data = nil
resolved_icon_set = FontAwesomeIconSets.find {|candidate| (font_data = icon_font_data candidate).unicode icon_name rescue nil }
if resolved_icon_set
if (resolved_icon_set = FontAwesomeIconSets.find {|candidate| (font_data = icon_font_data candidate).unicode icon_name rescue nil })
icon_set = resolved_icon_set
logger.info { %(#{icon_name} icon found in deprecated fa icon set; use #{icon_set} icon set instead) }
else
raise
glyph = font_data.unicode icon_name
logger.info { %(#{icon_name} icon not found in deprecated fa icon set; using match found in #{resolved_icon_set} icon set instead) }
end
else
font_data = icon_font_data icon_set
end
else
glyph = (icon_font_data icon_set).unicode icon_name rescue nil
end
if glyph
# TODO support rotate and flip attributes
%(<font name="#{icon_set}"#{size_attr}>#{font_data.unicode icon_name}</font>)
rescue
%(<font name="#{icon_set}"#{size_attr}>#{glyph}</font>)
else
logger.warn %(#{icon_name} is not a valid icon name in the #{icon_set} icon set)
%([#{node.attr 'alt'}])
end
Expand Down
4 changes: 4 additions & 0 deletions lib/asciidoctor-pdf/prawn_ext/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ def icon_font_data family
::Prawn::Icon::FontData.load self, family
end

def resolve_legacy_icon_name name
::Prawn::Icon::Compatibility::SHIMS[%(fa-#{name})]
end

def calc_line_metrics line_height = 1, font = self.font, font_size = self.font_size
line_height_length = line_height * font_size
leading = line_height_length - font_size
Expand Down
55 changes: 55 additions & 0 deletions spec/icon_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require_relative 'spec_helper'

describe 'Asciidoctor::PDF::Converter - Icon' do
it 'should use icon name from specified icon set' do
pdf = to_pdf <<~'EOS', analyze: true
:icons: font
:icon-set: fas
A icon:wrench[] ought to fix it.
EOS
wink_text = pdf.find_text ?\uf0ad
(expect wink_text).to have_size 1
(expect wink_text[0][:font_name]).to eql 'FontAwesome5Free-Solid'
end

it 'should use icon name as alt text and warn if icon name not found in icon set' do
(expect {
pdf = to_pdf <<~'EOS', analyze: true
:icons: font
:icon-set: fas
icon:no-such-icon[] will surely fail.
EOS
text = pdf.text
(expect text).to have_size 1
(expect text[0][:string]).to eql '[no such icon] will surely fail.'
}).to log_message severity: :WARN, message: 'no-such-icon is not a valid icon name in the fas icon set'
end

it 'should remap legacy icon name if icon set is not specified and report remapping' do
(expect {
pdf = to_pdf <<~'EOS', analyze: true
:icons: font
Click the icon:hdd-o[] icon to see your files.
EOS
hdd_text = pdf.find_text ?\uf0a0
(expect hdd_text).to have_size 1
(expect hdd_text[0][:font_name]).to eql 'FontAwesome5Free-Regular'
}).to log_message severity: :INFO, message: 'hdd-o icon found in deprecated fa icon set; using hdd from far icon set instead', using_log_level: :INFO
end

it 'should resolve non-legacy icon name if icon set is not specified and report icon set in which it was found' do
(expect {
pdf = to_pdf <<~'EOS', analyze: true
:icons: font
Time to upgrade your icon set icon:smile-wink[]
EOS
wink_text = pdf.find_text ?\uf4da
(expect wink_text).to have_size 1
(expect wink_text[0][:font_name]).to eql 'FontAwesome5Free-Regular'
}).to log_message severity: :INFO, message: 'smile-wink icon not found in deprecated fa icon set; using match found in far icon set instead', using_log_level: :INFO
end
end
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ def compute_image_differences reference, actual, difference = nil

RSpec::Matchers.define :log_message do |expected|
match notify_expectation_failures: true do |actual|
with_memory_logger do |logger|
with_memory_logger expected[:using_log_level] do |logger|
actual.call
(expect logger).to have_message expected if logger
true
Expand Down

0 comments on commit 8b5faf4

Please sign in to comment.