Using textWidth(), redo Example 17-4 (the text “mirror”) to use a non-fixed-width font with proper character spacing.
example
Example
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Exercise 17-9: Using textWidth(), redo Example 17-4 (the text "mirror") to use a 
// non-fixed-width font with proper character spacing.


import processing.video.*;

// Size of each cell in the grid, ratio of window size to video size
int videoScale = 16;
// Number of columns and rows in our system
int cols, rows;
// Variable to hold onto capture object
Capture video;

// A String and Font
String chars = "itwasadarkandstormynightthequickbrownfoxjumpedoverthelazydog";
PFont f;

void setup() {
  size(640, 480);
  //set up columns and rows
  cols = width/videoScale;
  rows = height/videoScale;
  video = new Capture(this,cols,rows,15);

  // Load the font
  f = createFont("Arial",18,true);
}

void draw(){ 
  // Read image from the camera
  if (video.available()) {
    video.read(); 
  }
  video.loadPixels();
  //image(video,0,0,width,height);

  background(0);

  // Use a variable to count through chars in String
  int charcount = 0;

  // Begin loop for rows
  for ( int j = 0; j < rows;j++) {
    // Begin loop for columns, instead of pixel by pixel now
    // we move with a variable floating point x,  based on character width
    float x = 0;
    while (x < width) {
      // Where are we, pixel-wise for y?
      int y = j*videoScale;
      // Where are we pixel-wise for x? Convert to int, scale down, and make sure we don't go offscreen 
      int pix = constrain((int) (x / videoScale),0,cols-1);

      // Looking up the appropriate color in the pixel array
      color c = video.pixels[pix+j*video.width]; 

      // Displaying an individual character from the String
      // Instead of a rectangle
      textFont(f);
      fill(c);
      char ch = chars.charAt(charcount);
      text(ch,x,y);
      // Go on to the next character, loop back to zero at end
      charcount = (charcount + 1) % chars.length();
      // Move x according to character's widt
      x += textWidth(ch);
    }
  }

}


»

No comments yet.

Leave a comment