| 1340 | } |
| 1341 | |
| 1342 | void cbm_pdf_extract_document(CBMExtractCtx *ctx) { |
| 1343 | CBMFileResult *result = ctx->result; |
| 1344 | CBMArena *a = ctx->arena; |
| 1345 | cbm_pdf_result_t pr; |
| 1346 | if (!cbm_pdf_extract((const unsigned char *)ctx->source, (size_t)ctx->source_len, a, &pr)) { |
| 1347 | result->has_error = true; |
| 1348 | result->error_msg = cbm_arena_strdup(a, "pdf: out of memory"); |
| 1349 | return; |
| 1350 | } |
| 1351 | if (pr.status != CBM_PDF_OK) { |
| 1352 | static const char *const why[] = {"ok", "pdf: not a PDF", "pdf: no pages", "pdf: encrypted", |
| 1353 | "pdf: out of memory"}; |
| 1354 | result->has_error = pr.status != CBM_PDF_ENCRYPTED; |
| 1355 | result->error_msg = cbm_arena_strdup(a, why[pr.status]); |
| 1356 | return; |
| 1357 | } |
| 1358 | bool fixture = fixture_pdf(ctx->rel_path ? ctx->rel_path : ""); |
| 1359 | for (int i = 0; i < pr.npages; i++) { |
| 1360 | uint32_t page = (uint32_t)i + 1; |
| 1361 | char name[PDF_PAGE_NAME]; |
| 1362 | char seg[PDF_PAGE_NAME]; |
| 1363 | snprintf(name, sizeof(name), "page %u", page); |
| 1364 | snprintf(seg, sizeof(seg), "page_%u", page); |
| 1365 | char *nm = cbm_arena_strdup(a, name); |
| 1366 | char *qn = cbm_fqn_compute(a, ctx->project, ctx->rel_path, seg); |
| 1367 | char *pv = cbm_arena_sprintf(a, "%u", page); |
| 1368 | const char **kv = (const char **)cbm_arena_alloc(a, 3 * sizeof(char *)); |
| 1369 | if (!nm || !qn || !pv || !kv) { |
| 1370 | result->has_error = true; |
| 1371 | return; |
| 1372 | } |
| 1373 | kv[0] = "page"; |
| 1374 | kv[1] = pv; |
| 1375 | kv[2] = NULL; |
| 1376 | CBMDefinition def; |
| 1377 | memset(&def, 0, sizeof(def)); |
| 1378 | def.name = nm; |
| 1379 | def.qualified_name = qn; |
| 1380 | def.label = "Section"; |
| 1381 | def.file_path = ctx->rel_path; |
| 1382 | def.start_line = page; |
| 1383 | def.end_line = page; |
| 1384 | def.is_exported = true; |
| 1385 | def.docstring = pr.pages[i].len ? page_doc(a, &pr.pages[i]) : NULL; |
| 1386 | def.extra_props = kv; |
| 1387 | cbm_defs_push(&result->defs, a, def); |
| 1388 | if (!fixture && pr.pages[i].len) { |
| 1389 | scan_page(ctx, pr.pages[i].text, pr.pages[i].len, page, qn); |
| 1390 | } |
| 1391 | } |
| 1392 | } |
no test coverage detected