# TODO: Add comment
# 
# Author: E.Korsching  2025
###############################################################################


# write2PDF  --  very simple

write2PDF <- function(x, col="black", cex=0.7, ww=80, ll=30){
	# write text one to many lines into a graph page
	#  in conjunction with the pdf() -- dev.off() functions
	#  family="mono" 'sans' 'serif'   font=1 1-plain 2-bold 3-italic 4-bolditalic
	# design : page format according to pdf()
	#  so check number of possible lines and number of characters in one line - before -
	# x: one text element ev. with several line breaks and blank spaces
	#  assume a monospace font for the arrangement on the page
	# ww: width / characters, ll: number of lines
	nchar.l <- nchar(x)
	cat("\n nchar",nchar.l)
	
	parm <- par()$mar
	parf <- par()$family
	par(family="NimbusMon",font=2)	# mono,NimbusMon,ArialMT
	par(mar=c(2,1,1,1))
	plot(NA, xlim=c(0,ww), ylim=c(ll,0), type='n', axes=F, xlab='', ylab='')
	text( x=1, y=1, labels=x, adj=c(0,1), col=col, cex=cex)
	par(mar=parm)
	par(family=parf)
	return()
}


# simple example

#pdf("results_fn_tests/perm_cf_meanDiff_aadbi_aadbi2.pdf", width=11, height=5)
## text
#aaa <- paste(
#		"Comparison of meanDiff aadbi aadbi2\n\n",
#		"mean diff : classical\n",
#		"aadbi     : all local differences between two groups - abs on diff (see graph)\n",
#		"aadbi2    : all local differences between two groups - no abs on diff -> behaves identical to mean-mean\n",
#		sep="")
#write2PDF(aaa, col="black", cex=0.7, ww=120, ll=25)
## ...e.g. other graph pages... but this could also happen first
#dev.off()





# write2PDF.c  --  older function  --  but sometimes more flexible

write2PDF.c <- function(x, add=F, col="black", cex=0.7, txt.lines=40, txt.width=40){
	# write text line/block into a graph page -- aka create pdf text output
	#  useful in conjunction with the pdf() -- dev.off() functions
	#  family="mono" 'sans' 'serif'   font=1 1-plain 2-bold 3-italic 4-bolditalic
	# design : page format according to pdf(), 40 lines : txt.lines, width 40 : txt.width
	# x: (mandtory) data frame: column 1: text line ( "\n" : multilines),
	#    column 2: order of line positions: integer 1..txt.lines (top to bottom)
	#    column 3: indent: integer 1..txt.width or 0 if no indent
	#   see example below
	# add T,F : add to present graph page or create new
	nchars <- function(x){ sapply(x, function(x){ nchar(x) }, simplify=T) }
	
	if(is.matrix(x)){
		x <- as.data.frame(x)
		x[,2] <- as.integer(x[,2])
		x[,3] <- as.integer(x[,3])
	}
	
	nr <- nrow(x)
	if(is.null(nr)){ stop(paste("write2PDF.c: no input",nr,": lines")) }
	ind.r <- range(nchars(x[,1]))
	cat("\n line length",ind.r)
	if(ind.r[1]<0 | ind.r[2]>txt.width){ warning(paste("write2PDF.c: text line length <0 or >",txt.width)) }
	ind.r <- range(x[,3])
	cat("\n indent length",ind.r)
	if(ind.r[1]<0 | ind.r[2]>txt.width){ warning(paste("write2PDF.c: indent <0 or >",txt.width)) }
	ind.r <- range(x[,2])
	cat("\n line number",ind.r)
	if(ind.r[2]>txt.lines){ warning(paste("write2PDF.c: line numbers >",txt.lines)) }
	
	parm <- par()$mar
	parf <- par()$family
	par(family="NimbusMon",font=2)	# mono,NimbusMon,ArialMT
	par(mar=c(2,1,1,1))
	if(!add){ plot(NA, xlim=c(0,txt.width), ylim=c(txt.lines,0), type='n', axes=F, xlab='', ylab='') }
	for(i in 1:nr){
		text( x=x[i,3], y=x[i,2], labels=paste(x[i,1]), adj=c(0,0.5), col=col, cex=cex)
	}
	
	par(mar=parm)
	par(family=parf)
	return()
}



#aaa <- pdfFonts()#[["family"]]
# systemfonts::system_fonts()
# ... showing you which fonts are installed on your device. If that didn’t work:
# install.packages("systemfonts")
#
# now:
# systemfonts::system_fonts() |> View()			cursor buttons

# names(pdfFonts())


# simple example

#pdf("results_fn_tests/perm_cf_meanDiff_aadbi_aadbi2.pdf", width=11, height=5)
## text, vertical position, indent
#aaa <- NULL; aaz <- 1; aai <- 1	# content, real line counter, matrix counter
#aaa <- rbind(aaa, c(paste("Comparison of meanDiff aadbi aadbi2",collapse=""), aaz, 0) )
#aaz <- aaz+2; aai <- aai+1	# empty line
#aaa <- rbind(aaa, c(paste("mean diff : classical",collapse=","), aaz, 0) )
#aaz <- aaz+1; aai <- aai+1
#aaa <- rbind(aaa, c(paste("aadbi     : all local differences between two groups - abs on diff (see graph",collapse=","), aaz, 0) )
#aaz <- aaz+1; aai <- aai+1
#aaa <- rbind(aaa, c(paste("aadbi2    : all local differences between two groups - no abs on diff -> behaves identical to mean-mean",collapse=","), aaz, 0) )
#write2PDF.c(aaa, add=F, col="black", cex=0.7, txt.lines=40, txt.width=50)
## ...e.g. other graph pages... but this could also happen first
#dev.off()




correct.table2PDF <- function(x, seq=1, fromto=NULL){
	# adjust the line object - primarly good for mono font
	# x: character vector,  seq: one or more operations
	# fromto: c(from line:to line)
	if(is.null(fromto)){ xr <- 1:length(x) }else{ xr <- fromto }
	if(seq==1){
		# replace ^number by ^space-number
		# replace space-number by space-space-number to compensate for a minus sign by other numbers
		# replace [space-]0-space by space-0-space-space-space
		for(i in xr){
			x[i] <- gsub(pattern="^(?=\\d)"," ",x[i],perl=T)
			x[i] <- gsub(pattern="(?<=\\d) +(?=\\d)","  ",x[i],perl=T)
			x[i] <- gsub(pattern="(?<!\\S)\\d(?!\\S)","0   ",x[i],perl=T)
		}
	}
	return(x)
}

#gsub(pattern="(?<!\\S)\\d(?!\\S)","0   ","0 0.01 0.02 0 0.03 0.04 0",perl=T)

#correct.table2PDF("0.25 0.18", seq=1)
#correct.table2PDF("0.05 0.14 0.29", seq=1)
#correct.table2PDF("−0.03 0.05 0 −0.04 −0.07 −0.04 −0.04 0.03 -0.05 0 -0.05 0", seq=1)


# tables

#pdf(file=paste(getwd(),"/tech_tests/test-tables",".",format(Sys.time(), "%Y%m%d%H%M"),".pdf", sep=""),
#		paper="a4", width=7, height=11, title="test", pointsize=10)	# a4,a4r
#par(family="NimbusMon",font=2)	# mono,NimbusMon,ArialMT
## text, vertical position, indent
#aaa <- NULL; aaz <- 1; aai <- 1	# content, real line counter, matrix counter
#aaa <- rbind(aaa, c(paste("Test run aa1 - old dataset 'a.restart.He589366c2'",collapse=""), aaz, 0) )
#aaz <- aaz+2; aai <- aai+1	# empty line
#aaa <- rbind(aaa, c(paste("Global systemic scores (summary)",collapse=""), aaz, 0) )
#aaz <- aaz+1; aai <- aai+1
#aaa <- rbind(aaa, c(paste(dimnames(aa$summary)[[2]],collapse=", "), aaz, 0) )
#aaz <- aaz+1; aai <- aai+1
#aan <- rownames(aa$summary)
#aaa <- rbind(aaa, c(paste(paste(round(aa$summary[1,],2),collapse=" "),aan[1],sep="   "), aaz, 0) )
#aaz <- aaz+2; aai <- aai+1	# empty line
#aaa <- rbind(aaa, c(paste("Optimal, nomalized local systemic scores (sys.local)",collapse=""), aaz, 0) )
#aaz <- aaz+1; aai <- aai+1
#aaa <- rbind(aaa, c(paste(dimnames(aa$sys.local)[[2]],collapse=", "), aaz, 0) )
#aaz <- aaz+1; aai <- aai+1
#aan <- rownames(aa$sys.local)
#for(i in 1:nrow(aa$sys.local)){
#	aaa <- rbind(aaa, c(paste(paste(round(aa$sys.local[i,],2),collapse=" "),aan[i],sep="   "), aaz, 0) )
#	aaz <- aaz+1; aai <- aai+1
#}
#aaz <- aaz+1	# empty line
#aaa <- rbind(aaa, c(paste("Best order (sys.test)",collapse=""), aaz, 0) )
#aaz <- aaz+1; aai <- aai+1
#aan <- rownames(aa$sys.test)
#aaa <- rbind(aaa, c(paste(aan,collapse=" "), aaz, 0) )
#write2PDF(aaa, add=F, col="black", cex=0.7, txt.lines=40, txt.width=50)
#dev.off()


#pdf(file=paste(getwd(),"/tech_tests/test-tables",".",format(Sys.time(), "%Y%m%d%H%M"),".pdf", sep=""),
#		paper="a4", width=7, height=11, title="test", pointsize=10)	# a4,a4r
#par(family="NimbusMon",font=2)	# mono,NimbusMon,ArialMT
## text, vertical position, indent
#aaa <- NULL; aaz <- 1; aai <- 1	# content, real line counter, matrix counter
#aaa <- rbind(aaa, c(paste("Title Title Title",collapse=""), aaz, 0) )
#aaz <- aaz+2; aai <- aai+1	# empty line
#aaa <- rbind(aaa, c(paste(dimnames(aa$summary)[[2]],collapse=","), aaz, 0) )
#aaz <- aaz+1; aai <- aai+1
#aan <- rownames(aa$summary)
#aaa <- rbind(aaa, c(paste(paste(round(aa$summary[1,],2),collapse=" "),aan[1],sep="   "), aaz, 0) )
#aaz <- aaz+2; aai <- aai+1	# empty line
#aaa <- rbind(aaa, c(paste(dimnames(aa$sys.local)[[2]],collapse=","), aaz, 0) )
#aaz <- aaz+1; aai <- aai+1
#aan <- rownames(aa$sys.local)
#for(i in 1:nrow(aa$sys.local)){
#	aaa <- rbind(aaa, c(paste(paste(round(aa$sys.local[i,],2),collapse=" "),aan[i],sep="   "), aaz, 0) )
#	aaz <- aaz+1; aai <- aai+1
#}
#aaz <- aaz+1	# empty line
#aaa <- rbind(aaa, c(paste(paste(dimnames(aa$sys.test)[[2]][1:2],collapse=","),", ...",sep=""), aaz, 0) )
#aaz <- aaz+1; aai <- aai+1
#for(i in 1:nrow(aa$sys.test)){
#	aaa <- rbind(aaa, c(paste(round(aa$sys.test[i,],2),collapse=" "), aaz, 0) )
#	aaz <- aaz+1; aai <- aai+1
#}
#aaz <- aaz-1; aai <- aai-1
#aaa <- correct.table2PDF(aaa, seq=1, fromto=c((aai-nrow(aa$sys.test)+1):aai))
#aan <- rownames(aa$sys.test)
#k <- 1
#for(i in (aai-nrow(aa$sys.test)+1):aai){
#	aaa[i,1] <- paste(paste(aaa[i,1],collapse=" "),aan[k],sep="   ")
#	k <- k+1
#}
#write2PDF(aaa, add=F, col="black", cex=0.7, txt.lines=40, txt.width=50)
#dev.off()


