FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Programs
(Moderators: fry, REAS)
   Help with Glen Murphy's FastText?
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Help with Glen Murphy's FastText?  (Read 3727 times)
Jonathan Harris

WWW
Help with Glen Murphy's FastText?
« on: Sep 10th, 2004, 6:24pm »

I have been using Glen Murphy's FastText class for Processing, and it's been working well. However, I need to display pixelated text vertically (rotated 90 degress CCW) for an application I'm developing.  I have been trying to alter the FastText class to do this, but have been having a lot of trouble making it work (FastText is designed to display text horizontally).
 
For those who don't know, the FastText class creates pixelated fonts from a source image that contains all characters in the relevant font. FastText cuts up the image and produces character definitions, pixel by pixel. More on FastText is at: http://bodytag.org/fasttext
 
I was hoping that someone might be able to take a look at my code and offer a quick answer of what I'm doing wrong.
 
A short example, with source code, is online at: http://www.number27.org/processing/text
 
Right now, the vertical text, which should read "This is vertical text", is coming out all jumbled, even though, as far as my logic can tell, the algorithm should work.
 
Thanks so much for any help anyone can offer.
 
Best,
Jon
 
 

--
Jonathan Harris
Number27 >> http://www.number27.org
fjen

WWW
Re: Help with Glen Murphy's FastText?
« Reply #1 on: Sep 11th, 2004, 11:49pm »

well, not completely solved the problems ... but close. i'll leave the rest for yourself.
 
http://www.florianjenett.de/p55/verticalText_f/
 
best, F
 
wordcount is nice ... leaves a lot of open questions:
- what is the word we think we use most?
- what is the word we think we use least? (this is nearly impossible ..)
- misspelling like with google's "did you mean ..." -> are the words we use more often burned into our brains in terms of spelling?
- what's the top misspelled word?
- ...
 
redfrik


Re: Help with Glen Murphy's FastText?
« Reply #2 on: Sep 19th, 2004, 1:59pm »

and i have a similar problem...  i'm trying to create a simple scroller using the fasttext class.  how come the text gets garbled?
 
//--
FastText scrollWin;
String scrollText;
int scrollIndex;
 
void setup() {
  background(100);
  size(300, 150);
  framerate(30);
  scrollWin= new FastText("silkscreen.gif");
  scrollText= "abcde, abcde, abcde, abcde, abcde, abcde, abcde, abcde, abcde, abcde, abcde, abcde, abcde, abcde, abcde, ";
  scrollIndex= 0;
}
 
void loop() {
  background(100);
  scrollIndex= scrollIndex-1;
  if(scrollIndex<-796) {scrollIndex=0;}
  scrollWin.write(g, scrollText, scrollIndex, 60, true);
}
 
 
// ***********************************************************
// ****     glen murphy's FastText    ****
// ***********************************************************
class FastText {
  int characters;
  int charWidth[] = new int[255];
  int charHeight;
  int chars[][];
  int lineHeight;
  int col;
  int wh = width*height;
  BImage img;
   
  FastText(String fontFile) {
    loadFont(fontFile);
    charHeight = img.height;
    lineHeight = charHeight;
    // find the characters' endpoints.
    int currWidth = 0;
    int maxWidth = 0;
    for(int i = 0; i < img.width; i++) {
 currWidth++;
 if(img.pixels[i] == 0xffff0000) {
   charWidth[characters++] = currWidth;
   if(currWidth > maxWidth) maxWidth = currWidth;
   currWidth = 0;
 }
    }
    // create the character sprites.
    chars = new int[characters][maxWidth*charHeight];
    int indent = 0;
    for(int i = 0; i < characters; i++) {
 for(int u = 0; u < charWidth[i]*charHeight; u++) {
   chars[i][u] = img.pixels[indent + (u/charWidth[i])*img.width + (u%charWidth[i])];
 }
 indent += charWidth[i];
    }
  }
  void setLineHeight(int h) {
    lineHeight = h;
  }
  void setColor(int c) {
    col=c;
  }
  void loadFont(String name) {
    img = loadImage(name);
  }
  void putchar(BImage img, int c, int x, int y, boolean clearZ) {
    int[] pix=img.pixels;
    y*=img.width;
    for(int i = 0; i < charWidth[c]*charHeight; i++) {
 int xpos = x + i%charWidth[c];
 int pos = xpos + y + (i/charWidth[c])*img.width;
 if(chars[c][i] == 0xff000000 && xpos < img.width && pos > 0 && pos < wh) {
   pix[pos] = col;
     if (clearZ) g.zbuffer[pos]=0;
 }
    }
  }
  void write(BImage img, String text, int x, int y, int wrap, boolean clearZ) {
    int indent = 0;
    for(int i = 0; i < text.length(); i++) {
 int c = (int)text.charAt(i);
 if(c == 10 || (wrap > 0 && indent > wrap)) {
   indent = 0;
   y += lineHeight;
 }
 if(c != 10) {
   putchar(img, c-32, x+indent, y,clearZ);
   indent += charWidth[c-32];
 }
    }
  }
  void write(BImage img, String text, int x, int y, boolean clearZ) {
    write(img, text, x, y, -1, clearZ);
  }
 
  int pixelsWidth(String text) {
    int tot=0;
    for (int i=0; i<text.length(); i++) {
 int c = (int)text.charAt(i);
 tot+=charWidth[c-32];
    }
    return tot;
  }
}
 
fjen

WWW
Re: Help with Glen Murphy's FastText?
« Reply #3 on: Sep 19th, 2004, 6:30pm »

ok ... and your answer is here:
 
http://www.florianjenett.de/p55/scrolling_fastT_2/
 
the problem was that text starting in negative space was drawn to img although it should have been clipped ...
 
added support for chars that overlap with the left-img-edge as well ..
 
/F
 
redfrik


Re: Help with Glen Murphy's FastText?
« Reply #4 on: Sep 20th, 2004, 1:07pm »

brilliant.  many thanks.
 
fry


WWW
Re: Help with Glen Murphy's FastText?
« Reply #5 on: Sep 20th, 2004, 9:09pm »

there's also textSpace(SCREEN_SPACE) which will render things at pixel level and using pixel coords, ala fasttext. you just have to create the font in the exact size that you want.
 
toxi

WWW
Re: Help with Glen Murphy's FastText?
« Reply #6 on: Sep 21st, 2004, 3:09pm »

here's another modified, "value-added" version of FastText. i still prefer it over the default VLW font solution with SCREEN_SPACE setting as FastText offers alot more control over the actual font bitmap used, especially for small font sizes...
 
Code:
/* FastText - fast text for processing.
original author: Glen Murphy (glenmurphy.com)
 
added features by toxi@toxi.co.uk:
* vertical text
* set clipping region
* target image selection
* compute pixel width of string
* (word)wrap string to pixel width
 
*/
 
class FastText {
  int characters;
  int charWidth[] = new int[255];
  int charXOffset[] = new int[255];
  int charHeight;
  int chars[][];
  int lineHeight;
  int maxWidth;
  int col;
  int width, height;
  int wh;
  int[] pixels;
  int clipX1, clipY1, clipX2, clipY2, clipOffset1, clipOffset2;
  BImage img;
  BApplet parent;
   
  FastText(BApplet p, String fontFile) {
    parent = p;
    // set applet's pixel buffer as default target surface
    setTarget(p.g);
     
    loadFont(fontFile);
    charHeight = img.height;
    lineHeight = charHeight;
    // find the characters' endpoints.
    int currWidth = 0;
    maxWidth = 0;
    for (int i = 0; i < img.width; i++) {
 currWidth++;
 if (img.pixels[i] == 0xffff0000) {
   charWidth[characters++] = currWidth;
   charXOffset[characters] = i;
   if (currWidth > maxWidth)
     maxWidth = currWidth;
   currWidth = 0;
 }
    }
    // create the character sprites.
    chars = new int[characters][maxWidth * charHeight];
    int indent = 0;
    for (int i = 0; i < characters; i++) {
 for (int u = 0; u < charWidth[i] * charHeight; u++) {
   chars[i][u] = img.pixels[indent + (u / charWidth[i]) * img.width + (u % charWidth[i])];
 }
 indent += charWidth[i];
    }
  }
   
  public void setLineHeight(int h) {
    lineHeight = h;
  }
   
  public void setColor(int c) {
    col = c;
  }
   
  public void loadFont(String name) {
    System.out.println("loading font: " + name);
    img = parent.loadImage(name);
  }
   
  // set target image object to be used
  public void setTarget(BImage t) {
    pixels = t.pixels;
    width = t.width;
    height = t.height;
    wh = width * height;
    setClipping(0, 0, width, height);
  }
   
  // set clipping rectangle (left,top,right,bottom)
  public void setClipping(int x1, int y1, int x2, int y2) {
    clipX1 = x1;
    clipY1 = y1;
    clipOffset1 = clamp(y1 * width + x1, 0, wh);
    clipX2 = x2 - 1;
    clipY2 = y2 - 1;
    clipOffset2 = clamp(clipY2 * width + clipX2, clipOffset1, wh);
  }
   
  void putcharH(int c, int x, int y) {
    y *= width;
    for (int i = 0; i < charWidth[c] * charHeight; i++) {
 int xpos = x + i % charWidth[c];
 int pos = xpos + y + (i / charWidth[c]) * width;
 if (chars[c][i] == 0xff000000 && xpos < clipX2 && pos >= clipOffset1 && pos < clipOffset2) {
   pixels[pos] = col;
 }
    }
  }
   
  public void writeH(String text, int x, int y, int wrap) {
    int indent = 0;
    for (int i = 0; i < text.length(); i++) {
 int c = (int)text.charAt(i);
 if (c < 32 || (wrap > 0 && indent > wrap)) {
   indent = 0;
   y += lineHeight;
 }
 if (c > 31) {
   putcharH(c - 32, x + indent, y);
   indent += charWidth[c - 32];
 }
    }
  }
  public void writeH(String text, int x, int y) {
    writeH(text, x, y, clipX2 - x);
  }
   
  void putcharV(int c, int x, int y) {
    int[] cData = chars[c];
    int xpos = x;
    int i = 0;
    y *= width;
    for (int xx = 0; xx < charHeight; xx++) {
 for (int yy = 0; yy < charWidth[c]; yy++) {
   int pos = y + xpos - yy * width;
   if (cData[i] == 0xff000000 && xpos < clipX2 && pos > clipOffset1 && pos < clipOffset2) {
     pixels[pos] = col;
   }
   i++;
 }
 xpos++;
    }
  }
   
  public void writeV(String text, int x, int y, int wrap) {
    int indent = 0;
    for (int i = 0; i < text.length(); i++) {
 int c = (int)text.charAt(i);
 if (c < 32 || (wrap > 0 && indent > wrap)) {
   indent = 0;
   x += lineHeight;
 }
 if (c > 31) {
   putcharV(c - 32, x, y - indent);
   indent += charWidth[c - 32];
 }
    }
  }
   
  public void writeV(String text, int x, int y) {
    writeV(text, x, y, -1);
  }
   
  public int getPixelWidth(String s) {
    int len = 0;
    int sl = s.length();
    for (int i = 0; i < sl; i++) {
 int c = (int)s.charAt(i);
 if (c >= 32)
   len += charWidth[c - 32];
    }
    return len;
  }
   
  // return word wrapped substring for pixel width
  // if wordWrap is "false", the max.number of chars is used
  public String getSubstringForWidth(String s, int w, boolean wordWrap) {
    int i, len = 0, lastWS = 0, sl = s.length();
    for (i = 0;(i < sl && len < w); i++) {
 int c = (int)s.charAt(i);
 if (c >= 32) {
   len += charWidth[c - 32];
   if (c <= 32)
     lastWS = i;
 }
    }
    if (wordWrap) {
 return s.substring(0, (lastWS > 0 && len >= w) ? lastWS : i);
    }
    else {
 return s.substring(0, i);
    }
  }
   
  private int clamp(int a, int b, int c) {
    return (a < b ? b : (a > c ? c : a));
  }
}
 

http://toxi.co.uk/
fjen

WWW
Re: Help with Glen Murphy's FastText?
« Reply #7 on: Sep 21st, 2004, 6:00pm »

ha! very nice ... i was thinking about getting it all together too. so what's missing now? text at arbitrary angles ... text on a line/path.
 
/F
 
Euskadi


Re: Help with Glen Murphy's FastText?
« Reply #8 on: Sep 26th, 2004, 5:31am »

Has anyone timed FastText against regular P5 font drawing? I have six columns of 10+ numbers that update every frame. Would FastText run faster?
 
eskimoblood

222550793222550793 WWW
Re: Help with Glen Murphy's FastText?
« Reply #9 on: Mar 17th, 2005, 1:09pm »

Can someone explain how to add chars like to fasttext
 
fry


WWW
Re: Help with Glen Murphy's FastText?
« Reply #10 on: Mar 17th, 2005, 11:25pm »

on Mar 17th, 2005, 1:09pm, eskimoblood wrote:
Can someone explain how to add chars like to fasttext

i'd recommend using rev 69 of processing (which has several text fixes) along with textSpace(SCREEN_SPACE) which will operate similarly. offhand, i don't think fasttext does it, and generally it's a non-trivial problem to fix.
 
eskimoblood

222550793222550793 WWW
Re: Help with Glen Murphy's FastText?
« Reply #11 on: Mar 17th, 2005, 11:31pm »

Ok, is there a way to create a 10pt font with processing. All my tries are ending with a java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds! error.
 
fry


WWW
Re: Help with Glen Murphy's FastText?
« Reply #12 on: Mar 18th, 2005, 12:22am »

on Mar 17th, 2005, 11:31pm, eskimoblood wrote:
Ok, is there a way to create a 10pt font with processing. All my tries are ending with a java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds! error.

are you using rev 69 there was a bug in 68 that caused a lot of this. if you are, that's a bummer, but it's been fixed in later releases that will soon be public.
 
eskimoblood

222550793222550793 WWW
Re: Help with Glen Murphy's FastText?
« Reply #13 on: Mar 18th, 2005, 12:59am »

Thanks v69 works fine.
 
Pages: 1 

« Previous topic | Next topic »