Coder Social home page Coder Social logo

ptext's Introduction

๐Ÿšง This README (and library) is under construction ๐Ÿšง

PText

PText bridges the gap between Processing's PFont and PShape classes, providing some much needed functionality when working with text in Processing.

PText extends PShape, meaning that is stores text in a vector format. With this, it offers methods to:

  • Easily manipulate text dimensions
  • Get a text's exact bounds (dimensions)
  • Accurately get text's ascent and descent (unlike Processing's existing methods โ€” see appendix)
  • Manipulate text characteristics (such as shear and per-character rotation)
  • Visualise per-character bounds, whitespace, and vertices

API

The PText API is catalogued below.

Text

  • setText(text)
  • setFont(fontNameString, fontSize)
  • setFont(PFont)

Dimensions

  • scale(both)
  • scale(x, y)
  • setScale(both)
  • setScale(x, y)

Width

  • setTextWidth(n)

example

  • scaleWidth(n)
  • getTextWidth()

Height

  • setTextHeight(n)

example

  • scaleHeight(n)
  • getTextHeight()

Other Text Metrics

  • getTextAscent()
  • getTextDescent()
  • getFontAscent()
  • getFontDescent()
  • getWhiteSpaceLeft()
  • getWhiteSpaceRight()
  • getCharWidth(character)
  • getCharHeight(character)
  • getCharWhitespace(character)

Text Manipulation

  • setCharacterSpacing(n)

example

  • setCharacterRotation(charIndex, angle)

    TODO image

  • setShearX(maxShear)

    TODO image

Rendering

  • shape(myPText, x, y)

    Use Processing's shape() method to draw the PText like a PShape (alignment will be LEFT, BASELINE).

  • draw(x, y, alignX, alignY)

    Or call draw() on the PText object to specify a specific X and Y alignment (similar to textAlign()).

Debug

  • debug()

example

Example

Resizing a PText shape using setTextWidth() and setTextHeight(), using debug() to show

import pText.PText;

PText text;

void setup() {
  
  size(1280, 720);
  smooth(4);

  text = new PText(this, "Bauhaus 93", 192);
  
  text.setText("hello");
  
  text.setFill(color(55, 255, 90));
  text.setStrokeWeight(1);

  text.setScale(1, 1);
  //shapeMode(CENTER);
  text.setTextWidth(width);
  text.setTextHeight(height);
  
    noFill(); // you must call global noFill() after any setText(), otherwise text can't be filled
}

void draw() {

  background(255);

  shape(text, mouseX, mouseY);
  //text.debug(mouseX, mouseY);
}

Appendix

Using the inbuilt functions textWidth(), textAscent(), and textDescent() are an easy way to get a good approximate result for the height and width of a string (of a given font), but they are not exact.

Why?

  • textAscent() returns text height above the baseline based on the letter 'd'
  • textDescent() returns text height below the baseline based on the letter 'p'.
  • textWidth() includes glyph whitespace (aka padding; ideally we want to ignore this for the first and last characters)

textAscent() + textDescent() therefore measures the maximum height of a string in a given font and font size, and not the height of a specific string. In other words, if your text doesn't include both 'd' and 'p' characters, then using these methods to determine text height will overestimate the result.

TODO

  • Scale whitespace (to increase/decrease spacing between letters, independent of font size)
  • Display dimension labels (such as ascent & descent) in debug mode
  • String ascent: return the max ascent of the string's current characters
  • String descent: return the max descent of the string's current characters
  • Allow multiple fonts within one PText at once?
  • Support multiple lines / blocks of text

ptext's People

Contributors

micycle1 avatar tidensbarn avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.