Eski 11.04.2008   #1 (permalink)
 
Katılma Tarihi: Nis 2008
Nereden: istanbul
Yaş: 16
Mesajlar: 3
Varsayılan Ruby ile SVG grafik

geçenlerde can sıkıntısına yazdığım svg librarysi.en son bitmap eklemeye çalıştım ama sorun çıkartıyor.nabalım artık bir ara tekrar eklemeyi denerim
Kod:
#require 'RMagick'
#include Magick
#Gives an error??

def get_style(styleh)
	style = nil
	if !styleh.nil?
		style = ""
		styleh.each do |key,value|
			style += "#{key}:#{value.to_s}; "
		end
		style.chop!
	end
	style
end

def get_attrs(attrh)
	attrs = nil
	if !attrh.nil?
		attrs = ""
		attrh.each do |key,value|
			attrs += " #{key}=\"#{value}\""
		end
	end
	attrs
end

def get_path(d)
	if d.respond_to?("each")
		d.each do |e|
			if e.respond_to?("join")
				e = e.join(" ")
			end
		end
		d = d.join(" ")
	end
	d
end

def get_points(pts)
	points = nil
    unless pts.respond_to?("each")
		pts = pts.split(" ")
	end
	pts.each do |p|
		unless p.respond_to?("each")
			p = p.split(",")
		end
		points += "#{p[0].to_i},#{p[1].to_i} "
	end
	points.chop
end

def mult_str(str,num)
	res = ""
	num.times{res+=str}
	res
end

class Comment
    attr_accessor :content
    def initialize(content="")
        @content = content
    end
    def to_s
        if @content.empty?
            return ""
        end
        "<!-- #{@content} -->"
    end
end

class Tag
	attr_accessor :children
	attr_accessor :attrs
	include Enumerable
	def initialize(name,attrs=Hash.new,children=Array.new)
		@name = name
		@attrs = attrs
		@children = children
	end
	def to_s(indent=0)
		res = "#{mult_str("\t",indent)}<#{@name}"
		atts = get_attrs(@attrs)
		unless atts.nil?
			res += atts
		end
		unless @children.empty?
			res += ">\n"
			indent += 1
			@children.each do |o|
				begin
					res += o.to_s(indent) + "\n"
				rescue ArgumentError
					res += "#{mult_str("\t",indent)}#{o.to_s}"
				end
			end
			indent -= 1
			res += "#{mult_str("\t",indent)}</#{@name}>"
		else
			res += "/>"
		end
		return res
	end
	def add_child(obj)
		if obj.respond_to?("each") and obj.class != String
			obj.each do |o|
				@children << o
            end
		else
			@children << obj	
        end
		return obj
    end
	def <<(obj)
		return add_child(obj)
    end
	def each
		@children.each do |o|
			yield o
		end
	end
    def method_missing(name)
        if @attrs.keys.include?(name)
            return @atttrs[name]
        end
        raise "Tag: No method or attribute called #{name}"
    end
end

class Rectangle<Tag
	def initialize(x,y,width,height,style=nil,attrs=Hash.new)
		attrs["x"]=x
		attrs["y"]=y
		attrs["width"]=width
		attrs["height"]=height
		unless style.nil?
				attrs["style"]=get_style(style)
        end
		super("rect",attrs)
	end
end

class Circle<Tag
	def initialize(x,y,r,style=nil,attrs=Hash.new)
		atrs["cx"]=x
		attrs["cy"]=y
		attrs["r"]=r
		unless style.nil?
				attrs["style"]=get_style(style)
        end
		super("circle",attrs)
	end
end

class Ellipse<Tag
	def initialize(x,y,rx,ry,style=nil,attrs=Hash.new)
		attrs["cx"]=x
		attrs["cy"]=y
		attrs["rx"]=rx
		attrs["ry"]=ry
		unless style.nil?
				attrs["style"]=get_style(style)
        end
		super("ellipse",attrs)
	end
end

class Line<Tag
	def initialize(x1,y1,x2,y2,style=nil,attrs=Hash.new)
		attrs["x1"]=x1
		attrs["y1"]=y1
		attrs["x2"]=x2
		attrs["y2"]=y2
		unless style.nil?
				attrs["style"]=get_style(style)
        end
		super("line",attrs)
	end
end

class Text<Tag
    include Comparable
	def initialize(x,y,text,style=nil,attrs=Hash.new)
		unless style.nil?
				attrs["style"]=get_style(style)
        end
		children = [text]
		if attrs.nil?
			attrs = Hash.new
        end
		attrs["x"]=x
		attrs["y"]=y
		unless style.nil?
				attrs["style"]=get_style(style)
        end
		children = [text]
		super("text",attrs,children)
	end
	def text
		@children[0]
	end
	def text=(obj)
		if obj.class == Text
			@children[0] = obj.text
		else
			@children[0] = obj
		end
	end
    def length
        self.text.length
    end
    def <=>(o)
        self.length <=> o.length
    end
end

class Polygon<Tag
	def initialize(points,style=nil,attrs=Hash.new)
		if attrs.nil?
			attrs = Hash.new
        end
		attrs["points"]=get_points(points)
		unless style.nil?
				attrs["style"]=get_style(style)
        end
		super("polygon",attrs)
	end
end

class Polyline<Tag
	def initialize(points,style=nil,attrs=Hash.new)
		if attrs.nil?
			attrs = Hash.new
        end
		attrs["points"]=get_points(points)
		unless style.nil?
				attrs["style"]=get_style(style)
        end
		super("polyline",attrs)
	end
end

class Path<Tag
	def initialize(d,style=nil,attrs=Hash.new)
		if attrs.nil?
			attrs = Hash.new
        end
		attrs["d"]=get_path(d)
		unless style.nil?
				attrs["style"]=get_style(style)
        end
		super("path",attrs)
	end
end

class Bitmap<Tag
    def initialize(x,y,filename,zoom=1,attrs=Hash.new)
        attrs['x'],attrs['y'] = x,y
        raise "No file called #{filename}" unless File.exist?("filename")
        bmp = Image.ping(filename)[0]
        if zoom.respond_to?("each")
            attrs['width'],attrs['height'] = bmp.columns*zoom[0].to_f, bmp.rows*zoom[1].to_f
        else
            attrs['width'],attrs['height'] = bmp.columns*zoom.to_f, bmp.rows*zoom.to_f
        end
        bmp.destroy!
        attrs['xlink:href'] = filename
        super("image",attrs)
    end
    def filename
        @attrs['xlink:href']
    end
    def image
        Image.ping(self.filename)[0]
    end
end

class SVG
	attr_accessor :objects
	attr_accessor :defs
    attr_accessor :xmlns
	include Enumerable
    
	def initialize(width=400,height=400,desc=nil)
		@width = width
		@height = height
		@children = Array.new
		@defs = Array.new
		@desc = desc
        @xmlns = Hash.new
	end
    
    def get_xmlns
        if @xmlns.empty?
            return ""
        end
        xmlnss = []
        @xmlns.each do |k,v|
            xmlnss << "xmlns:#{k}=\"#{v}\""
        end
        xmlnss.join(" ")
    end
    
    def has_bitmap?
        fnd = false
        @children.each do |c|
            if c.class == Bitmap
                fnd = true
                break
            end
        end
        fnd
    end
    
	def <<(object)
		return add_obj(object)
	end
    
	def add_obj(object)
		@children << object
		return object
	end
    
    def to_s
        if self.has_bitmap?
            @xmlns["xlink"] = "http://www.w3.org/1999/xlink"
        end
        xmlnss = get_xmlns
		indent = 1
		res = "<?xml version=\"1.0\" standalone=\"no\"?>
<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"
	\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">
<svg width=\"#{@width}\" height=\"#{@height}\" version=\"1.1\"
	xmlns=\"http://www.w3.org/2000/svg\" #{xmlnss}>\n"
		unless @desc.nil?
			if @desc.include?(":") and @desc[@desc.index(":")-1]!="\\"
				descs = @desc.split(":",2) #desc string will be splitted into (max.)two, from the first colon
				res += "#{mult_str("\t",indent)}<title>#{descs[0]}</title>\n"
				res += "#{mult_str("\t",indent)}<desc>#{descs[1]}</desc>\n"
			else
				res += "#{mult_str("\t",indent)}<desc>#{@desc}</desc>\n"
			end
		end
		unless @defs.empty?
			res += "#{mult_str("\t",indent)}<defs>\n"
			indent += 1
			@defs.each do |d|
				res += "#{d.to_s(indent)}\n"
			end
			indent -= 1
			res += "#{mult_str("\t",indent)}</defs>\n"
		end
		@children.each do |o|
			res += "#{mult_str("\t",indent)}#{o.to_s}\n"
		end
		indent -= 1
		res += "</svg>"
	end
	def each
		@children.each do |o|
			yield o
		end
	end
end

if __FILE__ == $0
	puts "TEST TEST TEST!!"
	s = SVG.new(400,400,"Test:TEST TEST TEST")
	blur = Tag.new("feGaussianBlur",{"in"=>"SourceGraphic","stdDeviation"=>2})
	s.defs << Tag.new("filter",{"id"=>"Gaussian_Blur"},[blur])
	s << Circle.new(20,20,10,{"fill"=>"red","filter"=>"url(#Gaussian_Blur)"})
	s << Rectangle.new(20,20,10,10,{"fill"=>"blue","fill-opacity"=>0.5},{"rx"=>"1"})
	s << Ellipse.new(50,50,25,15,{"fill"=>"purple"})
	s << Line.new(15,30,25,25,{"stroke"=>"black","stroke-width"=>"2px"})
	s << Text.new(100,100,"asd!!")
	File.open("deneme.svg","w") do |f|
		f.puts(s.to_s)
	end
    puts s.to_s
end
ve bu library'i kullanarak yaptığım örnek çubuk grafik şeysi
Kod:
require 'svg'

Colors = [:red,:olive,:purple,:lime,:fuchsia,:aqua,:orange,:yellow,:green]

def get_rect(place,number,coeff)
    y = place*60+10
    width = number*coeff
    rect = Rectangle.new(10,y,width,50,{:fill=>Colors[place],"stroke-width"=>2,:stroke=>:black,"stroke-opacity"=>0.5})
    
    if number%1 == 0
        number = number.to_i
    end
    txt = Text.new(20,y+30,number)
    return rect,txt
end

if ARGV.length > 0
    numbers = ARGV
    numbers.map!{ |x| x = x.to_f }
else
    numbers = []
    inp = 0
    puts "please enter some numbers(-1 to finish input)"
    while inp > -1
        inp = gets.chomp.to_f
        if inp > -1
            numbers << inp
        end
    end
end

s = SVG.new(420,numbers.length*60+10)
s << Comment.new("Created with KuzuX's SVG Graphic Thingie")
max = numbers.max
if  max > 400
    coeff = max/400
elsif max < 400
    coeff = 400/max
end

i=0
numbers.each do |n|
    r,t = get_rect(i,n,coeff)
    s << r
    s << t
    i += 1
end

File.open("graph.svg","w") do |f|
    f.puts(s.to_s)
end
kuzux Şuanda Forumda Değil   Alıntı yaparak cevapla
Cevap


Şu Anda Konuyu İnceleyen Aktif Kullanıcılar: 1 (0 üye ve 1 misafir)
 
Konu Araçları



Şu anki forum saati: 19:58.


cnt hizmet sağlayan firma
ForumTi.com'un yapımı ve yayınlanması CNT'ye aittir.
Sitedeki içerikleri foruma ücretsiz şekilde üye olabilen ziyaretçiler oluşturur. Bu içeriklerin sorumluluğu yazana aittir.
Eğer yasak ve aykırı içerik tespit edilirse site yöneticilerine bu konular bildirilir ve kaldırılır. Site yönetimi haberdar edildiğinde sonuç alınamaz ise servis sağlayıcı CNT'ye bildiride bulunabilirsiniz.
vBulletin® v3.7.2, Copyright ©2000-2008, Jelsoft Enterprises Ltd. Search Engine Friendly URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259