ElyPDF worker: text blocks are always valid JSON, whatever the text or font name holds (P-14)
This commit is contained in:
4 files changed
+81
-12
No files matched your search
@@ -9,18 +9,34 @@ static const char *stamps[] = { "Approved", "AsIs", "Confidential", "Departmenta
|
||||
"Expired", "Final", "ForComment", "ForPublicRelease", "NotApproved", "NotForPublicRelease", "Sold",
|
||||
"TopSecret", NULL };
|
||||
|
||||
/* Valid UTF-8 always: a stray byte becomes U+FFFD. */
|
||||
void ely_json_string(fz_context *ctx, fz_output *out, const char *s)
|
||||
{
|
||||
char utf8[FZ_UTFMAX];
|
||||
int rune;
|
||||
fz_write_byte(ctx, out, '"');
|
||||
for (; *s; s++)
|
||||
while (*s)
|
||||
{
|
||||
unsigned char c = (unsigned char)*s;
|
||||
if (c == '"' || c == '\\')
|
||||
fz_write_printf(ctx, out, "\\%c", c);
|
||||
else if (c < 32)
|
||||
fz_write_printf(ctx, out, "\\u%04x", c);
|
||||
s += fz_chartorune(&rune, s);
|
||||
/* A surrogate pair joins; a lone one is no character. */
|
||||
if (rune >= 0xD800 && rune < 0xDC00)
|
||||
{
|
||||
int low;
|
||||
int n = fz_chartorune(&low, s);
|
||||
if (low >= 0xDC00 && low < 0xE000)
|
||||
{
|
||||
rune = 0x10000 + ((rune - 0xD800) << 10) + (low - 0xDC00);
|
||||
s += n;
|
||||
}
|
||||
}
|
||||
if ((rune >= 0xD800 && rune < 0xE000) || rune > 0x10FFFF)
|
||||
rune = FZ_REPLACEMENT_CHARACTER;
|
||||
if (rune == '"' || rune == '\\')
|
||||
fz_write_printf(ctx, out, "\\%c", rune);
|
||||
else if (rune < 32)
|
||||
fz_write_printf(ctx, out, "\\u%04x", rune);
|
||||
else
|
||||
fz_write_byte(ctx, out, c);
|
||||
fz_write_data(ctx, out, utf8, fz_runetochar(utf8, rune));
|
||||
}
|
||||
fz_write_byte(ctx, out, '"');
|
||||
}
|
||||
|
||||
@@ -313,10 +313,13 @@ int ely_text_blocks(fz_context *ctx, fz_document *doc, int number, unsigned char
|
||||
words = fz_new_buffer(ctx, 256);
|
||||
block_text(ctx, words, block);
|
||||
fz_terminate_buffer(ctx, words);
|
||||
fz_write_printf(ctx, out, "%s{\"rect\":[%g,%g,%g,%g],\"text\":%q,\"font\":%q,\"size\":%g,\"color\":%d,\"editable\":%s}",
|
||||
first ? "" : ",", block->bbox.x0, block->bbox.y0, block->bbox.x1, block->bbox.y1,
|
||||
fz_string_from_buffer(ctx, words), fz_font_name(ctx, ch->font), ch->size, (int)(ch->argb & 0xffffff),
|
||||
editable ? "true" : "false");
|
||||
fz_write_printf(ctx, out, "%s{\"rect\":[%g,%g,%g,%g],\"text\":",
|
||||
first ? "" : ",", block->bbox.x0, block->bbox.y0, block->bbox.x1, block->bbox.y1);
|
||||
ely_json_string(ctx, out, fz_string_from_buffer(ctx, words));
|
||||
fz_write_string(ctx, out, ",\"font\":");
|
||||
ely_json_string(ctx, out, fz_font_name(ctx, ch->font));
|
||||
fz_write_printf(ctx, out, ",\"size\":%g,\"color\":%d,\"editable\":%s}",
|
||||
ch->size, (int)(ch->argb & 0xffffff), editable ? "true" : "false");
|
||||
first = 0;
|
||||
}
|
||||
fz_write_string(ctx, out, "]");
|
||||
|
||||
Reference in new issue
Block a user