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.



712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
# File 'lib/inkmake.rb', line 712

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.



710
711
712
# File 'lib/inkmake.rb', line 710

def area
  @area
end

#formatObject (readonly)

Returns the value of attribute format.



710
711
712
# File 'lib/inkmake.rb', line 710

def format
  @format
end

#inkfileObject (readonly)

Returns the value of attribute inkfile.



710
711
712
# File 'lib/inkmake.rb', line 710

def inkfile
  @inkfile
end

#prefixObject (readonly)

Returns the value of attribute prefix.



710
711
712
# File 'lib/inkmake.rb', line 710

def prefix
  @prefix
end

#resObject (readonly)

Returns the value of attribute res.



710
711
712
# File 'lib/inkmake.rb', line 710

def res
  @res
end

#showhideObject (readonly)

Returns the value of attribute showhide.



710
711
712
# File 'lib/inkmake.rb', line 710

def showhide
  @showhide
end

#suffixObject (readonly)

Returns the value of attribute suffix.



710
711
712
# File 'lib/inkmake.rb', line 710

def suffix
  @suffix
end

#variantsObject (readonly)

Returns the value of attribute variants.



710
711
712
# File 'lib/inkmake.rb', line 710

def variants
  @variants
end

Instance Method Details

#svg_pathObject



732
733
734
# File 'lib/inkmake.rb', line 732

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

#svg_resObject



736
737
738
739
740
741
742
743
744
745
746
747
# File 'lib/inkmake.rb', line 736

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



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
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
# File 'lib/inkmake.rb', line 749

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