netsurf/framebuffer/font_internal.c
2014-05-06 21:58:49 +01:00

408 lines
9.2 KiB
C

/*
* Copyright 2005 James Bursa <bursa@users.sourceforge.net>
* 2008 Vincent Sanders <vince@simtec.co.uk>
*
* This file is part of NetSurf, http://www.netsurf-browser.org/
*
* NetSurf 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.
*
* NetSurf 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <inttypes.h>
#include <string.h>
#include <assert.h>
#include "utils/nsoption.h"
#include "utils/utf8.h"
#include "desktop/gui.h"
#include "css/css.h"
#include "render/font.h"
#include "framebuffer/gui.h"
#include "framebuffer/font.h"
#include "framebuffer/GEN_font_internal.c"
#define GLYPH_LEN 16
uint8_t code_point[GLYPH_LEN];
#define SEVEN_SET ((1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | \
(1 << 4) | (1 << 5) | (1 << 6))
#define THREE_SSS ((1 << 0) | (1 << 1) | (1 << 2))
#define THREE_SS ( (1 << 1) | (1 << 2))
#define THREE_S_S ((1 << 0) | (1 << 2))
#define THREE__SS ((1 << 0) | (1 << 1) )
#define THREE_SS_ ( (1 << 1) | (1 << 2))
#define THREE_S__ (1 << 2)
#define THREE__S_ (1 << 1)
#define THREE___S (1 << 0)
uint8_t frag[16][5] = {
{ THREE_SSS,
THREE_S_S,
THREE_S_S,
THREE_S_S,
THREE_SSS },
{ THREE__S_,
THREE_SS_,
THREE__S_,
THREE__S_,
THREE_SSS },
{ THREE_SS_,
THREE___S,
THREE__S_,
THREE_S__,
THREE_SSS },
{ THREE_SS_,
THREE___S,
THREE_SS_,
THREE___S,
THREE_SS_ },
{ THREE_S_S,
THREE_S_S,
THREE_SSS,
THREE___S,
THREE___S },
{ THREE_SSS,
THREE_S__,
THREE_SSS,
THREE___S,
THREE_SSS },
{ THREE__SS,
THREE_S__,
THREE_SSS,
THREE_S_S,
THREE_SSS },
{ THREE_SSS,
THREE___S,
THREE__S_,
THREE__S_,
THREE__S_ },
{ THREE_SSS,
THREE_S_S,
THREE_SSS,
THREE_S_S,
THREE_SSS },
{ THREE_SSS,
THREE_S_S,
THREE_SSS,
THREE___S,
THREE___S },
{ THREE__S_,
THREE_S_S,
THREE_SSS,
THREE_S_S,
THREE_S_S },
{ THREE_SS_,
THREE_S_S,
THREE_SS_,
THREE_S_S,
THREE_SS_ },
{ THREE__S_,
THREE_S_S,
THREE_S__,
THREE_S_S,
THREE__S_ },
{ THREE_SS_,
THREE_S_S,
THREE_S_S,
THREE_S_S,
THREE_SS_ },
{ THREE_SSS,
THREE_S__,
THREE_SS_,
THREE_S__,
THREE_SSS },
{ THREE_SSS,
THREE_S__,
THREE_SS_,
THREE_S__,
THREE_S__ }
};
static uint8_t * get_codepoint(uint32_t id, bool italic)
{
int shift = 0;
int l;
int r;
if (!italic)
shift = 1;
l = (id >> 12);
r = 0xf & (id >> 8);
code_point[ 0] = SEVEN_SET << shift;
code_point[ 2] = (frag[l][0] << (4 + shift)) | (frag[r][0] << shift);
code_point[ 3] = (frag[l][1] << (4 + shift)) | (frag[r][1] << shift);
code_point[ 4] = (frag[l][2] << (4 + shift)) | (frag[r][2] << shift);
code_point[ 5] = (frag[l][3] << (4 + shift)) | (frag[r][3] << shift);
code_point[ 6] = (frag[l][4] << (4 + shift)) | (frag[r][4] << shift);
shift = 1;
l = 0xf & (id >> 4);
r = 0xf & id;
code_point[ 8] = (frag[l][0] << (4 + shift)) | (frag[r][0] << shift);
code_point[ 9] = (frag[l][1] << (4 + shift)) | (frag[r][1] << shift);
code_point[10] = (frag[l][2] << (4 + shift)) | (frag[r][2] << shift);
code_point[11] = (frag[l][3] << (4 + shift)) | (frag[r][3] << shift);
code_point[12] = (frag[l][4] << (4 + shift)) | (frag[r][4] << shift);
code_point[14] = SEVEN_SET << shift;
return (uint8_t *)code_point;
}
bool fb_font_init(void)
{
return true;
}
bool fb_font_finalise(void)
{
return true;
}
enum fb_font_style
fb_get_font_style(const plot_font_style_t *fstyle)
{
if (fstyle->weight >= 700) {
if ((fstyle->flags & FONTF_ITALIC) ||
(fstyle->flags & FONTF_OBLIQUE)) {
return FB_BOLD_ITALIC;
} else {
return FB_BOLD;
}
} else {
if ((fstyle->flags & FONTF_ITALIC) ||
(fstyle->flags & FONTF_OBLIQUE)) {
return FB_ITALIC;
} else {
return FB_REGULAR;
}
}
}
const uint8_t *
fb_get_glyph(uint32_t ucs4, enum fb_font_style style)
{
unsigned int section;
unsigned int offset;
uint16_t g_offset;
switch (style) {
case FB_BOLD_ITALIC:
section = fb_bold_italic_section_table[ucs4 / 256];
if (section != 0 || ucs4 / 256 == 0) {
offset = section * 256 + (ucs4 & 0xff);
g_offset = fb_bold_italic_sections[offset] * 16;
if (g_offset != 0) {
return &font_glyph_data[g_offset];
}
}
case FB_BOLD:
section = fb_bold_section_table[ucs4 / 256];
if (section != 0 || ucs4 / 256 == 0) {
offset = section * 256 + (ucs4 & 0xff);
g_offset = fb_bold_sections[offset] * 16;
if (g_offset != 0) {
return &font_glyph_data[g_offset];
}
}
case FB_ITALIC:
section = fb_italic_section_table[ucs4 / 256];
if (section != 0 || ucs4 / 256 == 0) {
offset = section * 256 + (ucs4 & 0xff);
g_offset = fb_italic_sections[offset] * 16;
if (g_offset != 0) {
return &font_glyph_data[g_offset];
}
}
case FB_REGULAR:
section = fb_regular_section_table[ucs4 / 256];
if (section != 0 || ucs4 / 256 == 0) {
offset = section * 256 + (ucs4 & 0xff);
g_offset = fb_regular_sections[offset] * 16;
if (g_offset != 0) {
return &font_glyph_data[g_offset];
}
}
}
return get_codepoint(ucs4, style & FB_ITALIC);
}
static nserror utf8_to_local(const char *string,
size_t len,
char **result)
{
return utf8_to_enc(string, "CP1252", len, result);
}
static nserror utf8_from_local(const char *string,
size_t len,
char **result)
{
*result = malloc(len + 1);
if (*result == NULL) {
return NSERROR_NOMEM;
}
memcpy(*result, string, len);
(*result)[len] = '\0';
return NSERROR_OK;
}
static bool nsfont_width(const plot_font_style_t *fstyle,
const char *string, size_t length,
int *width)
{
*width = FB_FONT_WIDTH * utf8_bounded_length(string, length);
return true;
}
/**
* Find the position in a string where an x coordinate falls.
*
* \param fstyle style for this text
* \param string UTF-8 string to measure
* \param length length of string
* \param x x coordinate to search for
* \param char_offset updated to offset in string of actual_x, [0..length]
* \param actual_x updated to x coordinate of character closest to x
* \return true on success, false on error and error reported
*/
static bool nsfont_position_in_string(const plot_font_style_t *fstyle,
const char *string, size_t length,
int x, size_t *char_offset, int *actual_x)
{
size_t nxtchr = 0;
int x_pos = 0;
while (nxtchr < length) {
if (abs(x_pos - x) <= (FB_FONT_WIDTH / 2))
break;
x_pos += FB_FONT_WIDTH;
nxtchr = utf8_next(string, length, nxtchr);
}
*actual_x = x_pos;
*char_offset = nxtchr;
return true;
}
/**
* Find where to split a string to make it fit a width.
*
* \param fstyle style for this text
* \param string UTF-8 string to measure
* \param length length of string, in bytes
* \param x width available
* \param char_offset updated to offset in string of actual_x, [1..length]
* \param actual_x updated to x coordinate of character closest to x
* \return true on success, false on error and error reported
*
* On exit, char_offset indicates first character after split point.
*
* Note: char_offset of 0 should never be returned.
*
* Returns:
* char_offset giving split point closest to x, where actual_x <= x
* else
* char_offset giving split point closest to x, where actual_x > x
*
* Returning char_offset == length means no split possible
*/
static bool nsfont_split(const plot_font_style_t *fstyle,
const char *string, size_t length,
int x, size_t *char_offset, int *actual_x)
{
size_t nxtchr = 0;
int last_space_x = 0;
int last_space_idx = 0;
*actual_x = 0;
while (nxtchr < length) {
if (string[nxtchr] == ' ') {
last_space_x = *actual_x;
last_space_idx = nxtchr;
}
*actual_x += FB_FONT_WIDTH;
if (*actual_x > x && last_space_idx != 0) {
/* string has exceeded available width and we've
* found a space; return previous space */
*actual_x = last_space_x;
*char_offset = last_space_idx;
return true;
}
nxtchr = utf8_next(string, length, nxtchr);
}
*char_offset = nxtchr;
return true;
}
const struct font_functions nsfont = {
nsfont_width,
nsfont_position_in_string,
nsfont_split
};
static struct gui_utf8_table utf8_table = {
.utf8_to_local = utf8_to_local,
.local_to_utf8 = utf8_from_local,
};
struct gui_utf8_table *framebuffer_utf8_table = &utf8_table;
/*
* Local Variables:
* c-basic-offset:8
* End:
*/