Skip to content

Commit 21167f0

Browse files
authored
Merge pull request #7521 from dhowe/dev-2.0
Minor refactors, cleanup, comments
2 parents 91b2571 + 84462bf commit 21167f0

File tree

1 file changed

+22
-42
lines changed

1 file changed

+22
-42
lines changed

src/type/text2d.js

+22-42
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,5 @@
11
import { Renderer } from '../core/p5.Renderer';
22

3-
/*
4-
* TODO:
5-
* - more with variable fonts, do slider example
6-
* - better font-loading? (google fonts, font-face declarations, multiple fonts with Promise.all())
7-
* - test textToPoints with google/variable fonts?
8-
* - add test for line-height property in textFont() and textProperty()
9-
* - how does this integrate with textLeading?
10-
* - spurious warning in oneoff.html (local)
11-
12-
* ON HOLD:
13-
* - get axes and values for parsed fonts
14-
* - change renderer.state to use getters for textAlign, textBaseline, etc. ??
15-
* DONE:
16-
* - textToPoints/Paths should accept offscreen `graphics` passed in as `options.graphics` [x]
17-
* - textToPaths: test rendering in p5 [x]
18-
* - support direct setting of context2d.font with string [x]
19-
* - textToPoints/Path: add re-sampling support with current options [x]
20-
* - add fontAscent/Descent and textWeight functions [x]
21-
* - textToPaths should split into glyphs and paths [x]
22-
* - add textFont(string) that forces context2d.font to be set (if including size part) [x]
23-
* - textToPoints: test rectMode for all alignments [x]
24-
* - test textToPoints with single line, and overlapping text [x]
25-
* ENHANCEMENTS:
26-
* - cache parsed fonts
27-
* - support idographic and hanging baselines
28-
* - support start and end text-alignments
29-
* - add 'justify' alignment
30-
*/
31-
323
/**
334
* @module Type
345
* @submodule text2d
@@ -91,6 +62,7 @@ function text2d(p5, fn) {
9162
}
9263
return this._renderer[func](...args);
9364
};
65+
// attach also to p5.Graphics.prototype
9466
p5.Graphics.prototype[func] = function (...args) {
9567
return this._renderer[func](...args);
9668
};
@@ -227,6 +199,7 @@ function text2d(p5, fn) {
227199
return this.textDrawingContext().measureText('_').fontBoundingBoxDescent;
228200
};
229201

202+
230203
// setters/getters for text properties //////////////////////////
231204

232205
Renderer.prototype.textAlign = function (h, v) {
@@ -503,7 +476,8 @@ function text2d(p5, fn) {
503476
*/
504477
Renderer.prototype._computeBounds = function (type, str, x, y, width, height, opts) {
505478

506-
let setBaseline = this.textDrawingContext().textBaseline;
479+
let context = this.textDrawingContext();
480+
let setBaseline = context.textBaseline;
507481
let { textLeading, textAlign } = this.states;
508482

509483
// adjust width, height based on current rectMode
@@ -544,13 +518,13 @@ function text2d(p5, fn) {
544518
}
545519

546520
if (0 && opts?.ignoreRectMode) boxes.forEach((b, i) => { // draw bounds for debugging
547-
let ss = this.textDrawingContext().strokeStyle;
548-
this.textDrawingContext().strokeStyle = 'green';
549-
this.textDrawingContext().strokeRect(bounds.x, bounds.y, bounds.w, bounds.h);
550-
this.textDrawingContext().strokeStyle = ss;
521+
let ss = context.strokeStyle;
522+
context.strokeStyle = 'green';
523+
context.strokeRect(bounds.x, bounds.y, bounds.w, bounds.h);
524+
context.strokeStyle = ss;
551525
});
552526

553-
this.textDrawingContext().textBaseline = setBaseline; // restore baseline
527+
context.textBaseline = setBaseline; // restore baseline
554528

555529
return { bounds, lines };
556530
};
@@ -793,8 +767,9 @@ function text2d(p5, fn) {
793767
Renderer.prototype._processLines = function (str, width, height) {
794768

795769
if (typeof width !== 'undefined') { // only for text with bounds
796-
if (this.textDrawingContext().textBaseline === fn.BASELINE) {
797-
this.textDrawingContext().textBaseline = fn.TOP;
770+
let drawingContext = this.textDrawingContext();
771+
if (drawingContext.textBaseline === fn.BASELINE) {
772+
this.drawingContext.textBaseline = fn.TOP;
798773
}
799774
}
800775

@@ -1044,6 +1019,8 @@ function text2d(p5, fn) {
10441019
- font-family must be the last value specified.
10451020
*/
10461021
let { textFont, textSize, lineHeight, fontStyle, fontWeight, fontVariant } = this.states;
1022+
let drawingContext = this.textDrawingContext();
1023+
10471024
let family = this._parseFontFamily(textFont.family);
10481025
let style = fontStyle !== fn.NORMAL ? `${fontStyle} ` : '';
10491026
let weight = fontWeight !== fn.NORMAL ? `${fontWeight} ` : '';
@@ -1053,12 +1030,12 @@ function text2d(p5, fn) {
10531030
//console.log('fontString="' + fontString + '"');
10541031

10551032
// set the font string on the context
1056-
this.textDrawingContext().font = fontString;
1033+
drawingContext.font = fontString;
10571034

10581035
// verify that it was set successfully
1059-
if (this.textDrawingContext().font !== fontString) {
1036+
if (drawingContext.font !== fontString) {
10601037
let expected = fontString;
1061-
let actual = this.textDrawingContext().font;
1038+
let actual = drawingContext.font;
10621039
if (expected !== actual) {
10631040
//console.warn(`Unable to set font property on context2d. It may not be supported.`);
10641041
//console.log('Expected "' + expected + '" but got: "' + actual + '"'); // TMP
@@ -1106,11 +1083,14 @@ function text2d(p5, fn) {
11061083
};
11071084

11081085
if (p5.Renderer2D) {
1086+
11091087
p5.Renderer2D.prototype.textDrawingContext = function () {
11101088
return this.drawingContext;
11111089
};
1090+
11121091
p5.Renderer2D.prototype._renderText = function (text, x, y, maxY, minY) {
11131092
let states = this.states;
1093+
let context = this.textDrawingContext();
11141094

11151095
if (y < minY || y >= maxY) {
11161096
return; // don't render lines beyond minY/maxY
@@ -1120,7 +1100,7 @@ function text2d(p5, fn) {
11201100

11211101
// no stroke unless specified by user
11221102
if (states.strokeColor && states.strokeSet) {
1123-
this.textDrawingContext().strokeText(text, x, y);
1103+
context.strokeText(text, x, y);
11241104
}
11251105

11261106
if (!this._clipping && states.fillColor) {
@@ -1129,7 +1109,7 @@ function text2d(p5, fn) {
11291109
if (!states.fillSet) {
11301110
this._setFill(DefaultFill);
11311111
}
1132-
this.textDrawingContext().fillText(text, x, y);
1112+
context.fillText(text, x, y);
11331113
}
11341114

11351115
this.pop();

0 commit comments

Comments
 (0)