=21 =&harr#leftrightarrow; 24 25 =201by 1 @* Introduction. This program reads a ĠF file and packs it into a ṖK file. ṖK files are significantly smaller than ĠF files, and they are much easier to interpret. This program is meant to be the bridge between <#367#>#tex2html_accent_inline513#<#367#><#368#>#tex2html_accent_inline514#<#368#> and ḊVI drivers that read ṖK files. Here are some statistics comparing typical input and output file sizes:

#math29#

#tex2html_wrap_indisplay516##tex2html_wrap_indisplay517##;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp; ;SPMamp; #;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp; ;SPMamp; ;SPMamp; #;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;#tex2html_wrap_indisplay518#Font ;SPMamp; Resolution;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp; ;SPMamp; ˙GFsize ;SPMamp; ˙PKsize ;SPMamp; Reductionfactor#tex2html_wrap_indisplay519#


cmr10 ;SPMamp; 300 ;SPMamp; 13200 ;SPMamp; 5484 ;SPMamp; 42%#tex2html_wrap_indisplay520#cmr10 ;SPMamp; 360 ;SPMamp; 15342 ;SPMamp; 6496 ;SPMamp; 42%#tex2html_wrap_indisplay521#cmr10 ;SPMamp; 432 ;SPMamp; 18120 ;SPMamp; 7808 ;SPMamp; 43%#tex2html_wrap_indisplay522#cmr10 ;SPMamp; 511 ;SPMamp; 21020 ;SPMamp; 9440 ;SPMamp; 45%#tex2html_wrap_indisplay523#cmr10 ;SPMamp; 622 ;SPMamp; 24880 ;SPMamp; 11492 ;SPMamp; 46%#tex2html_wrap_indisplay524#cmr10 ;SPMamp; 746 ;SPMamp; 29464 ;SPMamp; 13912 ;SPMamp; 47%#tex2html_wrap_indisplay525#cminch ;SPMamp; 300 ;SPMamp; 48764 ;SPMamp; 22076 ;SPMamp; 45%#tex2html_wrap_indisplay526#Itishopedthatthesimplicityandsmallsizeofthe˙PKfileswillmakethemwidelyaccepted.The˙PKformatwasdesignedandimplementedbyTomasRokickiduring@Rokicki, TomasGerhardPaul@ ;SPMgt; thesummerof1985.Thisprogramborrowsafewroutinesfrom˙GFtoPXLbyArthurSamuel.@Samuel, ArthurLee@ ;SPMgt; The| banner| stringdefinedhereshouldbechangedwhenever˙GFtoPKgetsmodified.The| preamblecomment| macro(neartheendoftheprogram)shouldbechangedtoo.@dbanner=='ThisisGFtoPK, Version2.3'printedwhentheprogramstarts@Someofthediagnosticinformationisprintedusing| dprintln|.Whendebugging, itshouldbesetthesameas| println|, definedlater.@debugging@ ;SPMgt; @ddprintln(#)==@ThisprogramiswritteninstandardPascal, exceptwhereitisnecessarytouseextensions;forexample, oneextensionistouseadefault| case| asin˙TANGLEWEAVE, etc.Allplaceswherenonstandardconstructionsareusedshouldbelistedintheindexunder``systemdependencies.''@!@systemdependencies@ ;SPMgt; @dothercases==others:defaultforcasesnotlistedexplicitly@dendcases==@+endfollowsthedefaultcaseinanextended| case| statement@fothercases==else@fendcases==end@Thebinaryinputcomesfrom| gffile|, andtheoutputfontiswrittenon| pkfile|.AlltextoutputiswrittenonPascal'sstandard| output| file.Theterm| print| isusedinsteadof| write| whenthisprogramwriteson| output|, sothatallsuchoutputcouldeasilyberedirectedifdesired.@dprint(#)==write(#)@dprintln(#)==writeln(#)@pprogramGFtoPK(@!gffile,@!pkfile,@!output);label@ ;SPMlt; Labelsintheouterblock@ ;SPMgt; @/const@ ;SPMlt; Constantsintheouterblock@ ;SPMgt; @/type@ ;SPMlt; Typesintheouterblock@ ;SPMgt; @/var@ ;SPMlt; Globalsintheouterblock@ ;SPMgt; @/procedureinitialize;thisproceduregetsthingsstartedproperlyvari:integer;loopindexforinitializationsbeginprintln(banner);@/@ ;SPMlt; Setinitialvalues@ ;SPMgt; @/end;@Iftheprogramhastostopprematurely, itgoestothe`| finalend|'.@dfinalend=9999labelfortheendofitall@ ;SPMlt; Labels...@ ;SPMgt; =finalend;@Thefollowingparameterscanbechangedatcompiletimetoextendorreduce˙GFtoPKscapacity.Thevaluesgivenhereshouldbequiteadequateformostuses.Assuminganaverageofaboutthreestrokesperrasterline, therearesixrun-countsperline, andtherefore| maxrow| willbesufficientforacharacter2600pixelshigh.@ ;SPMlt; Constants...@ ;SPMgt; =@!linelength=79;bracketedlinesofoutputwillbeatmostthislong@!maxrow=16000;largestindexinthemain| row| array@Herearesomemacrosforcommonprogrammingidioms.@dincr(#)==#:=#+1increaseavariablebyunity@ddecr(#)==#:=#-1decreaseavariablebyunity@Ifthe˙GFfileisbadlymalformed, thewholeprocessmustbeabortedGFtoPKwillgiveup, afterissuinganerrormessageaboutthesymptomsthatwerenoticed.Sucherrorsmightbediscoveredinsideofsubroutinesinsideofsubroutines, soaprocedurecalled| jumpout| hasbeenintroduced.Thisprocedure, whichsimplytransferscontroltothelabel| finalend| attheendoftheprogram, containstheonlynon-local| goto| statementin˙GFtoPK.@systemdependencies@ ;SPMgt; @dabort(#)==beginprint('',#);jumpout;end@dbadgf (#)==abort('BadGFfile:',#,'!')@.BadGFfile@ ;SPMgt; @pprocedurejumpout;begingotofinalend;@*Thecharacterset.Likeallprogramswrittenwiththe˙WEBsystemGFtoPKcanbeusedwithanycharacterset.ButitusesASCIIcodeinternally, becausetheprogrammingforportableinput-outputiseasierwhenafixedinternalcodeisused.Thenextfewsectionsof˙GFtoPKhavethereforebeencopiedfromtheanalogousonesinthe˙WEBsystemroutines.Theyhavebeenconsiderablysimplified, since˙GFtoPKneednotdealwiththecontroversialASCIIcodeslessthan@'40orgreaterthan@'176.Ifsuchcodesappearinthe˙GFfile, theywillbeprintedasquestionmarks.@ ;SPMlt; Types...@ ;SPMgt; =@!ASCIIcode=;SPMquot;;SPMquot;..;SPMquot;~;SPMquot;;asubrangeoftheintegers@TheoriginalPascal compilerwasdesignedinthelate60s, whensix-bitcharactersetswerecommon, soitdidnotmakeprovisionforlowercaseletters.Nowadays, ofcourse, weneedtodealwithbothupperandlowercasealphabetsinaconvenientway, especiallyinaprogramlike˙GFtoPK.SoweshallassumethatthePascal systembeingusedfor˙GFtoPKhasacharactersetcontainingatleastthestandardvisiblecharactersofASCIIcode(|;SPMquot;!;SPMquot;| through|;SPMquot;~;SPMquot;|).SomePascal compilersusetheoriginalname| char| forthedatatypeassociatedwiththecharactersintextfiles, whileotherPascalsconsider| char| tobea64-elementsubrangeofalargerdatatypethathassomeothername.Inordertoaccommodatethisdifference, weshallusethename| textchar| tostandforthedatatypeofthecharactersintheoutputfile.Weshallalsoassumethat| textchar| consistsoftheelements| chr(firsttextchar)| through| chr(lasttextchar)|, inclusive.Thefollowingdefinitionsshouldbeadjustedifnecessary.@systemdependencies@ ;SPMgt; @dtextchar==charthedatatypeofcharactersintextfiles@dfirsttextchar=0ordinalnumberofthesmallestelementof| textchar|@dlasttextchar=127ordinalnumberofthelargestelementof| textchar|@ ;SPMlt; Types...@ ;SPMgt; =@!textfile=packedfileoftextchar;@The˙GFtoPKprocessorconvertsbetweenASCIIcodeandtheuser'sexternalcharactersetbymeansofarrays| xord| and| xchr| thatareanalogoustoPascal's| ord| and| chr| functions.@ ;SPMlt; Globals...@ ;SPMgt; =@!xord:array[textchar]ofASCIIcode;specifiesconversionofinputcharacters@!xchr:array[0..255]oftextchar;specifiesconversionofoutputcharacters@UnderourassumptionthatthevisiblecharactersofstandardASCIIareallpresent, thefollowingassignmentstatementsinitializethe| xchr| arrayproperly, withoutneedinganysystem-dependentchanges.@ ;SPMlt; Setinit...@ ;SPMgt; =fori:=0to@'37doxchr[i]:='?';xchr[@'40]:='';xchr[@'41]:='!';xchr[@'42]:=';SPMquot;';xchr[@'43]:='#';xchr[@'44]:='$';xchr[@'45]:='27xchr[@'46]:=' ;SPMamp; ';xchr[@'47]:='''';@/xchr[@'50]:='(';xchr[@'51]:=')';xchr[@'52]:='*';xchr[@'53]:='+';xchr[@'54]:=',';xchr[@'55]:='-';xchr[@'56]:='.';xchr[@'57]:='/';@/xchr[@'60]:='0';xchr[@'61]:='1';xchr[@'62]:='2';xchr[@'63]:='3';xchr[@'64]:='4';xchr[@'65]:='5';xchr[@'66]:='6';xchr[@'67]:='7';@/xchr[@'70]:='8';xchr[@'71]:='9';xchr[@'72]:=':';xchr[@'73]:=';';xchr[@'74]:=' ;SPMlt; ';xchr[@'75]:='=';xchr[@'76]:=' ;SPMgt; ';xchr[@'77]:='?';@/xchr[@'100]:='@@';xchr[@'101]:='A';xchr[@'102]:='B';xchr[@'103]:='C';xchr[@'104]:='D';xchr[@'105]:='E';xchr[@'106]:='F';xchr[@'107]:='G';@/xchr[@'110]:='H';xchr[@'111]:='I';xchr[@'112]:='J';xchr[@'113]:='K';xchr[@'114]:='L';xchr[@'115]:='M';xchr[@'116]:='N';xchr[@'117]:='O';@/xchr[@'120]:='P';xchr[@'121]:='Q';xchr[@'122]:='R';xchr[@'123]:='S';xchr[@'124]:='T';xchr[@'125]:='U';xchr[@'126]:='V';xchr[@'127]:='W';@/xchr[@'130]:='X';xchr[@'131]:='Y';xchr[@'132]:='Z';xchr[@'133]:='[';xchr[@'134]:=';́xchr[@'135]:=']';xchr[@'136]:='';xchr[@'137]:='';@/xchr[@'140]:='`';xchr[@'141]:='a';xchr[@'142]:='b';xchr[@'143]:='c';xchr[@'144]:='d';xchr[@'145]:='e';xchr[@'146]:='f';xchr[@'147]:='g';@/xchr[@'150]:='h';xchr[@'151]:='i';xchr[@'152]:='j';xchr[@'153]:='k';xchr[@'154]:='l';xchr[@'155]:='m';xchr[@'156]:='n';xchr[@'157]:='o';@/xchr[@'160]:='p';xchr[@'161]:='q';xchr[@'162]:='r';xchr[@'163]:='s';xchr[@'164]:='t';xchr[@'165]:='u';xchr[@'166]:='v';xchr[@'167]:='w';@/xchr[@'170]:='x';xchr[@'171]:='y';xchr[@'172]:='z';xchr[@'173]:='';xchr[@'174]:='|';xchr[@'175]:=';xchr[@'176]:='~';fori:=@'177to255doxchr[i]:='?';@Thefollowingsystem-independentcodemakesthe| xord| arraycontainasuitableinversetotheinformationin| xchr|.@ ;SPMlt; Setinit...@ ;SPMgt; =fori:=firsttextchartolasttextchardoxord[chr(i)]:=@'40;fori:=;SPMquot;;SPMquot;to;SPMquot;~;SPMquot;doxord[xchr[i]]:=i;@*Genericfontfileformat.Themostimportantoutputproducedbyatypicalrunof#tex2html_wrap_indisplay529#META#tex2html_wrap_indisplay530#FONT isthe``genericfont''GF)filethatspecifiesthebitpatternsofthecharactersthathavebeendrawn.Theterm#tex2html_wrap_indisplay531#indicatesthatthisfileformatdoesn'tmatchtheconventionsofanyname-brandmanufacturer;butitiseasytoconvert˙GFfilestothespecialformatrequiredbyalmostalldigitalphototypesettingequipment.There'sastronganalogybetweenthe˙DVIfileswrittenbyTEX andthe˙GFfileswrittenby#tex2html_wrap_indisplay532#META#tex2html_wrap_indisplay533#FONT;and, infact, thefileformatshavealotincommon.A˙GFfileisastreamof8-bitbytesthatmayberegardedasaseriesofcommandsinamachine-likelanguage.Thefirstbyteofeachcommandistheoperationcode, andthiscodeisfollowedbyzeroormorebytesthatprovideparameterstothecommand.Theparametersthemselvesmayconsistofseveralconsecutivebytes;forexample, the`| boc|'(beginningofcharacter)commandhassixparameters, eachofwhichisfourbyteslong.Parametersareusuallyregardedasnonnegativeintegers;butfour-byte-longparameterscanbeeitherpositiveornegative, hencetheyrangeinvaluefrom$-231$to$231-1$.Asin˙TFMfiles, numbersthatoccupymorethanonebytepositionappearinBigEndianorder, andnegativenumbersappearintwo'scomplementnotation.A˙GFfileconsistsofa``preamble,''followedbyasequenceofoneormore``characters,''followedbya``postamble.''Thepreambleissimplya| pre| command, withitsparametersthatintroducethefile;thismustcomefirst.Each``character''consistsofa| boc| command, followedbyanynumberofothercommandsthatspecify``black''pixels, followedbyan| eoc| command.Thecharactersappearintheorderthat#tex2html_wrap_indisplay534#META#tex2html_wrap_indisplay535#FONTI>generatedthem.Ifweignoreno-opcommands(whichareallowedbetweenanytwocommandsinthefile), each| eoc| commandisimmediatelyfollowedbya| boc| command, orbya| post| command;inthelattercase, therearenomorecharactersinthefile, andtheremainingbytesformthepostamble.Furtherdetailsaboutthepostamblewillbeexplainedlater.Someparametersin˙GFcommandsare``pointers.''Thesearefour-bytequantitiesthatgivethelocationnumberofsomeotherbyteinthefile;thefirstfilebyteisnumber~0, thencomesnumber~1, andsoon.@The˙GFformatisintendedtobebothcompactandeasilyinterpretedbyamachine.Compactnessisachievedbymakingmostoftheinformationrelativeinsteadofabsolute.Whena˙GF-readingprogramreadsthecommandsforacharacter, itkeepstrackoftwoquantities:(a)~thecurrentcolumnnumber,~| m|;and (b)~thecurrentrownumber,~| n|.Theseare32-bitsignedintegers, althoughmostactualfontformatsproducedfrom˙GFfileswillneedtocurtailthisvastrangebecauseofpracticallimitations.(#tex2html_wrap_indisplay536#META#tex2html_wrap_indisplay537#FONT outputwillneverallow$|m|$or$|n|$togetextremelylarge, butthe˙GFformattriestobemoregeneral.)Howdo˙GFsrowandcolumnnumberscorrespondtotheconventionsofTEX and#tex2html_wrap_indisplay539#META#tex2html_wrap_indisplay540#FONT?Well, the``referencepoint''ofacharacter, inTEX'sview, isconsideredtobeatthelowerleftcornerofthepixelinrow~0andcolumn~0.Thispointistheintersectionofthebaselinewiththeleftedgeofthetype;itcorrespondstolocation$(0, 0)$in#tex2html_wrap_indisplay541#META#tex2html_wrap_indisplay542#FONT programs.Thusthepixelin˙GFrow~0andcolumn~0is#tex2html_wrap_indisplay543#META#tex2html_wrap_indisplay545#FONTsunitsquare, comprisingtheregionoftheplanewhosecoordinatesbothliebetween0and~1.Thepixelin˙GFrow~| n| andcolumn~| m| consistsofthepointswhose#tex2html_wrap_indisplay546#META#tex2html_wrap_indisplay547#FONTI>coordinates|(x, y)| satisfy| m ;SPMlt; =x ;SPMlt; =m+1| and| n ;SPMlt; =y ;SPMlt; =n+1|.Negativevaluesof| m| and~| x| correspondtocolumnsofpixels#tex2html_wrap_indisplay548#ofthereferencepoint;negativevaluesof| n| and~| y| correspondtorowsofpixels#tex2html_wrap_indisplay549#thebaseline.Besides| m| and| n|, there'salsoathirdaspectofthecurrentstate, namelythe@!| paintswitch|, whichisalwayseither
blackor
white.Each
paintcommandadvances| m| byaspecifiedamount~| d|, andblackenstheinterveningpixelsif| paintswitch=black|;thenthe| paintswitch| changestotheoppositestateGFscommandsaredesignedsothat| m| willneverdecreasewithinarow, and| n| willneverincreasewithinacharacter;hencethereisnowaytowhitenapixelthathasbeenblackened.@Hereisalistofallthecommandsthatmayappearina˙GFfile.Eachcommandisspecifiedbyitssymbolicname(e.g.,| boc|), itsopcodebyte(e.g., 67), anditsparameters(ifany).Theparametersarefollowedbyabracketednumbertellinghowmanybytestheyoccupy;forexample,`| d[2]|'meansthatparameter| d| istwobyteslong.#tex2html_wrap_indisplay551##tex2html_wrap_indisplay552#3em | paint0| 0.Thisisa
paintcommandwith| d=0|;itdoesnothingbutchangethe| paintswitch| from
blackto
whiteorvice~versa.#tex2html_wrap_indisplay553##tex2html_wrap_indisplay554#3em 
paint#tex2html_wrap_indisplay555#1through
paint#tex2html_wrap_indisplay556#63(opcodes1to63).Theseare
paintcommandswith| d=1| to~63, definedasfollows:If| paintswitch=black|, blacken| d|~pixelsofthecurrentrow~| n|, incolumns| m| through| m+d-1| inclusive.Then, inanycase, complementthe| paintswitch| andadvance| m| by~| d|.#tex2html_wrap_indisplay557##tex2html_wrap_indisplay558#3em | paint1| 64| d[1]|.Thisisa
paintcommandwithaspecifiedvalueof~| d|;#tex2html_wrap_indisplay559#META#tex2html_wrap_indisplay560#FONT usesittopaintwhen| 64 ;SPMlt; =d ;SPMlt; 256|.#tex2html_wrap_indisplay561##tex2html_wrap_indisplay562#3em |@!paint2| 65| d[2]|.Sameas| paint1|, but| d|~canbeashighas~65535.#tex2html_wrap_indisplay563##tex2html_wrap_indisplay564#3em |@!paint3| 66| d[3]|.Sameas| paint1|, but| d|~canbeashighas$224-1$.#tex2html_wrap_indisplay565#META#tex2html_wrap_indisplay566#FONT neverneedsthiscommand, anditishardtoimagineanybodymakingpracticaluseofit;surelyamorecompactencodingwillbedesirablewhencharacterscanbethislarge.Butthecommandisthere, anyway, justincase.#tex2html_wrap_indisplay567##tex2html_wrap_indisplay568#3em | boc| 67| c[4]|| p[4]|| minm[4]|| maxm[4]|| minn[4]|| maxn[4]|.Beginningofacharacter:Here| c| isthecharactercode, and| p| pointstothepreviouscharacterbeginning(ifany)forcharactershavingthiscodenumbermodulo256.(Thepointer| p| is|-1| iftherewasnopriorcharacterwithanequivalentcode.)Thevaluesofregisters| m| and| n| definedbytheinstructionsthatfollowforthischaractermustsatisfy| minm ;SPMlt; =m ;SPMlt; =maxm| and| minn ;SPMlt; =n ;SPMlt; =maxn|.(Thevaluesof| maxm| and| minn| neednotbethetightestboundspossible.)Whena˙GF-readingprogramseesa| boc|, itcanuse| minm|,| maxm|,| minn|, and| maxn| toinitializetheboundsofanarray.Thenitsets| m:=minm|,| n:=maxn|, and| paintswitch:=white|.#tex2html_wrap_indisplay569##tex2html_wrap_indisplay570#3em | boc1| 68| c[1]||@!delm[1]|| maxm[1]||@!deln[1]|| maxn[1]|.Sameas| boc|, but| p| isassumedtobe~$-1$;also| delm=maxm-minm| and| deln=maxn-minn| aregiveninsteadof| minm| and| minn|.Theone-byteparametersmustbebetween0and255, inclusive. (Thisabbreviated| boc| saves19~bytespercharacter, incommoncases.)#tex2html_wrap_indisplay571##tex2html_wrap_indisplay572#3em | eoc| 69.Endofcharacter:Allpixelsblackenedsofarconstitutethepatternforthischaracter.Inparticular, acompletelyblankcharactermighthave| eoc| immediatelyfollowing| boc|.#tex2html_wrap_indisplay573##tex2html_wrap_indisplay574#3em | skip0| 70.Decrease| n| by1andset| m:=minm|,| paintswitch:=white|. (Thisfinishesonerowandbeginsanother, readytowhitentheleftmostpixelinthenewrow.)#tex2html_wrap_indisplay575##tex2html_wrap_indisplay576#3em | skip1| 71| d[1]|.Decrease| n| by| d+1|, set| m:=minm|, andset| paintswitch:=white|.Thisisawaytoproduce| d| all-whiterows.#tex2html_wrap_indisplay577##tex2html_wrap_indisplay578#3em |@!skip2| 72| d[2]|.Sameas| skip1|, but| d| canbeaslargeas65535.#tex2html_wrap_indisplay579##tex2html_wrap_indisplay580#3em |@!skip3| 73| d[3]|.Sameas| skip1|, but| d| canbeaslargeas$224-1$.#tex2html_wrap_indisplay581#META#tex2html_wrap_indisplay582#FONT obviouslyneverneedsthiscommand.#tex2html_wrap_indisplay583##tex2html_wrap_indisplay584#3em | newrow0| 74.Decrease| n| by1andset| m:=minm|,| paintswitch:=black|. (Thisfinishesonerowandbeginsanother, readyto#tex2html_wrap_indisplay585#theleftmostpixelinthenewrow.)#tex2html_wrap_indisplay586##tex2html_wrap_indisplay587#3em |@!newrow1| through|@!newrow164|(opcodes75to238).Sameas| newrow0|, butwith| m:=minm+1| through| minm+164|, respectively.#tex2html_wrap_indisplay588##tex2html_wrap_indisplay589#3em | xxx1| 239| k[1]|| x[k]|.Thiscommandisundefinedingeneral;itfunctionsasa$(k+2)$-byte| noop| unlessspecial˙GF-readingprogramsarebeingused.#tex2html_wrap_indisplay590#META#tex2html_wrap_indisplay591#FONT generates
xxxcommandswhenencounteringaspecialstring;thisoccursinthe˙GFfileonlybetweencharacters, afterthepreamble, andbeforethepostamble.However,
xxxcommandsmightappearwithincharacters, in˙GFfilesgeneratedbyotherprocessors.Itisrecommendedthat| x| beastringhavingtheformofakeywordfollowedbypossibleparametersrelevanttothatkeyword.#tex2html_wrap_indisplay592##tex2html_wrap_indisplay593#3em |@!xxx2| 240| k[2]|| x[k]|.Like| xxx1|, but| 0 ;SPMlt; =k ;SPMlt; 65536|.#tex2html_wrap_indisplay594##tex2html_wrap_indisplay595#3em | xxx3| 241| k[3]|| x[k]|.Like| xxx1|, but| 0 ;SPMlt; =k ;SPMlt; @t$224$@ ;SPMgt; |.#tex2html_wrap_indisplay596#META#tex2html_wrap_indisplay597#FONT usesthiswhensendingaspecialstringwhoselengthexceeds~255.#tex2html_wrap_indisplay598##tex2html_wrap_indisplay599#3em |@!xxx4| 242| k[4]|| x[k]|.Like| xxx1|, but| k| canberidiculouslylarge;| k| mustn'tbenegative.#tex2html_wrap_indisplay600##tex2html_wrap_indisplay601#3em | yyy| 243| y[4]|.Thiscommandisundefinedingeneral;itfunctionsasa5-byte| noop| unlessspecial˙GF-readingprogramsarebeingused.#tex2html_wrap_indisplay602#META#tex2html_wrap_indisplay603#FONT puts| scaled| numbersinto| yyy|'s, asaresultofnumspecialcommands;theintentistoprovidenumericparametersto
xxxcommandsthatimmediatelyprecede.#tex2html_wrap_indisplay604##tex2html_wrap_indisplay605#3em | noop| 244.Nooperation, donothing.Anynumberof| noop|'smayoccurbetween˙GFcommands, buta| noop| cannotbeinsertedbetweenacommandanditsparametersorbetweentwoparameters.#tex2html_wrap_indisplay606##tex2html_wrap_indisplay607#3em | charloc| 245| c[1]|| dx[4]|| dy[4]|| w[4]|| p[4]|.Thiscommandwillappearonlyinthepostamble, whichwillbeexplainedshortly.#tex2html_wrap_indisplay608##tex2html_wrap_indisplay609#3em |@!charloc0| 246| c[1]||@!dm[1]|| w[4]|| p[4]|.Sameas| charloc|, exceptthat| dy| isassumedtobezero, andthevalueof~| dx| istakentobe| 65536*dm|, where| 0 ;SPMlt; =dm ;SPMlt; 256|.#tex2html_wrap_indisplay610##tex2html_wrap_indisplay611#3em | pre| 247| i[1]|| k[1]|| x[k]|.Beginningofthepreamble;thismustcomeattheverybeginningofthefile.Parameter| i| isanidentifyingnumberfor˙GFformat, currently131.Theotherinformationismerelycommentary;itisnotgivenspecialinterpretationlike
xxxcommandsare.(Notethat
xxxcommandsmayimmediatelyfollowthepreamble, beforethefirst| boc|.)#tex2html_wrap_indisplay612##tex2html_wrap_indisplay613#3em | post| 248.Beginningofthepostamble, seebelow.#tex2html_wrap_indisplay614##tex2html_wrap_indisplay615#3em | postpost| 249.Endingofthepostamble, seebelow.#tex2html_wrap_indisplay616#Commands250--255areundefinedatthepresenttime.@dgfidbyte=131identifiesthekindof˙GFfilesdescribedhere@Herearetheopcodesthat˙GFtoPKactuallyrefersto.@dpaint0=0beginningofthe
paintcommands@dpaint1=64moverightagivennumberofcolumns, thenblack$#tex2html_wrap_indisplay617#$white@dboc=67beginningofacharacter@dboc1=68abbreviated| boc|@deoc=69endofacharacter@dskip0=70skipnoblankrows@dskip1=71skipoverblankrows@dnewrow0=74movedownonerowandthenright@dmaxnewrow=238movedownonerowandthenright@dxxx1=239forspecialstrings@dyyy=243fornumspecialnumbers@dnoop=244nooperation@dcharloc=245characterlocatorsinthepostamble@dcharloc0=246characterlocatorsinthepostamble@dpre=247preamble@dpost=248postamblebeginning@dpostpost=249postambleending@dundefinedcommands==250, 251, 252, 253, 254, 255@Thelastcharacterina˙GFfileisfollowedby`| post|';thiscommandintroducesthepostamble, whichsummarizesimportantfactsthat#tex2html_wrap_indisplay618#META#tex2html_wrap_indisplay619#FONT hasaccumulated.Thepostamblehastheform#tex2html_wrap_indisplay620##tex2html_wrap_indisplay621##;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp; ;SPMamp; #;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp; ;SPMamp; ;SPMamp; #;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;#tex2html_wrap_indisplay622#Font ;SPMamp; Resolution;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp; ;SPMamp; ˙GFsize ;SPMamp; ˙PKsize ;SPMamp; Reductionfactor#tex2html_wrap_indisplay623#


cmr10 ;SPMamp; 300 ;SPMamp; 13200 ;SPMamp; 5484 ;SPMamp; 42%#tex2html_wrap_indisplay624#cmr10 ;SPMamp; 360 ;SPMamp; 15342 ;SPMamp; 6496 ;SPMamp; 42%#tex2html_wrap_indisplay625#cmr10 ;SPMamp; 432 ;SPMamp; 18120 ;SPMamp; 7808 ;SPMamp; 43%#tex2html_wrap_indisplay626#cmr10 ;SPMamp; 511 ;SPMamp; 21020 ;SPMamp; 9440 ;SPMamp; 45%#tex2html_wrap_indisplay627#cmr10 ;SPMamp; 622 ;SPMamp; 24880 ;SPMamp; 11492 ;SPMamp; 46%#tex2html_wrap_indisplay628#cmr10 ;SPMamp; 746 ;SPMamp; 29464 ;SPMamp; 13912 ;SPMamp; 47%#tex2html_wrap_indisplay629#cminch ;SPMamp; 300 ;SPMamp; 48764 ;SPMamp; 22076 ;SPMamp; 45%#tex2html_wrap_indisplay630#Itishopedthatthesimplicityandsmallsizeofthe˙PKfileswillmakethemwidelyaccepted.The˙PKformatwasdesignedandimplementedbyTomasRokickiduring@Rokicki, TomasGerhardPaul@ ;SPMgt; thesummerof1985.Thisprogramborrowsafewroutinesfrom˙GFtoPXLbyArthurSamuel.@Samuel, ArthurLee@ ;SPMgt; The| banner| stringdefinedhereshouldbechangedwhenever˙GFtoPKgetsmodified.The| preamblecomment| macro(neartheendoftheprogram)shouldbechangedtoo.@dbanner = = 'ThisisGFtoPK, Version2.3'printedwhentheprogramstarts@Someofthediagnosticinformationisprintedusing| dprintln|.Whendebugging, itshouldbesetthesameas| println|, definedlater.@debugging@ ;SPMgt; @ddprintln(#) = = @ThisprogramiswritteninstandardPascal, exceptwhereitisnecessarytouseextensions;forexample, oneextensionistouseadefault| case| asin˙TANGLEWEAVE, etc.Allplaceswherenonstandardconstructionsareusedshouldbelistedintheindexunder``systemdependencies.''@!@systemdependencies@ ;SPMgt; @dothercases = = others : defaultforcasesnotlistedexplicitly@dendcases = = @ + endfollowsthedefaultcaseinanextended| case| statement@fothercases = = else@fendcases = = end@Thebinaryinputcomesfrom| gffile|, andtheoutputfontiswrittenon| pkfile|.AlltextoutputiswrittenonPascal'sstandard| output| file.Theterm| print| isusedinsteadof| write| whenthisprogramwriteson| output|, sothatallsuchoutputcouldeasilyberedirectedifdesired.@dprint(#) = = write(#)@dprintln(#) = = writeln(#)@pprogramGFtoPK(@!gffile,@!pkfile,@!output);label@ ;SPMlt; Labelsintheouterblock@ ;SPMgt; @/const@ ;SPMlt; Constantsintheouterblock@ ;SPMgt; @/type@ ;SPMlt; Typesintheouterblock@ ;SPMgt; @/var@ ;SPMlt; Globalsintheouterblock@ ;SPMgt; @/procedureinitialize;thisproceduregetsthingsstartedproperlyvari : integer;loopindexforinitializationsbeginprintln(banner);@/@ ;SPMlt; Setinitialvalues@ ;SPMgt; @/end;@Iftheprogramhastostopprematurely, itgoestothe`| finalend|'.@dfinalend = 9999labelfortheendofitall@ ;SPMlt; Labels...@ ;SPMgt; = finalend;@Thefollowingparameterscanbechangedatcompiletimetoextendorreduce˙GFtoPKscapacity.Thevaluesgivenhereshouldbequiteadequateformostuses.Assuminganaverageofaboutthreestrokesperrasterline, therearesixrun - countsperline, andtherefore| maxrow| willbesufficientforacharacter2600pixelshigh.@ ;SPMlt; Constants...@ ;SPMgt; = @!linelength = 79;bracketedlinesofoutputwillbeatmostthislong@!maxrow = 16000;largestindexinthemain| row| array@Herearesomemacrosforcommonprogrammingidioms.@dincr(#) = = # : = # + 1increaseavariablebyunity@ddecr(#) = = # : = # - 1decreaseavariablebyunity@Ifthe˙GFfileisbadlymalformed, thewholeprocessmustbeabortedGFtoPKwillgiveup, afterissuinganerrormessageaboutthesymptomsthatwerenoticed.Sucherrorsmightbediscoveredinsideofsubroutinesinsideofsubroutines, soaprocedurecalled| jumpout| hasbeenintroduced.Thisprocedure, whichsimplytransferscontroltothelabel| finalend| attheendoftheprogram, containstheonlynon - local| goto| statementin˙GFtoPK.@systemdependencies@ ;SPMgt; @dabort(#) = = beginprint('',#);jumpout;end@dbadgf (#) = = abort('BadGFfile : ',#,'!')@.BadGFfile@ ;SPMgt; @pprocedurejumpout;begingotofinalend;@*Thecharacterset.Likeallprogramswrittenwiththe˙WEBsystemGFtoPKcanbeusedwithanycharacterset.ButitusesASCIIcodeinternally, becausetheprogrammingforportableinput - outputiseasierwhenafixedinternalcodeisused.Thenextfewsectionsof˙GFtoPKhavethereforebeencopiedfromtheanalogousonesinthe˙WEBsystemroutines.Theyhavebeenconsiderablysimplified, since˙GFtoPKneednotdealwiththecontroversialASCIIcodeslessthan@'40orgreaterthan@'176.Ifsuchcodesappearinthe˙GFfile, theywillbeprintedasquestionmarks.@ ;SPMlt; Types...@ ;SPMgt; = @!ASCIIcode = ;SPMquot;;SPMquot;..;SPMquot;~;SPMquot;;asubrangeoftheintegers@TheoriginalPascal compilerwasdesignedinthelate60s, whensix - bitcharactersetswerecommon, soitdidnotmakeprovisionforlowercaseletters.Nowadays, ofcourse, weneedtodealwithbothupperandlowercasealphabetsinaconvenientway, especiallyinaprogramlike˙GFtoPK.SoweshallassumethatthePascal systembeingusedfor˙GFtoPKhasacharactersetcontainingatleastthestandardvisiblecharactersofASCIIcode(|;SPMquot;!;SPMquot;| through|;SPMquot;~;SPMquot;|).SomePascal compilersusetheoriginalname| char| forthedatatypeassociatedwiththecharactersintextfiles, whileotherPascalsconsider| char| tobea64 - elementsubrangeofalargerdatatypethathassomeothername.Inordertoaccommodatethisdifference, weshallusethename| textchar| tostandforthedatatypeofthecharactersintheoutputfile.Weshallalsoassumethat| textchar| consistsoftheelements| chr(firsttextchar)| through| chr(lasttextchar)|, inclusive.Thefollowingdefinitionsshouldbeadjustedifnecessary.@systemdependencies@ ;SPMgt; @dtextchar = = charthedatatypeofcharactersintextfiles@dfirsttextchar = 0ordinalnumberofthesmallestelementof| textchar|@dlasttextchar = 127ordinalnumberofthelargestelementof| textchar|@ ;SPMlt; Types...@ ;SPMgt; = @!textfile = packedfileoftextchar;@The˙GFtoPKprocessorconvertsbetweenASCIIcodeandtheuser'sexternalcharactersetbymeansofarrays| xord| and| xchr| thatareanalogoustoPascal's| ord| and| chr| functions.@ ;SPMlt; Globals...@ ;SPMgt; = @!xord : array[textchar]ofASCIIcode;specifiesconversionofinputcharacters@!xchr : array[0..255]oftextchar;specifiesconversionofoutputcharacters@UnderourassumptionthatthevisiblecharactersofstandardASCIIareallpresent, thefollowingassignmentstatementsinitializethe| xchr| arrayproperly, withoutneedinganysystem - dependentchanges.@ ;SPMlt; Setinit...@ ;SPMgt; = fori : = 0to@'37doxchr[i] : = '?';xchr[@'40] : = '';xchr[@'41] : = '!';xchr[@'42] : = ';SPMquot;';xchr[@'43] : = '#';xchr[@'44] : = '$';xchr[@'45] : = '27xchr[@'46] : = ' ;SPMamp; ';xchr[@'47] : = '''';@/xchr[@'50] : = '(';xchr[@'51] : = ')';xchr[@'52] : = '*';xchr[@'53] : = ' + ';xchr[@'54] : = ',';xchr[@'55] : = ' - ';xchr[@'56] : = '.';xchr[@'57] : = '/';@/xchr[@'60] : = '0';xchr[@'61] : = '1';xchr[@'62] : = '2';xchr[@'63] : = '3';xchr[@'64] : = '4';xchr[@'65] : = '5';xchr[@'66] : = '6';xchr[@'67] : = '7';@/xchr[@'70] : = '8';xchr[@'71] : = '9';xchr[@'72] : = ' : ';xchr[@'73] : = ';';xchr[@'74] : = ' ;SPMlt; ';xchr[@'75] : = ' = ';xchr[@'76] : = ' ;SPMgt; ';xchr[@'77] : = '?';@/xchr[@'100] : = '@@';xchr[@'101] : = 'A';xchr[@'102] : = 'B';xchr[@'103] : = 'C';xchr[@'104] : = 'D';xchr[@'105] : = 'E';xchr[@'106] : = 'F';xchr[@'107] : = 'G';@/xchr[@'110] : = 'H';xchr[@'111] : = 'I';xchr[@'112] : = 'J';xchr[@'113] : = 'K';xchr[@'114] : = 'L';xchr[@'115] : = 'M';xchr[@'116] : = 'N';xchr[@'117] : = 'O';@/xchr[@'120] : = 'P';xchr[@'121] : = 'Q';xchr[@'122] : = 'R';xchr[@'123] : = 'S';xchr[@'124] : = 'T';xchr[@'125] : = 'U';xchr[@'126] : = 'V';xchr[@'127] : = 'W';@/xchr[@'130] : = 'X';xchr[@'131] : = 'Y';xchr[@'132] : = 'Z';xchr[@'133] : = '[';xchr[@'134] : = ';́xchr[@'135] : = ']';xchr[@'136] : = '';xchr[@'137] : = '';@/xchr[@'140] : = '`';xchr[@'141] : = 'a';xchr[@'142] : = 'b';xchr[@'143] : = 'c';xchr[@'144] : = 'd';xchr[@'145] : = 'e';xchr[@'146] : = 'f';xchr[@'147] : = 'g';@/xchr[@'150] : = 'h';xchr[@'151] : = 'i';xchr[@'152] : = 'j';xchr[@'153] : = 'k';xchr[@'154] : = 'l';xchr[@'155] : = 'm';xchr[@'156] : = 'n';xchr[@'157] : = 'o';@/xchr[@'160] : = 'p';xchr[@'161] : = 'q';xchr[@'162] : = 'r';xchr[@'163] : = 's';xchr[@'164] : = 't';xchr[@'165] : = 'u';xchr[@'166] : = 'v';xchr[@'167] : = 'w';@/xchr[@'170] : = 'x';xchr[@'171] : = 'y';xchr[@'172] : = 'z';xchr[@'173] : = '';xchr[@'174]:='|';xchr[@'175]:=';xchr[@'176] : = '~';fori : = @'177to255doxchr[i] : = '?';@Thefollowingsystem - independentcodemakesthe| xord| arraycontainasuitableinversetotheinformationin| xchr|.@ ;SPMlt; Setinit...@ ;SPMgt; = fori : = firsttextchartolasttextchardoxord[chr(i)] : = @'40;fori : = ;SPMquot;;SPMquot;to;SPMquot;~;SPMquot;doxord[xchr[i]] : = i;@*Genericfontfileformat.Themostimportantoutputproducedbyatypicalrunof#tex2html_wrap_indisplay633#META#tex2html_wrap_indisplay634#FONT isthe``genericfont''GF)filethatspecifiesthebitpatternsofthecharactersthathavebeendrawn.Theterm#tex2html_wrap_indisplay635#indicatesthatthisfileformatdoesn'tmatchtheconventionsofanyname - brandmanufacturer;butitiseasytoconvert˙GFfilestothespecialformatrequiredbyalmostalldigitalphototypesettingequipment.There'sastronganalogybetweenthe˙DVIfileswrittenbyTEX andthe˙GFfileswrittenby#tex2html_wrap_indisplay636#META#tex2html_wrap_indisplay637#FONT;and, infact, thefileformatshavealotincommon.A˙GFfileisastreamof8 - bitbytesthatmayberegardedasaseriesofcommandsinamachine - likelanguage.Thefirstbyteofeachcommandistheoperationcode, andthiscodeisfollowedbyzeroormorebytesthatprovideparameterstothecommand.Theparametersthemselvesmayconsistofseveralconsecutivebytes;forexample, the`| boc|'(beginningofcharacter)commandhassixparameters, eachofwhichisfourbyteslong.Parametersareusuallyregardedasnonnegativeintegers;butfour - byte - longparameterscanbeeitherpositiveornegative, hencetheyrangeinvaluefrom$ - 231$to$231 -1$.Asin˙TFMfiles, numbersthatoccupymorethanonebytepositionappearinBigEndianorder, andnegativenumbersappearintwo'scomplementnotation.A˙GFfileconsistsofa``preamble,''followedbyasequenceofoneormore``characters,''followedbya``postamble.''Thepreambleissimplya| pre| command, withitsparametersthatintroducethefile;thismustcomefirst.Each``character''consistsofa| boc| command, followedbyanynumberofothercommandsthatspecify``black''pixels, followedbyan| eoc| command.Thecharactersappearintheorderthat#tex2html_wrap_indisplay638#META#tex2html_wrap_indisplay639#FONTI>generatedthem.Ifweignoreno - opcommands(whichareallowedbetweenanytwocommandsinthefile), each| eoc| commandisimmediatelyfollowedbya| boc| command, orbya| post| command;inthelattercase, therearenomorecharactersinthefile, andtheremainingbytesformthepostamble.Furtherdetailsaboutthepostamblewillbeexplainedlater.Someparametersin˙GFcommandsare``pointers.''Thesearefour - bytequantitiesthatgivethelocationnumberofsomeotherbyteinthefile;thefirstfilebyteisnumber~0, thencomesnumber~1, andsoon.@The˙GFformatisintendedtobebothcompactandeasilyinterpretedbyamachine.Compactnessisachievedbymakingmostoftheinformationrelativeinsteadofabsolute.Whena˙GF - readingprogramreadsthecommandsforacharacter, itkeepstrackoftwoquantities : (a)~thecurrentcolumnnumber,~| m|;and (b)~thecurrentrownumber,~| n|.Theseare32 - bitsignedintegers, althoughmostactualfontformatsproducedfrom˙GFfileswillneedtocurtailthisvastrangebecauseofpracticallimitations.(#tex2html_wrap_indisplay640#META#tex2html_wrap_indisplay641#FONT outputwillneverallow$|m|$or$|n|$togetextremelylarge, butthe˙GFformattriestobemoregeneral.)Howdo˙GFsrowandcolumnnumberscorrespondtotheconventionsofTEX and#tex2html_wrap_indisplay643#META#tex2html_wrap_indisplay644#FONT?Well, the``referencepoint''ofacharacter, inTEX'sview, isconsideredtobeatthelowerleftcornerofthepixelinrow~0andcolumn~0.Thispointistheintersectionofthebaselinewiththeleftedgeofthetype;itcorrespondstolocation$(0, 0)$in#tex2html_wrap_indisplay645#META#tex2html_wrap_indisplay646#FONT programs.Thusthepixelin˙GFrow~0andcolumn~0is#tex2html_wrap_indisplay647#META#tex2html_wrap_indisplay649#FONTsunitsquare, comprisingtheregionoftheplanewhosecoordinatesbothliebetween0and~1.Thepixelin˙GFrow~| n| andcolumn~| m| consistsofthepointswhose#tex2html_wrap_indisplay650#META#tex2html_wrap_indisplay651#FONTI>coordinates|(x, y)| satisfy| m ;SPMlt; = x ;SPMlt; = m + 1| and| n ;SPMlt; = y ;SPMlt; = n + 1|.Negativevaluesof| m| and~| x| correspondtocolumnsofpixels#tex2html_wrap_indisplay652#ofthereferencepoint;negativevaluesof| n| and~| y| correspondtorowsofpixels#tex2html_wrap_indisplay653#thebaseline.Besides| m| and| n|, there'salsoathirdaspectofthecurrentstate, namelythe@!| paintswitch|, whichisalwayseither
blackor
white.Each
paintcommandadvances| m| byaspecifiedamount~| d|, andblackenstheinterveningpixelsif| paintswitch = black|;thenthe| paintswitch| changestotheoppositestateGFscommandsaredesignedsothat| m| willneverdecreasewithinarow, and| n| willneverincreasewithinacharacter;hencethereisnowaytowhitenapixelthathasbeenblackened.@Hereisalistofallthecommandsthatmayappearina˙GFfile.Eachcommandisspecifiedbyitssymbolicname(e.g.,| boc|), itsopcodebyte(e.g., 67), anditsparameters(ifany).Theparametersarefollowedbyabracketednumbertellinghowmanybytestheyoccupy;forexample,`| d[2]|'meansthatparameter| d| istwobyteslong.#tex2html_wrap_indisplay655##tex2html_wrap_indisplay656#3em | paint0| 0.Thisisa
paintcommandwith| d = 0|;itdoesnothingbutchangethe| paintswitch| from
blackto
whiteorvice~versa.#tex2html_wrap_indisplay657##tex2html_wrap_indisplay658#3em 
paint#tex2html_wrap_indisplay659#1through
paint#tex2html_wrap_indisplay660#63(opcodes1to63).Theseare
paintcommandswith| d = 1| to~63, definedasfollows : If| paintswitch = black|, blacken| d|~pixelsofthecurrentrow~| n|, incolumns| m| through| m + d - 1| inclusive.Then, inanycase, complementthe| paintswitch| andadvance| m| by~| d|.#tex2html_wrap_indisplay661##tex2html_wrap_indisplay662#3em | paint1| 64| d[1]|.Thisisa
paintcommandwithaspecifiedvalueof~| d|;#tex2html_wrap_indisplay663#META#tex2html_wrap_indisplay664#FONT usesittopaintwhen| 64 ;SPMlt; = d ;SPMlt; 256|.#tex2html_wrap_indisplay665##tex2html_wrap_indisplay666#3em |@!paint2| 65| d[2]|.Sameas| paint1|, but| d|~canbeashighas~65535.#tex2html_wrap_indisplay667##tex2html_wrap_indisplay668#3em |@!paint3| 66| d[3]|.Sameas| paint1|, but| d|~canbeashighas$224 -1$.#tex2html_wrap_indisplay669#META#tex2html_wrap_indisplay670#FONT neverneedsthiscommand, anditishardtoimagineanybodymakingpracticaluseofit;surelyamorecompactencodingwillbedesirablewhencharacterscanbethislarge.Butthecommandisthere, anyway, justincase.#tex2html_wrap_indisplay671##tex2html_wrap_indisplay672#3em | boc| 67| c[4]|| p[4]|| minm[4]|| maxm[4]|| minn[4]|| maxn[4]|.Beginningofacharacter : Here| c| isthecharactercode, and| p| pointstothepreviouscharacterbeginning(ifany)forcharactershavingthiscodenumbermodulo256.(Thepointer| p| is| - 1| iftherewasnopriorcharacterwithanequivalentcode.)Thevaluesofregisters| m| and| n| definedbytheinstructionsthatfollowforthischaractermustsatisfy| minm ;SPMlt; = m ;SPMlt; = maxm| and| minn ;SPMlt; = n ;SPMlt; = maxn|.(Thevaluesof| maxm| and| minn| neednotbethetightestboundspossible.)Whena˙GF - readingprogramseesa| boc|, itcanuse| minm|,| maxm|,| minn|, and| maxn| toinitializetheboundsofanarray.Thenitsets| m : = minm|,| n : = maxn|, and| paintswitch : = white|.#tex2html_wrap_indisplay673##tex2html_wrap_indisplay674#3em | boc1| 68| c[1]||@!delm[1]|| maxm[1]||@!deln[1]|| maxn[1]|.Sameas| boc|, but| p| isassumedtobe~$ - 1$;also| delm = maxm - minm| and| deln = maxn - minn| aregiveninsteadof| minm| and| minn|.Theone - byteparametersmustbebetween0and255, inclusive. (Thisabbreviated| boc| saves19~bytespercharacter, incommoncases.)#tex2html_wrap_indisplay675##tex2html_wrap_indisplay676#3em | eoc| 69.Endofcharacter : Allpixelsblackenedsofarconstitutethepatternforthischaracter.Inparticular, acompletelyblankcharactermighthave| eoc| immediatelyfollowing| boc|.#tex2html_wrap_indisplay677##tex2html_wrap_indisplay678#3em | skip0| 70.Decrease| n| by1andset| m : = minm|,| paintswitch : = white|. (Thisfinishesonerowandbeginsanother, readytowhitentheleftmostpixelinthenewrow.)#tex2html_wrap_indisplay679##tex2html_wrap_indisplay680#3em | skip1| 71| d[1]|.Decrease| n| by| d + 1|, set| m : = minm|, andset| paintswitch : = white|.Thisisawaytoproduce| d| all - whiterows.#tex2html_wrap_indisplay681##tex2html_wrap_indisplay682#3em |@!skip2| 72| d[2]|.Sameas| skip1|, but| d| canbeaslargeas65535.#tex2html_wrap_indisplay683##tex2html_wrap_indisplay684#3em |@!skip3| 73| d[3]|.Sameas| skip1|, but| d| canbeaslargeas$224 -1$.#tex2html_wrap_indisplay685#META#tex2html_wrap_indisplay686#FONT obviouslyneverneedsthiscommand.#tex2html_wrap_indisplay687##tex2html_wrap_indisplay688#3em | newrow0| 74.Decrease| n| by1andset| m : = minm|,| paintswitch : = black|. (Thisfinishesonerowandbeginsanother, readyto#tex2html_wrap_indisplay689#theleftmostpixelinthenewrow.)#tex2html_wrap_indisplay690##tex2html_wrap_indisplay691#3em |@!newrow1| through|@!newrow164|(opcodes75to238).Sameas| newrow0|, butwith| m : = minm +1| through| minm +164|, respectively.#tex2html_wrap_indisplay692##tex2html_wrap_indisplay693#3em | xxx1| 239| k[1]|| x[k]|.Thiscommandisundefinedingeneral;itfunctionsasa$(k + 2)$ - byte| noop| unlessspecial˙GF - readingprogramsarebeingused.#tex2html_wrap_indisplay694#META#tex2html_wrap_indisplay695#FONT generates
xxxcommandswhenencounteringaspecialstring;thisoccursinthe˙GFfileonlybetweencharacters, afterthepreamble, andbeforethepostamble.However,
xxxcommandsmightappearwithincharacters, in˙GFfilesgeneratedbyotherprocessors.Itisrecommendedthat| x| beastringhavingtheformofakeywordfollowedbypossibleparametersrelevanttothatkeyword.#tex2html_wrap_indisplay696##tex2html_wrap_indisplay697#3em |@!xxx2| 240| k[2]|| x[k]|.Like| xxx1|, but| 0 ;SPMlt; = k ;SPMlt; 65536|.#tex2html_wrap_indisplay698##tex2html_wrap_indisplay699#3em | xxx3| 241| k[3]|| x[k]|.Like| xxx1|, but| 0 ;SPMlt; = k ;SPMlt; @t$224$@ ;SPMgt; |.#tex2html_wrap_indisplay700#META#tex2html_wrap_indisplay701#FONT usesthiswhensendingaspecialstringwhoselengthexceeds~255.#tex2html_wrap_indisplay702##tex2html_wrap_indisplay703#3em |@!xxx4| 242| k[4]|| x[k]|.Like| xxx1|, but| k| canberidiculouslylarge;| k| mustn'tbenegative.#tex2html_wrap_indisplay704##tex2html_wrap_indisplay705#3em | yyy| 243| y[4]|.Thiscommandisundefinedingeneral;itfunctionsasa5 - byte| noop| unlessspecial˙GF - readingprogramsarebeingused.#tex2html_wrap_indisplay706#META#tex2html_wrap_indisplay707#FONT puts| scaled| numbersinto| yyy|'s, asaresultofnumspecialcommands;theintentistoprovidenumericparametersto
xxxcommandsthatimmediatelyprecede.#tex2html_wrap_indisplay708##tex2html_wrap_indisplay709#3em | noop| 244.Nooperation, donothing.Anynumberof| noop|'smayoccurbetween˙GFcommands, buta| noop| cannotbeinsertedbetweenacommandanditsparametersorbetweentwoparameters.#tex2html_wrap_indisplay710##tex2html_wrap_indisplay711#3em | charloc| 245| c[1]|| dx[4]|| dy[4]|| w[4]|| p[4]|.Thiscommandwillappearonlyinthepostamble, whichwillbeexplainedshortly.#tex2html_wrap_indisplay712##tex2html_wrap_indisplay713#3em |@!charloc0| 246| c[1]||@!dm[1]|| w[4]|| p[4]|.Sameas| charloc|, exceptthat| dy| isassumedtobezero, andthevalueof~| dx| istakentobe| 65536*dm|, where| 0 ;SPMlt; = dm ;SPMlt; 256|.#tex2html_wrap_indisplay714##tex2html_wrap_indisplay715#3em | pre| 247| i[1]|| k[1]|| x[k]|.Beginningofthepreamble;thismustcomeattheverybeginningofthefile.Parameter| i| isanidentifyingnumberfor˙GFformat, currently131.Theotherinformationismerelycommentary;itisnotgivenspecialinterpretationlike
xxxcommandsare.(Notethat
xxxcommandsmayimmediatelyfollowthepreamble, beforethefirst| boc|.)#tex2html_wrap_indisplay716##tex2html_wrap_indisplay717#3em | post| 248.Beginningofthepostamble, seebelow.#tex2html_wrap_indisplay718##tex2html_wrap_indisplay719#3em | postpost| 249.Endingofthepostamble, seebelow.#tex2html_wrap_indisplay720#Commands250 - -255areundefinedatthepresenttime.@dgfidbyte = 131identifiesthekindof˙GFfilesdescribedhere@Herearetheopcodesthat˙GFtoPKactuallyrefersto.@dpaint0 = 0beginningofthe
paintcommands@dpaint1 = 64moverightagivennumberofcolumns, thenblack$#tex2html_wrap_indisplay721#$white@dboc = 67beginningofacharacter@dboc1 = 68abbreviated| boc|@deoc = 69endofacharacter@dskip0 = 70skipnoblankrows@dskip1 = 71skipoverblankrows@dnewrow0 = 74movedownonerowandthenright@dmaxnewrow = 238movedownonerowandthenright@dxxx1 = 239forspecialstrings@dyyy = 243fornumspecialnumbers@dnoop = 244nooperation@dcharloc = 245characterlocatorsinthepostamble@dcharloc0 = 246characterlocatorsinthepostamble@dpre = 247preamble@dpost = 248postamblebeginning@dpostpost = 249postambleending@dundefinedcommands = = 250, 251, 252, 253, 254, 255@Thelastcharacterina˙GFfileisfollowedby`| post|';thiscommandintroducesthepostamble, whichsummarizesimportantfactsthat#tex2html_wrap_indisplay722#META#tex2html_wrap_indisplay723#FONT hasaccumulated.Thepostamblehastheform$$#tex2html_wrap_indisplay724##tex2html_wrap_indisplay725###tex2html_wrap_indisplay726#| post|| p[4]||@!ds[4]||@!cs[4]||@!hppp[4]||@!vppp[4]||@!minm[4]||@!maxm[4]||@!minn[4]||@!maxn[4]|#tex2html_wrap_indisplay727#$〈 $characterlocators$ 〉$#tex2html_wrap_indisplay728#| postpost|| q[4]|| i[1]| 223's$[#tex2html_wrap_indisplay729#4]$#tex2html_wrap_indisplay730#

Here |p| is a pointer to the byte following the final |eoc| in the file (or to the byte following the preamble, if there are no characters); it can be used to locate the beginning of
<#139#>xxx<#139#> commands that might have preceded the postamble. The |ds| and |cs| parameters @^design size@;SPMgt; @^check sum@;SPMgt; give the design size and check sum, respectively, which are exactly the values put into the header of any ṪFM file that shares information with this ĠF file. Parameters |hppp| and |vppp| are the ratios of pixels per point, horizontally and vertically, expressed as |scaled| integers (i.e., multiplied by 216); they can be used to correlate the font with specific device resolutions, magnifications, and ``at sizes.'' Then come |min_m|, |max_m|, |min_n|, and |max_n|, which bound the values that registers |m| and~|n| assume in all characters in this ĠF file. (These bounds need not be the best possible; |max_m| and |min_n| may, on the other hand, be tighter than the similar bounds in |boc| commands. For example, some character may have |min_n=-100| in its |boc|, but it might turn out that |n| never gets lower than |-50| in any character; then |min_n| can have any value |;SPMlt;=-50|. If there are no characters in the file, it's possible to have |min_m;SPMgt;max_m| and/or |min_n;SPMgt;max_n|.) @ Character locators are introduced by |char_loc| commands, which specify a character residue~|c|, character escapements (|dx,dy|), a character width~|w|, and a pointer~|p| to the beginning of that character. (If two or more characters have the same code~|c| modulo 256, only the last will be indicated; the others can be located by following backpointers. Characters whose codes differ by a multiple of 256 are assumed to share the same font metric information, hence the ṪFM file contains only residues of character codes modulo~256. This convention is intended for oriental languages, when there are many character shapes but few distinct widths.) @^oriental characters@;SPMgt;@^Chinese characters@;SPMgt;@^Japanese characters@;SPMgt; The character escapements (|dx,dy|) are the values of <#399#>#tex2html_accent_inline732#<#399#><#400#>#tex2html_accent_inline733#<#400#>'s <#145#>chardx<#145#> and <#146#>chardy<#146#> parameters; they are in units of |scaled| pixels; i.e., |dx| is in horizontal pixel units times 216, and |dy| is in vertical pixel units times 216. This is the intended amount of displacement after typesetting the character; for ḊVI files, |dy| should be zero, but other document file formats allow nonzero vertical escapement. The character width~|w| duplicates the information in the ṪFM file; it is 224 times the ratio of the true width to the font's design size. The backpointer |p| points to the character's |boc|, or to the first of a sequence of consecutive
<#152#>xxx<#152#> or |yyy| or |no_op| commands that immediately precede the |boc|, if such commands exist; such ``special'' commands essentially belong to the characters, while the special commands after the final character belong to the postamble (i.e., to the font as a whole). This convention about |p| applies also to the backpointers in |boc| commands, even though it wasn't explained in the description of~|boc|. @^backpointers@;SPMgt; Pointer |p| might be |-1| if the character exists in the ṪFM file but not in the ĠF file. This unusual situation can arise in <#401#>#tex2html_accent_inline737#<#401#><#402#>#tex2html_accent_inline738#<#402#> output if the user had |proofing;SPMlt;0| when the character was being shipped out, but then made |proofing;SPMgt;=0| in order to get a ĠF file. @ The last part of the postamble, following the |post_post| byte that signifies the end of the character locators, contains |q|, a pointer to the |post| command that started the postamble. An identification byte, |i|, comes next; this currently equals~131, as in the preamble. The |i| byte is followed by four or more bytes that are all equal to the decimal number 223 (i.e., @'337 in octal). <#403#>#tex2html_accent_inline739#<#403#><#404#>#tex2html_accent_inline740#<#404#> puts out four to seven of these trailing bytes, until the total length of the file is a multiple of four bytes, since this works out best on machines that pack four bytes per word; but any number of 223's is allowed, as long as there are at least four of them. In effect, 223 is a sort of signature that is added at the very end. @^Fuchs, David Raymond@;SPMgt; This curious way to finish off a ĠF file makes it feasible for ĠF-reading programs to find the postamble first, on most computers, even though <#405#>#tex2html_accent_inline741#<#405#><#406#>#tex2html_accent_inline742#<#406#> wants to write the postamble last. Most operating systems permit random access to individual words or bytes of a file, so the ĠF reader can start at the end and skip backwards over the 223's until finding the identification byte. Then it can back up four bytes, read |q|, and move to byte |q| of the file. This byte should, of course, contain the value 248 (|post|); now the postamble can be read, so the ĠF reader can discover all the information needed for individual characters. Unfortunately, however, standard Pascal does not include the ability to @^system dependencies@;SPMgt; access a random position in a file, or even to determine the length of a file. Almost all systems nowadays provide the necessary capabilities, so ĠF format has been designed to work most efficiently with modern operating systems. ĠFtoPK first reads the postamble, and then scans the file from front to back. @* Packed file format. The packed file format is a compact representation of the data contained in a ĠF file. The information content is the same, but packed (ṖK) files are almost always less than half the size of their ĠF counterparts. They are also easier to convert into a raster representation because they do not have a profusion of
<#165#>paint<#165#>,
<#166#>skip<#166#>, and
<#167#>new_row<#167#> commands to be separately interpreted. In addition, the ṖK format expressedly forbids <#169#>special<#169#> commands within a character. The minimum bounding box for each character is explicit in the format, and does not need to be scanned for as in the ĠF format. Finally, the width and escapement values are combined with the raster information into character ``packets'', making it simpler in many cases to process a character. A ṖK file is organized as a stream of 8-bit bytes. At times, these bytes might be split into 4-bit nybbles or single bits, or combined into multiple byte parameters. When bytes are split into smaller pieces, the `first' piece is always the most significant of the byte. For instance, the first bit of a byte is the bit with value 128; the first nybble can be found by dividing a byte by 16. Similarly, when bytes are combined into multiple byte parameters, the first byte is the most significant of the parameter. If the parameter is signed, it is represented by two's-complement notation. The set of possible eight-bit values is separated into two sets, those that introduce a character definition, and those that do not. The values that introduce a character definition range from 0 to 239; byte values above 239 are interpreted as commands. Bytes that introduce character definitions are called flag bytes, and various fields within the byte indicate various things about how the character definition is encoded. Command bytes have zero or more parameters, and can never appear within a character definition or between parameters of another command, where they would be interpeted as data. A ṖK file consists of a preamble, followed by a sequence of one or more character definitions, followed by a postamble. The preamble command must be the first byte in the file, followed immediately by its parameters. Any number of character definitions may follow, and any command but the preamble command and the postamble command may occur between character definitions. The very last command in the file must be the postamble. @ The packed file format is intended to be easy to read and interpret by device drivers. The small size of the file reduces the input/output overhead each time a font is loaded. For those drivers that load and save each font file into memory, the small size also helps reduce the memory requirements. The length of each character packet is specified, allowing the character raster data to be loaded into memory by simply counting bytes, rather than interpreting each command; then, each character can be interpreted on a demand basis. This also makes it possible for a driver to skip a particular character quickly if it knows that the character is unused. @ First, the command bytes will be presented; then the format of the character definitions will be defined. Eight of the possible sixteen commands (values 240 through 255) are currently defined; the others are reserved for future extensions. The commands are listed below. Each command is specified by its symbolic name (e.g.,
<#173#>pk_no_op<#173#>), its opcode byte, and any parameters. The parameters are followed by a bracketed number telling how many bytes they occupy, with the number preceded by a plus sign if it is a signed quantity. (Four byte quantities are always signed, however.) 3em|pk_xxx1| 240 |k[1]| |x[k]|. This command is undefined in general; it functions as a (k + 2)-byte
<#174#>no_op<#174#> unless special ṖK-reading programs are being used. <#407#>#tex2html_accent_inline744#<#407#><#408#>#tex2html_accent_inline745#<#408#> generates
<#176#>xxx<#176#> commands when encountering a <#177#>special<#177#> string. It is recommended that |x| be a string having the form of a keyword followed by possible parameters relevant to that keyword. 3em
<#178#>pk_xxx2<#178#> 241 |k[2]| |x[k]|. Like |pk_xxx1|, but |0;SPMlt;=k;SPMlt;65536|. 3em
<#179#>pk_xxx3<#179#> 242 |k[3]| |x[k]|. Like |pk_xxx1|, but |0;SPMlt;=k;SPMlt;@t224@;SPMgt;|. <#409#>#tex2html_accent_inline747#<#409#><#410#>#tex2html_accent_inline748#<#410#> uses this when sending a <#181#>special<#181#> string whose length exceeds~255. 3em
<#182#>pk_xxx4<#182#> 243 |k[4]| |x[k]|. Like |pk_xxx1|, but |k| can be ridiculously large; |k| musn't be negative. 3em|pk_yyy| 244 |y[4]|. This command is undefined in general; it functions as a five-byte
<#183#>no_op<#183#> unless special ṖK reading programs are being used. <#411#>#tex2html_accent_inline749#<#411#><#412#>#tex2html_accent_inline750#<#412#> puts |scaled| numbers into |yyy|'s, as a result of <#185#>numspecial<#185#> commands; the intent is to provide numeric parameters to
<#186#>xxx<#186#> commands that immediately precede. 3em|pk_post| 245. Beginning of the postamble. This command is followed by enough |pk_no_op| commands to make the file a multiple of four bytes long. Zero through three bytes are usual, but any number is allowed. This should make the file easy to read on machines that pack four bytes to a word. 3em|pk_no_op| 246. No operation, do nothing. Any number of |pk_no_op|'s may appear between ṖK commands, but a |pk_no_op| cannot be inserted between a command and its parameters, between two parameters, or inside a character definition. 3em|pk_pre| 247 |i[1]| |k[1]| |x[k]| |ds[4]| |cs[4]| |hppp[4]| |vppp[4]|. Preamble command. Here, |i| is the identification byte of the file, currently equal to 89. The string |x| is merely a comment, usually indicating the source of the ṖK file. The parameters |ds| and |cs| are the design size of the file in 1/220 points, and the checksum of the file, respectively. The checksum should match the ṪFM file and the ĠF files for this font. Parameters |hppp| and |vppp| are the ratios of pixels per point, horizontally and vertically, multiplied by 216; they can be used to correlate the font with specific device resolutions, magnifications, and ``at sizes''. Usually, the name of the ṖK file is formed by concatenating the font name (e.g., cmr10) with the resolution at which the font is prepared in pixels per inch multiplied by the magnification factor, and the letters ṗk. For instance, cmr10 at 300 dots per inch should be named ċmr10.300pk; at one thousand dots per inch and magstephalf, it should be named ċmr10.1095pk. @ We put a few of the above opcodes into definitions for symbolic use by this program. @d pk_id = 89 <#349#>the version of ṖK file described<#349#> @d pk_xxx1 = 240 <#350#><#198#>special<#198#> commands<#350#> @d pk_yyy = 244 <#351#><#199#>numspecial<#199#> commands<#351#> @d pk_post = 245 <#200#>postamble<#200#> @d pk_no_op = 246 <#201#>no operation<#201#> @d pk_pre = 247 <#202#>preamble<#202#> @ The ṖK format has two conflicting goals: to pack character raster and size information as compactly as possible, while retaining ease of translation into raster and other forms. A suitable compromise was found in the use of run-encoding of the raster information. Instead of packing the individual bits of the character, we instead count the number of consecutive `black' or `white' pixels in a horizontal raster row, and then encode this number. Run counts are found for each row from left to right, traversing rows from the top to bottom. This is essentially the way the ĠF format works. Instead of presenting each row individually, however, we concatenate all of the horizontal raster rows into one long string of pixels, and encode this row. With knowledge of the width of the bit-map, the original character glyph can easily be reconstructed. In addition, we do not need special commands to mark the end of one row and the beginning of the next. Next, we place the burden of finding the minimum bounding box on the part of the font generator, since the characters will usually be used much more often than they are generated. The minimum bounding box is the smallest rectangle that encloses all `black' pixels of a character. We also eliminate the need for a special end of character marker, by supplying exactly as many bits as are required to fill the minimum bounding box, from which the end of the character is implicit. Let us next consider the distribution of the run counts. Analysis of several dozen pixel files at 300 dots per inch yields a distribution peaking at four, falling off slowly until ten, then a bit more steeply until twenty, and then asymptotically approaching the horizontal. Thus, the great majority of our run counts will fit in a four-bit nybble. The eight-bit byte is attractive for our run-counts, as it is the standard on many systems; however, the wasted four bits in the majority of cases seem a high price to pay. Another possibility is to use a Huffman-type encoding scheme with a variable number of bits for each run-count; this was rejected because of the overhead in fetching and examining individual bits in the file. Thus, the character raster definitions in the ṖK file format are based on the four-bit nybble. @ An analysis of typical pixel files yielded another interesting statistic: Fully 37% of the raster rows were duplicates of the previous row. Thus, the ṖK format allows the specification of repeat counts, which indicate how many times a horizontal raster row is to be repeated. These repeated rows are taken out of the character glyph before individual rows are concatenated into the long string of pixels. For elegance, we disallow a run count of zero. The case of a null raster description should be gleaned from the character width and height being equal to zero, and no raster data should be read. No other zero counts are ever necessary. Also, in the absence of repeat counts, the repeat value is set to be zero (only the original row is sent.) If a repeat count is seen, it takes effect on the current row. The current row is defined as the row on which the first pixel of the next run count will lie. The repeat count is set back to zero when the last pixel in the current row is seen, and the row is sent out. This poses a problem for entirely black and entirely white rows, however. Let us say that the current row ends with four white pixels, and then we have five entirely empty rows, followed by a black pixel at the beginning of the next row, and the character width is ten pixels. We would like to use a repeat count, but there is no legal place to put it. If we put it before the white run count, it will apply to the current row. If we put it after, it applies to the row with the black pixel at the beginning. Thus, entirely white or entirely black repeated rows are always packed as large run counts (in this case, a white run count of 54) rather than repeat counts. @ Now we turn our attention to the actual packing of the run counts and repeat counts into nybbles. There are only sixteen possible nybble values. We need to indicate run counts and repeat counts. Since the run counts are much more common, we will devote the majority of the nybble values to them. We therefore indicate a repeat count by a nybble of 14 followed by a packed number, where a packed number will be explained later. Since the repeat count value of one is so common, we indicate a repeat one command by a single nybble of 15. A 14 followed by the packed number 1 is still legal for a repeat one count. The run counts are coded directly as packed numbers. For packed numbers, therefore, we have the nybble values 0 through 13. We need to represent the positive integers up to, say, 231 - 1. We would like the more common smaller numbers to take only one or two nybbles, and the infrequent large numbers to take three or more. We could therefore allocate one nybble value to indicate a large run count taking three or more nybbles. We do this with the value 0. @ We are left with the values 1 through 13. We can allocate some of these, say |dyn_f|, to be one-nybble run counts. These will work for the run counts |1..dyn_f|. For subsequent run counts, we will use a nybble greater than |dyn_f|, followed by a second nybble, whose value can run from 0 through 15. Thus, the two-nybble values will run from |dyn_f+1..(13-dyn_f)*16+dyn_f|. We have our definition of large run count values now, being all counts greater than |(13-dyn_f)*16+dyn_f|. We can analyze our several dozen pixel files and determine an optimal value of |dyn_f|, and use this value for all of the characters. Unfortunately, values of |dyn_f| that pack small characters well tend to pack the large characters poorly, and values that pack large characters well are not efficient for the smaller characters. Thus, we choose the optimal |dyn_f| on a character basis, picking the value that will pack each individual character in the smallest number of nybbles. Legal values of |dyn_f| run from 0 (with no one-nybble run counts) to 13 (with no two-nybble run counts). @ Our only remaining task in the coding of packed numbers is the large run counts. We use a scheme suggested by D.~E.~Knuth @^Knuth, Donald Ervin@;SPMgt; that simply and elegantly represents arbitrarily large values. The general scheme to represent an integer |i| is to write its hexadecimal representation, with leading zeros removed. Then we count the number of digits, and prepend one less than that many zeros before the hexadecimal representation. Thus, the values from one to fifteen occupy one nybble; the values sixteen through 255 occupy three, the values 256 through 4095 require five, etc. For our purposes, however, we have already represented the numbers one through |(13-dyn_f)*16+dyn_f|. In addition, the one-nybble values have already been taken by our other commands, which means that only the values from sixteen up are available to us for long run counts. Thus, we simply normalize our long run counts, by subtracting |(13-dyn_f)*16+dyn_f+1| and adding 16, and then we represent the result according to the scheme above. @ The final algorithm for decoding the run counts based on the above scheme might look like this, assuming that a procedure called
<#208#>pk_nyb<#208#> is available to get the next nybble from the file, and assuming that the global |repeat_count| indicates whether a row needs to be repeated. Note that this routine is recursive, but since a repeat count can never directly follow another repeat count, it can only be recursive to one level. @p@<#209#> function pk_packed_num : integer ; var i,@!j : integer ; begin i := get_nyb ; if i = 0 then begin repeat j := get_nyb ; incr(i) ; until j ;SPMlt;;SPMgt; 0 ; while i ;SPMgt; 0 do begin j := j * 16 + get_nyb ; decr(i) ; end ; pk_packed_num := j - 15 + (13-dyn_f)*16 + dyn_f ; end else if i ;SPMlt;= dyn_f then pk_packed_num := i else if i ;SPMlt; 14 then pk_packed_num := (i-dyn_f-1)*16+get_nyb+dyn_f+1 else begin if i = 14 then repeat_count := pk_packed_num else repeat_count := 1 ; pk_packed_num := pk_packed_num ; end ; end ; @<#209#> @ For low resolution fonts, or characters with `gray' areas, run encoding can often make the character many times larger. Therefore, for those characters that cannot be encoded efficiently with run counts, the ṖK format allows bit-mapping of the characters. This is indicated by a |dyn_f| value of 14. The bits are packed tightly, by concatenating all of the horizontal raster rows into one long string, and then packing this string eight bits to a byte. The number of bytes required can be calculated by |(width*height+7) div 8|. This format should only be used when packing the character by run counts takes more bytes than this, although, of course, it is legal for any character. Any extra bits in the last byte should be set to zero. @ At this point, we are ready to introduce the format for a character descriptor. It consists of three parts: a flag byte, a character preamble, and the raster data. The most significant four bits of the flag byte yield the |dyn_f| value for that character. (Notice that only values of 0 through 14 are legal for |dyn_f|, with 14 indicating a bit mapped character; thus, the flag bytes do not conflict with the command bytes, whose upper nybble is always 15.) The next bit (with weight 8) indicates whether the first run count is a black count or a white count, with a one indicating a black count. For bit-mapped characters, this bit should be set to a zero. The next bit (with weight 4) indicates whether certain later parameters (referred to as size parameters) are given in one-byte or two-byte quantities, with a one indicating that they are in two-byte quantities. The last two bits are concatenated on to the beginning of the packet-length parameter in the character preamble, which will be explained below. However, if the last three bits of the flag byte are all set (normally indicating that the size parameters are two-byte values and that a 3 should be prepended to the length parameter), then a long format of the character preamble should be used instead of one of the short forms. Therefore, there are three formats for the character preamble; the one that is used depends on the least significant three bits of the flag byte. If the least significant three bits are in the range zero through three, the short format is used. If they are in the range four through six, the extended short format is used. Otherwise, if the least significant bits are all set, then the long form of the character preamble is used. The preamble formats are explained below. 3emShort form: |flag[1]| |pl[1]| |cc[1]| |tfm[3]| |dm[1]| |w[1]| |h[1]| |hoff[+1]| |voff[+1]|. If this format of the character preamble is used, the above parameters must all fit in the indicated number of bytes, signed or unsigned as indicated. Almost all of the standard TEX font characters fit; the few exceptions are fonts such as ċminch. 3emExtended short form: |flag[1]| |pl[2]| |cc[1]| |tfm[3]| |dm[2]| |w[2]| |h[2]| |hoff[+2]| |voff[+2]|. Larger characters use this extended format. 3emLong form: |flag[1]| |pl[4]| |cc[4]| |tfm[4]| |dx[4]| |dy[4]| |w[4]| |h[4]| |hoff[4]| |voff[4]|. This is the general format that allows all of the parameters of the ĠF file format, including vertical escapement. The |flag| parameter is the flag byte. The parameter |pl| (packet length) contains the offset of the byte following this character descriptor, with respect to the beginning of the |tfm| width parameter. This is given so a ṖK reading program can, once it has read the flag byte, packet length, and character code (|cc|), skip over the character by simply reading this many more bytes. For the two short forms of the character preamble, the last two bits of the flag byte should be considered the two most-significant bits of the packet length. For the short format, the true packet length might be calculated as |(flag mod 4)*256+pl|; for the short extended format, it might be calculated as |(flag mod 4)*65536+pl|. The |w| parameter is the width and the |h| parameter is the height in pixels of the minimum bounding box. The |dx| and |dy| parameters are the horizontal and vertical escapements, respectively. In the short formats, |dy| is assumed to be zero and |dm| is |dx| but in pixels; in the long format, |dx| and |dy| are both in pixels multiplied by 216. The |hoff| is the horizontal offset from the upper left pixel to the reference pixel; the |voff| is the vertical offset. They are both given in pixels, with right and down being positive. The reference pixel is the pixel that occupies the unit square in <#413#>#tex2html_accent_inline755#<#413#><#414#>#tex2html_accent_inline756#<#414#>; the <#415#>#tex2html_accent_inline757#<#415#><#416#>#tex2html_accent_inline758#<#416#> reference point is the lower left hand corner of this pixel. (See the example below.) @ TEX requires all characters that have the same character codes modulo 256 to have also the same |tfm| widths and escapement values. The ṖK format does not itself make this a requirement, but in order for the font to work correctly with the TEX software, this constraint should be observed. (The standard version of TEX cannot output character codes greater than 255, but extended versions do exist.) Following the character preamble is the raster information for the character, packed by run counts or by bits, as indicated by the flag byte. If the character is packed by run counts and the required number of nybbles is odd, then the last byte of the raster description should have a zero for its least significant nybble. @ As an illustration of the ṖK format, the character  from the font amr10 at 300 dots per inch will be encoded. This character was chosen because it illustrates some of the borderline cases. The raster for the character looks like this (the row numbers are chosen for convenience, and are not <#417#>#tex2html_accent_inline759#<#417#><#418#>#tex2html_accent_inline760#<#418#>'s row numbers.) <#363#>`&sstarf#star;==height 7pt width 7pt depth 0pt
<#364#><#365#><#366#>#;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMamp;;SPMamp;# 0;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;* 1;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;* 2;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;* 3;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;* 4;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;*;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;* 5;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;*;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;* 6;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;*;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;* 9;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;*;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;*;SPMamp; ;SPMamp; 10;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;*;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;*;SPMamp; ;SPMamp; 11;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;*;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;*;SPMamp; ;SPMamp; 12;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp; ;SPMamp; 13;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp; ;SPMamp; 14;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp; ;SPMamp; 15;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp; ;SPMamp; 16;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;*;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;*;SPMamp; ;SPMamp; 17;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;*;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;*;SPMamp; ;SPMamp; 18;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;*;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;*;SPMamp; ;SPMamp; 19 20 21 22;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;*;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;* 23;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;*;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;* 24;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;*;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;* 25;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;* 26;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;* 27;SPMamp; ;SPMamp; ;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;* 28;SPMamp;+;SPMamp; ;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;*;SPMamp;* ;SPMamp;<#218#>*<#218#>;SPMamp;<#219#>*<#219#> The width of the minimum bounding box for this character is 20; its height is 29. The `+' represents the reference pixel; notice how it lies outside the minimum bounding box. The |hoff| value is -2, and the |voff| is~28. The first task is to calculate the run counts and repeat counts. The repeat counts are placed at the first transition (black to white or white to black) in a row, and are enclosed in brackets. White counts are enclosed in parentheses. It is relatively easy to generate the counts list:
82 [2] (16) 2 (42) [2] 2 (12) 2 (4) [3]
16 (4) [2] 2 (12) 2 (62) [2] 2 (16) 82
Note that any duplicated rows that are not all white or all black are removed before the run counts are calculated. The rows thus removed are rows 5, 6, 10, 11, 13, 14, 15, 17, 18, 23, and 24. @ The next step in the encoding of this character is to calculate the optimal value of |dyn_f|. The details of how this calculation is done are not important here; suffice it to say that there is a simple algorithm that can determine the best value of |dyn_f| in one pass over the count list. For this character, the optimal value turns out to be 8 (atypically low). Thus, all count values less than or equal to 8 are packed in one nybble; those from nine to #math30#(13 - 8)*16 + 8 or 88 are packed in two nybbles. The run encoded values now become (in hex, separated according to the above list):
D9 E2 97 2 B1 E2 2 93 2 4 E3
97 4 E2 2 93 2 C5 E2 2 97 D9
which comes to 36 nybbles, or 18 bytes. This is shorter than the 73 bytes required for the bit map, so we use the run count packing. @ The short form of the character preamble is used because all of the parameters fit in their respective lengths. The packet length is therefore 18 bytes for the raster, plus eight bytes for the character preamble parameters following the character code, or 26. The |tfm| width for this character is 640796, or <#224#>9C71C<#224#> in hexadecimal. The horizontal escapement is 25 pixels. The flag byte is 88 hex, indicating the short preamble, the black first count, and the |dyn_f| value of 8. The final total character packet, in hexadecimal, is:

#math31#

#tex2html_wrap_indisplay764##tex2html_wrap_indisplay765##;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp; ;SPMamp; ;SPMamp; #tex2html_wrap_indisplay766##tex2html_wrap_indisplay767#Flagbyte ;SPMamp; 88#tex2html_wrap_indisplay768#Packetlength ;SPMamp; 1A#tex2html_wrap_indisplay769#Charactercode ;SPMamp; 04#tex2html_wrap_indisplay770#| tfm| width ;SPMamp; 09 ;SPMamp; C7 ;SPMamp; 1C#tex2html_wrap_indisplay771#Horizontalescapement(pixels) ;SPMamp; 19#tex2html_wrap_indisplay772#Widthofbitmap ;SPMamp; 14#tex2html_wrap_indisplay773#Heightofbitmap ;SPMamp; 1D#tex2html_wrap_indisplay774#Horizontaloffset(signed ) ;SPMamp; FE#tex2html_wrap_indisplay775#Verticaloffset ;SPMamp; 1C#tex2html_wrap_indisplay776#Rasterdata ;SPMamp; D9 ;SPMamp; E2 ;SPMamp; 97#tex2html_wrap_indisplay777# ;SPMamp; 2B ;SPMamp; 1E ;SPMamp; 22#tex2html_wrap_indisplay778# ;SPMamp; 93 ;SPMamp; 24 ;SPMamp; E3#tex2html_wrap_indisplay779# ;SPMamp; 97 ;SPMamp; 4E ;SPMamp; 22#tex2html_wrap_indisplay780# ;SPMamp; 93 ;SPMamp; 2C ;SPMamp; 5E#tex2html_wrap_indisplay781# ;SPMamp; 22 ;SPMamp; 97 ;SPMamp; D9#tex2html_wrap_indisplay782#

@* Input and output for binary files. We have seen that a ĠF file is a sequence of 8-bit bytes. The bytes appear physically in what is called a `|packed file of 0..255|' in Pascal lingo. The ṖK file is also a sequence of 8-bit bytes. Packing is system dependent, and many Pascal systems fail to implement such files in a sensible way (at least, from the viewpoint of producing good production software). For example, some systems treat all byte-oriented files as text, looking for end-of-line marks and such things. Therefore some system-dependent code is often needed to deal with binary files, even though most of the program in this section of ĠFtoPK is written in standard Pascal. @^system dependencies@;SPMgt; We shall stick to simple Pascal in this program, for reasons of clarity, even if such simplicity is sometimes unrealistic. @;SPMlt;Types...@;SPMgt;= @!eight_bits=0..255; <#229#>unsigned one-byte quantity<#229#> @!byte_file=packed file of eight_bits; <#230#>files that contain binary data<#230#> @ The program deals with two binary file variables: |gf_file| is the input file that we are translating into ṖK format, to be written on |pk_file|. @;SPMlt;Glob...@;SPMgt;= @!gf_file:byte_file; <#353#>the stuff we are ĠFtoPKing<#353#> @!pk_file:byte_file; <#354#>the stuff we have ĠFtoPKed<#354#> @ To prepare the |gf_file| for input, we |reset| it. @p procedure open_gf_file; <#234#>prepares to read packed bytes in |gf_file|<#234#> begin reset(gf_file); gf_loc := 0 ; @ To prepare the |pk_file| for output, we |rewrite| it. @p procedure open_pk_file; <#235#>prepares to write packed bytes in |pk_file|<#235#> begin rewrite(pk_file); pk_loc := 0 ; pk_open := true ; @ The variable |pk_loc| contains the number of the byte about to be written to the |pk_file|, and |gf_loc| is the byte about to be read from the |gf_file|. Also, |pk_open| indicates that the packed file has been opened and is ready for output. @;SPMlt;Glob...@;SPMgt;= @!pk_loc:integer; <#236#>where we are about to write, in |pk_file|<#236#> @!gf_loc:integer; <#237#>where are we in the |gf_file|<#237#> @!pk_open:boolean; <#238#>is the packed file open?<#238#> @ We do not open the |pk_file| until after the postamble of the |gf_file| has been read. This can be used, for instance, to calculate a resolution to put in the suffix of the |pk_file| name. This also means, however, that specials in the postamble (which <#419#>#tex2html_accent_inline783#<#419#><#420#>#tex2html_accent_inline784#<#420#> never generates) do not get sent to the |pk_file|. @;SPMlt;Set init...@;SPMgt;= pk_open := false ; @ We shall use two simple functions to read the next byte or bytes from |gf_file|. We either need to get an individual byte or a set of four bytes. @^system dependencies@;SPMgt; @p function gf_byte:integer; <#239#>returns the next byte, unsigned<#239#> var b:eight_bits; begin if eof(gf_file) then bad_gf('Unexpected end of file!') @.Unexpected end of file@;SPMgt; else begin read(gf_file,b); gf_byte:=b; end; incr(gf_loc); function gf_signed_quad:integer; <#240#>returns the next four bytes, signed<#240#> var a,@!b,@!c,@!d:eight_bits; begin read(gf_file,a); read(gf_file,b); read(gf_file,c); read(gf_file,d); if a;SPMlt;128 then gf_signed_quad:=((a*256+b)*256+c)*256+d else gf_signed_quad:=(((a-256)*256+b)*256+c)*256+d; gf_loc := gf_loc + 4 ; @ We also need a few routines to write data to the ṖK file. We write data in 4-, 8-, 16-, 24-, and 32-bit chunks, so we define the appropriate routines. We must be careful not to let the sign bit mess us up, as some Pascals implement division of a negative integer differently. @p procedure pk_byte(a:integer) ; begin if pk_open then begin if a ;SPMlt; 0 then a := a + 256 ; write(pk_file, a) ; incr(pk_loc) ; end ; end ; procedure pk_halfword(a:integer) ; begin if a ;SPMlt; 0 then a := a + 65536 ; write(pk_file, a div 256) ; write(pk_file, a mod 256) ; pk_loc := pk_loc + 2 ; end ; procedure pk_three_bytes(a:integer); begin write(pk_file, a div 65536 mod 256) ; write(pk_file, a div 256 mod 256) ; write(pk_file, a mod 256) ; pk_loc := pk_loc + 3 ; end ; procedure pk_word(a:integer) ; var b : integer ; begin if pk_open then begin if a ;SPMlt; 0 then begin a := a + @'10000000000 ; a := a + @'10000000000 ; b := 128 + a div 16777216 ; end else b := a div 16777216 ; write(pk_file, b) ; write(pk_file, a div 65536 mod 256) ; write(pk_file, a div 256 mod 256) ; write(pk_file, a mod 256) ; pk_loc := pk_loc + 4 ; end ; end ; procedure pk_nyb(a:integer) ; begin if bit_weight = 16 then begin output_byte := a * 16 ; bit_weight := 1 ; end else begin pk_byte(output_byte + a) ; bit_weight := 16 ; end ; end ; @ We need the globals |bit_weight| and |output_byte| for buffering. @;SPMlt;Glob...@;SPMgt;= @!bit_weight : integer ; <#242#>output bit weight<#242#> @!output_byte : integer ; <#243#>output byte for pk file<#243#> @ Finally we come to the routines that are used for random access of the |gf_file|. To correctly find and read the postamble of the file, we need two routines, one to find the length of the |gf_file|, and one to position the |gf_file|. We assume that the first byte of the file is numbered zero. Such routines are, of course, highly system dependent. They are implemented here in terms of two assumed system routines called |set_pos| and |cur_pos|. The call |set_pos(f,n)| moves to item |n| in file |f|, unless |n| is negative or larger than the total number of items in |f|; in the latter case, |set_pos(f,n)| moves to the end of file |f|. The call |cur_pos(f)| gives the total number of items in |f|, if |eof(f)| is true; we use |cur_pos| only in such a situation. @^system dependencies@;SPMgt; @p procedure find_gf_length ; begin set_pos(gf_file, -1) ; gf_len := cur_pos(gf_file) ; end ; procedure move_to_byte(@!n : integer) ; begin set_pos(gf_file, n); gf_loc := n ; end ; @ The global |gf_len| contains the final total length of the |gf_file|. @;SPMlt;Glob...@;SPMgt;= @!gf_len : integer ; <#244#>length of |gf_file|<#244#> @* Plan of attack. It would seem at first that converting a ĠF file to ṖK format should be relatively easy, since they both use a form of run-encoding. Unfortunately, several idiosyncracies of the ĠF format make this conversion slightly cumbersome. The ĠF format separates the raster information from the escapement values and ṪFM widths; the ṖK format combines all information about a single character into one character packet. The ĠF run-encoding is on a row-by-row basis, and the ṖK format is on a glyph basis, as if all of the raster rows in the glyph were concatenated into one long row. The encoding of the run-counts in the ĠF files is fixed, whereas the ṖK format uses a dynamic encoding scheme that must be adjusted for each character. And, finally, any repeated rows can be marked and sent with a single command in the ṖK format. There are four major steps in the conversion process. First, the postamble of the |gf_file| is found and read, and the data from the character locators is stored in memory. Next, the preamble of the |pk_file| is written. The third and by far the most difficult step reads the raster representation of all of the characters from the ĠF file, packs them, and writes them to the |pk_file|. Finally, the postamble is written to the |pk_file|. The conversion of the character raster information from the |gf_file| to the format required by the |pk_file| takes several smaller steps. The ĠF file is read, the commands are interpreted, and the run counts are stored in the working |row| array. Each row is terminated by a |end_of_row| value, and the character glyph is terminated by an |end_of_char| value. Then, this representation of the character glyph is scanned to determine the minimum bounding box in which it will fit, correcting the |min_m|, |max_m|, |min_n|, and |max_n| values, and calculating the offset values. The third sub-step is to restructure the row list from a list based on rows to a list based on the entire glyph. Then, an optimal value of |dyn_f| is calculated, and the final size of the counts is found for the ṖK file format, and compared with the bit-wise packed glyph. If the run-encoding scheme is shorter, the character is written to the |pk_file| as row counts; otherwise, it is written using a bit-packed scheme. To save various information while the ĠF file is being loaded, we need several arrays. The |tfm_width|, |dx|, and |dy| arrays store the obvious values. The |status| array contains the current status of the particular character. A value of 0 indicates that the character has never been defined; a 1 indicates that the character locator for that character was read in; and a 2 indicates that the raster information for at least one character was read from the |gf_file| and written to the |pk_file|. The |row| array contains row counts. It is filled anew for each character, and is used as a general workspace. The ĠF counts are stored starting at location 2 in this array, so that the ṖK counts can be written to the same array, overwriting the ĠF counts, without destroying any counts before they are used. (A possible repeat count in the first row might make the first row of the ṖK file one count longer; all succeeding rows are guaranteed to be the same length or shorter because of the |end_of_row| flags in the ĠF format that are unnecessary in the ṖK format.) @d virgin==0 <#266#>never heard of this character yet<#266#> @d located==1 <#267#>locators read for this character<#267#> @d sent==2 <#268#>at least one of these characters has been sent<#268#> @;SPMlt;Glob...@;SPMgt;= @!tfm_width: array[0..255] of integer; <#355#>the ṪFM widths of characters<#355#> @!dx, @!dy: array[0..255] of integer; <#270#>the horizontal and vertical escapements<#270#> @!status: array[0..255] of virgin..sent; <#271#>character status<#271#> @!row: array[0..max_row] of integer; <#272#>the row counts for working<#272#> @ Here we initialize all of the character |status| values to |virgin|. @;SPMlt;Set init...@;SPMgt;= for i := 0 to 255 do status[i] := virgin ; @ And, finally, we need to define the |end_of_row| and |end_of_char| values. These cannot be values that can be taken on either by legitimate run counts, even when wrapping around an entire character. Nor can they be values that repeat counts can take on. Since repeat counts can be arbitrarily large, we restrict ourselves to negative values whose absolute values are greater than the largest possible repeat count. @d end_of_row==(-99999) <#273#>indicates the end of a row<#273#> @d end_of_char==(-99998) <#274#>indicates the end of a character<#274#> @* Reading the generic font file. There are two major procedures in this program that do all of the work. The first is |convert_gf_file|, which interprets the ĠF commands and puts row counts into the |row| array. The second, which we only anticipate at the moment, actually packs the row counts into nybbles and writes them to the packed file. @p @;SPMlt;Packing procedures@;SPMgt; ; procedure convert_gf_file; @!i, @!j, @!k : integer ; <#276#>general purpose indices<#276#> @!gf_com : integer ; <#277#>current gf command<#277#> @;SPMlt;Locals to |convert_gf_file|@;SPMgt; begin open_gf_file ; if gf_byte ;SPMlt;;SPMgt; pre then bad_gf('First byte is not preamble'); @.First byte is not preamble@;SPMgt; if gf_byte ;SPMlt;;SPMgt; gf_id_byte then bad_gf('Identification byte is incorrect'); @.Identification byte incorrect@;SPMgt; @;SPMlt;Find and interpret postamble@;SPMgt; ; move_to_byte(2) ; open_pk_file ; @;SPMlt;Write preamble@;SPMgt; ; repeat gf_com := gf_byte ; case gf_com of boc, boc1 : @;SPMlt;Interpret character@;SPMgt; ; @;SPMlt;Specials and |no_op| cases@;SPMgt; ; post : ; <#278#>we will actually do the work for this one later<#278#> othercases bad_gf('Unexpected ',gf_com:1,' command between characters') @.Unexpected command@;SPMgt; endcases ; until gf_com = post ; @;SPMlt;Write postamble@;SPMgt; ; end ; @ We need a few easy macros to expand some case statements: @d four_cases(#)==#,#+1,#+2,#+3 @d sixteen_cases(#)==four_cases(#),four_cases(#+4),four_cases(#+8), four_cases(#+12) @d sixty_four_cases(#)==sixteen_cases(#),sixteen_cases(#+16), sixteen_cases(#+32),sixteen_cases(#+48) @d one_sixty_five_cases(#)==sixty_four_cases(#),sixty_four_cases(#+64), sixteen_cases(#+128),sixteen_cases(#+144),four_cases(#+160),#+164 @ In this program, all special commands are passed unchanged and any |no_op| bytes are ignored, so we write some code to handle these: @;SPMlt;Specials and |no_op| cases@;SPMgt;= four_cases(xxx1) : begin pk_byte(gf_com - xxx1 + pk_xxx1) ; i := 0 ; for j := 0 to gf_com - xxx1 do begin k := gf_byte ; pk_byte(k) ; i := i * 256 + k ; end ; for j := 1 to i do pk_byte(gf_byte) ; end ; yyy : begin pk_byte(pk_yyy) ; pk_word(gf_signed_quad) ; end ; no_op : @ Now we need the routine that handles the character commands. Again, only a subset of the gf commands are permissible inside character definitions, so we only look for these. @;SPMlt;Interpret character@;SPMgt;= begin if gf_com = boc then begin gf_ch := gf_signed_quad ; i := gf_signed_quad ; <#279#>dispose of back pointer<#279#> min_m := gf_signed_quad ; max_m := gf_signed_quad ; min_n := gf_signed_quad ; max_n := gf_signed_quad ; end else begin gf_ch := gf_byte ; i := gf_byte ; max_m := gf_byte ; min_m := max_m - i ; i := gf_byte ; max_n := gf_byte ; min_n := max_n - i ; end ; d_print_ln('Character ',gf_ch:1) ; if gf_ch;SPMgt;=0 then gf_ch_mod_256 := gf_ch mod 256 else gf_ch_mod_256 := 255-((-(1+gf_ch)) mod 256); if status[gf_ch_mod_256] = virgin then bad_gf('no character locator for character ',gf_ch:1) ; @.no character locator...@;SPMgt; @;SPMlt;Convert character to packed form@;SPMgt; ; @ Communication between the procedures |convert_gf_file| and |pack_and_send_character| is done with a few global variables. @;SPMlt;Glob...@;SPMgt;= @!gf_ch : integer ; <#280#>the character we are working with<#280#> @!gf_ch_mod_256 : integer ; <#281#>locater pointer<#281#> @!pred_pk_loc : integer ; <#282#>where we predict the end of the character to be.<#282#> @!max_n, @!min_n : integer ; <#283#>the maximum and minimum horizontal rows<#283#> @!max_m, @!min_m : integer ; <#284#>the maximum and minimum vertical rows<#284#> @!row_ptr : integer ; <#285#>where we are in the |row| array.<#285#> @ Now we are at the beginning of a character that we need the raster for. Before we get into the complexities of decoding the |paint|, |skip|, and |new_row| commands, let's define a macro that will help us fill up the |row| array. Note that we check that |row_ptr| never exceeds |max_row|; Instead of calling |bad_gf| directly, as this macro is repeated eight times, we simply set the |bad| flag true. @d put_in_rows(#)==begin if row_ptr ;SPMgt; max_row then bad := true else begin row[row_ptr]:=#; incr(row_ptr); end ; end @ Now we have the procedure that decodes the various commands and puts counts into the |row| array. This would be a trivial procedure, except for the |paint_0| command. Because the |paint_0| command exists, it is possible to have a sequence like |paint| 42, |paint_0|, |paint| 38, |paint_0|, |paint_0|, |paint_0|, |paint| 33, |skip_0|. This would be an entirely empty row, but if we left the zeros in the |row| array, it would be difficult to recognize the row as empty. This type of situation probably would never occur in practice, but it is defined by the ĠF format, so we must be able to handle it. The extra code is really quite simple, just difficult to understand; and it does not cut down the speed appreciably. Our goal is this: to collapse sequences like |paint| 42, |paint_0|, |paint| 32 to a single count of 74, and to insure that the last count of a row is a black count rather than a white count. A buffer variable |extra|, and two state flags, |on| and |state|, enable us to accomplish this. The |on| variable is essentially the |paint_switch| described in the ĠF description. If it is true, then we are currently painting black pixels. The |extra| variable holds a count that is about to be placed into the |row| array. We hold it in this array until we get a |paint| command of the opposite color that is greater than 0. If we get a |paint_0| command, then the |state| flag is turned on, indicating that the next count we receive can be added to the |extra| variable as it is the same color. @;SPMlt;Convert character to packed form@;SPMgt;= begin bad := false ; row_ptr := 2 ; on := false ; extra := 0 ; state := true ; repeat gf_com := gf_byte ; case gf_com of @t@;SPMgt;@;SPMlt;Cases for |paint| commands@;SPMgt;; four_cases(skip0) : begin i := 0 ; for j := 1 to gf_com - skip0 do i := i * 256 + gf_byte ; if on = state then put_in_rows(extra) ; for j := 0 to i do put_in_rows(end_of_row) ; on := false ; extra := 0 ; state := true ; end ; one_sixty_five_cases(new_row_0) : begin if on = state then put_in_rows(extra) ; put_in_rows(end_of_row) ; on := true ; extra := gf_com - new_row_0 ; state := false ; end ; @t@;SPMgt;@;SPMlt;Specials and |no_op| cases@;SPMgt; ; eoc : begin if on = state then put_in_rows(extra) ; if ( row_ptr ;SPMgt; 2 ) and ( row[row_ptr - 1] ;SPMlt;;SPMgt; end_of_row) then put_in_rows(end_of_row) ; put_in_rows(end_of_char) ; if bad then abort('Ran out of internal memory for row counts!') ; @.Ran out of memory@;SPMgt; pack_and_send_character ; status[gf_ch_mod_256] := sent ; if pk_loc ;SPMlt;;SPMgt; pred_pk_loc then abort('Internal error while writing character!') ; @.Internal error@;SPMgt; end ; othercases bad_gf('Unexpected ',gf_com:1,' command in character definition') @.Unexpected command@;SPMgt; endcases ; until gf_com = eoc ; @ A few more locals used above and below: @;SPMlt;Locals to |convert_gf_file|@;SPMgt;= @!on : boolean ; <#288#>indicates whether we are white or black<#288#> @!state : boolean ; <#289#>a state variable---is the next count the same race as the one in the |extra| buffer?<#289#> @!extra : integer ; <#290#>where we pool our counts<#290#> @!bad : boolean ; <#291#>did we run out of space?<#291#> @ @;SPMlt;Cases for |paint| commands@;SPMgt;= paint_0 : begin state := not state ; on := not on ; end ; sixty_four_cases(paint_0+1),paint1+1,paint1+2 : begin if gf_com ;SPMlt; paint1 then i := gf_com - paint_0 else begin i := 0 ; for j := 0 to gf_com - paint1 do i := i * 256 + gf_byte ; end ; if state then begin extra := extra + i ; state := false ; end else begin put_in_rows(extra) ; extra := i ; end ; on := not on ; @ Our last remaining task is to interpret the postamble commands. The only things that may appear in the postamble are |post_post|, |char_loc|, |char_loc0|, and the special commands. Note that any special commands that might appear in the postamble are not written to the |pk_file|. Since <#421#>#tex2html_accent_inline785#<#421#><#422#>#tex2html_accent_inline786#<#422#> does not generate special commands in the postamble, this should not be a major difficulty. @;SPMlt;Find and interpret postamble@;SPMgt;= find_gf_length ; post_loc := gf_len - 4 ; repeat if post_loc = 0 then bad_gf('all 223''s'); @.all 223's@;SPMgt; move_to_byte(post_loc); k := gf_byte; decr(post_loc) ; until k ;SPMlt;;SPMgt; 223 ; if k ;SPMlt;;SPMgt; gf_id_byte then bad_gf('ID byte is ',k:1); @.ID byte is wrong@;SPMgt; move_to_byte(post_loc - 3); q := gf_signed_quad ; if (q;SPMlt;0) or (q;SPMgt;post_loc-3) then bad_gf('post pointer is ',q:1) ; @.post pointer is wrong@;SPMgt; move_to_byte(q) ; k := gf_byte ; if k ;SPMlt;;SPMgt; post then bad_gf('byte at ',q:1,' is not post') ; @.byte is not post@;SPMgt; i := gf_signed_quad ; <#292#>skip over junk<#292#> design_size := gf_signed_quad ; check_sum := gf_signed_quad ; hppp := gf_signed_quad ; h_mag := round ( hppp * 72.27 / 65536 ) ; vppp := gf_signed_quad ; if hppp ;SPMlt;;SPMgt; vppp then print_ln('Odd aspect ratio!') ; @.Odd aspect ratio@;SPMgt; i := gf_signed_quad ; i := gf_signed_quad ; <#293#>skip over junk<#293#> i := gf_signed_quad ; i := gf_signed_quad ; repeat gf_com := gf_byte ; case gf_com of char_loc, char_loc0 : begin gf_ch := gf_byte ; if status[gf_ch] ;SPMlt;;SPMgt; virgin then bad_gf('Locator for this character already found.'); @.Locator...already found@;SPMgt; if gf_com = char_loc then begin dx[gf_ch] := gf_signed_quad ; dy[gf_ch] := gf_signed_quad ; end else begin dx[gf_ch] := gf_byte * 65536 ; dy[gf_ch] := 0 ; end ; tfm_width[gf_ch] := gf_signed_quad ; i := gf_signed_quad ; status[gf_ch] := located ; end ; @;SPMlt;Specials and |no_op| cases@;SPMgt; ; post_post : ; othercases bad_gf('Unexpected ',gf_com:1,' in postamble') @.Unexpected command@;SPMgt; endcases ; until gf_com = post_post @ Just a few more locals: @;SPMlt;Locals to |convert_gf_file|@;SPMgt;= @!hppp, @!vppp : integer ; <#294#>horizontal and vertical pixels per point<#294#> @!q : integer ; <#295#>quad temporary<#295#> @!post_loc : integer ; <#296#>where the postamble was<#296#> @* Converting the counts to packed format. This procedure is passed the set of row counts from the ĠF file. It writes the character to the ṖK file. First, the minimum bounding box is determined. Next, the row-oriented count list is converted to a count list based on the entire glyph. Finally, we calculate the optimal |dyn_f| and send the character. @;SPMlt;Packing procedures@;SPMgt;= procedure pack_and_send_character ; var i, @!j, @!k : integer ; <#299#>general indices<#299#> @;SPMlt;Locals to |pack_and_send_character|@;SPMgt; begin @;SPMlt;Scan for bounding box@;SPMgt; ; @;SPMlt;Convert row-list to glyph-list@;SPMgt; ; @;SPMlt;Calculate |dyn_f| and packed size and write character@;SPMgt; ; @ Now we have the row counts in our |row| array. To find the real |max_n|, we look for the first non-|end_of_row| value in the |row|. If it is an |end_of_char|, the entire character is blank. Otherwise, we first eliminate all of the blank rows at the end of the character. Next, for each remaining row, we check the first white count for a new |min_m|, and the total length of the row for a new |max_m|. @;SPMlt;Scan for bounding box@;SPMgt;= i := 2 ; decr(row_ptr) ; while row[i] = end_of_row do incr(i) ; if row[i] ;SPMlt;;SPMgt; end_of_char then begin max_n := max_n - i + 2 ; while row[row_ptr - 2] = end_of_row do begin decr(row_ptr) ; row[row_ptr] := end_of_char ; end ; min_n := max_n + 1 ; extra := max_m - min_m + 1 ; max_m := 0 ; j := i ; while row[j] ;SPMlt;;SPMgt; end_of_char do begin decr(min_n) ; if row[j] ;SPMlt;;SPMgt; end_of_row then begin k := row[j] ; if k ;SPMlt; extra then extra := k ; incr(j) ; while row[j] ;SPMlt;;SPMgt; end_of_row do begin k := k + row[j] ; incr(j) ; end ; if max_m ;SPMlt; k then max_m := k ; end ; incr(j) ; end ; min_m := min_m + extra ; max_m := min_m + max_m - 1 - extra ; height := max_n - min_n + 1 ; width := max_m - min_m + 1 ; x_offset := - min_m ; y_offset := max_n ; d_print_ln('W ',width:1,' H ',height:1,' X ',x_offset:1, ' Y ',y_offset:1); end else begin height := 0 ; width := 0 ; x_offset := 0 ; y_offset := 0 ; d_print_ln('Empty raster.'); @ We must convert the run-count array from a row orientation to a glyph orientation, with repeat counts for repeated rows. We seperate this task into two smaller tasks, on a per row basis. But first, we define a new macro to help us fill up this new array. Here, we have no fear that we will run out of space, as the glyph representation is provably smaller than the rows representation. @d put_count(#)==begin row[put_ptr] := #; incr(put_ptr); if repeat_flag ;SPMgt; 0 then begin row[put_ptr] := - repeat_flag ; repeat_flag := 0 ; incr(put_ptr) ; end ; @;SPMlt;Convert row-list to glyph-list@;SPMgt;= put_ptr := 0 ; row_ptr := 2 ; repeat_flag := 0 ; state := true ; buff := 0 ; while row[row_ptr] = end_of_row do incr(row_ptr) ; while row[row_ptr] ;SPMlt;;SPMgt; end_of_char do begin @;SPMlt;Skip over repeated rows@;SPMgt; ; @;SPMlt;Reformat count list@;SPMgt; ; end ; if buff ;SPMgt; 0 then put_count(buff) ; put_count(end_of_char) @ Some more locals for |pack_and_send_character| used above: @;SPMlt;Locals to |pack_and_send_character|@;SPMgt;= @!extra : integer ; <#300#>little buffer for count values<#300#> @!put_ptr : integer ; <#301#>next location to fill in |row|<#301#> @!repeat_flag : integer ; <#302#>how many times the current row is repeated<#302#> @!h_bit : integer ; <#303#>horizontal bit count for each row<#303#> @!buff : integer ; <#304#>our count accumulator<#304#> @ In this short section of code, we are at the beginning of a new row. We scan forward, looking for repeated rows. If there are any, |repeat_flag| gets the count, and the |row_ptr| points to the beginning of the last of the repeated rows. Two points must be made here. First, we do not count all-black or all-white rows as repeated, as a large ``paint'' count will take care of them, and also there is no black to white or white to black transition in the row where we could insert a repeat count. That is the meaning of the big if statement that conditions this section. Secondly, the |while row[i] = row[j] do| loop is guaranteed to terminate, as | j| ;SPMgt; | i| and the character is terminated by a unique |end_of_char| value. @;SPMlt;Skip over repeated rows@;SPMgt;= i := row_ptr ; if ( row[i] ;SPMlt;;SPMgt; end_of_row ) and ( ( row[i] ;SPMlt;;SPMgt; extra ) or ( row[i+1] ;SPMlt;;SPMgt; width ) ) then begin j := i + 1 ; while row[j-1] ;SPMlt;;SPMgt; end_of_row do incr(j) ; while row[i] = row[j] do begin if row[i] = end_of_row then begin incr(repeat_flag) ; row_ptr := i + 1 ; end ; incr(i) ; incr(j) ; end ; @ Here we actually spit out a row. The routine is somewhat similar to the routine where we actually interpret the ĠF commands in the count buffering. We must make sure to keep track of how many bits have actually been sent, so when we hit the end of a row, we can send a white count for the remaining bits, and possibly add the white count of the next row to it. And, finally, we must not forget to subtract the |extra| white space at the beginning of each row from the first white count. @;SPMlt;Reformat count list@;SPMgt;= if row[row_ptr] ;SPMlt;;SPMgt; end_of_row then row[row_ptr] := row[row_ptr] - extra ; h_bit := 0; while row[row_ptr] ;SPMlt;;SPMgt; end_of_row do begin h_bit := h_bit + row[row_ptr] ; if state then begin buff := buff + row[row_ptr] ; state := false ; end else if row[row_ptr] ;SPMgt; 0 then begin put_count(buff) ; buff := row[row_ptr] ; end else state := true ; incr(row_ptr) ; end ; if h_bit ;SPMlt; width then if state then buff := buff + width - h_bit else begin put_count(buff) ; buff := width - h_bit ; state := true ; end else state := false ; incr(row_ptr) @ Here is another piece of rather intricate code. We determine the smallest size in which we can pack the data, calculating |dyn_f| in the process. To do this, we calculate the size required if |dyn_f| is 0, and put this in |comp_size|. Then, we calculate the changes in the size for each increment of |dyn_f|, and stick these values in the |deriv| array. Finally, we scan through this array and find the final minimum value, which we then use to send the character data. @;SPMlt;Calculate |dyn_f| and packed size and write character@;SPMgt;= for i := 1 to 13 do deriv[i] := 0 ; i := 0 ; first_on := row[i] = 0 ; if first_on then incr(i) ; comp_size := 0 ; while row[i] ;SPMlt;;SPMgt; end_of_char do @;SPMlt;Process count for best |dyn_f| value@;SPMgt; ; b_comp_size := comp_size ; dyn_f := 0 ; for i := 1 to 13 do begin comp_size := comp_size + deriv[i] ; if comp_size ;SPMlt;= b_comp_size then begin b_comp_size := comp_size ; dyn_f := i ; end ; end ; comp_size := (b_comp_size + 1) div 2 ; if (comp_size ;SPMgt; (height * width + 7) div 8) or (height * width = 0) then begin comp_size := (height * width + 7) div 8 ; dyn_f := 14 ; end ; d_print_ln('Best packing is dyn_f of ',dyn_f:1,' with length ' ,comp_size:1); @;SPMlt;Write character preamble@;SPMgt; ; if dyn_f ;SPMlt;;SPMgt; 14 then @;SPMlt;Send compressed format@;SPMgt; else if height ;SPMgt; 0 then @;SPMlt;Send bit map@;SPMgt; @ When we enter this module, we have a count at |row[i]|. First, we add to the |comp_size| the number of nybbles that this count would require, assuming |dyn_f| to be zero. When |dyn_f| is zero, there are no one nybble counts, so we simply choose between two-nybble and extensible counts and add the appropriate value. Next, we take the count value and determine the value of |dyn_f| (if any) that would cause this count to take either more or less nybbles. If a valid value for |dyn_f| exists in this range, we accumulate this change in the |deriv| array. One special case handled here is a repeat count of one. A repeat count of one will never change the length of the raster representation, no matter what |dyn_f| is, because it is always represented by the nybble value 15. @;SPMlt;Process count for best |dyn_f| value@;SPMgt;= begin j := row[i] ; if j = -1 then incr(comp_size) else begin if j ;SPMlt; 0 then begin incr(comp_size) ; j := - j ; end ; if j ;SPMlt; 209 then comp_size := comp_size + 2 else begin k := j - 193 ; while k ;SPMgt;= 16 do begin k := k div 16 ; comp_size := comp_size + 2 ; end ; incr(comp_size) ; end ; if j ;SPMlt; 14 then decr(deriv[j]) else if j ;SPMlt; 209 then incr(deriv[(223 - j) div 15]) else begin k := 16 ; while ( k * 16 ;SPMlt; j + 3 ) do k := k * 16 ; if j-k ;SPMlt;= 192 then deriv[(207-j+k) div 15] := deriv[(207-j+k) div 15] + 2 ; end ; end ; incr(i) ; @ We need a handful of locals: @;SPMlt;Locals to |pack_and_send_character|@;SPMgt;= @!dyn_f : integer ; <#306#>packing value<#306#> @!height, @!width : integer ; <#307#>height and width of character<#307#> @!x_offset, @!y_offset : integer ; <#308#>offsets<#308#> @!deriv : array[1..13] of integer ; <#309#>derivative<#309#> @!b_comp_size : integer ; <#310#>best size<#310#> @!first_on : boolean ; <#311#>indicates that the first bit is on<#311#> @!flag_byte : integer ; <#312#>flag byte for character<#312#> @!state : boolean ; <#313#>state variable<#313#> @!on : boolean ; <#314#>white or black?<#314#> @ Now we write the character preamble information. First we need to determine which of the three formats we should use. @;SPMlt;Write character preamble@;SPMgt;= flag_byte := dyn_f * 16 ; if first_on then flag_byte := flag_byte + 8 ; if (gf_ch ;SPMlt;;SPMgt; gf_ch_mod_256) or (tfm_width[gf_ch_mod_256] ;SPMgt; 16777215) or (tfm_width[gf_ch_mod_256] ;SPMlt; 0) or (dy[gf_ch_mod_256] ;SPMlt;;SPMgt; 0) or (dx[gf_ch_mod_256] ;SPMlt; 0) or (dx[gf_ch_mod_256] mod 65536 ;SPMlt;;SPMgt; 0) or (comp_size ;SPMgt; 196594) or (width ;SPMgt; 65535) or (height ;SPMgt; 65535) or (x_offset ;SPMgt; 32767) or (y_offset ;SPMgt; 32767) or (x_offset ;SPMlt; -32768) or (y_offset ;SPMlt; -32768) then @;SPMlt;Write long character preamble@;SPMgt; else if (dx[gf_ch] ;SPMgt; 16777215) or (width ;SPMgt; 255) or (height ;SPMgt; 255) or (x_offset ;SPMgt; 127) or (y_offset ;SPMgt; 127) or (x_offset ;SPMlt; -128) or (y_offset ;SPMlt; -128) or (comp_size ;SPMgt; 1015) then @;SPMlt;Write two-byte short character preamble@;SPMgt; @;SPMlt;Write one-byte short character preamble@;SPMgt; @ If we must write a long character preamble, we adjust a few parameters, then write the data. @;SPMlt;Write long character preamble@;SPMgt;= begin flag_byte := flag_byte + 7 ; pk_byte(flag_byte) ; comp_size := comp_size + 28 ; pk_word(comp_size) ; pk_word(gf_ch) ; pred_pk_loc := pk_loc + comp_size ; pk_word(tfm_width[gf_ch_mod_256]) ; pk_word(dx[gf_ch_mod_256]) ; pk_word(dy[gf_ch_mod_256]) ; pk_word(width) ; pk_word(height) ; pk_word(x_offset) ; pk_word(y_offset) ; @ Here we write a short short character preamble, with one-byte size parameters. @;SPMlt;Write one-byte short character preamble@;SPMgt;= begin comp_size := comp_size + 8 ; flag_byte := flag_byte + comp_size div 256 ; pk_byte(flag_byte) ; pk_byte(comp_size mod 256) ; pk_byte(gf_ch) ; pred_pk_loc := pk_loc + comp_size ; pk_three_bytes(tfm_width[gf_ch_mod_256]) ; pk_byte(dx[gf_ch_mod_256] div 65536) ; pk_byte(width) ; pk_byte(height) ; pk_byte(x_offset) ; pk_byte(y_offset) ; @ Here we write an extended short character preamble, with two-byte size parameters. @;SPMlt;Write two-byte short character preamble@;SPMgt;= begin comp_size := comp_size + 13 ; flag_byte := flag_byte + comp_size div 65536 + 4 ; pk_byte(flag_byte) ; pk_halfword(comp_size mod 65536) ; pk_byte(gf_ch) ; pred_pk_loc := pk_loc + comp_size ; pk_three_bytes(tfm_width[gf_ch_mod_256]) ; pk_halfword(dx[gf_ch_mod_256] div 65536) ; pk_halfword(width) ; pk_halfword(height) ; pk_halfword(x_offset) ; pk_halfword(y_offset) ; @ At this point, we have decided that the run-encoded format is smaller. (This is almost always the case.) We send out the data, a nybble at a time. @;SPMlt;Send compressed format@;SPMgt;= begin bit_weight := 16 ; max_2 := 208 - 15 * dyn_f ; i := 0 ; if row[i] = 0 then incr(i) ; while row[i] ;SPMlt;;SPMgt; end_of_char do begin j := row[i] ; if j = -1 then pk_nyb(15) else begin if j ;SPMlt; 0 then begin pk_nyb(14) ; j := - j ; end ; if j ;SPMlt;= dyn_f then pk_nyb(j) else if j ;SPMlt;= max_2 then begin j := j - dyn_f - 1 ; pk_nyb(j div 16 + dyn_f + 1) ; pk_nyb(j mod 16) ; end else begin j := j - max_2 + 15 ; k := 16 ; while k ;SPMlt;= j do begin k := k * 16 ; pk_nyb(0) ; end ; while k ;SPMgt; 1 do begin k := k div 16 ; pk_nyb(j div k) ; j := j mod k ; end ; end ; end ; incr(i) ; end ; if bit_weight ;SPMlt;;SPMgt; 16 then pk_byte(output_byte) ; @ This code is for the case where we have decided to send the character raster packed by bits. It uses the bit counts as well, sending eight at a time. Here we have a miniature packed format interpreter, as we must repeat any rows that are repeated. The algorithm to do this was a lot of fun to generate. Can you figure out how it works? @;SPMlt;Send bit map@;SPMgt;= begin buff := 0 ; p_bit := 8 ; i := 1 ; h_bit := width ; on := false ; state := false ; count := row[0] ; repeat_flag := 0 ; while ( row[i] ;SPMlt;;SPMgt; end_of_char ) or state or ( count ;SPMgt; 0 ) do begin if state then begin count := r_count ; i := r_i ; on := r_on ; decr(repeat_flag) ; end else begin r_count := count ; r_i := i ; r_on := on ; end ; @;SPMlt;Send one row by bits@;SPMgt; ; if state and ( repeat_flag = 0 ) then begin count := s_count ; i := s_i ; on := s_on ; state := false ; end else if not state and ( repeat_flag ;SPMgt; 0 ) then begin s_count := count ; s_i := i ; s_on := on ; state := true ; end ; end ; if p_bit ;SPMlt;;SPMgt; 8 then pk_byte(buff) ; @ All of the remaining locals: @;SPMlt;Locals to |pack_and_send_character|@;SPMgt;= @!comp_size : integer ; <#315#>length of the packed representation in bytes<#315#> @!count : integer ; <#316#>number of bits in current state to send<#316#> @!p_bit : integer ; <#317#>what bit are we about to send out?<#317#> @!r_on, @!s_on : boolean ; <#318#>state saving variables<#318#> @!r_count, @!s_count : integer ; <#319#>ditto<#319#> @!r_i, @!s_i : integer ; <#320#>and again.<#320#> @!max_2 : integer ; <#321#>the highest count that fits in two bytes<#321#> @ We make the |power| array global. @;SPMlt;Glob...@;SPMgt;= @!power : array[0..8] of integer ; <#322#>easy powers of two<#322#> @ We initialize the power array. @;SPMlt;Set init...@;SPMgt;= power[0] := 1 ; for i := 1 to 8 do power[i] := power[i-1] + power[i-1] ; @ Here we are at the beginning of a row and simply output the next |width| bits. We break the possibilities up into three cases: we finish a byte but not the row, we finish a row, and we finish neither a row nor a byte. But, first, we insure that we have a |count| value. @;SPMlt;Send one row by bits@;SPMgt;= repeat if count = 0 then begin if row[i] ;SPMlt; 0 then begin if not state then repeat_flag := - row[i] ; incr(i) ; end ; count := row[i] ; incr(i) ; on := not on ; end ; if ( count ;SPMgt;= p_bit ) and ( p_bit ;SPMlt; h_bit ) then begin <#323#> we end a byte, we don't end the row <#323#> if on then buff := buff + power[p_bit] - 1 ; pk_byte(buff) ; buff := 0 ; h_bit := h_bit - p_bit ; count := count - p_bit ; p_bit := 8 ; end else if ( count ;SPMlt; p_bit ) and ( count ;SPMlt; h_bit ) then begin <#324#> we end neither the row nor the byte <#324#> if on then buff := buff + power[p_bit] - power[p_bit - count] ; p_bit := p_bit - count ; h_bit := h_bit - count ; count := 0 ; end else begin <#325#> we end a row and maybe a byte <#325#> if on then buff := buff + power[p_bit] - power[p_bit - h_bit] ; count := count - h_bit ; p_bit := p_bit - h_bit ; h_bit := width ; if p_bit = 0 then begin pk_byte(buff) ; buff := 0 ; p_bit := 8 ; end ; end ; until h_bit = width @ Now we are ready for the routine that writes the preamble of the packed file. @d preamble_comment == 'GFtoPK 2.3 output from ' @d comm_length = 23 <#326#>length of |preamble_comment|<#326#> @d from_length = 6 <#327#>length of its |' from '| part<#327#> @;SPMlt;Write preamble@;SPMgt;= pk_byte(pk_pre) ; pk_byte(pk_id) ; i := gf_byte ; <#328#>get length of introductory comment<#328#> repeat if i=0 then j:=;SPMquot;.;SPMquot;@+else j:=gf_byte; decr(i); <#329#>some people think it's wise to avoid |goto| statements<#329#> until j;SPMlt;;SPMgt;;SPMquot; ;SPMquot;; <#330#>remove leading blanks<#330#> incr(i); <#331#>this many bytes to copy<#331#> if i=0 then k:=comm_length-from_length else k := i+comm_length; if k;SPMgt;255 then pk_byte(255)@+else pk_byte(k); for k := 1 to comm_length do if(i;SPMgt;0)or(k;SPMlt;=comm_length-from_length) then pk_byte(xord[comment[k]]) ; print('''') ; for k := 1 to i do begin if k;SPMgt;1 then j:=gf_byte; print(xchr[j]); if k;SPMlt;256-comm_length then pk_byte(j); end; print_ln('''') ;@/ pk_word(design_size) ; pk_word(check_sum) ; pk_word(hppp) ; pk_word(vppp) @ Of course, we need an array to hold the comment. @;SPMlt;Glob...@;SPMgt;= @!comment : packed array[1..comm_length] of char ; @ @;SPMlt;Set init...@;SPMgt;= comment := preamble_comment ; @ Writing the postamble is even easier. @;SPMlt;Write postamble@;SPMgt;= pk_byte(pk_post) ; while (pk_loc mod 4 ;SPMlt;;SPMgt; 0) do pk_byte(pk_no_op) @ Once we are finished with the ĠF file, we check the status of each character to insure that each character that had a locater also had raster information. @;SPMlt;Check for un-rasterized locaters@;SPMgt;= for i := 0 to 255 do if status[i] = located then print_ln('Character ',i:1,' missing raster information!') @.missing raster information@;SPMgt; @ Finally, the main program. @p begin initialize ; convert_gf_file ; @;SPMlt;Check for un-rasterized locaters@;SPMgt; ; print_ln(gf_len:1,' bytes packed to ',pk_loc:1,' bytes.') ; final_end : end . @ A few more globals. @;SPMlt;Glob...@;SPMgt;= @!check_sum : integer ; <#333#>the checksum of the file<#333#> @!design_size : integer ; <#334#>the design size of the font<#334#> @!h_mag : integer ; <#335#>the pixel magnification in pixels per inch<#335#> @!i : integer ; @* System-dependent changes. This section should be replaced, if necessary, by changes to the program that are necessary to make ĠFtoPK work at a particular installation. It is usually best to design your change file so that all changes to previous sections preserve the section numbering; then everybody's version will be consistent with the printed program. More extensive changes, which introduce new sections, can be inserted here; then only the index itself will get a new section number. @^system dependencies@;SPMgt; @* Index. Pointers to error messages appear here together with the section numbers where each identifier is used.