Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

116 Zeilen
2.8 KiB

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