summaryrefslogtreecommitdiff
path: root/framebuffer
diff options
context:
space:
mode:
authorVincent Sanders <vince@netsurf-browser.org>2009-02-20 09:51:21 +0000
committerVincent Sanders <vince@netsurf-browser.org>2009-02-20 09:51:21 +0000
commit0cecbe3cbcef566fb679aed5f0055397cb4c040c (patch)
tree82e0b523e73d5c069060b79b41d697839585b924 /framebuffer
parent8aa291df0f63bb152d6d2893f361f6ae06fe06d7 (diff)
downloadnetsurf-0cecbe3cbcef566fb679aed5f0055397cb4c040c.tar.gz
netsurf-0cecbe3cbcef566fb679aed5f0055397cb4c040c.tar.bz2
allow framebuffer port to use freetype2 library
svn path=/trunk/netsurf/; revision=6570
Diffstat (limited to 'framebuffer')
-rw-r--r--framebuffer/fb_16bpp_plotters.c9
-rw-r--r--framebuffer/fb_32bpp_plotters.c116
-rw-r--r--framebuffer/fb_8bpp_plotters.c11
-rw-r--r--framebuffer/fb_font.h15
-rw-r--r--framebuffer/fb_font_freetype.c260
-rw-r--r--framebuffer/fb_font_internal.c (renamed from framebuffer/fb_font.c)5
-rw-r--r--framebuffer/fb_font_internal.h39
-rw-r--r--framebuffer/fb_frontend_linuxfb.c9
-rw-r--r--framebuffer/fb_gui.c8
-rw-r--r--framebuffer/fb_rootwindow.c8
10 files changed, 465 insertions, 15 deletions
diff --git a/framebuffer/fb_16bpp_plotters.c b/framebuffer/fb_16bpp_plotters.c
index 827cc8f6e..095dcda52 100644
--- a/framebuffer/fb_16bpp_plotters.c
+++ b/framebuffer/fb_16bpp_plotters.c
@@ -191,7 +191,13 @@ static bool fb_16bpp_clg(colour c)
return true;
}
-
+#ifdef FB_USE_FREETYPE
+static bool fb_16bpp_text(int x, int y, const struct css_style *style,
+ const char *text, size_t length, colour bg, colour c)
+{
+ return false;
+}
+#else
static bool fb_16bpp_text(int x, int y, const struct css_style *style,
const char *text, size_t length, colour bg, colour c)
{
@@ -278,6 +284,7 @@ static bool fb_16bpp_text(int x, int y, const struct css_style *style,
free(buffer);
return true;
}
+#endif
static bool fb_16bpp_disc(int x, int y, int radius, colour c, bool filled)
{
diff --git a/framebuffer/fb_32bpp_plotters.c b/framebuffer/fb_32bpp_plotters.c
index 8e8f353f6..28a6030a9 100644
--- a/framebuffer/fb_32bpp_plotters.c
+++ b/framebuffer/fb_32bpp_plotters.c
@@ -178,7 +178,114 @@ static bool fb_32bpp_clg(colour c)
return true;
}
+#ifdef FB_USE_FREETYPE
+
+static bool
+fb_32bpp_draw_ft_bitmap(FT_Bitmap *bp, int x, int y, colour c)
+{
+ int height = bp->rows;
+ int width = bp->width;
+ uint32_t row;
+ int xloop, yloop;
+
+ uint32_t *pvideo;
+ uint32_t fgcol;
+
+ int x0,y0,x1,y1;
+ int xoff, yoff; /* x and y offset into image */
+
+ unsigned char *fntd;
+
+ if (width == 0) {
+ LOG(("null width char!"));
+ return false;
+ }
+
+
+ y+=1; /* the coord is the bottom-left of the pixels offset by 1 to make
+ * it work since fb coords are the top-left of pixels
+ */
+
+ /* The part of the text displayed is cropped to the current context. */
+ x0 = x;
+ y0 = y;
+ x1 = x + width;
+ y1 = y + height;
+
+ if (!fb_plotters_clip_rect_ctx(&x0, &y0, &x1, &y1)) {
+ return true; /* text lies outside current clipping region */
+ }
+
+ /* find width and height to plot */
+ if (height > (y1 - y0))
+ height = (y1 - y0);
+
+ if (width > (x1 - x0))
+ width = (x1 - x0);
+
+ xoff = x0 - x;
+ yoff = y0 - y;
+
+ fgcol = ((c & 0xff0000) >> 16) | (c & 0xff00) | ((c & 0xff) << 16);
+
+
+ pvideo = fb_32bpp_get_xy_loc(x, y0);
+
+ for (yloop = yoff; yloop < height; yloop++) {
+ fntd = bp->buffer + (yloop * bp->pitch);
+ for (xloop = 0; xloop < width ; xloop++) {
+ if ((xloop % 8) == 0) {
+ row = *fntd++;
+ }
+
+ if ((row & 0x80) != 0) {
+ *(pvideo + xloop) = fgcol;
+ }
+ row = row << 1;
+ }
+
+ pvideo += (framebuffer->linelen >> 2);
+ }
+
+
+ return true;
+}
+
+static bool fb_32bpp_text(int x, int y, const struct css_style *style,
+ const char *text, size_t length, colour bg, colour c)
+{
+ uint32_t ucs4;
+ size_t nxtchr = 0;
+ FT_UInt glyph_index;
+ FT_Face face = fb_get_face(style);
+ FT_Error error;
+
+ while (nxtchr < length) {
+ ucs4 = utf8_to_ucs4(text + nxtchr, length - nxtchr);
+ nxtchr = utf8_next(text, length, nxtchr);
+ glyph_index = FT_Get_Char_Index(face, ucs4);
+
+ error = FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
+ if (error)
+ continue;
+
+ error = FT_Render_Glyph(face->glyph, FT_RENDER_MODE_MONO );
+ if (error)
+ continue;
+
+ /* now, draw to our target surface */
+ fb_32bpp_draw_ft_bitmap( &face->glyph->bitmap,
+ x + face->glyph->bitmap_left,
+ y - face->glyph->bitmap_top,
+ c);
+
+ x += face->glyph->advance.x >> 6;
+
+ }
+ return true;
+}
+#else
static bool fb_32bpp_text(int x, int y, const struct css_style *style,
const char *text, size_t length, colour bg, colour c)
{
@@ -259,17 +366,18 @@ static bool fb_32bpp_text(int x, int y, const struct css_style *style,
free(buffer);
return true;
}
+#endif
static bool fb_32bpp_disc(int x, int y, int radius, colour c, bool filled)
{
- printf("disc\n");
+ LOG(("disc unimplemented"));
return true;
}
static bool fb_32bpp_arc(int x, int y, int radius, int angle1, int angle2,
colour c)
{
- printf("arc\n");
+ LOG(("arc unimplemented"));
return true;
}
@@ -365,14 +473,14 @@ static bool fb_32bpp_bitmap_tile(int x, int y, int width, int height,
static bool fb_32bpp_flush(void)
{
- printf("flush\n");
+ LOG(("flush unimplemnted"));
return true;
}
static bool fb_32bpp_path(const float *p, unsigned int n, colour fill, float width,
colour c, const float transform[6])
{
- printf("path\n");
+ LOG(("path unimplemented"));
return true;
}
diff --git a/framebuffer/fb_8bpp_plotters.c b/framebuffer/fb_8bpp_plotters.c
index 087cf025b..1d0eccc83 100644
--- a/framebuffer/fb_8bpp_plotters.c
+++ b/framebuffer/fb_8bpp_plotters.c
@@ -127,6 +127,15 @@ static bool fb_8bpp_clg(colour c)
return true;
}
+#ifdef FB_USE_FREETYPE
+
+static bool fb_8bpp_text(int x, int y, const struct css_style *style,
+ const char *text, size_t length, colour bg, colour c)
+{
+ return false;
+
+}
+#else
static bool fb_8bpp_text(int x, int y, const struct css_style *style,
const char *text, size_t length, colour bg, colour c)
{
@@ -206,7 +215,7 @@ static bool fb_8bpp_text(int x, int y, const struct css_style *style,
free(buffer);
return true;
}
-
+#endif
static bool fb_8bpp_disc(int x, int y, int radius, colour c, bool filled)
{
diff --git a/framebuffer/fb_font.h b/framebuffer/fb_font.h
index 502cf2e83..2e453d465 100644
--- a/framebuffer/fb_font.h
+++ b/framebuffer/fb_font.h
@@ -19,6 +19,20 @@
#ifndef NETSURF_FB_FONT_H
#define NETSURF_FB_FONT_H
+bool fb_font_init(void);
+bool fb_font_finalise(void);
+
+#ifdef FB_USE_FREETYPE
+
+#include <ft2build.h>
+#include FT_FREETYPE_H
+
+FT_Face fb_get_face(const struct css_style *style);
+
+#else
+
+#include "utils/utf8.h"
+
struct fb_font_desc {
const char *name;
int width, height;
@@ -34,6 +48,7 @@ extern utf8_convert_ret utf8_to_font_encoding(const struct fb_font_desc* font,
const char *string,
size_t len,
char **result);
+#endif
#endif /* NETSURF_FB_FONT_H */
diff --git a/framebuffer/fb_font_freetype.c b/framebuffer/fb_font_freetype.c
new file mode 100644
index 000000000..36130fa55
--- /dev/null
+++ b/framebuffer/fb_font_freetype.c
@@ -0,0 +1,260 @@
+/*
+ * 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 <assert.h>
+
+#include "css/css.h"
+#include "render/font.h"
+#include "desktop/options.h"
+#include "utils/utf8.h"
+#include "utils/log.h"
+
+#include "framebuffer/fb_gui.h"
+#include "framebuffer/fb_font.h"
+
+static FT_Library library;
+static FT_Face face_sans_serif;
+
+
+utf8_convert_ret utf8_to_local_encoding(const char *string,
+ size_t len,
+ char **result)
+{
+ return utf8_to_enc(string, "UTF-8", len, result);
+}
+
+
+/* initialise font handling */
+bool fb_font_init(void)
+{
+ FT_Error error;
+
+ error = FT_Init_FreeType( &library );
+ if (error) {
+ LOG(("Freetype could not initialised (code %d)\n", error));
+ return false;
+ }
+
+ error = FT_New_Face(library,
+ "/usr/share/fonts/truetype/freefont/FreeSans.ttf",
+ 0,
+ &face_sans_serif );
+ if (error) {
+ LOG(("Could not find default font (code %d)\n", error));
+ FT_Done_FreeType(library);
+ return false;
+ }
+
+ error = FT_Set_Pixel_Sizes(face_sans_serif, 0, 14 );
+ if (error) {
+ LOG(("Could not set pixel size (code %d)\n", error));
+ return false;
+ }
+
+
+ return true;
+}
+
+bool fb_font_finalise(void)
+{
+ FT_Done_FreeType(library);
+ return true;
+}
+
+
+
+FT_Face
+fb_get_face(const struct css_style *style)
+{
+ FT_Face face;
+ face = face_sans_serif;
+ FT_Error error;
+ int size;
+
+ if (style->font_size.value.length.unit == CSS_UNIT_PX) {
+ size = style->font_size.value.length.value;
+
+ error = FT_Set_Pixel_Sizes(face_sans_serif, 0, size );
+ if (error) {
+ LOG(("Could not set pixel size (code %d)\n", error));
+ }
+ } else {
+ size = css_len2pt(&style->font_size.value.length, style);
+ error = FT_Set_Char_Size( face, 0, size*64, 72, 72 );
+ if (error) {
+ LOG(("Could not set pixel size (code %d)\n", error));
+ }
+ }
+
+
+ return face;
+}
+
+/**
+ * Measure the width of a string.
+ *
+ * \param style css_style for this text, with style->font_size.size ==
+ * CSS_FONT_SIZE_LENGTH
+ * \param string UTF-8 string to measure
+ * \param length length of string
+ * \param width updated to width of string[0..length)
+ * \return true on success, false on error and error reported
+ */
+static bool nsfont_width(const struct css_style *style,
+ const char *string, size_t length,
+ int *width)
+{
+ uint32_t ucs4;
+ size_t nxtchr = 0;
+ FT_UInt glyph_index;
+ FT_Face face = fb_get_face(style);
+ FT_Error error;
+
+ *width = 0;
+ while (nxtchr < length) {
+ ucs4 = utf8_to_ucs4(string + nxtchr, length - nxtchr);
+ nxtchr = utf8_next(string, length, nxtchr);
+ glyph_index = FT_Get_Char_Index(face, ucs4);
+
+ error = FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
+ if (error)
+ continue;
+
+ *width += face->glyph->advance.x >> 6;
+ }
+
+ return true;
+}
+
+/**
+ * Find the position in a string where an x coordinate falls.
+ *
+ * \param style css_style for this text, with style->font_size.size ==
+ * CSS_FONT_SIZE_LENGTH
+ * \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 struct css_style *style,
+ const char *string, size_t length,
+ int x, size_t *char_offset, int *actual_x)
+{
+ uint32_t ucs4;
+ size_t nxtchr = 0;
+ FT_UInt glyph_index;
+ FT_Face face = fb_get_face(style);
+ FT_Error error;
+
+ *actual_x = 0;
+ while (nxtchr < length) {
+ ucs4 = utf8_to_ucs4(string + nxtchr, length - nxtchr);
+ glyph_index = FT_Get_Char_Index(face, ucs4);
+
+ error = FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
+ if (error)
+ continue;
+
+ *actual_x += face->glyph->advance.x >> 6;
+ if (*actual_x > x)
+ break;
+
+ nxtchr = utf8_next(string, length, nxtchr);
+ }
+
+ *char_offset = nxtchr;
+ return true;
+}
+
+
+/**
+ * Find where to split a string to make it fit a width.
+ *
+ * \param style css_style for this text, with style->font_size.size ==
+ * CSS_FONT_SIZE_LENGTH
+ * \param string UTF-8 string to measure
+ * \param length length of string
+ * \param x width available
+ * \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
+ *
+ * On exit, [char_offset == 0 ||
+ * string[char_offset] == ' ' ||
+ * char_offset == length]
+ */
+
+static bool nsfont_split(const struct css_style *style,
+ const char *string, size_t length,
+ int x, size_t *char_offset, int *actual_x)
+{
+ uint32_t ucs4;
+ size_t nxtchr = 0;
+ FT_UInt glyph_index;
+ FT_Face face = fb_get_face(style);
+ FT_Error error;
+ int last_space_x = -1;
+ int last_space_idx = -1;
+
+ *actual_x = 0;
+ while (nxtchr < length) {
+ ucs4 = utf8_to_ucs4(string + nxtchr, length - nxtchr);
+
+ if (ucs4 == 0x20) {
+ last_space_x = *actual_x;
+ last_space_idx = nxtchr;
+ }
+
+ glyph_index = FT_Get_Char_Index(face, ucs4);
+
+ error = FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
+ if (error)
+ continue;
+
+ *actual_x += face->glyph->advance.x >> 6;
+ if (*actual_x > x)
+ break;
+
+ nxtchr = utf8_next(string, length, nxtchr);
+ }
+
+ if (last_space_x == -1) {
+ *char_offset = nxtchr;
+ } else {
+ *actual_x = last_space_x;
+ *char_offset = last_space_idx;
+ }
+ return true;
+}
+
+const struct font_functions nsfont = {
+ nsfont_width,
+ nsfont_position_in_string,
+ nsfont_split
+};
+
+/*
+ * Local Variables:
+ * c-basic-offset:8
+ * End:
+ */
diff --git a/framebuffer/fb_font.c b/framebuffer/fb_font_internal.c
index 15679ff15..9d7c9a76b 100644
--- a/framebuffer/fb_font.c
+++ b/framebuffer/fb_font_internal.c
@@ -28,6 +28,11 @@
#include "framebuffer/fb_gui.h"
#include "framebuffer/fb_font.h"
+bool fb_font_init(void)
+{
+ return true;
+}
+
const struct fb_font_desc*
fb_get_font(const struct css_style *style)
{
diff --git a/framebuffer/fb_font_internal.h b/framebuffer/fb_font_internal.h
new file mode 100644
index 000000000..502cf2e83
--- /dev/null
+++ b/framebuffer/fb_font_internal.h
@@ -0,0 +1,39 @@
+/*
+ * Copyright 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/>.
+ */
+
+#ifndef NETSURF_FB_FONT_H
+#define NETSURF_FB_FONT_H
+
+struct fb_font_desc {
+ const char *name;
+ int width, height;
+ const char *encoding;
+ const uint32_t *data;
+};
+
+extern const struct fb_font_desc font_vga_8x16;
+
+extern const struct fb_font_desc* fb_get_font(const struct css_style *style);
+
+extern utf8_convert_ret utf8_to_font_encoding(const struct fb_font_desc* font,
+ const char *string,
+ size_t len,
+ char **result);
+
+#endif /* NETSURF_FB_FONT_H */
+
diff --git a/framebuffer/fb_frontend_linuxfb.c b/framebuffer/fb_frontend_linuxfb.c
index b1e89c537..3422c9fb8 100644
--- a/framebuffer/fb_frontend_linuxfb.c
+++ b/framebuffer/fb_frontend_linuxfb.c
@@ -49,6 +49,7 @@
#include "framebuffer/fb_cursor.h"
#include "framebuffer/fb_frontend.h"
#include "framebuffer/fb_options.h"
+#include "framebuffer/fb_rootwindow.h"
#include "utils/log.h"
#include "utils/messages.h"
@@ -647,15 +648,11 @@ void fb_os_input(struct gui_window *g, bool active)
} else if (event.type == EV_REL) {
switch (event.code) {
case 0:
- fb_cursor_move(framebuffer,
- event.value,
- 0);
+ fb_rootwindow_move(framebuffer, g, event.value, 0, true);
break;
case 1:
- fb_cursor_move(framebuffer,
- 0,
- event.value);
+ fb_rootwindow_move(framebuffer, g, 0, event.value, true);
break;
case 8:
diff --git a/framebuffer/fb_gui.c b/framebuffer/fb_gui.c
index dfa3865e3..705ccdc80 100644
--- a/framebuffer/fb_gui.c
+++ b/framebuffer/fb_gui.c
@@ -44,6 +44,7 @@
#include "framebuffer/fb_findfile.h"
#include "framebuffer/fb_rootwindow.h"
#include "framebuffer/fb_image_data.h"
+#include "framebuffer/fb_font.h"
#include "content/urldb.h"
#include "desktop/history_core.h"
@@ -225,6 +226,9 @@ void gui_init(int argc, char** argv)
}
framebuffer->cursor = fb_cursor_init(framebuffer, &pointer_image);
+
+ fb_font_init();
+
}
void gui_init2(int argc, char** argv)
@@ -542,7 +546,7 @@ bool gui_window_box_scroll_start(struct gui_window *g,
bool gui_window_frame_resize_start(struct gui_window *g)
{
- printf("resize frame\n");
+ LOG(("resize frame\n"));
return true;
}
@@ -552,7 +556,7 @@ void gui_window_save_as_link(struct gui_window *g, struct content *c)
void gui_window_set_scale(struct gui_window *g, float scale)
{
- printf("set scale\n");
+ LOG(("set scale\n"));
}
struct gui_download_window *gui_download_window_create(const char *url,
diff --git a/framebuffer/fb_rootwindow.c b/framebuffer/fb_rootwindow.c
index dd04ff726..05b3eaeaf 100644
--- a/framebuffer/fb_rootwindow.c
+++ b/framebuffer/fb_rootwindow.c
@@ -79,6 +79,8 @@ struct fb_widget {
char* text;
};
+static struct css_style root_style;
+
static struct fb_widget *widget_list;
/* widget for status */
@@ -141,7 +143,7 @@ fb_redraw_widget(struct fb_widget *widget)
if (widget->text != NULL) {
plot.text(fb_plot_ctx.x0 + 2,
fb_plot_ctx.y0 + 15,
- NULL,
+ &root_style,
widget->text,
strlen(widget->text),
widget->bg,
@@ -383,6 +385,10 @@ void fb_rootwindow_create(framebuffer_t *fb)
/* no widget yet has input */
inputfocus_widget = NULL;
+ /* setup root css style (for text etc.) */
+ root_style.font_size.value.length.unit = CSS_UNIT_PX;
+ root_style.font_size.value.length.value = 14;
+
/* underlying root window, cannot take input and lowest in stack */
rootwindow = calloc(1, sizeof(struct gui_window));
rootwindow->x = 0;