Fixed exception message.
[redact.git] / lib / redact / redact.rb
1 #--
2 # $Id: redact.rb 31 2005-04-23 22:54:01Z tilman $
3 #
4 # Copyright (c) 2005 Tilman Sauerbeck (tilman at code-monkey de)
5 #
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:
13 #
14 # The above copyright notice and this permission notice shall be
15 # included in all copies or substantial portions of the Software.
16 #
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.
24
25 require "eet"
26 require "imlib2"
27 require "redact/part"
28 require "redact/program"
29
30 class Object # :nodoc:
31         undef :id
32 end
33
34 class String
35         def to_embryo(collection)
36                 unless strip.length > 0
37                         raise(Redact::RedactError, "invalid embryo code")
38                 end
39
40                 s = strip.dup
41
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 }
46                                 if found.nil?
47                                         raise(Redact::RedactError,
48                                               "#{entity} not found - #{$1}")
49                                 else
50                                         found.last.id
51                                 end
52                         end
53                 end
54
55                 s
56         end
57 end
58
59 module Redact
60         VERSION = "0.1.0"
61
62         class RedactError < StandardError; end
63
64         class Edje
65                 attr_reader :collections, :data, :collection_dir, :image_dir,
66                             :font_dir
67
68                 def initialize
69                         clear
70                 end
71
72                 def clear
73                         @font_dir = FontDirectory.new
74                         @image_dir = ImageDirectory.new
75                         @collection_dir = CollectionDirectory.new
76
77                         @collections = Hash.new do |h, k|
78                                 c = Collection.new(k, h.size)
79
80                                 @collection_dir << CollectionDirectoryEntry.new(c)
81                                 h[k] = c
82                         end
83
84                         @data = DataHash.new
85                 end
86
87                 def collection(name) # :yields: collection
88                         c = @collections[name]
89
90                         block_given? ? (yield c) : c
91                 end
92
93                 protected
94                 def to_eet_name
95                         "Edje_File"
96                 end
97
98                 def to_eet_properties
99                         {"compiler" => ["redact"],
100                          "version" => [2],
101                          "feature_ver" => [1],
102                          "font_dir" => [@font_dir, :sub],
103                          "image_dir" => [@image_dir, :sub],
104                          "collection_dir" => [@collection_dir, :sub],
105                          "data" => [@data]}
106                 end
107         end
108
109         class DataHash < Hash # :nodoc:
110                 def [](key)
111                         super.value
112                 end
113
114                 def []=(key, value)
115                         super(key, HashEntry.new(key, value))
116                 end
117         end
118
119         class HashEntry # :nodoc:
120                 attr_reader :key, :value
121
122                 def initialize(key, value)
123                         @key = key.to_str.dup.freeze
124                         @value = value.to_str.dup.freeze
125                 end
126
127                 protected
128                 def to_eet_name
129                         "Edje_Data"
130                 end
131         end
132
133         class FontDirectory < Array # :nodoc:
134                 protected
135                 def to_eet_name
136                         "Edje_Font_Directory"
137                 end
138
139                 def to_eet_properties
140                         {"entries" => [self]}
141                 end
142         end
143
144         class FontDirectoryEntry # :nodoc:
145                 attr_reader :filename
146
147                 def initialize(filename)
148                         @filename = filename.to_str.dup.freeze
149                 end
150
151                 protected
152                 def to_eet_name
153                         "Edje_Font_Directory_Entry"
154                 end
155
156                 def to_eet_properties
157                         {"entry" => [@filename]}
158                 end
159         end
160
161         class ImageDirectory < Array # :nodoc:
162                 def <<(entry)
163                         super
164
165                         entry.id = size - 1
166                 end
167
168                 protected
169                 def to_eet_name
170                         "Edje_Image_Directory"
171                 end
172
173                 def to_eet_properties
174                         {"entries" => [self]}
175                 end
176         end
177
178         class ImageDirectoryEntry # :nodoc:
179                 attr_reader :filename, :image, :id
180
181                 def initialize(filename)
182                         @filename = filename.to_str.dup.freeze
183                         @image = Imlib2::Image.load(@filename)
184                         @id = -1
185                         @source_type = 1 # COMP
186                         @source_param = 1 # COMP
187                 end
188
189                 def id=(id)
190                         if id < 0
191                                 raise(ArgumentError, "invalid ID - #{id}")
192                         elsif @id != -1
193                                 raise(RedactError, "ID already set")
194                         else
195                                 @id = id
196                         end
197                 end
198
199                 protected
200                 def to_eet_name
201                         "Edje_Image_Directory_Entry"
202                 end
203
204                 def to_eet_properties
205                         {"entry" => [@filename],
206                          "source_type" => [@source_type],
207                          "source_param" => [@source_param],
208                          "id" => [@id]}
209                 end
210         end
211
212         class CollectionDirectory < Array # :nodoc:
213                 protected
214                 def to_eet_name
215                         "Edje_Part_Collection_Directory"
216                 end
217
218                 def to_eet_properties
219                         {"entries" => [self]}
220                 end
221         end
222
223         class CollectionDirectoryEntry # :nodoc:
224                 def initialize(col)
225                         @name = col.name.to_str.dup.freeze
226                         @id = col.id
227                 end
228
229                 protected
230                 def to_eet_name
231                         "Edje_Part_Collection_Directory_Entry"
232                 end
233
234                 def to_eet_properties
235                         {"entry" => [@name],
236                          "id" => [@id]}
237                 end
238         end
239
240         class Collection
241                 attr_reader :name, :id, :data, :min, :max, :parts, :programs,
242                             :script
243
244                 def initialize(name, id)
245                         @name = name.to_str.dup.freeze
246                         @id = id
247
248                         @min = [0, 0]
249                         @max = [0, 0]
250                         @data = DataHash.new
251
252                         @parts = {}
253                         @programs = {}
254
255                         @script = nil
256                 end
257
258                 def script=(v)
259                         @script = v.to_str.dup
260                 end
261
262                 def part(name, type = :invalid) # :yields: part
263                         p = @parts[name]
264                         if p.nil?
265                                 klass = case type
266                                 when :rect
267                                         Part
268                                 when :swallow
269                                         SwallowPart
270                                 when :text
271                                         TextPart
272                                 when :image
273                                         ImagePart
274                                 else
275                                         raise(ArgumentError,
276                                               "invalid part type - #{type.to_s}")
277                                 end
278
279                                 p = klass.new(self, @parts.size, name)
280                                 @parts[name] = p
281                         end
282
283                         block_given? ? (yield p) : p
284                 end
285
286                 def program(name, type = :invalid) # :yields: program
287                         p = @programs[name]
288                         if p.nil?
289                                 klass = case type
290                                 when :base
291                                         Program
292                                 when :set_state
293                                         SetStateProgram
294                                 when :stop_program
295                                         StopProgramProgram
296                                 when :emit_signal
297                                         EmitSignalProgram
298                                 when :exec_script
299                                         ExecScriptProgram
300                                 else
301                                         raise(ArgumentError,
302                                               "invalid program type - #{type.to_s}")
303                                 end
304
305                                 p = klass.new(self, @programs.size, name)
306                                 @programs[name] = p
307                         end
308
309                         block_given? ? (yield p) : p
310                 end
311
312                 def set_size(w, h)
313                         set_min(w, h)
314                         set_max(w, h)
315                 end
316
317                 def set_min(w, h)
318                         @min = [w, h]
319                 end
320
321                 def set_max(w, h)
322                         @max = [w, h]
323                 end
324
325                 def has_embryo?(search_programs = true)
326                         if !@script.nil? && @script.to_str.strip.length > 0
327                                 true
328                         elsif search_programs
329                                 p = @programs.find do |(k, p)|
330                                         p.is_a?(ExecScriptProgram)
331                                 end
332
333                                 !p.nil?
334                         end
335                 end
336
337                 protected
338                 def to_eet_name
339                         "Edje_Part_Collection"
340                 end
341
342                 def to_eet_properties
343                         # sort parts and programs by id, since edje doesn't sort
344                         # them on load
345                         parts = @parts.to_a.map { |(key, value)| value }.sort
346                         programs = @programs.to_a.map { |(key, value)| value }.sort
347
348                         {"id" => [@id],
349                          "parts" => [parts],
350                          "programs" => [programs],
351                          "data" => [@data],
352                          "prop.min.w" => [@min[0]],
353                          "prop.min.h" => [@min[1]],
354                          "prop.max.w" => [@max[0]],
355                          "prop.max.h" => [@max[1]]}
356                 end
357         end
358
359         EDJE = Redact::Edje.new
360 end