1

Output tags for missing font data

This commit is contained in:
Alex Yatskov 2016-12-01 20:46:44 -08:00
parent 619ee1bb16
commit 8ea44e780a
7 changed files with 51 additions and 25 deletions

32
book.c
View File

@ -48,7 +48,7 @@ typedef struct {
* Helper functions * Helper functions
*/ */
static char* book_read(EB_Book* book, EB_Hookset* hookset, const EB_Position* position, Book_Mode mode, const Font_Table* table) { static char* book_read(EB_Book* book, EB_Hookset* hookset, const EB_Position* position, Book_Mode mode, const Font_Table* table, int flags) {
if (eb_seek_text(book, position) != EB_SUCCESS) { if (eb_seek_text(book, position) != EB_SUCCESS) {
return NULL; return NULL;
} }
@ -57,13 +57,17 @@ static char* book_read(EB_Book* book, EB_Hookset* hookset, const EB_Position* po
ssize_t data_length = 0; ssize_t data_length = 0;
EB_Error_Code error; EB_Error_Code error;
Hook_Params params = {};
params.table = table;
params.flags = flags;
switch (mode) { switch (mode) {
case BOOK_MODE_TEXT: case BOOK_MODE_TEXT:
error = eb_read_text( error = eb_read_text(
book, book,
NULL, NULL,
hookset, hookset,
(void*)table, &params,
ARRSIZE(data) - 1, ARRSIZE(data) - 1,
data, data,
&data_length &data_length
@ -74,7 +78,7 @@ static char* book_read(EB_Book* book, EB_Hookset* hookset, const EB_Position* po
book, book,
NULL, NULL,
hookset, hookset,
(void*)table, &params,
ARRSIZE(data) - 1, ARRSIZE(data) - 1,
data, data,
&data_length &data_length
@ -97,9 +101,9 @@ static char* book_read(EB_Book* book, EB_Hookset* hookset, const EB_Position* po
return result; return result;
} }
static Book_Block book_read_content(EB_Book* book, EB_Hookset* hookset, const EB_Position* position, Book_Mode mode, const Font_Table* table) { static Book_Block book_read_content(EB_Book* book, EB_Hookset* hookset, const EB_Position* position, Book_Mode mode, const Font_Table* table, int flags) {
Book_Block block = {}; Book_Block block = {};
block.text = book_read(book, hookset, position, mode, table); block.text = book_read(book, hookset, position, mode, table, flags);
block.page = position->page; block.page = position->page;
block.offset = position->offset; block.offset = position->offset;
return block; return block;
@ -237,7 +241,7 @@ static void book_encode(json_t* book_json, const Book* book, int flags) {
* Importing from EPWING * Importing from EPWING
*/ */
static void subbook_entries_import(Book_Subbook* subbook, EB_Book* eb_book, EB_Hookset* eb_hookset, const Font_Table* table) { static void subbook_entries_import(Book_Subbook* subbook, EB_Book* eb_book, EB_Hookset* eb_hookset, const Font_Table* table, int flags) {
if (subbook->entry_alloc == 0) { if (subbook->entry_alloc == 0) {
subbook->entry_alloc = 16384; subbook->entry_alloc = 16384;
subbook->entries = malloc(subbook->entry_alloc * sizeof(Book_Entry)); subbook->entries = malloc(subbook->entry_alloc * sizeof(Book_Entry));
@ -260,14 +264,14 @@ static void subbook_entries_import(Book_Subbook* subbook, EB_Book* eb_book, EB_H
} }
Book_Entry* entry = subbook->entries + subbook->entry_count++; Book_Entry* entry = subbook->entries + subbook->entry_count++;
entry->heading = book_read_content(eb_book, eb_hookset, &hit->heading, BOOK_MODE_HEADING, table); entry->heading = book_read_content(eb_book, eb_hookset, &hit->heading, BOOK_MODE_HEADING, table, flags);
entry->text = book_read_content(eb_book, eb_hookset, &hit->text, BOOK_MODE_TEXT, table); entry->text = book_read_content(eb_book, eb_hookset, &hit->text, BOOK_MODE_TEXT, table, flags);
} }
} }
while (hit_count > 0); while (hit_count > 0);
} }
static void subbook_import(Book_Subbook* subbook, const Font_Context* context, EB_Book* eb_book, EB_Hookset* eb_hookset) { static void subbook_import(Book_Subbook* subbook, const Font_Context* context, EB_Book* eb_book, EB_Hookset* eb_hookset, int flags) {
const Font_Table* table = NULL; const Font_Table* table = NULL;
char title[EB_MAX_TITLE_LENGTH + 1]; char title[EB_MAX_TITLE_LENGTH + 1];
if (eb_subbook_title(eb_book, title) == EB_SUCCESS) { if (eb_subbook_title(eb_book, title) == EB_SUCCESS) {
@ -278,20 +282,20 @@ static void subbook_import(Book_Subbook* subbook, const Font_Context* context, E
if (eb_have_copyright(eb_book)) { if (eb_have_copyright(eb_book)) {
EB_Position position; EB_Position position;
if (eb_copyright(eb_book, &position) == EB_SUCCESS) { if (eb_copyright(eb_book, &position) == EB_SUCCESS) {
subbook->copyright = book_read_content(eb_book, eb_hookset, &position, BOOK_MODE_TEXT, table); subbook->copyright = book_read_content(eb_book, eb_hookset, &position, BOOK_MODE_TEXT, table, flags);
} }
} }
if (eb_search_all_alphabet(eb_book) == EB_SUCCESS) { if (eb_search_all_alphabet(eb_book) == EB_SUCCESS) {
subbook_entries_import(subbook, eb_book, eb_hookset, table); subbook_entries_import(subbook, eb_book, eb_hookset, table, flags);
} }
if (eb_search_all_kana(eb_book) == EB_SUCCESS) { if (eb_search_all_kana(eb_book) == EB_SUCCESS) {
subbook_entries_import(subbook, eb_book, eb_hookset, table); subbook_entries_import(subbook, eb_book, eb_hookset, table, flags);
} }
if (eb_search_all_asis(eb_book) == EB_SUCCESS) { if (eb_search_all_asis(eb_book) == EB_SUCCESS) {
subbook_entries_import(subbook, eb_book, eb_hookset, table); subbook_entries_import(subbook, eb_book, eb_hookset, table, flags);
} }
} }
@ -404,7 +408,7 @@ bool book_import(Book* book, const Font_Context* context, const char path[], int
for (int i = 0; i < book->subbook_count; ++i) { for (int i = 0; i < book->subbook_count; ++i) {
Book_Subbook* subbook = book->subbooks + i; Book_Subbook* subbook = book->subbooks + i;
if ((error = eb_set_subbook(&eb_book, sub_codes[i])) == EB_SUCCESS) { if ((error = eb_set_subbook(&eb_book, sub_codes[i])) == EB_SUCCESS) {
subbook_import(subbook, context, &eb_book, &eb_hookset); subbook_import(subbook, context, &eb_book, &eb_hookset, flags);
} }
else { else {
fprintf(stderr, "Failed to set subbook: %s\n", eb_error_message(error)); fprintf(stderr, "Failed to set subbook: %s\n", eb_error_message(error));

10
font.c
View File

@ -157,7 +157,7 @@ const Font_Table* font_table_select(const Font_Context* context, const char name
return NULL; return NULL;
} }
void font_stub_encode(char output[], int size, int code, const Font_Table* table, Font_Width width) { void font_stub_encode(char output[], int size, int code, const Font_Table* table, Font_Width width, int flags) {
do { do {
if (table == NULL) { if (table == NULL) {
break; break;
@ -190,8 +190,12 @@ void font_stub_encode(char output[], int size, int code, const Font_Table* table
} }
while (0); while (0);
/* unable to decode, use output the unknown character symbol */ if (flags & FLAG_FONT_TAGS) {
encode_sequence(output, size, "\xef\xbf\xbd"); snprintf(output, size, "{?%.8x}", code);
}
else {
encode_sequence(output, size, "\xef\xbf\xbd");
}
} }
void font_stub_decode(char output[], int size, const char input[]) { void font_stub_decode(char output[], int size, const char input[]) {

2
font.h
View File

@ -64,7 +64,7 @@ void font_context_destroy(Font_Context* context);
const Font_Table* font_table_select(const Font_Context* context, const char name[]); const Font_Table* font_table_select(const Font_Context* context, const char name[]);
void font_stub_encode(char output[], int size, int code, const Font_Table* table, Font_Width width); void font_stub_encode(char output[], int size, int code, const Font_Table* table, Font_Width width, int flags);
void font_stub_decode(char output[], int size, const char input[]); void font_stub_decode(char output[], int size, const char input[]);
#endif /* FONT_H */ #endif /* FONT_H */

11
hooks.c
View File

@ -19,7 +19,6 @@
#include <assert.h> #include <assert.h>
#include "hooks.h" #include "hooks.h"
#include "font.h"
#include "util.h" #include "util.h"
#include "eb/eb/eb.h" #include "eb/eb/eb.h"
@ -112,9 +111,11 @@ static EB_Error_Code hook_narrow_font( /* EB_HOOK_NARROW_FONT */
(void)appendix; (void)appendix;
(void)code; (void)code;
Hook_Params* params = (Hook_Params*)container;
assert(argc > 0); assert(argc > 0);
char stub[MAX_STUB_BYTES]; char stub[MAX_STUB_BYTES];
font_stub_encode(stub, ARRSIZE(stub), argv[0], container, FONT_WIDTH_NARROW); font_stub_encode(stub, ARRSIZE(stub), argv[0], params->table, FONT_WIDTH_NARROW, params->flags);
eb_write_text_string(book, stub); eb_write_text_string(book, stub);
return 0; return 0;
@ -131,9 +132,11 @@ static EB_Error_Code hook_wide_font( /* EB_HOOK_WIDE_FONT */
(void)appendix; (void)appendix;
(void)code; (void)code;
Hook_Params* params = (Hook_Params*)container;
assert(argc > 0); assert(argc > 0);
char stub[MAX_STUB_BYTES]; char stub[MAX_STUB_BYTES];
font_stub_encode(stub, ARRSIZE(stub), argv[0], container, FONT_WIDTH_WIDE); font_stub_encode(stub, ARRSIZE(stub), argv[0], params->table, FONT_WIDTH_WIDE, params->flags);
eb_write_text_string(book, stub); eb_write_text_string(book, stub);
return 0; return 0;
@ -207,7 +210,7 @@ void hooks_install(EB_Hookset* hookset, int flags) {
eb_set_hook(hookset, s_hooks_basic + i); eb_set_hook(hookset, s_hooks_basic + i);
} }
if (flags & FLAG_MARKUP) { if (flags & FLAG_HOOK_MARKUP) {
for (unsigned i = 0; i < ARRSIZE(s_hooks_markup); ++i) { for (unsigned i = 0; i < ARRSIZE(s_hooks_markup); ++i) {
eb_set_hook(hookset, s_hooks_markup + i); eb_set_hook(hookset, s_hooks_markup + i);
} }

12
hooks.h
View File

@ -19,10 +19,20 @@
#ifndef HOOKS_H #ifndef HOOKS_H
#define HOOKS_H #define HOOKS_H
#include "font.h"
#include "eb/eb/eb.h" #include "eb/eb/eb.h"
/* /*
* Exported functions * Types
*/
typedef struct {
const void* table;
int flags;
} Hook_Params;
/*
* Functions
*/ */
void hooks_install(EB_Hookset* hookset, int flags); void hooks_install(EB_Hookset* hookset, int flags);

6
main.c
View File

@ -58,7 +58,10 @@ static error_t argp_parser(int key, char* arg, struct argp_state* state) {
options->flags |= FLAG_PRETTY_PRINT; options->flags |= FLAG_PRETTY_PRINT;
break; break;
case 'm': case 'm':
options->flags |= FLAG_MARKUP; options->flags |= FLAG_HOOK_MARKUP;
break;
case 't':
options->flags |= FLAG_FONT_TAGS;
break; break;
case 's': case 's':
options->flags |= FLAG_POSITIONS; options->flags |= FLAG_POSITIONS;
@ -86,6 +89,7 @@ int main(int argc, char *argv[]) {
{ "pretty-print", 'p', NULL, OPTION_ARG_OPTIONAL, "output pretty-printed JSON", 0 }, { "pretty-print", 'p', NULL, OPTION_ARG_OPTIONAL, "output pretty-printed JSON", 0 },
{ "markup", 'm', NULL, OPTION_ARG_OPTIONAL, "output formatting tags", 0 }, { "markup", 'm', NULL, OPTION_ARG_OPTIONAL, "output formatting tags", 0 },
{ "positions", 's', NULL, OPTION_ARG_OPTIONAL, "output positional data", 0 }, { "positions", 's', NULL, OPTION_ARG_OPTIONAL, "output positional data", 0 },
{ "font-tags", 't', NULL, OPTION_ARG_OPTIONAL, "output missing font data tags", 0 },
{ } { }
}; };

3
util.h
View File

@ -27,8 +27,9 @@
enum { enum {
FLAG_PRETTY_PRINT = 1 << 0, FLAG_PRETTY_PRINT = 1 << 0,
FLAG_MARKUP = 1 << 1, FLAG_HOOK_MARKUP = 1 << 1,
FLAG_POSITIONS = 1 << 2, FLAG_POSITIONS = 1 << 2,
FLAG_FONT_TAGS = 1 << 3,
}; };
#endif /* UTIL_H */ #endif /* UTIL_H */