char * fz_copy_selection(fz_context *ctx, fz_stext_page *page, fz_rect rect) { fz_buffer *buffer; fz_rect hitbox; int c, i, block_num, seen = 0; unsigned char *s; float x0 = rect.x0; float x1 = rect.x1; float y0 = rect.y0; float y1 = rect.y1; buffer = fz_new_buffer(ctx, 1024); for (block_num = 0; block_num < page->len; block_num++) { fz_stext_block *block; fz_stext_line *line; fz_stext_span *span; if (page->blocks[block_num].type != FZ_PAGE_BLOCK_TEXT) continue; block = page->blocks[block_num].u.text; for (line = block->lines; line < block->lines + block->len; line++) { for (span = line->first_span; span; span = span->next) { if (seen) { fz_write_buffer_byte(ctx, buffer, '\n'); } seen = 0; for (i = 0; i < span->len; i++) { fz_stext_char_bbox(ctx, &hitbox, span, i); c = span->text[i].c; if (c < 32) c = 0xFFFD; if (hitbox.x1 >= x0 && hitbox.x0 <= x1 && hitbox.y1 >= y0 && hitbox.y0 <= y1) { fz_write_buffer_rune(ctx, buffer, c); seen = 1; } } seen = (seen && span == line->last_span); } } } fz_write_buffer_byte(ctx, buffer, 0); fz_buffer_extract(ctx, buffer, &s); fz_drop_buffer(ctx, buffer); return (char*)s; }
char * fz_copy_selection(fz_context *ctx, fz_text_page *page, fz_rect rect) { fz_buffer *buffer; fz_rect hitbox; fz_text_block *block; fz_text_line *line; int c, i, seen = 0; char *s; float x0 = rect.x0; float x1 = rect.x1; float y0 = rect.y0; float y1 = rect.y1; buffer = fz_new_buffer(ctx, 1024); for (block = page->blocks; block < page->blocks + page->len; block++) { for (line = block->lines; line < block->lines + block->len; line++) { int span_num; for (span_num = 0; span_num < line->len; span_num++) { fz_text_span *span = line->spans[span_num]; if (seen) { fz_write_buffer_byte(ctx, buffer, '\n'); } seen = 0; for (i = 0; i < span->len; i++) { fz_text_char_bbox(&hitbox, span, i); c = span->text[i].c; if (c < 32) c = '?'; if (hitbox.x1 >= x0 && hitbox.x0 <= x1 && hitbox.y1 >= y0 && hitbox.y0 <= y1) { fz_write_buffer_rune(ctx, buffer, c); seen = 1; } } seen = (seen && span_num + 1 == line->len); } } } fz_write_buffer_byte(ctx, buffer, 0); s = (char*)buffer->data; fz_free(ctx, buffer); return s; }
static void do_copy_region(fz_rect *screen_sel, int xofs, int yofs) { fz_buffer *buf; fz_rect page_sel; xofs -= page_tex.x; yofs -= page_tex.y; page_sel.x0 = screen_sel->x0 - xofs; page_sel.y0 = screen_sel->y0 - yofs; page_sel.x1 = screen_sel->x1 - xofs; page_sel.y1 = screen_sel->y1 - yofs; fz_transform_rect(&page_sel, &page_inv_ctm); #ifdef _WIN32 buf = fz_new_buffer_from_page(ctx, page, &page_sel, 1); #else buf = fz_new_buffer_from_page(ctx, page, &page_sel, 0); #endif fz_write_buffer_rune(ctx, buf, 0); glfwSetClipboardString(window, (char*)buf->data); fz_drop_buffer(ctx, buf); }
fz_buffer * fz_new_buffer_from_stext_page(fz_context *ctx, fz_stext_page *text, const fz_rect *sel, int crlf) { fz_buffer *buf; fz_rect hitbox; float x0, y0, x1, y1; int block_num; int need_newline; int i; need_newline = 0; if (fz_is_infinite_rect(sel)) { x0 = y0 = -FLT_MAX; x1 = y1 = FLT_MAX; } else { x0 = sel->x0; y0 = sel->y0; x1 = sel->x1; y1 = sel->y1; } buf = fz_new_buffer(ctx, 256); fz_try(ctx) { for (block_num = 0; block_num < text->len; block_num++) { fz_stext_line *line; fz_stext_block *block; fz_stext_span *span; if (text->blocks[block_num].type != FZ_PAGE_BLOCK_TEXT) continue; block = text->blocks[block_num].u.text; for (line = block->lines; line < block->lines + block->len; line++) { int saw_text = 0; for (span = line->first_span; span; span = span->next) { for (i = 0; i < span->len; i++) { int c; fz_stext_char_bbox(ctx, &hitbox, span, i); c = span->text[i].c; if (c < 32) c = '?'; if (hitbox.x1 >= x0 && hitbox.x0 <= x1 && hitbox.y1 >= y0 && hitbox.y0 <= y1) { saw_text = 1; if (need_newline) { if (crlf) fz_write_buffer_rune(ctx, buf, '\r'); fz_write_buffer_rune(ctx, buf, '\n'); need_newline = 0; } fz_write_buffer_rune(ctx, buf, c); } } } if (saw_text) need_newline = 1; } } } fz_catch(ctx) { fz_drop_buffer(ctx, buf); fz_rethrow(ctx); } return buf; }