From 621d064545e81602b13f0689a50801a9a58f4313 Mon Sep 17 00:00:00 2001 From: Jakub Valtar Date: Fri, 4 Sep 2015 10:43:01 -0400 Subject: [PATCH] Clean up textFont and textSize, add impl --- core/src/processing/awt/PGraphicsJava2D.java | 10 +- core/src/processing/core/PGraphics.java | 73 +++++++-- core/src/processing/javafx/PGraphicsFX2D.java | 143 +++++++----------- .../processing/opengl/PGraphicsOpenGL.java | 7 +- 4 files changed, 116 insertions(+), 117 deletions(-) diff --git a/core/src/processing/awt/PGraphicsJava2D.java b/core/src/processing/awt/PGraphicsJava2D.java index f26ad3f3d..a35936b8a 100644 --- a/core/src/processing/awt/PGraphicsJava2D.java +++ b/core/src/processing/awt/PGraphicsJava2D.java @@ -1889,15 +1889,11 @@ public class PGraphicsJava2D extends PGraphics { /** * Same as parent, but override for native version of the font. *

- * Also gets called by textFont, so the metrics + * Called from textFontImpl and textSizeImpl, so the metrics * will get recorded properly. */ @Override - public void textSize(float size) { - if (textFont == null) { - defaultFontOrDeath("textSize", size); - } - + protected void setTextSize(float size) { // if a native version available, derive this font // if (textFontNative != null) { // textFontNative = textFontNative.deriveFont(size); @@ -1931,7 +1927,7 @@ public class PGraphicsJava2D extends PGraphics { // take care of setting the textSize and textLeading vars // this has to happen second, because it calls textAscent() // (which requires the native font metrics to be set) - super.textSize(size); + super.setTextSize(size); } diff --git a/core/src/processing/core/PGraphics.java b/core/src/processing/core/PGraphics.java index 8b4325b27..75c2ac081 100644 --- a/core/src/processing/core/PGraphics.java +++ b/core/src/processing/core/PGraphics.java @@ -4171,8 +4171,43 @@ public class PGraphics extends PImage implements PConstants { * @see PGraphics#textSize(float) */ public void textFont(PFont which) { - if (which != null) { - textFont = which; + if (which == null) { + throw new RuntimeException(ERROR_TEXTFONT_NULL_PFONT); + } + textFontImpl(which, which.getDefaultSize()); + } + + + /** + * @param size the size of the letters in units of pixels + */ + public void textFont(PFont which, float size) { + if (which == null) { + throw new RuntimeException(ERROR_TEXTFONT_NULL_PFONT); + } + // https://github.com/processing/processing/issues/3110 + if (size <= 0) { + // Using System.err instead of showWarning to avoid running out of + // memory with a bunch of textSize() variants (cause of this bug is + // usually something done with map() or in a loop). + System.err.println("textFont: ignoring size " + size + " px:" + + "the text size must be larger than zero"); + size = textSize; + } + textFontImpl(which, size); + } + + + /** + * Called from textFont. Check the validity of args and + * print possible errors to the user before calling this. + * Subclasses will want to override this one. + * + * @param which font to set, not null + * @param size size to set, greater than zero + */ + protected void textFontImpl(PFont which, float size) { + textFont = which; // if (hints[ENABLE_NATIVE_FONTS]) { // //if (which.font == null) { // which.findNative(); @@ -4201,20 +4236,8 @@ public class PGraphics extends PImage implements PConstants { // float w = font.getStringBounds(text, g2.getFontRenderContext()).getWidth(); } */ - textSize(which.getDefaultSize()); - } else { - throw new RuntimeException(ERROR_TEXTFONT_NULL_PFONT); - } - } - - - /** - * @param size the size of the letters in units of pixels - */ - public void textFont(PFont which, float size) { - textFont(which); - textSize(size); + setTextSize(size); } @@ -4328,6 +4351,26 @@ public class PGraphics extends PImage implements PConstants { if (textFont == null) { defaultFontOrDeath("textSize", size); } + textSizeImpl(size); + } + + + /** + * Called from textSize() after validating size. Subclasses + * will want to override this one. + * @param size size of the text, greater than zero + */ + protected void textSizeImpl(float size) { + setTextSize(size); + } + + + /** + * Sets the actual size. Called from textSizeImpl and + * from textFontImpl after setting the font. + * @param size size of the text, greater than zero + */ + protected void setTextSize(float size) { textSize = size; textLeading = (textAscent() + textDescent()) * 1.275f; } diff --git a/core/src/processing/javafx/PGraphicsFX2D.java b/core/src/processing/javafx/PGraphicsFX2D.java index a9534fdaa..ef275c2d2 100644 --- a/core/src/processing/javafx/PGraphicsFX2D.java +++ b/core/src/processing/javafx/PGraphicsFX2D.java @@ -1313,102 +1313,13 @@ public class PGraphicsFX2D extends PGraphics { } - @Override - public void textSize(float size) { - if (size <= 0) { - // Using System.err instead of showWarning to avoid running out of - // memory with a bunch of textSize() variants (cause of this bug is - // usually something done with map() or in a loop). - System.err.println("textSize(" + size + ") ignored: " + - "the text size must be larger than zero"); - return; - } - if (textFont == null) { - defaultFontOrDeath("textSize", size); - } - textFontImpl(textFont, size); - } - - - @Override - public void textFont(PFont which) { - if (which != null) { - textFontImpl(which, which.getDefaultSize()); - } else { - throw new RuntimeException(ERROR_TEXTFONT_NULL_PFONT); - } - } - - - @Override - public void textFont(PFont which, float size) { - if (which != null) { - if (size <= 0) { - // Using System.err instead of showWarning to avoid running out of - // memory with a bunch of textSize() variants (cause of this bug is - // usually something done with map() or in a loop). - System.err.println("textFont with size " + size + " ignored: " + - "the text size must be larger than zero"); - size = textSize; - } - textFontImpl(which, size); - } else { - throw new RuntimeException(ERROR_TEXTFONT_NULL_PFONT); - } - } - - - /** - * Sets the font and the size. Check the validity of args and - * print possible errors to the user before calling this. - * - * @param which font to set, not null - * @param size size to set, greater than zero - */ - protected void textFontImpl(PFont which, float size) { - textFont = which; - textSize = size; - - String fontName = which.getName(); - - textFontInfo = fontCache.get(fontName, size); - if (textFontInfo == null) { - Font font; - String filename = fontCache.nameToFilename.get(fontName); - if (filename != null) { - font = Font.loadFont(parent.createInput(filename), size); - } else { - font = new Font(fontName, size); - } - - // Loading from file is guaranteed to succeed, font was already - // successfully loaded in createFont(). - // Loading system font may return fallback font if the font - // does not exist; this can be detected by comparing font names. - // Please note that some font names can differ in FX vs. AWT. - boolean isFallbackFont = filename == null && - !fontName.equalsIgnoreCase(font.getName()); - textFontInfo = fontCache.createFontInfo(font, isFallbackFont); - fontCache.put(fontName, size, textFontInfo); - } - - context.setFont(textFontInfo.font); - fontCache.measuringText.setFont(textFontInfo.font); - if (textFontInfo.isFallbackFont) { - textLeading = (textFont.ascent() + textFont.descent()) * 1.275f; - } else { - textLeading = (textFontInfo.ascent + textFontInfo.descent) * 1.275f; - } - } - - @Override public float textAscent() { if (textFont == null) { defaultFontOrDeath("textAscent"); } if (textFontInfo.isFallbackFont) { - return textFont.ascent(); + return super.textAscent(); } return textFontInfo.ascent; } @@ -1420,7 +1331,7 @@ public class PGraphicsFX2D extends PGraphics { defaultFontOrDeath("textDescent"); } if (textFontInfo.isFallbackFont) { - return textFont.descent(); + return super.textDescent(); } return textFontInfo.descent; } @@ -1518,6 +1429,56 @@ public class PGraphicsFX2D extends PGraphics { // TEXT IMPL + @Override + protected void textFontImpl(PFont which, float size) { + setTextFont(which, size); + setTextSize(size); + } + + + @Override + protected void textSizeImpl(float size) { + setTextFont(textFont, size); + setTextSize(size); + } + + /** + * FX specific. When setting font or size, new font has to + * be created. Both textFontImpl and textSizeImpl call this one. + * @param which font to be set, not null + * @param size size to be set, greater than zero + */ + protected void setTextFont(PFont which, float size) { + textFont = which; + + String fontName = which.getName(); + + textFontInfo = fontCache.get(fontName, size); + if (textFontInfo == null) { + Font font; + String filename = fontCache.nameToFilename.get(fontName); + if (filename != null) { + font = Font.loadFont(parent.createInput(filename), size); + } else { + font = new Font(fontName, size); + } + + // Loading from file is guaranteed to succeed, font was already + // successfully loaded in createFont(). + // Loading system font may return fallback font if the font + // does not exist; this can be detected by comparing font names. + // Please note that some font names can differ in FX vs. AWT. + boolean isFallbackFont = filename == null && + !fontName.equalsIgnoreCase(font.getName()); + textFontInfo = fontCache.createFontInfo(font, isFallbackFont); + fontCache.put(fontName, size, textFontInfo); + } + + context.setFont(textFontInfo.font); + fontCache.measuringText.setFont(textFontInfo.font); + } + + @Override protected void textLineImpl(char[] buffer, int start, int stop, float x, float y) { if (textFontInfo.isFallbackFont) { diff --git a/core/src/processing/opengl/PGraphicsOpenGL.java b/core/src/processing/opengl/PGraphicsOpenGL.java index 80bdf2ba8..1f3fe3eb7 100644 --- a/core/src/processing/opengl/PGraphicsOpenGL.java +++ b/core/src/processing/opengl/PGraphicsOpenGL.java @@ -4050,7 +4050,7 @@ public class PGraphicsOpenGL extends PGraphics { @Override public float textDescent() { - if (textFont == null) defaultFontOrDeath("textAscent"); + if (textFont == null) defaultFontOrDeath("textDescent"); Font font = (Font) textFont.getNative(); float descent = 0; if (font != null) descent = pgl.getFontDescent(font); @@ -4070,14 +4070,13 @@ public class PGraphicsOpenGL extends PGraphics { @Override - public void textSize(float size) { - if (textFont == null) defaultFontOrDeath("textSize", size); + protected void setTextSize(float size) { Font font = (Font) textFont.getNative(); if (font != null) { Object dfont = pgl.getDerivedFont(font, size); textFont.setNative(dfont); } - super.textSize(size); + super.setTextSize(size); }