Skip to content

Commit cc6db62

Browse files
author
greatriver
committed
Optimize output string module
1 parent 14884d7 commit cc6db62

9 files changed

Lines changed: 25 additions & 12 deletions

File tree

include/hdtd/context.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ void hd_free(hd_context *ctx, void *p);
214214
*/
215215
void hd_flush_warnings(hd_context *ctx);
216216

217+
enum {
218+
HD_DEFAULT_CONTENT_SIZE = 256,
219+
HD_DEFAULT_EXTRACT_SIZE = 128,
220+
};
221+
217222
struct hd_context_s
218223
{
219224
void *user;
@@ -225,7 +230,8 @@ struct hd_context_s
225230

226231
//Extracted contents
227232
unsigned int flush_size;
228-
unsigned char contents[256];
233+
unsigned int buf_pos;
234+
unsigned char contents[HD_DEFAULT_CONTENT_SIZE];
229235
};
230236

231237
enum {

include/hdtd/document.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ typedef void (hd_page_drop_page_fn)(hd_context *ctx, hd_page *page);
2727
contents of a page. See hd_run_page_contents for more
2828
information.
2929
*/
30-
typedef void (hd_page_run_page_contents_fn)(hd_context *ctx, hd_page *page, char* buffer);
30+
typedef void (hd_page_run_page_contents_fn)(hd_context *ctx, hd_page *page, char* buffer, uint32_t *extract_len);
3131

3232
/*
3333
Structure definition is public so other classes can
@@ -167,6 +167,6 @@ void hd_drop_document(hd_context *ctx, hd_document *doc);
167167
*/
168168
hd_page *hd_load_page(hd_context *ctx, hd_document *doc, int number);
169169

170-
void hd_run_page_contents(hd_context *ctx, hd_page *page, char* buf);
170+
void hd_run_page_contents(hd_context *ctx, hd_page *page, char* buf, uint32_t *extract_len);
171171

172172
#endif //HDCONTENTS_HDTD_DOCUMENT_H

include/pdf/page.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pdf_obj *pdf_page_resources(hd_context *ctx, pdf_page *page);
2525
pdf_run_page_contents: Interpret a loaded page and render it on a device.
2626
Just the main page contents without the annotations
2727
*/
28-
void pdf_run_page_contents(hd_context *ctx, pdf_page *page, char* buf);
28+
void pdf_run_page_contents(hd_context *ctx, pdf_page *page, char* buf, uint32_t *extract_len);
2929

3030
/*
3131
* Page tree, pages and related objects

main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,14 @@ int main() {
106106

107107
hd_page *page = hd_load_page(ctx, doc, 0);
108108
char buf[512] = {0};
109-
109+
uint32_t extract_len = 0;
110110
hd_try(ctx)
111111
{
112-
hd_run_page_contents(ctx, page, buf);
112+
hd_run_page_contents(ctx, page, buf, &extract_len);
113113
unsigned char filenameUtf8[128];
114114
memset(filenameUtf8, 0, 128);
115115
unsigned int len;
116-
len = (ctx->flush_size > 32) ? 32 : ctx->flush_size;
116+
len = (extract_len > 32) ? 32 : extract_len;
117117
if (len > 0)
118118
{
119119
unicode_to_utf8(filenameUtf8, (hd_wchar_t* )buf, len);

source/hdtd/context.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ hd_new_context_imp(const hd_alloc_context *alloc, size_t max_store, const char *
8686
return NULL;
8787

8888
ctx->flush_size = 0;
89+
ctx->buf_pos = 0;
8990
memset(ctx->contents, 0, sizeof(ctx->contents));
9091

9192
/* Now initialise sections that are shared */

source/hdtd/document.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,13 @@ hd_new_page_of_size(hd_context *ctx, int size)
139139
}
140140

141141
void
142-
hd_run_page_contents(hd_context *ctx, hd_page *page, char* buf)
142+
hd_run_page_contents(hd_context *ctx, hd_page *page, char* buf, uint32_t *extract_len)
143143
{
144144
if (page && page->run_page_contents && page)
145145
{
146146
hd_try(ctx)
147147
{
148-
page->run_page_contents(ctx, page, buf);
148+
page->run_page_contents(ctx, page, buf, extract_len);
149149
}
150150
hd_catch(ctx)
151151
{

source/pdf/pdf-interpret.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ pdf_process_stream(hd_context *ctx, pdf_processor *proc, pdf_csi *csi, hd_stream
159159
pdf_token tok = PDF_TOK_ERROR;
160160
//whether to enter []
161161
int in_text_array = 0;
162-
int syntax_errors = 0;
162+
//int syntax_errors = 0;
163163

164164
/* make sure we have a clean slate if we come here from flush_text */
165165
pdf_clear_stack(ctx, csi);
@@ -173,6 +173,8 @@ pdf_process_stream(hd_context *ctx, pdf_processor *proc, pdf_csi *csi, hd_stream
173173
{
174174
do
175175
{
176+
if (ctx->buf_pos >= HD_DEFAULT_EXTRACT_SIZE)
177+
return;
176178
tok = pdf_lex(ctx, stm, buf);
177179
if (in_text_array)
178180
{

source/pdf/pdf-op-run.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ pdf_show_char(hd_context *ctx, pdf_run_processor *pr, int cid)
3535
ucslen = 1;
3636
}
3737

38-
if ((ctx->flush_size < 62) && cid > 0 && ucslen > 0)
38+
if ((ctx->buf_pos < HD_DEFAULT_EXTRACT_SIZE) && cid > 0 && ucslen > 0)
3939
{
4040
hd_wchar_t *wc = (hd_wchar_t *)&ucsbuf[0];
41+
ctx->buf_pos += 2;
4142
switch (*wc)
4243
{
4344
case '/':

source/pdf/pdf-run.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,18 @@ pdf_run_page_contents_with_usage(hd_context *ctx, pdf_document *doc, pdf_page *p
3737
hd_rethrow(ctx);
3838
}
3939

40-
void pdf_run_page_contents(hd_context *ctx, pdf_page *page, char* buf)
40+
void pdf_run_page_contents(hd_context *ctx, pdf_page *page, char* buf, uint32_t *extract_len)
4141
{
4242
pdf_document *doc = page->doc;
4343

4444
hd_try(ctx)
4545
{
4646
pdf_run_page_contents_with_usage(ctx, doc, page);
4747
if (ctx->flush_size > 1)
48+
{
49+
*extract_len = ctx->flush_size;
4850
memcpy(buf, ctx->contents, ctx->flush_size);
51+
}
4952
}
5053
hd_catch(ctx)
5154
{

0 commit comments

Comments
 (0)