2003-06-30 16:44:03 +04:00
|
|
|
/*
|
2006-02-13 02:07:28 +03:00
|
|
|
* Copyright 2006 James Bursa <bursa@users.sourceforge.net>
|
2006-02-16 02:09:55 +03:00
|
|
|
* Copyright 2006 Adrian Lees <adrianl@users.sourceforge.net>
|
2007-08-08 20:16:03 +04:00
|
|
|
*
|
|
|
|
* 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/>.
|
2003-02-09 15:58:15 +03:00
|
|
|
*/
|
|
|
|
|
2017-06-02 00:43:45 +03:00
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
*
|
|
|
|
* plain text content handling implementation.
|
2006-02-13 02:07:28 +03:00
|
|
|
*/
|
|
|
|
|
2010-09-14 02:18:51 +04:00
|
|
|
#include <parserutils/input/inputstream.h>
|
|
|
|
|
2020-05-10 17:15:33 +03:00
|
|
|
#include "utils/errors.h"
|
2014-10-13 14:56:31 +04:00
|
|
|
#include "utils/corestrings.h"
|
|
|
|
#include "utils/http.h"
|
|
|
|
#include "utils/log.h"
|
|
|
|
#include "utils/messages.h"
|
|
|
|
#include "utils/utils.h"
|
|
|
|
#include "utils/utf8.h"
|
2020-05-10 17:15:33 +03:00
|
|
|
#include "utils/nsoption.h"
|
2016-06-06 10:59:23 +03:00
|
|
|
#include "netsurf/content.h"
|
2016-07-01 16:27:04 +03:00
|
|
|
#include "netsurf/keypress.h"
|
|
|
|
#include "netsurf/browser_window.h"
|
|
|
|
#include "netsurf/plotters.h"
|
|
|
|
#include "netsurf/layout.h"
|
2010-03-28 16:56:39 +04:00
|
|
|
#include "content/content_protected.h"
|
2020-05-07 22:55:44 +03:00
|
|
|
#include "content/content_factory.h"
|
2010-03-28 16:56:39 +04:00
|
|
|
#include "content/hlcache.h"
|
2020-05-11 01:22:29 +03:00
|
|
|
#include "content/textsearch.h"
|
2007-05-31 02:39:54 +04:00
|
|
|
#include "desktop/selection.h"
|
2016-04-24 01:32:21 +03:00
|
|
|
#include "desktop/gui_internal.h"
|
2014-10-13 14:56:31 +04:00
|
|
|
|
2018-05-10 13:34:26 +03:00
|
|
|
#include "text/textplain.h"
|
2006-02-13 02:07:28 +03:00
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
struct textplain_line {
|
|
|
|
size_t start;
|
|
|
|
size_t length;
|
|
|
|
};
|
|
|
|
|
2020-05-10 17:15:33 +03:00
|
|
|
/**
|
|
|
|
* plain text content
|
|
|
|
*/
|
2011-05-07 00:40:09 +04:00
|
|
|
typedef struct textplain_content {
|
|
|
|
struct content base;
|
|
|
|
|
2011-07-08 12:38:17 +04:00
|
|
|
lwc_string *encoding;
|
2011-05-07 00:40:09 +04:00
|
|
|
void *inputstream;
|
|
|
|
char *utf8_data;
|
|
|
|
size_t utf8_data_size;
|
|
|
|
size_t utf8_data_allocated;
|
|
|
|
unsigned long physical_line_count;
|
|
|
|
struct textplain_line *physical_line;
|
|
|
|
int formatted_width;
|
2011-06-30 22:27:24 +04:00
|
|
|
struct browser_window *bw;
|
2011-07-13 17:20:26 +04:00
|
|
|
|
|
|
|
struct selection sel; /** Selection state */
|
2011-08-24 16:29:30 +04:00
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
} textplain_content;
|
|
|
|
|
2003-02-09 15:58:15 +03:00
|
|
|
|
2010-10-23 22:43:48 +04:00
|
|
|
#define CHUNK 32768 /* Must be a power of 2 */
|
2006-02-13 02:07:28 +03:00
|
|
|
#define MARGIN 4
|
2003-02-09 15:58:15 +03:00
|
|
|
|
2006-03-25 11:53:32 +03:00
|
|
|
#define TAB_WIDTH 8 /* must be power of 2 currently */
|
2018-05-23 13:48:35 +03:00
|
|
|
#define TEXT_SIZE 10 * PLOT_STYLE_SCALE /* Unscaled text size in pt */
|
2006-03-25 11:53:32 +03:00
|
|
|
|
2009-07-21 14:59:53 +04:00
|
|
|
static plot_font_style_t textplain_style = {
|
|
|
|
.family = PLOT_FONT_FAMILY_MONOSPACE,
|
2011-03-15 18:10:10 +03:00
|
|
|
.size = TEXT_SIZE,
|
2009-07-21 14:59:53 +04:00
|
|
|
.weight = 400,
|
|
|
|
.flags = FONTF_NONE,
|
|
|
|
.background = 0xffffff,
|
|
|
|
.foreground = 0x000000,
|
|
|
|
};
|
|
|
|
|
2006-03-25 11:53:32 +03:00
|
|
|
static int textplain_tab_width = 256; /* try for a sensible default */
|
2003-02-09 15:58:15 +03:00
|
|
|
|
2011-07-08 12:38:17 +04:00
|
|
|
static lwc_string *textplain_default_charset;
|
2011-05-07 00:40:09 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clean up after the text content handler
|
|
|
|
*/
|
2017-06-02 00:43:45 +03:00
|
|
|
static void textplain_fini(void)
|
2011-05-07 00:40:09 +04:00
|
|
|
{
|
2011-09-16 02:31:16 +04:00
|
|
|
if (textplain_default_charset != NULL) {
|
|
|
|
lwc_string_unref(textplain_default_charset);
|
|
|
|
textplain_default_charset = NULL;
|
|
|
|
}
|
2011-05-07 00:40:09 +04:00
|
|
|
}
|
2003-02-09 15:58:15 +03:00
|
|
|
|
2010-04-04 16:41:19 +04:00
|
|
|
|
2017-06-02 00:43:45 +03:00
|
|
|
/**
|
|
|
|
* Work around feature in libparserutils
|
2010-09-14 02:18:51 +04:00
|
|
|
*
|
2017-06-02 00:43:45 +03:00
|
|
|
* 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,
|
2010-09-14 02:18:51 +04:00
|
|
|
* thus preserving whatever charset information we decided on in
|
|
|
|
* textplain_create.
|
|
|
|
*/
|
2017-06-02 00:43:45 +03:00
|
|
|
static parserutils_error
|
|
|
|
textplain_charset_hack(const uint8_t *data,
|
|
|
|
size_t len,
|
|
|
|
uint16_t *mibenum,
|
|
|
|
uint32_t *source)
|
2010-09-14 02:18:51 +04:00
|
|
|
{
|
|
|
|
return PARSERUTILS_OK;
|
2017-06-02 00:43:45 +03:00
|
|
|
}
|
2010-09-14 02:18:51 +04:00
|
|
|
|
2017-06-02 00:43:45 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* setup plain text render.
|
|
|
|
*
|
|
|
|
* \param[in] c content object.
|
|
|
|
* \param[in] encoding the encoding of the content.
|
|
|
|
* \return NSERROR_OK else appropriate error code.
|
|
|
|
*/
|
|
|
|
static nserror
|
|
|
|
textplain_create_internal(textplain_content *c, lwc_string *encoding)
|
2010-04-04 16:41:19 +04:00
|
|
|
{
|
|
|
|
char *utf8_data;
|
2010-09-14 02:18:51 +04:00
|
|
|
parserutils_inputstream *stream;
|
|
|
|
parserutils_error error;
|
2010-04-04 16:41:19 +04:00
|
|
|
|
2018-05-23 13:48:35 +03:00
|
|
|
textplain_style.size = (nsoption_int(font_size) * PLOT_STYLE_SCALE) / 10;
|
2011-05-07 00:40:09 +04:00
|
|
|
|
2012-10-03 22:24:58 +04:00
|
|
|
utf8_data = malloc(CHUNK);
|
2010-09-14 02:18:51 +04:00
|
|
|
if (utf8_data == NULL)
|
2010-04-04 16:41:19 +04:00
|
|
|
goto no_memory;
|
|
|
|
|
2017-06-02 00:43:45 +03:00
|
|
|
error = parserutils_inputstream_create(lwc_string_data(encoding), 0,
|
|
|
|
textplain_charset_hack, &stream);
|
2010-09-14 02:18:51 +04:00
|
|
|
if (error == PARSERUTILS_BADENCODING) {
|
|
|
|
/* Fall back to Windows-1252 */
|
|
|
|
error = parserutils_inputstream_create("Windows-1252", 0,
|
2017-06-02 00:43:45 +03:00
|
|
|
textplain_charset_hack, &stream);
|
2006-02-13 02:07:28 +03:00
|
|
|
}
|
2010-09-14 02:18:51 +04:00
|
|
|
if (error != PARSERUTILS_OK) {
|
2012-10-03 22:24:58 +04:00
|
|
|
free(utf8_data);
|
2010-09-14 02:18:51 +04:00
|
|
|
goto no_memory;
|
2006-02-13 02:07:28 +03:00
|
|
|
}
|
2017-06-02 00:43:45 +03:00
|
|
|
|
2011-07-08 12:38:17 +04:00
|
|
|
c->encoding = lwc_string_ref(encoding);
|
2011-05-07 00:40:09 +04:00
|
|
|
c->inputstream = stream;
|
|
|
|
c->utf8_data = utf8_data;
|
|
|
|
c->utf8_data_size = 0;
|
|
|
|
c->utf8_data_allocated = CHUNK;
|
|
|
|
c->physical_line = 0;
|
|
|
|
c->physical_line_count = 0;
|
|
|
|
c->formatted_width = 0;
|
2011-06-30 22:27:24 +04:00
|
|
|
c->bw = NULL;
|
2006-02-13 02:07:28 +03:00
|
|
|
|
2011-07-26 17:53:42 +04:00
|
|
|
selection_prepare(&c->sel, (struct content *)c, false);
|
2011-07-13 17:20:26 +04:00
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
return NSERROR_OK;
|
2006-02-13 02:07:28 +03:00
|
|
|
|
|
|
|
no_memory:
|
2019-08-05 17:25:15 +03:00
|
|
|
content_broadcast_error(&c->base, NSERROR_NOMEM, NULL);
|
2017-06-02 00:43:45 +03:00
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
return NSERROR_NOMEM;
|
2003-02-09 15:58:15 +03:00
|
|
|
}
|
|
|
|
|
2017-06-02 00:43:45 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a CONTENT_TEXTPLAIN.
|
|
|
|
*/
|
|
|
|
static nserror
|
|
|
|
textplain_create(const content_handler *handler,
|
|
|
|
lwc_string *imime_type,
|
2020-05-10 17:15:33 +03:00
|
|
|
const struct http_parameter *params,
|
2017-06-02 00:43:45 +03:00
|
|
|
llcache_handle *llcache,
|
|
|
|
const char *fallback_charset,
|
|
|
|
bool quirks,
|
|
|
|
struct content **c)
|
|
|
|
{
|
|
|
|
textplain_content *text;
|
|
|
|
nserror error;
|
|
|
|
lwc_string *encoding;
|
|
|
|
|
|
|
|
text = calloc(1, sizeof(textplain_content));
|
|
|
|
if (text == NULL) {
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = content__init(&text->base, handler, imime_type, params,
|
|
|
|
llcache, fallback_charset, quirks);
|
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
free(text);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = http_parameter_list_find_item(params, corestring_lwc_charset,
|
|
|
|
&encoding);
|
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
encoding = lwc_string_ref(textplain_default_charset);
|
|
|
|
}
|
|
|
|
|
|
|
|
error = textplain_create_internal(text, encoding);
|
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
lwc_string_unref(encoding);
|
|
|
|
free(text);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
lwc_string_unref(encoding);
|
|
|
|
|
|
|
|
*c = (struct content *) text;
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* copy utf8 encoded data
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
textplain_copy_utf8_data(textplain_content *c, const uint8_t *buf, size_t len)
|
|
|
|
{
|
|
|
|
if (c->utf8_data_size + len >= c->utf8_data_allocated) {
|
|
|
|
/* Compute next multiple of chunk above the required space */
|
|
|
|
size_t allocated;
|
|
|
|
char *utf8_data;
|
|
|
|
|
|
|
|
allocated = (c->utf8_data_size + len + CHUNK - 1) & ~(CHUNK - 1);
|
|
|
|
utf8_data = realloc(c->utf8_data, allocated);
|
|
|
|
if (utf8_data == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
c->utf8_data = utf8_data;
|
|
|
|
c->utf8_data_allocated = allocated;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(c->utf8_data + c->utf8_data_size, buf, len);
|
|
|
|
c->utf8_data_size += len;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* drain input
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
textplain_drain_input(textplain_content *c,
|
|
|
|
parserutils_inputstream *stream,
|
|
|
|
parserutils_error terminator)
|
2004-11-22 03:33:04 +03:00
|
|
|
{
|
2010-09-14 02:32:52 +04:00
|
|
|
static const uint8_t *u_fffd = (const uint8_t *) "\xef\xbf\xfd";
|
2010-09-14 02:18:51 +04:00
|
|
|
const uint8_t *ch;
|
2010-10-23 22:43:48 +04:00
|
|
|
size_t chlen, offset = 0;
|
2010-09-14 02:18:51 +04:00
|
|
|
|
2017-06-02 00:43:45 +03:00
|
|
|
while (parserutils_inputstream_peek(stream, offset, &ch, &chlen) !=
|
|
|
|
terminator) {
|
2010-09-14 02:32:52 +04:00
|
|
|
/* Replace all instances of NUL with U+FFFD */
|
|
|
|
if (chlen == 1 && *ch == 0) {
|
2010-10-23 22:43:48 +04:00
|
|
|
if (offset > 0) {
|
|
|
|
/* Obtain pointer to start of input data */
|
2017-06-02 00:43:45 +03:00
|
|
|
parserutils_inputstream_peek(stream, 0,
|
|
|
|
&ch, &chlen);
|
2010-10-23 22:43:48 +04:00
|
|
|
/* Copy from it up to the start of the NUL */
|
2017-06-02 00:43:45 +03:00
|
|
|
if (textplain_copy_utf8_data(c, ch,
|
|
|
|
offset) == false)
|
2010-10-23 22:43:48 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Emit U+FFFD */
|
|
|
|
if (textplain_copy_utf8_data(c, u_fffd, 3) == false)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Advance inputstream past the NUL we just read */
|
|
|
|
parserutils_inputstream_advance(stream, offset + 1);
|
|
|
|
/* Reset the read offset */
|
|
|
|
offset = 0;
|
2010-09-14 02:32:52 +04:00
|
|
|
} else {
|
2010-10-23 22:43:48 +04:00
|
|
|
/* Accumulate input */
|
|
|
|
offset += chlen;
|
|
|
|
|
|
|
|
if (offset > CHUNK) {
|
|
|
|
/* Obtain pointer to start of input data */
|
2017-06-02 00:43:45 +03:00
|
|
|
parserutils_inputstream_peek(stream, 0,
|
|
|
|
&ch, &chlen);
|
2010-10-23 22:43:48 +04:00
|
|
|
|
|
|
|
/* Emit the data we've read */
|
2017-06-02 00:43:45 +03:00
|
|
|
if (textplain_copy_utf8_data(c, ch,
|
|
|
|
offset) == false)
|
2010-10-23 22:43:48 +04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Advance the inputstream */
|
|
|
|
parserutils_inputstream_advance(stream, offset);
|
|
|
|
/* Reset the read offset */
|
|
|
|
offset = 0;
|
|
|
|
}
|
2010-09-14 02:32:52 +04:00
|
|
|
}
|
2010-10-23 22:43:48 +04:00
|
|
|
}
|
2010-09-14 02:32:52 +04:00
|
|
|
|
2010-10-23 22:43:48 +04:00
|
|
|
if (offset > 0) {
|
|
|
|
/* Obtain pointer to start of input data */
|
2017-06-02 00:43:45 +03:00
|
|
|
parserutils_inputstream_peek(stream, 0, &ch, &chlen);
|
2010-10-23 22:43:48 +04:00
|
|
|
/* Emit any data remaining */
|
|
|
|
if (textplain_copy_utf8_data(c, ch, offset) == false)
|
|
|
|
return false;
|
2010-09-14 02:18:51 +04:00
|
|
|
|
2010-10-23 22:43:48 +04:00
|
|
|
/* Advance the inputstream past the data we've read */
|
|
|
|
parserutils_inputstream_advance(stream, offset);
|
|
|
|
}
|
2007-03-12 01:08:57 +03:00
|
|
|
|
2010-10-23 22:43:48 +04:00
|
|
|
return true;
|
|
|
|
}
|
2007-03-12 01:08:57 +03:00
|
|
|
|
2010-09-14 02:18:51 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Process data for CONTENT_TEXTPLAIN.
|
|
|
|
*/
|
2017-06-02 00:43:45 +03:00
|
|
|
static bool
|
|
|
|
textplain_process_data(struct content *c, const char *data, unsigned int size)
|
2010-09-14 02:18:51 +04:00
|
|
|
{
|
2011-05-07 00:40:09 +04:00
|
|
|
textplain_content *text = (textplain_content *) c;
|
|
|
|
parserutils_inputstream *stream = text->inputstream;
|
2010-09-14 02:18:51 +04:00
|
|
|
parserutils_error error;
|
|
|
|
|
2017-06-02 00:43:45 +03:00
|
|
|
error = parserutils_inputstream_append(stream,
|
|
|
|
(const uint8_t *) data, size);
|
2010-09-14 02:18:51 +04:00
|
|
|
if (error != PARSERUTILS_OK) {
|
|
|
|
goto no_memory;
|
|
|
|
}
|
2006-02-13 02:07:28 +03:00
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
if (textplain_drain_input(text, stream, PARSERUTILS_NEEDDATA) == false)
|
2010-09-14 02:18:51 +04:00
|
|
|
goto no_memory;
|
2006-02-13 02:07:28 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
no_memory:
|
2019-08-05 17:25:15 +03:00
|
|
|
content_broadcast_error(c, NSERROR_NOMEM, NULL);
|
2006-02-13 02:07:28 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a CONTENT_TEXTPLAIN for display.
|
|
|
|
*/
|
2017-06-02 00:43:45 +03:00
|
|
|
static bool textplain_convert(struct content *c)
|
2006-02-13 02:07:28 +03:00
|
|
|
{
|
2011-05-07 00:40:09 +04:00
|
|
|
textplain_content *text = (textplain_content *) c;
|
|
|
|
parserutils_inputstream *stream = text->inputstream;
|
2010-09-14 02:18:51 +04:00
|
|
|
parserutils_error error;
|
|
|
|
|
|
|
|
error = parserutils_inputstream_append(stream, NULL, 0);
|
|
|
|
if (error != PARSERUTILS_OK) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
if (textplain_drain_input(text, stream, PARSERUTILS_EOF) == false)
|
2010-09-14 02:18:51 +04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
parserutils_inputstream_destroy(stream);
|
2011-05-07 00:40:09 +04:00
|
|
|
text->inputstream = NULL;
|
2006-02-13 02:07:28 +03:00
|
|
|
|
2011-02-27 23:11:39 +03:00
|
|
|
content_set_ready(c);
|
|
|
|
content_set_done(c);
|
2006-07-05 01:01:17 +04:00
|
|
|
content_set_status(c, messages_get("Done"));
|
2006-02-13 02:07:28 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2017-06-02 00:43:45 +03:00
|
|
|
* Calculate the line height, in pixels
|
|
|
|
*
|
|
|
|
* \return Line height, in pixels
|
2006-02-13 02:07:28 +03:00
|
|
|
*/
|
2017-06-02 00:43:45 +03:00
|
|
|
static float textplain_line_height(void)
|
|
|
|
{
|
|
|
|
/* Size is in points, so convert to pixels.
|
|
|
|
* Then use a constant line height of 1.2 x font size.
|
|
|
|
*/
|
2018-05-23 13:48:35 +03:00
|
|
|
return FIXTOFLT(FDIV((FMUL(FLTTOFIX(1.2), FMUL(nscss_screen_dpi, INTTOFIX((textplain_style.size / PLOT_STYLE_SCALE))))), F_72));
|
2017-06-02 00:43:45 +03:00
|
|
|
}
|
2006-02-13 02:07:28 +03:00
|
|
|
|
2017-06-02 00:43:45 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reformat a CONTENT_TEXTPLAIN to a new width.
|
|
|
|
*/
|
|
|
|
static void textplain_reformat(struct content *c, int width, int height)
|
2006-02-13 02:07:28 +03:00
|
|
|
{
|
2011-05-07 00:40:09 +04:00
|
|
|
textplain_content *text = (textplain_content *) c;
|
|
|
|
char *utf8_data = text->utf8_data;
|
|
|
|
size_t utf8_data_size = text->utf8_data_size;
|
2006-02-13 02:07:28 +03:00
|
|
|
unsigned long line_count = 0;
|
2011-05-07 00:40:09 +04:00
|
|
|
struct textplain_line *line = text->physical_line;
|
2006-02-16 02:09:55 +03:00
|
|
|
struct textplain_line *line1;
|
2006-02-13 02:07:28 +03:00
|
|
|
size_t i, space, col;
|
|
|
|
size_t columns = 80;
|
|
|
|
int character_width;
|
2006-02-16 02:09:55 +03:00
|
|
|
size_t line_start;
|
2016-04-24 01:32:21 +03:00
|
|
|
nserror res;
|
2006-02-13 02:07:28 +03:00
|
|
|
|
Use coccinelle to change logging macro calls in c files
for F in $(git ls-files '*.c');do spatch --sp-file foo.cocci --in-place ${F};done
@@ expression E; @@
-LOG(E);
+NSLOG(netsurf, INFO, E);
@@ expression E, E1; @@
-LOG(E, E1);
+NSLOG(netsurf, INFO, E, E1);
@@ expression E, E1, E2; @@
-LOG(E, E1, E2);
+NSLOG(netsurf, INFO, E, E1, E2);
@@ expression E, E1, E2, E3; @@
-LOG(E, E1, E2, E3);
+NSLOG(netsurf, INFO, E, E1, E2, E3);
@@ expression E, E1, E2, E3, E4; @@
-LOG(E, E1, E2, E3, E4);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4);
@@ expression E, E1, E2, E3, E4, E5; @@
-LOG(E, E1, E2, E3, E4, E5);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5);
@@ expression E, E1, E2, E3, E4, E5, E6; @@
-LOG(E, E1, E2, E3, E4, E5, E6);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6);
@@ expression E, E1, E2, E3, E4, E5, E6, E7; @@
-LOG(E, E1, E2, E3, E4, E5, E6, E7);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6, E7);
2017-09-06 20:28:12 +03:00
|
|
|
NSLOG(netsurf, INFO, "content %p w:%d h:%d", c, width, height);
|
2014-05-29 14:25:52 +04:00
|
|
|
|
2006-02-13 02:07:28 +03:00
|
|
|
/* compute available columns (assuming monospaced font) - use 8
|
2016-04-24 01:32:21 +03:00
|
|
|
* characters for better accuracy
|
|
|
|
*/
|
|
|
|
res = guit->layout->width(&textplain_style,
|
|
|
|
"ABCDEFGH", 8,
|
|
|
|
&character_width);
|
|
|
|
if (res != NSERROR_OK) {
|
2006-02-13 02:07:28 +03:00
|
|
|
return;
|
2016-04-24 01:32:21 +03:00
|
|
|
}
|
|
|
|
|
2006-02-13 02:07:28 +03:00
|
|
|
columns = (width - MARGIN - MARGIN) * 8 / character_width;
|
2006-03-25 11:53:32 +03:00
|
|
|
textplain_tab_width = (TAB_WIDTH * character_width) / 8;
|
2006-02-13 02:07:28 +03:00
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
text->formatted_width = width;
|
2006-02-16 02:09:55 +03:00
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
text->physical_line_count = 0;
|
2006-02-13 02:07:28 +03:00
|
|
|
|
2006-02-16 02:09:55 +03:00
|
|
|
if (!line) {
|
2011-05-07 00:40:09 +04:00
|
|
|
text->physical_line = line =
|
2012-10-03 22:24:58 +04:00
|
|
|
malloc(sizeof(struct textplain_line) * (1024 + 3));
|
2006-02-16 02:09:55 +03:00
|
|
|
if (!line)
|
2006-02-13 02:07:28 +03:00
|
|
|
goto no_memory;
|
2004-11-22 03:33:04 +03:00
|
|
|
}
|
|
|
|
|
2006-02-16 02:09:55 +03:00
|
|
|
line[line_count++].start = line_start = 0;
|
2006-02-13 02:07:28 +03:00
|
|
|
space = 0;
|
2014-05-29 14:25:52 +04:00
|
|
|
i = 0;
|
|
|
|
col = 0;
|
|
|
|
while (i < utf8_data_size) {
|
|
|
|
size_t csize; /* number of bytes in character */
|
|
|
|
uint32_t chr;
|
|
|
|
bool term;
|
|
|
|
size_t next_col;
|
|
|
|
parserutils_error perror;
|
|
|
|
|
|
|
|
perror = parserutils_charset_utf8_to_ucs4((const uint8_t *)utf8_data + i, utf8_data_size - i, &chr, &csize);
|
|
|
|
if (perror != PARSERUTILS_OK) {
|
|
|
|
chr = 0xfffd;
|
|
|
|
}
|
|
|
|
|
|
|
|
term = (chr == '\n' || chr == '\r');
|
2006-03-25 11:53:32 +03:00
|
|
|
|
2014-05-29 14:25:52 +04:00
|
|
|
next_col = col + 1;
|
|
|
|
|
|
|
|
if (chr == '\t') {
|
2006-03-25 11:53:32 +03:00
|
|
|
next_col = (next_col + TAB_WIDTH - 1) & ~(TAB_WIDTH - 1);
|
2014-05-29 14:25:52 +04:00
|
|
|
}
|
2006-03-25 11:53:32 +03:00
|
|
|
|
|
|
|
if (term || next_col >= columns) {
|
2006-02-13 02:07:28 +03:00
|
|
|
if (line_count % 1024 == 0) {
|
2017-06-02 00:43:45 +03:00
|
|
|
line1 = realloc(line,
|
|
|
|
sizeof(struct textplain_line) *
|
2012-10-03 22:24:58 +04:00
|
|
|
(line_count + 1024 + 3));
|
2006-02-16 02:09:55 +03:00
|
|
|
if (!line1)
|
2006-02-13 02:07:28 +03:00
|
|
|
goto no_memory;
|
2011-05-07 00:40:09 +04:00
|
|
|
text->physical_line = line = line1;
|
2006-02-16 02:09:55 +03:00
|
|
|
}
|
2017-06-02 00:43:45 +03:00
|
|
|
|
2006-02-16 02:09:55 +03:00
|
|
|
if (term) {
|
|
|
|
line[line_count-1].length = i - line_start;
|
|
|
|
|
|
|
|
/* skip second char of CR/LF or LF/CR pair */
|
|
|
|
if (i + 1 < utf8_data_size &&
|
2017-06-02 00:43:45 +03:00
|
|
|
utf8_data[i+1] != utf8_data[i] &&
|
|
|
|
(utf8_data[i+1] == '\n' ||
|
|
|
|
utf8_data[i+1] == '\r')) {
|
2006-02-16 02:09:55 +03:00
|
|
|
i++;
|
2017-06-02 00:43:45 +03:00
|
|
|
}
|
|
|
|
} else {
|
2006-02-16 02:09:55 +03:00
|
|
|
if (space) {
|
|
|
|
/* break at last space in line */
|
|
|
|
i = space;
|
|
|
|
line[line_count-1].length = (i + 1) - line_start;
|
2012-08-19 14:39:00 +04:00
|
|
|
} else
|
2006-02-16 02:09:55 +03:00
|
|
|
line[line_count-1].length = i - line_start;
|
|
|
|
}
|
2017-06-02 00:43:45 +03:00
|
|
|
|
2006-02-16 02:09:55 +03:00
|
|
|
line[line_count++].start = line_start = i + 1;
|
2006-02-13 02:07:28 +03:00
|
|
|
col = 0;
|
|
|
|
space = 0;
|
|
|
|
} else {
|
|
|
|
col++;
|
2014-05-29 14:25:52 +04:00
|
|
|
if (chr == ' ')
|
2006-02-13 02:07:28 +03:00
|
|
|
space = i;
|
2004-11-22 03:33:04 +03:00
|
|
|
}
|
2014-05-29 14:25:52 +04:00
|
|
|
i += csize;
|
2004-11-22 03:33:04 +03:00
|
|
|
}
|
2006-02-16 02:09:55 +03:00
|
|
|
line[line_count-1].length = i - line[line_count-1].start;
|
|
|
|
line[line_count].start = utf8_data_size;
|
2004-11-22 03:33:04 +03:00
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
text->physical_line_count = line_count;
|
2006-02-13 02:07:28 +03:00
|
|
|
c->width = width;
|
2009-07-21 14:59:53 +04:00
|
|
|
c->height = line_count * textplain_line_height() + MARGIN + MARGIN;
|
2004-11-22 03:33:04 +03:00
|
|
|
|
2006-02-13 02:07:28 +03:00
|
|
|
return;
|
2004-11-22 03:33:04 +03:00
|
|
|
|
2006-02-13 02:07:28 +03:00
|
|
|
no_memory:
|
Use coccinelle to change logging macro calls in c files
for F in $(git ls-files '*.c');do spatch --sp-file foo.cocci --in-place ${F};done
@@ expression E; @@
-LOG(E);
+NSLOG(netsurf, INFO, E);
@@ expression E, E1; @@
-LOG(E, E1);
+NSLOG(netsurf, INFO, E, E1);
@@ expression E, E1, E2; @@
-LOG(E, E1, E2);
+NSLOG(netsurf, INFO, E, E1, E2);
@@ expression E, E1, E2, E3; @@
-LOG(E, E1, E2, E3);
+NSLOG(netsurf, INFO, E, E1, E2, E3);
@@ expression E, E1, E2, E3, E4; @@
-LOG(E, E1, E2, E3, E4);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4);
@@ expression E, E1, E2, E3, E4, E5; @@
-LOG(E, E1, E2, E3, E4, E5);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5);
@@ expression E, E1, E2, E3, E4, E5, E6; @@
-LOG(E, E1, E2, E3, E4, E5, E6);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6);
@@ expression E, E1, E2, E3, E4, E5, E6, E7; @@
-LOG(E, E1, E2, E3, E4, E5, E6, E7);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6, E7);
2017-09-06 20:28:12 +03:00
|
|
|
NSLOG(netsurf, INFO, "out of memory (line_count %lu)", line_count);
|
2006-02-13 02:07:28 +03:00
|
|
|
return;
|
2004-11-22 03:33:04 +03:00
|
|
|
}
|
2003-02-09 15:58:15 +03:00
|
|
|
|
2006-02-13 02:07:28 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy a CONTENT_TEXTPLAIN and free all resources it owns.
|
|
|
|
*/
|
|
|
|
|
2017-06-02 00:43:45 +03:00
|
|
|
static void textplain_destroy(struct content *c)
|
2006-02-13 02:07:28 +03:00
|
|
|
{
|
2011-05-07 00:40:09 +04:00
|
|
|
textplain_content *text = (textplain_content *) c;
|
2010-04-04 16:41:19 +04:00
|
|
|
|
2011-07-08 12:38:17 +04:00
|
|
|
lwc_string_unref(text->encoding);
|
2011-05-07 00:40:09 +04:00
|
|
|
|
2012-10-03 22:24:58 +04:00
|
|
|
if (text->inputstream != NULL) {
|
2011-05-07 00:40:09 +04:00
|
|
|
parserutils_inputstream_destroy(text->inputstream);
|
2012-10-03 22:24:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (text->physical_line != NULL) {
|
|
|
|
free(text->physical_line);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (text->utf8_data != NULL) {
|
|
|
|
free(text->utf8_data);
|
|
|
|
}
|
2006-02-13 02:07:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-02 00:43:45 +03:00
|
|
|
static nserror textplain_clone(const struct content *old, struct content **newc)
|
2010-04-04 16:41:19 +04:00
|
|
|
{
|
2011-05-07 00:40:09 +04:00
|
|
|
const textplain_content *old_text = (textplain_content *) old;
|
|
|
|
textplain_content *text;
|
|
|
|
nserror error;
|
2019-05-06 00:46:40 +03:00
|
|
|
const uint8_t *data;
|
|
|
|
size_t size;
|
2010-04-04 16:41:19 +04:00
|
|
|
|
2012-10-03 22:24:58 +04:00
|
|
|
text = calloc(1, sizeof(textplain_content));
|
2011-05-07 00:40:09 +04:00
|
|
|
if (text == NULL)
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
|
|
|
|
error = content__clone(old, &text->base);
|
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
content_destroy(&text->base);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2010-04-04 16:41:19 +04:00
|
|
|
/* Simply replay create/process/convert */
|
2011-05-07 00:40:09 +04:00
|
|
|
error = textplain_create_internal(text, old_text->encoding);
|
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
content_destroy(&text->base);
|
|
|
|
return error;
|
|
|
|
}
|
2010-04-04 16:41:19 +04:00
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
data = content__get_source_data(&text->base, &size);
|
2010-04-04 16:41:19 +04:00
|
|
|
if (size > 0) {
|
2019-05-06 00:46:40 +03:00
|
|
|
if (textplain_process_data(&text->base,
|
|
|
|
(const char *)data,
|
|
|
|
size) == false) {
|
2011-05-07 00:40:09 +04:00
|
|
|
content_destroy(&text->base);
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
2010-04-04 16:41:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (old->status == CONTENT_STATUS_READY ||
|
2017-06-02 00:43:45 +03:00
|
|
|
old->status == CONTENT_STATUS_DONE) {
|
2011-05-07 00:40:09 +04:00
|
|
|
if (textplain_convert(&text->base) == false) {
|
|
|
|
content_destroy(&text->base);
|
|
|
|
return NSERROR_CLONE_FAILED;
|
|
|
|
}
|
2010-04-04 16:41:19 +04:00
|
|
|
}
|
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
return NSERROR_OK;
|
2010-04-04 16:41:19 +04:00
|
|
|
}
|
|
|
|
|
2010-06-04 13:35:08 +04:00
|
|
|
|
2017-06-02 00:43:45 +03:00
|
|
|
static content_type textplain_content_type(void)
|
2010-06-04 13:35:08 +04:00
|
|
|
{
|
2017-06-02 00:43:45 +03:00
|
|
|
return CONTENT_TEXTPLAIN;
|
2010-06-04 13:35:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-20 01:38:34 +03:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* \param[in] c content of type CONTENT_TEXTPLAIN
|
|
|
|
* \param[in] x x ordinate of point
|
|
|
|
* \param[in] y y ordinate of point
|
|
|
|
* \param[in] dir direction of search if not within line
|
|
|
|
* \return byte offset of character containing (or nearest to) point
|
|
|
|
*/
|
|
|
|
static size_t
|
|
|
|
textplain_offset_from_coords(struct content *c, int x, int y, int dir)
|
|
|
|
{
|
|
|
|
textplain_content *textc = (textplain_content *) c;
|
|
|
|
float line_height = textplain_line_height();
|
|
|
|
struct textplain_line *line;
|
|
|
|
const char *text;
|
|
|
|
unsigned nlines;
|
|
|
|
size_t length;
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
assert(c != NULL);
|
|
|
|
|
|
|
|
y = (int)((float)(y - MARGIN) / line_height);
|
|
|
|
x -= MARGIN;
|
|
|
|
|
|
|
|
nlines = textc->physical_line_count;
|
|
|
|
if (!nlines)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (y <= 0) y = 0;
|
|
|
|
else if ((unsigned)y >= nlines)
|
|
|
|
y = nlines - 1;
|
|
|
|
|
|
|
|
line = &textc->physical_line[y];
|
|
|
|
text = textc->utf8_data + line->start;
|
|
|
|
length = line->length;
|
|
|
|
idx = 0;
|
|
|
|
|
|
|
|
while (x > 0) {
|
|
|
|
size_t next_offset = 0;
|
|
|
|
int width = INT_MAX;
|
|
|
|
|
|
|
|
while (next_offset < length && text[next_offset] != '\t') {
|
|
|
|
next_offset = utf8_next(text, length, next_offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (next_offset < length) {
|
|
|
|
guit->layout->width(&textplain_style,
|
|
|
|
text,
|
|
|
|
next_offset,
|
|
|
|
&width);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x <= width) {
|
|
|
|
int pixel_offset;
|
|
|
|
size_t char_offset;
|
|
|
|
|
|
|
|
guit->layout->position(&textplain_style,
|
|
|
|
text, next_offset, x,
|
|
|
|
&char_offset, &pixel_offset);
|
|
|
|
|
|
|
|
idx += char_offset;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
x -= width;
|
|
|
|
length -= next_offset;
|
|
|
|
text += next_offset;
|
|
|
|
idx += next_offset;
|
|
|
|
|
|
|
|
/* check if it's within the tab */
|
|
|
|
width = textplain_tab_width - (width % textplain_tab_width);
|
|
|
|
if (x <= width) break;
|
|
|
|
|
|
|
|
x -= width;
|
|
|
|
length--;
|
|
|
|
text++;
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return line->start + idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-04 13:35:08 +04:00
|
|
|
/**
|
|
|
|
* Handle mouse clicks and movements in a TEXTPLAIN content window.
|
|
|
|
*
|
2017-06-02 00:43:45 +03:00
|
|
|
* \param c content of type textplain
|
|
|
|
* \param bw browser window
|
|
|
|
* \param mouse mouse state on action
|
|
|
|
* \param x coordinate of mouse
|
|
|
|
* \param y coordinate of mouse
|
2010-06-04 13:35:08 +04:00
|
|
|
*/
|
2019-11-13 01:48:00 +03:00
|
|
|
static nserror
|
2017-06-02 00:43:45 +03:00
|
|
|
textplain_mouse_action(struct content *c,
|
|
|
|
struct browser_window *bw,
|
|
|
|
browser_mouse_state mouse,
|
|
|
|
int x, int y)
|
2010-06-04 13:35:08 +04:00
|
|
|
{
|
2011-07-13 17:20:26 +04:00
|
|
|
textplain_content *text = (textplain_content *) c;
|
2012-08-17 01:21:08 +04:00
|
|
|
browser_pointer_shape pointer = BROWSER_POINTER_DEFAULT;
|
2012-08-17 13:02:10 +04:00
|
|
|
union content_msg_data msg_data;
|
2010-06-04 13:35:08 +04:00
|
|
|
const char *status = 0;
|
|
|
|
size_t idx;
|
|
|
|
int dir = 0;
|
|
|
|
|
2012-01-11 02:02:19 +04:00
|
|
|
browser_window_set_drag_type(bw, DRAGGING_NONE, NULL);
|
2010-06-04 13:35:08 +04:00
|
|
|
|
2011-07-26 17:53:42 +04:00
|
|
|
idx = textplain_offset_from_coords(c, x, y, dir);
|
2020-05-21 01:17:48 +03:00
|
|
|
if (selection_click(&text->sel, text->bw, mouse, idx)) {
|
2010-06-04 13:35:08 +04:00
|
|
|
|
2011-07-13 17:20:26 +04:00
|
|
|
if (selection_dragging(&text->sel)) {
|
2012-08-19 01:45:17 +04:00
|
|
|
browser_window_set_drag_type(bw,
|
2017-06-02 00:43:45 +03:00
|
|
|
DRAGGING_SELECTION, NULL);
|
2010-06-04 13:35:08 +04:00
|
|
|
status = messages_get("Selecting");
|
|
|
|
}
|
|
|
|
|
2012-08-19 14:35:32 +04:00
|
|
|
} else {
|
2010-06-04 13:35:08 +04:00
|
|
|
if (mouse & (BROWSER_MOUSE_DRAG_1 | BROWSER_MOUSE_DRAG_2)) {
|
|
|
|
browser_window_page_drag_start(bw, x, y);
|
2012-08-17 01:21:08 +04:00
|
|
|
pointer = BROWSER_POINTER_MOVE;
|
2010-06-04 13:35:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-19 14:35:32 +04:00
|
|
|
msg_data.explicit_status_text = status;
|
2017-08-26 17:50:03 +03:00
|
|
|
content_broadcast(c, CONTENT_MSG_STATUS, &msg_data);
|
2010-06-04 13:35:08 +04:00
|
|
|
|
2012-08-17 13:02:10 +04:00
|
|
|
msg_data.pointer = pointer;
|
2017-08-26 17:50:03 +03:00
|
|
|
content_broadcast(c, CONTENT_MSG_POINTER, &msg_data);
|
2019-11-13 01:48:00 +03:00
|
|
|
|
|
|
|
return NSERROR_OK;
|
2010-06-04 13:35:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-22 16:19:35 +04:00
|
|
|
/**
|
2017-06-02 00:43:45 +03:00
|
|
|
* Handle mouse tracking (including drags) in a TEXTPLAIN content window.
|
2013-02-22 16:19:35 +04:00
|
|
|
*
|
2017-06-02 00:43:45 +03:00
|
|
|
* \param c content of type textplain
|
|
|
|
* \param bw browser window
|
|
|
|
* \param mouse state of mouse buttons and modifier keys
|
|
|
|
* \param x coordinate of mouse
|
|
|
|
* \param y coordinate of mouse
|
2013-02-22 16:19:35 +04:00
|
|
|
*/
|
2019-11-13 01:48:00 +03:00
|
|
|
static nserror
|
2017-06-02 00:43:45 +03:00
|
|
|
textplain_mouse_track(struct content *c,
|
|
|
|
struct browser_window *bw,
|
|
|
|
browser_mouse_state mouse,
|
|
|
|
int x, int y)
|
2013-02-22 16:19:35 +04:00
|
|
|
{
|
|
|
|
textplain_content *text = (textplain_content *) c;
|
|
|
|
|
2017-06-02 00:43:45 +03:00
|
|
|
if (browser_window_get_drag_type(bw) == DRAGGING_SELECTION && !mouse) {
|
|
|
|
int dir = -1;
|
|
|
|
size_t idx;
|
|
|
|
|
|
|
|
if (selection_dragging_start(&text->sel))
|
|
|
|
dir = 1;
|
|
|
|
|
|
|
|
idx = textplain_offset_from_coords(c, x, y, dir);
|
|
|
|
selection_track(&text->sel, mouse, idx);
|
|
|
|
|
|
|
|
browser_window_set_drag_type(bw, DRAGGING_NONE, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (browser_window_get_drag_type(bw)) {
|
|
|
|
|
|
|
|
case DRAGGING_SELECTION: {
|
|
|
|
int dir = -1;
|
|
|
|
size_t idx;
|
|
|
|
|
|
|
|
if (selection_dragging_start(&text->sel)) dir = 1;
|
|
|
|
|
|
|
|
idx = textplain_offset_from_coords(c, x, y, dir);
|
|
|
|
selection_track(&text->sel, mouse, idx);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
textplain_mouse_action(c, bw, mouse, x, y);
|
|
|
|
break;
|
|
|
|
}
|
2019-11-13 01:48:00 +03:00
|
|
|
|
|
|
|
return NSERROR_OK;
|
2017-06-02 00:43:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle keypresses.
|
|
|
|
*
|
|
|
|
* \param c content of type CONTENT_TEXTPLAIN
|
|
|
|
* \param key The UCS4 character codepoint
|
|
|
|
* \return true if key handled, false otherwise
|
|
|
|
*/
|
|
|
|
static bool textplain_keypress(struct content *c, uint32_t key)
|
|
|
|
{
|
|
|
|
textplain_content *text = (textplain_content *) c;
|
|
|
|
struct selection *sel = &text->sel;
|
|
|
|
|
|
|
|
switch (key) {
|
|
|
|
case NS_KEY_COPY_SELECTION:
|
|
|
|
selection_copy_to_clipboard(sel);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case NS_KEY_CLEAR_SELECTION:
|
|
|
|
selection_clear(sel, true);
|
|
|
|
return true;
|
2013-02-22 16:19:35 +04:00
|
|
|
|
2015-03-27 12:36:19 +03:00
|
|
|
case NS_KEY_SELECT_ALL:
|
2013-02-22 16:19:35 +04:00
|
|
|
selection_select_all(sel);
|
|
|
|
return true;
|
|
|
|
|
2015-03-27 12:36:19 +03:00
|
|
|
case NS_KEY_ESCAPE:
|
2013-02-22 16:19:35 +04:00
|
|
|
if (selection_defined(sel)) {
|
|
|
|
selection_clear(sel, true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if there's no selection, leave Escape for the caller */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-07 17:41:40 +04:00
|
|
|
|
|
|
|
|
2018-05-10 13:34:26 +03:00
|
|
|
/**
|
|
|
|
* Redraw a text string with highlighting
|
|
|
|
* (for selection/search)
|
|
|
|
*
|
|
|
|
* \param utf8_text pointer to UTF-8 text string
|
|
|
|
* \param utf8_len length of string, in bytes
|
|
|
|
* \param offset byte offset within textual representation
|
|
|
|
* \param x x ordinate at which to plot text
|
|
|
|
* \param y y ordinate at which to plot text
|
|
|
|
* \param clip pointer to current clip rectangle
|
|
|
|
* \param height height of text string
|
|
|
|
* \param scale current display scale (1.0 = 100%)
|
|
|
|
* \param text Content being redrawn.
|
|
|
|
* \param sel Selection context
|
|
|
|
* \param search Search context
|
|
|
|
* \param ctx current redraw context
|
|
|
|
* \return true iff successful and redraw should proceed
|
|
|
|
*/
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
bool highlighted = false;
|
|
|
|
plot_font_style_t plot_fstyle;
|
|
|
|
nserror res;
|
|
|
|
|
|
|
|
/* Need scaled text size to pass to plotters */
|
|
|
|
plot_fstyle = textplain_style;
|
|
|
|
plot_fstyle.size *= scale;
|
|
|
|
|
|
|
|
/* is this box part of a selection? */
|
|
|
|
if (ctx->interactive == true) {
|
|
|
|
unsigned len = utf8_len;
|
|
|
|
unsigned start_idx;
|
|
|
|
unsigned end_idx;
|
|
|
|
|
|
|
|
/* first try the browser window's current selection */
|
|
|
|
if (selection_defined(sel) &&
|
|
|
|
selection_highlighted(sel,
|
|
|
|
offset,
|
|
|
|
offset + len,
|
|
|
|
&start_idx,
|
|
|
|
&end_idx)) {
|
|
|
|
highlighted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* what about the current search operation, if any? */
|
|
|
|
if (!highlighted &&
|
2020-05-12 23:09:41 +03:00
|
|
|
(text->base.textsearch.context != NULL) &&
|
|
|
|
content_textsearch_ishighlighted(text->base.textsearch.context,
|
2020-05-11 01:22:29 +03:00
|
|
|
offset,
|
|
|
|
offset + len,
|
|
|
|
&start_idx,
|
|
|
|
&end_idx)) {
|
2018-05-10 13:34:26 +03:00
|
|
|
highlighted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* \todo make search terms visible within selected text */
|
|
|
|
if (highlighted) {
|
|
|
|
struct rect r;
|
|
|
|
unsigned endtxt_idx = end_idx;
|
|
|
|
bool clip_changed = false;
|
|
|
|
bool text_visible = true;
|
|
|
|
int startx, endx;
|
|
|
|
plot_style_t pstyle_fill_hback = *plot_style_fill_white;
|
|
|
|
plot_font_style_t fstyle_hback = plot_fstyle;
|
|
|
|
|
|
|
|
if (end_idx > utf8_len) {
|
|
|
|
/* adjust for trailing space, not present in
|
|
|
|
* utf8_text
|
|
|
|
*/
|
|
|
|
assert(end_idx == utf8_len + 1);
|
|
|
|
endtxt_idx = utf8_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = guit->layout->width(&textplain_style,
|
|
|
|
utf8_text,
|
|
|
|
start_idx,
|
|
|
|
&startx);
|
|
|
|
if (res != NSERROR_OK) {
|
|
|
|
startx = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = guit->layout->width(&textplain_style,
|
|
|
|
utf8_text,
|
|
|
|
endtxt_idx,
|
|
|
|
&endx);
|
|
|
|
if (res != NSERROR_OK) {
|
|
|
|
endx = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scale != 1.0) {
|
|
|
|
startx *= scale;
|
|
|
|
endx *= scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* draw any text preceding highlighted portion */
|
|
|
|
if (start_idx > 0) {
|
|
|
|
res = ctx->plot->text(ctx,
|
|
|
|
&plot_fstyle,
|
|
|
|
x,
|
|
|
|
y + (int)(height * 0.75 * scale),
|
|
|
|
utf8_text,
|
|
|
|
start_idx);
|
|
|
|
if (res != NSERROR_OK) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pstyle_fill_hback.fill_colour = textplain_style.foreground;
|
|
|
|
|
|
|
|
/* highlighted portion */
|
|
|
|
r.x0 = x + startx;
|
|
|
|
r.y0 = y;
|
|
|
|
r.x1 = x + endx;
|
|
|
|
r.y1 = y + height * scale;
|
|
|
|
res = ctx->plot->rectangle(ctx, &pstyle_fill_hback, &r);
|
|
|
|
if (res != NSERROR_OK) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (start_idx > 0) {
|
|
|
|
int px0 = max(x + startx, clip->x0);
|
|
|
|
int px1 = min(x + endx, clip->x1);
|
|
|
|
|
|
|
|
if (px0 < px1) {
|
|
|
|
r.x0 = px0;
|
|
|
|
r.y0 = clip->y0;
|
|
|
|
r.x1 = px1;
|
|
|
|
r.y1 = clip->y1;
|
|
|
|
res = ctx->plot->clip(ctx, &r);
|
|
|
|
if (res != NSERROR_OK) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
clip_changed = true;
|
|
|
|
} else {
|
|
|
|
text_visible = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fstyle_hback.background =
|
|
|
|
pstyle_fill_hback.fill_colour;
|
|
|
|
fstyle_hback.foreground = colour_to_bw_furthest(
|
|
|
|
pstyle_fill_hback.fill_colour);
|
|
|
|
|
|
|
|
if (text_visible &&
|
|
|
|
(ctx->plot->text(ctx,
|
|
|
|
&fstyle_hback,
|
|
|
|
x,
|
|
|
|
y + (int)(height * 0.75 * scale),
|
|
|
|
utf8_text,
|
|
|
|
endtxt_idx) != NSERROR_OK)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* draw any text succeeding highlighted portion */
|
|
|
|
if (endtxt_idx < utf8_len) {
|
|
|
|
int px0 = max(x + endx, clip->x0);
|
|
|
|
if (px0 < clip->x1) {
|
|
|
|
|
|
|
|
r.x0 = px0;
|
|
|
|
r.y0 = clip->y0;
|
|
|
|
r.x1 = clip->x1;
|
|
|
|
r.y1 = clip->y1;
|
|
|
|
res = ctx->plot->clip(ctx, &r);
|
|
|
|
if (res != NSERROR_OK) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
clip_changed = true;
|
|
|
|
|
|
|
|
res = ctx->plot->text(ctx,
|
|
|
|
&plot_fstyle,
|
|
|
|
x,
|
|
|
|
y + (int)(height * 0.75 * scale),
|
|
|
|
utf8_text,
|
|
|
|
utf8_len);
|
|
|
|
if (res != NSERROR_OK) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clip_changed &&
|
|
|
|
(ctx->plot->clip(ctx, clip) != NSERROR_OK)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!highlighted) {
|
|
|
|
res = ctx->plot->text(ctx,
|
|
|
|
&plot_fstyle,
|
|
|
|
x,
|
|
|
|
y + (int) (height * 0.75 * scale),
|
|
|
|
utf8_text,
|
|
|
|
utf8_len);
|
|
|
|
if (res != NSERROR_OK) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-13 02:07:28 +03:00
|
|
|
/**
|
|
|
|
* Draw a CONTENT_TEXTPLAIN using the current set of plotters (plot).
|
|
|
|
*
|
2017-06-02 00:43:45 +03:00
|
|
|
* x, y, clip_[xy][01] are in target coordinates.
|
|
|
|
*
|
2011-06-29 00:17:39 +04:00
|
|
|
* \param c content of type CONTENT_TEXTPLAIN
|
|
|
|
* \param data redraw data for this content redraw
|
|
|
|
* \param clip current clip region
|
2011-06-30 19:48:07 +04:00
|
|
|
* \param ctx current redraw context
|
2006-02-13 02:07:28 +03:00
|
|
|
* \return true if successful, false otherwise
|
|
|
|
*/
|
2017-06-02 00:43:45 +03:00
|
|
|
static bool
|
|
|
|
textplain_redraw(struct content *c,
|
|
|
|
struct content_redraw_data *data,
|
|
|
|
const struct rect *clip,
|
|
|
|
const struct redraw_context *ctx)
|
2003-02-09 15:58:15 +03:00
|
|
|
{
|
2011-05-07 00:40:09 +04:00
|
|
|
textplain_content *text = (textplain_content *) c;
|
2011-06-30 22:27:24 +04:00
|
|
|
struct browser_window *bw = text->bw;
|
2011-05-07 00:40:09 +04:00
|
|
|
char *utf8_data = text->utf8_data;
|
2006-02-16 02:09:55 +03:00
|
|
|
long lineno;
|
2011-06-29 00:17:39 +04:00
|
|
|
int x = data->x;
|
|
|
|
int y = data->y;
|
2011-05-07 00:40:09 +04:00
|
|
|
unsigned long line_count = text->physical_line_count;
|
2009-07-21 14:59:53 +04:00
|
|
|
float line_height = textplain_line_height();
|
2011-06-29 00:17:39 +04:00
|
|
|
float scaled_line_height = line_height * data->scale;
|
|
|
|
long line0 = (clip->y0 - y * data->scale) / scaled_line_height - 1;
|
|
|
|
long line1 = (clip->y1 - y * data->scale) / scaled_line_height + 1;
|
2011-05-07 00:40:09 +04:00
|
|
|
struct textplain_line *line = text->physical_line;
|
2006-03-26 08:48:45 +04:00
|
|
|
size_t length;
|
2009-07-06 14:38:13 +04:00
|
|
|
plot_style_t *plot_style_highlight;
|
2017-02-11 16:54:08 +03:00
|
|
|
nserror res;
|
2006-02-16 02:09:55 +03:00
|
|
|
|
2006-02-13 02:07:28 +03:00
|
|
|
if (line0 < 0)
|
|
|
|
line0 = 0;
|
|
|
|
if (line1 < 0)
|
|
|
|
line1 = 0;
|
|
|
|
if (line_count < (unsigned long) line0)
|
|
|
|
line0 = line_count;
|
|
|
|
if (line_count < (unsigned long) line1)
|
|
|
|
line1 = line_count;
|
|
|
|
if (line1 < line0)
|
|
|
|
line1 = line0;
|
|
|
|
|
2017-02-11 16:54:08 +03:00
|
|
|
res = ctx->plot->rectangle(ctx, plot_style_fill_white, clip);
|
|
|
|
if (res != NSERROR_OK) {
|
2006-02-13 02:07:28 +03:00
|
|
|
return false;
|
2017-02-11 16:54:08 +03:00
|
|
|
}
|
2006-02-13 02:07:28 +03:00
|
|
|
|
2006-02-16 02:09:55 +03:00
|
|
|
if (!line)
|
2006-02-13 02:07:28 +03:00
|
|
|
return true;
|
|
|
|
|
2006-03-26 08:48:45 +04:00
|
|
|
/* choose a suitable background colour for any highlighted text */
|
2011-06-29 00:17:39 +04:00
|
|
|
if ((data->background_colour & 0x808080) == 0x808080)
|
2009-07-06 14:38:13 +04:00
|
|
|
plot_style_highlight = plot_style_fill_black;
|
|
|
|
else
|
|
|
|
plot_style_highlight = plot_style_fill_white;
|
2006-03-26 08:48:45 +04:00
|
|
|
|
2011-03-15 18:10:10 +03:00
|
|
|
/* Set up font plot style */
|
2011-06-29 00:17:39 +04:00
|
|
|
textplain_style.background = data->background_colour;
|
2009-07-21 14:59:53 +04:00
|
|
|
|
2011-06-29 00:17:39 +04:00
|
|
|
x = (x + MARGIN) * data->scale;
|
|
|
|
y = (y + MARGIN) * data->scale;
|
2006-02-16 02:09:55 +03:00
|
|
|
for (lineno = line0; lineno != line1; lineno++) {
|
2011-07-13 17:20:26 +04:00
|
|
|
const char *text_d = utf8_data + line[lineno].start;
|
2011-06-29 00:17:39 +04:00
|
|
|
int tab_width = textplain_tab_width * data->scale;
|
2006-03-25 11:53:32 +03:00
|
|
|
size_t offset = 0;
|
|
|
|
int tx = x;
|
|
|
|
|
2006-03-26 08:48:45 +04:00
|
|
|
if (!tab_width) tab_width = 1;
|
|
|
|
|
2006-02-16 02:09:55 +03:00
|
|
|
length = line[lineno].length;
|
2006-02-13 02:07:28 +03:00
|
|
|
if (!length)
|
|
|
|
continue;
|
2006-03-25 11:53:32 +03:00
|
|
|
|
|
|
|
while (offset < length) {
|
|
|
|
size_t next_offset = offset;
|
|
|
|
int width;
|
2006-03-26 08:48:45 +04:00
|
|
|
int ntx;
|
2016-04-28 12:50:13 +03:00
|
|
|
nserror res;
|
2006-03-25 11:53:32 +03:00
|
|
|
|
2018-05-10 13:34:26 +03:00
|
|
|
while ((next_offset < length) &&
|
|
|
|
(text_d[next_offset] != '\t')) {
|
|
|
|
next_offset = utf8_next(text_d,
|
|
|
|
length,
|
|
|
|
next_offset);
|
|
|
|
}
|
2006-03-25 11:53:32 +03:00
|
|
|
|
2018-05-10 13:34:26 +03:00
|
|
|
if (!text_draw(text_d + offset,
|
|
|
|
next_offset - offset,
|
|
|
|
line[lineno].start + offset,
|
|
|
|
tx,
|
|
|
|
y + (lineno * scaled_line_height),
|
|
|
|
clip,
|
|
|
|
line_height,
|
|
|
|
data->scale,
|
|
|
|
text,
|
|
|
|
&text->sel,
|
|
|
|
ctx)) {
|
2006-03-25 11:53:32 +03:00
|
|
|
return false;
|
2018-05-10 13:34:26 +03:00
|
|
|
}
|
2006-03-25 11:53:32 +03:00
|
|
|
|
|
|
|
if (next_offset >= length)
|
|
|
|
break;
|
|
|
|
|
2016-04-28 12:50:13 +03:00
|
|
|
res = guit->layout->width(&textplain_style,
|
2017-06-02 00:43:45 +03:00
|
|
|
&text_d[offset],
|
|
|
|
next_offset - offset,
|
2016-04-28 12:50:13 +03:00
|
|
|
&width);
|
|
|
|
/* locate end of string and align to next tab position */
|
|
|
|
if (res == NSERROR_OK) {
|
2011-06-29 00:17:39 +04:00
|
|
|
tx += (int)(width * data->scale);
|
2016-04-24 01:32:21 +03:00
|
|
|
}
|
2006-03-26 08:48:45 +04:00
|
|
|
|
|
|
|
ntx = x + ((1 + (tx - x) / tab_width) * tab_width);
|
2006-03-25 11:53:32 +03:00
|
|
|
|
2017-06-02 00:43:45 +03:00
|
|
|
/* if the tab character lies within the
|
|
|
|
* selection, if any, then we must draw it as
|
|
|
|
* a filled rectangle so that it's consistent
|
|
|
|
* with background of the selected text
|
|
|
|
*/
|
2006-03-26 08:48:45 +04:00
|
|
|
|
|
|
|
if (bw) {
|
|
|
|
unsigned tab_ofst = line[lineno].start + next_offset;
|
2011-07-13 17:20:26 +04:00
|
|
|
struct selection *sel = &text->sel;
|
2006-03-26 08:48:45 +04:00
|
|
|
bool highlighted = false;
|
|
|
|
|
|
|
|
if (selection_defined(sel)) {
|
|
|
|
unsigned start_idx, end_idx;
|
|
|
|
if (selection_highlighted(sel,
|
2017-06-02 00:43:45 +03:00
|
|
|
tab_ofst,
|
|
|
|
tab_ofst + 1,
|
|
|
|
&start_idx,
|
|
|
|
&end_idx))
|
2006-03-26 08:48:45 +04:00
|
|
|
highlighted = true;
|
|
|
|
}
|
|
|
|
|
2020-05-11 01:22:29 +03:00
|
|
|
if (!highlighted &&
|
2020-05-12 23:09:41 +03:00
|
|
|
(c->textsearch.context != NULL)) {
|
2006-03-26 08:48:45 +04:00
|
|
|
unsigned start_idx, end_idx;
|
2020-05-11 01:22:29 +03:00
|
|
|
if (content_textsearch_ishighlighted(
|
2020-05-12 23:09:41 +03:00
|
|
|
c->textsearch.context,
|
2020-05-11 01:22:29 +03:00
|
|
|
tab_ofst,
|
|
|
|
tab_ofst + 1,
|
|
|
|
&start_idx,
|
|
|
|
&end_idx)) {
|
2006-03-26 08:48:45 +04:00
|
|
|
highlighted = true;
|
2020-05-11 01:22:29 +03:00
|
|
|
}
|
2006-03-26 08:48:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (highlighted) {
|
2017-02-11 16:54:08 +03:00
|
|
|
struct rect rect;
|
|
|
|
rect.x0 = tx;
|
|
|
|
rect.y0 = y + (lineno * scaled_line_height);
|
|
|
|
rect.x1 = ntx;
|
|
|
|
rect.y1 = rect.y0 + scaled_line_height;
|
|
|
|
res = ctx->plot->rectangle(ctx,
|
|
|
|
plot_style_highlight,
|
|
|
|
&rect);
|
|
|
|
if (res != NSERROR_OK) {
|
2006-03-26 08:48:45 +04:00
|
|
|
return false;
|
2017-02-11 16:54:08 +03:00
|
|
|
}
|
2006-03-26 08:48:45 +04:00
|
|
|
}
|
|
|
|
}
|
2006-03-25 11:53:32 +03:00
|
|
|
|
|
|
|
offset = next_offset + 1;
|
2006-03-26 08:48:45 +04:00
|
|
|
tx = ntx;
|
2006-03-25 11:53:32 +03:00
|
|
|
}
|
2006-02-13 02:07:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2003-02-09 15:58:15 +03:00
|
|
|
}
|
2006-02-16 02:09:55 +03:00
|
|
|
|
2011-06-30 22:27:24 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a window containing a CONTENT_TEXTPLAIN being opened.
|
|
|
|
*/
|
2019-07-10 18:42:52 +03:00
|
|
|
static nserror
|
2017-06-02 00:43:45 +03:00
|
|
|
textplain_open(struct content *c,
|
|
|
|
struct browser_window *bw,
|
|
|
|
struct content *page,
|
|
|
|
struct object_params *params)
|
2011-06-30 22:27:24 +04:00
|
|
|
{
|
2011-07-01 15:16:43 +04:00
|
|
|
textplain_content *text = (textplain_content *) c;
|
|
|
|
|
|
|
|
text->bw = bw;
|
2011-07-02 16:34:35 +04:00
|
|
|
|
2011-07-13 17:20:26 +04:00
|
|
|
/* text selection */
|
2018-01-04 02:58:18 +03:00
|
|
|
selection_init(&text->sel, NULL, NULL);
|
2019-07-10 18:42:52 +03:00
|
|
|
|
|
|
|
return NSERROR_OK;
|
2011-07-01 15:16:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a window containing a CONTENT_TEXTPLAIN being closed.
|
|
|
|
*/
|
2019-07-10 18:42:52 +03:00
|
|
|
static nserror textplain_close(struct content *c)
|
2011-07-01 15:16:43 +04:00
|
|
|
{
|
|
|
|
textplain_content *text = (textplain_content *) c;
|
|
|
|
|
|
|
|
text->bw = NULL;
|
2019-07-10 18:42:52 +03:00
|
|
|
|
|
|
|
return NSERROR_OK;
|
2011-06-30 22:27:24 +04:00
|
|
|
}
|
|
|
|
|
2011-07-13 17:20:26 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an textplain content's selection context
|
|
|
|
*/
|
2017-06-02 00:43:45 +03:00
|
|
|
static char *textplain_get_selection(struct content *c)
|
2011-07-13 17:20:26 +04:00
|
|
|
{
|
|
|
|
textplain_content *text = (textplain_content *) c;
|
|
|
|
|
2013-02-22 16:19:35 +04:00
|
|
|
return selection_get_copy(&text->sel);
|
2011-07-13 17:20:26 +04:00
|
|
|
}
|
|
|
|
|
2017-06-02 00:43:45 +03:00
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/**
|
2017-06-02 00:43:45 +03:00
|
|
|
* Convert a character offset within a line of text into the
|
|
|
|
* horizontal co-ordinate
|
2010-03-28 16:56:39 +04:00
|
|
|
*
|
2017-06-02 00:43:45 +03:00
|
|
|
* The conversion takes into account the font being used and any tabs
|
|
|
|
* in the text
|
|
|
|
*
|
|
|
|
* \param text line of text
|
|
|
|
* \param offset char offset within text
|
|
|
|
* \param length line length
|
|
|
|
* \return x ordinate
|
2010-03-28 16:56:39 +04:00
|
|
|
*/
|
2017-06-02 00:43:45 +03:00
|
|
|
static int
|
|
|
|
textplain_coord_from_offset(const char *text, size_t offset, size_t length)
|
|
|
|
{
|
|
|
|
int x = 0;
|
|
|
|
|
|
|
|
while (offset > 0) {
|
|
|
|
size_t next_offset = 0;
|
|
|
|
int tx;
|
|
|
|
|
|
|
|
while (next_offset < offset && text[next_offset] != '\t') {
|
|
|
|
next_offset = utf8_next(text, length, next_offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
guit->layout->width(&textplain_style, text, next_offset, &tx);
|
|
|
|
|
|
|
|
x += tx;
|
|
|
|
|
|
|
|
if (next_offset >= offset)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* align to next tab boundary */
|
|
|
|
next_offset++;
|
|
|
|
x = (1 + (x / textplain_tab_width)) * textplain_tab_width;
|
|
|
|
offset -= next_offset;
|
|
|
|
text += next_offset;
|
|
|
|
length -= next_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2020-05-20 01:38:34 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve number of lines in content
|
|
|
|
*
|
|
|
|
* \param[in] c Content to retrieve line count from
|
|
|
|
* \return Number of lines
|
|
|
|
*/
|
|
|
|
static unsigned long textplain_line_count(struct content *c)
|
|
|
|
{
|
|
|
|
textplain_content *text = (textplain_content *) c;
|
|
|
|
|
|
|
|
assert(c != NULL);
|
|
|
|
|
|
|
|
return text->physical_line_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a pointer to the requested line of text.
|
|
|
|
*
|
|
|
|
* \param[in] c content of type CONTENT_TEXTPLAIN
|
|
|
|
* \param[in] lineno line number
|
|
|
|
* \param[out] poffset receives byte offset of line start within text
|
|
|
|
* \param[out] plen receives length of returned line
|
|
|
|
* \return pointer to text, or NULL if invalid line number
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
textplain_get_line(struct content *c,
|
|
|
|
unsigned lineno,
|
|
|
|
size_t *poffset,
|
|
|
|
size_t *plen)
|
|
|
|
{
|
|
|
|
textplain_content *text = (textplain_content *) c;
|
|
|
|
struct textplain_line *line;
|
|
|
|
|
|
|
|
assert(c != NULL);
|
|
|
|
|
|
|
|
if (lineno >= text->physical_line_count)
|
|
|
|
return NULL;
|
|
|
|
line = &text->physical_line[lineno];
|
|
|
|
|
|
|
|
*poffset = line->start;
|
|
|
|
*plen = line->length;
|
|
|
|
return text->utf8_data + line->start;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find line number of byte in text
|
|
|
|
*
|
|
|
|
* Given a byte offset within the text, return the line number
|
|
|
|
* of the line containing that offset.
|
|
|
|
*
|
|
|
|
* \param[in] c content of type CONTENT_TEXTPLAIN
|
|
|
|
* \param[in] offset byte offset within textual representation
|
|
|
|
* \return line number, or -1 if offset invalid (larger than size)
|
|
|
|
*/
|
|
|
|
static int textplain_find_line(struct content *c, unsigned offset)
|
|
|
|
{
|
|
|
|
textplain_content *text = (textplain_content *) c;
|
|
|
|
struct textplain_line *line;
|
|
|
|
int nlines;
|
|
|
|
int lineno = 0;
|
|
|
|
|
|
|
|
assert(c != NULL);
|
|
|
|
|
|
|
|
line = text->physical_line;
|
|
|
|
nlines = text->physical_line_count;
|
|
|
|
|
|
|
|
if (offset > text->utf8_data_size) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* \todo - implement binary search here */
|
|
|
|
while (lineno < nlines && line[lineno].start < offset) {
|
|
|
|
lineno++;
|
|
|
|
}
|
|
|
|
if (line[lineno].start > offset) {
|
|
|
|
lineno--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lineno;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-13 01:22:05 +03:00
|
|
|
/**
|
|
|
|
* Finds all occurrences of a given string in a textplain content
|
|
|
|
*
|
|
|
|
* \param c the content to be searched
|
|
|
|
* \param context The search context to add the entry to.
|
|
|
|
* \param pattern the string pattern to search for
|
|
|
|
* \param p_len pattern length
|
|
|
|
* \param case_sens whether to perform a case sensitive search
|
|
|
|
* \return NSERROR_OK on success else error code on faliure
|
|
|
|
*/
|
|
|
|
static nserror
|
|
|
|
textplain_textsearch_find(struct content *c,
|
|
|
|
struct textsearch_context *context,
|
|
|
|
const char *pattern,
|
|
|
|
int p_len,
|
|
|
|
bool case_sens)
|
|
|
|
{
|
|
|
|
int nlines = textplain_line_count(c);
|
|
|
|
int line;
|
|
|
|
nserror res = NSERROR_OK;
|
|
|
|
|
|
|
|
for(line = 0; line < nlines; line++) {
|
|
|
|
size_t offset, length;
|
|
|
|
const char *text;
|
|
|
|
|
|
|
|
text = textplain_get_line(c, line, &offset, &length);
|
|
|
|
if (text) {
|
|
|
|
while (length > 0) {
|
|
|
|
unsigned match_length;
|
|
|
|
size_t start_idx;
|
|
|
|
const char *new_text;
|
|
|
|
const char *pos;
|
|
|
|
|
|
|
|
pos = content_textsearch_find_pattern(
|
|
|
|
text,
|
|
|
|
length,
|
|
|
|
pattern,
|
|
|
|
p_len,
|
|
|
|
case_sens,
|
|
|
|
&match_length);
|
|
|
|
if (!pos)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* found string in line => add to list */
|
|
|
|
start_idx = offset + (pos - text);
|
|
|
|
res = content_textsearch_add_match(context,
|
|
|
|
start_idx,
|
|
|
|
start_idx + match_length,
|
|
|
|
NULL,
|
|
|
|
NULL);
|
|
|
|
if (res != NSERROR_OK) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_text = pos + match_length;
|
|
|
|
offset += (new_text - text);
|
|
|
|
length -= (new_text - text);
|
|
|
|
text = new_text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-23 20:39:25 +03:00
|
|
|
/**
|
|
|
|
* Given a range of byte offsets within a UTF8 textplain content,
|
|
|
|
* return a box that fully encloses the text
|
|
|
|
*
|
|
|
|
* \param[in] c content of type CONTENT_TEXTPLAIN
|
|
|
|
* \param[in] start byte offset of start of text range
|
|
|
|
* \param[in] end byte offset of end
|
|
|
|
* \param[out] r rectangle to be completed
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
textplain_coords_from_range(struct content *c,
|
|
|
|
unsigned start,
|
|
|
|
unsigned end,
|
|
|
|
struct rect *r)
|
|
|
|
{
|
|
|
|
textplain_content *text = (textplain_content *) c;
|
|
|
|
float line_height = textplain_line_height();
|
|
|
|
char *utf8_data;
|
|
|
|
struct textplain_line *line;
|
|
|
|
unsigned lineno = 0;
|
|
|
|
unsigned nlines;
|
|
|
|
|
|
|
|
assert(c != NULL);
|
|
|
|
assert(start <= end);
|
|
|
|
assert(end <= text->utf8_data_size);
|
|
|
|
|
|
|
|
utf8_data = text->utf8_data;
|
|
|
|
nlines = text->physical_line_count;
|
|
|
|
line = text->physical_line;
|
|
|
|
|
|
|
|
/* find start */
|
|
|
|
lineno = textplain_find_line(c, start);
|
|
|
|
|
|
|
|
r->y0 = (int)(MARGIN + lineno * line_height);
|
|
|
|
|
|
|
|
if (lineno + 1 <= nlines || line[lineno + 1].start >= end) {
|
|
|
|
/* \todo - it may actually be more efficient just to
|
|
|
|
* run forwards most of the time
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* find end */
|
|
|
|
lineno = textplain_find_line(c, end);
|
|
|
|
|
|
|
|
r->x0 = 0;
|
|
|
|
r->x1 = text->formatted_width;
|
|
|
|
} else {
|
|
|
|
/* single line */
|
|
|
|
const char *text = utf8_data + line[lineno].start;
|
|
|
|
|
|
|
|
r->x0 = textplain_coord_from_offset(text,
|
|
|
|
start - line[lineno].start,
|
|
|
|
line[lineno].length);
|
|
|
|
|
|
|
|
r->x1 = textplain_coord_from_offset(text,
|
|
|
|
end - line[lineno].start,
|
|
|
|
line[lineno].length);
|
|
|
|
}
|
|
|
|
|
|
|
|
r->y1 = (int)(MARGIN + (lineno + 1) * line_height);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-23 22:38:41 +03:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* \param[in] c content of type CONTENT_TEXTPLAIN
|
|
|
|
* \param[in] start starting byte offset within UTF-8 text
|
|
|
|
* \param[in] end ending byte offset
|
|
|
|
* \param[out] plen receives validated length
|
|
|
|
* \return pointer to text, or NULL if no text
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
textplain_get_raw_data(struct content *c,
|
|
|
|
unsigned start,
|
|
|
|
unsigned end,
|
|
|
|
size_t *plen)
|
|
|
|
{
|
|
|
|
textplain_content *text = (textplain_content *) c;
|
|
|
|
size_t utf8_size;
|
|
|
|
|
|
|
|
assert(c != NULL);
|
|
|
|
|
|
|
|
utf8_size = text->utf8_data_size;
|
|
|
|
|
|
|
|
/* any text at all? */
|
|
|
|
if (!utf8_size) return NULL;
|
|
|
|
|
|
|
|
/* clamp to valid offset range */
|
|
|
|
if (start >= utf8_size) start = utf8_size;
|
|
|
|
if (end >= utf8_size) end = utf8_size;
|
|
|
|
|
|
|
|
*plen = end - start;
|
|
|
|
|
|
|
|
return text->utf8_data + start;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-13 01:22:05 +03:00
|
|
|
/**
|
|
|
|
* get bounds of a free text search match
|
|
|
|
*/
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
textplain_coords_from_range(c, start_idx, end_idx, bounds);
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create a selection object suitable for this content
|
|
|
|
*/
|
|
|
|
static nserror
|
|
|
|
textplain_create_selection(struct content *c, struct selection **sel_out)
|
|
|
|
{
|
|
|
|
struct selection *sel;
|
|
|
|
sel = selection_create(c, false);
|
|
|
|
if (sel == NULL) {
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
selection_init(sel, NULL, NULL);
|
|
|
|
|
|
|
|
*sel_out = sel;
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
2017-06-02 00:43:45 +03:00
|
|
|
|
2020-05-23 20:39:25 +03:00
|
|
|
/**
|
|
|
|
* invalidate a region based on offsets into the text cauing a redraw
|
|
|
|
*/
|
|
|
|
static nserror
|
|
|
|
textplain_textselection_redraw(struct content *c,
|
|
|
|
unsigned start_idx,
|
|
|
|
unsigned end_idx)
|
|
|
|
{
|
|
|
|
struct rect r;
|
|
|
|
|
|
|
|
if (end_idx <= start_idx) {
|
|
|
|
return NSERROR_BAD_PARAMETER;
|
|
|
|
}
|
|
|
|
|
|
|
|
textplain_coords_from_range(c, start_idx, end_idx, &r);
|
|
|
|
|
|
|
|
content__request_redraw(c, r.x0, r.y0, r.x1 - r.x0, r.y1 - r.y0);
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
2020-05-23 22:38:41 +03:00
|
|
|
static nserror
|
|
|
|
textplain_textselection_copy(struct content *c,
|
|
|
|
unsigned start_idx,
|
|
|
|
unsigned end_idx,
|
|
|
|
struct selection_string *selstr)
|
|
|
|
{
|
|
|
|
const char *text;
|
|
|
|
size_t length;
|
|
|
|
bool res;
|
|
|
|
|
|
|
|
text = textplain_get_raw_data(c, start_idx, end_idx, &length);
|
|
|
|
res = selection_string_append(text, length, false, NULL, selstr);
|
|
|
|
if (res == false) {
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
2020-05-23 20:39:25 +03:00
|
|
|
|
2017-06-02 00:43:45 +03:00
|
|
|
/**
|
|
|
|
* plain text content handler table
|
|
|
|
*/
|
|
|
|
static const content_handler textplain_content_handler = {
|
|
|
|
.fini = textplain_fini,
|
|
|
|
.create = textplain_create,
|
|
|
|
.process_data = textplain_process_data,
|
|
|
|
.data_complete = textplain_convert,
|
|
|
|
.reformat = textplain_reformat,
|
|
|
|
.destroy = textplain_destroy,
|
|
|
|
.mouse_track = textplain_mouse_track,
|
|
|
|
.mouse_action = textplain_mouse_action,
|
|
|
|
.keypress = textplain_keypress,
|
|
|
|
.redraw = textplain_redraw,
|
|
|
|
.open = textplain_open,
|
|
|
|
.close = textplain_close,
|
|
|
|
.get_selection = textplain_get_selection,
|
|
|
|
.clone = textplain_clone,
|
|
|
|
.type = textplain_content_type,
|
2020-05-13 01:22:05 +03:00
|
|
|
.textsearch_find = textplain_textsearch_find,
|
|
|
|
.textsearch_bounds = textplain_textsearch_bounds,
|
2020-05-23 20:39:25 +03:00
|
|
|
.textselection_redraw = textplain_textselection_redraw,
|
2020-05-23 22:38:41 +03:00
|
|
|
.textselection_copy = textplain_textselection_copy,
|
2020-05-13 01:22:05 +03:00
|
|
|
.create_selection = textplain_create_selection,
|
2017-06-02 00:43:45 +03:00
|
|
|
.no_share = true,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-05-10 13:34:26 +03:00
|
|
|
/* exported interface documented in html/textplain.h */
|
2017-06-02 00:43:45 +03:00
|
|
|
nserror textplain_init(void)
|
|
|
|
{
|
|
|
|
lwc_error lerror;
|
|
|
|
nserror error;
|
|
|
|
|
|
|
|
lerror = lwc_intern_string("Windows-1252",
|
|
|
|
SLEN("Windows-1252"),
|
|
|
|
&textplain_default_charset);
|
|
|
|
if (lerror != lwc_error_ok) {
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = content_factory_register_handler("text/plain",
|
|
|
|
&textplain_content_handler);
|
|
|
|
if (error != NSERROR_OK) {
|
2018-08-14 18:14:10 +03:00
|
|
|
lwc_string_unref(textplain_default_charset);
|
|
|
|
}
|
|
|
|
|
|
|
|
error = content_factory_register_handler("application/json",
|
|
|
|
&textplain_content_handler);
|
|
|
|
if (error != NSERROR_OK) {
|
2017-06-02 00:43:45 +03:00
|
|
|
lwc_string_unref(textplain_default_charset);
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2017-06-02 00:43:45 +03:00
|
|
|
|
2018-05-10 13:34:26 +03:00
|
|
|
/* exported interface documented in html/textplain.h */
|
2011-07-26 17:53:42 +04:00
|
|
|
size_t textplain_size(struct content *c)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
2011-07-26 17:53:42 +04:00
|
|
|
textplain_content *text = (textplain_content *) c;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
|
|
|
assert(c != NULL);
|
|
|
|
|
2011-07-26 17:53:42 +04:00
|
|
|
return text->utf8_data_size;
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
2006-02-16 02:09:55 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|