From 2ced8ea9d2e72d03ba46a5a56e8a68288957360a Mon Sep 17 00:00:00 2001 From: codeanticode Date: Fri, 15 Jan 2010 20:50:41 +0000 Subject: [PATCH] GLFont added --- .../processing/android/core/GLConstants.java | 46 +-- .../src/processing/android/core/GLFont.java | 357 ++++++++++++++++++ .../processing/android/core/GLTexture.java | 182 +++++---- .../src/processing/android/core/PApplet.java | 34 ++ .../src/processing/android/core/PImage.java | 2 +- 5 files changed, 505 insertions(+), 116 deletions(-) create mode 100644 android/core/src/processing/android/core/GLFont.java diff --git a/android/core/src/processing/android/core/GLConstants.java b/android/core/src/processing/android/core/GLConstants.java index 33665ab48..402374051 100644 --- a/android/core/src/processing/android/core/GLConstants.java +++ b/android/core/src/processing/android/core/GLConstants.java @@ -17,9 +17,9 @@ public interface GLConstants { public static final int TEX_ONEDIM = 2; /** - * This constant identifies the texture internal format GL_RGBA: 4 color components of 8 bits each. + * This constant identifies the texture internal format GL_RGBA: 4 color components of 8 bits each, identical as ARGB. */ - public static final int COLOR = 0; + public static final int COLOR = 2; /** * This constant identifies the texture internal format GL_RGBA16F_ARB: 4 float compontents of 16 bits each. */ @@ -29,48 +29,6 @@ public interface GLConstants { */ public static final int DOUBLE = 2; - /** - * This constant identifies an image buffer that contains only RED channel info. - */ - public static final int TEX1 = 0; - - /** - * This constant identifies an image buffer that contains only GREEN channel info. - */ - //public static final int GREEN = 0; - - /** - * This constant identifies an image buffer that contains only BLUE channel info. - */ - //public static final int BLUE = 0; - /** - * This constant identifies an image buffer that contains only ALPHA channel info. - */ - //public static final int ALPHA = 0; Already defined in Processing with value = 4 - - - - /** - * This constant identifies a texture with 3 color components. - */ - public static final int TEX3 = 1; - - /** - * This constant identifies an image buffer that contains RGB channel info. - */ - //public static final int RGB = 0; Already defined in Processing with value = 1 - - /** - * This constant identifies an image buffer that contains RGB channel info. - */ - //public static final int ARGB = 0; Already defined in Processing with value = 2 - - - /** - * This constant identifies a texture with 4 color components. - */ - public static final int TEX4 = 2; - /** * This constant identifies an integer texture buffer. */ diff --git a/android/core/src/processing/android/core/GLFont.java b/android/core/src/processing/android/core/GLFont.java new file mode 100644 index 000000000..0ec13fd73 --- /dev/null +++ b/android/core/src/processing/android/core/GLFont.java @@ -0,0 +1,357 @@ +/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */ + +/* + Part of the Processing project - http://processing.org + + Copyright (c) 2004-07 Ben Fry & Casey Reas + Copyright (c) 2001-04 Massachusetts Institute of Technology + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General + Public License along with this library; if not, write to the + Free Software Foundation, Inc., 59 Temple Place, Suite 330, + Boston, MA 02111-1307 USA +*/ + +package processing.android.core; + +//import java.awt.*; +//import java.awt.image.BufferedImage; +//import java.awt.image.Raster; +import java.io.*; +//import java.lang.reflect.*; +import java.util.Arrays; +import java.util.HashMap; + +import android.graphics.Bitmap; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.graphics.Typeface; +import android.graphics.Bitmap.Config; + + +public class GLFont extends PFont { + public GLFont(PApplet parent, InputStream input) throws IOException { + DataInputStream is = new DataInputStream(input); + + // number of character images stored in this font + charCount = is.readInt(); + + // bit count is ignored since this is always 8 + //int numBits = is.readInt(); + // used to be the bitCount, but now used for version number. + // version 8 is any font before 69, so 9 is anything from 83+ + // 9 was buggy so gonna increment to 10. + int version = is.readInt(); + + // this was formerly ignored, now it's the actual font size + //mbox = is.readInt(); + size = is.readInt(); + // this was formerly mboxY, the one that was used + // this will make new fonts downward compatible + //mbox2 = is.readInt(); + mbox2 = is.readInt(); + + fwidth = size; //mbox; + fheight = size; //mbox; + + // size for image ("texture") is next power of 2 + // over the font size. for most vlw fonts, the size is 48 + // so the next power of 2 is 64. + // double-check to make sure that mbox2 is a power of 2 + // there was a bug in the old font generator that broke this + //mbox2 = (int) Math.pow(2, Math.ceil(Math.log(mbox2) / Math.log(2))); + mbox2 = (int) Math.pow(2, Math.ceil(Math.log(mbox2) / Math.log(2))); + // size for the texture is stored in the font + twidth = theight = mbox2; //mbox2; + + ascent = is.readInt(); // formerly baseHt (zero/ignored) + descent = is.readInt(); // formerly ignored struct padding + + // allocate enough space for the character info + value = new int[charCount]; + height = new int[charCount]; + width = new int[charCount]; + setWidth = new int[charCount]; + topExtent = new int[charCount]; + leftExtent = new int[charCount]; + + ascii = new int[128]; + for (int i = 0; i < 128; i++) ascii[i] = -1; + + // read the information about the individual characters + for (int i = 0; i < charCount; i++) { + value[i] = is.readInt(); + height[i] = is.readInt(); + width[i] = is.readInt(); + setWidth[i] = is.readInt(); + topExtent[i] = is.readInt(); + leftExtent[i] = is.readInt(); + + // pointer in the c version, ignored + is.readInt(); + + // cache locations of the ascii charset + if (value[i] < 128) ascii[value[i]] = i; + + // the values for getAscent() and getDescent() from FontMetrics + // seem to be way too large.. perhaps they're the max? + // as such, use a more traditional marker for ascent/descent + if (value[i] == 'd') { + if (ascent == 0) ascent = topExtent[i]; + } + if (value[i] == 'p') { + if (descent == 0) descent = -topExtent[i] + height[i]; + } + } + + // not a roman font, so throw an error and ask to re-build. + // that way can avoid a bunch of error checking hacks in here. + if ((ascent == 0) && (descent == 0)) { + throw new RuntimeException("Please use \"Create Font\" to " + + "re-create this font."); + } + + images = new GLTexture[charCount]; + GLTexture tex; + for (int i = 0; i < charCount; i++) { + tex = new GLTexture(parent, twidth, theight, ARGB); + tex.loadPixels(); + int bitmapSize = height[i] * width[i]; + + byte temp[] = new byte[bitmapSize]; + is.readFully(temp); + + // convert the bitmap to an alpha channel + int w = width[i]; + int h = height[i]; + for (int y = 0; y < h; y++) { + for (int x = 0; x < w; x++) { + int valu = temp[y*w + x] & 0xff; + tex.pixels[y * twidth + x] = valu << 24 | 255 << 16 | 255 << 8 | 255; + //(valu << 24) | 0xFFFFFF; // windows + //0xFFFFFF00 | valu; // macosx + + //System.out.print((images[i].pixels[y*64+x] > 128) ? "*" : "."); + } + //System.out.println(); + } + tex.loadTexture(); + images[i] = tex; + //System.out.println(); + } + + if (version >= 10) { // includes the font name at the end of the file + name = is.readUTF(); + psname = is.readUTF(); + } + if (version == 11) { + smooth = is.readBoolean(); + } + } + + /** + * Create a new image-based font on the fly. + * + * @param font the font object to create from + * @param charset array of all unicode chars that should be included + * @param smooth true to enable smoothing/anti-aliasing + */ + public GLFont(PApplet parent, Typeface font, int size, boolean smooth, char charset[]) { + // save this so that we can use the native version + this.font = font; + this.smooth = smooth; + +// name = font.getName(); +// psname = font.getPSName(); + + // fix regression from sorting (bug #564) + if (charset != null) { + // charset needs to be sorted to make index lookup run more quickly + // http://dev.processing.org/bugs/show_bug.cgi?id=494 + Arrays.sort(charset); + } + + // the count gets reset later based on how many of + // the chars are actually found inside the font. + this.charCount = (charset == null) ? 65536 : charset.length; + this.size = size; + + fwidth = fheight = size; + + PImage bitmaps[] = new PImage[charCount]; + + // allocate enough space for the character info + value = new int[charCount]; + height = new int[charCount]; + width = new int[charCount]; + setWidth = new int[charCount]; + topExtent = new int[charCount]; + leftExtent = new int[charCount]; + + ascii = new int[128]; + for (int i = 0; i < 128; i++) ascii[i] = -1; + + int mbox3 = size * 3; + +// BufferedImage playground = +// new BufferedImage(mbox3, mbox3, BufferedImage.TYPE_INT_RGB); + Bitmap playground = Bitmap.createBitmap(mbox3, mbox3, Config.ARGB_8888); +// Graphics2D g = (Graphics2D) playground.getGraphics(); + Canvas canvas = new Canvas(playground); +// g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, +// smooth ? +// RenderingHints.VALUE_ANTIALIAS_ON : +// RenderingHints.VALUE_ANTIALIAS_OFF); + Paint paint = new Paint(); + paint.setAntiAlias(smooth); + +// g.setFont(font); +// FontMetrics metrics = g.getFontMetrics(); + paint.setTypeface(font); + paint.setTextSize(size); + + int samples[] = new int[mbox3 * mbox3]; + + int maxWidthHeight = 0; + int index = 0; + for (int i = 0; i < charCount; i++) { + char c = (charset == null) ? (char)i : charset[i]; + +// if (!font.canDisplay(c)) { // skip chars not in the font +// continue; +// } + +// g.setColor(Color.white); +// g.fillRect(0, 0, mbox3, mbox3); + canvas.drawColor(Color.WHITE); +// g.setColor(Color.black); + paint.setColor(Color.BLACK); +// g.drawString(String.valueOf(c), size, size * 2); + canvas.drawText(String.valueOf(c), size, size * 2, paint); + + // grabs copy of the current data.. so no updates (do each time) +// Raster raster = playground.getData(); +// raster.getSamples(0, 0, mbox3, mbox3, 0, samples); + playground.getPixels(samples, 0, mbox3, 0, 0, mbox3, mbox3); + + int minX = 1000, maxX = 0; + int minY = 1000, maxY = 0; + boolean pixelFound = false; + + for (int y = 0; y < mbox3; y++) { + for (int x = 0; x < mbox3; x++) { + //int sample = raster.getSample(x, y, 0); // maybe? + int sample = samples[y * mbox3 + x] & 0xff; + // or int samples[] = raster.getPixel(x, y, null); + + //if (sample == 0) { // or just not white? hmm + if (sample != 255) { + if (x < minX) minX = x; + if (y < minY) minY = y; + if (x > maxX) maxX = x; + if (y > maxY) maxY = y; + pixelFound = true; + } + } + } + + if (!pixelFound) { + minX = minY = 0; + maxX = maxY = 0; + // this will create a 1 pixel white (clear) character.. + // maybe better to set one to -1 so nothing is added? + } + + value[index] = c; + height[index] = (maxY - minY) + 1; + width[index] = (maxX - minX) + 1; +// setWidth[index] = metrics.charWidth(c); + setWidth[index] = (int) paint.measureText(String.valueOf(c)); + //System.out.println((char)c + " " + setWidth[index]); + + // cache locations of the ascii charset + //if (value[i] < 128) ascii[value[i]] = i; + if (c < 128) ascii[c] = index; + + // offset from vertical location of baseline + // of where the char was drawn (size*2) + topExtent[index] = size*2 - minY; + + // offset from left of where coord was drawn + leftExtent[index] = minX - size; + + if (c == 'd') { + ascent = topExtent[index]; + } + if (c == 'p') { + descent = -topExtent[index] + height[index]; + } + + if (width[index] > maxWidthHeight) maxWidthHeight = width[index]; + if (height[index] > maxWidthHeight) maxWidthHeight = height[index]; + + bitmaps[index] = new PImage(width[index], height[index], ARGB); + + for (int y = minY; y <= maxY; y++) { + for (int x = minX; x <= maxX; x++) { + int val = 255 - (samples[y * mbox3 + x] & 0xff); + int pindex = (y - minY) * width[index] + (x - minX); + bitmaps[index].pixels[pindex] = val << 24 | 255 << 16 | 255 << 8 | 255; + } + } + index++; + } + charCount = index; + + // foreign font, so just make ascent the max topExtent + if ((ascent == 0) && (descent == 0)) { + for (int i = 0; i < charCount; i++) { + char cc = (char) value[i]; + if (Character.isWhitespace(cc) || + (cc == '\u00A0') || (cc == '\u2007') || (cc == '\u202F')) { + continue; + } + if (topExtent[i] > ascent) { + ascent = topExtent[i]; + } + int d = -topExtent[i] + height[i]; + if (d > descent) { + descent = d; + } + } + } + // size for image/texture is next power of 2 over largest char + mbox2 = (int) + Math.pow(2, Math.ceil(Math.log(maxWidthHeight) / Math.log(2))); + twidth = theight = mbox2; + + // shove the smaller PImage data into textures of next-power-of-2 size, + // so that this font can be used immediately by p5. + images = new GLTexture[charCount]; + GLTexture tex; + for (int i = 0; i < charCount; i++) { + tex = new GLTexture(parent, mbox2, mbox2, ARGB); + tex.loadPixels(); + for (int y = 0; y < height[i]; y++) { + System.arraycopy(bitmaps[i].pixels, y*width[i], + tex.pixels, y*mbox2, + width[i]); + } + tex.loadTexture(); + images[i] = tex; + bitmaps[i] = null; + } + } + +} diff --git a/android/core/src/processing/android/core/GLTexture.java b/android/core/src/processing/android/core/GLTexture.java index fc9a1ee89..fe7b18e96 100644 --- a/android/core/src/processing/android/core/GLTexture.java +++ b/android/core/src/processing/android/core/GLTexture.java @@ -11,6 +11,8 @@ import java.nio.*; // Fix the setBuffer methods to make them into setTexChannel or something like that. // Overload the loadPixels and updatePixels so that texture info is copies to data[] array instead to // pixels[] when the texture is floating point. +// Properly implement putBuffer method to copy ALPHA, RGB and RGBA buffers to the texture. Now it has been +// quickly hacked to properly use alpha textures needed by GLFonts. /** * This class adds an opengl texture to a PImage object. The texture is @@ -69,7 +71,7 @@ public class GLTexture extends PImage implements PConstants, GLConstants */ public GLTexture(PApplet parent, int width, int height, Parameters params) { - super(width, height, ARGB); + super(width, height, params.format); this.parent = parent; pgl = (PGraphicsAndroid3D)parent.g; @@ -90,7 +92,7 @@ public class GLTexture extends PImage implements PConstants, GLConstants */ public GLTexture(PApplet parent, int width, int height, int format) { - super(width, height, ARGB); + super(width, height, format); this.parent = parent; pgl = (PGraphicsAndroid3D)parent.g; @@ -112,7 +114,7 @@ public class GLTexture extends PImage implements PConstants, GLConstants */ public GLTexture(PApplet parent, int width, int height, int format, int filter) { - super(width, height, ARGB); + super(width, height, format); this.parent = parent; pgl = (PGraphicsAndroid3D)parent.g; @@ -142,7 +144,7 @@ public class GLTexture extends PImage implements PConstants, GLConstants public GLTexture(PApplet parent, int width, int height, Parameters params, int id) { - super(width, height, ARGB); + super(width, height, params.format); this.parent = parent; pgl = (PGraphicsAndroid3D)parent.g; @@ -161,7 +163,7 @@ public class GLTexture extends PImage implements PConstants, GLConstants */ public GLTexture(PApplet parent, String filename, Parameters params) { - super(1, 1, ARGB); + super(1, 1, params.format); this.parent = parent; pgl = (PGraphicsAndroid3D)parent.g; @@ -179,7 +181,7 @@ public class GLTexture extends PImage implements PConstants, GLConstants */ public GLTexture(PApplet parent, String filename, int format) { - super(1, 1, ARGB); + super(1, 1, format); this.parent = parent; pgl = (PGraphicsAndroid3D)parent.g; @@ -198,7 +200,7 @@ public class GLTexture extends PImage implements PConstants, GLConstants */ public GLTexture(PApplet parent, String filename, int format, int filter) { - super(1, 1, ARGB); + super(1, 1, format); this.parent = parent; pgl = (PGraphicsAndroid3D)parent.g; @@ -237,7 +239,7 @@ public class GLTexture extends PImage implements PConstants, GLConstants */ public GLTexture(PApplet parent, int size, Parameters params) { - super(1, 1, ARGB); + super(1, 1, params.format); this.parent = parent; pgl = (PGraphicsAndroid3D)parent.g; @@ -269,8 +271,8 @@ public class GLTexture extends PImage implements PConstants, GLConstants */ public void init(int width, int height, Parameters params) { - super.init(width, height, ARGB); - setTextureParams(params); + super.init(width, height, params.format); + setTextureParams(params); initTexture(width, height); } @@ -457,8 +459,19 @@ public class GLTexture extends PImage implements PConstants, GLConstants } // Putting into texture. - putBuffer(img.pixels); - } + if (texInternalFormat == GL10.GL_RGB) + { + putBuffer(img.pixels, RGB); + } + if (texInternalFormat == GL10.GL_RGBA) + { + putBuffer(img.pixels, ARGB); + } + if (texInternalFormat == GL10.GL_ALPHA) + { + putBuffer(img.pixels, ALPHA); + } + } /** * Puts the pixels of img inside the rectangle (x, y, x+w, y+h) into texture only. @@ -489,7 +502,18 @@ public class GLTexture extends PImage implements PConstants, GLConstants } // Putting into texture. - putBuffer(dest); + if (texInternalFormat == GL10.GL_RGB) + { + putBuffer(dest, RGB); + } + if (texInternalFormat == GL10.GL_RGBA) + { + putBuffer(dest, ARGB); + } + if (texInternalFormat == GL10.GL_ALPHA) + { + putBuffer(dest, ALPHA); + } } /** @@ -570,7 +594,19 @@ public class GLTexture extends PImage implements PConstants, GLConstants */ public void loadTexture() { - putBuffer(pixels); + // Putting into texture. + if (texInternalFormat == GL10.GL_RGB) + { + putBuffer(pixels, RGB); + } + if (texInternalFormat == GL10.GL_RGBA) + { + putBuffer(pixels, ARGB); + } + if (texInternalFormat == GL10.GL_ALPHA) + { + putBuffer(pixels, ALPHA); + } } /** @@ -677,7 +713,7 @@ public class GLTexture extends PImage implements PConstants, GLConstants */ public void putBuffer(int[] intArray) { - putBuffer(intArray, TEX4, TEX_BYTE); + putBuffer(intArray, ARGB, TEX_BYTE); } /** @@ -725,30 +761,28 @@ public class GLTexture extends PImage implements PConstants, GLConstants int[] convArray = intArray; int glFormat; - if (format == TEX1) + if (format == ALPHA) { - glFormat = GL10.GL_LUMINANCE; - if (type == TEX_BYTE) - { - convArray = convertToRGBA(intArray, ALPHA); - /* - - PApplet.println("Hey"); - int[] convArray2 = new int[width * height]; - for (int i = 0; i < width * height; i++) - { - convArray2[i] = 0x88000000; - } - gl.glBindTexture(texTarget, tex[0]); - gl.glTexSubImage2D(texTarget, 0, 0, 0, width, height, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, IntBuffer.wrap(convArray2)); - gl.glBindTexture(texTarget, 0); - PApplet.println("CHAU"); - return; - */ - } + glFormat = GL10.GL_ALPHA; + if (type == TEX_BYTE) + { + byte[] convArray2 = convertToAlpha(intArray); + gl.glBindTexture(texTarget, tex[0]); + gl.glTexSubImage2D(texTarget, 0, 0, 0, width, height, glFormat, GL10.GL_UNSIGNED_BYTE, ByteBuffer.wrap(convArray2)); + gl.glBindTexture(texTarget, 0); + return; + } } - else if (format == TEX3) + else if (format == RGB) { + // If in the previous case, we need to use a byte array, here with RGB we should use a byte array with 3 bytes per color, + // i.e.: byte[] convArray3 = byte[3 * widht * height] and then storing RGB components from intArray as follows: + // for (int i = 0; i < width * height; i++) { + // convArray3[3 * i ] = red(intArray[i]); + // convArray3[3 * i + 1] = green(intArray[i]); + // convArray3[3 * i + 2] = blue(intArray[i]); + //} + // where the red, green, green and blue operators involve the correct bit shifting to extract the component from the int value. glFormat = GL10.GL_RGB; if (type == TEX_BYTE) convArray = convertToRGBA(intArray, RGB); } @@ -794,15 +828,15 @@ public class GLTexture extends PImage implements PConstants, GLConstants */ public void putBuffer(float[] floatArray) { - putBuffer(floatArray, TEX4); + putBuffer(floatArray, 4); } /** - * Copies floatArray into the texture, using the specified format. + * Copies floatArray into the texture, using the specified number of channels. * @param floatArray float[] - * @param format int + * @param nchan int */ - public void putBuffer(float[] floatArray, int format) + public void putBuffer(float[] floatArray, int nchan) { if (tex[0] == 0) { @@ -810,8 +844,8 @@ public class GLTexture extends PImage implements PConstants, GLConstants } int glFormat; - if (format == TEX1) glFormat = GL10.GL_LUMINANCE; - else if (format == TEX3) glFormat = GL10.GL_RGB; + if (nchan == 1) glFormat = GL10.GL_LUMINANCE; + else if (nchan == 3) glFormat = GL10.GL_RGB; else glFormat = GL10.GL_RGBA; gl.glBindTexture(texTarget, tex[0]); @@ -835,7 +869,7 @@ public class GLTexture extends PImage implements PConstants, GLConstants */ public void getBuffer(int[] intArray) { - getBuffer(intArray, TEX4, TEX_BYTE); + getBuffer(intArray, ARGB, TEX_BYTE); } /** @@ -880,12 +914,12 @@ public class GLTexture extends PImage implements PConstants, GLConstants { int mult; int glFormat; - if (format == TEX1) + if (format == ALPHA) { mult = 1; glFormat = GL10.GL_LUMINANCE; } - else if (format == TEX3) + else if (format == RGB) { mult = 3; glFormat = GL10.GL_RGB; @@ -933,16 +967,16 @@ public class GLTexture extends PImage implements PConstants, GLConstants * @param floatArray float[] * @param format int */ - public void getBuffer(float[] floatArray, int format) + public void getBuffer(float[] floatArray, int nchan) { int mult; // int glFormat; - if (format == TEX1) + if (format == 1) { mult = 1; // glFormat = GL10.GL_LUMINANCE; } - else if (format == TEX3) + else if (format == 3) { mult = 3; // glFormat = GL10.GL_RGB; @@ -1518,7 +1552,7 @@ public class GLTexture extends PImage implements PConstants, GLConstants return tIntArray; } - + /** * @invisible * Reorders a pixel array in RGBA format into ARGB. @@ -1572,6 +1606,21 @@ public class GLTexture extends PImage implements PConstants, GLConstants return tIntArray; } + /** + * @invisible + * Creates a byte version of intArray.. + * @param intArray int[] + */ + protected byte[] convertToAlpha(int[] intArray) + { + byte[] tByteArray = new byte[width * height]; + for (int i = 0; i < width * height; i++) + { + tByteArray[i] = (byte)(intArray[i]); + } + return tByteArray; + } + /** * @invisible * Creates the opengl texture object. @@ -1597,7 +1646,7 @@ public class GLTexture extends PImage implements PConstants, GLConstants //if (texTarget == GL.GL_TEXTURE_1D) gl.glTexImage1D(texTarget, 0, texInternalFormat, w, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, null); //else gl.glTexImage2D(texTarget, 0, texInternalFormat, w, h, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, null); - gl.glTexImage2D(texTarget, 0, texInternalFormat, w, h, 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, null); + gl.glTexImage2D(texTarget, 0, GL10.GL_RGBA/*texInternalFormat*/, w, h, 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, null); gl.glBindTexture(texTarget, 0); /* @@ -1683,9 +1732,9 @@ public class GLTexture extends PImage implements PConstants, GLConstants protected void setTextureParams(Parameters params) { if (params.target == TEX_NORM) - { - texTarget = GL10.GL_TEXTURE_2D; - } + { + texTarget = GL10.GL_TEXTURE_2D; + } /* else if (params.target == TEX_RECT) { @@ -1697,27 +1746,18 @@ public class GLTexture extends PImage implements PConstants, GLConstants } */ - if (params.format == COLOR) - { + if (params.format == RGB) + { + texInternalFormat = GL10.GL_RGB; + } + if (params.format == ARGB) + { texInternalFormat = GL10.GL_RGBA; - } - /* - No Float formats - else if (params.format == FLOAT) + } + if (params.format == ALPHA) { - texInternalFormat = GL10.GL_RGBA16F; - } - else if (params.format == DOUBLE) - { - texInternalFormat = GL10.GL_RGBA32F_ARB; - } - */ - - else if (params.format == 3) - { - PApplet.println("Setting alpha tex"); texInternalFormat = GL10.GL_ALPHA; - } + } if (params.minFilter == NEAREST) diff --git a/android/core/src/processing/android/core/PApplet.java b/android/core/src/processing/android/core/PApplet.java index 6fc51cb64..c03b40b21 100644 --- a/android/core/src/processing/android/core/PApplet.java +++ b/android/core/src/processing/android/core/PApplet.java @@ -2997,7 +2997,41 @@ public class PApplet extends Activity implements PConstants, Runnable { return new PFont(baseFont, round(size), smooth, charset); } + public GLFont loadGLFont(String filename) { + try { + InputStream input = createInput(filename); + return new GLFont(this, 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; + } + + public GLFont createGLFont(String name, float size) { + return createGLFont(name, size, true, GLFont.DEFAULT_CHARSET); + } + + public GLFont createGLFont(String name, float size, boolean smooth) { + return createGLFont(name, size, smooth, GLFont.DEFAULT_CHARSET); + } + + public GLFont createGLFont(String name, float size, + boolean smooth, char charset[]) { + String lowerName = name.toLowerCase(); + Typeface baseFont = null; + + if (lowerName.endsWith(".otf") || lowerName.endsWith(".ttf")) { + AssetManager assets = getBaseContext().getAssets(); + baseFont = Typeface.createFromAsset(assets, "data/" + name); + + } else { + baseFont = PFont.findFont(name); + } + return new GLFont(this, baseFont, round(size), smooth, charset); + } ////////////////////////////////////////////////////////////// diff --git a/android/core/src/processing/android/core/PImage.java b/android/core/src/processing/android/core/PImage.java index 7bfd713c6..85749b54d 100644 --- a/android/core/src/processing/android/core/PImage.java +++ b/android/core/src/processing/android/core/PImage.java @@ -300,7 +300,7 @@ public class PImage implements PConstants, Cloneable { public void loadPixels() { // ignore if (pixels == null || pixels.length != width*height) this.pixels = new int[width*height]; - this.image.getPixels(this.pixels, 0, width, 0, 0, width, height); + if (this.image != null) this.image.getPixels(this.pixels, 0, width, 0, 0, width, height); }