This repository has been archived by the owner on Jan 10, 2018. It is now read-only.
/
postprocessor.rb
173 lines (158 loc) · 4.42 KB
/
postprocessor.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/local/bin/macruby
require 'rubygems'
require 'hotcocoa/graphics'
require 'hotcocoa/graphics/gradient'
include HotCocoa
include Graphics
def run(f)
puts "Processing #{f}..."
gradient = /-postprocessor-linear-gradient\((\d+), (\d+), (\w+ \w+), (\w+ \w+), (\d|\d?\.\d+), (.+?), (\d|\d?\.\d+), (rgba?\(.+?\)|.+?)\)/
rule = /^(.+?) \{/
rgba_background_color = /background(?:-color)?: rgba\((\d+), (\d+), (\d+), (\d\.\d)\);/
rgba_background = /-postprocessor-rgba-bg\((rgba\(\d+, \d+, \d+, \d\.\d\))\)/
box_shadow_without_blur = /^\W*box-shadow: (\d+)(?:\w\w)? (\d+)(?:\w\w)?( 0 | 0px | )rgba\((\d+), (\d+), (\d+), (\d\.\d)\)/
last_rule, ie_style, png = "", {}, false
str = File.open(f).readlines
str.map! do |l|
if (a = l.match gradient)
l.gsub!(gradient, draw(a)) # pass in the matched variables from the regexp
png = true
elsif (a = l.match rule)
last_rule = a[1]
elsif (a = l.match rgba_background_color)
col = "#" + to_hex(a[4].to_f * 255) + to_hex(a[1]) + to_hex(a[2]) + to_hex(a[3])
add_filter(ie_style, last_rule, "Gradient(startColorstr=#{col},endColorstr=#{col})", :background => :transparent, :zoom => 1)
elsif (a = l.match rgba_background)
l.gsub! rgba_background, "url(data:image/png;base64,#{data_url(a)})"
elsif (a = l.match box_shadow_without_blur)
col = "#" + to_hex(a[3]) + to_hex(a[4]) + to_hex(a[5])
add_filter(ie_style, last_rule, "dropShadow(color=#{col}, offX=#{a[1]}, offY=#{a[2]}, positive=true)")
end
l
end
File.open(f, 'w').write(str.join)
if ie_style.length > 0
puts "Internet explorer stylesheet:\n\n", "<!--[if IE]>\n<style type=\"text/css\">\n"
ie_style.each do |sel, rules|
puts sel + " {"
rules.each do |k, v|
puts "\t#{k}: #{v};"
end
puts "}"
end
puts "</style>\n<![endif]-->"
end
puts "\nAlso don't forget that older IEs don't support alpha transparency. Use a script such as http://24ways.org/2007/supersleight-transparent-png-in-ie6 to support it." if png
end
def add_filter(s, sel, filter_str, h = {})
if s[sel]
if s[sel][:filter]
s[sel][:filter] << " progid:DXImageTransform.Microsoft.#{filter_str}"
else
s[sel][:filter] = "progid:DXImageTransform.Microsoft.#{filter_str}"
end
else
s[sel] = {:filter => "progid:DXImageTransform.Microsoft.#{filter_str}"}
end
s[sel]["-ms-filter"] = "\"#{s[sel][:filter]}\""
h.each do |k, v|
s[sel][k] = v
end
end
def data_url(args)
c = parse_color(args[1])
filename = "postprocessor-tmp-image.png"
canvas = Canvas.new :type => :image, :filename => filename, :size => [1,1]
canvas.background c
canvas.save
puts " Created data url for #{c.name} image with #{c.a} opacity."
[File.read(filename)].pack("m*").gsub /\n/, "" #base64 encoding
end
def to_hex(i)
s = i.to_i.to_s(16)
if s.length == 1
"0" + s
else
s
end
end
def draw(args)
puts args.inspect
w, h = args[1].to_i, args[2].to_i
s2a, s1a = args[3].split " "
e2a, e1a = args[4].split " "
s1 = pos(pos_y(s1a, h), h, args[5], s1a, e1a)
s2 = pos(pos_x(s2a, w), w, args[5], s2a, e2a)
e1 = pos(pos_y(s1a, h), h, args[7], s1a, e1a)
e2 = pos(pos_x(s2a, w), w, args[7], s2a, e2a)
#puts "sy: #{s1}, sx: #{s2}, ey: #{e1}, ex: #{e2}, cp1: #{cp1}, cp2: #{cp2}"
c1 = parse_color(args[6])
c2 = parse_color(args[8])
filename = "images/lgradient-#{w}x#{h}-#{c1.name}-#{c2.name}.png"
canvas = Canvas.new :type => :image, :filename => filename, :size => [w,h]
canvas.gradient(Gradient.new(c1, c2), s2, s1, e2, e1)
canvas.save
puts " Wrote #{filename}."
"url(../#{filename})"
end
def parse_color(c)
if a = c.match(/rgba\((\d+), ?(\d+), ?(\d+), ?(\d?\.\d*)\)?/)
#TODO add support for otheer color schemes then rgba
r = a[1].to_f / 255
g = a[2].to_f / 255
b = a[3].to_f / 255
a = a[4].to_f
Color.new r, g, b, a
end
end
# this takes the position of the start point and moved r percent down the line
def pos(v, dim, r, orig, endp)
v + dim * r.to_f * g(orig, endp)
end
# this function finds the aim for the linear gradient
def g(a,b)
case a
when /top|right/
case b
when /top|right/
0.0
when /bottom|left/
-1.0
when /center|middle/
-0.5
end
when /bottom|left/
case b
when /bottom|left/
0.0
when /top|right/
1.0
when /center|middle/
0.5
end
end
end
def pos_y(v, h)
case v
when 'top'
h
when 'middle'
(h/2).floor
when 'bottom'
0
end
end
def pos_x(v, w)
case v
when 'left'
0
when 'center'
(w/2).floor
when 'right'
w
end
end
ARGV.each do |f|
run f
end
File.unlink("postprocessor-tmp-image.png") rescue nil