From: bae@hplsdrn.col.hp.com (Bruce Erickson)
Subject: List information about paragraphs
Date: Fri, 28 Aug 1992 20:44:20 GMT
Organization: Hewlett-Packard Logic Systems Operation

A while ago I posted a note asking if there was a quick way to get
a table of information about paragraphs -- font names, point sizes,
etc.  All responses were of the form "convert to MIF and write an
[awk, sed, ....] script".  Many added "...and when you have done that,
let me know!"

Well, here it is.  It only scans for paragraph formats, not for any
changes to the default paragraph formats actually in the text.

Not clean code, but gives output like this:

          font       var      weight   angle    size     lead    nexttag   
al0_in0   Helvetica  Regular  Regular  Regular  10.0_pt  2.0_pt  body_in1  
al0_in1   Helvetica  Regular  Regular  Regular  10.0_pt  2.0_pt  al0_in1   
bl_in0    Helvetica  Regular  Regular  Regular  10.0_pt  2.0_pt  body_in1  
bl_in1    Helvetica  Regular  Regular  Regular  10.0_pt  2.0_pt  body_in2  
Body      Times      Regular  Regular  Regular  9.0_pt   2.0_pt  none      
body_in0  Helvetica  Regular  Regular  Regular  10.0_pt  2.0_pt  body_in   
body_in1  Helvetica  Regular  Regular  Regular  10.0_pt  0.0_pt  body_in1  
body_in2  Helvetica  Regular  Regular  Regular  10.0_pt  2.0_pt  body_in2  
body_in3  Helvetica  Regular  Regular  Regular  10.0_pt  2.0_pt  body_in3  
Comment   Times      Regular  Regular  Regular  9.0_pt   2.0_pt  none      
Figure    Times      Regular  Regular  Regular  12.0_pt  2.0_pt  none      
h1        Times      Regular  Bold     Regular  12.0_pt  2.0_pt  body_in0  
h2        Times      Regular  Regular  Regular  12.0_pt  2.0_pt  body_in0  
h3        Times      Regular  Regular  Regular  12.0_pt  2.0_pt  body_in0  
h4        Times      Regular  Regular  Regular  12.0_pt  2.0_pt  body_in0  
vl0_in0   Helvetica  Regular  Regular  Regular  10.0_pt  2.0_pt  body_in1  
vl_in0    Helvetica  Regular  Regular  Regular  10.0_pt  2.0_pt  body_in1  
vl_in1    Helvetica  Regular  Regular  Regular  10.0_pt  0.0_pt  body_in2  
vl_in2    Helvetica  Regular  Regular  Regular  10.0_pt  2.0_pt  body_in3  

The code consists of three shell scripts (sorry non UNIX users!):
	-parasumm, the outer script
	-awkgen, which is a generic script I use to create awk scripts....
	-awkrep, another generic script I use to convert labled data to tables..

                  - Bruce Erickson
                     Hewlett Packard
                     bae@col.hp.com


# This is a shell archive.  Remove anything before this line,
# then unpack it by saving it in a file and typing "sh file".
#
# Wrapped by Bruce Erickson <bae@hplsdrn> on Fri Aug 28 13:47:04 1992
#
# This archive contains:
#	parasumm	awkgen		awkrep		
#

LANG=""; export LANG
PATH=/bin:/usr/bin:$PATH; export PATH

echo x - parasumm
cat >parasumm <<'@EOF'
#!/bin/ksh
#	ex:se ts=3 sw=3:
usage="usage: parasumm <MIF file>"
#
#

FMDIR=/site/frame/bin
#	Set up the PATH variable
PATH=/bin:/usr/bin:$FMDIR:.
SHELL=/bin/ksh


set -- `getopt "" "$@"`
if [ $? -ne 0 ]
then
	echo "$usage" >&2
	exit 1
fi

while [ $# -gt 0 ]
do
	case $1 in
		--)	shift; break;;
		-*)	echo "$usage" >&2; exit 1;;
	esac
done

if [ $# -ne 1 ]
then
	echo "$usage" >&2
	exit 1
fi

framefile=$1
tmpawk1=/usr/tmp/psawk_$$
tmpawk2=/usr/tmp/psawk2_$$

trap "rm -f $tmpawk1 $tmpawk2; trap '' 0 1 2 3 4; exit" 0 1 2 3 4

if [ ${framefile%.mif} = $framefile ]
then
	miffile=${framefile#.*}.mif
	# Must obtain .MIF file
	fmbatch <<-EOF
		Open $framefile
		SaveAs mif $framefile $miffile
		Quit
	EOF
else
	# Passed in the .mif file -- a timesaver!
	miffile=$framefile
fi

#	Read in PgfCatalog; create info based on PgfTag
#	When we get to a Paragraph which uses PgfTag xx, look for
#	overrides of font (etc).

# remember: any $, ', or ` to be passed in must be escaped so the shell doesn't intrepret it!
awkgen > $tmpawk1 <<-EOF
	<PgfTag
	tag
				/<PgfTag/ { s=index(\$0, "\`")+1; l=length(\$0)-s-1; tag = substr(\$0, s, l) }
	font
				/<FFamily/ { s=index(\$0, "\`")+1; l=length(\$0)-s-1; font = substr(\$0, s, l) } 
	var
				/<FVar/ { s=index(\$0, "\`")+1; l=length(\$0)-s-1; var = substr(\$0, s, l) }
	weight
				/<FWeight/ { s=index(\$0, "\`")+1; l=length(\$0)-s-1; weight = substr(\$0, s, l) }
	angle
				/<FAngle/ { s=index(\$0, "\`")+1; l=length(\$0)-s-1; angle = substr(\$0, s, l) }
	size
				/<FSize/ { t = \$2 "_" \$3; size = substr(t, 1, length(t) - 1)}
	lead
				/<PgfLeading/ { t = \$2 "_" \$3; lead = substr(t, 1, length(t) - 1)}
	InSeRt	
				/<PgfUseNextTag/	{ if (\$2 == "Yes") { nexttag = 1 } else { nexttag = 0 }}
	nexttag
				/<PgfNextTag/ { if (nexttag) {s=index(\$0, "\`")+1; l=length(\$0)-s-1; nexttag = substr(\$0, s, l)} else { nexttag = "none" }}
EOF
	#cat $tmpawk1

sed -e '1,/^<PgfCatalog/ d' -e '/^>/,$ d' < $miffile |
	awk -f $tmpawk1 > $tmpawk2

awkrep $tmpawk2
@EOF

chmod 775 parasumm

echo x - awkgen
cat >awkgen <<'@EOF'
#!/bin/ksh
#	ex:se ts=3 sw=3:
usage="usage: "
#
#	Generate an awk file which will:
#		create a file which is of the form 'aentry' 'elname' 'elvalue' ['elname' 'elvalue' ....]
#	this essentially can be used to create a series of arrays:
#		elname1[aentry] = elvalue1; elname2[aentry] = elvalue2.... or
#		array[aentry, elname1] = elvalue1; array[aentry, elname2] = elvalue2, .....
#
#	This reads stdin to create the awk file:
#		line  1 defines a pattern which is the start of 'aentry'.
#		line 2,3 defines a pattern and an action to define 'aentry' -- it must occur prior to any elements
#				aentryname '/<pattern>/ { aentryname = value }
#	
#	The rest of the lines define patterns and actions which define 'elname' and 'elvalue' as:
#			'elname /<pattern>/ { elname = value }'
#
#		if $1 is 'InSeRt', then the rest of the line s a string which will be inserted into the awk
#		script prior to any automatically generated statements.

#	This emits the awk file to stdout...

#	Set up the PATH variable
PATH=/bin:/usr/bin:
SHELL=/bin/ksh

# Cannot call getopt!

if [ $# -ne 0 ]
then
	echo "$usage" >&2
	exit 1
fi

	awk	'	
				NR == 1	{	startptn = $0 ; next }
				NR == 2	{	aentryname = $0; next }
				NR == 3	{	aentryawk = $0; next 
								printf "BEGIN  {  %s = \"\"; }\n", aentryname
							}

				/InSeRt/	{	getline
								print $0; next
							}

							{	numparts++
								partname[numparts] = $0
								getline
								partawk[numparts] = $0
								#printf "partname = @%s@, awk = @%s@\n", partname[numparts], partawk[numparts]
							}

				END		{	
								
								# Found start pattern -- emit previous information if any
								printf "/%s/	{ if (%s !~ /^$/)\n", startptn, aentryname
								printf "		{\n"
								printf "			printf \"%%s \", %s\n", aentryname
								for (p = 1; p <= numparts; p++)
								{
									printf "			printf \"%s %%s \", %s\n", partname[p], partname[p]
								}
								printf "			printf \"\\n\"\n		}\n"

								printf "	}\n"

								printf "%s\n", aentryawk

								for (p = 1; p <= numparts; p++)
								{
									printf "%s\n", partawk[p]
								}

								printf "\n"
								printf "END	{\n"
								printf "	{ if (%s !~ /^$/)\n", aentryname
								printf "		{\n"
								printf "			printf \"%%s \", %s\n", aentryname
								for (p = 1; p <= numparts; p++)
								{
									printf "			printf \"%s %%s \", %s\n", partname[p], partname[p]
								}
								printf "			printf \"\\n\"\n		}\n"
								printf "	}\n}"

							}
			'
exit
@EOF

chmod 775 awkgen

echo x - awkrep
cat >awkrep <<'@EOF'
#!/bin/ksh
#	ex:se ts=3 sw=3:
usage="usage: awkrep <file>"
#
#	<file> is of the form:
#	<tag> <label1> <value1> [<label2> <value2> ..]
#	Converts to a table of output...
#	
#

#	Set up the PATH variable
PATH=/bin:/usr/bin:
SHELL=/bin/ksh

set -- `getopt "" "$@"`
if [ $? -ne 0 ]
then
	echo "$usage" >&2
	exit 1
fi

while [ $# -gt 0 ]
do
	case $1 in
		--)	shift; break;;
		-*)	echo "$usage" >&2; exit 1;;
	esac
done

if [ $# -ne 1 ]
then
	echo "$usage" >&2
	exit 1
fi

infile=$1

(
	#	Pass 1: get list of all labels and their max width...
	#	This emits a line which is of the form:
	#	<label> <width> [<label> <width> ....
	awk '	BEGIN	{	numlabels = 1 }

			{	for (label = 2; label <= NF; label = label + 2)
				{
					if (labels[$label] == "" )
					{
						labels[$label] = $label
						# New label!
						labellist[numlabels] = $label
						# printf "label %s is %d\n", $label, numlabels
						numlabels++
						width = length($label)
						widths[$label] = width
					}
					
					value = label + 1
					width = length($value)
					if (widths[$label] < width)
						widths[$label] = width
				}

				if (length($1) > tagwidth)
					tagwidth = length($1)
			}

		END	{	printf "%d ", tagwidth
					for (l = 1; l < numlabels; l++)
					{
						printf "%s %d ", labellist[l], widths[labellist[l]]
					}
					printf "\n"

				}' < $infile
	cat $infile
) |
	awk '
		NR == 1	{	# list of labels & widths, in order
						tagwidth = $1
						numlabels = 0
						for (l = 2; l <= NF; l = l + 2)
						{
							numlabels = numlabels + 1
							label2order[$l] = numlabels
							labellist[numlabels] = $l
							m = l + 1
							width[numlabels] = $m
						}

						# Print heading
						printf "%-*s  ", tagwidth, ""
						for (l = 1; l <= numlabels; l++)
						{
							printf "%-*s  ", width[l], labellist[l]
						}
						printf "\n"
						next
					}

					{	# Input line!  
						leader = $1

						for (a = 1; a <= numlabels; a++)
							args[a] = ""
						
						for (b = 2; b <= NF; b++)
						{
							valn = b + 1
							value = $valn
							lnum = label2order[$b]	# Which label?
							args[lnum] = value
						}

						# Print out line
						printf "%-*s  ", tagwidth, $1
						for (l = 1; l <= numlabels; l++)
						{
							printf "%-*s  ", width[l], args[l]
						}
						printf "\n"
					}'

@EOF

chmod 775 awkrep

exit 0
Newsgroups: comp.text.frame