/**
  * Change the textMode() to either SHAPE or MODEL. <br>
  * This resets all renderer settings, and should therefore be called <EM>before</EM> any other
  * commands that set the fill() or the textFont() or anything. Unlike other renderers, use
  * textMode() directly after the size() command.
  */
 public void textMode(int mode) {
   if (textMode != mode) {
     if (mode == SHAPE) {
       g2.dispose();
       g2 = content.createGraphicsShapes(width, height);
     } else if (mode == MODEL) {
       g2.dispose();
       g2 = content.createGraphics(width, height, mapper);
       //        g2 = template.createGraphics(width, height, mapper);
     } else if (mode == SCREEN) {
       throw new RuntimeException("textMode(SCREEN) not supported with PDF");
     } else {
       throw new RuntimeException("That textMode() does not exist");
     }
   }
 }
  public void beginDraw() {
    //    long t0 = System.currentTimeMillis();

    if (document == null) {
      document = new Document(new Rectangle(width, height));
      try {
        if (file != null) {
          // BufferedOutputStream output = new BufferedOutputStream(stream, 16384);
          output = new BufferedOutputStream(new FileOutputStream(file), 16384);

        } else if (output == null) {
          throw new RuntimeException(
              "PGraphicsPDF requires a path " + "for the location of the output file.");
        }
        writer = PdfWriter.getInstance(document, output);
        document.open();
        content = writer.getDirectContent();
        //        template = content.createTemplate(width, height);

      } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Problem saving the PDF file.");
      }

      //      System.out.println("beginDraw fonts " + (System.currentTimeMillis() - t));
      g2 = content.createGraphics(width, height, getMapper());
      //      g2 = template.createGraphics(width, height, mapper);
    }
    //    System.out.println("beginDraw " + (System.currentTimeMillis() - t0));
    super.beginDraw();
  }
  /** Call to explicitly go to the next page from within a single draw(). */
  public void nextPage() {
    PStyle savedStyle = getStyle();
    g2.dispose();

    try {
      //    writer.setPageEmpty(false);  // maybe useful later
      document.newPage(); // is this bad if no addl pages are made?
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (textMode == SHAPE) {
      g2 = content.createGraphicsShapes(width, height);
    } else if (textMode == MODEL) {
      g2 = content.createGraphics(width, height, mapper);
    }
    style(savedStyle);

    // should there be a beginDraw/endDraw in here?
  }