NetSurf
|
plain text content handling implementation. More...
#include <string.h>
#include <parserutils/input/inputstream.h>
#include "utils/errors.h"
#include "utils/corestrings.h"
#include "utils/http.h"
#include "utils/log.h"
#include "utils/messages.h"
#include "utils/utils.h"
#include "utils/utf8.h"
#include "utils/nsoption.h"
#include "netsurf/content.h"
#include "netsurf/keypress.h"
#include "netsurf/browser_window.h"
#include "netsurf/plotters.h"
#include "netsurf/layout.h"
#include "content/content_protected.h"
#include "content/content_factory.h"
#include "content/hlcache.h"
#include "content/textsearch.h"
#include "content/handlers/css/utils.h"
#include "desktop/selection.h"
#include "desktop/gui_internal.h"
#include "text/textplain.h"
Go to the source code of this file.
Data Structures | |
struct | textplain_line |
struct | textplain_content |
plain text content More... | |
Macros | |
#define | CHUNK 32768 /* Must be a power of 2 */ |
#define | MARGIN 4 |
#define | TAB_WIDTH 8 /* must be power of 2 currently */ |
#define | TEXT_SIZE 10 * PLOT_STYLE_SCALE /* Unscaled text size in pt */ |
Typedefs | |
typedef struct textplain_content | textplain_content |
plain text content More... | |
Functions | |
static void | textplain_fini (void) |
Clean up after the text content handler. More... | |
static parserutils_error | textplain_charset_hack (const uint8_t *data, size_t len, uint16_t *mibenum, uint32_t *source) |
Work around feature in libparserutils. More... | |
static nserror | textplain_create_internal (textplain_content *c, lwc_string *encoding) |
setup plain text render. More... | |
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. More... | |
static bool | textplain_copy_utf8_data (textplain_content *c, const uint8_t *buf, size_t len) |
copy utf8 encoded data More... | |
static bool | textplain_drain_input (textplain_content *c, parserutils_inputstream *stream, parserutils_error terminator) |
drain input More... | |
static bool | textplain_process_data (struct content *c, const char *data, unsigned int size) |
Process data for CONTENT_TEXTPLAIN. More... | |
static bool | textplain_convert (struct content *c) |
Convert a CONTENT_TEXTPLAIN for display. More... | |
static float | textplain_line_height (void) |
Calculate the line height, in pixels. More... | |
static void | textplain_reformat (struct content *c, int width, int height) |
Reformat a CONTENT_TEXTPLAIN to a new width. More... | |
static void | textplain_destroy (struct content *c) |
Destroy a CONTENT_TEXTPLAIN and free all resources it owns. More... | |
static nserror | textplain_clone (const struct content *old, struct content **newc) |
static content_type | textplain_content_type (void) |
static size_t | textplain_offset_from_coords (struct content *c, int x, int y, int dir) |
Return byte offset within UTF8 textplain content. More... | |
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. More... | |
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. More... | |
static bool | textplain_keypress (struct content *c, uint32_t key) |
Handle keypresses. More... | |
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) More... | |
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). More... | |
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. More... | |
static nserror | textplain_close (struct content *c) |
Handle a window containing a CONTENT_TEXTPLAIN being closed. More... | |
static char * | textplain_get_selection (struct content *c) |
Return an textplain content's selection context. More... | |
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. More... | |
static unsigned long | textplain_line_count (struct content *c) |
Retrieve number of lines in content. More... | |
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. More... | |
static int | textplain_find_line (struct content *c, unsigned offset) |
Find line number of byte in text. More... | |
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. More... | |
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 text. More... | |
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. More... | |
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 More... | |
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 More... | |
static nserror | textplain_textselection_copy (struct content *c, unsigned start_idx, unsigned end_idx, struct selection_string *selstr) |
static nserror | textplain_textselection_get_end (struct content *c, unsigned *end_idx) |
Retrieve the index of the end of the text. More... | |
nserror | textplain_init (void) |
Initialise the text content handler. More... | |
Variables | |
static plot_font_style_t | textplain_style |
static int | textplain_tab_width = 256 |
static lwc_string * | textplain_default_charset |
static const content_handler | textplain_content_handler |
plain text content handler table More... | |
plain text content handling implementation.
Definition in file textplain.c.
#define CHUNK 32768 /* Must be a power of 2 */ |
Definition at line 78 of file textplain.c.
#define MARGIN 4 |
Definition at line 79 of file textplain.c.
#define TAB_WIDTH 8 /* must be power of 2 currently */ |
Definition at line 81 of file textplain.c.
#define TEXT_SIZE 10 * PLOT_STYLE_SCALE /* Unscaled text size in pt */ |
Definition at line 82 of file textplain.c.
typedef struct textplain_content textplain_content |
plain text content
|
static |
Redraw a text string with highlighting (for selection/search)
utf8_text | pointer to UTF-8 text string |
utf8_len | length of string, in bytes |
offset | byte offset within textual representation |
x | x ordinate at which to plot text |
y | y ordinate at which to plot text |
clip | pointer to current clip rectangle |
height | height of text string |
scale | current display scale (1.0 = 100%) |
text | Content being redrawn. |
sel | Selection context |
search | Search context |
ctx | current redraw context |
Definition at line 844 of file textplain.c.
References plot_font_style::background, clip(), plotter_table::clip, colour_to_bw_furthest, content_textsearch_ishighlighted(), selection::end_idx, plot_style_s::fill_colour, plot_font_style::foreground, guit, height, redraw_context::interactive, netsurf_table::layout, max, min, NSERROR_OK, redraw_context::plot, plot_style_fill_white, plotter_table::rectangle, selection_highlighted(), plot_font_style::size, selection::start_idx, text(), plotter_table::text, textplain_style, gui_layout_table::width, rect::x0, rect::x1, rect::y0, and rect::y1.
Referenced by textplain_redraw().
|
static |
Work around feature in libparserutils.
if the client provides an encoding up front, but does not provide a charset detection callback, then libparserutils will replace the provided encoding with UTF-8. This breaks our input handling.
Avoid this by providing a callback that does precisely nothing, thus preserving whatever charset information we decided on in textplain_create.
Definition at line 122 of file textplain.c.
Referenced by textplain_create_internal().
Definition at line 548 of file textplain.c.
References content__clone(), content__get_source_data(), content_destroy(), CONTENT_STATUS_DONE, CONTENT_STATUS_READY, textplain_content::encoding, NSERROR_CLONE_FAILED, NSERROR_NOMEM, NSERROR_OK, content::status, text(), textplain_convert(), textplain_create_internal(), and textplain_process_data().
Handle a window containing a CONTENT_TEXTPLAIN being closed.
Definition at line 1236 of file textplain.c.
References NSERROR_OK, and text().
|
static |
Definition at line 595 of file textplain.c.
References CONTENT_TEXTPLAIN.
|
static |
Convert a CONTENT_TEXTPLAIN for display.
Definition at line 360 of file textplain.c.
References content_set_done(), content_set_ready(), content_set_status(), messages_get(), text(), and textplain_drain_input().
Referenced by textplain_clone().
|
static |
Convert a character offset within a line of text into the horizontal co-ordinate.
The conversion takes into account the font being used and any tabs in the text
text | line of text |
offset | char offset within text |
length | line length |
Definition at line 1270 of file textplain.c.
References guit, netsurf_table::layout, text(), textplain_style, textplain_tab_width, utf8_next(), and gui_layout_table::width.
Referenced by textplain_coords_from_range().
|
static |
Given a range of byte offsets within a UTF8 textplain content, return a box that fully encloses the text.
[in] | c | content of type CONTENT_TEXTPLAIN |
[in] | start | byte offset of start of text range |
[in] | end | byte offset of end |
[out] | r | rectangle to be completed |
Definition at line 1461 of file textplain.c.
References textplain_line::length, line(), line_height(), MARGIN, textplain_line::start, text(), textplain_coord_from_offset(), textplain_find_line(), textplain_line_height(), rect::x0, rect::x1, rect::y0, and rect::y1.
Referenced by textplain_textsearch_bounds(), and textplain_textselection_redraw().
|
static |
copy utf8 encoded data
Definition at line 236 of file textplain.c.
References CHUNK, textplain_content::utf8_data, textplain_content::utf8_data_allocated, and textplain_content::utf8_data_size.
Referenced by textplain_drain_input().
|
static |
Create a CONTENT_TEXTPLAIN.
Definition at line 187 of file textplain.c.
References selection::c, content__init(), http_parameter_list_find_item(), llcache, NSERROR_NOMEM, NSERROR_OK, text(), textplain_create_internal(), and textplain_default_charset.
|
static |
setup plain text render.
[in] | c | content object. |
[in] | encoding | the encoding of the content. |
Definition at line 139 of file textplain.c.
References selection::c, CHUNK, content_broadcast_error(), NSERROR_NOMEM, NSERROR_OK, nsoption_int, PLOT_STYLE_SCALE, selection_create(), plot_font_style::size, textplain_charset_hack(), and textplain_style.
Referenced by textplain_clone(), and textplain_create().
|
static |
Destroy a CONTENT_TEXTPLAIN and free all resources it owns.
Definition at line 524 of file textplain.c.
References selection_destroy(), and text().
|
static |
drain input
Definition at line 263 of file textplain.c.
References CHUNK, and textplain_copy_utf8_data().
Referenced by textplain_convert(), and textplain_process_data().
|
static |
Find line number of byte in text.
Given a byte offset within the text, return the line number of the line containing that offset.
[in] | c | content of type CONTENT_TEXTPLAIN |
[in] | offset | byte offset within textual representation |
Definition at line 1357 of file textplain.c.
References line(), textplain_line::start, and text().
Referenced by textplain_coords_from_range().
|
static |
Clean up after the text content handler.
Definition at line 101 of file textplain.c.
References textplain_default_charset.
|
static |
Return a pointer to the requested line of text.
[in] | c | content of type CONTENT_TEXTPLAIN |
[in] | lineno | line number |
[out] | poffset | receives byte offset of line start within text |
[out] | plen | receives length of returned line |
Definition at line 1327 of file textplain.c.
References line(), and text().
Referenced by textplain_textsearch_find().
|
static |
Return a pointer to the raw UTF-8 data, as opposed to the reformatted text to fit the window width.
Thus only hard newlines are preserved in the saved/copied text of a selection.
[in] | c | content of type CONTENT_TEXTPLAIN |
[in] | start | starting byte offset within UTF-8 text |
[in] | end | ending byte offset |
[out] | plen | receives validated length |
Definition at line 1525 of file textplain.c.
References textplain_line::start, and text().
Referenced by textplain_textselection_copy().
|
static |
Return an textplain content's selection context.
Definition at line 1249 of file textplain.c.
References selection_get_copy(), and text().
nserror textplain_init | ( | void | ) |
Initialise the text content handler.
Definition at line 1654 of file textplain.c.
References content_factory_register_handler(), NSERROR_NOMEM, NSERROR_OK, SLEN, textplain_content_handler, and textplain_default_charset.
Referenced by netsurf_init().
|
static |
Handle keypresses.
c | content of type CONTENT_TEXTPLAIN |
key | The UCS4 character codepoint |
Definition at line 798 of file textplain.c.
References NS_KEY_CLEAR_SELECTION, NS_KEY_COPY_SELECTION, NS_KEY_ESCAPE, NS_KEY_SELECT_ALL, selection_clear(), selection_copy_to_clipboard(), selection_select_all(), and text().
|
static |
Retrieve number of lines in content.
[in] | c | Content to retrieve line count from |
Definition at line 1307 of file textplain.c.
References text().
Referenced by textplain_textsearch_find().
|
static |
Calculate the line height, in pixels.
Definition at line 390 of file textplain.c.
References nscss_screen_dpi, PLOT_STYLE_SCALE, plot_font_style::size, and textplain_style.
Referenced by textplain_coords_from_range(), textplain_offset_from_coords(), textplain_redraw(), and textplain_reformat().
|
static |
Handle mouse clicks and movements in a TEXTPLAIN content window.
c | content of type textplain |
bw | browser window |
mouse | mouse state on action |
x | coordinate of mouse |
y | coordinate of mouse |
Definition at line 699 of file textplain.c.
References BROWSER_MOUSE_DRAG_1, BROWSER_MOUSE_DRAG_2, BROWSER_POINTER_DEFAULT, BROWSER_POINTER_MOVE, browser_window_page_drag_start(), browser_window_set_drag_type(), content_broadcast(), CONTENT_MSG_POINTER, CONTENT_MSG_STATUS, DRAGGING_NONE, DRAGGING_SELECTION, content_msg_data::explicit_status_text, messages_get(), NSERROR_OK, content_msg_data::pointer, selection_click(), selection_dragging(), text(), textplain_offset_from_coords(), content_msg_data::x, and content_msg_data::y.
Referenced by textplain_mouse_track().
|
static |
Handle mouse tracking (including drags) in a TEXTPLAIN content window.
c | content of type textplain |
bw | browser window |
mouse | state of mouse buttons and modifier keys |
x | coordinate of mouse |
y | coordinate of mouse |
Definition at line 749 of file textplain.c.
References browser_window_get_drag_type(), browser_window_set_drag_type(), DRAGGING_NONE, DRAGGING_SELECTION, NSERROR_OK, selection_dragging_start(), selection_track(), text(), textplain_mouse_action(), textplain_offset_from_coords(), content_msg_data::x, and content_msg_data::y.
|
static |
Return byte offset within UTF8 textplain content.
given the co-ordinates of a point within a textplain content. 'dir' specifies the direction in which to search (-1 = above-left, +1 = below-right) if the co-ordinates are not contained within a line.
[in] | c | content of type CONTENT_TEXTPLAIN |
[in] | x | x ordinate of point |
[in] | y | y ordinate of point |
[in] | dir | direction of search if not within line |
Definition at line 615 of file textplain.c.
References guit, netsurf_table::layout, textplain_line::length, line(), line_height(), MARGIN, textplain_content::physical_line, textplain_content::physical_line_count, gui_layout_table::position, text(), textplain_line_height(), textplain_style, textplain_tab_width, textplain_content::utf8_data, utf8_next(), width, and gui_layout_table::width.
Referenced by textplain_mouse_action(), and textplain_mouse_track().
|
static |
Handle a window containing a CONTENT_TEXTPLAIN being opened.
Definition at line 1217 of file textplain.c.
References NSERROR_OK, selection_init(), and text().
|
static |
Process data for CONTENT_TEXTPLAIN.
Definition at line 334 of file textplain.c.
References content_broadcast_error(), NSERROR_NOMEM, content::size, text(), and textplain_drain_input().
Referenced by textplain_clone().
|
static |
Draw a CONTENT_TEXTPLAIN using the current set of plotters (plot).
x, y, clip_[xy][01] are in target coordinates.
c | content of type CONTENT_TEXTPLAIN |
data | redraw data for this content redraw |
clip | current clip region |
ctx | current redraw context |
Definition at line 1051 of file textplain.c.
References plot_font_style::background, content_redraw_data::background_colour, browser_window::bw, selection::c, clip(), content_textsearch_ishighlighted(), content::context, selection::end_idx, guit, netsurf_table::layout, textplain_line::length, line(), line_height(), MARGIN, NSERROR_OK, redraw_context::plot, plot_style_fill_black, plot_style_fill_white, plotter_table::rectangle, content_redraw_data::scale, selection_highlighted(), textplain_line::start, selection::start_idx, text(), text_draw(), textplain_line_height(), textplain_style, textplain_tab_width, content::textsearch, utf8_next(), width, gui_layout_table::width, browser_window::x, content_redraw_data::x, rect::x0, rect::x1, browser_window::y, content_redraw_data::y, rect::y0, and rect::y1.
|
static |
Reformat a CONTENT_TEXTPLAIN to a new width.
Definition at line 402 of file textplain.c.
References guit, content::height, height, netsurf_table::layout, line(), MARGIN, NSERROR_OK, NSLOG, TAB_WIDTH, text(), textplain_line_height(), textplain_style, textplain_tab_width, content::width, width, and gui_layout_table::width.
|
static |
get bounds of a free text search match
Definition at line 1554 of file textplain.c.
References NSERROR_OK, and textplain_coords_from_range().
|
static |
Finds all occurrences of a given string in a textplain content.
c | the content to be searched |
context | The search context to add the entry to. |
pattern | the string pattern to search for |
p_len | pattern length |
case_sens | whether to perform a case sensitive search |
Definition at line 1396 of file textplain.c.
References content_textsearch_add_match(), content_textsearch_find_pattern(), textplain_line::length, line(), NSERROR_OK, text(), textplain_get_line(), and textplain_line_count().
|
static |
Definition at line 1589 of file textplain.c.
References NSERROR_NOMEM, NSERROR_OK, selection_string_append(), text(), and textplain_get_raw_data().
Retrieve the index of the end of the text.
[in] | c | Content to retrieve size of |
Definition at line 1616 of file textplain.c.
References NSERROR_OK, and text().
|
static |
invalidate a region based on offsets into the text cauing a redraw
Definition at line 1571 of file textplain.c.
References content__request_redraw(), NSERROR_BAD_PARAMETER, NSERROR_OK, textplain_coords_from_range(), rect::x0, rect::x1, rect::y0, and rect::y1.
|
static |
plain text content handler table
Definition at line 1628 of file textplain.c.
Referenced by textplain_init().
|
static |
Definition at line 95 of file textplain.c.
Referenced by textplain_create(), textplain_fini(), and textplain_init().
|
static |
Definition at line 84 of file textplain.c.
Referenced by text_draw(), textplain_coord_from_offset(), textplain_create_internal(), textplain_line_height(), textplain_offset_from_coords(), textplain_redraw(), and textplain_reformat().
|
static |
Definition at line 93 of file textplain.c.
Referenced by textplain_coord_from_offset(), textplain_offset_from_coords(), textplain_redraw(), and textplain_reformat().