]> git.lyx.org Git - lyx.git/blob - development/MacOSX/svg2icns.sh
Translation of new index features step 2
[lyx.git] / development / MacOSX / svg2icns.sh
1 #! /bin/bash
2 ###
3 ### svg2icns.sh
4 ###   Create ICNS file from SVG
5 ###   https://gist.github.com/plroebuck/af19a26c908838c7f9e363c571199deb
6 ###
7 ### See also:
8 ###   <https://stackoverflow.com/questions/12306223/how-to-manually-create-icns-files-using-iconutil#39678276>
9 ###   <https://developer.apple.com/library/content/documentation/GraphicsAnimation/Conceptual/HighResolutionOSX/Optimizing/Optimizing.html>
10 ###
11
12 script="$(basename $0 .sh)"
13
14 PATH=/Applications/Inkscape.app/Contents/MacOS:/Applications/Inkscape.app/Contents/Resources/bin:"$PATH"
15
16 CONVERT_BIN=$(which inkscape)
17 : ${CONVERT_BIN?Requires Inkscape to be installed}
18
19 ## Usage function
20 usage() {
21     cat <<END_USAGE >&2
22 Usage: ${script} <svgfilename> [<icnsfilename>]
23   svgfilename:  input file in SVG format
24   icnsfilename: output file in ICNS format (optional)
25
26 END_USAGE
27 }
28  
29 ## Call usage() function if args not supplied
30 argc="$#"
31 if [[ "$argc" -lt 1 ]] || [[ "$argc" -gt 2 ]];
32 then
33     usage
34     exit 1
35 fi
36
37 ## Verify first positional argument
38 svgfilename="$1"
39 if [ ! -f "${svgfilename}" ];
40 then
41     usage
42     echo "${script}: no such file: ${svgfilename}" >&2
43     exit 1
44 else
45     ## Verify scalable vector graphics file
46     $(file "${svgfilename}" | cut -d':' -f2- | grep -qs 'SVG')
47     if [ "$?" -ne 0 ];
48     then
49         usage
50         echo "${script}: SVG file required: ${svgfilename}" >&2
51         exit 1
52     fi
53 fi
54
55 ## Verify second positional argument if given
56 if [ "$argc" -eq 2 ];
57 then
58     icnsfilename="$2"
59     if [ -n "${icnsfilename}" ];
60     then
61         ## Ensure file extension
62         ext=${icnsfilename##*.}
63         if [ "$ext" != "icns" ];
64         then
65             icnsfilename="${icnsfilename}.icns"
66         fi
67     else
68         ## Given empty string as second arg
69         icnsfilename="${svgfilename%.*}.icns"
70     fi
71 else
72     ## No second arg
73     icnsfilename="${svgfilename%.*}.icns"
74 fi
75
76 ## Create iconset directory if necessary
77 iconsetdirname=/tmp/$(basename "${icnsfilename}" .icns)-$$.iconset
78 mkdir -p "${iconsetdirname}"
79
80 ##+---------------------+--------------------+--------------+
81 ##|      filename       | resolution, pixels | density, PPI |
82 ##+---------------------+--------------------+--------------+
83 ##| icon_16x16.png      | 16x16              |           72 |
84 ##| icon_16x16@2x.png   | 32x32              |          144 |
85 ##| icon_32x32.png      | 32x32              |           72 |
86 ##| icon_32x32@2x.png   | 64x64              |          144 |
87 ##| icon_128x128.png    | 128x128            |           72 |
88 ##| icon_128x128@2x.png | 256x256            |          144 |
89 ##| icon_256x256.png    | 256x256            |           72 |
90 ##| icon_256x256@2x.png | 512x512            |          144 |
91 ##| icon_512x512.png    | 512x512            |           72 |
92 ##| icon_512x512@2x.png | 1024x1024          |          144 |
93 ##+---------------------+--------------------+--------------+
94
95 ## Create PNG files as described in table
96 sizes=( 16 32 128 256 512 )
97 densities=( 72 144 )
98
99 for size in "${sizes[@]}"
100 do
101     dimen="${size}x${size}"
102     for density in "${densities[@]}"
103     do
104         if [ "${density}" == "72" ];
105         then
106             ## std
107             resolution="${dimen}"
108             scale=""
109         else
110             ## hires
111             resolution="$(( $size * 2 ))x$(( $size * 2 ))"
112             scale="@2x"
113         fi
114         pngfilename="${iconsetdirname}/icon_${dimen}${scale}.png"
115                   inkscape --export-type=png -w $size -h $size -o "${pngfilename}" "${svgfilename}"
116         if [ "$?" -ne 0 ];
117         then
118             echo "${script}: error creating icon file: ${pngfilename}" >&2
119             exit 1
120         fi
121     done
122 done
123
124 ## Convert iconset to ICNS file
125 iconutil --convert icns --output "${icnsfilename}" "${iconsetdirname}"
126 if [ "$?" -ne 0 ];
127 then
128     echo "${script}: error converting iconset to ICNS: ${iconsetdirname}" >&2
129     exit 1
130 else
131     rm -rf "${iconsetdirname}"
132 fi
133
134 exit 0