Class: Inkmake::InkImage

Inherits:
Object
  • Object
show all
Defined in:
lib/inkmake.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inkfile, opts) ⇒ InkImage

Returns a new instance of InkImage.



682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
# 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

Instance Attribute Details

#areaObject (readonly)

Returns the value of attribute area.



680
681
682
# File 'lib/inkmake.rb', line 680

def area
  @area
end

#formatObject (readonly)

Returns the value of attribute format.



680
681
682
# File 'lib/inkmake.rb', line 680

def format
  @format
end

#inkfileObject (readonly)

Returns the value of attribute inkfile.



680
681
682
# File 'lib/inkmake.rb', line 680

def inkfile
  @inkfile
end

#prefixObject (readonly)

Returns the value of attribute prefix.



680
681
682
# File 'lib/inkmake.rb', line 680

def prefix
  @prefix
end

#resObject (readonly)

Returns the value of attribute res.



680
681
682
# File 'lib/inkmake.rb', line 680

def res
  @res
end

#showhideObject (readonly)

Returns the value of attribute showhide.



680
681
682
# File 'lib/inkmake.rb', line 680

def showhide
  @showhide
end

#suffixObject (readonly)

Returns the value of attribute suffix.



680
681
682
# File 'lib/inkmake.rb', line 680

def suffix
  @suffix
end

#variantsObject (readonly)

Returns the value of attribute variants.



680
681
682
# File 'lib/inkmake.rb', line 680

def variants
  @variants
end

Instance Method Details

#svg_pathObject



702
703
704
# File 'lib/inkmake.rb', line 702

def svg_path
  File.expand_path(@svg || File.basename(@prefix + @suffix, ".*") + ".svg", inkfile.svg_path)
end

#svg_resObject



706
707
708
709
710
711
712
713
714
715
716
717
# 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_fileObject



719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
# 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