[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

[G-I] Font stripping/language based - current status



Hello all,

This is an update to the message I sent a while back about languge
based font stripping.

I have made a few improvemnts to it:
- placing generated files under a directory (whose existance is tested)
- test if the required font is installed
- generate stripped fonts with a custom suffix (i.e. do not generate
font_bg.ttf and font_ru.ttf if there could be font_cyrillic.ttf which
is the same as the first two)

The script will need a little more work, but is fairly functional, ATM.


The most important thing (IMO) is that I have implemented "custom font
suffixes" for the font stripping.
The idea is that if one will strip a cyrillic specfic range, and
specified on the (newly added) second column of lang2range the
"cyrillic" suffix, that suffix will be appended to the original font
name when a langauge specific range will be stripped, thus generating
a font_cyrillic.ttf file instead of many identical files, but which
have names like font_bg.ttf, font_ru.ttf, font_mk.ttf.


As examples are better than words, here goes:

for this snippet of lang2range:

lang=:ar:fa:,arabic,Arabic,u0600:u06FF
lang=:fa:,,Old Persian,u103A0:u103DF
lang=:ar:fa:,arabic,Arabic Supplement,u0750:u077F

and this of lang2font:
ar:ttf-arabeyes:
fa:ttf-farsiweb:


when the chosen language code "fa" will be chosen,

out of all ttf files from the package ttf-farsiweb (which should be
installed) there will be produced two stripped font files:

e.g.: for nazli.ttf (from ttf-farsiweb) + fa will result:
nazli_arabic.ttf - contains glyphs within ranges u0600:u06FF, u0750:u077F
and
nazli_fa.ttf - contains glyphs within range u103A0:u103DF

So one will need both of these to display farsi (I assumed that farsi
needs u103A0:u103DF for proper display and that the D-I translation
uses glyphs in this range), but only nazli_arabic.ttf to display
arabic specific text.

Note: When arabic (ar) stripped fonts will be generated, the
nazli_arabic.ttf font will not be generated again, but only ar
specific sptripped ones (if any - not the case in the example).




If anybody is interested in this work please say so and I will send
the needed file personaly (as the ML does not accept attachments)

------------------------------------
Here is the current form of the font-stripper ;-) script:

#!/bin/sh -x

LANG2RANGE=lang2range
LANG2FONT=lang2font
LANGCODE=$1
[ -z "$SRCTTFDIR" ] && SRCTTFDIR='.'
[ -z "$DSTTTFDIR" ] && DSTTTFDIR='strippedfonts'


help () {
	echo " $0 <lang_code>"
	echo '    Strips the font(s) found in the third column of ./lang2font'
	echo '    of the line containing the given laguage code, so that'
	echo '    it(they) will contain only the ranges specified on'
	echo "    <lang_code>'s line in lang2range"
}

# strips font $1 for language $2
stripfont4lang () {
	# extract the lines which contain only the ranges for the current laguage
	# if no suffix is specified, use the langauge code as a suffix
	LINES=`cat $LANG2RANGE | grep -E "^lang=[^,]*:$2:[^,]*," | sed "s/:,,/:,$2,/g"`

	# make a sorted list of all the suffixes (no duplicates)
	SUFFIXES=`echo $LINES | sed "s/lang=/\nlang=/g" | cut -f2 -d ',' | sort -u`
	
	for SUFFIX in $SUFFIXES
	do
		STRIPPED_F_NAME=`echo "$1" | sed "s:^\(.*\)\.ttf$:\1_$SUFFIX.ttf:" |
sed 's:^.*/\(.*\)$:\1:g'`
		[ ! -d "$DSTTTFDIR" ] && mkdir -p "$DSTTTFDIR" && echo "Created
missing destination directory: $DSTTTFDIR"
		STRIPPED_F_NAME="$DSTTTFDIR/$STRIPPED_F_NAME"

		if [ -f "$STRIPPED_F_NAME" ]
		then
			echo "$STRIPPED_F_NAME was already created. Skipping..."
		else
			echo "Creating $STRIPPED_F_NAME for $2 ... starting from $1"
			RANGES=`echo "$LINES" | sed "s/lang=/\nlang=/g" | grep -E
":,$SUFFIX," | cut -f4 -d ','`
			./strip_glyphs.pe --include "$1" "$STRIPPED_F_NAME" $RANGES
		fi
	done
}

[ -z "$LANGCODE" ] && help "$0" && exit 1

## Check if the needed font package is installed
PACK=`grep -e "^$LANGCODE:.*" $LANG2FONT | cut -f2 -d ':'`
#
#INSTVER=`LANG=C apt-cache policy "$PACK" | sed s/.*Installed:\ \(.*\)/\1`
#[ "$INSTVER" = '(none)' ] && echo "$PACK font package is not
installed.Quitting." && exit 1


FONTFILES=`grep -e "^$LANGCODE:.*" $LANG2FONT | cut -f3 -d ':'`

# if no font is explicitly specified, then all the fonts of the
package should be stripped
if [ -z "$FONTFILES" ]
then
	echo "No special font file specified in $LANG2FONT. Will try to strip
all fonts from the font package."
	[ -z $PACK ] && echo "No default package was defined for $LANGCODE!
Quitting!" && exit 1

	#TODO: test if $PACK is installed
	ISINSTALLED=`dpkg --get-selections | tr -s '\t' ' ' | grep "^$PACK install$"`
	[ ! -z "$ISINSTALLED" ] && FONTFILES=`dpkg -L $PACK | grep -e "\.ttf$"`
fi

[ -z "$FONTFILES" ] && echo 'No font files nor font package was
specified. There is no input font. Quitting!' && exit 1

for FONT in $FONTFILES
do
	stripfont4lang $FONT $LANGCODE
done

------------------------------------


--
Regards,
EddyP
=============================================
"Imagination is more important than knowledge" A.Einstein



Reply to: