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