|
| 1 | +#include "pdf/fonts/cmap.h" |
| 2 | + |
| 3 | +#include <stdint.h> |
| 4 | +#include <stdio.h> |
| 5 | + |
| 6 | +#include "arena/arena.h" |
| 7 | +#include "logger/log.h" |
| 8 | +#include "pdf_error/error.h" |
| 9 | + |
| 10 | +static char* |
| 11 | +load_file_to_buffer(Arena* arena, const char* path, size_t* out_size) { |
| 12 | + FILE* file = fopen(path, "rb"); |
| 13 | + *out_size = 0; |
| 14 | + if (!file) { |
| 15 | + return NULL; |
| 16 | + } |
| 17 | + |
| 18 | + if (fseek(file, 0, SEEK_END) != 0) { |
| 19 | + fclose(file); |
| 20 | + return NULL; |
| 21 | + } |
| 22 | + |
| 23 | + long len = ftell(file); |
| 24 | + if (len < 0) { |
| 25 | + fclose(file); |
| 26 | + return NULL; |
| 27 | + } |
| 28 | + |
| 29 | + if (fseek(file, 0, SEEK_SET) != 0) { |
| 30 | + fclose(file); |
| 31 | + return NULL; |
| 32 | + } |
| 33 | + |
| 34 | + char* buffer = arena_alloc(arena, (size_t)len); |
| 35 | + if (!buffer) { |
| 36 | + fclose(file); |
| 37 | + return NULL; |
| 38 | + } |
| 39 | + |
| 40 | + if (fread(buffer, 1, (size_t)len, file) != (size_t)len) { |
| 41 | + fclose(file); |
| 42 | + return NULL; |
| 43 | + } |
| 44 | + fclose(file); |
| 45 | + |
| 46 | + *out_size = (size_t)len; |
| 47 | + return buffer; |
| 48 | +} |
| 49 | + |
| 50 | +int main(void) { |
| 51 | + Arena* arena = arena_new(1024); |
| 52 | + |
| 53 | + size_t buffer_len; |
| 54 | + const char* buffer = load_file_to_buffer( |
| 55 | + arena, |
| 56 | + "assets/cmap-resources/Adobe-Identity-0/CMap/Identity-H", |
| 57 | + &buffer_len |
| 58 | + ); |
| 59 | + |
| 60 | + PdfCMap* cmap = NULL; |
| 61 | + PDF_REQUIRE(pdf_parse_cmap(arena, buffer, buffer_len, &cmap)); |
| 62 | + |
| 63 | + for (uint32_t codepoint = 0; codepoint <= 0xffff; codepoint++) { |
| 64 | + uint32_t cid; |
| 65 | + PDF_REQUIRE( |
| 66 | + pdf_cmap_get_cid(cmap, codepoint, &cid), |
| 67 | + "Failed to get cid for codepoint 0x%04x", |
| 68 | + (unsigned int)codepoint |
| 69 | + ); |
| 70 | + |
| 71 | + LOG_DIAG( |
| 72 | + INFO, |
| 73 | + CMAP, |
| 74 | + "0x%04x = %llu", |
| 75 | + codepoint, |
| 76 | + (unsigned long long int)cid |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + arena_free(arena); |
| 81 | + return 0; |
| 82 | +} |
0 commit comments