reformat ruby render script

This commit is contained in:
Daniel Gultsch 2022-12-26 15:21:18 +01:00
parent eb51b03d1a
commit a157d6796a
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2

View file

@ -1,4 +1,5 @@
#!/bin/env ruby
# frozen_string_literal: true
require 'xml'
@ -7,7 +8,7 @@ resolutions = {
'hdpi' => 1.5,
'xhdpi' => 2,
'xxhdpi' => 3,
'xxxhdpi' => 4,
'xxxhdpi' => 4
}
images = {
@ -28,7 +29,6 @@ images = {
'conversations_mono.svg' => ['conversations/ic_notification', 24],
'quicksy_mono.svg' => ['quicksy/ic_notification', 24],
'flip_camera_android-black-24dp.svg' => ['ic_flip_camera_android_black_24dp', 24],
'ic_missed_call_notification.svg' => ['ic_missed_call_notification', 24],
'ic_send_text_offline.svg' => ['ic_send_text_offline', 36],
'ic_send_text_offline_white.svg' => ['ic_send_text_offline_white', 36],
'ic_send_text_online.svg' => ['ic_send_text_online', 36],
@ -82,11 +82,8 @@ images = {
'marker.svg' => ['marker', 0]
}
# Executable paths for Mac OSX
# "/Applications/Inkscape.app/Contents/Resources/bin/inkscape"
inkscape = "inkscape"
imagemagick = "magick"
inkscape = 'inkscape'
imagemagick = 'convert'
def execute_cmd(cmd)
puts cmd
@ -95,17 +92,16 @@ end
images.each do |source_filename, settings|
svg_content = File.read(source_filename)
svg = XML::Document.string(svg_content)
base_width = svg.root["width"].to_i
base_height = svg.root["height"].to_i
guides = svg.find(".//sodipodi:guide","sodipodi:http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd")
resolutions.each do |resolution, factor|
output_filename, base_size = settings
if base_size > 0
svg = XML::Document.string(svg_content)
base_width = svg.root['width'].to_i
base_height = svg.root['height'].to_i
guides = svg.find('.//sodipodi:guide', 'sodipodi:http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd')
resolutions.each do |resolution, factor|
if base_size.positive?
width = factor * base_size
height = factor * base_size
else
@ -115,12 +111,12 @@ images.each do |source_filename, settings|
output_parts = output_filename.split('/')
if output_parts.count != 2
path = "../src/main/res/drawable-#{resolution}/#{output_filename}.png"
path = if output_parts.count != 2
"../src/main/res/drawable-#{resolution}/#{output_filename}.png"
else
path = "../src/#{output_parts[0]}/res/drawable-#{resolution}/#{output_parts[1]}.png"
"../src/#{output_parts[0]}/res/drawable-#{resolution}/#{output_parts[1]}.png"
end
execute_cmd "#{inkscape} #{source_filename} -C -w #{width} -h #{height} -e #{path}"
execute_cmd "#{inkscape} #{source_filename} -C -w #{width.to_i} -h #{height.to_i} --export-filename=#{path}"
top = []
right = []
@ -128,25 +124,18 @@ images.each do |source_filename, settings|
left = []
guides.each do |guide|
orientation = guide["orientation"]
x, y = guide["position"].split(",")
x, y = x.to_i, y.to_i
orientation = guide['orientation']
x, y = guide['position'].split(',')
x = x.to_i
y = y.to_i
if orientation == "1,0" and y == base_height
top.push(x * factor)
end
top.push(x * factor) if (orientation == '1,0') && (y == base_height)
if orientation == "0,1" and x == base_width
right.push((base_height - y) * factor)
end
right.push((base_height - y) * factor) if (orientation == '0,1') && (x == base_width)
if orientation == "1,0" and y == 0
bottom.push(x * factor)
end
bottom.push(x * factor) if (orientation == '1,0') && y.zero?
if orientation == "0,1" and x == 0
left.push((base_height - y) * factor)
end
left.push((base_height - y) * factor) if (orientation == '0,1') && x.zero?
end
next if top.length != 2
@ -156,11 +145,11 @@ images.each do |source_filename, settings|
execute_cmd "#{imagemagick} -background none PNG32:#{path} -gravity center -extent #{width + 2}x#{height + 2} PNG32:#{path}"
draw_format = "-draw \"line %d,%d %d,%d\""
top_line = draw_format % [top.min + 1, 0, top.max, 0]
right_line = draw_format % [width + 1, right.min + 1, width + 1, right.max]
bottom_line = draw_format % [bottom.min + 1, height + 1, bottom.max, height + 1]
left_line = draw_format % [0, left.min + 1, 0, left.max]
draw_format = '-draw "line %d,%d %d,%d"'
top_line = format(draw_format, top.min + 1, 0, top.max, 0)
right_line = format(draw_format, width + 1, right.min + 1, width + 1, right.max)
bottom_line = format(draw_format, bottom.min + 1, height + 1, bottom.max, height + 1)
left_line = format(draw_format, 0, left.min + 1, 0, left.max)
draws = "#{top_line} #{right_line} #{bottom_line} #{left_line}"
execute_cmd "#{imagemagick} -background none PNG32:#{path} -fill black -stroke none #{draws} PNG32:#{path}"