mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
add pdf page reflow
Conflicts: pdfreader.lua
This commit is contained in:
6
Makefile
6
Makefile
@@ -112,13 +112,14 @@ POPENNSLIB := $(POPENNSDIR)/libpopen_noshell.a
|
||||
all: kpdfview
|
||||
|
||||
VERSION?=$(shell git describe HEAD)
|
||||
kpdfview: kpdfview.o einkfb.o pdf.o blitbuffer.o drawcontext.o input.o $(POPENNSLIB) util.o ft.o lfs.o mupdfimg.o $(MUPDFLIBS) $(THIRDPARTYLIBS) $(LUALIB) djvu.o $(DJVULIBS) cre.o $(CRENGINELIBS) pic.o pic_jpeg.o
|
||||
kpdfview: kpdfview.o einkfb.o pdf.o k2pdfopt.o blitbuffer.o drawcontext.o input.o $(POPENNSLIB) util.o ft.o lfs.o mupdfimg.o $(MUPDFLIBS) $(THIRDPARTYLIBS) $(LUALIB) djvu.o $(DJVULIBS) cre.o $(CRENGINELIBS) pic.o pic_jpeg.o
|
||||
echo $(VERSION) > git-rev
|
||||
$(CC) \
|
||||
$(CFLAGS) \
|
||||
kpdfview.o \
|
||||
einkfb.o \
|
||||
pdf.o \
|
||||
k2pdfopt.o \
|
||||
blitbuffer.o \
|
||||
drawcontext.o \
|
||||
input.o \
|
||||
@@ -155,6 +156,9 @@ ft.o: %.o: %.c $(THIRDPARTYLIBS)
|
||||
kpdfview.o pdf.o blitbuffer.o util.o drawcontext.o einkfb.o input.o mupdfimg.o: %.o: %.c
|
||||
$(CC) -c $(KPDFREADER_CFLAGS) $(EMU_CFLAGS) -I$(LFSDIR)/src $< -o $@
|
||||
|
||||
k2pdfopt.o: %.o: %.c
|
||||
$(CC) -c -I$(MUPDFDIR)/ $(CFLAGS) $< -o $@
|
||||
|
||||
djvu.o: %.o: %.c
|
||||
$(CC) -c $(KPDFREADER_CFLAGS) -I$(DJVUDIR)/ $< -o $@
|
||||
|
||||
|
||||
6338
k2pdfopt.c
Normal file
6338
k2pdfopt.c
Normal file
File diff suppressed because it is too large
Load Diff
33
k2pdfopt.h
Normal file
33
k2pdfopt.h
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
** k2pdfopt.h K2pdfopt optimizes PDF/DJVU files for mobile e-readers
|
||||
** (e.g. the Kindle) and smartphones. It works well on
|
||||
** multi-column PDF/DJVU files. K2pdfopt is freeware.
|
||||
**
|
||||
** Copyright (C) 2012 http://willus.com
|
||||
**
|
||||
** This program is free software: you can redistribute it and/or modify
|
||||
** it under the terms of the GNU Affero General Public License as
|
||||
** published by the Free Software Foundation, either version 3 of the
|
||||
** License, or (at your option) any later version.
|
||||
**
|
||||
** This program is distributed in the hope that it will be useful,
|
||||
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
** GNU Affero General Public License for more details.
|
||||
**
|
||||
** You should have received a copy of the GNU Affero General Public License
|
||||
** along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
**
|
||||
*/
|
||||
|
||||
#ifndef _K2PDFOPT_H
|
||||
#define _K2PDFOPT_H
|
||||
|
||||
#include <fitz/fitz-internal.h>
|
||||
|
||||
void k2pdfopt_mupdf_reflow_bmp(fz_context *ctx, fz_pixmap *pix, double rot_deg);
|
||||
void k2pdfopt_mupdf_rfbmp_size(int *width, int *height);
|
||||
void k2pdfopt_mupdf_rfbmp_ptr(unsigned char** bmp_ptr_ptr);
|
||||
|
||||
#endif
|
||||
|
||||
93
pdf.c
93
pdf.c
@@ -20,6 +20,7 @@
|
||||
#include "blitbuffer.h"
|
||||
#include "drawcontext.h"
|
||||
#include "pdf.h"
|
||||
#include "k2pdfopt.h"
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <stddef.h>
|
||||
@@ -511,6 +512,96 @@ static int closePage(lua_State *L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int reflowPage(lua_State *L) {
|
||||
fz_context *ctx;
|
||||
fz_device *dev;
|
||||
fz_pixmap *pix;
|
||||
fz_rect bounds,bounds2;
|
||||
fz_matrix ctm;
|
||||
fz_bbox bbox;
|
||||
|
||||
PdfPage *page = (PdfPage*) luaL_checkudata(L, 1, "pdfpage");
|
||||
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 2, "drawcontext");
|
||||
|
||||
double dpi = 200;
|
||||
double dpp;
|
||||
dpp = dpi / 72.;
|
||||
pix = NULL;
|
||||
fz_var(pix);
|
||||
bounds = fz_bound_page(page->doc->xref, page->page);
|
||||
ctm = fz_scale(dpp, dpp);
|
||||
// ctm=fz_concat(ctm,fz_rotate(rotation));
|
||||
bounds2 = fz_transform_rect(ctm, bounds);
|
||||
bbox = fz_round_rect(bounds2);
|
||||
// ctm=fz_translate(0,-page->mediabox.y1);
|
||||
// ctm=fz_concat(ctm,fz_scale(dpp,-dpp));
|
||||
// ctm=fz_concat(ctm,fz_rotate(page->rotate));
|
||||
// ctm=fz_concat(ctm,fz_rotate(0));
|
||||
// bbox=fz_round_rect(fz_transform_rect(ctm,page->mediabox));
|
||||
// pix=fz_new_pixmap_with_rect(colorspace,bbox);
|
||||
pix = fz_new_pixmap_with_bbox(page->doc->context, fz_device_gray, bbox);
|
||||
printf("bbox:%d,%d,%d,%d\n",bbox.x0,bbox.y0,bbox.x1,bbox.y1);
|
||||
fz_clear_pixmap_with_value(page->doc->context, pix, 0xff);
|
||||
dev = fz_new_draw_device(page->doc->context, pix);
|
||||
#ifdef MUPDF_TRACE
|
||||
fz_device *tdev;
|
||||
fz_try(page->doc->context) {
|
||||
tdev = fz_new_trace_device(page->doc->context);
|
||||
fz_run_page(page->doc->xref, page->page, tdev, ctm, NULL);
|
||||
}
|
||||
fz_always(page->doc->context) {
|
||||
fz_free_device(tdev);
|
||||
}
|
||||
#endif
|
||||
fz_run_page(page->doc->xref, page->page, dev, ctm, NULL);
|
||||
fz_free_device(dev);
|
||||
|
||||
if(dc->gamma >= 0.0) {
|
||||
fz_gamma_pixmap(page->doc->context, pix, dc->gamma);
|
||||
}
|
||||
int width, height;
|
||||
k2pdfopt_mupdf_reflow_bmp(page->doc->context, pix, 0);
|
||||
k2pdfopt_mupdf_rfbmp_size(&width, &height);
|
||||
|
||||
lua_pushnumber(L, (double)width);
|
||||
lua_pushnumber(L, (double)height);
|
||||
|
||||
fz_drop_pixmap(page->doc->context, pix);
|
||||
|
||||
return 2;
|
||||
}
|
||||
|
||||
static int drawReflowedPage(lua_State *L) {
|
||||
static unsigned char *bmptr = NULL;
|
||||
|
||||
PdfPage *page = (PdfPage*) luaL_checkudata(L, 1, "pdfpage");
|
||||
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 2, "drawcontext");
|
||||
BlitBuffer *bb = (BlitBuffer*) luaL_checkudata(L, 3, "blitbuffer");
|
||||
k2pdfopt_mupdf_rfbmp_ptr(&bmptr);
|
||||
|
||||
uint8_t *bbptr = (uint8_t*)bb->data;
|
||||
uint8_t *pmptr = (uint8_t*)bmptr;
|
||||
|
||||
int x_offset = 0;
|
||||
int y_offset = 0;
|
||||
|
||||
bbptr += bb->pitch * y_offset;
|
||||
int x, y;
|
||||
for(y = y_offset; y < bb->h; y++) {
|
||||
for(x = x_offset/2; x < (bb->w/2); x++) {
|
||||
int p = x*2 - x_offset;
|
||||
bbptr[x] = (((pmptr[p + 1] & 0xF0) >> 4) | (pmptr[p] & 0xF0)) ^ 0xFF;
|
||||
}
|
||||
bbptr += bb->pitch;
|
||||
pmptr += bb->w;
|
||||
if (bb->w & 1) {
|
||||
bbptr[x] = 255 - (pmptr[x*2] & 0xF0);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int drawPage(lua_State *L) {
|
||||
fz_pixmap *pix;
|
||||
fz_device *dev;
|
||||
@@ -657,6 +748,8 @@ static const struct luaL_Reg pdfpage_meth[] = {
|
||||
{"getPageLinks", getPageLinks},
|
||||
{"close", closePage},
|
||||
{"__gc", closePage},
|
||||
{"reflow", reflowPage},
|
||||
{"rfdraw", drawReflowedPage},
|
||||
{"draw", drawPage},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user