#!/bin/bash
#Combining fontwizard, pFont and set-xftdpi (Barry Kauler 2009)
#Sigmund Berglund, Nov 2013

export TEXTDOMAIN=fontmanager
export OUTPUT_CHARSET=UTF-8

#read current dpi setting...
CURRDPI=`grep "^Xft\\.dpi:" /root/.Xresources | tr '\t' ' ' | tr -s ' ' | cut -f 2 -d ' '`
[ ! $CURRDPI ] && CURRDPI=78
CURRINDEX=$(( $CURRDPI / 6 - 9 ))

#read appearance values
if [ -f $HOME/.fonts.conf ];then
 export antialiasing=$(grep '<bool>' $HOME/.fonts.conf|head -1|cut -d '>' -f2|cut -d '<' -f1)
 export hinting=$(grep '<bool>' $HOME/.fonts.conf|head -2|tail -1|cut -d '>' -f2|cut -d '<' -f1)
 export autohint=$(grep '<bool>' $HOME/.fonts.conf|head -3|tail -1|cut -d '>' -f2|cut -d '<' -f1)
 export hintstyle=$(grep '<int>' $HOME/.fonts.conf|cut -d '>' -f2|cut -d '<' -f1)
else 
 export antialiasing=false hinting=false autohint=false hintstyle=0
fi

#what tab should be active
[ "$1" = "install" ] && PAGE_NR=2 || PAGE_NR=0

install (){
	case $FONT in
	*.ttf)
		cp "$FONT" /usr/share/fonts/default/TTF/
		rm /usr/share/fonts/default/TTF/fonts.dir
		rm /usr/share/fonts/default/TTF/fonts.scale
		mkfontscale /usr/share/fonts/default/TTF
		mkfontdir /usr/share/fonts/default/TTF
		;;
	*)
		Xdialog --title "Font installer" --msgbox "'$(gettext 'No TTF-file to install')" 0 0
		;;
	esac
}
export -f install

S='
<window title="'$(gettext 'Font Manager')'" icon-name="gtk-execute"> 
<vbox space-expand="true" space-fill="true">
  <notebook tab-pos="2" page="'$PAGE_NR'" labels="'$(gettext 'Size')'|'$(gettext 'Appearance')'|'$(gettext 'Install fonts')'">


    <frame '$(gettext 'Global font size')'>
      '"`/usr/lib/gtkdialog/xml_info scale font_size.svg 60 "$(gettext 'Please choose dpi (dots per inch) for the screen. The larger the value, the bigger that antialiased fonts will render on the screen. Adjust up or down if fonts are rendering too small or too big on screen.')" " " "<b>$(gettext 'Note!')</b> $(gettext 'Some apps do not use antialiased rendering and will not be affected by this setting.')" "<b>$(gettext 'Note!')</b> $(gettext 'Restart X for changes to take effect')"`"'        
      <vbox space-expand="false" space-fill="false">
        <text height-request="20"><label>""</label></text>
        <hbox>
          <text><label>""</label></text>
          <text><label>'$(gettext 'Choose global fontsize')'</label></text>
          <comboboxtext width-request="70">
            <default>'$CURRDPI'</default>
            <variable>NEWDPI</variable>
            <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>
        </hbox>
        <text height-request="5"><label>""</label></text>
      </vbox>
    </frame>


    <frame '$(gettext 'Font appearance')'>
      '"`/usr/lib/gtkdialog/xml_info scale font.svg 60 "$(gettext "You can change the way fonts appear on your screen: make them either sharper or smoother.")" "$(gettext "For LCD it is recommend to turn everything on. For CRT screens, do the opposite.")" " " "<b>$(gettext 'Note!')</b> $(gettext 'Restart application for changes to take affect.')"`"'        
      <vbox space-expand="false" space-fill="false">
        <text height-request="20"><label>""</label></text>
        <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>
        <hbox space-expand="true" space-fill="true">
          <checkbox>
            <label>'$(gettext 'Automatic hinting')'</label>
            <variable>autohint</variable>
            <default>'$autohint'</default>
          </checkbox>
		  <text space-expand="true" space-fill="true"><label>""</label></text>
		  <text space-expand="false" space-fill="false"><label>'$(gettext 'Hint style')'</label></text>
          <spinbutton range-min="0" range-max="3" width="100" editable="false" space-expand="false" space-fill="false">
		    <variable>hintstyle</variable>
		    <default>'$hintstyle'</default>
		    <sensitive>true</sensitive>
		  </spinbutton> 
		</hbox>
        <text height-request="5"><label>""</label></text>
        </vbox>
      </frame>


      <frame '$(gettext 'Install font')'>
        '"`/usr/lib/gtkdialog/xml_info scale font_add.svg 60 "$(gettext 'Define a *.TTF file by browsing, or just drag it to the field below. Then click install. Windows TTF-files are fine.')" "$(gettext 'To install several fonts at once, copy new *.TTF files to the font directory') (<i>/usr/share/fonts/default/TTF</i>), $(gettext 'and click install.')"  " " "$(gettext 'You find a lot of free fonts on the web. - ie. at')" "<i>http://www.1001freefonts.com/</i>"`"'        
        <vbox space-expand="false" space-fill="false">
          <text height-request="20"><label>""</label></text>
          <hbox>
            <entry accept="filename"><variable>FONT</variable></entry>
            <button>
              '"`/usr/lib/gtkdialog/xml_button-icon open`"'
              <action type="fileselect">FONT</action>
              <action>refresh:FONT</action>
            </button>
          </hbox>
          <hbox>'
            type rox > /dev/null 2>&1 && S=$S'<button space-expand="false" space-fill="false">
              '"`/usr/lib/gtkdialog/xml_button-icon open`"'
              <label>'$(gettext 'Open font directory')'</label>
              <action>rox -d /usr/share/fonts/default/TTF</action>
            </button>'
            S=$S'<button>
	          '"`/usr/lib/gtkdialog/xml_button-icon font_add.svg`"'
	          <label>'$(gettext 'Install font(s)')'</label>
	          <action>install</action>
	          <action>clear:FONT</action>
            </button>
          </hbox>
          <text height-request="5"><label>""</label></text>
        </vbox>
      </frame>


    </notebook>
    <hbox space-expand="false" space-fill="false">'
      type gfontsel > /dev/null 2>&1 && S=$S' <button space-expand="false" space-fill="false">
        '"`/usr/lib/gtkdialog/xml_button-icon select-font`"'
        <label>'$(gettext 'View installed fonts')'</label>
        <action>gfontsel &</action>
      </button>'
      S=$S'<text space-expand="true" space-fill="true"><label>""</label></text>
    <button space-expand="false" space-fill="false">
      <label>'$(gettext "Cancel")'</label>
      '"`/usr/lib/gtkdialog/xml_button-icon cancel`"'
      <action type="exit">EXIT_NOW</action>
    </button>
    <button space-expand="false" space-fill="false">
      <label>'$(gettext "Ok")'</label>
      '"`/usr/lib/gtkdialog/xml_button-icon ok`"'
      <action type="exit">save</action>
    </button>
  </hbox>
  </vbox>
</window>'
export Font_Manager="$S"

. /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
eval $(gtkdialog -p Font_Manager)

case $EXIT in
	save)
		#set size
		TMP1="`cat /root/.Xresources | grep -v "Xft\\.dpi:"`"
		echo "$TMP1" > /root/.Xresources
		echo "Xft.dpi: $NEWDPI" >> /root/.Xresources
		sync
		
		if [[ "$antialiasing" = "false" && "$hinting" = "false" && "$autohint" = "false" && "$hintstyle" = 0 ]];then
		 #don't write the file, delete if it's there.
		 [ -f $HOME/.fonts.conf ] && rm $HOME/.fonts.conf 
		 echo "not writing file"
		else
		 cat > $HOME/.fonts.conf << _EOF
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
	<match target="font">
		<test qual="all" name="rgba">
			<const>unknown</const>
		</test>
		<edit name="rgba" mode="assign"><const>rgb</const></edit>
	</match>		
	<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">
			<int>$hintstyle</int>
		</edit>
	</match>
	<selectfont>
		<rejectfont>
			<pattern>
				<patelt name="scalable"><bool>false</bool></patelt>
			</pattern>
		</rejectfont>
	</selectfont> 
</fontconfig>
_EOF
		fi
		;;
esac

exit 0
