From 929fc058c56781763c79015f4dbbf9e14dc808a2 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Sat, 26 Mar 2011 19:26:41 +0100 Subject: Font: Make font instances size agnostic (remove all size states), size is only a render time parameter. Add missing bounds/width/height queries to TypecastFont --- src/jogamp/graph/font/FontInt.java | 9 +- src/jogamp/graph/font/typecast/TypecastFont.java | 101 +++++++++++---- .../graph/font/typecast/TypecastFontFactory.java | 11 +- src/jogamp/graph/font/typecast/TypecastGlyph.java | 135 ++++++++------------- .../graph/font/typecast/TypecastMetrics.java | 65 +++++----- .../graph/font/typecast/TypecastRenderer.java | 14 +-- 6 files changed, 172 insertions(+), 163 deletions(-) (limited to 'src/jogamp') diff --git a/src/jogamp/graph/font/FontInt.java b/src/jogamp/graph/font/FontInt.java index 5b938bdbf..c18787723 100644 --- a/src/jogamp/graph/font/FontInt.java +++ b/src/jogamp/graph/font/FontInt.java @@ -35,11 +35,10 @@ import com.jogamp.graph.font.Font; public interface FontInt extends Font { public interface Glyph extends Font.Glyph { - public Path2D getPath(); - public Path2D getPathForPixelSize(float pixelSize); + public Path2D getPath(); // unscaled path + public Path2D getPath(float pixelSize); } - public void getOutline(String string, - AffineTransform transform, - Path2D[] result); + public void getOutline(String string, float pixelSize, + AffineTransform transform, Path2D[] result); } diff --git a/src/jogamp/graph/font/typecast/TypecastFont.java b/src/jogamp/graph/font/typecast/TypecastFont.java index 86c9601de..1ac57c100 100644 --- a/src/jogamp/graph/font/typecast/TypecastFont.java +++ b/src/jogamp/graph/font/typecast/TypecastFont.java @@ -32,12 +32,15 @@ import java.io.IOException; import jogamp.graph.font.FontInt; import jogamp.graph.font.JavaFontLoader; +import jogamp.graph.font.ttf.TTFontGlyph; +import jogamp.graph.font.ttf.TTFont.Metrics; import jogamp.graph.geom.plane.AffineTransform; import jogamp.graph.geom.plane.Path2D; import net.java.dev.typecast.ot.OTFont; import net.java.dev.typecast.ot.OTFontCollection; import net.java.dev.typecast.ot.table.CmapFormat; import net.java.dev.typecast.ot.table.CmapTable; +import net.java.dev.typecast.ot.table.HdmxTable; import net.java.dev.typecast.ot.table.ID; import com.jogamp.common.util.IntObjectHashMap; @@ -50,30 +53,28 @@ class TypecastFont implements FontInt { final Vertex.Factory pointFactory; final OTFontCollection fontset; final OTFont font; - final int size; - Metrics metrics; + TypecastMetrics metrics; final CmapFormat cmapFormat; int cmapentries; // final IntIntHashMap char2Code; IntObjectHashMap char2Glyph; - public static TypecastFont create(Vertex.Factory factory, String name, int size) { + public static TypecastFont create(Vertex.Factory factory, String name) { String path = JavaFontLoader.getByName(name); OTFontCollection fontset; try { fontset = OTFontCollection.create(new File(path)); - return new TypecastFont(factory, fontset, size); + return new TypecastFont(factory, fontset); } catch (IOException e) { e.printStackTrace(); } return null; } - public TypecastFont(Vertex.Factory factory, OTFontCollection fontset, int size) { + public TypecastFont(Vertex.Factory factory, OTFontCollection fontset) { this.pointFactory = factory; this.fontset = fontset; this.font = fontset.getFont(0); - this.size = size; CmapTable cmapTable = font.getCmapTable(); CmapFormat _cmapFormat = null; @@ -124,10 +125,6 @@ class TypecastFont implements FontInt { return fontset.getFileName(); } - public float getSize() { - return size; - } - public Metrics getMetrics() { if (metrics == null) { metrics = new TypecastMetrics(this); @@ -146,28 +143,90 @@ class TypecastFont implements FontInt { if(DEBUG) { System.err.println("New glyph: " + (int)symbol + " ( " + (char)symbol +" ) -> " + code + ", contours " + glyph.getPointCount() + ": " + path); } + final HdmxTable hdmx = font.getHdmxTable(); + if (null != hdmx) { + /*if(DEBUG) { + System.err.println("hdmx "+hdmx); + }*/ + for (int i=0; i 0) { + totalHeight -= advanceY; + totalWidth = Math.max(curLineWidth, totalWidth); + } + return new AABBox(0, 0, 0, totalWidth, totalHeight,0); } final public int getNumGlyphs() { diff --git a/src/jogamp/graph/font/typecast/TypecastFontFactory.java b/src/jogamp/graph/font/typecast/TypecastFontFactory.java index 6179986fe..04559a138 100644 --- a/src/jogamp/graph/font/typecast/TypecastFontFactory.java +++ b/src/jogamp/graph/font/typecast/TypecastFontFactory.java @@ -40,12 +40,12 @@ public class TypecastFontFactory implements FontFactory { Map fonts = new HashMap(); - public Font createFont(Factory factory, String name, int size) { - Font result = fonts.get(name + ":"+size); + public Font createFont(Factory factory, String name) { + Font result = fonts.get(name); if (result == null) { - result = TypecastFont.create(factory, name, size); + result = TypecastFont.create(factory, name); if(result != null) { - fonts.put(name+":"+size, result); + fonts.put(name, result); } } return result; @@ -56,8 +56,7 @@ public class TypecastFontFactory implements FontFactory { String[] families, String style, String variant, - String weight, - String size) { + String weight) { throw new Error("not implemented"); } diff --git a/src/jogamp/graph/font/typecast/TypecastGlyph.java b/src/jogamp/graph/font/typecast/TypecastGlyph.java index 9bb582974..88d865f9c 100644 --- a/src/jogamp/graph/font/typecast/TypecastGlyph.java +++ b/src/jogamp/graph/font/typecast/TypecastGlyph.java @@ -27,6 +27,8 @@ */ package jogamp.graph.font.typecast; +import java.util.HashMap; + import jogamp.graph.font.FontInt; import jogamp.graph.geom.plane.AffineTransform; import jogamp.graph.geom.plane.Path2D; @@ -37,93 +39,58 @@ import com.jogamp.graph.geom.AABBox; public class TypecastGlyph implements FontInt.Glyph { public class Advance { - Font font; - float advance; - float advances[]; // in pixels - float sizes[]; - float advanceCached = -1; // in pixels - float sizeCached = -1; + final Font font; + final float advance; + HashMap size2advance = new HashMap(); public Advance(Font font, float advance) { this.font = font; this.advance = advance; - - this.advances = new float[0]; - this.sizes = new float[0]; } - public float getScaleForPixelSize(float pixelSize) + public void reset() { + size2advance.clear(); + } + + public float getScale(float pixelSize) { - return this.font.getMetrics().getScaleForPixelSize(pixelSize); + return this.font.getMetrics().getScale(pixelSize); } public void add(float advance, float size) { - float advancesNew[] = new float[this.advances.length+1]; - float sizesNew[] = new float[this.sizes.length+1]; - - for (int i=0; i 0) { - string = "\n advances: \n"+string; - } return "\nAdvance:"+ "\n advance: "+this.advance+ - string; + "\n advances: \n"+size2advance; } } public class Metrics { AABBox bbox; - AABBox bbox_sized; - Advance advance; + Advance advance; public Metrics(Font font, AABBox bbox, float advance) { @@ -131,9 +98,13 @@ public class TypecastGlyph implements FontInt.Glyph { this.advance = new Advance(font, advance); } - public float getScaleForPixelSize(float pixelSize) + public void reset() { + advance.reset(); + } + + public float getScale(float pixelSize) { - return this.advance.getScaleForPixelSize(pixelSize); + return this.advance.getScale(pixelSize); } public AABBox getBBox() @@ -146,7 +117,7 @@ public class TypecastGlyph implements FontInt.Glyph { this.advance.add(advance, size); } - public float getAdvanceForPixelSize(float size, boolean useFrationalMetrics) + public float getAdvance(float size, boolean useFrationalMetrics) { return this.advance.get(size, useFrationalMetrics); } @@ -197,6 +168,11 @@ public class TypecastGlyph implements FontInt.Glyph { this.metrics = new Metrics(this.font, bbox, this.advance); } + public void reset(Path2D path) { + this.path = path; + this.metrics.reset(); + } + public Font getFont() { return this.font; } @@ -221,51 +197,36 @@ public class TypecastGlyph implements FontInt.Glyph { return this.id; } - public float getScaleForPixelSize(float pixelSize) { - return this.metrics.getScaleForPixelSize(pixelSize); + public float getScale(float pixelSize) { + return this.metrics.getScale(pixelSize); } - public AABBox getBBox(float size) { - AABBox newBox = getBBox().clone(); - newBox.scale(size); - return newBox; - } - - public AABBox getBBoxForPixelSize(float pixelSize) { - return getBBox(getScaleForPixelSize(pixelSize)); + public AABBox getBBox(float pixelSize) { + final float size = getScale(pixelSize); + AABBox newBox = getBBox().clone(); + newBox.scale(size); + return newBox; } protected void addAdvance(float advance, float size) { this.metrics.addAdvance(advance, size); } - public float getAdvanceForPixelSize(float size, boolean useFrationalMetrics) { - return this.metrics.getAdvanceForPixelSize(size, useFrationalMetrics); - } - - public float getAdvance() { - return getAdvanceForPixelSize(font.getSize(), false); + public float getAdvance(float pixelSize, boolean useFrationalMetrics) { + return this.metrics.getAdvance(pixelSize, useFrationalMetrics); } public Path2D getPath() { - return getPath(getScaleForPixelSize(font.getSize())); + return this.path; } - private Path2D getPath(float size) - { + public Path2D getPath(float pixelSize) { + final float size = getScale(pixelSize); + if (this.numberSized != size) { - this.numberSized = size; - this.pathSized = AffineTransform.getScaleInstance(null, size, size).createTransformedShape(getPath()); + this.numberSized = size; + this.pathSized = AffineTransform.getScaleInstance(null, size, size).createTransformedShape(getPath()); } return this.pathSized; - } - - public Path2D getPathForPixelSize(float pixelSize) { - return getPath(getScaleForPixelSize(pixelSize)); - } - - public Path2D getNormalPath() { - return this.path; - } - + } } diff --git a/src/jogamp/graph/font/typecast/TypecastMetrics.java b/src/jogamp/graph/font/typecast/TypecastMetrics.java index 541ed90d8..9ca1e3bf7 100644 --- a/src/jogamp/graph/font/typecast/TypecastMetrics.java +++ b/src/jogamp/graph/font/typecast/TypecastMetrics.java @@ -37,51 +37,42 @@ class TypecastMetrics implements Metrics { private final TypecastFont fontImpl; // HeadTable + private final HeadTable headTable; private final float unitsPerEM_Inv; - private final float unitsPerEM_Inv_sized; - private final AABBox bbox_sized; - + private final AABBox bbox; // HheaTable - private final float ascend_sized; - private final float descent_sized; - private final float linegap_sized; + private final HheaTable hheaTable; public TypecastMetrics(TypecastFont fontImpl) { this.fontImpl = fontImpl; - final HeadTable headTable = this.fontImpl.font.getHeadTable(); - unitsPerEM_Inv = 1.0f / ( (float) headTable.getUnitsPerEm() ); - unitsPerEM_Inv_sized = this.fontImpl.size * unitsPerEM_Inv; - int maxWidth = headTable.getXMax() - headTable.getXMin(); - int maxHeight = headTable.getYMax() - headTable.getYMin(); - float lowx= headTable.getXMin(); - float lowy = -(headTable.getYMin()+maxHeight); - float highx = lowx + maxWidth; - float highy = lowy + maxHeight; - bbox_sized = new AABBox(lowx, lowy, 0, highx, highy, 0); // invert - bbox_sized.scale(unitsPerEM_Inv_sized); - - final HheaTable hheaTable = this.fontImpl.font.getHheaTable(); - ascend_sized = unitsPerEM_Inv_sized * -hheaTable.getAscender(); // invert - descent_sized = unitsPerEM_Inv_sized * -hheaTable.getDescender(); // invert - linegap_sized = unitsPerEM_Inv_sized * -hheaTable.getLineGap(); // invert + headTable = this.fontImpl.font.getHeadTable(); + hheaTable = this.fontImpl.font.getHheaTable(); + unitsPerEM_Inv = 1.0f / ( (float) headTable.getUnitsPerEm() ); + + int maxWidth = headTable.getXMax() - headTable.getXMin(); + int maxHeight = headTable.getYMax() - headTable.getYMin(); + float lowx= headTable.getXMin(); + float lowy = -(headTable.getYMin()+maxHeight); + float highx = lowx + maxWidth; + float highy = lowy + maxHeight; + bbox = new AABBox(lowx, lowy, 0, highx, highy, 0); // invert } - - public final float getAscent() { - return ascend_sized; + + public final float getAscent(float pixelSize) { + return getScale(pixelSize) * -hheaTable.getAscender(); // invert } - public final float getDescent() { - return descent_sized; + public final float getDescent(float pixelSize) { + return getScale(pixelSize) * -hheaTable.getDescender(); // invert } - public final float getLineGap() { - return linegap_sized; + public final float getLineGap(float pixelSize) { + return getScale(pixelSize) * -hheaTable.getLineGap(); // invert } - public final float getScale() { - return unitsPerEM_Inv_sized; - } - public float getScaleForPixelSize(float pixelSize) { - return pixelSize * unitsPerEM_Inv; - } - public final AABBox getBBox() { - return bbox_sized; + public final float getScale(float pixelSize) { + return pixelSize * unitsPerEM_Inv; + } + public final AABBox getBBox(float pixelSize) { + AABBox res = new AABBox(bbox.getLow(), bbox.getHigh()); + res.scale(getScale(pixelSize)); + return res; } } \ No newline at end of file diff --git a/src/jogamp/graph/font/typecast/TypecastRenderer.java b/src/jogamp/graph/font/typecast/TypecastRenderer.java index 74e5cd604..11f1ec028 100644 --- a/src/jogamp/graph/font/typecast/TypecastRenderer.java +++ b/src/jogamp/graph/font/typecast/TypecastRenderer.java @@ -44,16 +44,16 @@ import net.java.dev.typecast.ot.OTGlyph; public class TypecastRenderer { public static void getOutline(Factory factory, TypecastFont font, - String string, AffineTransform transform, Path2D[] p) + String string, float pixelSize, AffineTransform transform, Path2D[] p) { if (string == null) { return; } Font.Metrics metrics = font.getMetrics(); float advanceTotal = 0; - float lineGap = metrics.getLineGap() ; - float ascent = metrics.getAscent() ; - float descent = metrics.getDescent() ; + float lineGap = metrics.getLineGap(pixelSize) ; + float ascent = metrics.getAscent(pixelSize) ; + float descent = metrics.getDescent(pixelSize) ; if (transform == null) { transform = new AffineTransform(factory); } @@ -73,12 +73,12 @@ public class TypecastRenderer { continue; } TypecastGlyph glyph = (TypecastGlyph) font.getGlyph(character); - Path2D gp = glyph.getNormalPath(); - float scale = metrics.getScale(); + Path2D gp = glyph.getPath(); + float scale = metrics.getScale(pixelSize); t.translate(advanceTotal, y); t.scale(scale, scale); p[i].append(gp.iterator(t), false); - advanceTotal += glyph.getAdvanceForPixelSize(font.getSize(), true); + advanceTotal += glyph.getAdvance(pixelSize, true); } } -- cgit v1.2.3 From 23646bedac2fe9ff27e35ac167e6872afb84fc50 Mon Sep 17 00:00:00 2001 From: Rami Santina Date: Mon, 28 Mar 2011 10:08:12 +0300 Subject: Fix: remove TTF imports --- src/jogamp/graph/font/typecast/TypecastFont.java | 2 -- 1 file changed, 2 deletions(-) (limited to 'src/jogamp') diff --git a/src/jogamp/graph/font/typecast/TypecastFont.java b/src/jogamp/graph/font/typecast/TypecastFont.java index 1ac57c100..b95dccf21 100644 --- a/src/jogamp/graph/font/typecast/TypecastFont.java +++ b/src/jogamp/graph/font/typecast/TypecastFont.java @@ -32,8 +32,6 @@ import java.io.IOException; import jogamp.graph.font.FontInt; import jogamp.graph.font.JavaFontLoader; -import jogamp.graph.font.ttf.TTFontGlyph; -import jogamp.graph.font.ttf.TTFont.Metrics; import jogamp.graph.geom.plane.AffineTransform; import jogamp.graph.geom.plane.Path2D; import net.java.dev.typecast.ot.OTFont; -- cgit v1.2.3