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

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

Large source code cleanup, getting rid of spaces, tabs, and svn keywords.

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