Class: Pixelart::Vector

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

Overview

holds a vector graphics image (in source)

Defined Under Namespace

Classes: Circle, Path, Shape

Instance Method Summary collapse

Constructor Details

#initialize(width, height, header: nil) ⇒ Vector

Returns a new instance of Vector.



77
78
79
80
81
82
83
# File 'lib/pixelart/vector.rb', line 77

def initialize( width, height, header: nil )
   @width  = width
   @height = height

   @header = header
   @shapes = []
end

Instance Method Details

#circle(cx:, cy:, r:, fill:) ⇒ Object



85
86
87
# File 'lib/pixelart/vector.rb', line 85

def circle( cx:, cy:, r:, fill: )
   @shapes << Circle.new( cx, cy, r, fill: fill )
end

#path(stroke: '#000000', fill: nil) ⇒ Object

note: default stroke (color) to black (#000000) for now - why? why not?



90
91
92
93
94
95
# File 'lib/pixelart/vector.rb', line 90

def path( stroke: '#000000', fill: nil )
   path = Path.new( stroke: stroke, fill: fill )
   @shapes << path

   path   ## note: MUST return "inner" path shape for chaining "dsl-like" methods / commands
end

#save(path, format: nil) ⇒ Object Also known as: write



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/pixelart/vector.rb', line 147

def save( path, format: nil )
  if format && format.downcase == 'png'  ## support png with image magick
    img = to_image
    img.save( path )
  else
   # make sure outdir exits
   outdir = File.dirname( path )
   FileUtils.mkdir_p( outdir )  unless Dir.exist?( outdir )
   File.open( path, 'w:utf-8' ) do |f|
      f.write( to_svg )
    end
  end
end

#to_imageObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/pixelart/vector.rb', line 99

def to_image
   ## use an empty image (canvas) with transparent background
   ##   as magick input image
   canvas = Image.new( @width, @height )
   canvas.save( MAGICK_INPUT )

   ## note: magick command line might get way too long, thus,
   ##   save commands to a script
   ## note: save magick input first (see above) before save script
   ##        will (auto-)create missing directories in path (if missing)

  File.open( MAGICK_SCRIPT, 'w:utf-8' ) do |f|
    f.write( "#{MAGICK_INPUT} \\\n" )
    @shapes.each do |shape|
      f.write( "#{shape.to_magick} \\\n" )
    end
    f.write( "-write #{MAGICK_OUTPUT}\n" )
  end

  MiniMagick::Tool::Magick.new do |magick|
    magick.script( MAGICK_SCRIPT )
  end

  Image.read( MAGICK_OUTPUT )
end

#to_svgObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/pixelart/vector.rb', line 126

def to_svg
  buf = String.new('')

  if @header
    buf << "<!--\n"
    ## auto-indent lines by five (5) spaces for now
    @header.each_line do |line|
      buf << "     #{line}"
    end
    buf << "\n-->\n\n"
  end

  buf << %Q{<svg version="1.1" width="#{@width}" height="#{@height}" xmlns="http://www.w3.org/2000/svg">\n}
  @shapes.each do |shape|
     buf << "  #{shape.to_svg}\n"
  end
  buf << "</svg>"
  buf
end