1
0

add mupdf demo

This commit is contained in:
2025-05-20 21:24:57 +08:00
parent 1129ac8dfc
commit a589f12bd2
3 changed files with 95 additions and 2 deletions

View File

@@ -7,7 +7,7 @@ OBJS = $(SRCS:.c=.o)
CC := gcc CC := gcc
CFLAGS := -O2 -Wall $(INCLUDES) $(shell pkg-config --cflags glfw3) CFLAGS := -O2 -Wall $(INCLUDES) $(shell pkg-config --cflags glfw3)
CXXFLAGS := $(CFLAGS) -std=c++17 CXXFLAGS := $(CFLAGS) -std=c++17
LDFLAGS := $(shell pkg-config glfw3 --libs) -lgdi32 LDFLAGS := -lmupdf -lmupdf-third $(shell pkg-config glfw3 --libs) -lz -lgdi32
all: $(TARGET) all: $(TARGET)

91
main.c
View File

@@ -1,6 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <mupdf/fitz.h>
#define IMGL3W_IMPL #define IMGL3W_IMPL
#include "opengl3_loader.h" #include "opengl3_loader.h"
#define GLFW_INCLUDE_NONE #define GLFW_INCLUDE_NONE
@@ -23,6 +24,48 @@ extern "C" void userAppExit()
} }
#endif #endif
const char *vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
static const GLfloat vertices[] = {
0.f, 0.f,
1.f, 0.f,
1.f, 1.f,
0.f, 1.f,
};
struct texture
{
GLuint id;
int x, y, w, h;
};
void ui_texture_from_pixmap(struct texture *tex, fz_pixmap *pix)
{
if (!tex->id)
glGenTextures(1, &tex->id);
glBindTexture(GL_TEXTURE_2D, tex->id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
tex->x = pix->x;
tex->y = pix->y;
tex->w = pix->w;
tex->h = pix->h;
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex->w, tex->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, pix->samples);
glBindTexture(GL_TEXTURE_2D, 0);
}
void ui_draw_image(struct texture *tex, float x, float y)
{
}
static void die(const char *msg) static void die(const char *msg)
{ {
fprintf(stderr, "%s\n", msg); fprintf(stderr, "%s\n", msg);
@@ -32,6 +75,13 @@ static void die(const char *msg)
// Main code // Main code
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
if (argc < 2)
{
fprintf(stderr, "usage: example input-file\n");
return 0;
}
char *input = argv[1];
if (!glfwInit()) if (!glfwInit())
die("init glfw failed"); die("init glfw failed");
@@ -39,8 +89,40 @@ int main(int argc, char *argv[])
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
/* Create a context to hold the exception stack and various caches. */
fz_document *doc;
fz_context *ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
if (!ctx)
die("cannot create mupdf context");
fz_try(ctx)
fz_register_document_handlers(ctx);
fz_catch(ctx)
{
fprintf(stderr, "cannot register document handlers\n");
fz_drop_context(ctx);
return EXIT_FAILURE;
}
fz_try(ctx)
doc = fz_open_document(ctx, input);
fz_catch(ctx)
{
fprintf(stderr, "cannot open document\n");
fz_drop_context(ctx);
return EXIT_FAILURE;
}
struct texture page_tex = {0};
int page_count = fz_count_pages(ctx, doc);
printf("page_count %d\n", page_count);
fz_matrix ctm = fz_scale(1.f, 1.f);
fz_pixmap *pix = fz_new_pixmap_from_page_number(ctx, doc, 0, ctm, fz_device_rgb(ctx), 1);
ui_texture_from_pixmap(&page_tex, pix);
// Create window with graphics context // Create window with graphics context
GLFWwindow *window = glfwCreateWindow(1280, 720, "GLTEXT", NULL, NULL); GLFWwindow *window = glfwCreateWindow(pix->w, pix->h, "mupdf", NULL, NULL);
if (!window) if (!window)
{ {
glfwTerminate(); glfwTerminate();
@@ -58,6 +140,8 @@ int main(int argc, char *argv[])
glfwGetFramebufferSize(window, &fbw, &fbh); glfwGetFramebufferSize(window, &fbw, &fbh);
glViewport(0, 0, fbw, fbh); glViewport(0, 0, fbw, fbh);
ui_draw_image(&page_tex, pix->w, pix->h);
glfwSwapBuffers(window); glfwSwapBuffers(window);
glfwWaitEvents(); glfwWaitEvents();
} }
@@ -65,5 +149,10 @@ int main(int argc, char *argv[])
glfwDestroyWindow(window); glfwDestroyWindow(window);
glfwTerminate(); glfwTerminate();
/* Clean up. */
fz_drop_pixmap(ctx, pix);
fz_drop_document(ctx, doc);
fz_drop_context(ctx);
return 0; return 0;
} }

View File

@@ -151,6 +151,7 @@ typedef khronos_uint8_t GLubyte;
#define GL_FALSE 0 #define GL_FALSE 0
#define GL_TRUE 1 #define GL_TRUE 1
#define GL_TRIANGLES 0x0004 #define GL_TRIANGLES 0x0004
#define GL_TRIANGLE_STRIP 0x0005
#define GL_ONE 1 #define GL_ONE 1
#define GL_SRC_ALPHA 0x0302 #define GL_SRC_ALPHA 0x0302
#define GL_ONE_MINUS_SRC_ALPHA 0x0303 #define GL_ONE_MINUS_SRC_ALPHA 0x0303
@@ -166,18 +167,21 @@ typedef khronos_uint8_t GLubyte;
#define GL_SCISSOR_BOX 0x0C10 #define GL_SCISSOR_BOX 0x0C10
#define GL_SCISSOR_TEST 0x0C11 #define GL_SCISSOR_TEST 0x0C11
#define GL_UNPACK_ROW_LENGTH 0x0CF2 #define GL_UNPACK_ROW_LENGTH 0x0CF2
#define GL_UNPACK_ALIGNMENT 0x0CF5
#define GL_PACK_ALIGNMENT 0x0D05 #define GL_PACK_ALIGNMENT 0x0D05
#define GL_TEXTURE_2D 0x0DE1 #define GL_TEXTURE_2D 0x0DE1
#define GL_UNSIGNED_BYTE 0x1401 #define GL_UNSIGNED_BYTE 0x1401
#define GL_UNSIGNED_SHORT 0x1403 #define GL_UNSIGNED_SHORT 0x1403
#define GL_UNSIGNED_INT 0x1405 #define GL_UNSIGNED_INT 0x1405
#define GL_FLOAT 0x1406 #define GL_FLOAT 0x1406
#define GL_RGB 0x1907
#define GL_RGBA 0x1908 #define GL_RGBA 0x1908
#define GL_FILL 0x1B02 #define GL_FILL 0x1B02
#define GL_VENDOR 0x1F00 #define GL_VENDOR 0x1F00
#define GL_RENDERER 0x1F01 #define GL_RENDERER 0x1F01
#define GL_VERSION 0x1F02 #define GL_VERSION 0x1F02
#define GL_EXTENSIONS 0x1F03 #define GL_EXTENSIONS 0x1F03
#define GL_NEAREST 0x2600
#define GL_LINEAR 0x2601 #define GL_LINEAR 0x2601
#define GL_TEXTURE_MAG_FILTER 0x2800 #define GL_TEXTURE_MAG_FILTER 0x2800
#define GL_TEXTURE_MIN_FILTER 0x2801 #define GL_TEXTURE_MIN_FILTER 0x2801