2 # $Id: redact.rb 6 2005-03-26 20:24:40Z 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
98 {"compiler" => ["redact"],
100 "feature_ver" => [1],
101 "font_dir" => [@font_dir, :sub],
102 "image_dir" => [@image_dir, :sub],
103 "collection_dir" => [@collection_dir, :sub],
108 class DataHash < Hash # :nodoc:
114 super(key, HashEntry.new(key, value))
118 class HashEntry # :nodoc:
119 attr_reader :key, :value
121 def initialize(key, value)
122 @key = key.to_str.dup.freeze
123 @value = value.to_str.dup.freeze
131 class FontDirectory < Array # :nodoc:
133 "Edje_Font_Directory"
136 def to_eet_properties
137 {"entries" => [self]}
141 class FontDirectoryEntry # :nodoc:
142 attr_reader :filename
144 def initialize(filename)
145 @filename = filename.to_str.dup.freeze
149 "Edje_Font_Directory_Entry"
152 def to_eet_properties
153 {"entry" => [@filename]}
157 class ImageDirectory < Array # :nodoc:
165 "Edje_Image_Directory"
168 def to_eet_properties
169 {"entries" => [self]}
173 class ImageDirectoryEntry # :nodoc:
174 attr_reader :filename, :image, :id
176 def initialize(filename)
177 @filename = filename.to_str.dup.freeze
178 @image = Imlib2::Image.load(@filename)
180 @source_type = 1 # COMP
181 @source_param = 1 # COMP
186 raise(ArgumentError, "invalid ID - #{id}")
188 raise(RedactError, "ID already set")
195 "Edje_Image_Directory_Entry"
198 def to_eet_properties
199 {"entry" => [@filename],
200 "source_type" => [@source_type],
201 "source_param" => [@source_param],
206 class CollectionDirectory < Array # :nodoc:
208 "Edje_Part_Collection_Directory"
211 def to_eet_properties
212 {"entries" => [self]}
216 class CollectionDirectoryEntry # :nodoc:
218 @name = col.name.to_str.dup.freeze
223 "Edje_Part_Collection_Directory_Entry"
226 def to_eet_properties
233 attr_reader :name, :id, :data, :min, :max, :parts, :programs
234 attr_accessor :script
236 def initialize(name, id)
237 @name = name.to_str.dup.freeze
250 def part(name, type = :invalid) # :yields: part
264 "invalid program type - #{type.to_s}")
267 p = klass.new(self, @parts.size, name)
271 block_given? ? (yield p) : p
274 def program(name, type = :invalid) # :yields: program
290 "invalid program type - #{type.to_s}")
293 p = klass.new(self, @programs.size, name)
297 block_given? ? (yield p) : p
313 def has_embryo?(search_programs = true)
314 if !@script.nil? && @script.to_str.strip.length > 0
316 elsif search_programs
317 p = @programs.find do |(k, p)|
318 p.is_a?(ExecScriptProgram)
326 "Edje_Part_Collection"
329 def to_eet_properties
330 # sort parts and programs by id, since edje doesn't sort
332 parts = @parts.to_a.map { |(key, value)| value }.sort
333 programs = @programs.to_a.map { |(key, value)| value }.sort
337 "programs" => [programs],
339 "prop.min.w" => [@min[0]],
340 "prop.min.h" => [@min[1]],
341 "prop.max.w" => [@max[0]],
342 "prop.max.h" => [@max[1]]}
346 EDJE = Redact::Edje.new