/* gnu_java_awt_peer_wce_font_WCEFontPeer.c
   Copyright (C) 2005 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

#include <windows.h>
#include <jni.h>
#include "wce-peer.h"
#include <gnu_java_awt_peer_wce_font_WCEFontPeer.h>

/**
 * Font定数
 */
#define Font_PLAIN		0
#define Font_BOLD		1
#define Font_ITALIC		2 


/*
 * Class:     gnu_java_awt_peer_wce_font_WCEFontPeer
 * Method:    createNativeFont
 * Signature: (ILjava/lang/String;II)I
 */
JNIEXPORT jint JNICALL Java_gnu_java_awt_peer_wce_font_WCEFontPeer_createNativeFont(JNIEnv *env, jobject peer_obj, jstring name, jint style, jint size) {
	// システムフォント情報を取得する
	HDC hdc = GetDC(NULL);
	HFONT hFont = GetStockObject(SYSTEM_FONT);
	LOGFONT lf;
	GetObject(hFont, sizeof(lf), &lf);

	// LOGFONT構造体に必要な値を設定する

	// サイズを設定
	// 72で割ると、フォントサイズが大きすぎる
	// 96で割るとちょうどよいサイズになる
//	lf.lfHeight = -size * GetDeviceCaps(hdc, LOGPIXELSY) / 72;
	lf.lfHeight = -size * GetDeviceCaps(hdc, LOGPIXELSY) / 96;
	lf.lfWidth  = 0;
	ReleaseDC(NULL, hdc);
	
	// スタイルを設定
	if ((style & Font_BOLD) == Font_BOLD) {
		lf.lfWeight = FW_BOLD;
	} else {
		lf.lfWeight = FW_NORMAL;
	}
	if ((style & Font_ITALIC) == Font_ITALIC) {
		lf.lfItalic = TRUE;
	} else {
		lf.lfItalic = FALSE;
	}

	return (jint) CreateFontIndirect(&lf);
}

/*
 * Class:     gnu_java_awt_peer_wce_font_WCEFontPeer
 * Method:    disposeNative
 * Signature: (I)V
 */
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_wce_font_WCEFontPeer_disposeNative(JNIEnv *env, jobject peer_obj, jint fontHandle) {
	DeleteObject((HFONT) fontHandle);
}

/*
 * Class:     gnu_java_awt_peer_wce_font_WCEFontPeer
 * Method:    getNativeStringBounds
 * Signature: (I[C)Ljava/awt/geom/Rectangle2D$Double;
 */
JNIEXPORT jobject JNICALL Java_gnu_java_awt_peer_wce_font_WCEFontPeer_getNativeStringBounds(JNIEnv *env, jobject peer_obj, jint fontHandle, jcharArray target) {
	SIZE size;
	HFONT hOldFont;
	HFONT hFont = (HFONT) fontHandle;
	HDC hdc;
	jchar* target_pointer;
	jint length;
	jclass clazz;
	jmethodID mid;
	LONG tmAscent;
	TEXTMETRIC textmetric;

	length = (*env)->GetArrayLength(env, target);
	target_pointer = (*env)->GetCharArrayElements(env, target, NULL);
	if (! target_pointer) {
		return NULL;
	}

	// テキストのサイズを取得する
	hdc = GetDC(NULL);
	hOldFont = SelectObject(hdc, hFont);
	GetTextExtentPoint(hdc, target_pointer, length, &size);
	GetTextMetrics(hdc, &textmetric);
	SelectObject(hdc, hOldFont);
	ReleaseDC(NULL, hdc);
	
	(*env)->ReleaseCharArrayElements(env, target, target_pointer, 0);
	
	tmAscent = textmetric.tmAscent;

	// Rectangle2D.Double を生成して返す
	clazz = (*env)->FindClass(env, "java/awt/geom/Rectangle2D$Double");
	if (! clazz) {
		throw_AWTError(env, "Failed to load Rectangle2D.Double");
		return NULL;
	}
	mid = (*env)->GetMethodID(env, clazz, "<init>", "(DDDD)V");
	if (! mid) {
		throw_AWTError(env, "Failed to get construtor method id.");
		return NULL;
	}
	return (*env)->NewObject(env,
							 clazz,
							 mid,
							 (jdouble) 0.0,					// x
							 (jdouble) 0.0 - tmAscent,		// y(ベースラインにあわせる）
							 (jdouble) size.cx,				// width
							 (jdouble) size.cy);			// height
}

