1
1
import { Renderer } from '../core/p5.Renderer' ;
2
2
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
-
32
3
/**
33
4
* @module Type
34
5
* @submodule text2d
@@ -91,6 +62,7 @@ function text2d(p5, fn) {
91
62
}
92
63
return this . _renderer [ func ] ( ...args ) ;
93
64
} ;
65
+ // attach also to p5.Graphics.prototype
94
66
p5 . Graphics . prototype [ func ] = function ( ...args ) {
95
67
return this . _renderer [ func ] ( ...args ) ;
96
68
} ;
@@ -227,6 +199,7 @@ function text2d(p5, fn) {
227
199
return this . textDrawingContext ( ) . measureText ( '_' ) . fontBoundingBoxDescent ;
228
200
} ;
229
201
202
+
230
203
// setters/getters for text properties //////////////////////////
231
204
232
205
Renderer . prototype . textAlign = function ( h , v ) {
@@ -503,7 +476,8 @@ function text2d(p5, fn) {
503
476
*/
504
477
Renderer . prototype . _computeBounds = function ( type , str , x , y , width , height , opts ) {
505
478
506
- let setBaseline = this . textDrawingContext ( ) . textBaseline ;
479
+ let context = this . textDrawingContext ( ) ;
480
+ let setBaseline = context . textBaseline ;
507
481
let { textLeading, textAlign } = this . states ;
508
482
509
483
// adjust width, height based on current rectMode
@@ -544,13 +518,13 @@ function text2d(p5, fn) {
544
518
}
545
519
546
520
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 ;
551
525
} ) ;
552
526
553
- this . textDrawingContext ( ) . textBaseline = setBaseline ; // restore baseline
527
+ context . textBaseline = setBaseline ; // restore baseline
554
528
555
529
return { bounds, lines } ;
556
530
} ;
@@ -793,8 +767,9 @@ function text2d(p5, fn) {
793
767
Renderer . prototype . _processLines = function ( str , width , height ) {
794
768
795
769
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 ;
798
773
}
799
774
}
800
775
@@ -1044,6 +1019,8 @@ function text2d(p5, fn) {
1044
1019
- font-family must be the last value specified.
1045
1020
*/
1046
1021
let { textFont, textSize, lineHeight, fontStyle, fontWeight, fontVariant } = this . states ;
1022
+ let drawingContext = this . textDrawingContext ( ) ;
1023
+
1047
1024
let family = this . _parseFontFamily ( textFont . family ) ;
1048
1025
let style = fontStyle !== fn . NORMAL ? `${ fontStyle } ` : '' ;
1049
1026
let weight = fontWeight !== fn . NORMAL ? `${ fontWeight } ` : '' ;
@@ -1053,12 +1030,12 @@ function text2d(p5, fn) {
1053
1030
//console.log('fontString="' + fontString + '"');
1054
1031
1055
1032
// set the font string on the context
1056
- this . textDrawingContext ( ) . font = fontString ;
1033
+ drawingContext . font = fontString ;
1057
1034
1058
1035
// verify that it was set successfully
1059
- if ( this . textDrawingContext ( ) . font !== fontString ) {
1036
+ if ( drawingContext . font !== fontString ) {
1060
1037
let expected = fontString ;
1061
- let actual = this . textDrawingContext ( ) . font ;
1038
+ let actual = drawingContext . font ;
1062
1039
if ( expected !== actual ) {
1063
1040
//console.warn(`Unable to set font property on context2d. It may not be supported.`);
1064
1041
//console.log('Expected "' + expected + '" but got: "' + actual + '"'); // TMP
@@ -1106,11 +1083,14 @@ function text2d(p5, fn) {
1106
1083
} ;
1107
1084
1108
1085
if ( p5 . Renderer2D ) {
1086
+
1109
1087
p5 . Renderer2D . prototype . textDrawingContext = function ( ) {
1110
1088
return this . drawingContext ;
1111
1089
} ;
1090
+
1112
1091
p5 . Renderer2D . prototype . _renderText = function ( text , x , y , maxY , minY ) {
1113
1092
let states = this . states ;
1093
+ let context = this . textDrawingContext ( ) ;
1114
1094
1115
1095
if ( y < minY || y >= maxY ) {
1116
1096
return ; // don't render lines beyond minY/maxY
@@ -1120,7 +1100,7 @@ function text2d(p5, fn) {
1120
1100
1121
1101
// no stroke unless specified by user
1122
1102
if ( states . strokeColor && states . strokeSet ) {
1123
- this . textDrawingContext ( ) . strokeText ( text , x , y ) ;
1103
+ context . strokeText ( text , x , y ) ;
1124
1104
}
1125
1105
1126
1106
if ( ! this . _clipping && states . fillColor ) {
@@ -1129,7 +1109,7 @@ function text2d(p5, fn) {
1129
1109
if ( ! states . fillSet ) {
1130
1110
this . _setFill ( DefaultFill ) ;
1131
1111
}
1132
- this . textDrawingContext ( ) . fillText ( text , x , y ) ;
1112
+ context . fillText ( text , x , y ) ;
1133
1113
}
1134
1114
1135
1115
this . pop ( ) ;
0 commit comments