1 /*
2  * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 #ifndef AWT_FONT_H
27 #define AWT_FONT_H
28 
29 #include "awt.h"
30 #include "awt_Object.h"
31 
32 #include "java_awt_Font.h"
33 #include "sun_awt_windows_WFontMetrics.h"
34 #include "sun_awt_FontDescriptor.h"
35 #include "sun_awt_PlatformFont.h"
36 
37 
38 /************************************************************************
39  * AwtFont utilities
40  */
41 
42 extern jboolean IsMultiFont(JNIEnv *env, jobject obj);
43 
44 #define GET_PLATFORMFONT(font)\
45     (env->CallObjectMethod(env, font, AwtFont::peerMID))
46 
47 
48 /************************************************************************
49  * AwtFont class
50  */
51 
52 class AwtFont : public AwtObject {
53 public:
54 
55     /* int[] width field for sun.awt.windows.WFontDescriptor */
56     static jfieldID widthsID;
57 
58     /* int fields for sun.awt.windows.WFontDescriptor */
59     static jfieldID ascentID;
60     static jfieldID descentID;
61     static jfieldID leadingID;
62     static jfieldID heightID;
63     static jfieldID maxAscentID;
64     static jfieldID maxDescentID;
65     static jfieldID maxHeightID;
66     static jfieldID maxAdvanceID;
67 
68     /* sun.awt.FontDescriptor fontDescriptor field of sun.awt.CharsetString */
69     static jfieldID fontDescriptorID;
70     /* java.lang.String charsetString field of sun.awt.CharsetString */
71     static jfieldID charsetStringID;
72 
73     /* java.lang.String nativeName field of sun.awt.FontDescriptor*/
74     static jfieldID nativeNameID;
75     /* boolean useUnicode field of sun.awt.FontDescriptor*/
76     static jfieldID useUnicodeID;
77 
78     /* long field pData of java.awt.Font */
79     static jfieldID pDataID;
80     /* java.awt.peer.FontPeer field peer of java.awt.Font */
81     static jmethodID peerMID;
82     /* java.lang.String name field of java.awt.Font */
83     static jfieldID nameID;
84     /* int size field of java.awt.Font */
85     static jfieldID sizeID;
86     /* int style field of java.awt.Font */
87     static jfieldID styleID;
88 
89     /* java.awt.Font peer field of java.awt.FontMetrics */
90     static jfieldID fontID;
91 
92     /* sun.awt.FontConfiguration fontConfig field of sun.awt.PlatformFont */
93     static jfieldID fontConfigID;
94     /* FontDescriptor[] componentFonts field of sun.awt.PlatformFont */
95     static jfieldID componentFontsID;
96 
97     /* String textComponentFontName field of sun.awt.windows.WFontPeer */
98     static jfieldID textComponentFontNameID;
99 
100     /* String fontName field of sun.awt.windows.WDefaultFontCharset fields */
101     static jfieldID fontNameID;
102 
103     static jmethodID makeConvertedMultiFontStringMID;
104 
105     /* java.awt.Font methods */
106     static jmethodID getFontMID;
107 
108     /* java.awt.FontMetrics methods */
109     static jmethodID getHeightMID;
110 
111     /*
112      * The argument is used to determine how many handles of
113      * Windows font the instance has.
114      */
115     AwtFont(int num, JNIEnv *env, jobject javaFont);
116     ~AwtFont();    /* Releases all resources */
117 
118     virtual void Dispose();
119 
120     /*
121      * Access methods
122      */
GetHFontNum()123     INLINE int GetHFontNum()     { return m_hFontNum; }
GetHFont(int i)124     INLINE HFONT GetHFont(int i) {
125         DASSERT(m_hFont[i] != NULL);
126         return m_hFont[i];
127     }
128 
129     /* Used to keep English version unchanged as much as possiple. */
GetHFont()130     INLINE HFONT GetHFont() {
131         DASSERT(m_hFont[0] != NULL);
132         return m_hFont[0];
133     }
GetInputHFontIndex()134     INLINE int GetInputHFontIndex() { return m_textInput; }
135 
SetAscent(int ascent)136     INLINE void SetAscent(int ascent) { m_ascent = ascent; }
GetAscent()137     INLINE int GetAscent()           { return m_ascent; }
GetOverhang()138     INLINE int GetOverhang()         { return m_overhang; }
139 
140     /*
141      * Font methods
142      */
143 
144     /*
145      * Returns the AwtFont object associated with the pFontJavaObject.
146      * If none exists, create one.
147      */
148     static AwtFont* GetFont(JNIEnv *env, jobject font,
149                             jint angle=0, jfloat awScale=1.0f);
150 
151     /*
152      * Creates the specified font.  name names the font.  style is a bit
153      * vector that describes the style of the font.  height is the point
154      * size of the font.
155      */
156     static AwtFont* Create(JNIEnv *env, jobject font,
157                            jint angle = 0, jfloat awScale=1.0f);
158     static HFONT CreateHFont(WCHAR* name, int style, int height,
159                              int angle = 0, float awScale=1.0f);
160 
161     static void Cleanup();
162 
163     /*
164      * FontMetrics methods
165      */
166 
167     /*
168      * Loads the metrics of the associated font.  See Font.GetFont for
169      * purpose of pWS.  (Also, client should provide Font java object
170      * instead of getting it from the FontMetrics instance variable.)
171      */
172     static void LoadMetrics(JNIEnv *env, jobject fontMetrics);
173 
174     /* Returns the AwtFont associated with this metrics. */
175     static AwtFont* GetFontFromMetrics(JNIEnv *env, jobject fontMetrics);
176 
177     /*
178      * Sets the ascent of the font.  This member should be called if
179      * font->m_nAscent < 0.
180      */
181     static void SetupAscent(AwtFont* font);
182 
183     /*
184      * Determines the average dimension of the character in the
185      * specified font 'font' and multiplies it by the specified number
186      * of rows and columns.  'font' can be a temporary object.
187      */
188     static SIZE TextSize(AwtFont* font, int columns, int rows);
189 
190     /*
191      * If 'font' is NULL, the SYSTEM_FONT is used to compute the size.
192      * 'font' can be a temporary object.
193      */
194     static int getFontDescriptorNumber(JNIEnv *env, jobject font,
195                                        jobject fontDescriptor);
196 
197     /*
198      * 'font' is of type java.awt.Font.
199      */
200     static SIZE DrawStringSize_sub(jstring str, HDC hDC, jobject font,
201                                    long x, long y, BOOL draw,
202                                    UINT codePage = 0);
203 
204     INLINE static SIZE drawMFStringSize(HDC hDC, jobject font,
205                                         jstring str, long x, long y,
206                                         UINT codePage = 0)
207     {
208         return DrawStringSize_sub(str, hDC, font, x, y, TRUE , codePage);
209     }
210 
211 
212     INLINE static SIZE getMFStringSize(HDC hDC, jobject font, jstring str,
213                                        UINT codePage = 0)
214     {
215         return DrawStringSize_sub(str, hDC, font, 0, 0, FALSE, codePage);
216     }
217 
218 
getMFStringWidth(HDC hDC,jobject font,jstring str)219     INLINE static long getMFStringWidth(HDC hDC, jobject font,
220                                             jstring str) {
221         return getMFStringSize(hDC, font, str).cx;
222     }
223 
224     INLINE static void drawMFString(HDC hDC, jobject font, jstring str,
225                                     long x, long y, UINT codePage = 0)
226     {
227         DrawStringSize_sub(str, hDC, font, x, y, TRUE, codePage);
228     }
229 
GetComponentFonts(JNIEnv * env,jobject font)230     INLINE static jobjectArray GetComponentFonts(JNIEnv *env,
231                                                      jobject font) {
232       jobject platformFont = env->CallObjectMethod(font, AwtFont::peerMID);
233       if (platformFont != NULL) {
234           jobjectArray result =
235               (jobjectArray)(env->GetObjectField(platformFont,
236                                                  AwtFont::componentFontsID));
237           env->DeleteLocalRef(platformFont);
238           return result;
239       }
240       return NULL;
241     }
242 
243    /*
244     * Variables
245     */
246 
247 private:
248     /* The array of associated font handles */
249     HFONT* m_hFont;
250     /* The number of handles. */
251     int    m_hFontNum;
252     /* The index of the handle used to be set to AwtTextComponent. */
253     int    m_textInput;
254     /* The ascent of this font. */
255     int m_ascent;
256     /* The overhang, or amount added to a string's width, of this font. */
257     int m_overhang;
258     /* angle of text rotation in 10'ths of a degree*/
259     int textAngle;
260     /* average width scale factor to be applied */
261     float awScale;
262 };
263 
264 
265 
266 class AwtFontCache {
267 private:
268     class Item {
269     public:
270         Item(const WCHAR* s, HFONT f, Item* n = NULL);
271         ~Item();
272 
273         WCHAR*      name;
274         HFONT       font;
275         Item*       next;
276         DWORD       refCount;   /*  The same HFONT can be associated with
277                                     multiple Java objects.*/
278     };
279 
280 public:
AwtFontCache()281     AwtFontCache() { m_head = NULL; }
282     void    Add(WCHAR* name, HFONT font);
283     HFONT   Lookup(WCHAR* name);
284     BOOL    Search(HFONT font);
285     void    Remove(HFONT font);
286     void    Clear();
287     void    IncRefCount(HFONT hFont);
288     LONG    IncRefCount(Item* item);
289     LONG    DecRefCount(Item* item);
290 
291 
292     Item* m_head;
293 };
294 
295 #define GET_FONT(target, OBJ) \
296     ((jobject)env->CallObjectMethod(target, AwtComponent::getFontMID))
297 
298 #endif /* AWT_FONT_H */
299