#!/bin/bash
##################################################################################
## Bashish, a console theme engine
## Copyright (C) 2010 Thomas Eriksson
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
##################################################################################
## this code section contains resize, font and reposition code, this is needed since
## the windowmanager might reposition the window or move the window out of focus
##
###############################
###                         ###
### font changer version 4u ###
###                         ###
###############################
## versions
##
## 2.0.5  - version 5u  - optimization to use \033[50 if no complex font chaning is requested
## 2.0.4  - version 4u  - branched off an urxvt version since much furthter complexity was needed to support urxvt
## 2.0.4  - version 4.1 - fixed major bugs in 4.0
## 2.0.0  - version 4.0 - removed some features, ie. no font file changing
## 1.9.25 - version 3.5 - simplification, faster on XTerm, rxvt-unicode and Eterm
## 1.9.24 - version 3 - complete rewrite, increased number of font slots
## 1.9.23 - version 2.5 - align to grid, and position fixation
## 1.9.12 - version 2 - dual font slots, one for applications and one for btapp's
## ?????  - version 1.5 - XTerm optimization
## ?????  - version 1 - utilizing only one slot, both for applications and btapp
##
##################################################################################
function _bashish_urxvt_font
{
	## Disable scrollbar since it's impossible to detect, and thus causes the 
	## window to increment in size after a font change.
	## Need to do this every time since rxvt-unicode has a buggy scrollbar feature
	printf "\033[?30l"

	local IFS
	## if the font and size is the same as before, there is no need to traverse
	## this chunk of code
	if test "x${BASHISH_STATE[400]}" != "x${XFONT[*]}${FONTNAME[*]}${FONTNAME_ITALIC[*]}${FONTNAME_BOLD[*]}${FONTSIZE[*]}${FONTSIZE_ITALIC[*]}${FONTSIZE_BOLD[*]}"
	then
		local i=0 POSITION_X_INCREMENT="" POSITION_Y_INCREMENT="" PIXELS LINES PRE_SIZE_X PRE_SIZE_Y IFS="
"
		for LINES in $(xwininfo -id $WINDOWID)
		do
			IFS=" "
			for PIXELS in $LINES
			do :;done
			IFS="
"
			case $i in
			0):;;
			1) test x$POSITION_X = x&& {
					POSITION_X=$PIXELS
					POSITION_X_INCREMENT=1
				}
			
			;;
			2) test x$POSITION_Y = x&& {
					POSITION_Y=$PIXELS
					POSITION_Y_INCREMENT=1
				}
			;;
			3)
			test x$POSITION_X_INCREMENT = x1 && let POSITION_X=$POSITION_X-$PIXELS-1
			;;
			4) 
			test x$POSITION_Y_INCREMENT = x1 && let POSITION_Y=$POSITION_Y-$PIXELS-1
			;;
			5) PRE_WINSIZE_X=$PIXELS;;
			6) PRE_WINSIZE_Y=$PIXELS;;
			*)break;;
			esac
			let i++
		done
		## FIXME: test xft compiled in?
		## unset FONTNAME[*] FONTNAME_ITALIC[*] FONTNAME_BOLD[*] FONTSIZE_N N N

		local FONT_BULKVAR=""
		local URXVTANTIALIAS=false
		test "x$URXVTANTIALIAS" = x1 && URXVTANTIALIAS=true
		URXVTANTIALIAS=true

		
		## Bashish first checks for a scalable font in the FONTNAME array
		## if no font reference could be found at position i
		## the bitmap XFONT array is checked at position i
		## should XFONT be unset at position i, the font loading will stop.

		test "x${FONTNAME}${XFONT}" != x && {
		## regular slant
		local i=0 FONT_BULKVAR="\033]50;"
		while test $i -lt 1024
		do
			case "$i" in
			0) :;;
			*) FONT_BULKVAR=$FONT_BULKVAR",";;
			esac
			if test "x${FONTNAME[$i]}" != x
			then
				FONT_BULKVAR=${FONT_BULKVAR}"xft:${FONTNAME[$i]}:autohint=true:antialias=$URXVTANTIALIAS"
			elif test "x${XFONT[$i]}" != x
			then
				FONT_BULKVAR=$FONT_BULKVAR"${XFONT[$i]}"
			else
				FONT_BULKVAR=$FONT_BULKVAR"\007"
				i=1024
			fi
			test "x$i" != x1024 && if test "x${FONTSIZE[$i]}" != x
			then
				FONT_BULKVAR=${FONT_BULKVAR}":size=${FONTSIZE[$i]}"
			else
				FONT_BULKVAR=${FONT_BULKVAR}":size=${FONTSIZE[$i]}"
			fi
			let i++
		done
		}
		
		test "x${FONTNAME_BOLD}${XFONT_BOLD}" != x && {
		## bold slant
		i=0
		FONT_BULKVAR="$FONT_BULKVAR\033]711;"
		while test $i -lt 1024
		do
			case "$i" in
			0) :;;
			*) FONT_BULKVAR=$FONT_BULKVAR",";;
			esac
			if test "x${FONTNAME_BOLD[$i]}" != x
			then
				FONT_BULKVAR=${FONT_BULKVAR}"xft:${FONTNAME_BOLD[$i]}:bold:autohint=true:antialias=$URXVTANTIALIAS"
			elif test "x${XFONT[$i]}" != x
			then
				FONT_BULKVAR=$FONT_BULKVAR"${XFONT[$i]}"
			else
				FONT_BULKVAR=$FONT_BULKVAR"\007"
				i=1024
			fi
			test "x$i" != x1024 && if test "x${FONTSIZE[$i]}" != x
			then
				FONT_BULKVAR=${FONT_BULKVAR}":size=${FONTSIZE[$i]}"
			elif test x${FONTSIZE_BOLD[0]} != x
			then
				FONT_BULKVAR=${FONT_BULKVAR}":size=${FONTSIZE_BOLD[0]}"
			else
				FONT_BULKVAR=${FONT_BULKVAR}":size=${FONTSIZE[0]}"
			fi
			let i++
		done
		}
		
		test "x${FONTNAME_ITALIC}${XFONT_ITALIC}" != x && {
		## italic slant
		i=0
		FONT_BULKVAR="$FONT_BULKVAR\033]712;"
		while test $i -lt 1024
		do
			case "$i" in
			0) :;;
			*) FONT_BULKVAR=$FONT_BULKVAR",";;
			esac
			if test "x${FONTNAME_ITALIC[$i]}" != x
			then
				FONT_BULKVAR=${FONT_BULKVAR}"xft:${FONTNAME_ITALIC[$i]}:autohint=true:italic:antialias=$URXVTANTIALIAS"
			elif test "x${XFONT[$i]}" != x
			then
				FONT_BULKVAR=$FONT_BULKVAR"${XFONT[$i]}"
			else
				FONT_BULKVAR=$FONT_BULKVAR"\007"
				i=1024
			fi
			test "x$i" != x1024 && if test "x${FONTSIZE[$i]}" != x
			then
				FONT_BULKVAR=${FONT_BULKVAR}":size=${FONTSIZE[$i]}"
			elif test x${FONTSIZE_ITALIC[0]} != x
			then
				FONT_BULKVAR=${FONT_BULKVAR}":size=${FONTSIZE_ITALIC[0]}"
			else
				FONT_BULKVAR=${FONT_BULKVAR}":size=${FONTSIZE[0]}"
			fi
			let i++
		done
		}
		printf "$FONT_BULKVAR"
		BASHISH_STATE[400]="${XFONT[*]}${FONTNAME[*]}${FONTNAME_ITALIC[*]}${FONTNAME_BOLD[*]}${FONTSIZE[*]}${FONTSIZE_ITALIC[*]}${FONTSIZE_BOLD[*]}"
	else
		kill -s WINCH "$LAUNCHER_PID" 2>/dev/null
	fi
	IFS="
"
	i=0
	for LINES in $(xwininfo -id $WINDOWID)
	do
		IFS=" "
		for PIXELS in $LINES
		do :;done
		IFS="
"
		case $i in
		0|1|2|3|4):;;
		5) POST_WINSIZE_X=$PIXELS;;
		6) POST_WINSIZE_Y=$PIXELS;;
		*)break;;
		esac
		let i++
	done

	i=0
	IFS=" "
	## need to replace this with some spiffy while loop
	for PIXELS in $(xprop -id $WINDOWID |grep resize\ increment)
	do
		test "x$i" = x4 && FONTSIZE_X=$PIXELS
		test "x$i" = x6 && FONTSIZE_Y=$PIXELS
		let i++
	done

	## Get new size calculated from old size and new font size
	let SIZE_X="$PRE_WINSIZE_X/$FONTSIZE_X"
	let SIZE_Y="$PRE_WINSIZE_Y/$FONTSIZE_Y"

	test "x$PRE_SIZE_X" != x$SIZE_X -a "x$PRE_SIZE_Y" != x$SIZE_Y && kill -s WINCH $LAUNCHER_PID 2>/dev/null
	BASHISH_BULKVAR="$BASHISH_BULKVAR\033[?30l"
}
