module Caracal::Core::Styles

This module encapsulates all the functionality related to defining paragraph styles.

Public Class Methods

default_styles() click to toggle source
# File lib/caracal/core/styles.rb, line 19
def self.default_styles
  [
    { id: 'Normal',   name: 'normal',    font: 'Arial',    size: 20, line: 320, color: '333333' },
    { id: 'Header',   name: 'header',    font: 'Arial',    size: 20, bottom: 120 },
    { id: 'Heading1', name: 'heading 1', font: 'Palatino', size: 36, bottom: 120 },
    { id: 'Heading2', name: 'heading 2', font: 'Arial',    size: 26, bottom: 120, top: 120, bold: true },
    { id: 'Heading3', name: 'heading 3', font: 'Arial',    size: 24, bottom: 120, top: 120, bold: true, italic: true, color: '666666' },
    { id: 'Heading4', name: 'heading 4', font: 'Palatino', size: 24, bottom: 120, top: 120, bold: true },
    { id: 'Heading5', name: 'heading 5', font: 'Arial',    size: 22, bottom: 120, top: 120, bold: true },
    { id: 'Heading6', name: 'heading 6', font: 'Arial',    size: 22, bottom: 120, top: 120, underline: true, italic: true, color: '666666' },
    { id: 'TOC1',     name: 'TOC 1',     font: 'Palatino', size: 22, bottom: 120, top: 120, indent_left:   0, bold: true },
    { id: 'TOC2',     name: 'TOC 2',     font: 'Arial',    size: 22, bottom: 120, top: 120, indent_left:  60 },
    { id: 'TOC3',     name: 'TOC 3',     font: 'Arial',    size: 22, bottom: 120, top: 120, indent_left: 120 },
    { id: 'TOC4',     name: 'TOC 4',     font: 'Palatino', size: 22, bottom: 120, top: 120, indent_left: 180, italic: true, color: '666666' },
    { id: 'TOC5',     name: 'TOC 5',     font: 'Arial',    size: 22, bottom: 120, top: 120, indent_left: 240, italic: true, color: '666666' },
    { id: 'TOC6',     name: 'TOC 6',     font: 'Arial',    size: 22, bottom: 120, top: 120, indent_left: 320, italic: true, color: '666666' },
    { id: 'Title',    name: 'title',     font: 'Palatino', size: 60 },
    { id: 'Subtitle', name: 'subtitle',  font: 'Arial',    size: 28, top: 60 },
    { id: 'Hyperlink', name: 'hyperlink', type: 'character', underline: true, color: '0000ff' }
  ]           
end
included(base) click to toggle source
# File lib/caracal/core/styles.rb, line 12
def self.included(base)
  base.class_eval do
    
    #-------------------------------------------------------------
    # Class Methods
    #-------------------------------------------------------------
    
    def self.default_styles
      [
        { id: 'Normal',   name: 'normal',    font: 'Arial',    size: 20, line: 320, color: '333333' },
        { id: 'Header',   name: 'header',    font: 'Arial',    size: 20, bottom: 120 },
        { id: 'Heading1', name: 'heading 1', font: 'Palatino', size: 36, bottom: 120 },
        { id: 'Heading2', name: 'heading 2', font: 'Arial',    size: 26, bottom: 120, top: 120, bold: true },
        { id: 'Heading3', name: 'heading 3', font: 'Arial',    size: 24, bottom: 120, top: 120, bold: true, italic: true, color: '666666' },
        { id: 'Heading4', name: 'heading 4', font: 'Palatino', size: 24, bottom: 120, top: 120, bold: true },
        { id: 'Heading5', name: 'heading 5', font: 'Arial',    size: 22, bottom: 120, top: 120, bold: true },
        { id: 'Heading6', name: 'heading 6', font: 'Arial',    size: 22, bottom: 120, top: 120, underline: true, italic: true, color: '666666' },
        { id: 'TOC1',     name: 'TOC 1',     font: 'Palatino', size: 22, bottom: 120, top: 120, indent_left:   0, bold: true },
        { id: 'TOC2',     name: 'TOC 2',     font: 'Arial',    size: 22, bottom: 120, top: 120, indent_left:  60 },
        { id: 'TOC3',     name: 'TOC 3',     font: 'Arial',    size: 22, bottom: 120, top: 120, indent_left: 120 },
        { id: 'TOC4',     name: 'TOC 4',     font: 'Palatino', size: 22, bottom: 120, top: 120, indent_left: 180, italic: true, color: '666666' },
        { id: 'TOC5',     name: 'TOC 5',     font: 'Arial',    size: 22, bottom: 120, top: 120, indent_left: 240, italic: true, color: '666666' },
        { id: 'TOC6',     name: 'TOC 6',     font: 'Arial',    size: 22, bottom: 120, top: 120, indent_left: 320, italic: true, color: '666666' },
        { id: 'Title',    name: 'title',     font: 'Palatino', size: 60 },
        { id: 'Subtitle', name: 'subtitle',  font: 'Arial',    size: 28, top: 60 },
        { id: 'Hyperlink', name: 'hyperlink', type: 'character', underline: true, color: '0000ff' }
      ]           
    end
    
    
    #-------------------------------------------------------------
    # Public Methods
    #-------------------------------------------------------------
    
    #============== ATTRIBUTES ==========================
    
    def style(options={}, &block)
      model = Caracal::Core::Models::StyleModel.new(options, &block)
      
      if model.valid?
        register_style(model)
      else
        raise Caracal::Errors::InvalidModelError, 'style must define an :id and :name.'
      end
      model
    end
    
    
    #============== GETTERS =============================
    
    def styles
      @styles ||= []
    end
    
    def outline_styles
      @outline_styles ||= styles.select { |style| !style.style_outline_lvl.nil? }
    end
    
    def default_style
      styles.find { |s| s.style_default }
    end
    
    def find_style(id)
      styles.find { |s| s.matches?(id) }
    end
    
    
    #============== REGISTRATION ========================
    
    def register_style(model)
      unregister_style(model.style_id)
      styles << model
      model
    end
    
    def unregister_style(id)
      if s = find_style(id)
        styles.delete(s)
      end
    end
    
  end
end

Public Instance Methods

default_style() click to toggle source
# File lib/caracal/core/styles.rb, line 70
def default_style
  styles.find { |s| s.style_default }
end
find_style(id) click to toggle source
# File lib/caracal/core/styles.rb, line 74
def find_style(id)
  styles.find { |s| s.matches?(id) }
end
outline_styles() click to toggle source
# File lib/caracal/core/styles.rb, line 66
def outline_styles
  @outline_styles ||= styles.select { |style| !style.style_outline_lvl.nil? }
end
register_style(model) click to toggle source
REGISTRATION ========================
# File lib/caracal/core/styles.rb, line 81
def register_style(model)
  unregister_style(model.style_id)
  styles << model
  model
end
style(options={}, &block) click to toggle source
ATTRIBUTES ==========================
# File lib/caracal/core/styles.rb, line 48
def style(options={}, &block)
  model = Caracal::Core::Models::StyleModel.new(options, &block)
  
  if model.valid?
    register_style(model)
  else
    raise Caracal::Errors::InvalidModelError, 'style must define an :id and :name.'
  end
  model
end
styles() click to toggle source
GETTERS =============================
# File lib/caracal/core/styles.rb, line 62
def styles
  @styles ||= []
end
unregister_style(id) click to toggle source
# File lib/caracal/core/styles.rb, line 87
def unregister_style(id)
  if s = find_style(id)
    styles.delete(s)
  end
end