The making of a beautiful book... and how to survive it!

OpenType features in ConTeXt for the impatient

March 27, 2009 14:57
If you only would like to see how some OpenType features work before you write your typescript, you can use \font command. Start with "\definefontfeature" just like in a typescript. It does not exactly define anything new, rather gives a name to an OTF feature, so you can later access it in ConTeXt. You use it like this:
\definefontfeature[smallcaps][language=DFLT,script=latn,smcp=yes]
You could have found beforehand that e.g. Iwona-Regular does indeed have this feature by running:
mtxrun --script font --list --info --pattern=iwonabook
which would give you among other these three lines:
MtxRun | feature: smcp, script: cyrl, language: dflt
MtxRun | feature: smcp, script: grek, language: dflt
MtxRun | feature: smcp, script: latn, language: aze  crt  deu  mol  rom  srb  trk  dflt
ConTeXt now knows that it has to turn on "smcp" feature, when it sees "smallcaps" attached to a font name with an asterisk. Quick and simple font definition looks like this:
\font\iwonasmallcaps=Iwona-Regular*smallcaps
Now you can use "\iwonasmallcaps" in your text to typeset in iwona small caps. Now try if other features work. But remember, this low level approach is not encouraged by ConTeXt magazine #12
6 comment(s)

OpenType features in ConTeXt typescripts

March 28, 2009 23:18
When you start playing with ConTeXt there are few problems as intimidating as font installation, which means in practice, use of typescripts. Typescripts are needed, because ConTeXt needs them to find fonts. But typescripts can drive you crazy, because they greatly increase chances that you mess up something by a stupid typo. If you want to play with OpenType features you have to define them as ConTeXt features. You can of course use the predefined ones like "smallcaps", "oldstyle", "arabic", "virtualmath". See their definitions in font-ini.mkiv, in your ConTeXt source.
Let's try to write a typescript for Diavlo font by Jos Buivenga. You can see that it has "calt","dlig", "frac","kern","liga","onum" and "tnum" features when you run:
mtxrun --script font --list --info diavloblackblack
Why not define some of them as "myfeatures"?
\definefontfeature[myfeatures][language=DFLT,script=latn,kern=yes,liga=yes,onum=yes,tlig=yes,trep=yes]
The last two features "tlig" and "trep" did not appear in the above list. So where do they come from? From ConTeXt source (isn't it interesting?). They define traditional TeX ligatures and replacements (like double hyphen to en dash). See file "font-otc.lua" for details. You do not need it if you use proper unicode typographic signs. For italic let's use (for God's sake not in real life, only in this example!) Iwona, but will choose only one of its numerous features.
\definefontfeature[myitfeatures][language=DFLT,script=latn,kern=yes,tlig=yes,trep=yes]
The first typescript usually takes care of coupling font's filename, or, as in this case it's name reported by "mtxrun --list" in the second column, with its symbolic name:
\starttypescript[all][mydiavlo]
    \definefontsynonym [Diavlo-Bold]   [name:DiavloBold-Regular]   [features=default]
    \definefontsynonym [Iwona-BoldItalic] [name:Iwona-BoldItalic] [features=myitfeatures]
    \definefontsynonym [Diavlo-Regular]   [name:DiavloMedium-Regular]   [features=myfeatures]
    \definefontsynonym [Iwona-Italic]  [name:Iwona-Italic]  [features=myitfeatures]
\stoptypescript
They are also needed, because one of the good things about ConTeXt is you can use standardized commands to access fonts weights and styles. This is why the second part is necessary. You couple here the fontnames you used above to system names like "serif" or "italic"
\starttypescript [all] [mydiavlo]

    \definefontsynonym [Serif]            [Diavlo-Regular] [features=myfeatures]
    \definefontsynonym [SerifItalic]      [Iwona-Italic]  [features=myitfeatures]
    \definefontsynonym [SerifBold]        [Diavlo-Bold] [features=myfeatures]
    \definefontsynonym [SerifBoldItalic]  [Iwona-BoldItalic]  [features=myitfeatures]

\stoptypescript
And finally you pack the font styles declared above into one typeface which you can summon in \setupbodyfont command
\definetypeface[mydiavlo][rm][serif][mydiavlo][default]
Now it's enough to say:
\usetypescript[mydiavlo]
\setupbodyfont[mydiavlo]
There is a simple way to map OpenType features to font variants which you can access using \Var command. More details here. The idea is that you append to standard font synonyms a postfix, which you later register using \definefontvariant command
\definefontvariant  [Serif][osf][OsF]
There are already plenty of documents about fonts in ConTeXt. The ultimate reference now are the new manual chapters on fonts (section 1.5) and on typography (1.8.3, 1.10, 1.14.6)
59 comment(s)

Remote git repository

April 03, 2009 20:42
Strange that there are so many different tutorials on the web about such a simple thing as setting up a remote git repository. Here is what I did thanks to Hans Fugal's. notes. You need to set up ssh beforehand. First initialize local repository:
git init
git add .
git commit -m 'initial import'
Then on the server:
 git --git-dir=repo.git init --bare
Local machine:
cd local.repo
git remote add --mirror server.name ssh://server.com/repo.git
Do some work, add, commit.
git push server.name

Duotones without Photoshop

June 24, 2009 10:51
Fortunately there are already only few lacunas where linux software cannot offer what is available on other systems. One of most important (at least for me) was lack of of simple solutions for duotone creation. Quite essential, if you are doing prepress work. Though there are no ready-made tools (I mean real duotones, not sepia coloring) that does not mean it is impossible. My first successful essay was accomplished by preparing by hand two greyscale images, one containing more shadows, the other more midtones using venerable xv3.10 (but gimp or any other photo editor will do), then converting to grayscale and combining them with imagemagick (version 6.5.3-10). Here is the command line:
convert shadows.tif -negate black.tif
convert midtones.tif -negate cyan.tif
convert cyan.tif black.tif -set colorspace CMYK -channel cyan,black -combine duotone.tif
As you can see this one is using cyan and black, but you can ask your printer to use any ink you want instead of cyan obviously. This is just a first take, so expect some improvements. Thanks to messages of fmw42 and Anthony Thyssen the previous example can be replaced by a one-liner:
convert \( shadows.tif  midtones.tif -negate \) -set colorspace CMYK -channel cyan,black -combine duotone.tif
and you can even try to generate shadows.tif and midtones.tif in one go. This is my first attempt (replace rose: by your image, of course).
convert \( \( rose: -modulate 250 \)  \( rose: -modulate 90 \) -negate \) -set colorspace CMYK -channel cyan,black -combine rose-duotone.tif 
If you are using ConTeXt it might be more useful to have a pdf, just change the output file extension from "tif" to "pdf".

PDFClown "Hello World" App

July 13, 2009 10:51

import it.stefanochizzolini.clown.documents.Document;
import it.stefanochizzolini.clown.documents.Page;
import it.stefanochizzolini.clown.documents.contents.composition.PrimitiveFilter;
import it.stefanochizzolini.clown.documents.contents.fonts.StandardType1Font;
import it.stefanochizzolini.clown.files.File;
import it.stefanochizzolini.clown.files.SerializationModeEnum;
import java.awt.geom.Point2D;


class FirstDoc{
    
    public static void main(String[] args) {
	
	File file = new File();
	Document document = file.getDocument();
	Page page = new Page(document);
	document.getPages().add(page); 
	PrimitiveFilter builder = new PrimitiveFilter(page);
	builder.setFont(
			new StandardType1Font(
					      document,
					      StandardType1Font.FamilyNameEnum.Courier,
					      true,
					      false
					      ),
			32
			);
	
	builder.showText(
			 "Hello World!",
			 new Point2D.Double(32,48)
			 );
	builder.flush();
	file.writeTo(
		     new java.io.File("MyFile.pdf"),
		     SerializationModeEnum.Standard
		     );
    }
}



PDF by hand tutorial

September 08, 2009 09:30
It is certainly not a terribly wise thing to try to write PDF files by hand, but people are sometimes (often?) attracted by foolish ideas to the point they cannot resist them, although they may be perfectly sane otherwise. If you feel that inexplicable urge it is quite possible you might find these notes helpful. Or should I say harmful? As, instead of making you abandon the silly desire after your first, inevitably futile, exercise, it can indeed lead you further down the road you should not tread. Although, this is called a tutorial it does not mean it is written by someone with any authority to teach. It is just a transcript of my own endeavour to tame the beast. If indeed, you would like to try some of these examples do at your own risk. No guarantee they may be safe to use or useful indeed for anyone else. You will need some tools. Pdftk, vim, pdftk plugin for vim and xpdf will do for now but I suggest qpdf. Read the manual and next time use -qdf. For now, just write down your first "Hello World" file (based on the "H.3. Simple Text String Example" in PDF Reference), which will describe in a portable way how to place these two immortal words on a page and try to understand what you put down on (electronic) paper. So no copy and paste yet! It will not work ;)
%PDF-1.4

1 0 obj
	<< /Type /Catalog
	/Pages 2 0 R
	>>
endobj

2 0 obj
	<< /Type /Pages
	/Kids [3 0 R]
	/Count 1
	>>
endobj

3 0 obj
	<< /Type /Page
	/Parent 2 0 R
	/MediaBox [0 0 300 400]
	/Contents 4 0 R
	/Resources	<< /ProcSet 5 0 R
			/Font << /F1 6 0 R >>
			>>
	>>
endobj

4 0 obj
	<< /Length 0 >>
	stream
		BT
			/F1 24 Tf
			100 100 Td
			(Hello World) Tj
		ET
	endstream
endobj

5 0 obj
	[/PDF /Text]
endobj

6 0 obj
	<< /Type /Font
	/Subtype /Type1
	/Name /F1
	/BaseFont /Helvetica
	/Encoding /MacRomanEncoding
	>>
endobj

trailer
	<<
	   /Root 1 0 R
	>>

%%EOF
Now save your file. But before you open it in xpdf to take a look at the glorious result let's try to explain what you have just faithfully copied. I bet you understand quite a lot already without lengthy explanations.
Naturally we need some introduction, a header.
%PDF-1.4
As we are going to describe our page in a fairly detailed way, we need to impose some order on our information. What about a hierarchical order? After all a document consists usually of pages, and pages contain other pieces of information. Interestingly in a PDF file you start from the end. We skipped the two lines which usually appear before the end-of-file marker. In our case the trailer will contain the dictionary with a reference to the Catalog object which is the root of the document structure.
trailer
	<<
	   /Root 1 0 R
	>>
The "R" means it is a reference to object 1 0. You will see it often.

1 0 obj
	<< /Type /Catalog
	/Pages 2 0 R
	>>
endobj
The Catalog object sends you to Pages object which contains a reference to individual Page objects, just one in this case: 3 0.
3 0 obj
	<< /Type /Page
	/Parent 2 0 R
	/MediaBox [0 0 300 400]
	/Contents 4 0 R
	/Resources	<< /ProcSet 5 0 R
			/Font << /F1 6 0 R >>
			>>
	>>
endobj
And here we got a detailed description of what has to be done on the page. First, there is the /Parent key which sends us back to Pages object. /MediaBox contains the size of mediabox. More interesting are /Contents and /Resources keys. The Contents object (4 0 obj) contains a stream of instructions used to print out "Hello World". BT and ET open and close text object, Tf sets the font to /F1 and font size to 24, Td gives location, Tj does actually output the text.
4 0 obj
	<< /Length 0 >>
	stream
		BT
			/F1 24 Tf
			100 100 Td
			(Hello World) Tj
		ET
	endstream
endobj
Of the two remaining objects the second is more interesting now, as it contains a font definition.
5 0 obj
	[/PDF /Text]
endobj

6 0 obj
	<< /Type /Font
	/Subtype /Type1
	/Name /F1
	/BaseFont /Helvetica
	/Encoding /MacRomanEncoding
	>>
endobj
Now, you can easily figure out how to add another page. Simply change the number of pages in the /Pages object by increasing /Count to 2 and adding reference to a new page object in the /Kids array.

2 0 obj
	<< /Type /Pages
	/Kids [3 0 R
	       7 0 R	      
		   ]
	/Count 2
	>>
endobj
and add that Kid anywhere you want (except inside another object) !


8 0 obj
	<< /Length 0 >>
	stream
		BT
			/F1 64 Tf
			100 100 Td
			(Hi!) Tj
		ET
	endstream
endobj
What about adding some more contents? You can edit the stream or you can add another object and instead of a single reference in a /Page object pass an array as an argument to /Contents.

7 0 obj

	<< /Type /Page
	/Parent 2 0 R
	/MediaBox [0 0 300 400]
	/Contents [8 0 R
		  9 0 R]
	/Resources	<< /ProcSet 5 0 R
			/Font << /F1 6 0 R >>
			>>
	>>
endobj

By now you probably noticed that I was cheating. But I hope you forgive me that innocent lie. I guess you tried to open PDF files in your text editor before, and they looked, hmmm, a bit less readable. First there were four strange characters right after the header.
%PDF-1.4
%âăĎÓ
That's it. They are there to tell file transfer applications that a PDF file contains binary data, "as mostly do". PDF format is geared towards saving contents as binary data. We brushed swiftly past /Length instruction, as it is not usual for humans to give byte length of a sequence of instructions in your computer program. But when you use streams which usually hold binary data, and you cannot really abstain from streams, you are supposed to give their length. Xpdf is fortunately quite forgiving and displays your file even if you don't but other viewers don't have to.
Actually there is one more component which is indispensable in a valid PDF file. It is the Cross-Reference Table. As the PDF spec says: "The cross-reference table contains information that permits random access to indirect objects within the file so that the entire file need not be read to locate any particular object." Well, I'm not going to persuade you to write Cross-Reference Tables by hand. Fortunately in most cases pdftk can do it for you (if you haven't used qpdf). And more than that! It can also fix other errors like e.g. wrong string lengths (noticed "/Lenght 0" above? It was wrong, of course, and has to be fixed). Just do:
pdftk file-before-fixing.pdf output file-after-fixing.pdf
I recommend using qpdf and qdf form when writing, as this is the right tool to convert hand-written files into proper PDF.

Adding index (or rather register) entries in ConTeXt

September 24, 2009 08:01
Index is called "register" in ConTeXt. But there is a command \index available. Why? Because "index" register is defined by default. If you need just one that's fine. If not define your own. The arguments are just the new register's names in singular and in plural.
\defineregister[myindex][myindices]
Now you add entries with \myindex, and typeset index with:
\placemyindex
or
\completemyindex
which is usually not terribly useful as it will typeset the name of the register as its title (in this case "myindex"). Of course, if you use the default \index command you write \placeindex and \completeindex. There is one thing which, almost by default, needs to be setup. If you want page ranges instead of single numbers of consecutive pages in your index you add:
\placeindex[compress=yes] 
It is possible to get subentries and subsubentries with:

\index{entry+subentry}
\index{entry+subentry+subsubentry}
Some flexibility in formatting of the index entries is provided by the fact that \index argument can be a command, but then you need to provide a text-only version of the entry in brackets to get proper sorting.
\index[new entry]{\it new entry}
It's only the beginning, however. For more in-depth tuning use \setupregister.

i18n and collation in XSLT

October 24, 2009 22:17
It's quite amazing I was never bothered by sort order before. Well, until today. To my dismay I noticed that almost all XSLT processors I was using, including my favourite xsltproc, do not honour LANG attribute of the SORT element. To my surprise I had to revert to Saxon to sort Polish names using lang=pl right. But not Saxon alone. You need also the Innodata Isogen Internationalization (I18N) support library. Just unpack and point to the jar on the command line like this:
java -Dcom.innodata.i18n.home=/path-to/ISOGEN/i18n_support/ -cp /path-to/ISOGEN/i18n_support/i18n_support.jar com.icl.saxon.StyleSheet -o out.xml in.xml stylesheet.xsl

OpenType fonts in ConTeXt

June 06, 2010 09:36
Be warned I'm talking about Mark IV. I'm using ConTeXt Minimals installed on Debian-ppc. First of all, install the fonts. The way it used to work for me now was:
  1. Find out where luatex expects to find Open Type fonts (Already obsolete if you are using up-to-date ConTeXt installation e.g. recent Minimals. Go straight to next point.) :
    kpsewhich --show-path=.otf
    Copy the font to one of the directories on the path shown by the above command.
  2. Or even better use your system fonts. I install fonts on my system with kfontview which copies them to "/usr/local/share/fonts/" (now I started to use fontmatrix which is way more powerful and displays OpenType features). Now it's enough to
    export OSFONTDIR=/usr/local/share/fonts/
  3. Run:
    luatools --generate  
    mtxrun --script font --reload
    
    I once had problems with reloading fonts, which were solved by
    luatools --selfupdate
    All this should not be necessary now.
See also the article on contextgraden.net. Now you probably would like to find out the OpenType features of your fonts. There is a ConTeXt lua snippet you can put in your ConTeXt file and run with luatex that will print the features to a file but you can simply use mtxrun. First list the fonts:
mtxrun --script font --list
Then, take the fontname from the leftmost column and use it to limit the output, this time using the "--info" switch e.g.:
mtxrun --script font --list --info  --pattern=antykwatorunskamediumbook
To use the font in your ConTeXt file you will have to create a typescript file. I will write more about typescripts soon.
I think it's wonderful you actually have to stop for a moment and devote some time to prepare one (although you CAN use predefined ones that come with fonts installed with ConTeXt, like the Antykwa Toruńska in the above example). It's like collecting right brushes before you set off to paint a masterpiece. Now you can find out how to actually start using OpenType features in Mark IV.
© 2008 kle dodajdo