COMP 15 Project #2: PostScript graphics

COMP15 Fall 20XX
Due: Thursday, September 30, 11:00PM

Overview

Some kinds of information are best communicated visually using computer graphics. Pictures, diagrams, etc. can be rendered to a wide variety of devices such as display screens and printers. PostScript is an industry-standard, device independent page description language. A PostScript program describes what to draw on one or more pages: text, images, lines, arcs, etc. The PostScript language provides a flexible means to control the placement, orientation, scale and attributes (grey scale, fill pattern) of the primitive graphic forms that it supports.

Writing PostScript primitives is a tedious and error prone process. This project will develop a reusable module that hides some of the details of the PostScript language in order to make graphics programming easier. The module virtualizes the concept of a PostScript file and exports an application programming interface (API) that is similar to a simple graphics device. (The API is an abstraction of a simple graphics device.)

Modules wishing to use the API must include the declaration of the module interface and make calls through the API. A calling module performs the following sequence of operations to draw a picture:

  1. Defines a PostscriptFile object. Definition of the object invokes a constructor function that creates a PostScript file.
  2. Calls one or more method functions (e.g., line, rect, circle) to describe graphical marks to be drawn on the current page. The method functions write one or more PostScript primitives to the file in order to describe those marks.
  3. Calls the showpage method function to indicate that the current page description is complete. The showpage method writes the PostScript primitive of the same name to the file. When the file is interpreted, the marks on the page are rendered by the actual graphics device.
  4. Repeats steps 2 and 3 above for each page to be drawn.
  5. Invokes the destructor function when finished. The destructor closes the PostScript file.
The PostscriptFile API exposes the PostScript coordinate space (origin at the lower left corner of the page, 72 points per inch), transformations on coordinates and the rest of the PostScript graphics state.

The user may preview the finished PostScript file using the Ghostscript previewer (gs) or print the file to get hard-copy.

Objectives

The objectives for this project are:

This module will be reused in the third and eighth assignments. You may also choose to use this module to draw histograms when implementing the ninth project on discrete event simulation.

What needs to be done

Two files are provided: psfile.h, which is the declaration of the PostscriptFile class and its interface, and psfile.cpp, which is the definition of the PostscriptFile class and the function main. These two files will help get you started with the project. You need to copy these files from the directory /g/15/class/project2 to your own working directory and read through the existing implementation to get more information.

The existing implementation is incomplete. You will need to add new method functions to psfile.cpp to fully implement the interface. An informal interface specification is given below. Here are some additional requirements to be satisfied:

You should test as thoroughly as possible, but avoid exhaustive testing. Identify and test any special cases. Add comments to the source code to describe your approach and solution. Don't forget to fill out the header comments with your name, section and e-mail address.

Extra credit

Once you have completed the basic assignment, you can try adding more PostScript primitives. The specified set is, errrr, pretty basic. By the way, you will need to draw text for the eighth assignment. Of course, you must test what you build. Extra challenges are always above the call of duty and are for extra credit.

On-line resources

Be sure to read the on-line tutorials on PostScript and C++ file input/output. Just take the links from the COMP15 schedule page. The PostScript write-up has links to the Adobe PostScript tutorial and reference manuals (the "blue" and "red books.")

Submitting your work

Use the provide system to submit the finished program:

    provide comp15 a2 psfile.h psfile.cpp
Be sure you are completely satisfied with your work before submitting!

Informal interface specification

Function: PostscriptFile(char *filename) ;
  Purpose: Initializes a new PostscriptFile object; Opens the specified
    file for writing
  Parameters:
    filename: Name of the file to create and open (pointer to C string)
  Returns:
    Nothing
  Error conditions:
    Cannot create (open) file

Function: ~PostscriptFile() ;
  Purpose: Closes the output file before the PostscriptFile object
    is destroyed
  Parameters:
    None
  Returns:
    Nothing

Function initgraphics() ;
  Purpose: Emits the PostScript initgraphics primitive; "Emit" is a
    fancy word for "write"
  Parameters:
    None
  Returns:
    Nothing

Function: void showpage() ;
  Purpose: Emits the PostScript showpage primitive 
  Parameters:
    None
  Returns:
    Nothing

Function: void gsave() ;
  Purpose: Emits the PostScript gsave primitive 
  Parameters:
    None
  Returns:
    Nothing

Function: void grestore() ;
  Purpose: Emits the PostScript grestore primitive 
  Parameters:
    None
  Returns:
    Nothing

Function: void scale(double sx, double sy) ;
  Purpose: Emits the PostScript scale primitive and its parameters 
  Parameters:
    sx: X scale factor for coordinate transformations
    sy: Y scale factor for coordinate transformations
  Returns:
    Nothing

Function: void rotate(double angle) ;
  Purpose: Emits the PostScript rotate primitive and its parameter 
  Parameters:
    angle: Angle of rotation for coordinate transformations (degrees)
  Returns:
    Nothing

Function: void translate(int tx, int ty) ;
  Purpose: 
    tx: X translation offset for coordinate transformations
    ty: Y translation offset for coordinate transformations
  Parameters:
    None
  Returns:
    Nothing

Function: void line(int x1, int y1, int x2, int y2) ;
  Purpose: Describe a line from the first point to the second point;
    Emit necessary PostScript primitives
  Parameters:
    x1: X coordinate of first point
    y1: Y coordinate of first point
    x2: X coordinate of second point
    y2: Y coordinate of second point
  Returns:
    Nothing

Function: void rect(int x, int y, int width, int height) ;
  Purpose: Describe a rectangle; Emit necessary PostScript primitives
  Parameters:
    x: X coordinate of the lower left corner of the rectangle
    y: Y coordinate of the lower left corner of the rectangle
    width: Size of the rectangle in the X direction
    height: Size of the rectangle in the Y direction
  Returns:
    Nothing

Function: void circle(int x, int y, int radius) ;
  Purpose: Describe a circle centered at the specified point and having
    the specified radius; Emit necessary PostScript primitives
  Parameters:
    x: X coordinate of center point
    y: Y coordinate of center point
    radius: Radius of the circle
  Returns:
    Nothing
Copyright © 2004-2013 Paul J. Drongowski