Why is the text not centered in the exported PDF?

Hi,

I'm exporting a PDF from Processing and while the text is centred in the running Processing sketch and in a TIF file that I export, but not a PDF, for some mysterious reason. Anyone knows why? Thanks in advance.

TIF export example: Screen Shot 2014-10-16 at 15.56.52

In the Processing sketch: Screen Shot 2014-10-16 at 15.52.49

The exported PDF (opened in Illustrator): Screen Shot 2014-10-16 at 15.54.34

Answers

  • Answer ✓

    I don't really see a difference, because you chose different runs for your screenshots.

    But I guess PDF calculates text width a bit differently from Processing, hence the difference, perhaps.

  • Thanks for the reply. If you look closely, you can see that some of the lines are a bit off-center, like below.

    Screen Shot 2014-10-16 at 21.00.42

    It's just really frustrating, because it means that I can't get a proper PDF export of this sketch...

  • edited October 2014

    Asking people to debug code that you don't share, means they have to make educated guesses. My guess is that your text alignment call is outside of the begin-endRecord calls that generate the pdf, hence it affects the sketch but not the pdf.

    By the way, not sharing any code also means you posted in the wrong section, which is literally called "questions about code", but you did not post any! Moved.

  • edited March 2017

    A PDF example:

    /**
     * Letters-X (v3.01)
     * by  DiegoOriani (2014/Apr)
     * mod Chrisir & GoToLoop
     *
     * forum.Processing.org/two/discussion/4275/
     * how-to-compile-a-series-of-functions-into-one-variable#Item_4
     */
    
    //import processing.pdf.PGraphicsPDF;
    
    interface DataX {
      int ROWS = 8, COLS = 10, GAP = 030, MARGIN = 020;
      int LEN = 030, WEIGHT = 5, CAP = SQUARE;
    
      int HSIZE = COLS*(LEN+GAP) + MARGIN*2 - GAP;
      int VSIZE = ROWS*(LEN+GAP) + MARGIN*2 - GAP;
    
      color BG = 0300;
    
      color[] inks = {
        //#FFCC00, #FF9900, #FF35EE
        #FF0000, #008000, #0000FF
      };
    
      String PDF_FILE = "lettersX-####.pdf";
    }
    
    final LetterX[] xxx = new LetterX[DataX.ROWS * DataX.COLS];
    boolean savePDF;
    
    void setup() {
      size(DataX.HSIZE, DataX.VSIZE, JAVA2D);
      noLoop();
      frameRate(10);
    
      initCanvas();
      instantiateLettersX();
    
      println("Width: " + width + "\tHeight: " + height);
      println("# of Xs: " + xxx.length + "\n");
    }
    
    void initCanvas() {
      smooth(4);
      background(DataX.BG);
      strokeWeight(DataX.WEIGHT);
      strokeCap(DataX.CAP);
    }
    
    void draw() {
      if (savePDF) {
        beginRecord(PDF, dataPath(DataX.PDF_FILE));
        initCanvas();
      }
    
      for (LetterX currentX: xxx)  currentX.display();
    
      if (savePDF) {
        endRecord();
        savePDF = false;
        println("Frame #" + frameCount + " saved!");
      }
    }
    
    void keyPressed() {
      reshuffleInks();
      redraw();
    
      if (keyCode == 'S')  savePDF = true;
    }
    
    void mousePressed() {
      keyPressed();
    }
    
    void reshuffleInks() {
      for (LetterX currentX: xxx)
        currentX.c = DataX.inks[(int) random(DataX.inks.length)];
    }
    
    void instantiateLettersX() {
      for (int r = 0; r != DataX.ROWS; ++r)
        for (int c = 0; c != DataX.COLS; ++c) {
          int x = c*(DataX.LEN + DataX.GAP) + DataX.MARGIN;
          int y = r*(DataX.LEN + DataX.GAP) + DataX.MARGIN;
          color ink = DataX.inks[(int) random(DataX.inks.length)];
    
          xxx[r*DataX.COLS + c] = new LetterX(x, y, ink);
        }
    }
    
    class LetterX implements DataX {
      final short x, y;
      color c;
    
      LetterX(int xx, int yy, color cc) {
        x = (short) xx;
        y = (short) yy;
        c = cc;
      }
    
      void display() {
        stroke(c);
        line(x, y, x+LEN, y+LEN);
        line(x+LEN, y, x, y+LEN);
      }
    }
    
Sign In or Register to comment.