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
|
# File 'lib/compass-svg-polyfill/sass_functions.rb', line 7
def svg_polyfill(width, height, svgName, pngName, imageConverter)
assert_type width, :Number
assert_type height, :Number
assert_type svgName, :String
assert_type pngName, :String
assert_type imageConverter, :String
svgFilepath = File.join Compass.configuration.images_path, svgName.value.to_s
pngFilepath = File.join Compass.configuration.images_path, pngName.value.to_s
imageConverter = imageConverter.value.to_sym
logger = Compass::Logger.new
cache_store = environment.options[:cache_store]
if !File.exists? svgFilepath
raise Sass::SyntaxError, "SVG file does not exist #{svgName.value.to_s}"
end
tmpFile = Tempfile.new(['compass-svg-polyfill', '.png'])
tmpFile.close false
case imageConverter
when :imagemagick
img = Magick::Image.read(svgFilepath).first
img.resize!(width.value.to_i, height.value.to_i)
img.write tmpFile.path
when :librsvg
system(
"rsvg-convert", "-w", width.value, "-h", height.value, svgFilepath, "-o", tmpFile.path )
else
raise Sass::SyntaxError, "Unknown image converter #{imageConverter}"
end
cacheKey = cache_store.key(File.dirname(pngFilepath), File.basename(pngFilepath))
cacheSHA = Digest::SHA1.hexdigest(pngFilepath)
cacheVal = "#{width} #{height} #{Digest::SHA1.file(svgFilepath).hexdigest}"
action = nil
if File.exists? pngFilepath
action = :overwrite
if (cachedSHA = cache_store.retrieve(cacheKey, cacheSHA)) then
if cachedSHA.eql? cacheVal then
action = :unchanged
end
end
else
action = :create
end
logger.record action, pngName.value
case action
when :overwrite, :create
FileUtils.mv(tmpFile.path, pngFilepath)
end
cache_store.store(cacheKey, cacheSHA, cacheVal)
tmpFile.unlink
Sass::Script::Bool.new true
end
|