uPDF: remove console tools, some fixes for kolibri.c

git-svn-id: svn://kolibrios.org@8452 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
maxcodehack 2020-12-19 07:53:05 +00:00
parent 132591b85f
commit 855a24edb4
6 changed files with 17 additions and 651 deletions

View File

@ -12,7 +12,7 @@ LIBPATH = -L $(SDK_DIR)/lib -L /home/autobuild/tools/win32/mingw32/lib -L ../lib
APPS_SRC := $(notdir $(wildcard *.c)) APPS_SRC := $(notdir $(wildcard *.c))
default: $(patsubst %.c,%.o,$(APPS_SRC)) default: $(patsubst %.c,%.o,$(APPS_SRC))
$(LD) $(LDFLAGS) $(LIBPATH) -o updf kolibri.o pdfapp.o kos_main.o -lmupdf -lfreetype -lfitz -lgcc -lfitz -ldraw -ljpeg -ljbig2dec -lfreetype -lopenjpeg -lz -lc.dll --subsystem native $(LD) $(LDFLAGS) $(LIBPATH) --subsystem native -o updf kolibri.o pdfapp.o kos_main.o -lmupdf -lfitz -lgcc -lfitz -ldraw -ljpeg -ljbig2dec -lfreetype -lopenjpeg -lz -lc.dll
objcopy updf -O binary objcopy updf -O binary

View File

@ -33,6 +33,10 @@ void kol_wnd_move(unsigned x, unsigned y)
asm volatile ("int $0x40"::"a"(67), "b"(x), "c"(y), "d"(-1), "S"(-1)); asm volatile ("int $0x40"::"a"(67), "b"(x), "c"(y), "d"(-1), "S"(-1));
} }
void kol_wnd_size(unsigned x, unsigned y)
{
asm volatile ("int $0x40"::"a"(67), "b"(-1), "c"(-1), "d"(x), "S"(y));
};
void kol_event_mask(unsigned e) void kol_event_mask(unsigned e)
{ {
@ -485,17 +489,6 @@ void kos_blit(int dstx, int dsty, int w, int h, int srcx,
} }
void kos_move_window(int posx, int posy, int sizex, int sizey)
{
__asm__ __volatile__(
"int $0x40"
::"a"(67),
"b"(posx),
"c"(posy),
"d"(sizex),
"S"(sizey));
};
void kos_text(int x, int y, int color, const char* text, int len) void kos_text(int x, int y, int color, const char* text, int len)
{ {
asm volatile ("int $0x40"::"a"(4),"b"((x<<16) | y),"c"(color),"d"((unsigned long)text),"S"(len)); asm volatile ("int $0x40"::"a"(4),"b"((x<<16) | y),"c"(color),"d"((unsigned long)text),"S"(len));
@ -513,7 +506,7 @@ void kos_screen_max(int* x, int* y)
if(y) *y = v & 0xFFFF; if(y) *y = v & 0xFFFF;
}; };
int kol_get_key() int kos_get_key()
{ {
unsigned short __ret; unsigned short __ret;
asm volatile("int $0x40":"=a"(__ret):"0"(2)); asm volatile("int $0x40":"=a"(__ret):"0"(2));

View File

@ -97,7 +97,11 @@ void *data;
void kol_exit(); void kol_exit();
void kol_sleep(unsigned d); void kol_sleep(unsigned d);
void kol_wnd_define(unsigned x, unsigned y, unsigned w, unsigned h, unsigned cs, unsigned b, char *t); void kol_wnd_define(unsigned x, unsigned y, unsigned w, unsigned h, unsigned cs, unsigned b, char *t);
#define OLD -1
void kol_wnd_move(unsigned x, unsigned y); void kol_wnd_move(unsigned x, unsigned y);
void kol_wnd_size(unsigned x, unsigned y);
void kol_wnd_caption(char *s); void kol_wnd_caption(char *s);
void kol_event_mask(unsigned e); void kol_event_mask(unsigned e);
unsigned kol_event_wait(); unsigned kol_event_wait();
@ -154,22 +158,19 @@ void kol_buffer_close(char name[]);
int kol_clip_num(); int kol_clip_num();
char* kol_clip_get(int n); char* kol_clip_get(int n);
int kol_clip_set(int n, char buffer[]); int kol_clip_set(int n, char buffer[]);
void kol_btn_define(unsigned x, unsigned y, unsigned w, unsigned h, unsigned d, unsigned c);
void kos_blit(int dstx, int dsty, int w, int h, int srcx, void kos_blit(int dstx, int dsty, int w, int h, int srcx,
int srcy,int srcw, int srch, int stride, char *d); int srcy,int srcw, int srch, int stride, char *d);
int kos_random(int num); int kos_random(int num);
int kos_get_mouse_wheels(void); int kos_get_mouse_wheels(void);
void kos_screen_max(int* x, int* y);
#define OLD -1 int kos_get_key();
void kos_move_window(int posx, int posy, int sizex, int sizey);
void kos_text(int x, int y, int color, const char* text, int len); void kos_text(int x, int y, int color, const char* text, int len);
void kol_btn_define(unsigned x, unsigned y, unsigned w, unsigned h, unsigned d, unsigned c);
void kos_screen_max(int* x, int* y);
int kol_get_key();
//// Struct from menuet/os.h //// Struct from menuet/os.h
//// Need rewrite!
typedef unsigned char __u8; typedef unsigned char __u8;
typedef unsigned short __u16; typedef unsigned short __u16;

View File

@ -1,6 +1,5 @@
/*==== INCLUDES ====*/ /*==== INCLUDES ====*/
//#include <menuet/os.h>
#include <kos32sys.h> #include <kos32sys.h>
#include "fitz.h" #include "fitz.h"
#include "mupdf.h" #include "mupdf.h"
@ -415,14 +414,14 @@ int main (int argc, char* argv[])
if (Form.window_state > 2) continue; // if Rolled-up if (Form.window_state > 2) continue; // if Rolled-up
// Minimal size (700x600) // Minimal size (700x600)
if (Form.winx_size < 700) kos_move_window(OLD, OLD, 700, OLD); if (Form.winx_size < 700) kol_wnd_size(700, OLD);
if (Form.winy_size < 600) kos_move_window(OLD, OLD, OLD, 600); if (Form.winy_size < 600) kol_wnd_size(OLD, 600);
DrawMainWindow(); DrawMainWindow();
break; break;
case evKey: case evKey:
key = kol_get_key(); key = kos_get_key();
if (key_mode_enter_page_number) if (key_mode_enter_page_number)
{ {
HandleNewPageNumber(key); HandleNewPageNumber(key);

View File

@ -1,405 +0,0 @@
/*
* pdfdraw -- command line tool for drawing pdf documents
*/
#include "fitz.h"
#include "mupdf.h"
#ifdef _MSC_VER
#include <winsock2.h>
#else
#include <sys/time.h>
#endif
char *output = NULL;
float resolution = 72;
float rotation = 0;
int showxml = 0;
int showtext = 0;
int showtime = 0;
int showmd5 = 0;
int savealpha = 0;
int uselist = 1;
int alphabits = 8;
float gamma_value = 1;
int invert = 0;
fz_colorspace *colorspace;
fz_glyph_cache *glyphcache;
char *filename;
struct {
int count, total;
int min, max;
int minpage, maxpage;
} timing;
static void die(fz_error error)
{
fz_catch(error, "aborting");
exit(1);
}
static void usage(void)
{
fprintf(stderr,
"usage: pdfdraw [options] input.pdf [pages]\n"
"\t-o -\toutput filename (%%d for page number)\n"
"\t\tsupported formats: pgm, ppm, pam, png, pbm\n"
"\t-p -\tpassword\n"
"\t-r -\tresolution in dpi (default: 72)\n"
"\t-A\tdisable accelerated functions\n"
"\t-a\tsave alpha channel (only pam and png)\n"
"\t-b -\tnumber of bits of antialiasing (0 to 8)\n"
"\t-g\trender in grayscale\n"
"\t-m\tshow timing information\n"
"\t-t\tshow text (-tt for xml)\n"
"\t-x\tshow display list\n"
"\t-d\tdisable use of display list\n"
"\t-5\tshow md5 checksums\n"
"\t-R -\trotate clockwise by given number of degrees\n"
"\t-G gamma\tgamma correct output\n"
"\t-I\tinvert output\n"
"\tpages\tcomma separated list of ranges\n");
exit(1);
}
static int gettime(void)
{
static struct timeval first;
static int once = 1;
struct timeval now;
if (once)
{
gettimeofday(&first, NULL);
once = 0;
}
gettimeofday(&now, NULL);
return (now.tv_sec - first.tv_sec) * 1000 + (now.tv_usec - first.tv_usec) / 1000;
}
static int isrange(char *s)
{
while (*s)
{
if ((*s < '0' || *s > '9') && *s != '-' && *s != ',')
return 0;
s++;
}
return 1;
}
static void drawpage(pdf_xref *xref, int pagenum)
{
fz_error error;
pdf_page *page;
fz_display_list *list;
fz_device *dev;
int start;
if (showtime)
{
start = gettime();
}
error = pdf_load_page(&page, xref, pagenum - 1);
if (error)
die(fz_rethrow(error, "cannot load page %d in file '%s'", pagenum, filename));
list = NULL;
if (uselist)
{
list = fz_new_display_list();
dev = fz_new_list_device(list);
error = pdf_run_page(xref, page, dev, fz_identity);
if (error)
die(fz_rethrow(error, "cannot draw page %d in file '%s'", pagenum, filename));
fz_free_device(dev);
}
if (showxml)
{
dev = fz_new_trace_device();
printf("<page number=\"%d\">\n", pagenum);
if (list)
fz_execute_display_list(list, dev, fz_identity, fz_infinite_bbox);
else
pdf_run_page(xref, page, dev, fz_identity);
printf("</page>\n");
fz_free_device(dev);
}
if (showtext)
{
fz_text_span *text = fz_new_text_span();
dev = fz_new_text_device(text);
if (list)
fz_execute_display_list(list, dev, fz_identity, fz_infinite_bbox);
else
pdf_run_page(xref, page, dev, fz_identity);
fz_free_device(dev);
printf("[Page %d]\n", pagenum);
if (showtext > 1)
fz_debug_text_span_xml(text);
else
fz_debug_text_span(text);
printf("\n");
fz_free_text_span(text);
}
if (showmd5 || showtime)
printf("page %s %d", filename, pagenum);
if (output || showmd5 || showtime)
{
float zoom;
fz_matrix ctm;
fz_bbox bbox;
fz_pixmap *pix;
zoom = resolution / 72;
ctm = fz_translate(0, -page->mediabox.y1);
ctm = fz_concat(ctm, fz_scale(zoom, -zoom));
ctm = fz_concat(ctm, fz_rotate(page->rotate));
ctm = fz_concat(ctm, fz_rotate(rotation));
bbox = fz_round_rect(fz_transform_rect(ctm, page->mediabox));
/* TODO: banded rendering and multi-page ppm */
pix = fz_new_pixmap_with_rect(colorspace, bbox);
if (savealpha)
fz_clear_pixmap(pix);
else
fz_clear_pixmap_with_color(pix, 255);
dev = fz_new_draw_device(glyphcache, pix);
if (list)
fz_execute_display_list(list, dev, ctm, bbox);
else
pdf_run_page(xref, page, dev, ctm);
fz_free_device(dev);
if (invert)
fz_invert_pixmap(pix);
if (gamma_value != 1)
fz_gamma_pixmap(pix, gamma_value);
if (output)
{
char buf[512];
sprintf(buf, output, pagenum);
if (strstr(output, ".pgm") || strstr(output, ".ppm") || strstr(output, ".pnm"))
fz_write_pnm(pix, buf);
else if (strstr(output, ".pam"))
fz_write_pam(pix, buf, savealpha);
else if (strstr(output, ".png"))
fz_write_png(pix, buf, savealpha);
else if (strstr(output, ".pbm")) {
fz_halftone *ht = fz_get_default_halftone(1);
fz_bitmap *bit = fz_halftone_pixmap(pix, ht);
fz_write_pbm(bit, buf);
fz_drop_bitmap(bit);
fz_drop_halftone(ht);
}
}
if (showmd5)
{
fz_md5 md5;
unsigned char digest[16];
int i;
fz_md5_init(&md5);
fz_md5_update(&md5, pix->samples, pix->w * pix->h * pix->n);
fz_md5_final(&md5, digest);
printf(" ");
for (i = 0; i < 16; i++)
printf("%02x", digest[i]);
}
fz_drop_pixmap(pix);
}
if (list)
fz_free_display_list(list);
pdf_free_page(page);
if (showtime)
{
int end = gettime();
int diff = end - start;
if (diff < timing.min)
{
timing.min = diff;
timing.minpage = pagenum;
}
if (diff > timing.max)
{
timing.max = diff;
timing.maxpage = pagenum;
}
timing.total += diff;
timing.count ++;
printf(" %dms", diff);
}
if (showmd5 || showtime)
printf("\n");
pdf_age_store(xref->store, 3);
fz_flush_warnings();
}
static void drawrange(pdf_xref *xref, char *range)
{
int page, spage, epage;
char *spec, *dash;
spec = fz_strsep(&range, ",");
while (spec)
{
dash = strchr(spec, '-');
if (dash == spec)
spage = epage = pdf_count_pages(xref);
else
spage = epage = atoi(spec);
if (dash)
{
if (strlen(dash) > 1)
epage = atoi(dash + 1);
else
epage = pdf_count_pages(xref);
}
spage = CLAMP(spage, 1, pdf_count_pages(xref));
epage = CLAMP(epage, 1, pdf_count_pages(xref));
if (spage < epage)
for (page = spage; page <= epage; page++)
drawpage(xref, page);
else
for (page = spage; page >= epage; page--)
drawpage(xref, page);
spec = fz_strsep(&range, ",");
}
}
int main(int argc, char **argv)
{
char *password = "";
int grayscale = 0;
int accelerate = 1;
pdf_xref *xref;
fz_error error;
int c;
while ((c = fz_getopt(argc, argv, "o:p:r:R:Aab:dgmtx5G:I")) != -1)
{
switch (c)
{
case 'o': output = fz_optarg; break;
case 'p': password = fz_optarg; break;
case 'r': resolution = atof(fz_optarg); break;
case 'R': rotation = atof(fz_optarg); break;
case 'A': accelerate = 0; break;
case 'a': savealpha = 1; break;
case 'b': alphabits = atoi(fz_optarg); break;
case 'm': showtime++; break;
case 't': showtext++; break;
case 'x': showxml++; break;
case '5': showmd5++; break;
case 'g': grayscale++; break;
case 'd': uselist = 0; break;
case 'G': gamma_value = atof(fz_optarg); break;
case 'I': invert++; break;
default: usage(); break;
}
}
fz_set_aa_level(alphabits);
if (fz_optind == argc)
usage();
if (!showtext && !showxml && !showtime && !showmd5 && !output)
{
printf("nothing to do\n");
exit(0);
}
if (accelerate)
fz_accelerate();
glyphcache = fz_new_glyph_cache();
colorspace = fz_device_rgb;
if (grayscale)
colorspace = fz_device_gray;
if (output && strstr(output, ".pgm"))
colorspace = fz_device_gray;
if (output && strstr(output, ".ppm"))
colorspace = fz_device_rgb;
if (output && strstr(output, ".pbm"))
colorspace = fz_device_gray;
timing.count = 0;
timing.total = 0;
timing.min = 1 << 30;
timing.max = 0;
timing.minpage = 0;
timing.maxpage = 0;
if (showxml)
printf("<?xml version=\"1.0\"?>\n");
while (fz_optind < argc)
{
filename = argv[fz_optind++];
error = pdf_open_xref(&xref, filename, password);
if (error)
die(fz_rethrow(error, "cannot open document: %s", filename));
error = pdf_load_page_tree(xref);
if (error)
die(fz_rethrow(error, "cannot load page tree: %s", filename));
if (showxml)
printf("<document name=\"%s\">\n", filename);
if (fz_optind == argc || !isrange(argv[fz_optind]))
drawrange(xref, "1-");
if (fz_optind < argc && isrange(argv[fz_optind]))
drawrange(xref, argv[fz_optind++]);
if (showxml)
printf("</document>\n");
pdf_free_xref(xref);
}
if (showtime)
{
printf("total %dms / %d pages for an average of %dms\n",
timing.total, timing.count, timing.total / timing.count);
printf("fastest page %d: %dms\n", timing.minpage, timing.min);
printf("slowest page %d: %dms\n", timing.maxpage, timing.max);
}
fz_free_glyph_cache(glyphcache);
fz_flush_warnings();
return 0;
}

View File

@ -1,222 +0,0 @@
/*
* pdfextract -- the ultimate way to extract images and fonts from pdfs
*/
#include "fitz.h"
#include "mupdf.h"
static pdf_xref *xref = NULL;
static int dorgb = 0;
void die(fz_error error)
{
fz_catch(error, "aborting");
if (xref)
pdf_free_xref(xref);
exit(1);
}
static void usage(void)
{
fprintf(stderr, "usage: pdfextract [options] file.pdf [object numbers]\n");
fprintf(stderr, "\t-p\tpassword\n");
fprintf(stderr, "\t-r\tconvert images to rgb\n");
exit(1);
}
static int isimage(fz_obj *obj)
{
fz_obj *type = fz_dict_gets(obj, "Subtype");
return fz_is_name(type) && !strcmp(fz_to_name(type), "Image");
}
static int isfontdesc(fz_obj *obj)
{
fz_obj *type = fz_dict_gets(obj, "Type");
return fz_is_name(type) && !strcmp(fz_to_name(type), "FontDescriptor");
}
static void saveimage(int num)
{
fz_error error;
fz_pixmap *img;
fz_obj *ref;
char name[1024];
ref = fz_new_indirect(num, 0, xref);
/* TODO: detect DCTD and save as jpeg */
error = pdf_load_image(&img, xref, ref);
if (error)
die(error);
if (dorgb && img->colorspace && img->colorspace != fz_device_rgb)
{
fz_pixmap *temp;
temp = fz_new_pixmap_with_rect(fz_device_rgb, fz_bound_pixmap(img));
fz_convert_pixmap(img, temp);
fz_drop_pixmap(img);
img = temp;
}
if (img->n <= 4)
{
sprintf(name, "img-%04d.png", num);
printf("extracting image %s\n", name);
fz_write_png(img, name, 0);
}
else
{
sprintf(name, "img-%04d.pam", num);
printf("extracting image %s\n", name);
fz_write_pam(img, name, 0);
}
fz_drop_pixmap(img);
fz_drop_obj(ref);
}
static void savefont(fz_obj *dict, int num)
{
fz_error error;
char name[1024];
char *subtype;
fz_buffer *buf;
fz_obj *stream = NULL;
fz_obj *obj;
char *ext = "";
FILE *f;
char *fontname = "font";
int n;
obj = fz_dict_gets(dict, "FontName");
if (obj)
fontname = fz_to_name(obj);
obj = fz_dict_gets(dict, "FontFile");
if (obj)
{
stream = obj;
ext = "pfa";
}
obj = fz_dict_gets(dict, "FontFile2");
if (obj)
{
stream = obj;
ext = "ttf";
}
obj = fz_dict_gets(dict, "FontFile3");
if (obj)
{
stream = obj;
obj = fz_dict_gets(obj, "Subtype");
if (obj && !fz_is_name(obj))
die(fz_throw("Invalid font descriptor subtype"));
subtype = fz_to_name(obj);
if (!strcmp(subtype, "Type1C"))
ext = "cff";
else if (!strcmp(subtype, "CIDFontType0C"))
ext = "cid";
else
die(fz_throw("Unhandled font type '%s'", subtype));
}
if (!stream)
{
fz_warn("Unhandled font type");
return;
}
buf = fz_new_buffer(0);
error = pdf_load_stream(&buf, xref, fz_to_num(stream), fz_to_gen(stream));
if (error)
die(error);
sprintf(name, "%s-%04d.%s", fontname, num, ext);
printf("extracting font %s\n", name);
f = fopen(name, "wb");
if (f == NULL)
die(fz_throw("Error creating font file"));
n = fwrite(buf->data, 1, buf->len, f);
if (n < buf->len)
die(fz_throw("Error writing font file"));
if (fclose(f) < 0)
die(fz_throw("Error closing font file"));
fz_drop_buffer(buf);
}
static void showobject(int num)
{
fz_error error;
fz_obj *obj;
if (!xref)
die(fz_throw("no file specified"));
error = pdf_load_object(&obj, xref, num, 0);
if (error)
die(error);
if (isimage(obj))
saveimage(num);
else if (isfontdesc(obj))
savefont(obj, num);
fz_drop_obj(obj);
}
int main(int argc, char **argv)
{
fz_error error;
char *infile;
char *password = "";
int c, o;
while ((c = fz_getopt(argc, argv, "p:r")) != -1)
{
switch (c)
{
case 'p': password = fz_optarg; break;
case 'r': dorgb++; break;
default: usage(); break;
}
}
if (fz_optind == argc)
usage();
infile = argv[fz_optind++];
error = pdf_open_xref(&xref, infile, password);
if (error)
die(fz_rethrow(error, "cannot open input file '%s'", infile));
if (fz_optind == argc)
{
for (o = 0; o < xref->len; o++)
showobject(o);
}
else
{
while (fz_optind < argc)
{
showobject(atoi(argv[fz_optind]));
fz_optind++;
}
}
pdf_free_xref(xref);
fz_flush_warnings();
return 0;
}