source: libcaca/trunk/java/org_zoy_caca_Font.c @ 4320

Last change on this file since 4320 was 4197, checked in by Sam Hocevar, 10 years ago

Java bindings, courtesy of Adrien Grand <jpountz@dinauz.org>.

File size: 2.8 KB
Line 
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
14#include "caca_java_common.h"
15#include "org_zoy_caca_Font.h"
16
17#include "caca.h"
18
19#define FONT_CREATION_ERROR "Cannot load font"
20
21JNIEXPORT jlong JNICALL
22Java_org_zoy_caca_Font_loadFont__Ljava_lang_String_2(JNIEnv *env, jclass cls, jstring font_name)
23{
24  const char *font_name_chars;
25  caca_font_t *font;
26
27  font_name_chars = (*env)->GetStringUTFChars(env, font_name, 0);
28  font = caca_load_font(font_name_chars, 0);
29  (*env)->ReleaseStringUTFChars(env, font_name, font_name_chars);
30
31  if (!font)
32  {
33    THROW_EX(FONT_CREATION_ERROR);
34    return 0;
35  }
36
37  return (jlong)font;
38}
39
40
41JNIEXPORT jlong JNICALL
42Java_org_zoy_caca_Font_loadFont___3B(JNIEnv *env, jclass cls, jbyteArray font_bytes)
43{
44  jbyte *elems;
45  jsize size;
46  caca_font_t *font;
47
48  elems = (*env)->GetByteArrayElements(env, font_bytes, 0);
49  size = (*env)->GetArrayLength(env, font_bytes);
50  font = caca_load_font(elems, size);
51  (*env)->ReleaseByteArrayElements(env, font_bytes, elems, 0);
52
53  if (!font)
54  {
55    THROW_EX(FONT_CREATION_ERROR);
56    return 0;
57  }
58
59  return (jlong)font;
60}
61
62
63JNIEXPORT jobjectArray JNICALL
64Java_org_zoy_caca_Font_getFontNames(JNIEnv *env, jclass cls)
65{
66  const char *const *fonts = caca_get_font_list();
67  return caca_java_to_string_array(env, fonts);
68}
69
70
71JNIEXPORT jint JNICALL
72Java_org_zoy_caca_Font_getFontWidth(JNIEnv *env, jclass cls, jlong ptr)
73{
74  return caca_get_font_width(ptr);
75}
76
77
78JNIEXPORT jint JNICALL
79Java_org_zoy_caca_Font_getFontHeight(JNIEnv *env, jclass cls, jlong ptr)
80{
81  return caca_get_font_height(ptr);
82}
83
84
85JNIEXPORT jobjectArray JNICALL
86Java_org_zoy_caca_Font_getFontBlocks(JNIEnv *env, jclass cls, jlong ptr)
87{
88  const uint32_t *blocks;
89  jclass int_array;
90  jobjectArray ret;
91  jsize size;
92  jsize i;
93
94  blocks = caca_get_font_blocks(ptr);
95  for (size = 0; blocks[2*size] || blocks[2*size+1]; ++size);
96
97  int_array = (*env)->FindClass(env, "[I");
98  ret = (*env)->NewObjectArray(env, size, int_array, NULL);
99  for (i = 0; i < size; ++i)
100  {
101    jintArray nth = (*env)->NewIntArray(env, 2); /* Size is 2 */
102    jint nth_elems[] = { blocks[2*i], blocks[2*i+1] };
103    (*env)->SetIntArrayRegion(env, nth, 0, 2, nth_elems);
104    (*env)->SetObjectArrayElement(env, ret, i, nth);
105    (*env)->DeleteLocalRef(env, nth);
106  }
107
108  return ret;
109}
110
111
112JNIEXPORT void JNICALL
113Java_org_zoy_caca_Font_freeFont(JNIEnv *env, jclass cls, jlong ptr)
114{
115  caca_free_font((caca_font_t *)ptr);
116}
117
Note: See TracBrowser for help on using the repository browser.