summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Bursa <james@netsurf-browser.org>2002-12-27 17:28:19 +0000
committerJames Bursa <james@netsurf-browser.org>2002-12-27 17:28:19 +0000
commitc3a910193ff564f49bc5bb7353e0a27a39f2f728 (patch)
tree3e6c750848b13783599aaf3013876fe64f0e2738
parent8f5c1d0e6e5c0d57d27bd0b2aa953386df9df1f2 (diff)
downloadnetsurf-c3a910193ff564f49bc5bb7353e0a27a39f2f728.tar.gz
netsurf-c3a910193ff564f49bc5bb7353e0a27a39f2f728.tar.bz2
[project @ 2002-12-27 17:28:19 by bursa]
Improved word space handling svn path=/import/netsurf/; revision=60
-rw-r--r--render/box.c8
-rw-r--r--render/box.h3
-rw-r--r--render/layout.c51
-rw-r--r--riscos/font.c43
-rw-r--r--riscos/font.h3
5 files changed, 51 insertions, 57 deletions
diff --git a/render/box.c b/render/box.c
index 67b21250c..147818d55 100644
--- a/render/box.c
+++ b/render/box.c
@@ -1,5 +1,5 @@
/**
- * $Id: box.c,v 1.20 2002/10/15 10:41:12 monkeyson Exp $
+ * $Id: box.c,v 1.21 2002/12/27 17:28:19 bursa Exp $
*/
#include <assert.h>
@@ -186,6 +186,12 @@ struct box * convert_xml_to_box(xmlNode * n, struct css_style * parent_style,
box = box_create(n, BOX_INLINE, parent_style, href);
box->text = squash_whitespace(tolat1(n->content));
box->length = strlen(box->text);
+ if (box->text[box->length - 1] == ' ') {
+ box->space = 1;
+ box->length--;
+ } else {
+ box->space = 0;
+ }
box->font = font_open(fonts, box->style);
box_add_child(inline_container, box);
} else {
diff --git a/render/box.h b/render/box.h
index 3df758a60..ad5290d34 100644
--- a/render/box.h
+++ b/render/box.h
@@ -1,5 +1,5 @@
/**
- * $Id: box.h,v 1.11 2002/09/26 21:38:33 bursa Exp $
+ * $Id: box.h,v 1.12 2002/12/27 17:28:19 bursa Exp $
*/
#ifndef _NETSURF_RENDER_BOX_H_
@@ -34,6 +34,7 @@ struct box {
unsigned long x, y, width, height;
unsigned long min_width, max_width;
const char * text;
+ int space; /* 1 <=> followed by a space */
const char * href;
unsigned int length;
unsigned int columns;
diff --git a/render/layout.c b/render/layout.c
index d12c1f555..ced6f16ef 100644
--- a/render/layout.c
+++ b/render/layout.c
@@ -1,5 +1,5 @@
/**
- * $Id: layout.c,v 1.23 2002/12/26 23:02:38 bursa Exp $
+ * $Id: layout.c,v 1.24 2002/12/27 17:28:19 bursa Exp $
*/
#include <assert.h>
@@ -285,13 +285,14 @@ struct box * layout_line(struct box * first, unsigned long width, unsigned long
unsigned long height;
unsigned long x0 = 0;
unsigned long x1 = width;
- unsigned long x, h, xp;
+ unsigned long x, h, x_previous;
struct box * left;
struct box * right;
struct box * b;
struct box * c;
struct box * d;
int move_y = 0;
+ unsigned int space_before = 0, space_after = 0;
/* fprintf(stderr, "layout_line: '%.*s' %li %li %li\n", first->length, first->text, width, *y, cy); */
@@ -310,7 +311,7 @@ struct box * layout_line(struct box * first, unsigned long width, unsigned long
if (h > height) height = h;
if (b->width == UNKNOWN_WIDTH)
b->width = font_width(b->font, b->text, b->length);
- x += b->width;
+ x += b->width + b->space ? b->font->space_width : 0;
}
}
@@ -320,11 +321,14 @@ struct box * layout_line(struct box * first, unsigned long width, unsigned long
find_sides(cont->float_children, cy, cy + height, &x0, &x1, &left, &right);
/* pass 2: place boxes in line */
- for (x = xp = 0, b = first; x <= x1 - x0 && b != 0; b = b->next) {
+ for (x = x_previous = 0, b = first; x <= x1 - x0 && b != 0; b = b->next) {
if (b->type == BOX_INLINE) {
+ x_previous = x;
+ x += space_after;
b->x = x;
- xp = x;
x += b->width;
+ space_before = space_after;
+ space_after = b->space ? b->font->space_width : 0;
c = b;
move_y = 1;
/* fprintf(stderr, "layout_line: '%.*s' %li %li\n", b->length, b->text, xp, x); */
@@ -363,52 +367,62 @@ struct box * layout_line(struct box * first, unsigned long width, unsigned long
if (x1 - x0 < x) {
/* the last box went over the end */
char * space = strchr(c->text, ' ');
- char * space2 = space;
- unsigned long w, wp;
+ unsigned long w;
struct box * c2;
+ x = x_previous;
+
+ if (space != 0 && c->length <= space - c->text)
+ /* space after end of string */
+ space = 0;
+
if (space == 0)
- wp = w = c->width;
+ w = c->width;
else
- wp = w = font_width(c->font, c->text, space - c->text);
+ w = font_width(c->font, c->text, space - c->text);
- if (x1 - x0 < xp + w && left == 0 && right == 0 && c == first) {
+ if (x1 - x0 < x + space_before + w && left == 0 && right == 0 && c == first) {
/* first word doesn't fit, but no floats and first on line so force in */
if (space == 0) {
+ /* only one word in this box */
b = c->next;
} else {
+ /* cut off first word for this line */
c2 = memcpy(xcalloc(1, sizeof(struct box)), c, sizeof(struct box));
c2->text = xstrdup(space + 1);
c2->length = c->length - ((space + 1) - c->text);
c2->width = UNKNOWN_WIDTH;
c->length = space + 1 - c->text;
- c->width = wp + 4; /* should be the width of a space */
+ c->width = w;
+ c->space = 1;
c2->next = c->next;
c->next = c2;
b = c2;
}
- x = xp + wp;
+ x += space_before + w;
/* fprintf(stderr, "layout_line: overflow, forcing\n"); */
- } else if (x1 - x0 < xp + w) {
+ } else if (x1 - x0 < x + space_before + w) {
/* first word doesn't fit, but full width not available so leave for later */
b = c;
/* fprintf(stderr, "layout_line: overflow, leaving\n"); */
} else {
/* fit as many words as possible */
assert(space != 0);
- space = font_split(c->font, c->text, c->length, x1 - x0 - xp, &wp);
+ space = font_split(c->font, c->text, c->length,
+ x1 - x0 - x - space_before, &w);
LOG(("'%.*s' %lu %lu (%c) %lu", c->length, c->text,
- x1 - x0, space - c->text, *space, wp));
+ x1 - x0, space - c->text, *space, w));
c2 = memcpy(xcalloc(1, sizeof(struct box)), c, sizeof(struct box));
c2->text = xstrdup(space + 1);
c2->length = c->length - ((space + 1) - c->text);
c2->width = UNKNOWN_WIDTH;
c->length = space + 1 - c->text;
+ c->width = w;
+ c->space = 1;
c2->next = c->next;
c->next = c2;
b = c2;
- c->width = wp + 4; /* should be the width of a space */
- x = xp + wp;
+ x += space_before + w;
/* fprintf(stderr, "layout_line: overflow, fit\n"); */
}
move_y = 1;
@@ -465,9 +479,6 @@ void place_float_below(struct box * c, unsigned long width, unsigned long y, str
/**
* layout a table
- *
- * this is the fixed table layout algorithm,
- * <http://www.w3.org/TR/REC-CSS2/tables.html#fixed-table-layout>
*/
void layout_table(struct box * table, unsigned long width, struct box * cont,
diff --git a/riscos/font.c b/riscos/font.c
index cafabce76..9fc4d0e9c 100644
--- a/riscos/font.c
+++ b/riscos/font.c
@@ -1,5 +1,5 @@
/**
- * $Id: font.c,v 1.7 2002/10/15 10:41:12 monkeyson Exp $
+ * $Id: font.c,v 1.8 2002/12/27 17:28:19 bursa Exp $
*/
#include <assert.h>
@@ -33,7 +33,7 @@ void font_close(struct font_data *data);
unsigned long font_width(struct font_data *font, const char * text, unsigned int length)
{
- font_scan_block block;
+ int width;
os_error * error;
assert(font != 0 && text != 0);
@@ -41,44 +41,18 @@ unsigned long font_width(struct font_data *font, const char * text, unsigned int
if (length == 0)
return 0;
- block.space.x = block.space.y = 0;
- block.letter.x = block.letter.y = 0;
- block.split_char = -1;
-
error = xfont_scan_string(font->handle, text,
- font_GIVEN_BLOCK | font_GIVEN_FONT | font_KERN | font_RETURN_BBOX | font_GIVEN_LENGTH,
+ font_GIVEN_FONT | font_KERN | font_GIVEN_LENGTH,
0x7fffffff, 0x7fffffff,
- &block,
+ 0,
0, length,
- 0, 0, 0, 0);
+ 0, &width, 0, 0);
if (error != 0) {
fprintf(stderr, "%s\n", error->errmess);
die("font_scan_string failed");
}
-/* fprintf(stderr, "font_width: '%.*s' => '%s' => %i %i %i %i\n", length, text, text2, */
-/* block.bbox.x0, block.bbox.y0, block.bbox.x1, block.bbox.y1); */
-
- if (length < 0x7fffffff)
- {
- if (text[length - 1] == ' ')
-// {
- block.bbox.x1 += 4*800;
-/* int minx,miny,maxx,maxy;
- char space = ' ';
-// fprintf(stderr, "Space at the end!\n");
- error = xfont_char_bbox(font, space, 0, &minx, &miny, &maxx, &maxy);
- if (error != 0) {
- fprintf(stderr, "%s\n", error->errmess);
- die("font_char_bbox failed");
- }
- block.bbox.x1 += maxx;
- }
-// else
-// fprintf(stderr, "No space\n");*/
- }
-
- return block.bbox.x1 / 800;
+ return width / 800;
}
void font_position_in_string(const char* text, struct font_data* font,
@@ -171,6 +145,7 @@ struct font_data *font_open(struct font_set *set, struct css_style *style)
data->handle = handle;
}
data->size = size;
+ data->space_width = font_width(data, " ", 1);
data->next = set->font[f];
set->font[f] = data;
@@ -227,9 +202,9 @@ char * font_split(struct font_data *data, const char * text, unsigned int length
fprintf(stderr, "%s\n", error->errmess);
die("font_scan_string failed");
}
-
+
*used_width = browser_x_units(*used_width / 400);
-
+
return split;
}
diff --git a/riscos/font.h b/riscos/font.h
index 36684d197..9d2abf419 100644
--- a/riscos/font.h
+++ b/riscos/font.h
@@ -1,5 +1,5 @@
/**
- * $Id: font.h,v 1.4 2002/10/12 13:05:16 bursa Exp $
+ * $Id: font.h,v 1.5 2002/12/27 17:28:19 bursa Exp $
*/
#ifndef _NETSURF_RISCOS_FONT_H_
@@ -26,6 +26,7 @@ struct font_set {
struct font_data {
font_f handle;
unsigned int size;
+ unsigned int space_width;
struct font_data *next;
};