home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ARM Club 3
/
TheARMClub_PDCD3.iso
/
programs
/
desktop
/
newbar
/
Source
/
NewBar
/
c
/
font
next >
Wrap
Text File
|
1998-07-22
|
3KB
|
109 lines
/* font.c */
#include <stdlib.h>
#include <string.h>
#include "OS:font.h"
#include "OS:wimpreadsysinfo.h"
#include "font.h"
#include "options.h"
/* Private structures */
struct font_object {
int handle;
};
/* Returns a font handle for the font specified by options. May return 0
if malloc failed. */
font_object *font_open(void)
{
font_object *obj;
const char *font = options->font;
char *desktop_font_name = 0;
int y_size = (int) (options->font_size * 16),
x_size = (int) (options->font_width * 16);
obj = malloc(sizeof(font_object));
if(!obj) return 0;
/* Default to desktop font. */
obj->handle = 0;
if(!font && !y_size && !x_size) return obj;
/* Read separate defaults from desktop font. */
if(!font || !y_size) {
font_f desktop_font;
/* Desktop font could be the system font. */
if(xwimpreadsysinfo_font(&desktop_font, 0)) desktop_font = 0;
/* Read the desktop font sizes. */
if(!y_size) {
int new_x_size, new_y_size;
if(desktop_font)
font_read_defn(desktop_font, &new_x_size, &new_y_size, 0, 0, 0, 0);
else { new_x_size = new_y_size = 12*16; }
y_size = new_y_size;
if(!x_size) x_size = new_x_size;
}
/* Read the desktop font name. */
if(!font) {
int size;
/* May have to use the system font. */
if(!desktop_font) return obj;
size = font_read_identifier(desktop_font, 0);
font = desktop_font_name = malloc(size + 1);
if(!desktop_font_name) return obj;
/* I wish the docs would say how this is terminated. Never mind
though, since the Font manager should cope with its own strings. */
font_read_identifier(desktop_font, (byte *) desktop_font_name);
}
}
/* Width defaults to height. */
if(!x_size) x_size = y_size;
/* Open font. */
obj->handle = font_find_font(font, x_size, y_size, 0, 0, 0, 0);
free(desktop_font_name);
return obj;
}
void font_close(font_object *obj)
{
if(!obj) return;
if(obj->handle) font_lose_font(obj->handle);
free(obj);
}
int font_text_width(const font_object *obj, const char *text)
{
if(!obj) return 0;
if(obj->handle) {
/* Font manager. */
/* I hate the Font Manager. Horrible API, horrible documentation. */
font_scan_block b;
b.space.x = b.space.y = 0;
b.letter.x = b.letter.y = 0;
b.split_char = -1;
font_scan_string(obj->handle, text, font_GIVEN_BLOCK |
font_GIVEN_LENGTH | font_GIVEN_FONT | font_KERN | font_RETURN_BBOX,
1<<30, 1<<30, &b, 0, strlen(text), 0, 0, 0, 0);
/* Convert from millipoints. */
return (b.bbox.x1 - b.bbox.x0) / font_OS_UNIT;
}
else /* Desktop font. */
return wimpreadsysinfo_version() >= wimp_VERSION_RO35 ?
wimptextop_string_width(text, strlen(text)) :
strlen(text) * 16; /* System font. */
}
/* Return the font handle for this font. 0 means the desktop font. */
int font_get_handle(font_object *obj)
{
if(!obj) return 0;
return obj->handle;
}