Skip to content

Commit fb1e74d

Browse files
committed
Add optional index
1 parent 92c0928 commit fb1e74d

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

src/type/p5.Font.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -858,8 +858,7 @@ function font(p5, fn) {
858858
*
859859
* In 2D mode, `path` can take on a few other forms. It could be a path to a CSS file,
860860
* such as one from <a href="https://fonts.google.com/">Google Fonts.</a> It could also
861-
* be a string with a <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face">CSS `@font-face` declaration.</a> It can also be an object containing key-value pairs with
862-
* properties that you would find in an `@font-face` block.
861+
* be a string with a <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face">CSS `@font-face` declaration.</a>
863862
*
864863
* The second parameter, `successCallback`, is optional. If a function is
865864
* passed, it will be called once the font has loaded. The callback function
@@ -876,8 +875,10 @@ function font(p5, fn) {
876875
*
877876
* @method loadFont
878877
* @for p5
879-
* @param {String|Object} path path of the font to be loaded, a CSS `@font-face` string, or an object with font face properties.
878+
* @param {String} path path of the font or CSS file to be loaded, or a CSS `@font-face` string.
880879
* @param {String} [name] An alias that can be used for this font in `textFont()`. Defaults to the name in the font's metadata.
880+
* @param {Object} [options] An optional object with extra CSS font face descriptors, or p5.js font settings.
881+
* @param {Number} [options.index] An optional index specifying which font from a CSS file to use. Defaults to the last one in the file.
881882
* @param {Function} [successCallback] function called with the
882883
* <a href="#/p5.Font">p5.Font</a> object after it
883884
* loads.
@@ -966,13 +967,6 @@ function font(p5, fn) {
966967
* // Some other forms of loading fonts:
967968
* loadFont("https://fonts.googleapis.com/css2?family=Bricolage+Grotesque:opsz,[email protected],200..800&display=swap");
968969
* loadFont(`@font-face { font-family: "Bricolage Grotesque", serif; font-optical-sizing: auto; font-weight: 400; font-style: normal; font-variation-settings: "wdth" 100; }`);
969-
* loadFont({
970-
* fontFamily: '"Bricolage Grotesque", serif',
971-
* fontOpticalSizing: 'auto',
972-
* fontWeight: 400,
973-
* fontStyle: 'normal',
974-
* fontVariationSettings: '"wdth" 100',
975-
* });
976970
* </code>
977971
* </div>
978972
*/
@@ -990,7 +984,7 @@ function font(p5, fn) {
990984
*/
991985
fn.loadFont = async function (...args/*path, name, onSuccess, onError, descriptors*/) {
992986

993-
let { path, name, success, error, descriptors } = parseCreateArgs(...args);
987+
let { path, name, success, error, options: { index, ...descriptors } = {} } = parseCreateArgs(...args);
994988

995989
let isCSS = path.includes('@font-face');
996990

@@ -1038,8 +1032,12 @@ function font(p5, fn) {
10381032
})());
10391033
}
10401034
}
1041-
const fonts = await Promise.all(fontPromises);
1042-
return fonts.findLast(f => f.data) || fonts[0]; // TODO: handle multiple faces?
1035+
if (index !== undefined) {
1036+
return await fontPromises[index]
1037+
} else {
1038+
const fonts = await Promise.all(fontPromises);
1039+
return fonts.findLast(f => f.data) || fonts[0]; // TODO: handle multiple faces?
1040+
}
10431041
}
10441042

10451043
let pfont;

0 commit comments

Comments
 (0)