1
This commit is contained in:
Alex Yatskov 2016-11-28 21:16:43 -08:00
parent 71524395f6
commit 49b9497087
7 changed files with 56 additions and 56 deletions

View File

@ -2,5 +2,5 @@ cmake_minimum_required(VERSION 3.5)
project(zero-epwing) project(zero-epwing)
include_directories(eb) include_directories(eb)
link_directories(eb/eb/.libs jansson/lib) link_directories(eb/eb/.libs jansson/lib)
add_executable(zero-epwing main.c book.c gaiji.c convert.c hooks.c) add_executable(zero-epwing main.c book.c font.c convert.c hooks.c)
target_link_libraries(zero-epwing libeb.a libz.a libjansson.a) target_link_libraries(zero-epwing libeb.a libz.a libjansson.a)

14
book.c
View File

@ -47,7 +47,7 @@ static char* book_read(
EB_Hookset* hookset, EB_Hookset* hookset,
const EB_Position* position, const EB_Position* position,
Book_Mode mode, Book_Mode mode,
const Gaiji_Table* table const Font_Table* table
) { ) {
if (eb_seek_text(book, position) != EB_SUCCESS) { if (eb_seek_text(book, position) != EB_SUCCESS) {
return NULL; return NULL;
@ -93,7 +93,7 @@ static char* book_read(
return NULL; return NULL;
} }
gaiji_stub_decode(result, strlen(result) + 1, result); font_stub_decode(result, strlen(result) + 1, result);
return result; return result;
} }
@ -139,7 +139,7 @@ static void book_encode(Book* book, json_t* book_json) {
json_decref(subbook_json_array); json_decref(subbook_json_array);
} }
static void subbook_entries_export(Book_Subbook* subbook, EB_Book* eb_book, EB_Hookset* eb_hookset, const Gaiji_Table* table) { static void subbook_entries_export(Book_Subbook* subbook, EB_Book* eb_book, EB_Hookset* eb_hookset, const Font_Table* table) {
if (subbook->entry_capacity == 0) { if (subbook->entry_capacity == 0) {
subbook->entry_capacity = 16384; subbook->entry_capacity = 16384;
subbook->entries = malloc(subbook->entry_capacity * sizeof(Book_Entry)); subbook->entries = malloc(subbook->entry_capacity * sizeof(Book_Entry));
@ -169,12 +169,12 @@ static void subbook_entries_export(Book_Subbook* subbook, EB_Book* eb_book, EB_H
while (hit_count > 0); while (hit_count > 0);
} }
static void subbook_export(Book_Subbook* subbook, const Gaiji_Context* context, EB_Book* eb_book, EB_Hookset* eb_hookset) { static void subbook_export(Book_Subbook* subbook, const Font_Context* context, EB_Book* eb_book, EB_Hookset* eb_hookset) {
const Gaiji_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) {
subbook->title = eucjp_to_utf8(title); subbook->title = eucjp_to_utf8(title);
table = gaiji_table_select(context, subbook->title); table = font_table_select(context, subbook->title);
} }
if (eb_have_copyright(eb_book)) { if (eb_have_copyright(eb_book)) {
@ -236,7 +236,7 @@ bool book_dump(Book* book, bool pretty_print, FILE* fp) {
} }
bool book_export(Book* book, const Gaiji_Context* context, const char path[], bool markup) { bool book_export(Book* book, const Font_Context* context, const char path[], bool markup) {
EB_Error_Code error; EB_Error_Code error;
if ((error = eb_initialize_library()) != EB_SUCCESS) { if ((error = eb_initialize_library()) != EB_SUCCESS) {
fprintf(stderr, "Failed to initialize library: %s\n", eb_error_message(error)); fprintf(stderr, "Failed to initialize library: %s\n", eb_error_message(error));

4
book.h
View File

@ -22,7 +22,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include "gaiji.h" #include "font.h"
/* /*
* Types * Types
@ -54,7 +54,7 @@ typedef struct {
void book_init(Book* book); void book_init(Book* book);
void book_free(Book* book); void book_free(Book* book);
bool book_export(Book* book, const Gaiji_Context* context, const char path[], bool markup); bool book_export(Book* book, const Font_Context* context, const char path[], bool markup);
bool book_dump(Book* book, bool pretty_print, FILE* fp); bool book_dump(Book* book, bool pretty_print, FILE* fp);
#endif /* BOOK_H */ #endif /* BOOK_H */

View File

@ -24,7 +24,7 @@
#include "jansson/include/jansson.h" #include "jansson/include/jansson.h"
#include "util.h" #include "util.h"
#include "gaiji.h" #include "font.h"
/* /*
* Local functions * Local functions
@ -82,7 +82,7 @@ static void encode_sequence(char output[], int size, const char utf8[]) {
strncat(output, "}", size); strncat(output, "}", size);
} }
static void parse_entry(Gaiji_Entry* entry, const json_t* entry_json) { static void parse_entry(Font_Entry* entry, const json_t* entry_json) {
entry->code = json_integer_value(json_array_get(entry_json, 0)); entry->code = json_integer_value(json_array_get(entry_json, 0));
const char* utf8 = json_string_value(json_array_get(entry_json, 1)); const char* utf8 = json_string_value(json_array_get(entry_json, 1));
if (utf8 == NULL) { if (utf8 == NULL) {
@ -94,20 +94,20 @@ static void parse_entry(Gaiji_Entry* entry, const json_t* entry_json) {
} }
} }
static void parse_entries(Gaiji_Entry** entries, int* count, const json_t* entry_array_json) { static void parse_entries(Font_Entry** entries, int* count, const json_t* entry_array_json) {
*count = json_array_size(entry_array_json); *count = json_array_size(entry_array_json);
if (*count == 0) { if (*count == 0) {
*entries = NULL; *entries = NULL;
} }
else { else {
*entries = malloc(sizeof(Gaiji_Entry) * *count); *entries = malloc(sizeof(Font_Entry) * *count);
for (int i = 0; i < *count; ++i) { for (int i = 0; i < *count; ++i) {
parse_entry(*entries + i, json_array_get(entry_array_json, i)); parse_entry(*entries + i, json_array_get(entry_array_json, i));
} }
} }
} }
static void parse_table(Gaiji_Table* table, const json_t* table_json) { static void parse_table(Font_Table* table, const json_t* table_json) {
const char* name = json_string_value(json_object_get(table_json, "name")); const char* name = json_string_value(json_object_get(table_json, "name"));
if (name == NULL) { if (name == NULL) {
*table->name = 0; *table->name = 0;
@ -118,25 +118,25 @@ static void parse_table(Gaiji_Table* table, const json_t* table_json) {
} }
parse_entries( parse_entries(
(Gaiji_Entry**)&table->table_wide, (Font_Entry**)&table->table_wide,
&table->table_wide_size, &table->table_wide_size,
json_object_get(table_json, "wide") json_object_get(table_json, "wide")
); );
parse_entries( parse_entries(
(Gaiji_Entry**)&table->table_narrow, (Font_Entry**)&table->table_narrow,
&table->table_narrow_size, &table->table_narrow_size,
json_object_get(table_json, "narrow") json_object_get(table_json, "narrow")
); );
} }
static void parse_table_array(Gaiji_Context* context, const json_t* table_array_json) { static void parse_table_array(Font_Context* context, const json_t* table_array_json) {
context->table_count = json_array_size(table_array_json); context->table_count = json_array_size(table_array_json);
if (context->table_count == 0) { if (context->table_count == 0) {
context->tables = NULL; context->tables = NULL;
} }
else { else {
context->tables = malloc(sizeof(Gaiji_Table) * context->table_count); context->tables = malloc(sizeof(Font_Table) * context->table_count);
for (int i = 0; i < context->table_count; ++i) { for (int i = 0; i < context->table_count; ++i) {
parse_table(context->tables + i, json_array_get(table_array_json, i)); parse_table(context->tables + i, json_array_get(table_array_json, i));
} }
@ -147,9 +147,9 @@ static void parse_table_array(Gaiji_Context* context, const json_t* table_array_
* Exported functions * Exported functions
*/ */
const Gaiji_Table* gaiji_table_select(const Gaiji_Context* context, const char name[]) { const Font_Table* font_table_select(const Font_Context* context, const char name[]) {
for (int i = 0; i < context->table_count; ++i) { for (int i = 0; i < context->table_count; ++i) {
const Gaiji_Table* table = context->tables + i; const Font_Table* table = context->tables + i;
if (strstr(name, table->name) != NULL) { if (strstr(name, table->name) != NULL) {
return table; return table;
} }
@ -158,21 +158,21 @@ const Gaiji_Table* gaiji_table_select(const Gaiji_Context* context, const char n
return NULL; return NULL;
} }
void gaiji_stub_encode(char output[], int size, int code, const Gaiji_Table* table, Gaiji_Width width) { void font_stub_encode(char output[], int size, int code, const Font_Table* table, Font_Width width) {
do { do {
if (table == NULL) { if (table == NULL) {
break; break;
} }
const Gaiji_Entry* entries = NULL; const Font_Entry* entries = NULL;
int count = 0; int count = 0;
switch (width) { switch (width) {
case GAIJI_WIDTH_WIDE: case FONT_WIDTH_WIDE:
entries = table->table_wide; entries = table->table_wide;
count = table->table_wide_size; count = table->table_wide_size;
break; break;
case GAIJI_WIDTH_NARROW: case FONT_WIDTH_NARROW:
entries = table->table_narrow; entries = table->table_narrow;
count = table->table_narrow_size; count = table->table_narrow_size;
break; break;
@ -181,7 +181,7 @@ void gaiji_stub_encode(char output[], int size, int code, const Gaiji_Table* tab
assert(entries != NULL); assert(entries != NULL);
for (int i = 0; i < count; ++i) { for (int i = 0; i < count; ++i) {
const Gaiji_Entry* entry = entries + i; const Font_Entry* entry = entries + i;
if (entry->code == code) { if (entry->code == code) {
encode_sequence(output, size, entry->utf8); encode_sequence(output, size, entry->utf8);
return; return;
@ -195,7 +195,7 @@ void gaiji_stub_encode(char output[], int size, int code, const Gaiji_Table* tab
output[size - 1] = 0; output[size - 1] = 0;
} }
void gaiji_stub_decode(char output[], int size, const char input[]) { void font_stub_decode(char output[], int size, const char input[]) {
const char* ptr_in = input; const char* ptr_in = input;
char* ptr_out = output; char* ptr_out = output;
bool decode = false; bool decode = false;
@ -235,7 +235,7 @@ void gaiji_stub_decode(char output[], int size, const char input[]) {
*ptr_out = 0; *ptr_out = 0;
} }
bool gaiji_context_init(Gaiji_Context* context, const char path[]) { bool font_context_init(Font_Context* context, const char path[]) {
context->table_count = 0; context->table_count = 0;
context->tables = NULL; context->tables = NULL;
@ -254,11 +254,11 @@ bool gaiji_context_init(Gaiji_Context* context, const char path[]) {
return true; return true;
} }
void gaiji_context_destroy(Gaiji_Context* context) { void font_context_destroy(Font_Context* context) {
for (int i = 0; i < context->table_count; ++i) { for (int i = 0; i < context->table_count; ++i) {
Gaiji_Table* table = context->tables + i; Font_Table* table = context->tables + i;
free((Gaiji_Entry*)table->table_wide); free((Font_Entry*)table->table_wide);
free((Gaiji_Entry*)table->table_narrow); free((Font_Entry*)table->table_narrow);
} }
free(context->tables); free(context->tables);

View File

@ -16,8 +16,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef GAIJI_H #ifndef FONT_H
#define GAIJI_H #define FONT_H
#include <stdbool.h> #include <stdbool.h>
@ -35,36 +35,36 @@
typedef struct { typedef struct {
int code; int code;
char utf8[MAX_UTF8_BYTES]; char utf8[MAX_UTF8_BYTES];
} Gaiji_Entry; } Font_Entry;
typedef struct { typedef struct {
char name[256]; char name[256];
const Gaiji_Entry* table_wide; const Font_Entry* table_wide;
int table_wide_size; int table_wide_size;
const Gaiji_Entry* table_narrow; const Font_Entry* table_narrow;
int table_narrow_size; int table_narrow_size;
} Gaiji_Table; } Font_Table;
typedef struct { typedef struct {
Gaiji_Table* tables; Font_Table* tables;
int table_count; int table_count;
} Gaiji_Context; } Font_Context;
typedef enum { typedef enum {
GAIJI_WIDTH_WIDE, FONT_WIDTH_WIDE,
GAIJI_WIDTH_NARROW, FONT_WIDTH_NARROW,
} Gaiji_Width; } Font_Width;
/* /*
* Functions * Functions
*/ */
bool gaiji_context_init(Gaiji_Context* context, const char path[]); bool font_context_init(Font_Context* context, const char path[]);
void gaiji_context_destroy(Gaiji_Context* context); void font_context_destroy(Font_Context* context);
const Gaiji_Table* gaiji_table_select(const Gaiji_Context* context, const char name[]); const Font_Table* font_table_select(const Font_Context* context, const char name[]);
void gaiji_stub_encode(char output[], int size, int code, const Gaiji_Table* table, Gaiji_Width width); void font_stub_encode(char output[], int size, int code, const Font_Table* table, Font_Width width);
void gaiji_stub_decode(char output[], int size, const char input[]); void font_stub_decode(char output[], int size, const char input[]);
#endif /* GAIJI_H */ #endif /* FONT_H */

View File

@ -19,7 +19,7 @@
#include <assert.h> #include <assert.h>
#include "hooks.h" #include "hooks.h"
#include "gaiji.h" #include "font.h"
#include "util.h" #include "util.h"
#include "eb/eb/eb.h" #include "eb/eb/eb.h"
@ -114,7 +114,7 @@ static EB_Error_Code hook_narrow_font( /* EB_HOOK_NARROW_FONT */
assert(argc > 0); assert(argc > 0);
char stub[MAX_STUB_BYTES]; char stub[MAX_STUB_BYTES];
gaiji_stub_encode(stub, ARRSIZE(stub), argv[0], container, GAIJI_WIDTH_NARROW); font_stub_encode(stub, ARRSIZE(stub), argv[0], container, FONT_WIDTH_NARROW);
eb_write_text_string(book, stub); eb_write_text_string(book, stub);
return 0; return 0;
@ -133,7 +133,7 @@ static EB_Error_Code hook_wide_font( /* EB_HOOK_WIDE_FONT */
assert(argc > 0); assert(argc > 0);
char stub[MAX_STUB_BYTES]; char stub[MAX_STUB_BYTES];
gaiji_stub_encode(stub, ARRSIZE(stub), argv[0], container, GAIJI_WIDTH_WIDE); font_stub_encode(stub, ARRSIZE(stub), argv[0], container, FONT_WIDTH_WIDE);
eb_write_text_string(book, stub); eb_write_text_string(book, stub);
return 0; return 0;

8
main.c
View File

@ -22,7 +22,7 @@
#include "util.h" #include "util.h"
#include "book.h" #include "book.h"
#include "gaiji.h" #include "font.h"
/* /*
* Local types * Local types
@ -99,8 +99,8 @@ int main(int argc, char *argv[]) {
Options options = { }; Options options = { };
argp_parse(&argp, argc, argv, 0, 0, &options); argp_parse(&argp, argc, argv, 0, 0, &options);
Gaiji_Context context; Font_Context context;
if (!gaiji_context_init(&context, *options.font_path == 0 ? NULL : options.font_path)) { if (!font_context_init(&context, *options.font_path == 0 ? NULL : options.font_path)) {
return 1; return 1;
} }
@ -113,6 +113,6 @@ int main(int argc, char *argv[]) {
book_free(&book); book_free(&book);
gaiji_context_destroy(&context); font_context_destroy(&context);
return success ? 0 : 1; return success ? 0 : 1;
} }