This post is sequel to Conversion from ttf to type 2 CID font (type 42 base font)
How to write a postscript program that centers a text generated from character set of CID-Keyed
fonts on a page?
Note: CID-Keyed
fonts are Type 0 Composite
fonts which have been converted from truetype
fonts applicable to Indian Languages. Without this conversion, no postscript program can access truetype
fonts.
Thanks in advance.
The CID-keyed font NotoSansTamil-Regular.t42
has been converted from Google's Tamil ttf font.
Refer Post Conversion from ttf to type 2 CID font (type 42 base font) for conversion.
On Windows platform make sure that NotoSansTamil-Regular.t42
file is available in folder D:\cidfonts
(or any other drive of convenience).
On Linux platform, make sure that NotoSansTamil-Regular.t42
file is available in folder ~/cidfonts
.
Write the following Postscript program D:\cidfonts\center.ps
on Windows platform or ~/cidfonts/center.ps
on Linux platform as given below:
%!PS-Adobe-3.0
/CTXT {dup stringwidth pop 3 -1 roll 2 copy lt {sub neg 2 div 4 -1 roll add 3 -1 roll} {pop pop 3 1 roll} ifelse moveto show} bind def % Center text. usage: X Y Width <Hex String> CTX. Example-1: 36 300 500 <Hex String> CTXT and Example-2: 36 300 500 (String) CTXT
/myNoTo {/NotoSansTamil-Regular findfont exch scalefont setfont} bind def % usage: 15 myNoTo
13 myNoTo
% தமிழ் தங்களை வரவேற்கிறது!
0 500 594 <0155017201aa019801a500030163018801a5017f01b101aa018801c20003016901b101cb00aa> CTXT
% Tamil Welcomes You!
0 450 594 <0019001d002a005e00030019004e00120030002200030024001f002f0024005b0012002a0020007a00aa> CTXT
showpage
Issue the following Ghostscript command to execute the postscript program center.ps
.
gswin64c.exe "D:\cidfonts\NotoSansTamil-Regular.t42" "D:\cidfonts\center.ps
(on Windows Platform).gs ~/cidfonts/NotoSansTamil-Regular.t42 ~/cidfonts/center.ps
(on Linux Platform).This will display two strings தமிழ் தங்களை வரவேற்கிறது!
and Tamil Welcomes You!
respectively centered in two subsequent rows of the page.
Centering should work roughly the same regardless of the font's type.
The simple method is to use the stringwidth
operator which returns the x displacement and y displacement for the string according to the current font. But this will only give you one axis for centering. A left-to-right or right-to-left font will return 0 for the y displacement.
/bounds gsave clippath [ pathbbox ] grestore def
/min_x bounds 0 get def
/max_x bounds 2 get def
/span_x max_x min_x sub def
%center along x axis, requires currentpoint to be set to determine y coord
/cshow {
dup stringwidth pop span_x exch sub 2 div min_x add
currentpoint exch pop moveto
show
} def
If you need to center a string along both axes, you'll need to get a bounding box for the string using (string) false charpath flattenpath pathbbox
.