2 # $Id: redact.rb 44 2005-06-08 17:35:49Z tilman $
4 # Copyright (c) 2005 Tilman Sauerbeck (tilman at code-monkey de)
6 # Permission is hereby granted, free of charge, to any person obtaining
7 # a copy of this software and associated documentation files (the
8 # "Software"), to deal in the Software without restriction, including
9 # without limitation the rights to use, copy, modify, merge, publish,
10 # distribute, sublicense, and/or sell copies of the Software, and to
11 # permit persons to whom the Software is furnished to do so, subject to
12 # the following conditions:
14 # The above copyright notice and this permission notice shall be
15 # included in all copies or substantial portions of the Software.
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 require "redact/program"
30 class Object # :nodoc:
35 def to_embryo(collection)
36 unless strip.length > 0
37 raise(Redact::RedactError, "invalid embryo code")
42 {"part" => collection.parts,
43 "program" => collection.programs}.each do |entity, ary|
44 s.gsub!(/#{entity.upcase}:\"(.*?)\"/) do |m|
45 found = ary.find { |(k, p)| p.name == $1 }
47 raise(Redact::RedactError,
48 "#{entity} not found - #{$1}")
62 class RedactError < StandardError; end
65 attr_reader :collections, :data, :collection_dir, :image_dir,
73 @font_dir = FontDirectory.new
74 @image_dir = ImageDirectory.new
75 @collection_dir = CollectionDirectory.new
77 @collections = Hash.new do |h, k|
78 c = Collection.new(k, h.size)
80 @collection_dir << CollectionDirectoryEntry.new(c)
87 def collection(name) # :yields: collection
88 c = @collections[name]
90 block_given? ? (yield c) : c
99 {"compiler" => ["redact"],
101 "feature_ver" => [1],
102 "font_dir" => [@font_dir, :sub],
103 "image_dir" => [@image_dir, :sub],
104 "collection_dir" => [@collection_dir, :sub],
109 class DataHash < Hash # :nodoc:
115 super(key, HashEntry.new(key, value))
119 class HashEntry # :nodoc:
120 attr_reader :key, :value
122 def initialize(key, value)
123 @key = key.to_str.dup.freeze
124 @value = value.to_str.dup.freeze
133 class FontDirectory < Array # :nodoc:
136 "Edje_Font_Directory"
139 def to_eet_properties
140 {"entries" => [self]}
144 class FontDirectoryEntry # :nodoc:
145 attr_reader :filename
147 def initialize(filename)
148 @filename = find_font(filename.to_str).freeze
153 "Edje_Font_Directory_Entry"
156 def to_eet_properties
157 {"entry" => [@filename]}
162 [".", OPTIONS.font_dir].each do |d|
163 f2 = File.join(d, file)
164 return f2 if File.file?(f2)
171 class ImageDirectory < Array # :nodoc:
180 "Edje_Image_Directory"
183 def to_eet_properties
184 {"entries" => [self]}
188 class ImageDirectoryEntry # :nodoc:
189 attr_reader :filename, :image, :id
191 def initialize(filename)
192 @filename = find_image(filename.to_str).freeze
193 @image = Imlib2::Image.load(@filename)
195 @source_type = 1 # COMP
196 @source_param = 1 # COMP
201 raise(ArgumentError, "invalid ID - #{id}")
203 raise(RedactError, "ID already set")
211 "Edje_Image_Directory_Entry"
214 def to_eet_properties
215 {"entry" => [@filename],
216 "source_type" => [@source_type],
217 "source_param" => [@source_param],
223 [".", OPTIONS.image_dir].each do |d|
224 f2 = File.join(d, file)
225 return f2 if File.file?(f2)
232 class CollectionDirectory < Array # :nodoc:
235 "Edje_Part_Collection_Directory"
238 def to_eet_properties
239 {"entries" => [self]}
243 class CollectionDirectoryEntry # :nodoc:
245 @name = col.name.to_str.dup.freeze
251 "Edje_Part_Collection_Directory_Entry"
254 def to_eet_properties
261 attr_reader :name, :id, :data, :min, :max, :parts, :programs,
264 def initialize(name, id)
265 @name = name.to_str.dup.freeze
279 @script = v.to_str.dup
282 def part(name, type = :invalid) # :yields: part
296 "invalid part type - #{type.to_s}")
299 p = klass.new(self, @parts.size, name)
303 block_given? ? (yield p) : p
306 def program(name, type = :invalid) # :yields: program
322 "invalid program type - #{type.to_s}")
325 p = klass.new(self, @programs.size, name)
329 block_given? ? (yield p) : p
345 def has_embryo?(search_programs = true)
346 if !@script.nil? && @script.to_str.strip.length > 0
348 elsif search_programs
349 p = @programs.find do |(k, p)|
350 p.is_a?(ExecScriptProgram)
359 "Edje_Part_Collection"
362 def to_eet_properties
363 # sort parts and programs by id, since edje doesn't sort
365 parts = @parts.to_a.map { |(key, value)| value }.sort
366 programs = @programs.to_a.map { |(key, value)| value }.sort
370 "programs" => [programs],
372 "prop.min.w" => [@min[0]],
373 "prop.min.h" => [@min[1]],
374 "prop.max.w" => [@max[0]],
375 "prop.max.h" => [@max[1]]}
379 EDJE = Redact::Edje.new