qtfontmetrics.cpp 6.2 KB

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