jessedoyle / prawn-icon

Easy icons for Prawn.
Other
31 stars 15 forks source link

Position of icon text not in document flow #24

Closed ToniTornado closed 8 years ago

ToniTornado commented 8 years ago
text 'Some Text'
move_down 10
icon '<icon size="12">fa-info-circle</icon> Here is some information', size: 10, style: :bold
move_down 10
text 'Even more text'

I expect a call to icon() to render the text into the document flow, but instead it was rendered at the top of the current page. I'm not sure why exactly this happens; maybe it is because of Text::Formatted::Box (prawn-icon/lib/prawn/icon.rb line 125)?

I use the following patch in an initializer to print text with parsed icons at the correct position. But it will be a better place to put a method like this into the gem itself. Or am I missing something?

if defined?(Prawn::Icon::Parser)
  module Prawn
    class Document

      def icon_text(text_with_icons, opts={})
        opts.merge!(inline_format: true, document: self)
        self.text Prawn::Icon::Parser.format(self, text_with_icons), opts
      end

    end
  end
end
jessedoyle commented 8 years ago

I believe you need to provide the inline_format: true attribute directly to the icon method if you wish to use <icon> tags:

icon '<icon size="12">fa-info-circle</icon> Here is some information', size: 10, style: :bold, inline_format: true

I was unable to make your original code work. Prawn::Icon (correctly) generated the following exception:

Prawn::Errors::UnknownFont: Icon font not found for set: <icon size="12">fa

Were you seeing the same exception?

jessedoyle commented 8 years ago

@ToniTornado - I'm going to close this issue.

The solution is to provide the inline_format: true option to the icon method:

Prawn::Document.generate('test.pdf') do
  icon "Here's an icon that is inline <icon>fa-info-circle</icon>", inline_format: true
end

Produces this:

screen shot 2016-06-22 at 9 03 16 pm

Feel free to re-open this issue if you notice any odd behaviour!

jessedoyle commented 8 years ago

Actually, I've noticed the issue. Looks like a bug to me!

Example Code:

Prawn::Document.generate('test.pdf') do
  text 'Hello'
  move_down 10
  text 'More'
  move_down 10
  icon '<icon>fa-info-circle</icon> alert!', inline_format: true 
  move_down 10
  text 'END'
end

Output: screen shot 2016-06-22 at 9 24 53 pm

Sorry @ToniTornado - I'll look into when I get a chance!

ToniTornado commented 8 years ago

Thanks for your time! This is exactly what it looked like in my document, I should have added a screenshot. I missed the inline_format: true option in my code because I added it in my solution above by default.

When I understand your parser correctly, inline_format: true is always required because the parser removes the <icon> tag and generates an appropriate <font> tag with font face set to the correct icon font. I guess the method wouldn't work without inline_format: true so wouldn't it make sense to set it by default?

ToniTornado commented 8 years ago

@jessedoyle I installed your branch inline-cursor in my project and it works for me. 👍

jessedoyle commented 8 years ago

I installed your branch inline-cursor in my project and it works for me.

Perfect that's good to hear! I hacked something together, but the code definitely needs to be cleaned up (and tests need to be written).

But it's a start, I should have a solution merged into master over the next few days.

jessedoyle commented 8 years ago

@ToniTornado - I've merged #25 and released v1.1.1 to Rubygems. Please let me know if this solves your problem!