public int fontSize() { return intf.app.g.textFont.getSize(); }
/** * Create a bitmap font on the fly from either a font name that's * installed on the system, or from a .ttf or .otf that's inside * the data folder of this sketch. * <p/> * Use 'null' for the charset if you want to dynamically create * character bitmaps only as they're needed. */ public PFont createFont(String name, float size, boolean smooth, char[] charset) { String lowerName = name.toLowerCase(); Typeface baseFont = null; if (lowerName.endsWith(".otf") || lowerName.endsWith(".ttf")) { AssetManager assets = activity.getAssets(); baseFont = Typeface.createFromAsset(assets, name); } else { baseFont = (Typeface) PFont.findNative(name); } return new PFont(baseFont, round(size), smooth, charset); }
/** * Get index for the character. * @return index into arrays or -1 if not found */ protected int index(char c) { if (lazy) { int index = indexActual(c); if (index != -1) { return index; } // if (font.canDisplay(c)) { // create the glyph addGlyph(c); // now where did i put that? return indexActual(c); // } else { // return -1; // } } else { return indexActual(c); } }
public void addAllGlyphsToTexture(PGraphicsOpenGL pg, PFont font) { // loop over current glyphs. for (int i = 0; i < font.getGlyphCount(); i++) { addToTexture(pg, i, font.getGlyph(i)); } }
protected void textCharImpl(char ch, float x, float y) { //, float z) { PFont.Glyph glyph = textFont.getGlyph(ch); if (glyph != null) { if (textMode == MODEL) { float high = glyph.height / (float) textFont.getSize(); float bwidth = glyph.width / (float) textFont.getSize(); float lextent = glyph.leftExtent / (float) textFont.getSize(); float textent = glyph.topExtent / (float) textFont.getSize(); float x1 = x + lextent * textSize; float y1 = y - textent * textSize; float x2 = x1 + bwidth * textSize; float y2 = y1 + high * textSize; textCharModelImpl(glyph.image, x1, y1, x2, y2, glyph.width, glyph.height); } } else if (ch != ' ' && ch != 127) { showWarning("No glyph found for the " + ch + " (\\u" + PApplet.hex(ch, 4) + ") character"); } }
@Override public void textFont(PFont which) { super.textFont(which); fillPaint.setTypeface((Typeface) which.getNative()); }
protected void initTexture(PGraphicsOpenGL pg, PFont font) { lastTex = -1; int spow = PGL.nextPowerOfTwo(font.getSize()); minSize = PApplet.min(PGraphicsOpenGL.maxTextureSize, PApplet.max(PGL.MIN_FONT_TEX_SIZE, spow)); maxSize = PApplet.min(PGraphicsOpenGL.maxTextureSize, PApplet.max(PGL.MAX_FONT_TEX_SIZE, 2 * spow)); if (maxSize < spow) { PGraphics.showWarning("The font size is too large to be properly " + "displayed with OpenGL"); } addTexture(pg); offsetX = 0; offsetY = 0; lineHeight = 0; texinfoMap = new HashMap<PFont.Glyph, TextureInfo>(); glyphTexinfos = new TextureInfo[font.getGlyphCount()]; addAllGlyphsToTexture(pg, font); }
textFont = which; String fontName = which.getName(); String fontPsName = which.getPostScriptName(); Font font = null; if (which.isStream()) { if (font == null && which.getNative() != null) {
protected PFont createFont(String name, float size, boolean smooth, char[] charset) { String lowerName = name.toLowerCase(); Font baseFont = null; try { InputStream stream = null; if (lowerName.endsWith(".otf") || lowerName.endsWith(".ttf")) { stream = parent.createInput(name); if (stream == null) { System.err.println("The font \"" + name + "\" " + "is missing or inaccessible, make sure " + "the URL is valid or that the file has been " + "added to your sketch and is readable."); return null; } baseFont = Font.createFont(Font.TRUETYPE_FONT, parent.createInput(name)); } else { baseFont = PFont.findFont(name); } return new PFont(baseFont.deriveFont(size * parent.pixelDensity), smooth, charset, stream != null, parent.pixelDensity); } catch (Exception e) { System.err.println("Problem with createFont(\"" + name + "\")"); e.printStackTrace(); return null; } }
public PFont loadFont(String filename) { try { InputStream input = createInput(filename); return new PFont(input); } catch (Exception e) { die("Could not load font " + filename + ". " + "Make sure that the font has been copied " + "to the data folder of your sketch.", e); } return null; }
/** * Sets the current font. The font's size will be the "natural" * size of this font (the size that was set when using "Create Font"). * The leading will also be reset. */ public void textFont(PFont which) { if (which == null) { throw new RuntimeException(ERROR_TEXTFONT_NULL_PFONT); } textFontImpl(which, which.getDefaultSize()); }
/** * Returns the ascent of the current font at the current size. * This is a method, rather than a variable inside the PGraphics object * because it requires calculation. */ public float textAscent() { if (textFont == null) { defaultFontOrDeath("textAscent"); } return textFont.ascent() * textSize; }
/** * Returns the descent of the current font at the current size. * This is a method, rather than a variable inside the PGraphics object * because it requires calculation. */ public float textDescent() { if (textFont == null) { defaultFontOrDeath("textDescent"); } return textFont.descent() * textSize; }
public Glyph getGlyph(char c) { int index = index(c); return (index == -1) ? null : glyphs[index]; }
protected void textCharImpl(char ch, float x, float y) { PFont.Glyph glyph = textFont.getGlyph(ch); if (glyph != null) { if (textMode == MODEL) { float high = glyph.height / (float) textFont.size; float bwidth = glyph.width / (float) textFont.size; float lextent = glyph.leftExtent / (float) textFont.size; float textent = glyph.topExtent / (float) textFont.size; float x1 = x + lextent * textSize; float y1 = y - textent * textSize; float x2 = x1 + bwidth * textSize; float y2 = y1 + high * textSize; textCharModelImpl(glyph.image, x1, y1, x2, y2, glyph.width, glyph.height); } } }
protected PImage getTintedGlyphImage(PFont.Glyph glyph, int tintColor) { if (textFontInfo.tintCache == null) { textFontInfo.tintCache = new LinkedHashMap<Integer, PImage[]>(16, 0.75f, true) { @Override protected boolean removeEldestEntry(Map.Entry<Integer, PImage[]> eldest) { return size() > FontInfo.MAX_CACHED_COLORS_PER_FONT; } }; } PImage[] tintedGlyphs = textFontInfo.tintCache.get(tintColor); int index = glyph.index; if (tintedGlyphs == null || tintedGlyphs.length <= index) { PImage[] newArray = new PImage[textFont.getGlyphCount()]; if (tintedGlyphs != null) { System.arraycopy(tintedGlyphs, 0, newArray, 0, tintedGlyphs.length); } tintedGlyphs = newArray; textFontInfo.tintCache.put(tintColor, tintedGlyphs); } PImage tintedGlyph = tintedGlyphs[index]; if (tintedGlyph == null) { tintedGlyph = glyph.image.copy(); tintedGlyphs[index] = tintedGlyph; } return tintedGlyph; }
@Override protected void textCharImpl(char ch, float x, float y) { PFont.Glyph glyph = textFont.getGlyph(ch); if (glyph != null) { if (textMode == MODEL) { FontTexture.TextureInfo tinfo = textTex.getTexInfo(glyph); if (tinfo == null) { // Adding new glyph to the font texture. tinfo = textTex.addToTexture(this, glyph); } float high = glyph.height / (float) textFont.getSize(); float bwidth = glyph.width / (float) textFont.getSize(); float lextent = glyph.leftExtent / (float) textFont.getSize(); float textent = glyph.topExtent / (float) textFont.getSize(); float x1 = x + lextent * textSize; float y1 = y - textent * textSize; float x2 = x1 + bwidth * textSize; float y2 = y1 + high * textSize; textCharModelImpl(tinfo, x1, y1, x2, y2); } else if (textMode == SHAPE) { textCharShapeImpl(ch, x, y); } } }