@@ -163,10 +163,10 @@ public static enum Mode {
163163 */
164164// public static final String SIZE_REGEX =
165165// "(?:^|\\s|;)size\\s*\\(\\s*([^\\s,]+)\\s*,\\s*([^\\s,\\)]+)\\s*,?\\s*([^\\)]*)\\s*\\)\\s*\\;";
166- static private final String SIZE_CONTENTS_REGEX =
167- "(?:^|\\ s|;)size\\ s*\\ (([^\\ )]+)\\ )\\ s*\\ ;" ;
168- static private final String FULL_SCREEN_CONTENTS_REGEX =
169- "(?:^|\\ s|;)fullScreen\\ s*\\ (([^\\ )]+)\\ )\\ s*\\ ;" ;
166+ // static private final String SIZE_CONTENTS_REGEX =
167+ // "(?:^|\\s|;)size\\s*\\(([^\\)]+)\\)\\s*\\;";
168+ // static private final String FULL_SCREEN_CONTENTS_REGEX =
169+ // "(?:^|\\s|;)fullScreen\\s*\\(([^\\)]+)\\)\\s*\\;";
170170// /** Test whether there's a void somewhere (the program has functions). */
171171// static private final String VOID_REGEX =
172172// "(?:^|\\s|;)void\\s";
@@ -208,9 +208,11 @@ public SurfaceInfo initSketchSize(String code,
208208 }
209209
210210
211- // break on commas, except those inside quotes, e.g.:
212- // size(300, 200, PDF, "output,weirdname.pdf");
213- // no handling for escaped (\") quotes
211+ /**
212+ * Break on commas, except those inside quotes,
213+ * e.g.: size(300, 200, PDF, "output,weirdname.pdf");
214+ * No special handling implemented for escaped (\") quotes.
215+ */
214216 static private StringList breakCommas (String contents ) {
215217 StringList outgoing = new StringList ();
216218
@@ -248,7 +250,8 @@ static private StringList breakCommas(String contents) {
248250
249251 /**
250252 * Parse a chunk of code and extract the size() command and its contents.
251- * @param code Usually the code from the main tab in the sketch
253+ * Also goes after fullScreen(), smooth(), and noSmooth().
254+ * @param code The code from the main tab in the sketch
252255 * @param fussy true if it should show an error message if bad size()
253256 * @return null if there was an error, otherwise an array (might contain some/all nulls)
254257 */
@@ -306,14 +309,28 @@ make sure that it uses numbers (or displayWidth/Height), copy into settings
306309 searchArea = sb .toString ();
307310 }
308311
309- // Get everything inside the parens for the size() method
310- String [] contents = PApplet .match (searchArea , SIZE_CONTENTS_REGEX );
311- if (contents != null ) {
312- //String[] matches = split on commas, but not commas inside quotes
312+ // First look for noSmooth() or smooth(N) so we can hoist it into settings.
313+ String smoothStatement = null ;
314+ String [] smoothContents = matchMethod ("smooth" , searchArea );
315+ if (smoothContents != null ) {
316+ smoothStatement = smoothContents [0 ];
317+ }
318+ String [] noContents = matchMethod ("noSmooth" , searchArea );
319+ if (noContents != null ) {
320+ if (smoothStatement != null ) {
321+ throw new SketchException ("smooth() and noSmooth() cannot be used in the same sketch" );
322+ } else {
323+ smoothStatement = noContents [0 ];
324+ }
325+ }
313326
314- StringList args = breakCommas (contents [1 ]);
327+ // Get everything inside the parens for the size() method
328+ //String[] contents = PApplet.match(searchArea, SIZE_CONTENTS_REGEX);
329+ String [] sizeContents = matchMethod ("size" , searchArea );
330+ if (sizeContents != null ) {
331+ StringList args = breakCommas (sizeContents [1 ]);
315332 SurfaceInfo info = new SurfaceInfo ();
316- info .statement = contents [0 ];
333+ info .statement = sizeContents [0 ];
317334 info .width = args .get (0 ).trim ();
318335 info .height = args .get (1 ).trim ();
319336 info .renderer = (args .size () >= 3 ) ? args .get (2 ).trim () : null ;
@@ -340,30 +357,69 @@ make sure that it uses numbers (or displayWidth/Height), copy into settings
340357 throw new SketchException ("Please fix the size() line to continue." , false );
341358 }
342359
360+ if (smoothStatement != null ) {
361+ info .statement += smoothStatement ;
362+ }
343363 info .checkEmpty ();
344364 return info ;
345365 //return new String[] { contents[0], width, height, renderer, path };
346366 }
347367 // if no size() found, check for fullScreen()
348- contents = PApplet .match (searchArea , FULL_SCREEN_CONTENTS_REGEX );
349- if (contents != null ) {
368+ //contents = PApplet.match(searchArea, FULL_SCREEN_CONTENTS_REGEX);
369+ String [] fullContents = matchMethod ("fullScreen" , searchArea );
370+ if (fullContents != null ) {
350371 SurfaceInfo info = new SurfaceInfo ();
351- info .statement = contents [0 ];
352- StringList args = breakCommas (contents [1 ]);
353- info .renderer = args .get (0 ).trim ();
354- info .display = args .size () > 1 ? args .get (1 ).trim () : null ;
372+ info .statement = fullContents [0 ];
373+ StringList args = breakCommas (fullContents [1 ]);
374+ if (args .size () > 0 ) { // might have no args
375+ String args0 = args .get (0 ).trim ();
376+ if (args .size () == 1 ) {
377+ // could be either fullScreen(1) or fullScreen(P2D), figure out which
378+ if (args0 .equals ("SPAN" ) || PApplet .parseInt (args0 , -1 ) != -1 ) {
379+ // it's the display parameter, not the renderer
380+ info .display = args0 ;
381+ } else {
382+ info .renderer = args0 ;
383+ }
384+ } else if (args .size () == 2 ) {
385+ info .renderer = args0 ;
386+ info .display = args .get (1 ).trim ();
387+ } else {
388+ throw new SketchException ("That's too many parameters for fullScreen()" );
389+ }
390+ }
355391 info .width = "displayWidth" ;
356392 info .height = "displayHeight" ;
393+ if (smoothStatement != null ) {
394+ info .statement += smoothStatement ;
395+ }
357396 info .checkEmpty ();
358397 return info ;
359398 }
360399
400+ // Made it this far, but no size() or fullScreen(), and still
401+ // need to pull out the noSmooth() and smooth(N) methods.
402+ if (smoothStatement != null ) {
403+ SurfaceInfo info = new SurfaceInfo ();
404+ info .statement = smoothStatement ;
405+ return info ;
406+ }
407+
361408 // not an error, just no size() specified
362409 //return new String[] { null, null, null, null, null };
363410 return new SurfaceInfo ();
364411 }
365412
366413
414+ static protected String [] matchMethod (String methodName , String searchArea ) {
415+ final String left = "(?:^|\\ s|;)" ;
416+ // doesn't match empty pairs of parens
417+ //final String right = "\\s*\\(([^\\)]+)\\)\\s*\\;";
418+ final String right = "\\ s*\\ (([^\\ )]*)\\ )\\ s*\\ ;" ;
419+ return PApplet .match (searchArea , left + methodName + right );
420+ }
421+
422+
367423 /**
368424 * Replace all commented portions of a given String as spaces.
369425 * Utility function used here and in the preprocessor.
0 commit comments