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

Allow textAlign(CENTER, CENTER) with max width but no max height #6060

Merged
merged 6 commits into from
Mar 12, 2023

Conversation

sawaisinghh
Copy link
Contributor

@sawaisinghh sawaisinghh commented Mar 9, 2023

Resolves #5933

Changes:
Now we can use textAlign(CENTER, CENTER) and then call text('something', x, y, w) without specifying a height parameter.

Screenshots of the change:

Before:

text-align22

p5 sound22

After:

text-align

image

PR Checklist

@sawaisinghh
Copy link
Contributor Author

@davepagurek, please review it.

@dhowe
Copy link
Contributor

dhowe commented Mar 11, 2023

@sawaisinghh @davepagurek can you clarify the specification for this, specifically for multi-line text? For the CENTER/CENTER case without height, this PR appears to center on the y-position, and then cut-off all the text above that (left image), rather than the previous behavior (right image)

image   image

let txt = "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.";
let x = 20, y = 50, w = 250;
function setup() {
  createCanvas(300, 300);
  background(245);

  textAlign(CENTER, CENTER);
  textFont("georgia", 12);
  rect(x, y, w, height - y);

  text(txt, x, y, w);
}

@dhowe dhowe self-assigned this Mar 11, 2023
@sawaisinghh
Copy link
Contributor Author

@dhowe okay, I will try to resolve it.

@sawaisinghh
Copy link
Contributor Author

@dhowe, But, I think no text were visible in previous behavior specifically in multiline.
you can see here:

image

@dhowe
Copy link
Contributor

dhowe commented Mar 11, 2023

Correct -- there was only a warning. The upper right image is from 1.4.2, which is (I think) the behavior you were trying to restore

@sawaisinghh
Copy link
Contributor Author

sawaisinghh commented Mar 11, 2023

@dhowe yeah correct, according to issue, textAlign(CENTER, CENTER) without specifying height were working fine in 1.4.2, but not working in current version. Okay thanks for the review, I will try to fix it.

@sawaisinghh
Copy link
Contributor Author

@davepagurek @limzykenneth please review my pull request.

@davepagurek
Copy link
Contributor

Right, to clarify the expectations here, we want the textAlign example to look like it did in 1.4.2:
eg142

For multiline behaviour, @dhowe's observation is correct, we'd want it to center the text about the middle y position and not cut off anything vertically and match its behaviour in 1.4.2. Here's a test case (p5 editor):

function setup() {
  background(200);
  
  rectMode(CENTER);
  textAlign(CENTER, CENTER);
  text(
    'The quick brown fox jumps over the lazy dog',
    width/2,
    height/2,
    width
  );
}

image

Copy link
Contributor

@davepagurek davepagurek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your work on this! I tested it out and it looks like it meets those requirements in from my last comment 🙂 The functionality seems good to go! I just have some final code style comments before merging.

@@ -10618,7 +10618,7 @@ var soundRecorder_ac = main.audiocontext;
* // send result to soundFile
* recorder.stop();
*
* text('Done! Tap to play and download', width/2, height/2, width - 20);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably shouldn't edit p5.sound.js here, as it comes from builds in https://github.com/processing/p5.js-sound that we periodically pull from. If we want to make a change to it, we'd have to make it in that repo for it to not get overwritten in the next build.

if (this._textBaseline === constants.BOTTOM ||
this._textBaseline === constants.CENTER) {
// use rectHeight as an approximation for text height
let rectHeight = p.textSize() * (this._textLeading || 1.25);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this._textLeading is always initialized here

this._textLeading = 15;
and also set when the user calls textSize(), so I think it's safe to just use this._textLeading here.

If there's a case I'm not considering though, and we need the || 1.25, can we replace 1.25 with constants._DEFAULT_LEADMULT like we use here?

this._setProperty('_textLeading', s * constants._DEFAULT_LEADMULT);

@sawaisinghh
Copy link
Contributor Author

@davepagurek I made the changes. Can you please review it.

Copy link
Contributor

@davepagurek davepagurek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making those changes! One last very minor thing and then we're good to go!

if (this._textBaseline === constants.BOTTOM ||
this._textBaseline === constants.CENTER) {
// use rectHeight as an approximation for text height
let rectHeight = p.textSize() * (this._textLeading);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last tiny change, we can take out the brackets around this._textLeading now 🙂

Copy link
Contributor

@davepagurek davepagurek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again, great work!

@davepagurek davepagurek merged commit 1e29f74 into processing:main Mar 12, 2023
@sawaisinghh
Copy link
Contributor Author

@davepagurek done 🙂

@davepagurek
Copy link
Contributor

@all-contributors please add @sawaisinghh for bug, code

@allcontributors
Copy link
Contributor

@davepagurek

I've put up a pull request to add @sawaisinghh! 🎉

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.

Allow textAlign(CENTER, CENTER) with max width but no max height
3 participants