PostScript text formatting, shamelessly ripped from the web (http://www.mactech.com/articles/mactech/Vol.15/15.09/PDFIntro/)
Object 3, which contains the contents of page one of our document, is worth commenting on since it shows how text streams are used in PDF. The object looks like:
3 0 obj
<<
/Length 168
>>
stream
BT
/F4 1 Tf
12 0 0 12 50.64 731.52 Tm
0 0 0 rg
BX /GS2 gs EX
0 Tc
0 Tw
@charspc = charspc
[(This is 12-point )10(T)41(imes. )
18(This sentence will appear near
the top of page one.)]TJ
ET
endstream
endobj
The stream object (which is 168 bytes long) is bracketed by BT and ET operators, for Begin Text and End Text. The Tf command selects our font and its size in user-space units, which is given as 1. "But aren't we using 12-point type?" you may be wondering. Yes, we are. That's specified in the next line, ending in Tm (which is the set-text-matrix operator). For space reasons, we won't say much about coordinate system transformations and matrices here, but if you're familiar with the use of matrices in PostScript, the same rules apply in PDF. A transform matrix is given by an array of six numbers, the first and fourth of which determine scaling in x and y, respectively. We see in our text matrix, the scaling factor is 12. That means we will use 12-point type. The last two numbers in the matrix (50.64 and 731.52) specify a translation, in user-space units. The effect of the translation is to put our text approximately 10.1 inches high on the page, with a left margin of 0.7 inch.
The line ending with rg sets our ink color to an RGB value of 0 0 0, or black. The BX operator says that we are beginning a section that allows undefined operators. In this section, we apply the gs operator (which sets parameters in the extended graphics state), using /GS2 as our EGS specifications. The EX operator ends the section allowing undefined operators. In essence, we're saying "Any reading application that understands what's in this special section can execute the instructions contained there, but if you don't understand the instructions, just go on." The reason this section has to be handled this way is that extended graphics state instructions often contain device-dependent instructions. The lack of generality means we should bracket those instructions with BX/EX.
The Tc and Tw operators are for setting character spacing and word spacing, respectively.
Finally, we come to the text that will be displayed on our page. Oddly enough, it's specified in an array of text snippets interspersed with integers, such as:
(This is 12-point )10(T)41(imes. )
The number 10 represents a kerning value, in thousandths of an em. (An em is a typographical unit of measurement equal to the size of the font.) This number is subtracted from the 'x' coordinate of the letter(s) that follow, displacing the text to the left. The capital 'T' is displaced 10 units to the left, while "imes. " is displaced 41 units. The TJ at the end of the array is the operator for "show text, allowing individual character spacing."
Finally, ET closes off the text block, and endstream closes off the stream.
b closepath, fill,and stroke path.
B fill and stroke path.
b* closepath, eofill,and stroke path.
B* eofill and stroke path.
BI begin image.
BMC begin marked content.
BT begin text object.
BX begin section allowing undefined operators.
c curveto.
cm concat. Concatenates the matrix to the current transform.
cs setcolorspace for fill.
CS setcolorspace for stroke.
d setdash.
Do execute the named XObject.
DP mark a place in the content stream, with a dictionary.
EI end image.
EMC end marked content.
ET end text object.
EX end section that allows undefined operators.
f fill path.
f* eofill Even/odd fill path.
g setgray (fill).
G setgray (stroke).
gs set parameters in the extended graphics state.
h closepath.
i setflat.
ID begin image data.
j setlinejoin.
J setlinecap.
k setcmykcolor (fill).
K setcmykcolor (stroke).
l lineto.
m moveto.
M setmiterlimit.
n end path without fill or stroke.
q save graphics state.
Q restore graphics state.
re rectangle.
rg setrgbcolor (fill).
RG setrgbcolor (stroke).
s closepath and stroke path.
S stroke path.
sc setcolor (fill).
SC setcolor (stroke).
sh shfill (shaded fill).
Tc set character spacing.
Td move text current point.
TD move text current point and set leading.
Tf set font name and size.
Tj show text.
TJ show text, allowing individual character positioning.