Added support for the --image_dir and --font_dir command line switches.
[redact.git] / lib / redact / redact.rb
1 #--
2 # $Id: redact.rb 44 2005-06-08 17:35:49Z 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.1"
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 = find_font(filename.to_str).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
160                 private
161                 def find_font(file)
162                         [".", OPTIONS.font_dir].each do |d|
163                                 f2 = File.join(d, file)
164                                 return f2 if File.file?(f2)
165                         end
166
167                         nil
168                 end
169         end
170
171         class ImageDirectory < Array # :nodoc:
172                 def <<(entry)
173                         super
174
175                         entry.id = size - 1
176                 end
177
178                 protected
179                 def to_eet_name
180                         "Edje_Image_Directory"
181                 end
182
183                 def to_eet_properties
184                         {"entries" => [self]}
185                 end
186         end
187
188         class ImageDirectoryEntry # :nodoc:
189                 attr_reader :filename, :image, :id
190
191                 def initialize(filename)
192                         @filename = find_image(filename.to_str).freeze
193                         @image = Imlib2::Image.load(@filename)
194                         @id = -1
195                         @source_type = 1 # COMP
196                         @source_param = 1 # COMP
197                 end
198
199                 def id=(id)
200                         if id < 0
201                                 raise(ArgumentError, "invalid ID - #{id}")
202                         elsif @id != -1
203                                 raise(RedactError, "ID already set")
204                         else
205                                 @id = id
206                         end
207                 end
208
209                 protected
210                 def to_eet_name
211                         "Edje_Image_Directory_Entry"
212                 end
213
214                 def to_eet_properties
215                         {"entry" => [@filename],
216                          "source_type" => [@source_type],
217                          "source_param" => [@source_param],
218                          "id" => [@id]}
219                 end
220
221                 private
222                 def find_image(file)
223                         [".", OPTIONS.image_dir].each do |d|
224                                 f2 = File.join(d, file)
225                                 return f2 if File.file?(f2)
226                         end
227
228                         nil
229                 end
230         end
231
232         class CollectionDirectory < Array # :nodoc:
233                 protected
234                 def to_eet_name
235                         "Edje_Part_Collection_Directory"
236                 end
237
238                 def to_eet_properties
239                         {"entries" => [self]}
240                 end
241         end
242
243         class CollectionDirectoryEntry # :nodoc:
244                 def initialize(col)
245                         @name = col.name.to_str.dup.freeze
246                         @id = col.id
247                 end
248
249                 protected
250                 def to_eet_name
251                         "Edje_Part_Collection_Directory_Entry"
252                 end
253
254                 def to_eet_properties
255                         {"entry" => [@name],
256                          "id" => [@id]}
257                 end
258         end
259
260         class Collection
261                 attr_reader :name, :id, :data, :min, :max, :parts, :programs,
262                             :script
263
264                 def initialize(name, id)
265                         @name = name.to_str.dup.freeze
266                         @id = id
267
268                         @min = [0, 0]
269                         @max = [0, 0]
270                         @data = DataHash.new
271
272                         @parts = {}
273                         @programs = {}
274
275                         @script = nil
276                 end
277
278                 def script=(v)
279                         @script = v.to_str.dup
280                 end
281
282                 def part(name, type = :invalid) # :yields: part
283                         p = @parts[name]
284                         if p.nil?
285                                 klass = case type
286                                 when :rect
287                                         Part
288                                 when :swallow
289                                         SwallowPart
290                                 when :text
291                                         TextPart
292                                 when :image
293                                         ImagePart
294                                 else
295                                         raise(ArgumentError,
296                                               "invalid part type - #{type.to_s}")
297                                 end
298
299                                 p = klass.new(self, @parts.size, name)
300                                 @parts[name] = p
301                         end
302
303                         block_given? ? (yield p) : p
304                 end
305
306                 def program(name, type = :invalid) # :yields: program
307                         p = @programs[name]
308                         if p.nil?
309                                 klass = case type
310                                 when :base
311                                         Program
312                                 when :set_state
313                                         SetStateProgram
314                                 when :stop_program
315                                         StopProgramProgram
316                                 when :emit_signal
317                                         EmitSignalProgram
318                                 when :exec_script
319                                         ExecScriptProgram
320                                 else
321                                         raise(ArgumentError,
322                                               "invalid program type - #{type.to_s}")
323                                 end
324
325                                 p = klass.new(self, @programs.size, name)
326                                 @programs[name] = p
327                         end
328
329                         block_given? ? (yield p) : p
330                 end
331
332                 def set_size(w, h)
333                         set_min(w, h)
334                         set_max(w, h)
335                 end
336
337                 def set_min(w, h)
338                         @min = [w, h]
339                 end
340
341                 def set_max(w, h)
342                         @max = [w, h]
343                 end
344
345                 def has_embryo?(search_programs = true)
346                         if !@script.nil? && @script.to_str.strip.length > 0
347                                 true
348                         elsif search_programs
349                                 p = @programs.find do |(k, p)|
350                                         p.is_a?(ExecScriptProgram)
351                                 end
352
353                                 !p.nil?
354                         end
355                 end
356
357                 protected
358                 def to_eet_name
359                         "Edje_Part_Collection"
360                 end
361
362                 def to_eet_properties
363                         # sort parts and programs by id, since edje doesn't sort
364                         # them on load
365                         parts = @parts.to_a.map { |(key, value)| value }.sort
366                         programs = @programs.to_a.map { |(key, value)| value }.sort
367
368                         {"id" => [@id],
369                          "parts" => [parts],
370                          "programs" => [programs],
371                          "data" => [@data],
372                          "prop.min.w" => [@min[0]],
373                          "prop.min.h" => [@min[1]],
374                          "prop.max.w" => [@max[0]],
375                          "prop.max.h" => [@max[1]]}
376                 end
377         end
378
379         EDJE = Redact::Edje.new
380 end