[libbluray-devel] BDFontMetrics: load char widths on demand.
hpi1
git at videolan.org
Fri Nov 28 10:39:58 CET 2014
libbluray | branch: master | hpi1 <hpi1 at anonymous.org> | Mon Nov 24 13:39:53 2014 +0200| [1aeb017273d93d2c3812a391334aa30a31c7af1c] | committer: hpi1
BDFontMetrics: load char widths on demand.
Those are quite rarely used ...
> http://git.videolan.org/gitweb.cgi/libbluray.git/?a=commit;h=1aeb017273d93d2c3812a391334aa30a31c7af1c
---
src/libbluray/bdj/java/java/awt/BDFontMetrics.java | 27 ++++++++++++++------
1 file changed, 19 insertions(+), 8 deletions(-)
diff --git a/src/libbluray/bdj/java/java/awt/BDFontMetrics.java b/src/libbluray/bdj/java/java/awt/BDFontMetrics.java
index a25fdd7..ba3441c 100644
--- a/src/libbluray/bdj/java/java/awt/BDFontMetrics.java
+++ b/src/libbluray/bdj/java/java/awt/BDFontMetrics.java
@@ -207,11 +207,7 @@ public class BDFontMetrics extends FontMetrics {
if (ftFace == 0)
throw new AWTError("font face:" + nativeName + " not loaded");
- /* Cache first 256 char widths for use by the getWidths method and for faster metric
- calculation as they are commonly used (ASCII) characters. */
- widths = new int[256];
- for (int i = 0; i < 256; i++)
- widths[i] = charWidthN(ftFace, (char)i);
+ widths = null;
}
private native long loadFontN(long ftLib, String fontName, int size);
@@ -220,6 +216,18 @@ public class BDFontMetrics extends FontMetrics {
private native int stringWidthN(long ftFace, String string);
private native int charsWidthN(long ftFace, char chars[], int offset, int len);
+ private synchronized void loadWidths() {
+ /* Cache first 256 char widths for use by the getWidths method and for faster metric
+ calculation as they are commonly used (ASCII) characters. */
+ if (widths == null) {
+ widths = new int[256];
+ for (int i = 0; i < 256; i++) {
+ widths[i] = charWidthN(ftFace, (char)i);
+ }
+ }
+ }
+
+
protected synchronized void drawString(BDGraphics g, String string, int x, int y, int rgb) {
g.drawStringN(ftFace, string, x, y, rgb);
}
@@ -244,8 +252,10 @@ public class BDFontMetrics extends FontMetrics {
* Fast lookup of first 256 chars as these are always the same eg. ASCII charset.
*/
public synchronized int charWidth(char c) {
- if (c < 256)
+ if (c < 256) {
+ loadWidths();
return widths[c];
+ }
return charWidthN(ftFace, c);
}
@@ -267,8 +277,9 @@ public class BDFontMetrics extends FontMetrics {
* Get the widths of the first 256 characters in the font.
*/
public int[] getWidths() {
- int[] newWidths = new int[256];
- System.arraycopy(widths, 0, newWidths, 0, 256);
+ loadWidths();
+ int[] newWidths = new int[widths.length];
+ System.arraycopy(widths, 0, newWidths, 0, widths.length);
return newWidths;
}
More information about the libbluray-devel
mailing list