class Object

Constants

Rsvg

Public Instance Methods

apply_background_image_property(element, options={}) click to toggle source
# File lib/rabbit/theme/background-image-toolkit/background-image-toolkit.rb, line 1
def apply_background_image_property(element, options={})
  proc_name = options[:proc_name] || "background-image"
  compute_initial_geometry = options[:compute_initial_geometry]

  element.delete_pre_draw_proc_by_name(proc_name)

  if options.has_key?(:file_name)
    background_image = options[:file_name]
  else
    background_image = element["background-image"]
  end
  return if background_image.nil?

  properties = options[:properties] || {}
  element.user_property.each do |name, value|
    if /\Abackground-image-/ =~ name
      properties[$POSTMATCH.gsub(/-/, '_')] = value
    end
  end

  image = image_element(background_image, properties)
  align = properties["align"] || "center"
  vertical_align = properties["vertical_align"] || "middle"
  assign_box = properties["assign_box"]
  element_margin_right = 0
  case align
  when "center"
    image.horizontal_centering = true
  end
  case vertical_align
  when "middle"
    image.vertical_centering = true
  end

  layout = nil
  caption_height = 0
  caption_text = image.caption
  caption_text = nil if caption_text and caption_text.empty?

  element.add_pre_draw_proc(proc_name) do |canvas, x, y, w, h, simulation|
    if simulation
      if compute_initial_geometry
        _x, _y, _w, _h = compute_initial_geometry.call(canvas, x, y, w, h)
      else
        _x, _y, _w, _h = x, y, w, h
      end

      old_geometry = [_x, _y, _w, _h]
      image.compile(canvas, _x, _y, _w, _h)

      case vertical_align
      when "middle"
        adjust_height = ((_h - image.height - image.padding_bottom) / 2.0).ceil
        if _y + adjust_height > 0
          _y += adjust_height
          _h -= adjust_height
        end
      when "bottom"
        adjust_height =
          image.height + image.padding_bottom + element.margin_bottom
        _y = _h - adjust_height
        _h -= adjust_height
      end

      case align
      when "right"
        if assign_box
          element_margin_right = image.width +
            image.margin_left + image.margin_right +
            element.margin_right + element.padding_right
          element_margin_right += element.parent.margin_right if element.parent
        end
        _x = _w - image.width - image.margin_right
      when "center"
      end

      if old_geometry != [_x, _y, _w, _h]
        image.compile(canvas, _x, _y, _w, _h)
      end

      if image.do_horizontal_centering?
        image.do_horizontal_centering(canvas, _x, _y, _w, _h)
      end
    end
    image.draw(simulation)

    if caption_text
      # TODO: Should we move this to Image#draw and unify this and
      # similar code in lib/rabbit/theme/image/image.rb?
      if simulation
        caption = Text.new(caption_text)
        caption_font_size = image.caption_font_size
        caption_font_size = font_size(caption_font_size) if caption_font_size
        caption.prop_set("size",
                         caption_font_size || @image_caption_font_size)
        set_font_family(caption)
        if image.horizontal_centering
          caption.do_horizontal_centering(canvas, x, y, w, h)
        end
        caption.compile(canvas,
                        (image.x || x) + image.margin_left,
                        y + image.height + image.margin_top,
                        image.ow || w,
                        h - image.height)
        layout = caption.layout
        caption_height = caption.height
      end
      if !simulation and layout
        base_x = (image.x || x) + image.margin_left
        base_y = image.height + y + image.margin_top
        caption_color = image["caption-color"] || @image_caption_color
        canvas.draw_layout(layout,
                           base_x,
                           base_y + image.margin_bottom,
                           caption_color)
      end
    end

    [x, y, w - element_margin_right, h]
  end

  element.add_post_draw_proc(proc_name) do |canvas, x, y, w, h, simulation|
    [x, y, w + element_margin_right, h]
  end
end
color_circle_slide(slides, name="color-circle-slide") click to toggle source
# File lib/rabbit/theme/color-circle-common/color-circle-common.rb, line 1
def color_circle_slide(slides, name="color-circle-slide")
  slides.delete_pre_draw_proc_by_name(name)

  slides.add_pre_draw_proc(name) do |slide, canvas, x, y, w, h, simulation|
    unless simulation
      cx = w * 0.22
      cy = h * 0.72
      r = w * 0.35
      canvas.draw_circle_by_radius(true, cx, cy, r, @color_circle_light_color)
      r = w * 0.34
      canvas.draw_circle_by_radius(true, cx, cy, r, @color_circle_bright_color)
      r = w * 0.28
      canvas.draw_circle_by_radius(true, cx, cy, r, @color_circle_light_color)
    end
    [x, y, w, h]
  end
end
color_circle_title(titles, name="color-circle-title") click to toggle source
# File lib/rabbit/theme/color-circle-common/color-circle-common.rb, line 19
def color_circle_title(titles, name="color-circle-title")
  titles.delete_pre_draw_proc_by_name(name)
  titles.delete_post_draw_proc_by_name(name)

  margin = canvas.width * 0.2
  line_width = {:line_width => screen_size(0.1)}
  titles.margin_top = screen_y(1)
  titles.margin_left = margin
  titles.margin_bottom = canvas.height * 0.1
  titles.add_pre_draw_proc(name) do |title, canvas, x, y, w, h, simulation|
    unless simulation
      ly = y + title.first_line_height
      canvas.draw_line(x, ly, x + w, ly, "black", line_width)

      base_h = title.first_line_height * 0.8
      cx = margin * 0.25
      cy = y + base_h * 0.8
      r = base_h * 0.3
      rest = margin - cx
      ratio = rest / (r * 2.5)
      canvas.draw_circle_by_radius(false, cx, cy, r, @color_circle_color,
                                   line_width)
      cx += r * ratio
      canvas.draw_circle_by_radius(true, cx, cy, r, @color_circle_color)
      cx += r * ratio
      canvas.draw_circle_by_radius(true, cx, cy, r, @color_circle_color)
    end
    [x, y, w, h]
  end
end
emphasize_keyword(target, keyword) click to toggle source
# File lib/rabbit/theme/emphasize-keyword/emphasize-keyword.rb, line 1
def emphasize_keyword(target, keyword)
  to_container(target).substitute_text do |_, text|
    if /#{keyword}/u =~ text
      result = text.split(/(#{keyword})/u).collect do |sub_text|
        if sub_text == keyword
          Emphasis.new(Text.new(sub_text))
        else
          sub_text
        end
      end
      result
    else
      text
    end
  end
end
mirror_effect(target, options={}) click to toggle source
# File lib/rabbit/theme/mirror-effect/mirror-effect.rb, line 1
def mirror_effect(target, options={})
  proc_name = options[:proc_name] || "mirror-effect"

  target.delete_post_draw_proc_by_name(proc_name)

  return if options[:uninstall]

  scale_y = 0.4
  shear_x = 0.5
  target.add_around_draw_proc(proc_name) do |canvas, x, y, w, h, simulation,
                                             next_proc|
    rx, ry, rw, rh = next_proc.call(canvas, x, y, w, h, simulation)
    unless simulation
      canvas.save_context do
        height = ry - y
        base = ry + target.margin_bottom
        canvas.translate_context(0, base)
        canvas.reflect_context(:x)
        canvas.shear_context(shear_x, 0)
        canvas.scale_context(1, scale_y)
        canvas.translate_context(0, -base)
        next_proc.call(canvas, x, y, w, height, simulation)
      end
    end
    [rx, ry, rw, rh]
  end
end
scroll_effect(target, options={}) click to toggle source
# File lib/rabbit/theme/scroll-effect/scroll-effect.rb, line 1
def scroll_effect(target, options={})
  proc_name = options[:proc_name] || "scroll-effect"

  target.delete_post_draw_proc_by_name(proc_name)

  return if options[:uninstall]

  idle_id = nil
  effected = false
  slided_x = nil
  target.add_around_draw_proc(proc_name) do |canvas, x, y, w, h, simulation,
                                             next_proc|
    canvas.save_context do
      if !simulation and !effected
        slided_x ||= w
        slided_x = [slided_x - (w / 10), 0].max
        canvas.translate_context(slided_x, 0)
        idle_id ||= Gtk.idle_add do
          continue = !slided_x.zero?
          unless continue
            idle_id = nil
            slided_x = nil
            effected = true
          end
          canvas.activate("Redraw")
          continue
        end
      end
      next_proc.call(canvas, x, y, w, h, simulation)
    end
  end
end
setup_default_enum_item_mark(items, name, indent, space_ratio, props, options={}) { |item| ... } click to toggle source
# File lib/rabbit/theme/default-item-mark-setup/default-item-mark-setup.rb, line 87
def setup_default_enum_item_mark(items, name, indent, space_ratio, props,
                                 options={})
  option_value = Proc.new do |option_name, *args|
    setup_default_item_mark_get_option_value(options, option_name, *args)
  end

  indent_width = screen_x(indent)
  indent_width = option_value.call("indent_width", indent_width) || indent_width
  default_props = {
    "font-family" => @font_family,
  }.merge(props)

  delete_pre_draw_proc_by_name(name)
  delete_post_draw_proc_by_name(name)

  draw_order(items, indent_width, name) do |item|
    props = default_props
    props = props.merge(item.first.text_props) unless item.empty?
    if block_given?
      enum_mark = yield(item)
    else
      type = option_value.call("type", item)
      enum_mark = setup_default_enum_item_mark_type(type, item)
    end
    span(props, enum_mark)
  end

  space = @space * space_ratio
  margin_with(:bottom => space)
end
setup_default_enum_item_mark_type(type, item) click to toggle source
# File lib/rabbit/theme/default-item-mark-setup/default-item-mark-setup.rb, line 68
def setup_default_enum_item_mark_type(type, item)
  type ||= "numeric"
  normalized_type = type.to_s.downcase.gsub(/_/, '-')
  case normalized_type
  when "numeric"
    "#{item.order}. "
  when "lower-case", "upper-case"
    mark = normalized_type == "lower-case" ? "a" : "A"
    (item.order - 1).times do
      mark = mark.succ
    end
    "#{mark}. "
  else
    format = _("unknown enumeration item mark type: %s\n" \
               "Numeric type is used as fallback")
    setup_default_enum_item_mark_type("numeric", item)
  end
end
setup_default_item_mark(items, name, width, height, space_ratio, color, options={}) { |item, canvas, x, y, w, h, color, options| ... } click to toggle source
# File lib/rabbit/theme/default-item-mark-setup/default-item-mark-setup.rb, line 38
def setup_default_item_mark(items, name, width, height, space_ratio, color,
                            options={})
  option_value = Proc.new do |option_name, *args|
    setup_default_item_mark_get_option_value(options, option_name, *args)
  end

  mark_width = screen_x(width)
  mark_height = screen_y(height)
  mark_space = option_value.call("mark_space", mark_width) || mark_width
  indent_width = option_value.call("indent_width", mark_width, mark_space)
  indent_width ||= mark_width * 3

  delete_pre_draw_proc_by_name(name)
  delete_post_draw_proc_by_name(name)

  args = [items, indent_width, mark_width, mark_height, name]
  draw_mark(*args) do |item, canvas, x, y, w, h|
    x -= mark_space
    if block_given?
      yield(item, canvas, x, y, w, h, color, options)
    else
      type = options["type"]
      setup_default_item_mark_type(type, item, canvas, x, y, w, h, color)
    end
  end

  space = @space * space_ratio
  margin_with(:bottom => space)
end
setup_default_item_mark_get_option_value(options, name, *args) click to toggle source
# File lib/rabbit/theme/default-item-mark-setup/default-item-mark-setup.rb, line 1
def setup_default_item_mark_get_option_value(options, name, *args)
  value = options[name]
  if value and value.respond_to?(:call)
    value.call(*args)
  else
    value
  end
end
setup_default_item_mark_type(type, item, canvas, x, y, w, h, color) click to toggle source
# File lib/rabbit/theme/default-item-mark-setup/default-item-mark-setup.rb, line 10
def setup_default_item_mark_type(type, item, canvas, x, y, w, h, color)
  type ||= "rectangle"
  normalized_type = type.to_s.downcase
  case normalized_type
  when "rectangle"
    canvas.draw_rectangle(true, x, y, w, h, color)
  when "circle"
    canvas.draw_circle(true, x, y, w, h, color)
  when "check", "dash"
    props = {
      "font-family" => @font_family,
      "weight" => "bold",
      "foreground" => canvas.make_color(color).to_gdk_format,
    }.merge(item.first.text_props)

    layout = make_layout(span(props, entity(normalized_type)))
    text_width, text_height = layout.pixel_size
    canvas.draw_layout(layout,
                       x - text_width / 2.0,
                       y - text_height / 3.0)
  else
    format = _("unknown item mark type: %s\n" \
               "Rectangle type is used as fallback")
    canvas.logger.warn(format % type.inspect)
    setup_default_item_mark_type("rectangle")
  end
end
setup_lightning_talk_headline(head) click to toggle source
# File lib/rabbit/theme/lightning-talk-toolkit/lightning-talk-toolkit.rb, line 119
def setup_lightning_talk_headline(head)
  class << head
    def takahashi(params)
      proc_name = params[:proc_name]
      
      clear_pre_draw_procs
      clear_post_draw_procs

      font_params = {
        :size => params[:size],
        :color => params[:color],
        :family => params[:family],
      }
      font(font_params)
      self.wrap_mode = params[:wrap_mode]
      self.horizontal_centering = params[:horizontal_centering]
      self.justify = params[:justify]

      substitute_newline

      orig_x = orig_y = orig_w = orig_h = nil
      add_pre_draw_proc(proc_name) do |canvas, x, y, w, h, simulation|
        orig_x, orig_y, orig_w, orig_h = x, y, w, h
        [x, y, w, h]
      end
      
      add_post_draw_proc(proc_name) do |canvas, x, y, w, h, simulation|
        if empty?
          [orig_x, orig_y, orig_w, orig_h]
        else
          [x, y, w, h]
        end
      end

      as_large_as_possible = slide["as-large-as-possible"]
      if as_large_as_possible.nil?
        as_large_as_possible = params[:as_large_as_possible]
      else
        as_large_as_possible = as_large_as_possible == "true"
      end
      if as_large_as_possible
        lightning_talk_as_large_as_possible(params)
      end
    end
    alias lightning_talk takahashi

    private
    def lightning_talk_as_large_as_possible(params)
      proc_name = params[:proc_name]
      as_large_as_possible(proc_name)
    end
  end
end
setup_lightning_talk_slide(slide) click to toggle source
# File lib/rabbit/theme/lightning-talk-toolkit/lightning-talk-toolkit.rb, line 35
def setup_lightning_talk_slide(slide)
  class << slide
    attr_writer :lightning_talk_default_params
    def lightning_talk_default_params
      @lightning_talk_default_params ||= {}
    end
    
    def takahashi(params={}, &block)
      return unless lightning_talk?

      params = lightning_talk_default_params.merge(params)

      clear_pre_draw_procs
      clear_post_draw_procs

      self.vertical_centering = true
      self.horizontal_centering = true

      lightning_talk_setup_background(params)
      lightning_talk_setup_contact_information(params)

      headline.lightning_talk(params)
      block.call(self, headline) if block
    end
    alias lightning_talk takahashi

    def lightning_talk?
      body.empty? or
        ((headline.empty? or !headline.visible?) and
         body.elements.all? {|elem| elem.is_a?(Element::Image)})
    end
    alias takahashi? lightning_talk?
    
    private
    def lightning_talk_setup_background(params)
      proc_name = params[:proc_name]
      background_color = params[:background_color]
      add_pre_draw_proc(proc_name) do |canvas, x, y, w, h, simulation|
        unless simulation
          args = [
            true,
            x - margin_left,
            y - margin_top,
            w + margin_left + margin_right,
            h + margin_top + margin_bottom,
            background_color,
          ]
          canvas.draw_rectangle(*args)
        end
        [x, y, w, h]
      end
    end

    def lightning_talk_setup_contact_information(params)
      proc_name = params[:proc_name]
      contact_information = params[:contact_information]
      contact_information_size = params[:contact_information_size]
      contact_information_family = params[:contact_information_family]
      contact_information_color = params[:contact_information_color]

      if contact_information and /\A\s*\z/ !~ contact_information
        add_post_draw_proc(proc_name) do |canvas, x, y, w, h, simulation|
          unless simulation
            text = Element::Text.new(contact_information)
            params = {
              :size => contact_information_size,
              :family => contact_information_family,
            }
            text.font(params)
            text.align = Pango::Alignment::RIGHT
            text.compile(canvas, x, y, w, h)
            text.layout.set_width(width * Pango::SCALE)
            text_x = margin_left
            text_y = canvas.height - margin_bottom - text.height
            canvas.draw_layout(text.layout, text_x, text_y,
                               contact_information_color)
          end
          [x, y, w, h]
        end
      end
    end
  end
end
setup_rotate_zoom_effect_slide(slide) click to toggle source
# File lib/rabbit/theme/rotate-zoom-effect/rotate-zoom-effect.rb, line 1
def setup_rotate_zoom_effect_slide(slide)
  class << slide
    def rotate_zoom_effect
      angle = 0
      last_angle = 360
      last_scale_x = 1
      last_scale_y = 1
      idle_id = nil
      effected = false
      add_around_draw_proc do |canvas, x, y, w, h, simulation, next_proc|
        canvas.save_context do
          if !simulation and !effected
            angle = [angle + last_angle * 0.05, last_angle].min
            ratio = angle / last_angle
            scale_x = last_scale_x * ratio
            scale_y = last_scale_y * ratio
            translate(canvas, angle, scale_x, scale_y)
            idle_id ||= Gtk.idle_add do
              continue = (angle != last_angle and
                          canvas.current_slide == self)
              unless continue
                idle_id = nil
                effected = true
              end
              canvas.activate("Redraw")
              continue
            end
          end
          next_proc.call(canvas, x, y, w, h, simulation)
        end
      end
    end

    private
    def translate(canvas, angle, scale_x, scale_y)
      center_x = canvas.width / 2
      center_y = canvas.height / 2
      canvas.translate_context(center_x, center_y)
      canvas.rotate_context(angle)
      canvas.translate_context(-center_x, -center_y)
      canvas.scale_context(scale_x, scale_y)
    end
  end
end
setup_ruby_gnome2_item_paragraph(paragraphs, name, color, line_color, space_ratio) click to toggle source
# File lib/rabbit/theme/ruby-gnome2-item-mark/ruby-gnome2-item-mark.rb, line 3
def setup_ruby_gnome2_item_paragraph(paragraphs, name, color, line_color,
                                     space_ratio)
  paragraphs.delete_pre_draw_proc_by_name(name)
  paragraphs.delete_post_draw_proc_by_name(name)

  paragraphs.prop_set("foreground", color)
  space = @space * space_ratio

  post_draw_proc = Proc.new do |paragraph, canvas, x, y, w, h, simulation|
    unless simulation
      canvas.draw_line(x, y + space, x + w, y + space, line_color)
    end
    [x, y, w, h]
  end
  paragraphs.add_post_draw_proc(name, &post_draw_proc)
end
setup_title_on_image_headline(head) click to toggle source
# File lib/rabbit/theme/title-on-image-toolkit/title-on-image-toolkit.rb, line 106
def setup_title_on_image_headline(head)
  class << head
    def title_on_image(params)
      proc_name = params[:proc_name]
      color = params[:color]

      margin_set(0)

      clear_pre_draw_procs
      clear_post_draw_procs

      prop_set("foreground", color)
      hide

      orig_x = orig_y = orig_w = orig_h = nil
      add_pre_draw_proc(proc_name) do |canvas, x, y, w, h, simulation|
        orig_x, orig_y, orig_w, orig_h = x, y, w, h
        [x, y, w, h]
      end

      add_post_draw_proc(proc_name) do |canvas, x, y, w, h, simulation|
        [orig_x, orig_y, orig_w, orig_h]
      end
    end
  end
end
setup_title_on_image_slide(slide) click to toggle source
# File lib/rabbit/theme/title-on-image-toolkit/title-on-image-toolkit.rb, line 14
def setup_title_on_image_slide(slide)
  class << slide
    attr_accessor :applier

    attr_writer :title_on_image_default_params
    def title_on_image_default_params
      @title_on_image_default_params ||= {}
    end

    def title_on_image(params={}, &block)
      params = title_on_image_default_params.merge(params)

      clear_pre_draw_procs
      clear_post_draw_procs

      margin_set(0)

      self.vertical_centering = true
      self.horizontal_centering = true

      proc_name = params[:proc_name]
      shadow_color = params[:shadow_color]
      background_color = params[:background_color]
      title_background_color = params[:title_background_color]

      orig_x = orig_y = orig_w = orig_h = nil
      add_pre_draw_proc(proc_name) do |canvas, x, y, w, h, simulation|
        orig_x, orig_y, orig_w, orig_h = x, y, w, h
        unless simulation
          args = [
            true,
            x - margin_left,
            y - margin_top,
            w + margin_left + margin_right,
            h + margin_top + margin_bottom,
            background_color,
          ]
          canvas.draw_rectangle(*args)
        end
        [x, y, w, h]
      end

      add_post_draw_proc(proc_name) do |canvas, x, y, w, h, simulation|
        base_h = orig_h / 2 - headline.height / 2
        base_y = orig_y + base_h
        headline.show do
          unless simulation
            background_y = base_y - headline.padding_top
            background_h = headline.height
            canvas.draw_rectangle(true,
                                  0,            background_y,
                                  canvas.width, background_h,
                                  title_background_color)

            shadow_headline = headline.clone
            shadow_headline.font(:color => nil)
            shadow_layout = canvas.make_layout(shadow_headline.markuped_text)
            shadow_layout.set_width(w * Pango::SCALE)
            shadow_layout.set_alignment(Pango::Alignment::CENTER)

            font_size = headline.pixel_font_size
            move_x = @applier.screen_x(font_size.to_f / @applier.screen_size(10))
            move_y = @applier.screen_y(font_size.to_f / @applier.screen_size(20))
            canvas.draw_layout(shadow_layout, orig_x + move_x, base_y + move_y,
                               shadow_color)

            canvas.draw_layout(shadow_layout,
                               orig_x, base_y,
                               shadow_color,
                               :line_width => 5,
                               :stroke => true)
          end
          headline.draw_element(canvas,
                                orig_x, base_y, orig_w, base_h,
                                simulation)
        end
        [x, y, w, h]
      end

      headline.title_on_image(params)

      body.elements[0]["caption-color"] = params[:color]
    end

    def title_on_image?
      !headline.empty? and
        body.elements.size == 1 and
        body.elements[0].is_a?(Element::Image)
    end
  end
end
slide_params(slide) click to toggle source
# File lib/rabbit/theme/nari/nari.rb, line 111
def slide_params(slide)
  res = {}
  if !slide["size"].nil? && !slide["size"].empty?
    res[:size] = screen_size(slide["size"].to_i * Pango::SCALE)
  end
  %w(color family backgroundcolor).each do |key|
    unless slide[key].nil? || slide[key].empty?
      slide_key = key == "backgroundcolor" ? "background_color" : key
      res[slide_key.to_sym] = slide[key]
    end
  end
  res[:as_large_as_possible] = false unless res.empty?
  res
end