PDF-Make

 view release on metacpan or  search on metacpan

src/pdfmake_textract.c  view on Meta::CPAN

        }
    }
    return 0;
}

/*============================================================================
 * Phase 13 — Annotation + form field text extraction
 *==========================================================================*/

/* Decode a PDF "text string" (§7.9.2) to UTF-8, arena-allocated.
 * Handles UTF-16BE (with BOM FE FF) and UTF-8-with-BOM (EF BB BF); everything
 * else is treated as PDFDocEncoding, which matches ISO-8859-1 for the ASCII
 * subset used by every real-world annotation we've seen. */
static const char *decode_pdf_text(pdfmake_arena_t *arena,
                                    const uint8_t *b, size_t n)
{
    size_t i;
    pdfmake_buf_t out;
    char *s;
    uint32_t cp;
    uint32_t lo;
    uint8_t c;
    if (!arena || !b) return NULL;

    /* UTF-16BE BOM */
    if (n >= 2 && b[0] == 0xFE && b[1] == 0xFF) {
        pdfmake_buf_init(&out);
        s = NULL;
        for (i = 2; i + 1 < n; i += 2) {
            cp = ((uint32_t)b[i] << 8) | b[i + 1];
            if (cp >= 0xD800 && cp <= 0xDBFF && i + 3 < n) {
                lo = ((uint32_t)b[i + 2] << 8) | b[i + 3];
                if (lo >= 0xDC00 && lo <= 0xDFFF) {
                    cp = 0x10000 + ((cp - 0xD800) << 10) + (lo - 0xDC00);
                    i += 2;

src/pdfmake_textract.c  view on Meta::CPAN

                pdfmake_buf_append_byte(&out, 0x80 | ((cp >> 6) & 0x3F));
                pdfmake_buf_append_byte(&out, 0x80 | (cp & 0x3F));
            }
        }
        s = pdfmake_arena_alloc(arena, out.len + 1);
        if (s) { memcpy(s, out.data, out.len); s[out.len] = 0; }
        pdfmake_buf_free(&out);
        return s;
    }

    /* UTF-8 with BOM (PDF 2.0) */
    if (n >= 3 && b[0] == 0xEF && b[1] == 0xBB && b[2] == 0xBF) {
        b += 3; n -= 3;
    }

    /* PDFDocEncoding → promote high-bit bytes to 2-byte UTF-8. */
    pdfmake_buf_init(&out);
    for (i = 0; i < n; i++) {
        c = b[i];
        if (c < 0x80) {
            pdfmake_buf_append_byte(&out, c);



( run in 2.032 seconds using v1.01-cache-2.11-cpan-c966e8aa7e8 )