|
|
#1 (permalink) |
|
Katılma Tarihi: Nis 2008
Nereden: istanbul
Yaş: 16
Mesajlar: 3
|
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 denerimKod:
#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
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
|
|
|
|
![]() |
| Şu Anda Konuyu İnceleyen Aktif Kullanıcılar: 1 (0 üye ve 1 misafir) | |
| Konu Araçları | |
|
|