Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: set stroke and fill properly in PFont.getShape #899

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Junology
Copy link
Contributor

@Junology Junology commented Jan 3, 2025

Currently, PFont.getShape returns an instance of PShape which renders nothing.
This is because fill and storke are not properly set in the construction of the shape.
The commit fixes the issue by calling PShape.noStroke() and PShape.fill(0) at the begining of the construction.

Test code:

import java.awt.Font;
import java.util.stream.IntStream;

PFont symbols;
PShape[] symbolShapes;

void setup() {
  size(512, 512);
  
  symbols = createFont("winjs-symbols.ttf", 32);
  Font font = (Font) symbols.getNative();
  final int[] codes = IntStream.range(0, 0x10000).filter(font::canDisplay).toArray();
  symbolShapes = new PShape[codes.length];
  for(int i = 0; i < codes.length; ++i) {
    symbolShapes[i] = symbols.getShape((char) codes[i]);
    symbolShapes[i].width=32;
    symbolShapes[i].height=32;
  }
}

void draw() {
  background(#AABBCC);
  for(int i = 0; i < symbolShapes.length; ++i) {
    int x = i % 16;
    int y = i / 16;
    symbolShapes[i].setFill(#0000FF);
    shape(symbolShapes[i], 32 * x + 4, 32 * y + 36, 24, 24);
  }
}

winjs-symbols.ttf can be downladed here.

@Junology
Copy link
Contributor Author

Junology commented Jan 4, 2025

Another concern is the "origin" of the shape generated by PFont.getShape.
Namely, since the coordinate (0,0) is on the base line of the character, most of the part of the shape is above the origin, which may be inconsistent with the other shapes which are usually rendered below the origin.
However, the "height" of the character shapes completely depends on their use cases, so "on the base line" might be only a sane choice.
IMHO, PShape can have a functionality to set its origin after the creation (like we can for width and height); cf #788 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant