#!/bin/bash
#130317 rodin.s: internationalized 
v=191012 #sfs: arch compat.
if [ -f /etc/fonts/conf.d/50-user.conf ] ;then
    mkdir -p $HOME/.config/fontconfig && f="$HOME/.config/fontconfig/fonts.conf" 
else
    f="$HOME/.fonts.conf"
fi

export TEXTDOMAIN=fontwizard
export TEXTDOMAINDIR=/usr/share/locale
export OUTPUT_CHARSET=UTF-8
. gettext.sh

export antialiasing=$(grep '<bool>' $f|head -1|cut -d '>' -f2|cut -d '<' -f1)
export hinting=$(grep '<bool>' $f|head -2|tail -1|cut -d '>' -f2|cut -d '<' -f1)
export autohint=$(grep '<bool>' $f|head -3|tail -1|cut -d '>' -f2|cut -d '<' -f1)

[ "$antialiasing" = "" ] && antialiasing=false hinting=false autohint=false
                        
save_settings(){
case $hinting in
  true)
  
    hintstyle=hintslight
    ;;
  false)
   
    hintstyle=hintnone
    ;;
    
esac

  echo "<match target=\"font\">
 <edit name=\"antialias\" mode=\"assign\">
<bool>$antialiasing</bool>
</edit>
<edit name=\"hinting\" mode=\"assign\">
<bool>$hinting</bool>
</edit>
 <edit name=\"autohint\" mode=\"assign\">
<bool>$autohint</bool>
</edit>
<edit name=\"hintstyle\" mode=\"assign\">
<const>$hintstyle</const>
</edit>
</match>" > $f

`which sudo` rm /etc/fonts/local.conf
[ -f /etc/fonts/conf.d/50-user.conf ] || ln -s $HOME/.fonts.conf /etc/fonts/local.conf

TMP1="`cat $HOME/.Xresources | grep -v "Xft\\.dpi:"`"
echo "$TMP1" > $HOME/.Xresources
echo "Xft.dpi: $dpi" >> $HOME/.Xresources
sync
xrdb -merge $HOME/.Xresources
 
ntf -i "$(gettext 'Font appearance settings saved. Only newly started application will be affected.')" &
$0 &
exit
}

export -f save_settings

#read current dpi setting...
CURRDPI=`grep "^Xft\\.dpi:" $HOME/.Xresources | tr '\t' ' ' | tr -s ' ' | cut -f 2 -d ' '`
[ ! $CURRDPI ] && CURRDPI=96

export mainWindow='
<window title="'$(gettext 'Font Appearance')' v.'$v'" allow-grow="false"  icon-name="gtk-font"> 
  <vbox>
	<frame Вид>
##    <text><label>'$(gettext 'Welcome to the Puppy font appearance wizard!')'</label></text> 
<hbox>
    <text width-request="450" xalign="0"><label>'$(gettext 'This wizard allows you to change the way fonts appear on your screen: make them either sharper or smoother.')'</label></text>        
</hbox>       
<hbox>
<pixmap><height>48</height><width>48</width><input file icon="font"></input></pixmap>
    <text space-expand="1" xalign="0"><label>'$(gettext 'For LCD it is recommend to turn everything on. For CRT screens, do the opposite.')'</label></text>        
<vbox> 
   <checkbox tooltip-text="'$(gettext 'If checked, fonts will be smoothed with antialiasing.')'">
     <label>'$(gettext 'Antialiasing')'</label>
     <variable>antialiasing</variable>
     <default>'$antialiasing'</default>
   </checkbox>
       
   <checkbox tooltip-text=" '$(gettext 'If checked, fonts will be smoothed with hinting.')'">
      <label>'$(gettext 'Hinting')'</label>
      <variable>hinting</variable>
      <default>'$hinting'</default>
   </checkbox>
       
       <checkbox>
         <label>'$(gettext 'Automatic hinting')'</label>
         <variable>autohint</variable>
         <default>'$autohint'</default>
       </checkbox>
</vbox>
</hbox>      
	</frame>

		#<hbox width-request="6" space-fill="true" space-expand="false">
	<frame DPI>
		#<hbox spacing="200" space-expand="true">
		#<hbox  homogeneous="true">
		<hbox>
<pixmap><height>48</height><width>48</width><input file icon="font_size"></input></pixmap>
			<text space-expand="1">
				<label>Выберите DPI (точек на дюйм) экрана. Чем больше DPI, тем крупнее шрифты. Некоторые приложения не используют сглаживающий рендеринг и не будут затронуты этой настройкой.</label>
			</text>
		    <vbox>
			<comboboxtext>
			#<comboboxtext has-focus="true">
				<variable>dpi</variable>
				<default>'$CURRDPI'</default>
				<item>54</item>
				<item>60</item>
				<item>66</item>
				<item>72</item>
				<item>78</item>
				<item>84</item>
				<item>90</item>
				<item>96</item>
				<item>102</item>
				<item>108</item>
				<item>114</item>
				<item>120</item>
				<item>126</item>
				<item>132</item>
				<item>138</item>
				<item>144</item>
				<item>150</item>
			</comboboxtext>
		    </vbox>
		</hbox>
	</frame>

   <hbox>
     
#     <button use-stock="true" label="Размер шрифта">
#       <action>set-xftdpi</action>
#     </button> 
     <button use-stock="true" label="gtk-help">
       <action>defaultbrowser https://wiki.archlinux.org/index.php/Font_configuration &</action>
       #<action>defaultbrowser https://ya.ru &</action>
     </button> 

     <button use-stock="true" label="gtk-apply">
       <action type="exit">save</action>
     </button> 

	 <button use-stock="true" label="gtk-quit">
       <action type="exit">EXIT_NOW</action>
     </button> 
    
   </hbox>                    
  </vbox>
      
</window>'
export mainWindow="`echo "$mainWindow" | sed -e 's/#.*//'`"
eval $(gtkdialog -p mainWindow --center)
#ntf -w " $dpi"
[ "$EXIT" = "save" ] && save_settings
