Skip to content

Latest commit

 

History

History
106 lines (77 loc) · 5.42 KB

ps-fonts.md

File metadata and controls

106 lines (77 loc) · 5.42 KB

PostScript Fonts

PostScript fonts come in two flavors: Early, and Level 3.

In PostScript Level 3, you have more flexibility and control over font rendering than in previous versions, largely due to improved handling of fonts including Type 1 smooth shading and enhanced memory management, which is crucial for handling complex font designs.

Creating a Simple PostScript Level 3 Font

To illustrate how to define a sample font in PostScript, we can create a simple custom font based on vector paths for each character. Let's create a basic font with just a few characters (A, B, and C) to demonstrate the process.

Step 1: Define the Font Dictionary

First, we define a font dictionary that specifies the font's properties and the character procedures. Each character will be drawn using its own procedure that defines its shape.

%!
/MySimpleFont 25 dict def  % Create a dictionary for the font
MySimpleFont begin
  /FontType 3 def  % Type 3 font
  /FontMatrix [1 0 0 1 0 0] def  % Transformation matrix for the font
  /FontBBox [0 0 100 100] def  % Bounding box in which all glyphs fit
  /Encoding 256 array def  % Define the encoding array

  % Define character mappings
  Encoding 65 /A put
  Encoding 66 /B put
  Encoding 67 /C put

  % Define each character's drawing procedure
  /CharProcs 3 dict def
  CharProcs begin
    /A {newpath 10 0 moveto 50 100 lineto 90 0 lineto closepath} def
    /B {newpath 10 0 moveto 10 100 lineto 50 100 50 50 50 0 curveto closepath} def
    /C {newpath 100 75 moveto 50 100 25 75 25 25 curveto 25 25 50 0 100 25 curveto closepath} def
  end

  % Define how each character is built
  /BuildGlyph {
    exch begin
    CharProcs exch get exec
    end
  } def

  % Define how to show each character
  /BuildChar {
    1 0 0 1 0 0 setfontmatrix
    BuildGlyph
  } def
end

Step 2: Create a Test Page to Use the Font

Once the font is defined, we can use it within a document to display text. The following script sets up a page and writes "ABC" using MySimpleFont.

/MySimpleFont findfont 25 scalefont setfont  % Select the font and scale it
50 50 moveto  % Move to starting position on the page
(A) show
70 50 moveto  % Adjust position for next character
(B) show
90 50 moveto  % Adjust position for next character
(C) show
showpage  % Render the page

This code snippet sets up a very basic custom font where each character is a simple geometric shape. More complex glyphs would require more detailed path definitions and possibly hinting instructions for proper rendering at smaller sizes. By experimenting with the CharProcs, you can define any vector shape to represent your characters. This flexibility makes Type 3 fonts powerful tools for custom graphics and specialized typesetting tasks in PostScript.

For non-Level 3 fonts in PostScript, one of the most common types used is the Type 1 font. Type 1 fonts are outline fonts that are not as flexible as Type 3 fonts in terms of custom operations, but they are widely used because of their efficiency and support across various platforms and printers.

Creating a Simple Type 1 Font Example

Below, I will demonstrate how to set up a simple Type 1 font using standard font definitions available within PostScript interpreters. Unlike Type 3 fonts, where you can define the shape of each character directly in PostScript, Type 1 fonts typically come pre-defined and are simply invoked and used.

Step 1: Select and Set Up the Font

You typically select a Type 1 font by its name from the fonts available in the PostScript environment. Let’s use the well-known "Helvetica" for demonstration. This example will show how to use the font to print text on a page.

%!
/Helvetica findfont  % Find the Helvetica font
24 scalefont  % Scale the font to 24 points
setfont  % Set the current font to the scaled font

100 700 moveto  % Move to position on the page (x=100, y=700)
(Hello, this is Helvetica!) show  % Display the text

showpage  % Render the page

Explanation

  1. Font Selection: The /Helvetica findfont command searches for "Helvetica" in the font dictionary available to the PostScript interpreter.
  2. Scaling: The 24 scalefont command adjusts the font size to 24 points. This transformation scales the font metrics accordingly.
  3. Setting the Font: setfont applies the scaled font as the current font.
  4. Positioning and Showing Text: The moveto command sets the starting point for the text, and show outputs the string "Hello, this is Helvetica!" at the specified location.
  5. Rendering: The showpage command flushes the page to the output device, rendering the text.

Notes on Usage

  • Type 1 fonts are primarily used for text rather than for creating custom graphics or symbols (which is more flexible with Type 3 fonts).
  • These fonts are standardized and optimized for performance across different output devices, making them a preferred choice for text in professional printing and desktop publishing.
  • Modifying or creating custom Type 1 fonts generally requires specialized font editing software and a deep understanding of the font hinting, which goes beyond typical PostScript programming.

Using Type 1 fonts involves significantly less direct manipulation of font properties within PostScript code compared to Type 3 fonts. The focus with Type 1 fonts in PostScript is more on text layout, font selection, and simple transformations like scaling and rotating rather than on defining glyph shapes directly.