2 # Copyright (c) 2005 Tilman Sauerbeck (tilman at code-monkey de)
4 # Permission is hereby granted, free of charge, to any person obtaining
5 # a copy of this software and associated documentation files (the
6 # "Software"), to deal in the Software without restriction, including
7 # without limitation the rights to use, copy, modify, merge, publish,
8 # distribute, sublicense, and/or sell copies of the Software, and to
9 # permit persons to whom the Software is furnished to do so, subject to
10 # the following conditions:
12 # The above copyright notice and this permission notice shall be
13 # included in all copies or substantial portions of the Software.
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 require "redact/program"
28 class Object # :nodoc:
33 def to_embryo(collection)
34 unless strip.length > 0
35 raise(Redact::RedactError, "invalid embryo code")
40 {"part" => collection.parts,
41 "program" => collection.programs}.each do |entity, ary|
42 s.gsub!(/#{entity.upcase}:\"(.*?)\"/) do |m|
43 found = ary.find { |(k, p)| p.name == $1 }
45 raise(Redact::RedactError,
46 "#{entity} not found - #{$1}")
60 class RedactError < StandardError; end
63 attr_reader :collections, :data, :collection_dir, :image_dir,
71 @font_dir = FontDirectory.new
72 @image_dir = ImageDirectory.new
73 @collection_dir = CollectionDirectory.new
75 @collections = Hash.new do |h, k|
76 c = Collection.new(k, h.size)
78 @collection_dir << CollectionDirectoryEntry.new(c)
85 def collection(name) # :yields: collection
86 c = @collections[name]
88 block_given? ? (yield c) : c
97 {"compiler" => ["redact"],
100 "font_dir" => [@font_dir, :sub],
101 "image_dir" => [@image_dir, :sub],
102 "collection_dir" => [@collection_dir, :sub],
107 class DataHash < Hash # :nodoc:
113 super(key, HashEntry.new(key, value))
117 class HashEntry # :nodoc:
118 attr_reader :key, :value
120 def initialize(key, value)
121 @key = key.to_str.dup.freeze
122 @value = value.to_str.dup.freeze
131 class FontDirectory < Array # :nodoc:
134 "Edje_Font_Directory"
137 def to_eet_properties
138 {"entries" => [self]}
142 class FontDirectoryEntry # :nodoc:
143 attr_reader :save_as, :filename, :alias
145 def initialize(fn_alias, filename)
146 @save_as = fn_alias.to_str.dup.freeze
147 @filename = filename.to_str.dup.freeze
148 @alias = "Edje." + fn_alias.sub(/.[^.]+$/, "").freeze
153 "Edje_Font_Directory_Entry"
156 def to_eet_properties
157 {"entry" => [@alias]}
161 class ImageDirectory < Array # :nodoc:
170 "Edje_Image_Directory"
173 def to_eet_properties
174 {"entries" => [self]}
178 class ImageDirectoryEntry # :nodoc:
179 attr_reader :filename, :image, :id
181 def initialize(im_alias, filename)
182 @alias = im_alias.to_str.dup.freeze
183 @filename = filename.to_str.dup.freeze
184 @image = Imlib2::Image.load(@filename)
186 @source_type = 1 # COMP
187 @source_param = 1 # COMP
192 raise(ArgumentError, "invalid ID - #{id}")
194 raise(RedactError, "ID already set")
202 "Edje_Image_Directory_Entry"
205 def to_eet_properties
206 {"entry" => [@alias],
207 "source_type" => [@source_type],
208 "source_param" => [@source_param],
213 class CollectionDirectory < Array # :nodoc:
216 "Edje_Part_Collection_Directory"
219 def to_eet_properties
220 {"entries" => [self]}
224 class CollectionDirectoryEntry # :nodoc:
226 @name = col.name.to_str.dup.freeze
232 "Edje_Part_Collection_Directory_Entry"
235 def to_eet_properties
242 attr_reader :name, :id, :data, :min, :max, :parts, :programs,
245 def initialize(name, id)
246 @name = name.to_str.dup.freeze
261 @script = (File.exist?(v) ? File.read(v) : v).dup
264 def part(name, type = :invalid) # :yields: part
278 "invalid part type - #{type.to_s}")
281 p = klass.new(self, @parts.size, name)
285 block_given? ? (yield p) : p
288 def program(name, type = :invalid) # :yields: program
304 "invalid program type - #{type.to_s}")
307 p = klass.new(self, @programs.size, name)
311 block_given? ? (yield p) : p
327 def has_embryo?(search_programs = true)
328 if !@script.nil? && @script.to_str.strip.length > 0
330 elsif search_programs
331 p = @programs.find do |(k, p)|
332 p.is_a?(ExecScriptProgram)
341 "Edje_Part_Collection"
344 def to_eet_properties
345 # sort parts and programs by id, since edje doesn't sort
347 parts = @parts.to_a.map { |(key, value)| value }.sort
348 programs = @programs.to_a.map { |(key, value)| value }.sort
352 "programs" => [programs],
354 "prop.min.w" => [@min[0]],
355 "prop.min.h" => [@min[1]],
356 "prop.max.w" => [@max[0]],
357 "prop.max.h" => [@max[1]]}
361 EDJE = Redact::Edje.new