fontsize dynamically fitting into screen

edited January 2016 in Questions about Code

hey guys,

ive written a writing programm using geomerative library. now i got 2 problems to solve.

  • i want to make the fontsize dynamically fitting into the the given window depending on the string length. (here is an example video on youtube: )

  • i want to make a linebreak by pressing return/enter. what is the best way to do that?

here is the code:

import geomerative.*;
import processing.pdf.*;
boolean doSave = false;

RFont font;
RPoint[] myPoints; 
String myText = "";
RGroup myGroup;
int textsite;
//----------------SETUP---------------------------------
void setup() {
  size(800,300);
  background(255);
  smooth();
  RG.init(this); 
  font = new RFont("FreeSans.ttf",100, CENTER);
}

//----------------DRAW---------------------------------
void draw() {

  background(0);
  stroke(#00FFE8);
  noFill();
  translate(width/2, height/1.7);

  RCommand.setSegmentLength(1);
  RCommand.setSegmentator(RCommand.UNIFORMLENGTH);

  if (myText.length() > 0) {
    RGroup myGroup = font.toGroup(myText); 
    myGroup = myGroup.toPolygonGroup();
    RPoint[] myPoints = myGroup.getPoints();

    beginShape();
    for(int i=0; i<myPoints.length; i++) {
      ellipse(myPoints[i].x, myPoints[i].y,1,1);
      //curveVertex(myPoints[i].x, myPoints[i].y);
    }
    endShape();

    // ------ SAVING  ------ 
    if (doSave) {
      doSave = true;
      endRecord();

      println("gespeichert");
    }
  }
}

//----------------KEYS---------------------------------
void keyReleased() {
  if (keyCode == CONTROL) doSave = true;
}

void keyPressed() {
  if(key !=CODED) {
    switch(key) {
    case DELETE:
    case BACKSPACE:
      myText = myText.substring(0,max(0,myText.length()-1));
      break;
    case ENTER:

      break;
    default:
      myText +=key;
    }
  }
}

Answers

  • Answer ✓

    You will have to use textWidth to check if the string fits in the window.

    //before setup
    int f_size=100;
    
    //within draw
    while (textWidth(myText)>800){
      f_size--;
      font = RFont("FreeSans.ttf",f_size, CENTER);
    }
    

    As for the line break, try including the ENTER key in the string (in other words, delete case ENTER). I did that once and it worked fine for me!

  • edited January 2016

    thank you!

    but.. ..it doesent change the fontsize, because the calculated textWidth stays the same.

    im wondering about the calculation, because the textWidth of the string "l" is 3.0. It doesent matter which fontsize i use at the beginning. why??

  • You have created a font but you do not set it anywhere in the program and thus the program remains with the default font. You need to call textFont(font); in the beginning of draw and between lines 7 & 8 in my code.

  • edited January 2016

    hmm.. i can only call a Pfont with this.. not an Rfont. Sorry if its not true, but that is the problem as i see it. I´m a very rookie to this, so how can i call the created Rfont?

Sign In or Register to comment.