#!/bin/sh
#] 
#] *********************
#] $ bash "$d_bin"'image: put [cap, ref]s on [fig, tbl]s.sh' 
#]    generate captionHTML-to-imagePNG files
# www.BillHowell.ca  19Aug2023 initial
# view in text editor, using constant-width font (eg courier), tabWidth = 3


# 21Aug2021 WARNING! in case of spaces in [dir, fname] not apo-surround :
#		bash "$goApp"	"$delay_brs"	"browser"	"'""$d_temp""'"	"'""$fCaptionRefcHtml""'"
# 28Aug2023 MUST wait a few seconds after wmctrl & other operations! or window is corrupted!!!!
# from : https://stackoverflow.com/questions/125951/
#	edited Feb 6, 2013 at 18:10, booleys1012, answered Sep 27, 2008 at 6:52, Shannon Nelson

# see also :
#	"$d_web"'Neural nets/TrNNs_ART/0_TrNNs_ART TblOfContents.txt'
#	"$d_web"'Neural nets/TrNNs_ART/0_TrNNs_ART [ToDos, html, Kindle].txt'
#	"$d_SysMaint"'images/0_image notes.txt'  
#	"$d_bin"'image: put [cap, ref]s on [fig, tbl]s.sh'
#24************************24




#24************************24
# List of operators, generated with :
# $ grep  "^#]"  "$d_bin"'image: put [cap, ref]s on [fig, tbl]s.sh' |  sed "s/^#\] /#  /" 
#
#  *********************
#  $ bash "$d_bin"'image: put [cap, ref]s on [fig, tbl]s.sh' 
#     generate captionHTML-to-imagePNG files
#  +-----+
#  Setup 
#  +-----+
#  make_pCaptionHtmlL()  - create a list of captions in webPage "$p_listOfFigTbl"
#  pImg_get_width_height()  - res ipsa loquitor
#  dir_fHtml_refc_to_pPng()  - convert ONE html (text markup) file to png image
#     currently this is specific to the Grossberg project
#  pHtmlL_to_pPngL()  - convert a list of html (text markup) files to png images


#24************************24
#] +-----+
#] Setup 

# loadded by fileops.sh 
#	source  "$d_bin""standard header.sh"

source "$d_bin"'fileops.sh' 

# needed for ??? 
source  "$d_bin""starter/start_app.sh"  

# from "$d_bin""start.sh"
export  delay_fmg=" 3s"
export  delay_trm=" 2s"
export  delay_brs=" 4s"
export  delay_edt=" 3s"
export  delay_LOf=" 4s"
export  delay_pdf=" 4s"

# from "$d_bin""starter/start0_0_input.sh"  - handle start menu selections 
export d_start="$d_bin"'starter/'
export goApp="$d_start"'start0_app.sh'



d_TrNNs_ART="$d_web"'Neural nets/TrNNs_ART/'
d_caption="$d_TrNNs_ART"'captions html/'
d_capSide="$d_TrNNs_ART"'captions sideBar/'
d_imgCapt="$d_TrNNs_ART"'images- captioned/'
d_imgSide="$d_TrNNs_ART"'images- sideBar/'

p_listOfFigTbl="$d_TrNNs_ART"'Grossbergs list of [figure, table]s.html'
p_caption_txtL="$d_caption"'0_p_caption_txtL.txt'
p_caption_pngL="$d_caption"'0_p_caption_pngL.txt'

# from "$d_bin"'starter/start_app.sh'
#WARNING!:  [perpetual, deep] flaw was lack of escaping of "problematic regexpr" chrs :
#	eg win_PosnSize="0,$1,$2,$3,$4" -> should be "0\,$1\,$2\,$3\,$4" 
#	(or not so practical : "0"','"$1"','"$2"','"$3"','"$4")
# but win_move_last- just use space-separated quoted strs : 0  "$left"  "$top"  "$width"  "$height"  



#24************************24
#] +-----+
#] preparations: listo images, make html captions


#] dGrossbg_make_pListOfFigTbl()  - from dir of images, create pListOfFigTbl
#]    this assumes that ALL png files have captions, which may not be true
# 28Sep2023 is this outdated now?  not as first step?

	dGrossbg_make_pListOfFigTbl()
{  
	if		[ -f  "$d_caption"'0_pCaptionL all.txt' ]; then
		pInn_archiveLocal_pDateMod  "$d_caption"'0_pCaptionL all.txt'
	fi

	find  "$d_imgCapt"  -type f -name "*.png"  |  sed  's|/home/bill/web/Neural nets/TrNNs_ART/images- Grossberg 2021/||;s|\.png|.html|'  |  sort -u  >"$d_caption"'0_pCaptionL all.txt'   
} 


#] pListOfFigTbl_make_pCaptionHtmlL()  - create a list of captions in webPage "$p_listOfFigTbl"

	pListOfFigTbl_make_pCaptionHtmlL()
{  
	if		[ -f  "$p_caption_txtL" ]; then
		pInn_archiveLocal_pDateMod  "$p_caption_txtL"
	fi

	if		[ -f  "$p_caption_pngL" ]; then
		pInn_archiveLocal_pDateMod  "$p_caption_pngL"
	fi

	grep  '<LI><A HREF=\"'    "$p_listOfFigTbl"  |  grep  --invert-match  '^<LI><A HREF=\"#'  |  sed  's|<LI><A HREF=\"\(.*\).png\">image<\/a> ||'  >"$p_caption_txtL"
	grep  '<LI><A HREF=\"'    "$p_listOfFigTbl"  |  grep  --invert-match  '^<LI><A HREF=\"#'  |  sed  's|<LI><A HREF=\"\(.*\)images- Grossberg 2021\(.*\)\">image<\/a>.*|\1captions html\2|'   >"$p_caption_pngL"
	while IFS='' read -u 9 pPng  &&  read -u 8 -r pCaptionTxt;  do
		pHtml=$( echo  "$pPng"  |  sed  's|\(.*\)\.png|\1.html|' )
		#	echo  "$pHtml"
			echo  "$pCaptionTxt"  >"$pHtml"
	done 9<"$p_caption_pngL"  8<"$p_caption_txtL"  
}  


#] 08********08
#] convert normal html captions to images


#] pImg_get_width_height()  - res ipsa loquitor

	pImg_get_width_height()
{  
	file "$1"  |  sed  's|.*\, \([0-9]*\) x \([0-9]*\)\,.*|\1  \2|'
}  

# $ file  "$d_web"'Neural nets/TrNNs_ART/images- Grossberg 2021/p024fig01.15 READ circuit: REcurrent Associative Dipole.png'  |  sed  's|.*\, \([0-9]*\) x \([0-9]*\)\,.*|\1  \2|'
# /home/bill/web/Neural nets/TrNNs_ART/images- Grossberg 2021/p024fig01.15 READ circuit: REcurrent Associative Dipole.png: PNG image data, 332 x 409, 8-bit/color RGB, non-interlaced
#	pImg_get_width_height  "$d_imgCapt"'p024fig01.15 READ circuit: REcurrent Associative Dipole.png'


#] dir_fHtml_refc_to_pPng()  - convert ONE html (text markup) file to png image
#]    currently this is specific to the Grossberg project
# search "Linux and how do I count chrs in a string?"
# https://linuxize.com/post/linux-wc-command/
# wc -m  option counts chrs
# 28Sep2023 laptop beeps with each image (winID problem as below) - probably signal processing?
#	X Error of failed request:  BadWindow (invalid Window parameter)
#	  Major opcode of failed request:  25 (X_SendEvent)
#	  Resource id in failed request:  0x372f75f
#	  Serial number of failed request:  31
#	  Current serial number in output stream:  31
# current setting after : bash "$goApp" 1s; eval 2s; funcEnd commented out, no delay


	dir_fHtml_refc_to_pPng()
{  
	dir="$1"
	fCaptionTxt="$2" 
	refc="$3"

	pCaptionTxt="$dir$fCaptionTxt"
	# use same fNamBase for p[Caption, Grossbg]Png files : 
	pCaptionPng=$( echo  "$pCaptionTxt"  |  sed  's|captions html|images- captioned|;s|\.html|.png|' ) 
	pGrossbgPng=$( echo  "$pCaptionTxt"  |  sed  's|captions html|z_Archive/images- Grossberg 2021|;s|\.html|.png|' ) 
	#	echo  "$pCaptionTxt"
	#	echo  "$pCaptionPng"
	#	echo  "$pGrossbgPng"

	# remove [post-||-commentary from captions, ending <BR>]
	# should be ONE, and 1 only, BR to finish comment before reference
	captionTxt=$( cat  "$pCaptionTxt"  |  sed  's#\(.*\)[|][|].*#\1#' )
	flagBR=$( echo  "$captionTxt"  |  sed  's|.*\(<BR>\)$|<BR>|' )
	#	echo  "flagBR = $flagBR" 
	if  [[ "$flagBR" == '<BR>' ]]; then 
		BR=''
	else
		BR='<BR>'
	fi
	captionTxt="$captionTxt$BR$refc"
	fCaptionRefcHtml='dir_fHtml_refc_to_pPng captionTxt.html'
	pCaptionRefcHtml="$d_temp$fCaptionRefcHtml"
	#	echo  "captionTxt = $captionTxt"  
	#	echo  "fCaptionRefcHtml = $fCaptionRefcHtml" 
	#	echo  "pCaptionRefcHtml = $pCaptionRefcHtml" 
	echo  "$captionTxt"  >"$pCaptionRefcHtml" 

	# get pixel width of image, winID_last() from "$d_bin""starter/start0_app.sh"
	width_height=$( pImg_get_width_height "$pGrossbgPng" )
	width=$( echo  "$width_height"  |  sed  's|\(.*\)  .*|\1|' )
	hight=$( echo  "$width_height"  |  sed  's|.*  \(.*\)|\1|' )
	# for 10pt font, chrs are ~10px each
	nChrCaptionTxt=$( echo  "$captionTxt"  |  wc -m )
	#	echo  "width  hight = $width_height"
	#	echo  "image  width = $width"
	#	echo  "image  hight = $hight"
	#	echo  "nChrCaptionTxt = $nChrCaptionTxt"
	if  [ 200  -ge  "$width" ]; then
		let "lineChrLen = 30"
	else
		let "lineChrLen = $width / 7"
	fi 
	let  "nLines = $nChrCaptionTxt / $lineChrLen"

	# count <[BR, LI]> in caption to adjust nLines
	nBR=$( echo  "$captionTxt"  |  grep -c  '<BR>' )
	nLI=$( echo  "$captionTxt"  |  grep -c  '<LI>' )
	let  "nLines = $nLines + $nBR / 2 + $nLI"
	#let "captionPixHeight =  $nLines * 30 + $height_menu_top + $height_menu_btm"  
	let  "captionPixHeight =  $nLines * 25 + 80"  
	#	echo  "lineChrLen = $lineChrLen"
	#	echo  "nLines = $nLines"
	#	echo  "recalc'd nLines = $nLines"
	#	echo  "captionPixHeight = $captionPixHeight"

	# MUST wait a few seconds after wmctrl & other operations! or window is corrupted!!!!
	# standard is 4s sleep, try 1s  ?!?! -> works on one example, try set of test examples
	bash  "$goApp"  "1"  "browser"  "$d_temp"  "$fCaptionRefcHtml"
		sleep 2s			# normally 1s, also tried 2+s
	winID=$( winID_last )
	#	echo  "winID  = $winID"

	# resize window to compact caption, match width of image if pratical
	# [winID, top, win_move_last()] are from "$d_bin""starter/start0_app.sh"
	#	win_move_last ->  gravity\,$left\,$top\,$width\,$height  
	winPosnSize="0,985,0,$width,$captionPixHeight"
	cmd='wmctrl -i -r '"$winID"' -e  '"$winPosnSize"
	#	echo  "winPosnSize = $winPosnSize"
	#	echo  "cmd = $cmd"
	eval "$cmd"  
	# MUST wait a few seconds after wmctrl & other operations! or window is corrupted!!!!
	sleep 2s			# normally 1s, also tried 2+s
	winID=$( winID_last )
	#	echo  "winID  = $winID"

	# screenAreaCapture - make sure that captionTxt is on top of window viewing
	let  "captureWidth =  $width * 135 / 100"		# [from, to] : [130, 135]; 
	let  "capturePxHgh =  $nLines * 25 +  60"		# $nLines * 30"  
	let  "xOffset =   0"       
	let  "yOffset =  85"  
	#	echo "captureWidth = $captureWidth"
	#	echo "capturePxHgh =  nLines * 30 = $capturePxHgh"  
	#	echo "xOffset = $xOffset"
	#	echo "yOffset = $yOffset"
	import -window "$winID" -crop "$captureWidth"x"$capturePxHgh"+"$xOffset"+"$yOffset" -quality 90 "$pCaptionPng"

	# vertical combine [image, caption]
	convert -append  -fill  black  "$pGrossbgPng"  "$pCaptionPng" "$pCaptionPng"

	# close window
	cmd='xdotool key  --window '"$winID"' ctrl+shift+w' 
	#	echo  "cmd = $cmd"
	eval "$cmd"  
	# MUST wait a few seconds after wmctrl & other operations! or window is corrupted!!!!
	#	sleep 2s			# normally commented-out, no delay
}


# $ bash "$d_bin"'image: put [cap, ref]s on [fig, tbl]s.sh' 


#] dir_pImgCapL_refc_to_pPngL_run(dir pImgCapL refc)  - convert pImgCapL files to png images
#]    do NOT run directly: this is run ONLY from dir_pHtmlL_refc_to_pPngL()

	dir_pImgCapL_refc_to_pPngL_run()
{  
	dir="$1"
	pImgCapL="$2"
	refc="$3"

	while IFS='' read -u 9 fCapHtml;  do
		echo  >>"$pLog"  "$fCapHtml"
		dir_fHtml_refc_to_pPng  "$dir"  "$fCapHtml"  "$refc"
	done 9< "$pImgCapL"
}


#] dir_pImgCapL_refc_to_pPngL(no args)  - select pCaptionL, run dir_fHtml_refc_to_pPng
# www.BillHowell.ca 29Sep2023 initial built from existing lists

	dir_pImgCapL_refc_to_pPngL()
{  
	date_ymdhms=$(date +"%0y%0m%0d %0kh%0Mm%0Ss")
	echo  >>"$p_log" "$date_ymdhms dir_pImgCapL_refc_to_pPngL"

	pLog="$d_TrNNs_ART"'captions html/0_pLog dir_pImgCapL_refc_to_pPngL.txt'
	if 	[ -f "$pLog" ];then 
		pInn_archiveLocal_pDateMod  "$pLog"
	fi

	#pHtmlL="$d_caption"'0_pImgCapL all.txt'
	#pHtmlL="$d_caption"'2_pImgCapL problems.txt'
	#pHtmlL="$d_caption"'3_pImgCapL test 1.01-1.07.txt'
	#pHtmlL="$d_caption"'3_pImgCapL test 1.01-1.25.txt'
	#pHtmlL="$d_caption"'3_pImgCapL test 1.26-17.06.txt'
	#pHtmlL="$d_caption"'4_pImgCapL fixes 231019.txt'
	 pHtmlL="$d_caption"'4_pImgCapL fix cover image 231019.txt'

	start="$date_ymdhms"
	echo   >"$pLog"  '24************************24'
	echo  >>"$pLog"  '"$d_TrNNs_ART""captions html/0_pLog dir_pImgCapL_refc_to_pPngL_run.txt"' 
	echo  >>"$pLog"  "pHtmlL : $pHtmlL"
	echo  >>"$pLog"  ''

	dir_pImgCapL_refc_to_pPngL_run  "$d_caption"  "$pHtmlL"  "$refc"

	date_ymdhms=$(date +"%0y%0m%0d %0kh%0Mm%0Ss")
	echo  >>"$pLog"  ''
	echo  >>"$pLog"  "start : $start"
	echo  >>"$pLog"  "end   : $date_ymdhms"
	echo  >>"$pLog"  ''
}  


# $ bash "$d_bin"'image: put [cap, ref]s on [fig, tbl]s.sh' 


#] 08********08
#] convert nomenclatureHtml to image, join with image as sideBar


#] pImg_imgHeight_sideBar_to_pPngL_run()  - 
#]    do NOT run directly: this is run ONLY from pCapt_pSide_pBotm_join_pPng()


# d_caption="$d_TrNNs_ART"'captions html/'
# d_capSide="$d_TrNNs_ART"'captions sideBar/'
# d_imgCapt="$d_TrNNs_ART"'images- captioned/'
# d_imgSide="$d_TrNNs_ART"'images- sideBar/'


#] pCapt_pSide_pBotm_join_pPng(no args)  - screenCapture sideBarHtml, join images
#]    for images p520fig14.02, p552fig15.23
# www.BillHowell.ca 29Sep2023 initial built from existing lists

	pCapt_pSide_pBotm_join_pPng()
{  
	date_ymdhms=$(date +"%0y%0m%0d %0kh%0Mm%0Ss")
	echo  >>"$p_log" "$date_ymdhms dir_fHtmlL_refc_to_pPngL_run"


#	# p520fig14.02 Macrocircuit of the main brain regions
#	pImgCapt="$d_imgCapt"'p520fig14.02 pART [cognitive-emotional, working memory] dynamics: main brain [regions, connections].png' 
#	pInn_archiveLocal_pDateMod  "$pImgCapt"
#
#	pSide="$d_imgSide"'p520fig14.02 Side.png'
#	pBotm="$d_imgSide"'p520fig14.02 Botm.png'
#	pImgCaptSide="$d_imgSide"'p520fig14.02 ImgCaptSide.png'
#	pImgCaptSideBotm="$d_imgSide"'p520fig14.02 ImgCaptSideBotm.png'
#
#	convert +append  -fill  black  "$pImgCapt"		"$pSide"		"$pImgCaptSide"
#	convert -append  -fill  black  "$pImgCaptSide"	"$pBotm"		"$pImgCaptSideBotm"
#	cp  "$pImgCaptSideBotm"  "$pImgCapt"


#	# p552fig15.23 Cerebellar biochemistry: mGluR supports adaptively timed conditioning at cerebellar Purkinje cells.html
#	pImgCapt="$d_imgCapt"'p552fig15.23 Cerebellar biochemistry: mGluR supports adaptively timed conditioning at cerebellar Purkinje cells.png' 
#	pInn_archiveLocal_pDateMod  "$pImgCapt"
#
#	pSide="$d_imgSide"'p552fig15.23 Side.png'
#	pImgCaptSide="$d_imgSide"'p552fig15.23 ImgCaptSide.png'
#
#	convert +append  -fill  black  "$pImgCapt"		"$pSide"		"$pImgCaptSide"
#	cp  "$pImgCaptSide"  "$pImgCapt"


	# cover image
	pImg="$d_TrNNs_ART"'z_Archive/images- Grossberg 2021/cover image.png'
	pCap="$d_imgSide"'cover image caption.png' 
	pImgCapBotm="$d_imgSide"'cover image ImgCaptBotm.png' 
	pImgCapFinl="$d_imgCapt"'cover image.png'
	#	echo  "$pImg"
	#	echo  "$pCap"
	#	echo  "$pImgCapBotm"
	#	echo  "$pImgCapFinl"
	convert -append  -fill  black  "$pImg"				"$pCap"		"$pImgCapBotm"
	cp  "$pImgCapBotm"  "$pImgCapFinl"

}  



#08********08
# Procedures -  To run :



#08********08
# run - select one of options

# from dir of images, create pListOfFigTbl
#	dGrossbg_make_pListOfFigTbl

#	pListOfFigTbl_make_pCaptionHtmlL
#	pImg_get_width_height  "$d_imgCapt"'p024fig01.15 READ circuit: REcurrent Associative Dipole.png'

#	list of html cations in "$d_TrNNs_ART"'captions html/'
# $ d_TrNNs_ART="$d_Neural nets"'TrNNs_ART/'
# $ find  "$d_TrNNs_ART"'captions html/'  -type f  -name "*.html"  |  sed  "s|/home/bill/web/Neural nets/TrNNs_ART/captions html/||;s|:.*||"  |  sort  -u >"$d_TrNNs_ART"'captions html/0_pCaptionL all.txt'  

#	[reference, permission] 
	refc='&#169 <FONT size=2>Stephen Grossberg 2021 "Conscious Mind, Resonant Brain" Oxford University Press, ISBN 978-0-19-007055-7<BR>Figure and caption reprinted with the permission of Stephen Grossberg.</font>'

#	single image
	#dir_fHtml_refc_to_pPng  "$d_caption"  'p520fig14.02 pART [cognitive-emotional, working memory] dynamics: main brain [regions, connections].html'  "$refc"
	#dir_fHtml_refc_to_pPng  "$d_caption"  'p552fig15.23 Cerebellar biochemistry: mGluR supports adaptively timed conditioning at cerebellar Purkinje cells.html'  "$refc"


#	lists: pCaptionL in dir_fHtmlL_refc_to_pPngL()
	#dir_pImgCapL_refc_to_pPngL


#	join [Img, Capt, Side, Botm] images 
	pCapt_pSide_pBotm_join_pPng



# $ bash  "$d_bin"'image: put [cap, ref]s on [fig, tbl]s.sh'  

# enddoc
