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.



581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
# File 'lib/inkmake.rb', line 581

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.



579
580
581
# File 'lib/inkmake.rb', line 579

def area
  @area
end

#formatObject (readonly)

Returns the value of attribute format.



579
580
581
# File 'lib/inkmake.rb', line 579

def format
  @format
end

#inkfileObject (readonly)

Returns the value of attribute inkfile.



579
580
581
# File 'lib/inkmake.rb', line 579

def inkfile
  @inkfile
end

#prefixObject (readonly)

Returns the value of attribute prefix.



579
580
581
# File 'lib/inkmake.rb', line 579

def prefix
  @prefix
end

#resObject (readonly)

Returns the value of attribute res.



579
580
581
# File 'lib/inkmake.rb', line 579

def res
  @res
end

#showhideObject (readonly)

Returns the value of attribute showhide.



579
580
581
# File 'lib/inkmake.rb', line 579

def showhide
  @showhide
end

#suffixObject (readonly)

Returns the value of attribute suffix.



579
580
581
# File 'lib/inkmake.rb', line 579

def suffix
  @suffix
end

#variantsObject (readonly)

Returns the value of attribute variants.



579
580
581
# File 'lib/inkmake.rb', line 579

def variants
  @variants
end

Instance Method Details

#svg_pathObject



601
602
603
# File 'lib/inkmake.rb', line 601

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

#svg_resObject



605
606
607
608
609
610
611
612
613
614
615
616
# File 'lib/inkmake.rb', line 605

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



618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
# File 'lib/inkmake.rb', line 618

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")
      doc.write(:output => f)
      f.flush
      f
    end
end