| 1 |
774 |
jeremybenn |
/* qtfontmetrics.cpp --
|
| 2 |
|
|
Copyright (C) 2005 Free Software Foundation, Inc.
|
| 3 |
|
|
|
| 4 |
|
|
This file is part of GNU Classpath.
|
| 5 |
|
|
|
| 6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
| 7 |
|
|
it under the terms of the GNU General Public License as published by
|
| 8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
| 9 |
|
|
any later version.
|
| 10 |
|
|
|
| 11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
| 12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
| 14 |
|
|
General Public License for more details.
|
| 15 |
|
|
|
| 16 |
|
|
You should have received a copy of the GNU General Public License
|
| 17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
| 18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
| 19 |
|
|
02110-1301 USA.
|
| 20 |
|
|
|
| 21 |
|
|
Linking this library statically or dynamically with other modules is
|
| 22 |
|
|
making a combined work based on this library. Thus, the terms and
|
| 23 |
|
|
conditions of the GNU General Public License cover the whole
|
| 24 |
|
|
combination.
|
| 25 |
|
|
|
| 26 |
|
|
As a special exception, the copyright holders of this library give you
|
| 27 |
|
|
permission to link this library with independent modules to produce an
|
| 28 |
|
|
executable, regardless of the license terms of these independent
|
| 29 |
|
|
modules, and to copy and distribute the resulting executable under
|
| 30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
| 31 |
|
|
independent module, the terms and conditions of the license of that
|
| 32 |
|
|
module. An independent module is a module which is not derived from
|
| 33 |
|
|
or based on this library. If you modify this library, you may extend
|
| 34 |
|
|
this exception to your version of the library, but you are not
|
| 35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
| 36 |
|
|
exception statement from your version. */
|
| 37 |
|
|
|
| 38 |
|
|
#include <assert.h>
|
| 39 |
|
|
#include <QChar>
|
| 40 |
|
|
#include <QFont>
|
| 41 |
|
|
#include <QFontMetrics>
|
| 42 |
|
|
#include <QString>
|
| 43 |
|
|
#include <QPainter>
|
| 44 |
|
|
#include <QStringList>
|
| 45 |
|
|
#include <QFontDatabase>
|
| 46 |
|
|
#include <gnu_java_awt_peer_qt_QtFontMetrics.h>
|
| 47 |
|
|
#include "qtfont.h"
|
| 48 |
|
|
#include "qtstrings.h"
|
| 49 |
|
|
#include "qtgraphics.h"
|
| 50 |
|
|
|
| 51 |
|
|
QFontMetrics *getFontMetrics( JNIEnv *env, jobject obj )
|
| 52 |
|
|
{
|
| 53 |
|
|
jclass cls = env->GetObjectClass( obj );
|
| 54 |
|
|
jfieldID field = env->GetFieldID( cls, "nativeObject", "J" );
|
| 55 |
|
|
return (QFontMetrics *)env->GetLongField( obj, field );
|
| 56 |
|
|
}
|
| 57 |
|
|
|
| 58 |
|
|
static void setNativePtr( JNIEnv *env, jobject obj, void *value )
|
| 59 |
|
|
{
|
| 60 |
|
|
jlong longValue = (jlong) value;
|
| 61 |
|
|
jclass cls = env->GetObjectClass( obj );
|
| 62 |
|
|
jfieldID field = env->GetFieldID( cls, "nativeObject", "J" );
|
| 63 |
|
|
env->SetLongField( obj, field, longValue );
|
| 64 |
|
|
}
|
| 65 |
|
|
|
| 66 |
|
|
static jobject makeRectangle(JNIEnv *env, QRect *rect)
|
| 67 |
|
|
{
|
| 68 |
|
|
if( rect == NULL )
|
| 69 |
|
|
return NULL;
|
| 70 |
|
|
if( rect->isNull() || !rect->isValid() )
|
| 71 |
|
|
return NULL;
|
| 72 |
|
|
jclass cls = env->FindClass("java/awt/Rectangle");
|
| 73 |
|
|
jmethodID mid = env->GetMethodID(cls, "<init>", "(IIII)V");
|
| 74 |
|
|
jvalue values[4];
|
| 75 |
|
|
|
| 76 |
|
|
int x,y,w,h;
|
| 77 |
|
|
rect->getRect(&x, &y, &w, &h);
|
| 78 |
|
|
values[0].i = (jint) x;
|
| 79 |
|
|
values[1].i = (jint) y;
|
| 80 |
|
|
values[2].i = (jint) w;
|
| 81 |
|
|
values[3].i = (jint) h;
|
| 82 |
|
|
|
| 83 |
|
|
return env->NewObjectA(cls, mid, values);
|
| 84 |
|
|
}
|
| 85 |
|
|
|
| 86 |
|
|
/*
|
| 87 |
|
|
* Create font metrics from a font.
|
| 88 |
|
|
*/
|
| 89 |
|
|
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtFontMetrics_init
|
| 90 |
|
|
(JNIEnv *env, jobject obj, jobject fontPeer)
|
| 91 |
|
|
{
|
| 92 |
|
|
QFont *f = getFont(env, fontPeer);
|
| 93 |
|
|
assert( f );
|
| 94 |
|
|
QFontMetrics *fm = new QFontMetrics( *f );
|
| 95 |
|
|
assert( fm );
|
| 96 |
|
|
setNativePtr( env, obj, fm );
|
| 97 |
|
|
}
|
| 98 |
|
|
|
| 99 |
|
|
/*
|
| 100 |
|
|
* Create font metrics from a font.
|
| 101 |
|
|
*/
|
| 102 |
|
|
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtFontMetrics_initGraphics
|
| 103 |
|
|
(JNIEnv *env, jobject obj, jobject fontPeer, jobject graphics)
|
| 104 |
|
|
{
|
| 105 |
|
|
QFont *f = getFont(env, fontPeer);
|
| 106 |
|
|
assert( f );
|
| 107 |
|
|
QPainter *painter = getPainter( env, graphics );
|
| 108 |
|
|
assert( painter );
|
| 109 |
|
|
QFontMetrics *fm = new QFontMetrics( *f , painter->device());
|
| 110 |
|
|
assert( fm );
|
| 111 |
|
|
setNativePtr( env, obj, fm );
|
| 112 |
|
|
}
|
| 113 |
|
|
|
| 114 |
|
|
/*
|
| 115 |
|
|
* Dispose
|
| 116 |
|
|
*/
|
| 117 |
|
|
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtFontMetrics_dispose
|
| 118 |
|
|
(JNIEnv *env, jobject obj)
|
| 119 |
|
|
{
|
| 120 |
|
|
QFontMetrics *fm = getFontMetrics( env, obj );
|
| 121 |
|
|
if ( fm )
|
| 122 |
|
|
delete fm;
|
| 123 |
|
|
setNativePtr( env, obj, NULL );
|
| 124 |
|
|
}
|
| 125 |
|
|
|
| 126 |
|
|
/*
|
| 127 |
|
|
* Returns JNI_TRUE if a character is displayable.
|
| 128 |
|
|
*/
|
| 129 |
|
|
JNIEXPORT jboolean JNICALL Java_gnu_java_awt_peer_qt_QtFontMetrics_canDisplay
|
| 130 |
|
|
(JNIEnv *env, jobject obj, jint c)
|
| 131 |
|
|
{
|
| 132 |
|
|
QFontMetrics *fm = getFontMetrics( env, obj );
|
| 133 |
|
|
assert( fm );
|
| 134 |
|
|
bool result = fm->inFont( QChar( (unsigned int) c ) );
|
| 135 |
|
|
return (result ? JNI_TRUE : JNI_FALSE);
|
| 136 |
|
|
}
|
| 137 |
|
|
|
| 138 |
|
|
/*
|
| 139 |
|
|
* Returns the ascent.
|
| 140 |
|
|
*/
|
| 141 |
|
|
JNIEXPORT jint JNICALL Java_gnu_java_awt_peer_qt_QtFontMetrics_getAscent
|
| 142 |
|
|
(JNIEnv *env, jobject obj)
|
| 143 |
|
|
{
|
| 144 |
|
|
QFontMetrics *fm = getFontMetrics( env, obj );
|
| 145 |
|
|
assert( fm );
|
| 146 |
|
|
return fm->ascent();
|
| 147 |
|
|
}
|
| 148 |
|
|
|
| 149 |
|
|
/*
|
| 150 |
|
|
* Returns the descent
|
| 151 |
|
|
*/
|
| 152 |
|
|
JNIEXPORT jint JNICALL Java_gnu_java_awt_peer_qt_QtFontMetrics_getDescent
|
| 153 |
|
|
(JNIEnv *env, jobject obj)
|
| 154 |
|
|
{
|
| 155 |
|
|
QFontMetrics *fm = getFontMetrics( env, obj );
|
| 156 |
|
|
assert( fm );
|
| 157 |
|
|
return fm->descent();
|
| 158 |
|
|
}
|
| 159 |
|
|
|
| 160 |
|
|
/*
|
| 161 |
|
|
* Returns the height.
|
| 162 |
|
|
*/
|
| 163 |
|
|
JNIEXPORT jint JNICALL Java_gnu_java_awt_peer_qt_QtFontMetrics_getHeight
|
| 164 |
|
|
(JNIEnv *env, jobject obj)
|
| 165 |
|
|
{
|
| 166 |
|
|
QFontMetrics *fm = getFontMetrics( env, obj );
|
| 167 |
|
|
assert( fm );
|
| 168 |
|
|
return fm->height();
|
| 169 |
|
|
}
|
| 170 |
|
|
|
| 171 |
|
|
/*
|
| 172 |
|
|
*/
|
| 173 |
|
|
JNIEXPORT jint JNICALL Java_gnu_java_awt_peer_qt_QtFontMetrics_getLeading
|
| 174 |
|
|
(JNIEnv *env, jobject obj)
|
| 175 |
|
|
{
|
| 176 |
|
|
QFontMetrics *fm = getFontMetrics( env, obj );
|
| 177 |
|
|
assert( fm );
|
| 178 |
|
|
return fm->leading();
|
| 179 |
|
|
}
|
| 180 |
|
|
|
| 181 |
|
|
/*
|
| 182 |
|
|
* getStringBounds
|
| 183 |
|
|
*/
|
| 184 |
|
|
JNIEXPORT jobject JNICALL Java_gnu_java_awt_peer_qt_QtFontMetrics_getStringBounds
|
| 185 |
|
|
(JNIEnv *env, jobject obj, jstring str)
|
| 186 |
|
|
{
|
| 187 |
|
|
QFontMetrics *fm = getFontMetrics( env, obj );
|
| 188 |
|
|
assert( fm );
|
| 189 |
|
|
QString *qStr = getQString(env, str);
|
| 190 |
|
|
QRect r = fm->boundingRect( *qStr );
|
| 191 |
|
|
delete qStr;
|
| 192 |
|
|
|
| 193 |
|
|
return makeRectangle( env, &r );
|
| 194 |
|
|
}
|
| 195 |
|
|
|
| 196 |
|
|
/*
|
| 197 |
|
|
* Returns the width of the widest character.
|
| 198 |
|
|
*/
|
| 199 |
|
|
JNIEXPORT jint JNICALL Java_gnu_java_awt_peer_qt_QtFontMetrics_getMaxAdvance
|
| 200 |
|
|
(JNIEnv *env, jobject obj)
|
| 201 |
|
|
{
|
| 202 |
|
|
QFontMetrics *fm = getFontMetrics( env, obj );
|
| 203 |
|
|
assert( fm );
|
| 204 |
|
|
return fm->maxWidth();
|
| 205 |
|
|
}
|
| 206 |
|
|
|
| 207 |
|
|
/*
|
| 208 |
|
|
* Returns the width of a given character.
|
| 209 |
|
|
*/
|
| 210 |
|
|
JNIEXPORT jint JNICALL Java_gnu_java_awt_peer_qt_QtFontMetrics_charWidth
|
| 211 |
|
|
(JNIEnv *env, jobject obj, jchar c)
|
| 212 |
|
|
{
|
| 213 |
|
|
QFontMetrics *fm = getFontMetrics( env, obj );
|
| 214 |
|
|
assert( fm );
|
| 215 |
|
|
return fm->width( QChar( (unsigned short)c ) );
|
| 216 |
|
|
}
|
| 217 |
|
|
|
| 218 |
|
|
/*
|
| 219 |
|
|
* Returns the width of a string.
|
| 220 |
|
|
*/
|
| 221 |
|
|
JNIEXPORT jint JNICALL Java_gnu_java_awt_peer_qt_QtFontMetrics_stringWidth
|
| 222 |
|
|
(JNIEnv *env, jobject obj, jstring str)
|
| 223 |
|
|
{
|
| 224 |
|
|
QFontMetrics *fm = getFontMetrics( env, obj );
|
| 225 |
|
|
assert( fm );
|
| 226 |
|
|
QString *qStr = getQString(env, str);
|
| 227 |
|
|
int width = fm->width( *qStr );
|
| 228 |
|
|
delete qStr;
|
| 229 |
|
|
return width;
|
| 230 |
|
|
}
|
| 231 |
|
|
|
| 232 |
|
|
|
| 233 |
|
|
|