Skip to content

Defining and Using Tags

shairontoledo edited this page Apr 30, 2012 · 4 revisions

Defining tags

The RGhost::Document#define_tags method creates a map of tags which will be used in ‘writable’ classes (Show, Text, TextIn and TextArea). The font names file catalog can be generated by the code below.


  RGhost::Config.environment_fonts.render :pdf, :filename => 'mycatalog.pdf'

This can take a while. If it takes too long your are probably having font problems. Remove some fonts, particularly little used international fonts. Below is a little piece of the catalog:

After generating your catalog you can map your tags.
Tags have the name of tag(as Symbol) and its options. The options are

  • :name – Font name from catalog.
  • :size – Font size.
  • :color – Color.create facade
  • :from – Load external font, exemp: ‘/tmp/test.ttf’
  • :encoding – If true the font will be encoded using the pattern :font_encoding of the document.

Examples

d=Document.new :encoding => 'IsoLatin'
d.define_tags do
  tag :my_italic,    :name => 'Hershey-Gothic-Italian-Oblique', :size => 10
  tag :myfont,       :name => 'Hershey-Plain'
  tag :verdana,      :name => 'Verdana', :from => "/my/path/verdana.ttf", :size => 12
  tag :font_encoded, :name => 'NimbusMonL-Regu', :size => 8, :color => 0.5,:encoding => true
  tag :other_font,   :name => 'NimbusMonL-Regu',    :size => 10
  tag :arial,        :name => 'Arial-ItalicMT',     :color => '#ADAD66'
  tag :arial_bold,   :name => 'NimbusSanL-BoldItal',:size => 12, :color => '#ADAD66'
end

You can use the :default_font tag for customizing the default font.

Using tags

With Show class

doc.show 'My Text on this row', :with => :my_italic, :align => :page_center

With Show class overriding the tag‘s color.

doc.show 'My Text on this row', :with => :my_italic, :align => :page_center, :color => :red

With TextIn class.

doc.text_in :x=> 3, :y=> 10, :tag => :arial_bold , :write => "Here's point(3,10)"

With Text

doc.text '<myfont>My Text</myfont>on this row.<arial>Other text</arial><my_italic>Italic font</my_italic>'

With TextArea

txt='<myfont>My Text</myfont>on this row.<arial>Other text</arial><my_italic>Italic font</my_italic>'
doc.text_area txt, :text_align => :center, :width => 5, :x => 3, :y => 10

use_tag method

doc.use_tag :myfont
doc.show "Simple Text", :tag => nil # it will use :myfont
doc.show "Simple Text2"   # it will use :myfont too

Default tags

The default tags are defined in the constant RGhost::Config::FONTMAP, below the content of the block.

RGhost::FontMap.new :name => "Helvetica", :size => 8, :encoding => false do
  new :span
  new :b,     :name => "Helvetica-Bold"
  new :bold,     :name => "Helvetica-Bold"
  new :normal,   :name => "Helvetica"
  new :i,     :name => "Helvetica-Oblique", :size => 8
  new :bi,    :name => "Helvetica-BoldOblique"
  new :big,   :size => 10
  new :small, :size => 7
  new :h1,    :name => "Helvetica", :size => 14
  new :h2,    :name => "Helvetica", :size => 13
  new :h3,    :name => "Helvetica", :size => 12
  new :h4,    :name => "Helvetica", :size => 11
  new :h5,    :name => "Helvetica", :size => 10
  new :title, :name => "Helvetica", :size => 20
  new :pre,   :name => "Courier"
end

You can customize the tag by overriding tags with the same name. Example for tag b.
d=Document.new :encoding => 'IsoLatin'
d.define_tags do
  tag :b,  :name => "Helvetica-Bold", :font => 12
end

The tag :normal is default if is not specified.