Are you familiar with the PostScript code you're using? Or is it just blindly copied and pasted from someplace? If you want to understand it, you should google for "PostScript Language Reference" or "Red Book" or "PLRM". These resources are available as PDFs from Adobe.
Your PostScript snippet uses the following steps:
(test)
places the string "test" on the top of the stack.
dup
duplicates the topmost item on the stack. (You'll now have the string twice on the stack.)
stringwidth
. After this operator is executed, the topmost "test" string will have been consumed, and two values will have been added to the stack instead: the string's height (topmost) and the string's width (second from top). [Update: Actually, "string's height" isn't entirely correct -- it's rather the vertical offset of the current point after finishing to draw the string...]
- Next, you use
pop
. This simply deletes the topmost value on the stack. Now only string's width remains on the top of the stack.
2 div
divides that value by 2 and leaves the result (half the stringwidth).
neg
negates the topmost value on the stack. Now that negative value is topmost on the stack.
0
places the value "0" on top of the stack.
rmoveto
then consumes the two topmost values on the stack and moves the current point by that distance (half the string's width) to the left.
show
consumes the first "test" string that remained all the time at the bottom of the stack and "shows" it.
So what would work to take into account the string's height? Try as your last line:
200 700 moveto (test) dup true charpath pathbbox 3 -1 roll sub 2 div neg 3 1 roll sub 2 div exch 200 700 moveto rmoveto show"
To understand my changes look up the meaning of charpath
, div
, exch
, pathbbox
, roll
and sub
operators in The Red Book.
This command uses Ghostscript to create a PDF file on Windows from the code (easier to view and check results):
gswin32c.exe ^
-o my.pdf ^
-sDEVICE=pdfwrite ^
-c "/Helvetic-Oblique findfont 10 scalefont setfont 200 700 1 0 360 arc fill 0 0 moveto (test test) dup true charpath pathbbox 3 -1 roll sub 2 div neg 3 1 roll sub 2 div exch 200 700 moveto rmoveto show"
On Linux use:
gs
-o my.pdf
-sDEVICE=pdfwrite
-c "/Helvetic-Oblique findfont 10 scalefont setfont 200 700 1 0 360 arc fill 0 0 moveto (test test) dup true charpath pathbbox 3 -1 roll sub 2 div neg 3 1 roll sub 2 div exch 200 700 moveto rmoveto show"
Better readable forms are:
gswin32c ^
-o my.pdf ^
-sDEVICE=pdfwrite ^
-c "/Helvetic-Oblique findfont 10 scalefont setfont" ^
-c "200 700 1 0 360 arc fill 0 0 moveto (test test) dup" ^
-c "true charpath pathbbox 3 -1 roll sub 2 div neg 3 1 roll" ^
-c "sub 2 div exch 200 700 moveto rmoveto show"
and
gs
-o my.pdf
-sDEVICE=pdfwrite
-c "/Helvetic-Oblique findfont 10 scalefont setfont"
-c "200 700 1 0 360 arc fill 0 0 moveto (test test) dup"
-c "true charpath pathbbox 3 -1 roll sub 2 div neg 3 1 roll"
-c "sub 2 div exch 200 700 moveto rmoveto show"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…