2
Copyright (C) 2009 Volker Berlin (i-net software)
4
This software is provided 'as-is', without any express or implied
5
warranty. In no event will the authors be held liable for any damages
6
arising from the use of this software.
8
Permission is granted to anyone to use this software for any purpose,
9
including commercial applications, and to alter it and redistribute it
10
freely, subject to the following restrictions:
12
1. The origin of this software must not be misrepresented; you must not
13
claim that you wrote the original software. If you use this software
14
in a product, an acknowledgment in the product documentation would be
15
appreciated but is not required.
16
2. Altered source versions must be plainly marked as such, and must not be
17
misrepresented as being the original software.
18
3. This notice may not be removed or altered from any source distribution.
27
import java.awt.font.FontRenderContext;
28
import java.awt.geom.AffineTransform;
29
import java.util.Locale;
31
import sun.awt.SunHints;
38
public abstract class Font2D{
40
public Font2DHandle handle = new Font2DHandle(this);
42
/* SunGraphics2D has font, tx, aa and fm. From this info
43
* can get a Strike object from the cache, creating it if necessary.
44
* This code is designed for multi-threaded access.
45
* For that reason it creates a local FontStrikeDesc rather than filling
46
* in a shared one. Up to two AffineTransforms and one FontStrikeDesc will
47
* be created by every lookup. This appears to perform more than
48
* adequately. But it may make sense to expose FontStrikeDesc
49
* as a parameter so a caller can use its own.
50
* In such a case if a FontStrikeDesc is stored as a key then
51
* we would need to use a private copy.
53
* Note that this code doesn't prevent two threads from creating
54
* two different FontStrike instances and having one of the threads
55
* overwrite the other in the map. This is likely to be a rare
56
* occurrence and the only consequence is that these callers will have
57
* different instances of the strike, and there'd be some duplication of
58
* population of the strikes. However since users of these strikes are
59
* transient, then the one that was overwritten would soon be freed.
60
* If there is any problem then a small synchronized block would be
61
* required with its attendant consequences for MP scalability.
63
public FontStrike getStrike(Font font, AffineTransform devTx, int aa, int fm){
64
return getStrike(font, new FontRenderContext(devTx, aa == SunHints.INTVAL_TEXT_ANTIALIAS_ON,
65
fm == SunHints.INTVAL_FRACTIONALMETRICS_ON));
68
public FontStrike getStrike(Font font, FontRenderContext frc) {
69
// TODO Auto-generated method stub
73
public void removeFromCache(FontStrikeDesc desc){
74
// TODO Auto-generated method stub
79
* The length of the metrics array must be >= 8. This method will
80
* store the following elements in that array before returning:
84
* metrics[3]: max advance
85
* metrics[4]: strikethrough offset
86
* metrics[5]: strikethrough thickness
87
* metrics[6]: underline offset
88
* metrics[7]: underline thickness
90
public void getFontMetrics(Font font, AffineTransform identityTx, Object antiAliasingHint,
91
Object fractionalMetricsHint, float[] metrics){
92
FontRenderContext frc = new FontRenderContext(identityTx, antiAliasingHint, fractionalMetricsHint);
93
StrikeMetrics strikeMetrics = getStrike(font, frc).getFontMetrics();
94
metrics[0] = strikeMetrics.getAscent();
95
metrics[1] = strikeMetrics.getDescent();
96
metrics[2] = strikeMetrics.getLeading();
97
metrics[3] = strikeMetrics.getMaxAdvance();
99
getStyleMetrics(font.getSize2D(), metrics, 4);
103
* The length of the metrics array must be >= offset+4, and offset must be
104
* >= 0. Typically offset is 4. This method will
105
* store the following elements in that array before returning:
106
* metrics[off+0]: strikethrough offset
107
* metrics[off+1]: strikethrough thickness
108
* metrics[off+2]: underline offset
109
* metrics[off+3]: underline thickness
111
* Note that this implementation simply returns default values;
112
* subclasses can override this method to provide more accurate values.
114
public void getStyleMetrics(float pointSize, float[] metrics, int offset) {
115
metrics[offset] = -metrics[0] / 2.5f;
116
metrics[offset+1] = pointSize / 12;
117
metrics[offset+2] = metrics[offset+1] / 1.5f;
118
metrics[offset+3] = metrics[offset+1];
122
* The length of the metrics array must be >= 4. This method will
123
* store the following elements in that array before returning:
125
* metrics[1]: descent
126
* metrics[2]: leading
127
* metrics[3]: max advance
129
public void getFontMetrics(Font font, FontRenderContext frc,
131
StrikeMetrics strikeMetrics = getStrike(font, frc).getFontMetrics();
132
metrics[0] = strikeMetrics.getAscent();
133
metrics[1] = strikeMetrics.getDescent();
134
metrics[2] = strikeMetrics.getLeading();
135
metrics[3] = strikeMetrics.getMaxAdvance();
139
* All the important subclasses override this which is principally for
140
* the TrueType 'gasp' table.
142
public boolean useAAForPtSize(int ptsize) {
146
public boolean hasSupplementaryChars() {
150
/* The following methods implement public methods on java.awt.Font */
151
public String getPostscriptName(){
152
// TODO Auto-generated method stub
156
public String getFontName(Locale l){
157
// TODO Auto-generated method stub
161
public String getFamilyName(Locale l){
162
// TODO Auto-generated method stub
166
public int getNumGlyphs(){
167
// TODO Auto-generated method stub
171
public int charToGlyph(int wchar) {
175
public int getMissingGlyphCode(){
176
// TODO Auto-generated method stub
180
public boolean canDisplay(char c){
181
//HACK There is no equivalent in C# http://msdn2.microsoft.com/en-us/library/sf4dhbw8(VS.80).aspx
185
public boolean canDisplay(int cp){
186
//HACK There is no equivalent in C# http://msdn2.microsoft.com/en-us/library/sf4dhbw8(VS.80).aspx
190
public byte getBaselineFor(char c) {
191
return Font.ROMAN_BASELINE;
194
public float getItalicAngle(Font font, AffineTransform at,
195
Object aaHint, Object fmHint) {
196
/* hardwire psz=12 as that's typical and AA vs non-AA for 'gasp' mode
197
* isn't important for the caret slope of this rarely used API.
199
int aa = FontStrikeDesc.getAAHintIntVal(aaHint, this, 12);
200
int fm = FontStrikeDesc.getFMHintIntVal(fmHint);
201
FontStrike strike = getStrike(font, at, aa, fm);
202
StrikeMetrics metrics = strike.getFontMetrics();
203
if (metrics.ascentY == 0 || metrics.ascentX == 0) {
206
/* ascent is "up" from the baseline so its typically
207
* a negative value, so we need to compensate
209
return metrics.ascentX/-metrics.ascentY;
213
/** Returns the "real" style of this Font2D. Eg the font face
214
* Lucida Sans Bold" has a real style of Font.BOLD, even though
215
* it may be able to used to simulate bold italic
217
public abstract int getStyle();
219
public abstract cli.System.Drawing.Font createNetFont(Font font);