27#include <parserutils/input/inputstream.h>
82#define TEXT_SIZE 10 * PLOT_STYLE_SCALE
89 .background = 0xffffff,
90 .foreground = 0x000000,
121static parserutils_error
127 return PARSERUTILS_OK;
142 parserutils_inputstream *stream;
143 parserutils_error error;
147 utf8_data = malloc(
CHUNK);
148 if (utf8_data == NULL)
151 error = parserutils_inputstream_create(lwc_string_data(encoding), 0,
153 if (error == PARSERUTILS_BADENCODING) {
155 error = parserutils_inputstream_create(
"Windows-1252", 0,
158 if (error != PARSERUTILS_OK) {
163 c->encoding = lwc_string_ref(encoding);
164 c->inputstream = stream;
165 c->utf8_data = utf8_data;
166 c->utf8_data_size = 0;
167 c->utf8_data_allocated =
CHUNK;
168 c->physical_line = 0;
169 c->physical_line_count = 0;
170 c->formatted_width = 0;
188 lwc_string *imime_type,
191 const char *fallback_charset,
197 lwc_string *encoding;
205 llcache, fallback_charset, quirks);
219 lwc_string_unref(encoding);
224 lwc_string_unref(encoding);
244 utf8_data = realloc(c->
utf8_data, allocated);
245 if (utf8_data == NULL)
264 parserutils_inputstream *stream,
265 parserutils_error terminator)
267 static const uint8_t *u_fffd = (
const uint8_t *)
"\xef\xbf\xbd";
269 size_t chlen, offset = 0;
271 while (parserutils_inputstream_peek(stream, offset, &ch, &chlen) !=
274 if (chlen == 1 && *ch == 0) {
277 parserutils_inputstream_peek(stream, 0,
290 parserutils_inputstream_advance(stream, offset + 1);
297 if (offset >
CHUNK) {
299 parserutils_inputstream_peek(stream, 0,
308 parserutils_inputstream_advance(stream, offset);
317 parserutils_inputstream_peek(stream, 0, &ch, &chlen);
323 parserutils_inputstream_advance(stream, offset);
337 parserutils_inputstream *stream =
text->inputstream;
338 parserutils_error error;
340 error = parserutils_inputstream_append(stream,
341 (
const uint8_t *) data,
size);
342 if (error != PARSERUTILS_OK) {
363 parserutils_inputstream *stream =
text->inputstream;
364 parserutils_error error;
366 error = parserutils_inputstream_append(stream, NULL, 0);
367 if (error != PARSERUTILS_OK) {
374 parserutils_inputstream_destroy(stream);
375 text->inputstream = NULL;
405 char *utf8_data =
text->utf8_data;
406 size_t utf8_data_size =
text->utf8_data_size;
407 unsigned long line_count = 0;
410 size_t i, space, col;
433 text->physical_line_count = 0;
442 line[line_count++].start = line_start = 0;
446 while (i < utf8_data_size) {
451 parserutils_error perror;
453 perror = parserutils_charset_utf8_to_ucs4((
const uint8_t *)utf8_data + i, utf8_data_size - i, &chr, &csize);
454 if (perror != PARSERUTILS_OK) {
458 term = (chr ==
'\n' || chr ==
'\r');
466 if (term || next_col >=
columns) {
467 if (line_count % 1024 == 0) {
468 line1 = realloc(
line,
470 (line_count + 1024 + 3));
477 line[line_count-1].length = i - line_start;
480 if (i + 1 < utf8_data_size &&
481 utf8_data[i+1] != utf8_data[i] &&
482 (utf8_data[i+1] ==
'\n' ||
483 utf8_data[i+1] ==
'\r')) {
490 line[line_count-1].length = (i + 1) - line_start;
492 line[line_count-1].length = i - line_start;
495 line[line_count++].start = line_start = i + 1;
505 line[line_count-1].length = i -
line[line_count-1].start;
506 line[line_count].start = utf8_data_size;
508 text->physical_line_count = line_count;
515 NSLOG(netsurf, INFO,
"out of memory (line_count %lu)", line_count);
528 lwc_string_unref(
text->encoding);
530 if (
text->inputstream != NULL) {
531 parserutils_inputstream_destroy(
text->inputstream);
534 if (
text->physical_line != NULL) {
535 free(
text->physical_line);
538 if (
text->utf8_data != NULL) {
539 free(
text->utf8_data);
542 if (
text->sel != NULL) {
635 else if ((
unsigned)y >= nlines)
644 size_t next_offset = 0;
647 while (next_offset <
length &&
text[next_offset] !=
'\t') {
651 if (next_offset <
length) {
663 text, next_offset, x,
664 &char_offset, &pixel_offset);
677 if (x <=
width)
break;
685 return line->start + idx;
707 const char *status = 0;
856 bool highlighted =
false;
862 plot_fstyle.
size *= scale;
866 unsigned len = utf8_len;
881 (
text->base.textsearch.context != NULL) &&
893 unsigned endtxt_idx = end_idx;
894 bool clip_changed =
false;
895 bool text_visible =
true;
900 if (end_idx > utf8_len) {
904 assert(end_idx == utf8_len + 1);
905 endtxt_idx = utf8_len;
934 y + (
int)(
height * 0.75 * scale),
955 int px0 =
max(x + startx,
clip->x0);
956 int px1 =
min(x + endx,
clip->x1);
970 text_visible =
false;
983 y + (
int)(
height * 0.75 * scale),
990 if (endtxt_idx < utf8_len) {
991 int px0 =
max(x + endx,
clip->x0);
992 if (px0 < clip->
x1) {
1003 clip_changed =
true;
1008 y + (
int)(
height * 0.75 * scale),
1028 y + (
int) (
height * 0.75 * scale),
1058 char *utf8_data =
text->utf8_data;
1062 unsigned long line_count =
text->physical_line_count;
1065 long line0 = (
clip->y0 -
y * data->
scale) / scaled_line_height - 1;
1066 long line1 = (
clip->y1 -
y * data->
scale) / scaled_line_height + 1;
1076 if (line_count < (
unsigned long) line0)
1078 if (line_count < (
unsigned long) line1)
1102 for (lineno = line0; lineno != line1; lineno++) {
1103 const char *text_d = utf8_data +
line[lineno].start;
1108 if (!tab_width) tab_width = 1;
1114 while (offset <
length) {
1115 size_t next_offset = offset;
1120 while ((next_offset <
length) &&
1121 (text_d[next_offset] !=
'\t')) {
1128 next_offset - offset,
1131 y + (lineno * scaled_line_height),
1141 if (next_offset >=
length)
1146 next_offset - offset,
1153 ntx = x + ((1 + (tx - x) / tab_width) * tab_width);
1162 unsigned tab_ofst =
line[lineno].start + next_offset;
1164 bool highlighted =
false;
1192 rect.
y0 = y + (lineno * scaled_line_height);
1196 plot_style_highlight,
1204 offset = next_offset + 1;
1274 while (offset > 0) {
1275 size_t next_offset = 0;
1278 while (next_offset < offset &&
text[next_offset] !=
'\t') {
1286 if (next_offset >= offset)
1292 offset -= next_offset;
1293 text += next_offset;
1294 length -= next_offset;
1313 return text->physical_line_count;
1337 if (lineno >=
text->physical_line_count)
1339 line = &
text->physical_line[lineno];
1341 *poffset =
line->start;
1342 *plen =
line->length;
1343 return text->utf8_data +
line->start;
1367 nlines =
text->physical_line_count;
1369 if (offset >
text->utf8_data_size) {
1374 while (lineno < nlines &&
line[lineno].
start < offset) {
1398 const char *pattern,
1413 unsigned match_length;
1415 const char *new_text;
1429 start_idx = offset + (pos -
text);
1432 start_idx + match_length,
1439 new_text = pos + match_length;
1440 offset += (new_text -
text);
1470 unsigned lineno = 0;
1474 assert(
start <= end);
1475 assert(end <= text->utf8_data_size);
1477 utf8_data =
text->utf8_data;
1478 nlines =
text->physical_line_count;
1486 if (lineno + 1 <= nlines ||
line[lineno + 1].
start >= end) {
1495 r->
x1 =
text->formatted_width;
1498 const char *
text = utf8_data +
line[lineno].start;
1535 utf8_size =
text->utf8_data_size;
1538 if (!utf8_size)
return NULL;
1542 if (end >= utf8_size) end = utf8_size;
1544 *plen = end -
start;
1557 struct box *start_box,
1558 struct box *end_box,
1559 struct rect *bounds)
1577 if (end_idx <= start_idx) {
1620 *end_idx =
text->utf8_data_size;
1659 lerror = lwc_intern_string(
"Windows-1252",
1660 SLEN(
"Windows-1252"),
1662 if (lerror != lwc_error_ok) {
Browser window creation and manipulation interface.
void browser_window_page_drag_start(struct browser_window *bw, int x, int y)
Start drag scrolling the contents of the browser window.
void browser_window_set_drag_type(struct browser_window *bw, browser_drag_type type, const struct rect *rect)
Set drag type for a browser window, and inform front end.
browser_drag_type browser_window_get_drag_type(struct browser_window *bw)
Get type of any current drag for a browser window.
static int line_height(const css_unit_ctx *unit_len_ctx, const css_computed_style *style)
Calculate line height from a style.
void content_destroy(struct content *c)
Destroy and free a content.
void content_broadcast(struct content *c, content_msg msg, const union content_msg_data *data)
Send a message to all users.
void content_set_done(struct content *c)
Put a content in status CONTENT_STATUS_DONE.
nserror content__init(struct content *c, const content_handler *handler, lwc_string *imime_type, const struct http_parameter *params, llcache_handle *llcache, const char *fallback_charset, bool quirks)
void content__request_redraw(struct content *c, int x, int y, int width, int height)
Request a redraw of an area of a content.
const uint8_t * content__get_source_data(struct content *c, size_t *size)
Retrieve source of content.
nserror content__clone(const struct content *c, struct content *nc)
Clone a content's data members.
void content_set_ready(struct content *c)
Put a content in status CONTENT_STATUS_READY and unlock the content.
void content_set_status(struct content *c, const char *status_message)
Updates content with new status.
void content_broadcast_error(struct content *c, nserror errorcode, const char *msg)
Send an error message to all users.
nserror content_factory_register_handler(const char *mime_type, const content_handler *handler)
Register a handler with the content factory.
Protected interface to Content handling.
@ CONTENT_STATUS_READY
Some parts of content still being loaded, but can be displayed.
@ CONTENT_STATUS_DONE
Content has completed all processing.
content_type
The type of a content.
@ CONTENT_TEXTPLAIN
content is plain text
@ CONTENT_MSG_STATUS
new status string
@ CONTENT_MSG_POINTER
Wants a specific mouse pointer set.
Useful interned string pointers (interface).
css_fixed nscss_screen_dpi
Screen DPI in fixed point units: defaults to 90, which RISC OS uses.
bool selection_click(struct selection *s, struct browser_window *top, browser_mouse_state mouse, unsigned idx)
Handles mouse clicks (including drag starts) in or near a selection.
bool selection_highlighted(const struct selection *s, unsigned start, unsigned end, unsigned *start_idx, unsigned *end_idx)
Tests whether a text range lies partially within the selection, if there is a selection defined,...
bool selection_clear(struct selection *s, bool redraw)
Clears the current selection, optionally causing the screen to be updated.
struct selection * selection_create(struct content *c)
Creates a new selection object associated with a browser window.
bool selection_string_append(const char *text, size_t length, bool space, plot_font_style_t *style, struct selection_string *sel_string)
Append text to selection string.
void selection_init(struct selection *s)
Initialise the selection object to use the given box subtree as its root, ie.
bool selection_copy_to_clipboard(struct selection *s)
Copy the selected contents to the clipboard.
bool selection_dragging_start(struct selection *s)
void selection_track(struct selection *s, browser_mouse_state mouse, unsigned idx)
Handles movements related to the selection, eg.
bool selection_dragging(struct selection *s)
void selection_destroy(struct selection *s)
Destroys a selection object clearing it if nesessary.
void selection_select_all(struct selection *s)
Selects all the text within the box subtree controlled by this selection object, updating the screen ...
char * selection_get_copy(struct selection *s)
Get copy of selection as string.
Text selection within browser windows (interface).
nserror
Enumeration of error codes.
@ NSERROR_BAD_PARAMETER
Bad Parameter.
@ NSERROR_NOMEM
Memory exhaustion.
@ NSERROR_CLONE_FAILED
Failed to clone handle.
struct netsurf_table * guit
The global interface table.
Interface to core interface table.
High-level resource cache interface.
HTTP header parsing functions.
Public content interface.
Interface to platform-specific layout operation table.
browser_pointer_shape
Mouse pointer type.
@ BROWSER_POINTER_DEFAULT
browser_mouse_state
Mouse state: 1 is primary mouse button.
@ BROWSER_MOUSE_DRAG_1
start of button 1 drag
@ BROWSER_MOUSE_DRAG_2
start of button 2 drag
Target independent plotting interface.
Interface to key press operations.
static struct llcache_s * llcache
low level cache state
#define NSLOG(catname, level, logmsg, args...)
const char * messages_get(const char *key)
Fast lookup of a message by key from the standard Messages hash.
Localised message support (interface).
nserror http_parameter_list_find_item(const http_parameter *list, lwc_string *name, lwc_string **value)
Find a named item in an HTTP parameter list.
#define colour_to_bw_furthest(c0)
plot_style_t * plot_style_fill_white
@ PLOT_FONT_FAMILY_MONOSPACE
plot_style_t * plot_style_fill_black
#define PLOT_STYLE_SCALE
Scaling factor for plot styles.
Interface to utility string handling.
struct browser_window * bw
Column record for a table.
Content operation function table.
parameters to content redraw
int y
coordinate for top-left of redraw
int x
coordinate for top-left of redraw
colour background_colour
The background colour.
float scale
Scale for redraw (for scaling contents without intrinsic dimensions)
Content which corresponds to a single URL.
int height
Height dimension, if applicable.
int width
Width dimension, if applicable.
struct content::@117 textsearch
Free text search state.
struct textsearch_context * context
content_status status
Current status.
unsigned int size
Estimated size of all data associated with this content.
nserror(* position)(const struct plot_font_style *fstyle, const char *string, size_t length, int x, size_t *char_offset, int *actual_x)
Find the position in a string where an x coordinate falls.
nserror(* width)(const struct plot_font_style *fstyle, const char *string, size_t length, int *width)
Measure the width of a string.
Representation of an HTTP parameter.
Handle to low-level cache object.
struct gui_layout_table * layout
Layout table.
Parameters for object element and similar elements.
plot_font_generic_family_t family
Generic family to plot with.
colour foreground
Colour of text.
plot_style_fixed size
Font size, in pt.
colour background
Background colour to blend to, if appropriate.
Plot style for stroke/fill plotters.
colour fill_colour
Colour of fill.
nserror(* text)(const struct redraw_context *ctx, const plot_font_style_t *fstyle, int x, int y, const char *text, size_t length)
Text plotting.
nserror(* rectangle)(const struct redraw_context *ctx, const plot_style_t *pstyle, const struct rect *rectangle)
Plots a rectangle.
nserror(* clip)(const struct redraw_context *ctx, const struct rect *clip)
Sets a clip rectangle for subsequent plot operations.
const struct plotter_table * plot
Current plot operation table.
bool interactive
Redraw to show interactive features.
struct browser_window * bw
unsigned long physical_line_count
struct textplain_line * physical_line
size_t utf8_data_allocated
The context for a free text search.
static bool text_draw(const char *utf8_text, size_t utf8_len, size_t offset, int x, int y, const struct rect *clip, int height, float scale, textplain_content *text, const struct selection *sel, const struct redraw_context *ctx)
Redraw a text string with highlighting (for selection/search)
static nserror textplain_textsearch_find(struct content *c, struct textsearch_context *context, const char *pattern, int p_len, bool case_sens)
Finds all occurrences of a given string in a textplain content.
static bool textplain_convert(struct content *c)
Convert a CONTENT_TEXTPLAIN for display.
static plot_font_style_t textplain_style
static nserror textplain_mouse_track(struct content *c, struct browser_window *bw, browser_mouse_state mouse, int x, int y)
Handle mouse tracking (including drags) in a TEXTPLAIN content window.
static int textplain_coord_from_offset(const char *text, size_t offset, size_t length)
Convert a character offset within a line of text into the horizontal co-ordinate.
static char * textplain_get_raw_data(struct content *c, unsigned start, unsigned end, size_t *plen)
Return a pointer to the raw UTF-8 data, as opposed to the reformatted text to fit the window width.
static void textplain_reformat(struct content *c, int width, int height)
Reformat a CONTENT_TEXTPLAIN to a new width.
static nserror textplain_textselection_copy(struct content *c, unsigned start_idx, unsigned end_idx, struct selection_string *selstr)
static bool textplain_copy_utf8_data(textplain_content *c, const uint8_t *buf, size_t len)
copy utf8 encoded data
static size_t textplain_offset_from_coords(struct content *c, int x, int y, int dir)
Return byte offset within UTF8 textplain content.
static nserror textplain_create(const content_handler *handler, lwc_string *imime_type, const struct http_parameter *params, llcache_handle *llcache, const char *fallback_charset, bool quirks, struct content **c)
Create a CONTENT_TEXTPLAIN.
struct textplain_content textplain_content
plain text content
static char * textplain_get_selection(struct content *c)
Return an textplain content's selection context.
static bool textplain_redraw(struct content *c, struct content_redraw_data *data, const struct rect *clip, const struct redraw_context *ctx)
Draw a CONTENT_TEXTPLAIN using the current set of plotters (plot).
static bool textplain_keypress(struct content *c, uint32_t key)
Handle keypresses.
static void textplain_destroy(struct content *c)
Destroy a CONTENT_TEXTPLAIN and free all resources it owns.
static void textplain_coords_from_range(struct content *c, unsigned start, unsigned end, struct rect *r)
Given a range of byte offsets within a UTF8 textplain content, return a box that fully encloses the t...
static nserror textplain_mouse_action(struct content *c, struct browser_window *bw, browser_mouse_state mouse, int x, int y)
Handle mouse clicks and movements in a TEXTPLAIN content window.
static int textplain_find_line(struct content *c, unsigned offset)
Find line number of byte in text.
static char * textplain_get_line(struct content *c, unsigned lineno, size_t *poffset, size_t *plen)
Return a pointer to the requested line of text.
static nserror textplain_textsearch_bounds(struct content *c, unsigned start_idx, unsigned end_idx, struct box *start_box, struct box *end_box, struct rect *bounds)
get bounds of a free text search match
static bool textplain_drain_input(textplain_content *c, parserutils_inputstream *stream, parserutils_error terminator)
drain input
static int textplain_tab_width
static nserror textplain_open(struct content *c, struct browser_window *bw, struct content *page, struct object_params *params)
Handle a window containing a CONTENT_TEXTPLAIN being opened.
static bool textplain_process_data(struct content *c, const char *data, unsigned int size)
Process data for CONTENT_TEXTPLAIN.
static float textplain_line_height(void)
Calculate the line height, in pixels.
static nserror textplain_close(struct content *c)
Handle a window containing a CONTENT_TEXTPLAIN being closed.
static parserutils_error textplain_charset_hack(const uint8_t *data, size_t len, uint16_t *mibenum, uint32_t *source)
Work around feature in libparserutils.
static nserror textplain_textselection_get_end(struct content *c, unsigned *end_idx)
Retrieve the index of the end of the text.
static void textplain_fini(void)
Clean up after the text content handler.
static nserror textplain_textselection_redraw(struct content *c, unsigned start_idx, unsigned end_idx)
invalidate a region based on offsets into the text cauing a redraw
static content_type textplain_content_type(void)
static nserror textplain_create_internal(textplain_content *c, lwc_string *encoding)
setup plain text render.
static unsigned long textplain_line_count(struct content *c)
Retrieve number of lines in content.
nserror textplain_init(void)
Initialise the text content handler.
static const content_handler textplain_content_handler
plain text content handler table
static nserror textplain_clone(const struct content *old, struct content **newc)
static lwc_string * textplain_default_charset
Interface to content handler for plain text.
const char * content_textsearch_find_pattern(const char *string, int s_len, const char *pattern, int p_len, bool case_sens, unsigned int *m_len)
Find the first occurrence of 'match' in 'string' and return its index.
bool content_textsearch_ishighlighted(struct textsearch_context *textsearch, unsigned start_offset, unsigned end_offset, unsigned *start_idx, unsigned *end_idx)
Determines whether any portion of the given text box should be selected because it matches the curren...
nserror content_textsearch_add_match(struct textsearch_context *context, unsigned start_idx, unsigned end_idx, struct box *start_box, struct box *end_box)
Add a new entry to the list of matches.
Interface to HTML searching.
Extra data for some content_msg messages.
browser_pointer_shape pointer
CONTENT_MSG_POINTER - Mouse pointer to set.
const char * explicit_status_text
CONTENT_MSG_STATUS - Status message update.
Option reading and saving interface.
#define nsoption_int(OPTION)
Get the value of an integer option.
size_t utf8_next(const char *s, size_t l, size_t o)
Find next legal UTF-8 char in string.
UTF-8 manipulation functions (interface).
Interface to a number of general purpose functionality.
#define SLEN(x)
Calculate length of constant C string.
static nserror line(const struct redraw_context *ctx, const plot_style_t *style, const struct rect *line)
Plots a line.
static nserror text(const struct redraw_context *ctx, const struct plot_font_style *fstyle, int x, int y, const char *text, size_t length)
Text plotting.
static nserror clip(const struct redraw_context *ctx, const struct rect *clip)
Sets a clip rectangle for subsequent plot operations.