int main (int argc, char **argv)
{
    HPDF_Doc  pdf;
    HPDF_Page page;
    HPDF_Font def_font;
    HPDF_REAL height;
    HPDF_REAL width;
    HPDF_UINT i;
    int page_num = 1;
    int done = 0;
    char page_number[MAXSTRING];
    int line_num = 1;
    FILE *input_file = NULL;
    for (i = 1; i < argc; i++)
    {
        if (strcmp(argv[i], "-t") == 0)
        {
            strcpy(page_title, argv[++i]);
        }
        else if (strcmp(argv[i], "-o") == 0)
        {
            strcpy(fname, argv[++i]);
        }
        else if (strcmp(argv[i], "-k") == 0)
        {
            page_margin_odd = page_margin_kindle;
            page_margin_even = page_margin_kindle;
        }
        else if (strcmp(argv[i], "-n") == 0)
        {
            no_line_number = 1;
        }
        else if (strcmp(argv[i], "-h")==0)
        {
            usage();
        }
        else if (strcmp(argv[i], "-l") == 0)
        {
            use_landscape = 1;
            page_margin_odd = page_margin_odd_landscape;
            page_margin_even = page_margin_even_landscape;
            LINES_PER_PAGE=50;
        }
        else if (strcmp(argv[i], "-")==0)
        {
            input_file = stdin;
        }
        else
        {
            input_file = fopen(argv[i], "r");
        }
    }

    if (input_file == NULL)
    {
        printf("error: no input specified\n");
        usage();
    }

    pdf = HPDF_New (error_handler, NULL);
    if (!pdf) {
        printf ("error: cannot create PdfDoc object\n");
        return 1;
    }
    if (setjmp(env)) {
        HPDF_Free (pdf);
        return 1;
    }
    while (!done)
    {
        int n = 0;
        char buf[1000];
        char *p;

        while ((p = fgets(buf, 999, input_file)) && n < LINES_PER_PAGE)
        {
            if (buf[0] == '@')
            {
                if (strncmp(buf, "@title ", 7)==0)
                {
                    strcpy(page_title, buf+7);
                }
                else if (strncmp(buf, "@newpage", 8)==0)
                {
                    if (n != 0 && n != LINES_PER_PAGE-1)
                    {
                        line_num += LINES_PER_PAGE;
                        line_num -= line_num % LINES_PER_PAGE;
                        line_num ++;
                        break;
                    }
                }
                continue;
            }
            lines[n].line_num = line_num++;
            strcpy(lines[n].line, buf);
            n++;
        }

        /* If no more input in future, this is the last page,
         * flag as done.
         */
        if (p == NULL)
            done = 1;

        /* If no lines are read, we are done. */
        if (n == 0)
            break;

        /* Add a new page object. */
        page = HPDF_AddPage (pdf);
        HPDF_Page_SetSize (page, HPDF_PAGE_SIZE_A4, use_landscape?HPDF_PAGE_LANDSCAPE:HPDF_PAGE_PORTRAIT); 
        HPDF_Page_SetRGBFill(page, 0, 0, 0);
        
        height = HPDF_Page_GetHeight (page);
        width = HPDF_Page_GetWidth (page);

        page_margin = EVENPAGE(page_num) ? page_margin_even : page_margin_odd;
        page_size.cx = (int)width;
        page_size.cy = (int)height;

        def_font = HPDF_GetFont (pdf, default_font_name, NULL);
        HPDF_Page_SetFontAndSize (page, def_font, 16);
        print_page_header(pdf, page, page_title);
        print_page_content(pdf, page, lines, n);
        sprintf(page_number, "%d", page_num);
        print_page_footer(pdf, page, page_number);
        page_num++;
    }
    if (page_num > 1)
    {
        HPDF_SaveToFile (pdf, fname);
        printf("Output written to %s\n", fname);
    }
    else
    {
        printf("No output\n");
    }
    
    if (input_file != stdin && input_file != NULL)
        fclose(input_file);

    /* clean up */
    HPDF_Free (pdf);
    return 0;
}
Exemple #2
0
int main (int argc, char **argv)
{
	enum ParserState state = 0;
	gunichar in;
	gboolean init = FALSE;
	/* used to parse attribute */
	gint attributes = 0;
	/* Input/output */
	FILE *input = stdin;
	FILE  *output = stdout;
	/* Error */
	GError *error = NULL;

	/* init the glib system  */
	g_type_init();
	/* needed to get the right charset from g_get_charset */
	setlocale(LC_ALL, "");
	/* Get charset */
	g_get_charset(&input_charset);

	/* parse options */
	parse_cmd_options(&argc, &argv);

	/* Handle input file */
	if(input_file && strcmp(input_file, "-")) {
		input = g_fopen(input_file, "r");
		EXCEPTION(input == NULL, "Failed to open: %s: %s\n", input_file, strerror(errno));
	}
	/* Handle output file */
	if(output_file) {
		output = g_fopen(output_file, "w");
		EXCEPTION(output == NULL, "Failed to open: %s: %s\n", output_file, strerror(errno));
	}
	/* Create channel for input */
	GIOChannel *chan = g_io_channel_unix_new(fileno(input));
	/* Set channel encoding */
	g_io_channel_set_encoding(chan, input_charset,&error);
	EXCEPTION(error != NULL, "Failed to set input encoding: %s\n", error->message);

	/* Read input */
	while(g_io_channel_read_unichar(chan, &in, &error) == G_IO_STATUS_NORMAL && error == NULL)
	{
		if(!init) {
			/* Output html header */
			print_page_header(output);
			/* Convert and print body */
			fprintf(output, "<pre style='font-family:monospace'>\n");
			init = TRUE;
		}
		/* If we hit the escape character, go into 'attribute parsing' mode */
		if(in == ESCAPE_CHAR) {
			state = PARSE_ATTRIBUTE;
			/* reset */
			attributes = 0;
		}
		/* if we are in attribute parsing mode, parse attribute */
		else if(state == PARSE_ATTRIBUTE) {
			if(in == '[') {
				/* Begin of attributes */
				state = PARSE_COLOR_ATTRIBUTE;
			}else {
				WARNING("Unknown Escape sequence found: %i\n", in);
				state = PARSE_NORMAL;
			}
		} else if(state == PARSE_COLOR_ATTRIBUTE) {
			if (in == ';') { /* End of element */
				process_attribute(output, attributes);
				attributes = 0;
			} else if(in == 'm') { /* end of attribute */
				process_attribute(output, attributes);
				state = PARSE_NORMAL;
			} else if(in >= '0' && in <= '9') {
				attributes *= 10;
				attributes += in-'0';
			}else if (in == 'h' || in == 'l' ) {
				WARNING("Unsupported attribute found: %i\n", in);
				state = PARSE_NORMAL;
			}
			continue;
		} else if (state == PARSE_NORMAL) {
			/* special chars (htmlspecialchars php doc) */
			if(in == '"') fputs("&quot;", output);
			else if(in == '\'') fputs("&#039;", output);
			else if(in == '&') fputs("&amp;", output);
			else if(in == '<') fputs("&lt;", output);
			else if(in == '>') fputs("&gt;", output);
			/* ascii values stay ascii*/
			else if(in >= 0 && in <= 177)
				fprintf(output, "%c", (char)in);
			/* Rest we encode in utf8 */
			else fprintf(output, "&#%i;", in);
		}
	}
	EXCEPTION(error != NULL, "Failed to read input character: %s\n", error->message);
	if(init) {
		/* Close open tags */
		process_attribute(output, 0);
		fprintf(output,"\n  </pre>\n");
		print_page_footer(output);
	}

	/* free input channel */
	g_io_channel_unref(chan);

	/* close i/o */
	if(input != stdin) fclose(input);
	if(output != stdout) fclose(output);

	return EXIT_SUCCESS;
}