Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

118 righe
2.8 KiB

  1. /**
  2. * libcaca Java bindings for libcaca
  3. * Copyright (c) 2009 Adrien Grand <jpountz@dinauz.org>
  4. *
  5. * $Id$
  6. *
  7. * This library is free software. It comes without any warranty, to
  8. * the extent permitted by applicable law. You can redistribute it
  9. * and/or modify it under the terms of the Do What The Fuck You Want
  10. * To Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. #include "caca_java_common.h"
  14. #include "org_zoy_caca_Font.h"
  15. #include "caca.h"
  16. #define FONT_CREATION_ERROR "Cannot load font"
  17. JNIEXPORT jlong JNICALL
  18. Java_org_zoy_caca_Font_loadFont__Ljava_lang_String_2(JNIEnv *env, jclass cls, jstring font_name)
  19. {
  20. const char *font_name_chars;
  21. caca_font_t *font;
  22. font_name_chars = (*env)->GetStringUTFChars(env, font_name, 0);
  23. font = caca_load_font(font_name_chars, 0);
  24. (*env)->ReleaseStringUTFChars(env, font_name, font_name_chars);
  25. if (!font)
  26. {
  27. THROW_EX(FONT_CREATION_ERROR);
  28. return 0;
  29. }
  30. return (jlong)font;
  31. }
  32. JNIEXPORT jlong JNICALL
  33. Java_org_zoy_caca_Font_loadFont___3B(JNIEnv *env, jclass cls, jbyteArray font_bytes)
  34. {
  35. jbyte *elems;
  36. jsize size;
  37. caca_font_t *font;
  38. elems = (*env)->GetByteArrayElements(env, font_bytes, 0);
  39. size = (*env)->GetArrayLength(env, font_bytes);
  40. font = caca_load_font(elems, size);
  41. (*env)->ReleaseByteArrayElements(env, font_bytes, elems, 0);
  42. if (!font)
  43. {
  44. THROW_EX(FONT_CREATION_ERROR);
  45. return 0;
  46. }
  47. return (jlong)font;
  48. }
  49. JNIEXPORT jobjectArray JNICALL
  50. Java_org_zoy_caca_Font_getFontNames(JNIEnv *env, jclass cls)
  51. {
  52. const char *const *fonts = caca_get_font_list();
  53. return caca_java_to_string_array(env, fonts);
  54. }
  55. JNIEXPORT jint JNICALL
  56. Java_org_zoy_caca_Font_getFontWidth(JNIEnv *env, jclass cls, jlong ptr)
  57. {
  58. return caca_get_font_width(ptr);
  59. }
  60. JNIEXPORT jint JNICALL
  61. Java_org_zoy_caca_Font_getFontHeight(JNIEnv *env, jclass cls, jlong ptr)
  62. {
  63. return caca_get_font_height(ptr);
  64. }
  65. JNIEXPORT jobjectArray JNICALL
  66. Java_org_zoy_caca_Font_getFontBlocks(JNIEnv *env, jclass cls, jlong ptr)
  67. {
  68. const uint32_t *blocks;
  69. jclass int_array;
  70. jobjectArray ret;
  71. jsize size;
  72. jsize i;
  73. blocks = caca_get_font_blocks(ptr);
  74. for (size = 0; blocks[2*size] || blocks[2*size+1]; ++size);
  75. int_array = (*env)->FindClass(env, "[I");
  76. ret = (*env)->NewObjectArray(env, size, int_array, NULL);
  77. for (i = 0; i < size; ++i)
  78. {
  79. jintArray nth = (*env)->NewIntArray(env, 2); /* Size is 2 */
  80. jint nth_elems[] = { blocks[2*i], blocks[2*i+1] };
  81. (*env)->SetIntArrayRegion(env, nth, 0, 2, nth_elems);
  82. (*env)->SetObjectArrayElement(env, ret, i, nth);
  83. (*env)->DeleteLocalRef(env, nth);
  84. }
  85. return ret;
  86. }
  87. JNIEXPORT void JNICALL
  88. Java_org_zoy_caca_Font_freeFont(JNIEnv *env, jclass cls, jlong ptr)
  89. {
  90. caca_free_font((caca_font_t *)ptr);
  91. }