### Home Classes Methods

### In Files

- lib/prawn/font/ttf.rb

### Parent

Prawn::Font

### Methods

- ::new
- #basename
- #bbox
- #cap_height
- #character_count
- #encode_text
- #family_class
- #glyph_present?
- #has_kerning_data?
- #italic_angle
- #normalize_encoding
- #pdf_flags
- #script?
- #serif?
- #stemV
- #unicode?
- #x_height

### Files

- COPYING
- GPLv2
- GPLv3
- LICENSE

### Class/Module Index

Quicksearch

- Prawn
- Prawn::Core
- Prawn::Core::Page
- Prawn::Core::Page::GraphicsState
- Prawn::Core::Reference
- Prawn::Document
- Prawn::Document::BoundingBox
- Prawn::Document::ColumnBox
- Prawn::Document::GraphicsState
- Prawn::Document::Grid
- Prawn::Document::Internals
- Prawn::Document::PageGeometry
- Prawn::Document::Security
- Prawn::Document::Snapshot
- Prawn::Encoding
- Prawn::Errors
- Prawn::Font
- Prawn::Font::AFM
- Prawn::Font::DFont
- Prawn::Font::TTF
- Prawn::GraphicState
- Prawn::GraphicStateStack
- Prawn::Graphics
- Prawn::Graphics::CapStyle
- Prawn::Graphics::Color
- Prawn::Graphics::Dash
- Prawn::Graphics::Gradient
- Prawn::Graphics::JoinStyle
- Prawn::Graphics::Transformation
- Prawn::Graphics::Transparency
- Prawn::Images
- Prawn::Images::JPG
- Prawn::Images::PNG
- Prawn::Layout
- Prawn::Measurements
- Prawn::Outline
- Prawn::Stamp
- Prawn::Table
- Prawn::Table::Cell
- Prawn::Table::Cell::InTable
- Prawn::Table::Cell::Subtable
- Prawn::Table::Cell::Text
- Prawn::Table::Cells
- Prawn::Text
- Prawn::Text::Box
- Prawn::Text::Formatted
- Prawn::Text::Formatted::Box
- Prawn::Text::Formatted::Fragment
- Prawn::Text::Formatted::Parser
- Arcfour
- Numeric

No matching classes.

# Prawn::Font::TTF

### Constants

UNICODE_CMAP_TEMPLATE

### Attributes

subsets[R]
ttf[R]

### Public Class Methods

new(document, name, options={}) click to toggle source

    # File lib/prawn/font/ttf.rb, line 21
    def initialize(document, name, options={})
      super

      @ttf              = read_ttf_file
      @subsets          = TTFunk::SubsetCollection.new(@ttf)

      @attributes       = {}
      @bounding_boxes   = {}
      @char_widths      = {}
      @has_kerning_data = @ttf.kerning.exists? && @ttf.kerning.tables.any?

      @ascender         = Integer(@ttf.ascent * scale_factor)
      @descender        = Integer(@ttf.descent * scale_factor)
      @line_gap         = Integer(@ttf.line_gap * scale_factor)
    end

### Public Instance Methods

basename() click to toggle source

    # File lib/prawn/font/ttf.rb, line 105
    def basename
      @basename ||= @ttf.name.postscript_name
    end

bbox() click to toggle source

The font bbox, as an array of integers

    # File lib/prawn/font/ttf.rb, line 57
    def bbox
      @bbox ||= @ttf.bbox.map { |i| Integer(i * scale_factor) }
    end

cap_height() click to toggle source

    # File lib/prawn/font/ttf.rb, line 125
    def cap_height
      @cap_height ||= begin
        height = @ttf.os2.exists? && @ttf.os2.cap_height || 0
        height == 0 ? ascender : height
      end
    end

character_count(str) click to toggle source

Returns the number of characters in `str` (a UTF-8-encoded string).

    # File lib/prawn/font/ttf.rb, line 194
    def character_count(str)
      if str.respond_to?(:encode)
        str.length
      else
        str.unpack("U*").length
      end
    end

encode_text(text,options={}) click to toggle source

Perform any changes to the string that need to happen before it is rendered to the canvas. Returns an array of subset "chunks", where the even-numbered indices are the font subset number, and the following entry element is either a string or an array (for kerned text).

The `text` parameter must be UTF8-encoded.

    # File lib/prawn/font/ttf.rb, line 74
    def encode_text(text,options={})
      text = text.chomp

      if options[:kerning]
        last_subset = nil
        kern(text).inject([]) do |result, element|
          if element.is_a?(Numeric)
            result.last[1] = [result.last[1]] unless result.last[1].is_a?(Array)
            result.last[1] << element
            result
          else
            encoded = @subsets.encode(element)

            if encoded.first[0] == last_subset
              result.last[1] << encoded.first[1]
              encoded.shift
            end

            if encoded.any?
              last_subset = encoded.last[0]
              result + encoded
            else
              result
            end
          end
        end
      else
        @subsets.encode(text.unpack("U*"))
      end
    end

family_class() click to toggle source

    # File lib/prawn/font/ttf.rb, line 138
    def family_class
      @family_class ||= (@ttf.os2.exists? && @ttf.os2.family_class || 0) >> 8
    end

glyph_present?(char) click to toggle source

    # File lib/prawn/font/ttf.rb, line 187
    def glyph_present?(char)
      code = char.unpack("U*").first
      cmap[code] > 0
    end

has_kerning_data?() click to toggle source

Returns true if the font has kerning data, false otherwise

    # File lib/prawn/font/ttf.rb, line 62
    def has_kerning_data?
      @has_kerning_data
    end

italic_angle() click to toggle source

    # File lib/prawn/font/ttf.rb, line 114
    def italic_angle
      @italic_angle ||= if @ttf.postscript.exists?
        raw = @ttf.postscript.italic_angle
        hi, low = raw >> 16, raw & 0xFF
        hi = -((hi ^ 0xFFFF) + 1) if hi & 0x8000 != 0
        "#{hi}.#{low}".to_f
      else
        0
      end
    end

normalize_encoding(text) click to toggle source

    # File lib/prawn/font/ttf.rb, line 161
    def normalize_encoding(text)
      if text.respond_to?(:encode)
        # if we're running under a M17n aware VM, ensure the string provided is
        # UTF-8 (by converting it if necessary)
        begin
          text.encode("UTF-8")
        rescue
          raise Prawn::Errors::IncompatibleStringEncoding, "Encoding " +
          "#{text.encoding} can not be transparently converted to UTF-8. " +
          "Please ensure the encoding of the string you are attempting " +
          "to use is set correctly"
        end
      else
        # on a non M17N aware VM, use unpack as a hackish way to verify the
        # string is valid utf-8. I thought it was better than loading iconv
        # though.
        begin
          text.unpack("U*")
          return text.dup
        rescue
          raise Prawn::Errors::IncompatibleStringEncoding, "The string you " +
            "are attempting to render is not encoded in valid UTF-8."
        end
      end
    end

pdf_flags() click to toggle source

    # File lib/prawn/font/ttf.rb, line 150
    def pdf_flags
      @flags ||= begin
        flags = 0
        flags |= 0x0001 if @ttf.postscript.fixed_pitch?
        flags |= 0x0002 if serif?
        flags |= 0x0008 if script?
        flags |= 0x0040 if italic_angle != 0
        flags |= 0x0004 # assume the font contains at least some non-latin characters
      end
    end

script?() click to toggle source

    # File lib/prawn/font/ttf.rb, line 146
    def script?
      @script ||= family_class == 10
    end

serif?() click to toggle source

    # File lib/prawn/font/ttf.rb, line 142
    def serif?
      @serif ||= [1,2,3,4,5,7].include?(family_class)
    end

stemV() click to toggle source

not sure how to compute this for true-type fonts...

    # File lib/prawn/font/ttf.rb, line 110
    def stemV
      0
    end

unicode?() click to toggle source

    # File lib/prawn/font/ttf.rb, line 17
    def unicode?
      true
    end

x_height() click to toggle source

    # File lib/prawn/font/ttf.rb, line 132
    def x_height
      # FIXME: seems like if os2 table doesn't exist, we could
      # just find the height of the lower-case 'x' glyph?
      @ttf.os2.exists? && @ttf.os2.x_height || 0
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.