class Inkmake::InkImage

Attributes

area[R]
format[R]
inkfile[R]
prefix[R]
res[R]
showhide[R]
suffix[R]
variants[R]

Public Class Methods

new(inkfile, opts) click to toggle source
# File lib/inkmake.rb, line 682
def initialize(inkfile, opts)
  @inkfile = inkfile
  @prefix = opts[:prefix]
  variant_opts = {
    :rotate => opts[:rotate],
    :scale => opts[:scale],
    :dpi => opts[:dpi]
  }
  @variants = [InkVariant.new(self, "", variant_opts)]
  opts[:variants].each do |name, options|
    @variants << InkVariant.new(self, name, options)
  end
  @suffix = opts[:suffix]
  @res = opts[:res]
  @svg = opts[:svg]
  @format = opts[:format]
  @area = opts[:area]
  @showhide = opts[:showhide]
end

Public Instance Methods

svg_path() click to toggle source
# File lib/inkmake.rb, line 702
def svg_path
  File.expand_path(@svg || File.basename(@prefix + @suffix, ".*") + ".svg", inkfile.svg_path)
end
svg_res() click to toggle source
# File lib/inkmake.rb, line 706
def svg_res
  @svg_res ||=
    begin
      doc = REXML::Document.new File.read(svg_path)
      svgattr = doc.elements.to_a("//svg")[0].attributes
      if svgattr["width"] and svgattr["height"]
        InkscapeResolution.new(svgattr["width"], svgattr["height"], "uu")
      else
        nil
      end
    end
end
svg_showhide_file() click to toggle source
# File lib/inkmake.rb, line 719
def svg_showhide_file
  @svg_showhide_file ||=
    begin
      doc = REXML::Document.new File.read(svg_path)

      layers = {}
      REXML::XPath.each(doc, "//svg:g[@inkscape:groupmode='layer']").each do |e|
        label = e.attributes["label"]
        next if not label
        layers[label] = e
      end

      ids = {}
      REXML::XPath.each(doc, "//svg:*[@id]").each do |e|
        id = e.attributes["id"]
        next if not id
        ids[id] = e
      end

      @showhide.each do |sh|
        elms = nil
        if sh[:type] == :layer
          if sh[:name] == :all
            elms = layers.values
          else
            e = layers[sh[:name]]
            if not e
              raise InkFile::ProcessError, "Layer \"#{sh[:name]}\" not found in #{svg_path}"
            end
            elms = [e]
          end
        else
          e = ids[sh[:name]]
          if not e
            raise InkFile::ProcessError, "Id \"#{sh[:name]}\" not found in #{svg_path}"
          end
          elms = [e]
        end

        elms.each do |e|
          # NOTE: should be visibility for #ids to not affect flow etc?
          e.delete_attribute("display")
          # also remove display inside style attributes
          if e.attributes["style"]
            style_declarations = e.attributes["style"].split(";")
            style_declarations_to_keep = []
            style_declarations.each do | sd |
              property, value = sd.split(":", 2)
            if value && property == "display"
              # throw it out
            else
              style_declarations_to_keep.push(sd)
            end
            end
            e.attributes["style"] = style_declarations_to_keep.join(";")
          end
          if sh[:op] == :hide
            e.add_attribute("display", "none")
          else
            # show is a nop
          end
        end
      end

      f = Tempfile.new(["inkmake", ".svg"])
      doc.write(:output => f)
      f.flush
      f
    end
end