netsurf/render/textplain.c
John Tytgat 35d3d6d0bb First merge of Adam Blokus' GSoC work from his branch 'branches/adamblokus/netsurf'.
Merged revisions 4212-4552,4554-4709,4711-4724 via svnmerge from 
svn://svn.netsurf-browser.org/branches/adamblokus/netsurf

........
  r4212 | adamblokus | 2008-05-26 19:42:31 +0200 (Mon, 26 May 2008) | 4 lines
  
  Pdf plotting skeleton pinned on Print Preview in GTK.
  Just creates a file and draws lines. 
........
  r4213 | adamblokus | 2008-05-27 00:11:03 +0200 (Tue, 27 May 2008) | 4 lines
  
  Pdf plotter - added drawing some graphic primitives.
  Still with limited functionality, but a snapshot of the
  currently viewed page can be made and resembles the original.
........
  r4214 | adamblokus | 2008-05-27 11:43:31 +0200 (Tue, 27 May 2008) | 2 lines
  
  Corrected encoding name
........
  r4215 | adamblokus | 2008-05-27 12:47:26 +0200 (Tue, 27 May 2008) | 3 lines
  
  Colours and polygons added.
........
  r4217 | adamblokus | 2008-05-27 21:39:35 +0200 (Tue, 27 May 2008) | 6 lines
  
  Added rectangles, filled boxes and clipping.
  Taken into consideration joty's comments.
  Added a todo list for this part.
  Added some debug stuff and checking boundaries.
........
  r4218 | adamblokus | 2008-05-28 12:37:30 +0200 (Wed, 28 May 2008) | 2 lines
  
  Added path ploting (not sure if valid argument order for bezier) and dashed/dotted line styles
........
  r4221 | adamblokus | 2008-05-28 22:11:05 +0200 (Wed, 28 May 2008) | 3 lines
  
  Some more options in graphic primitives and normalizing some parameters.
........
  r4235 | adamblokus | 2008-05-31 22:54:56 +0200 (Sat, 31 May 2008) | 4 lines
  
  Plotting changed as jmb suggested (is the least invasive one from the possible)
  Added dummy bitmap plotting - way of plotting an image is determined by its type. 
........
  r4251 | adamblokus | 2008-06-03 17:12:15 +0200 (Tue, 03 Jun 2008) | 3 lines
  
  Added plotting jpg and png images - quite a lot to improve in this code, but it seems to work ;)
........
  r4263 | adamblokus | 2008-06-05 14:20:32 +0200 (Thu, 05 Jun 2008) | 3 lines
  
  Added hadling images other than png and jpeg - with transparency.
........
  r4267 | adamblokus | 2008-06-06 15:36:34 +0200 (Fri, 06 Jun 2008) | 5 lines
  
  Added handling NULL-returns from all mallocs.
  Added plot_bitmap_tile handling.
  Changed code style a little.
........
  r4327 | adamblokus | 2008-06-12 17:46:34 +0200 (Thu, 12 Jun 2008) | 5 lines
  
  Added a first prototype of the paged-output organization.
  Still not sure about naming, file locations etc.
  Works with the same pdf plotting as before.
........
  r4328 | adamblokus | 2008-06-13 13:52:15 +0200 (Fri, 13 Jun 2008) | 4 lines
  
  Added primitive width adjustment and outputing the whole
  website in multiple pages.
........
  r4336 | joty | 2008-06-15 15:06:57 +0200 (Sun, 15 Jun 2008) | 1 line
  
  Fix RISC OS build failure (change r4235 wasn't complete).
........
  r4337 | joty | 2008-06-15 18:15:32 +0200 (Sun, 15 Jun 2008) | 16 lines
  
  This enables "Export PDF" in RISC OS build:
  
  - Docs/Doxyfile(PREDEFINED): Added WITH_PDF_EXPORT
  - Makefile.sources(S_PDF): Add to RISC OS target as well.
  - utils/config.h: Define WITH_PDF_EXPORT which controls if we want to have
    PDF export functionality or not.
  - riscos/save_pdf.c,riscos/save_pdf.h(save_as_pdf): Use PDF print API made
    by Adam Blokus to write a PDF file under RISC OS.
  - riscos/save.c: Call save_as_pdf added.
  - riscos/menus.c: Add 'Export->PDF' menu entry.
  - riscos/menus.h(menu_action): Added BROWSER_EXPORT_PDF.
  - desktop/gui.h(gui_save_type): Added GUI_SAVE_PDF.
  - desktop/print.c(print_run): Added return value.
  - Makefile(CCACHE): Moved closed to the place where CC is set for the first time.
    (LDFLAGS): Centralised adding all non-pkgconfig libraries and added Haru + PNG libs.
........
  r4343 | adamblokus | 2008-06-16 01:08:52 +0200 (Mon, 16 Jun 2008) | 3 lines
  
  Added margins and page size adjustment.
........
  r4412 | adamblokus | 2008-06-21 20:22:07 +0200 (Sat, 21 Jun 2008) | 4 lines
  
  Added 'fuzzy' margins on page bottom. 
  Disabled direct png embedding, because it is too unstable in Haru now.
........
  r4421 | adamblokus | 2008-06-22 18:52:28 +0200 (Sun, 22 Jun 2008) | 2 lines
  
  Added "Save as.." dialog and Export->PDF menu entry. Print preview still works with default path.
........
  r4437 | adamblokus | 2008-06-25 02:44:46 +0200 (Wed, 25 Jun 2008) | 4 lines
  
  Added skeleton of applying loose layout.
  Minor code cleaning-up.
........
  r4492 | adamblokus | 2008-07-02 09:02:42 +0200 (Wed, 02 Jul 2008) | 5 lines
  
  Implemented the elementar ideas of the loose layout.
  Added scaling in the printing routine.
  Added some basic demonstrations.
........
  r4493 | adamblokus | 2008-07-02 09:05:55 +0200 (Wed, 02 Jul 2008) | 3 lines
  
  Cleaned up the loosing code - commited to much of leftover rubbish code.
........
  r4507 | adamblokus | 2008-07-04 14:25:48 +0200 (Fri, 04 Jul 2008) | 4 lines
  
  Added duplicating box tree and current content - window flickering during printing solved.
  Minor error checking after new HPDF_Image_AddSMask call.
........
  r4515 | adamblokus | 2008-07-06 22:28:16 +0200 (Sun, 06 Jul 2008) | 2 lines
  
  Changes in loosen layout (image resizing).
........
  r4517 | adamblokus | 2008-07-06 22:38:23 +0200 (Sun, 06 Jul 2008) | 2 lines
  
  Added pdf font handling and rendering functions with the use of Haru functions.
........
  r4555 | adamblokus | 2008-07-10 00:59:05 +0200 (Thu, 10 Jul 2008) | 2 lines
  
  Added a very basic and still buggy GTK print implementation.
........
  r4565 | adamblokus | 2008-07-10 14:50:16 +0200 (Thu, 10 Jul 2008) | 2 lines
  
  Added gtk printing one more time - I have forgotten to add the main file.
........
  r4566 | adamblokus | 2008-07-10 14:57:02 +0200 (Thu, 10 Jul 2008) | 2 lines
  
  removed error with comment
........
  r4569 | adamblokus | 2008-07-10 15:52:55 +0200 (Thu, 10 Jul 2008) | 5 lines
  
  Major style improvements - added a lot of doxygen comments, 
  followed tlsa's style guide.
  Added some more error checking, too.
........
  r4575 | adamblokus | 2008-07-10 18:48:26 +0200 (Thu, 10 Jul 2008) | 2 lines
  
  Cleaned up the code.
........
  r4687 | adamblokus | 2008-07-17 14:17:19 +0200 (Thu, 17 Jul 2008) | 2 lines
  
  Changed everything according to jmb's review plus some minor bug fixes to gtk_print.
........
  r4688 | adamblokus | 2008-07-17 17:16:34 +0200 (Thu, 17 Jul 2008) | 2 lines
  
  Solved the netsurf.glade clash from r4421.
........
  r4693 | adamblokus | 2008-07-18 18:11:51 +0200 (Fri, 18 Jul 2008) | 2 lines
  
  Fixed bug with wrong number of pages in gtk printing.
........
  r4695 | adamblokus | 2008-07-18 19:59:24 +0200 (Fri, 18 Jul 2008) | 3 lines
  
  - fixed uncommented line from the previous commit
  - fixed bug with scale bigger than 1.0 (incorretly clipped page)
........
  r4696 | adamblokus | 2008-07-18 23:28:00 +0200 (Fri, 18 Jul 2008) | 2 lines
  
  Fixed bug in gtk_print_font_paint (and nsfont_paint).
........
  r4697 | adamblokus | 2008-07-18 23:35:38 +0200 (Fri, 18 Jul 2008) | 2 lines
  
  Bug fix in nsfont_paint.
........
  r4711 | adamblokus | 2008-07-19 22:44:15 +0200 (Sat, 19 Jul 2008) | 2 lines
  
  Added gtk_selection files.
........
  r4712 | adamblokus | 2008-07-20 11:15:06 +0200 (Sun, 20 Jul 2008) | 2 lines
  
  Addam missing glade files.
........
  r4713 | joty | 2008-07-20 17:13:10 +0200 (Sun, 20 Jul 2008) | 1 line
  
  Follow change r4517 for RISC OS and BeOS platforms : Added pdf font handling and rendering functions with the use of Haru functions.
........
  r4714 | joty | 2008-07-20 18:19:50 +0200 (Sun, 20 Jul 2008) | 1 line
  
  Declare haru_nsfont iso define an instance for each C source including the font_haru.h header.  This fixes breakage of PDF export on RISC OS.
........
  r4724 | adamblokus | 2008-07-23 03:30:08 +0200 (Wed, 23 Jul 2008) | 6 lines
  
  Applied changes according to joty's review.
  Added checking the dimensions of a plotted image to pdf plotter.
  Commented out jpg embedding (it seems to cause some problems
  I'll bring it back when I figure out what's wrong) .
  Added back some files removed by mistake.
........


svn path=/trunk/netsurf/; revision=4741
2008-07-26 16:01:59 +00:00

719 lines
18 KiB
C

/*
* Copyright 2006 James Bursa <bursa@users.sourceforge.net>
* Copyright 2006 Adrian Lees <adrianl@users.sourceforge.net>
*
* 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/>.
*/
/** \file
* Content for text/plain (implementation).
*/
#include <assert.h>
#include <errno.h>
#include <stddef.h>
#define LIBICONV_PLUG
#include <iconv.h>
#include "content/content.h"
#include "css/css.h"
#include "desktop/gui.h"
#include "desktop/plotters.h"
#include "desktop/selection.h"
#include "render/box.h"
#include "render/font.h"
#include "render/textplain.h"
#include "utils/log.h"
#include "utils/messages.h"
#include "utils/talloc.h"
#include "utils/utils.h"
#include "utils/utf8.h"
#define CHUNK 20480
#define MARGIN 4
#define TAB_WIDTH 8 /* must be power of 2 currently */
static struct css_style textplain_style;
static int textplain_tab_width = 256; /* try for a sensible default */
static int textplain_coord_from_offset(const char *text, size_t offset,
size_t length);
/**
* Create a CONTENT_TEXTPLAIN.
*/
bool textplain_create(struct content *c, const char *params[])
{
unsigned int i;
char *utf8_data;
const char *encoding = "iso-8859-1";
iconv_t iconv_cd;
union content_msg_data msg_data;
textplain_style = css_base_style;
textplain_style.font_family = CSS_FONT_FAMILY_MONOSPACE;
utf8_data = talloc_array(c, char, CHUNK);
if (!utf8_data)
goto no_memory;
for (i = 0; params[i]; i += 2) {
if (strcasecmp(params[i], "charset") == 0) {
encoding = talloc_strdup(c, params[i + 1]);
if (!encoding)
goto no_memory;
break;
}
}
iconv_cd = iconv_open("utf-8", encoding);
if (iconv_cd == (iconv_t)(-1) && errno == EINVAL) {
LOG(("unsupported encoding \"%s\"", encoding));
iconv_cd = iconv_open("utf-8", "iso-8859-1");
}
if (iconv_cd == (iconv_t)(-1)) {
char buf[300];
snprintf(buf, sizeof buf, "IconvFailed %s", strerror(errno));
buf[sizeof buf - 1] = 0;
msg_data.error = buf;
content_broadcast(c, CONTENT_MSG_ERROR, msg_data);
return false;
}
c->data.textplain.encoding = encoding;
c->data.textplain.iconv_cd = iconv_cd;
c->data.textplain.converted = 0;
c->data.textplain.utf8_data = utf8_data;
c->data.textplain.utf8_data_size = 0;
c->data.textplain.utf8_data_allocated = CHUNK;
c->data.textplain.physical_line = 0;
c->data.textplain.physical_line_count = 0;
c->data.textplain.formatted_width = 0;
return true;
no_memory:
msg_data.error = messages_get("NoMemory");
content_broadcast(c, CONTENT_MSG_ERROR, msg_data);
return false;
}
/**
* Process data for CONTENT_TEXTPLAIN.
*/
bool textplain_process_data(struct content *c, char *data, unsigned int size)
{
iconv_t iconv_cd = c->data.textplain.iconv_cd;
size_t count;
union content_msg_data msg_data;
do {
char *inbuf = c->source_data + c->data.textplain.converted;
size_t inbytesleft = c->source_size -
c->data.textplain.converted;
char *outbuf = c->data.textplain.utf8_data +
c->data.textplain.utf8_data_size;
size_t outbytesleft = c->data.textplain.utf8_data_allocated -
c->data.textplain.utf8_data_size;
count = iconv(iconv_cd, &inbuf, &inbytesleft,
&outbuf, &outbytesleft);
c->data.textplain.converted = inbuf - c->source_data;
c->data.textplain.utf8_data_size = c->data.textplain.
utf8_data_allocated - outbytesleft;
if (count == (size_t)(-1) && errno == E2BIG) {
size_t allocated = CHUNK +
c->data.textplain.utf8_data_allocated;
char *utf8_data = talloc_realloc(c,
c->data.textplain.utf8_data,
char, allocated);
if (!utf8_data)
goto no_memory;
c->data.textplain.utf8_data = utf8_data;
c->data.textplain.utf8_data_allocated = allocated;
} else if (count == (size_t)(-1) && errno != EINVAL) {
char buf[300];
snprintf(buf, sizeof buf, "IconvFailed %s",
strerror(errno));
buf[sizeof buf - 1] = 0;
msg_data.error = buf;
content_broadcast(c, CONTENT_MSG_ERROR, msg_data);
return false;
}
gui_multitask();
} while (!(c->data.textplain.converted == c->source_size ||
(count == (size_t)(-1) && errno == EINVAL)));
return true;
no_memory:
msg_data.error = messages_get("NoMemory");
content_broadcast(c, CONTENT_MSG_ERROR, msg_data);
return false;
}
/**
* Convert a CONTENT_TEXTPLAIN for display.
*/
bool textplain_convert(struct content *c, int width, int height)
{
iconv_close(c->data.textplain.iconv_cd);
c->data.textplain.iconv_cd = 0;
textplain_reformat(c, width, height);
c->status = CONTENT_STATUS_DONE;
content_set_status(c, messages_get("Done"));
return true;
}
/**
* Reformat a CONTENT_TEXTPLAIN to a new width.
*/
void textplain_reformat(struct content *c, int width, int height)
{
char *utf8_data = c->data.textplain.utf8_data;
size_t utf8_data_size = c->data.textplain.utf8_data_size;
unsigned long line_count = 0;
struct textplain_line *line = c->data.textplain.physical_line;
struct textplain_line *line1;
size_t i, space, col;
size_t columns = 80;
int character_width;
size_t line_start;
/* compute available columns (assuming monospaced font) - use 8
* characters for better accuracy */
if (!nsfont.font_width(&textplain_style, "ABCDEFGH", 8, &character_width))
return;
columns = (width - MARGIN - MARGIN) * 8 / character_width;
textplain_tab_width = (TAB_WIDTH * character_width) / 8;
c->data.textplain.formatted_width = width;
c->data.textplain.physical_line_count = 0;
if (!line) {
c->data.textplain.physical_line = line =
talloc_array(c, struct textplain_line, 1024 + 3);
if (!line)
goto no_memory;
}
line[line_count++].start = line_start = 0;
space = 0;
for (i = 0, col = 0; i != utf8_data_size; i++) {
bool term = (utf8_data[i] == '\n' || utf8_data[i] == '\r');
size_t next_col = col + 1;
if (utf8_data[i] == '\t')
next_col = (next_col + TAB_WIDTH - 1) & ~(TAB_WIDTH - 1);
if (term || next_col >= columns) {
if (line_count % 1024 == 0) {
line1 = talloc_realloc(c, line,
struct textplain_line, line_count + 1024 + 3);
if (!line1)
goto no_memory;
c->data.textplain.physical_line =
line = line1;
}
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 &&
utf8_data[i+1] != utf8_data[i] &&
(utf8_data[i+1] == '\n' || utf8_data[i+1] == '\r'))
i++;
}
else {
if (space) {
/* break at last space in line */
i = space;
line[line_count-1].length = (i + 1) - line_start;
}
else
line[line_count-1].length = i - line_start;
}
line[line_count++].start = line_start = i + 1;
col = 0;
space = 0;
} else {
col++;
if (utf8_data[i] == ' ')
space = i;
}
}
line[line_count-1].length = i - line[line_count-1].start;
line[line_count].start = utf8_data_size;
c->data.textplain.physical_line_count = line_count;
c->width = width;
c->height = line_count *
css_len2px(&textplain_style.font_size.value.length,
&textplain_style) * 1.2 + MARGIN + MARGIN;
return;
no_memory:
LOG(("out of memory (line_count %lu)", line_count));
return;
}
/**
* Destroy a CONTENT_TEXTPLAIN and free all resources it owns.
*/
void textplain_destroy(struct content *c)
{
if (c->data.textplain.iconv_cd)
iconv_close(c->data.textplain.iconv_cd);
}
/**
* Draw a CONTENT_TEXTPLAIN using the current set of plotters (plot).
*
* \param c content of type CONTENT_TEXTPLAIN
* \param x coordinate for top-left of redraw
* \param y coordinate for top-left of redraw
* \param width available width
* \param height available height
* \param clip_x0 clip rectangle
* \param clip_y0 clip rectangle
* \param clip_x1 clip rectangle
* \param clip_y1 clip rectangle
* \param scale scale for redraw
* \param background_colour the background colour
* \return true if successful, false otherwise
*
* x, y, clip_[xy][01] are in target coordinates.
*/
bool textplain_redraw(struct content *c, int x, int y,
int width, int height,
int clip_x0, int clip_y0, int clip_x1, int clip_y1,
float scale, unsigned long background_colour)
{
struct browser_window *bw = current_redraw_browser;
char *utf8_data = c->data.textplain.utf8_data;
long lineno;
unsigned long line_count = c->data.textplain.physical_line_count;
float line_height = css_len2px(&textplain_style.font_size.value.length,
&textplain_style) * 1.2;
float scaled_line_height = line_height * scale;
long line0 = clip_y0 / scaled_line_height - 1;
long line1 = clip_y1 / scaled_line_height + 1;
struct textplain_line *line = c->data.textplain.physical_line;
colour hback_col;
struct rect clip;
size_t length;
clip.x0 = clip_x0;
clip.y0 = clip_y0;
clip.x1 = clip_x1;
clip.y1 = clip_y1;
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;
if (!plot.clg(0xffffff))
return false;
if (!line)
return true;
/* choose a suitable background colour for any highlighted text */
if ((background_colour & 0x808080) == 0x808080)
hback_col = 0;
else
hback_col = 0xffffff;
x += MARGIN * scale;
y += MARGIN * scale;
for (lineno = line0; lineno != line1; lineno++) {
const char *text = utf8_data + line[lineno].start;
int tab_width = textplain_tab_width * scale;
size_t offset = 0;
int tx = x;
if (!tab_width) tab_width = 1;
length = line[lineno].length;
if (!length)
continue;
while (offset < length) {
size_t next_offset = offset;
int width;
int ntx;
while (next_offset < length && text[next_offset] != '\t')
next_offset = utf8_next(text, length, next_offset);
if (!text_redraw(text + offset, next_offset - offset,
line[lineno].start + offset, false,
&textplain_style,
tx, y + (lineno * scaled_line_height),
&clip, line_height, scale,
background_colour, false))
return false;
if (next_offset >= length)
break;
/* locate end of string and align to next tab position */
if (nsfont.font_width(&textplain_style, &text[offset],
next_offset - offset, &width))
tx += (int)(width * scale);
ntx = x + ((1 + (tx - x) / tab_width) * tab_width);
/* 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 */
if (bw) {
unsigned tab_ofst = line[lineno].start + next_offset;
struct selection *sel = bw->sel;
bool highlighted = false;
if (selection_defined(sel)) {
unsigned start_idx, end_idx;
if (selection_highlighted(sel,
tab_ofst, tab_ofst + 1,
&start_idx, &end_idx))
highlighted = true;
}
if (!highlighted && search_current_window == bw->window) {
unsigned start_idx, end_idx;
if (gui_search_term_highlighted(bw->window,
tab_ofst, tab_ofst + 1,
&start_idx, &end_idx))
highlighted = true;
}
if (highlighted) {
int sy = y + (lineno * scaled_line_height);
if (!plot.fill(tx, sy, ntx, sy + scaled_line_height,
hback_col))
return false;
}
}
offset = next_offset + 1;
tx = ntx;
}
}
return true;
}
/**
* 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 c content of type CONTENT_TEXTPLAIN
* \param x x ordinate of point
* \param y y ordinate of point
* \param dir direction of search if not within line
* \return byte offset of character containing (or nearest to) point
*/
size_t textplain_offset_from_coords(struct content *c, int x, int y, int dir)
{
float line_height = css_len2px(&textplain_style.font_size.value.length,
&textplain_style) * 1.2;
struct textplain_line *line;
const char *text;
unsigned nlines;
size_t length;
int idx;
assert(c->type == CONTENT_TEXTPLAIN);
y = (int)((float)(y - MARGIN) / line_height);
x -= MARGIN;
nlines = c->data.textplain.physical_line_count;
if (!nlines)
return 0;
if (y <= 0) y = 0;
else if ((unsigned)y >= nlines)
y = nlines - 1;
line = &c->data.textplain.physical_line[y];
text = c->data.textplain.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)
nsfont.font_width(&textplain_style, text, next_offset, &width);
if (x <= width) {
int pixel_offset;
size_t char_offset;
nsfont.font_position_in_string(&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;
}
/**
* Given a byte offset within the text, return the line number
* of the line containing that offset (or -1 if offset invalid)
*
* \param c content of type CONTENT_TEXTPLAIN
* \param offset byte offset within textual representation
* \return line number, or -1 if offset invalid (larger than size)
*/
int textplain_find_line(struct content *c, unsigned offset)
{
struct textplain_line *line = c->data.textplain.physical_line;
int nlines = c->data.textplain.physical_line_count;
int lineno = 0;
assert(c->type == CONTENT_TEXTPLAIN);
if (offset > c->data.textplain.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;
}
/**
* Convert a character offset within a line of text into the
* horizontal co-ordinate, taking 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
*/
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);
nsfont.font_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;
}
/**
* Given a range of byte offsets within a UTF8 textplain content,
* return a box that fully encloses the text
*
* \param c content of type CONTENT_TEXTPLAIN
* \param start byte offset of start of text range
* \param end byte offset of end
* \param r rectangle to be completed
*/
void textplain_coords_from_range(struct content *c, unsigned start, unsigned end,
struct rect *r)
{
float line_height = css_len2px(&textplain_style.font_size.value.length,
&textplain_style) * 1.2;
char *utf8_data = c->data.textplain.utf8_data;
struct textplain_line *line;
unsigned lineno = 0;
unsigned nlines;
assert(c->type == CONTENT_TEXTPLAIN);
assert(start <= end);
assert(end <= c->data.textplain.utf8_data_size);
nlines = c->data.textplain.physical_line_count;
line = c->data.textplain.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 = c->data.textplain.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);
}
/**
* Return a pointer to the requested line of text.
*
* \param c content of type CONTENT_TEXTPLAIN
* \param lineno line number
* \param poffset receives byte offset of line start within text
* \param plen receives length of returned line
* \return pointer to text, or NULL if invalid line number
*/
char *textplain_get_line(struct content *c, unsigned lineno,
size_t *poffset, size_t *plen)
{
struct textplain_line *line;
assert(c->type == CONTENT_TEXTPLAIN);
if (lineno >= c->data.textplain.physical_line_count)
return NULL;
line = &c->data.textplain.physical_line[lineno];
*poffset = line->start;
*plen = line->length;
return c->data.textplain.utf8_data + line->start;
}
/**
* 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 c content of type CONTENT_TEXTPLAIN
* \param start starting byte offset within UTF-8 text
* \param end ending byte offset
* \param plen receives validated length
* \return pointer to text, or NULL if no text
*/
char *textplain_get_raw_data(struct content *c, unsigned start, unsigned end,
size_t *plen)
{
size_t utf8_size = c->data.textplain.utf8_data_size;
assert(c->type == CONTENT_TEXTPLAIN);
/* 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 c->data.textplain.utf8_data + start;
}