2003-06-30 16:44:03 +04:00
|
|
|
/*
|
|
|
|
* This file is part of NetSurf, http://netsurf.sourceforge.net/
|
|
|
|
* Licensed under the GNU General Public License,
|
|
|
|
* http://www.opensource.org/licenses/gpl-license
|
|
|
|
* Copyright 2003 James Bursa <bursa@users.sourceforge.net>
|
|
|
|
* Copyright 2003 Phil Mellor <monkeyson@users.sourceforge.net>
|
2002-07-28 01:10:45 +04:00
|
|
|
*/
|
|
|
|
|
2002-09-27 01:38:33 +04:00
|
|
|
#include <assert.h>
|
2002-07-28 01:10:45 +04:00
|
|
|
#include <stdio.h>
|
2003-07-18 03:01:02 +04:00
|
|
|
#include "oslib/font.h"
|
2003-04-04 19:19:32 +04:00
|
|
|
#include "netsurf/css/css.h"
|
2002-09-11 18:24:02 +04:00
|
|
|
#include "netsurf/desktop/gui.h"
|
2003-07-18 03:01:02 +04:00
|
|
|
#include "netsurf/render/font.h"
|
2002-10-05 21:50:21 +04:00
|
|
|
#include "netsurf/utils/log.h"
|
2003-07-18 03:01:02 +04:00
|
|
|
#include "netsurf/utils/utils.h"
|
|
|
|
|
|
|
|
#define FONT_FAMILIES 1
|
|
|
|
#define FONT_BOLD 2
|
|
|
|
#define FONT_SLANTED 1
|
|
|
|
|
|
|
|
/* a font_set is just a linked list of font_data for each face for now */
|
|
|
|
struct font_set {
|
|
|
|
struct font_data *font[FONT_FAMILIES * 4];
|
|
|
|
};
|
2002-07-28 01:10:45 +04:00
|
|
|
|
2002-09-27 01:38:33 +04:00
|
|
|
/**
|
|
|
|
* font id = font family * 4 + bold * 2 + slanted
|
|
|
|
* font family: 0 = sans-serif, 1 = serif, ...
|
|
|
|
*/
|
2002-09-11 18:24:02 +04:00
|
|
|
|
2002-10-08 15:15:29 +04:00
|
|
|
const char * const font_table[FONT_FAMILIES * 4] = {
|
2002-09-27 01:38:33 +04:00
|
|
|
/* sans-serif */
|
2002-10-08 15:15:29 +04:00
|
|
|
"Homerton.Medium\\ELatin1",
|
|
|
|
"Homerton.Medium.Oblique\\ELatin1",
|
|
|
|
"Homerton.Bold\\ELatin1",
|
|
|
|
"Homerton.Bold.Oblique\\ELatin1",
|
2002-09-27 01:38:33 +04:00
|
|
|
};
|
2002-09-11 18:24:02 +04:00
|
|
|
|
2003-03-04 14:59:36 +03:00
|
|
|
static void font_close(struct font_data *data);
|
2002-09-11 18:24:02 +04:00
|
|
|
|
2002-09-27 01:38:33 +04:00
|
|
|
/**
|
|
|
|
* functions
|
|
|
|
*/
|
2002-07-28 01:10:45 +04:00
|
|
|
|
2002-09-27 01:38:33 +04:00
|
|
|
unsigned long font_width(struct font_data *font, const char * text, unsigned int length)
|
2002-07-28 01:10:45 +04:00
|
|
|
{
|
2002-12-27 20:28:19 +03:00
|
|
|
int width;
|
2002-07-28 01:10:45 +04:00
|
|
|
os_error * error;
|
2002-09-27 01:38:33 +04:00
|
|
|
|
|
|
|
assert(font != 0 && text != 0);
|
2002-07-28 01:10:45 +04:00
|
|
|
|
2002-09-11 18:24:02 +04:00
|
|
|
if (length == 0)
|
|
|
|
return 0;
|
2002-07-28 01:10:45 +04:00
|
|
|
|
2002-10-08 15:15:29 +04:00
|
|
|
error = xfont_scan_string(font->handle, text,
|
2002-12-27 20:28:19 +03:00
|
|
|
font_GIVEN_FONT | font_KERN | font_GIVEN_LENGTH,
|
2002-07-28 01:10:45 +04:00
|
|
|
0x7fffffff, 0x7fffffff,
|
2002-12-27 20:28:19 +03:00
|
|
|
0,
|
2002-10-08 15:15:29 +04:00
|
|
|
0, length,
|
2002-12-27 20:28:19 +03:00
|
|
|
0, &width, 0, 0);
|
2002-07-28 01:10:45 +04:00
|
|
|
if (error != 0) {
|
|
|
|
fprintf(stderr, "%s\n", error->errmess);
|
2003-09-23 01:55:08 +04:00
|
|
|
die("font_width: font_scan_string failed");
|
2002-07-28 01:10:45 +04:00
|
|
|
}
|
2002-09-11 18:24:02 +04:00
|
|
|
|
2002-12-27 20:28:19 +03:00
|
|
|
return width / 800;
|
2002-07-28 01:10:45 +04:00
|
|
|
}
|
|
|
|
|
2002-09-27 01:38:33 +04:00
|
|
|
void font_position_in_string(const char* text, struct font_data* font,
|
2003-03-04 01:40:39 +03:00
|
|
|
unsigned int length, unsigned long x, int* char_offset, int* pixel_offset)
|
2002-09-11 18:24:02 +04:00
|
|
|
{
|
|
|
|
font_scan_block block;
|
|
|
|
char* split_point;
|
|
|
|
int x_out, y_out, length_out;
|
2002-09-27 01:38:33 +04:00
|
|
|
|
|
|
|
assert(font != 0 && text != 0);
|
2002-09-11 18:24:02 +04:00
|
|
|
|
|
|
|
block.space.x = block.space.y = 0;
|
|
|
|
block.letter.x = block.letter.y = 0;
|
|
|
|
block.split_char = -1;
|
|
|
|
|
2002-12-30 01:27:35 +03:00
|
|
|
xfont_scan_string(font->handle, text,
|
2002-10-08 15:15:29 +04:00
|
|
|
font_GIVEN_BLOCK | font_GIVEN_FONT | font_KERN | font_RETURN_CARET_POS | font_GIVEN_LENGTH,
|
2002-09-11 18:24:02 +04:00
|
|
|
ro_x_units(x) * 400,
|
|
|
|
0x7fffffff,
|
2002-10-08 15:15:29 +04:00
|
|
|
&block, 0, length,
|
2002-09-11 18:24:02 +04:00
|
|
|
&split_point, &x_out, &y_out, &length_out);
|
|
|
|
|
|
|
|
*char_offset = (int)(split_point - text);
|
|
|
|
*pixel_offset = browser_x_units(x_out / 400);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-09-27 01:38:33 +04:00
|
|
|
|
|
|
|
struct font_set *font_new_set()
|
|
|
|
{
|
|
|
|
struct font_set *set = xcalloc(1, sizeof(*set));
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < FONT_FAMILIES * 4; i++)
|
|
|
|
set->font[i] = 0;
|
|
|
|
|
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct font_data *font_open(struct font_set *set, struct css_style *style)
|
|
|
|
{
|
|
|
|
struct font_data *data;
|
|
|
|
unsigned int size = 16 * 11;
|
|
|
|
unsigned int f = 0;
|
|
|
|
|
|
|
|
assert(set != 0);
|
|
|
|
|
|
|
|
if (style->font_size.size == CSS_FONT_SIZE_LENGTH)
|
|
|
|
size = style->font_size.value.length.value * 16;
|
|
|
|
|
|
|
|
switch (style->font_weight) {
|
|
|
|
case CSS_FONT_WEIGHT_BOLD:
|
|
|
|
case CSS_FONT_WEIGHT_600:
|
|
|
|
case CSS_FONT_WEIGHT_700:
|
|
|
|
case CSS_FONT_WEIGHT_800:
|
|
|
|
case CSS_FONT_WEIGHT_900:
|
|
|
|
f += FONT_BOLD;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (style->font_style) {
|
|
|
|
case CSS_FONT_STYLE_ITALIC:
|
|
|
|
case CSS_FONT_STYLE_OBLIQUE:
|
|
|
|
f += FONT_SLANTED;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (data = set->font[f]; data != 0; data = data->next)
|
|
|
|
if (data->size == size)
|
|
|
|
return data;
|
|
|
|
|
|
|
|
data = xcalloc(1, sizeof(*data));
|
|
|
|
|
2002-10-08 15:15:29 +04:00
|
|
|
{
|
2002-10-05 21:50:21 +04:00
|
|
|
font_f handle;
|
|
|
|
os_error *error;
|
|
|
|
|
2002-10-08 15:15:29 +04:00
|
|
|
LOG(("font_find_font '%s' %i", font_table[f], size));
|
|
|
|
error = xfont_find_font(font_table[f], size, size, 0, 0, &handle, 0, 0);
|
2002-10-05 21:50:21 +04:00
|
|
|
if (error != 0) {
|
|
|
|
fprintf(stderr, "%s\n", error->errmess);
|
|
|
|
die("font_find_font failed");
|
|
|
|
}
|
2002-10-08 15:15:29 +04:00
|
|
|
data->handle = handle;
|
2002-09-27 01:38:33 +04:00
|
|
|
}
|
|
|
|
data->size = size;
|
2002-12-27 20:28:19 +03:00
|
|
|
data->space_width = font_width(data, " ", 1);
|
2002-09-27 01:38:33 +04:00
|
|
|
|
|
|
|
data->next = set->font[f];
|
|
|
|
set->font[f] = data;
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void font_free_set(struct font_set *set)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
struct font_data *data, *next;
|
|
|
|
|
|
|
|
assert(set != 0);
|
|
|
|
|
|
|
|
for (i = 0; i < FONT_FAMILIES * 4; i++) {
|
|
|
|
for (data = set->font[i]; data != 0; data = next) {
|
|
|
|
next = data->next;
|
|
|
|
font_close(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(set);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void font_close(struct font_data *data)
|
|
|
|
{
|
2002-10-08 15:15:29 +04:00
|
|
|
font_lose_font(data->handle);
|
2002-09-27 01:38:33 +04:00
|
|
|
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
|
2002-10-12 17:05:16 +04:00
|
|
|
|
|
|
|
char * font_split(struct font_data *data, const char * text, unsigned int length,
|
|
|
|
unsigned int width, unsigned int *used_width)
|
|
|
|
{
|
|
|
|
os_error *error;
|
|
|
|
font_scan_block block;
|
|
|
|
char *split;
|
|
|
|
|
|
|
|
block.space.x = block.space.y = block.letter.x = block.letter.y = 0;
|
|
|
|
block.split_char = ' ';
|
|
|
|
|
|
|
|
error = xfont_scan_string(data->handle, text,
|
|
|
|
font_GIVEN_BLOCK | font_GIVEN_FONT | font_KERN | font_GIVEN_LENGTH,
|
|
|
|
ro_x_units(width) * 400, 0x7fffffff,
|
|
|
|
&block,
|
|
|
|
0,
|
|
|
|
length,
|
|
|
|
&split,
|
|
|
|
used_width, 0, 0);
|
|
|
|
if (error != 0) {
|
|
|
|
fprintf(stderr, "%s\n", error->errmess);
|
2003-09-23 01:55:08 +04:00
|
|
|
die("font_split: font_scan_string failed");
|
2002-10-12 17:05:16 +04:00
|
|
|
}
|
2002-12-27 20:28:19 +03:00
|
|
|
|
2002-10-12 17:05:16 +04:00
|
|
|
*used_width = browser_x_units(*used_width / 400);
|
2002-12-27 20:28:19 +03:00
|
|
|
|
2002-10-12 17:05:16 +04:00
|
|
|
return split;
|
|
|
|
}
|
|
|
|
|