Example #1
0
static void reload(void)
{
    fz_drop_outline(ctx, outline);
    fz_drop_document(ctx, doc);

    doc = fz_open_document(ctx, filename);
    if (fz_needs_password(ctx, doc))
    {
        if (!fz_authenticate_password(ctx, doc, password))
        {
            fprintf(stderr, "Invalid password.\n");
            exit(1);
        }
    }

    fz_layout_document(ctx, doc, layout_w, layout_h, layout_em);

    fz_try(ctx)
    outline = fz_load_outline(ctx, doc);
    fz_catch(ctx)
    outline = NULL;

    pdf = pdf_specifics(ctx, doc);
    if (pdf)
        pdf_enable_js(ctx, pdf);

    currentpage = fz_clampi(currentpage, 0, fz_count_pages(ctx, doc) - 1);

    render_page();
    update_title();
}
Example #2
0
pdf::pdf(const char *filepath)
{
    _filepath       = filepath;
    _good           = true;
    _image_name     = nullptr;
    _needs_password = false;
    _size           = 0;

    // Create a context to hold the exception stack and various caches.
    _ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);

    if (!_ctx)
    {
        _good = false;
        return;
    }

    fz_try(_ctx)
    {
        // Register the default file types.
        fz_register_document_handlers(_ctx);

        // Open the PDF, XPS or CBZ document.
        _doc = fz_open_document(_ctx, filepath);

        if (fz_needs_password(_ctx, _doc))
        {
            _needs_password = true;
            _good = false;
        }
        else
        {
            // Retrieve the number of pages (not used in this example).
            _size = fz_count_pages(_ctx, _doc);
        }
    }
    fz_catch(_ctx)
    {
        _doc  = 0;
        _good = false;
    }
}
Example #3
0
int main(int argc, char **argv)
{
	char *password = "";
	fz_document *doc = NULL;
	fz_context *ctx;
	int c;

	fz_var(ctx, doc);

	while ((c = fz_getopt(argc, argv, "p:")) != -1)
	{
		switch (c)
		{
		default: usage(); break;
		case 'p': password = fz_optarg; break;
		}
	}

	if (fz_optind + 2 != argc)
		usage();

	filename = argv[fz_optind];
	mujstest_filename = argv[fz_optind+1];

	if (strcmp(mujstest_filename, "-") == 0)
		mujstest_file = stdout;
	else
		mujstest_file = fopen(mujstest_filename, "wb");

	ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT);
	if (!ctx)
	{
		fprintf(stderr, "cannot initialise context\n");
		exit(1);
	}

	fz_register_document_handlers(ctx);

	fz_try(ctx)
	{
		doc = fz_open_document(ctx, filename);

		if (fz_needs_password(doc))
		{
			if (!fz_authenticate_password(ctx, doc, password))
				fz_throw(ctx, FZ_ERROR_GENERIC, "cannot authenticate password: %s", filename);
			fprintf(mujstest_file, "PASSWORD %s\n", password);
		}

		fprintf(mujstest_file, "OPEN %s\n", filename);

		processpages(ctx, doc);

		fz_drop_document(ctx, doc);
	}
	fz_catch(ctx)
	{
		fprintf(stderr, "mjsgen: cannot process document: %s\n", filename);
		return 1;
	}

	fclose(mujstest_file);
	fz_drop_context(ctx);
	return 0;
}
bool MupdfBackend::isLocked() {
  return m_doc && fz_needs_password(m_ctx, m_doc) != 0;
}
Example #5
0
int main(int argc, char **argv)
#endif
{
	const GLFWvidmode *video_mode;
	char filename[2048];
	char *password = "";
	float layout_w = DEFAULT_LAYOUT_W;
	float layout_h = DEFAULT_LAYOUT_H;
	float layout_em = DEFAULT_LAYOUT_EM;
	char *layout_css = NULL;
	int c;

	while ((c = fz_getopt(argc, argv, "p:r:W:H:S:U:")) != -1)
	{
		switch (c)
		{
		default: usage(argv[0]); break;
		case 'p': password = fz_optarg; break;
		case 'r': currentzoom = fz_atof(fz_optarg); break;
		case 'W': layout_w = fz_atof(fz_optarg); break;
		case 'H': layout_h = fz_atof(fz_optarg); break;
		case 'S': layout_em = fz_atof(fz_optarg); break;
		case 'U': layout_css = fz_optarg; break;
		}
	}

	if (fz_optind < argc)
	{
		fz_strlcpy(filename, argv[fz_optind], sizeof filename);
	}
	else
	{
#ifdef _WIN32
		win_install();
		if (!win_open_file(filename, sizeof filename))
			exit(0);
#else
		usage(argv[0]);
#endif
	}

	title = strrchr(filename, '/');
	if (!title)
		title = strrchr(filename, '\\');
	if (title)
		++title;
	else
		title = filename;

	memset(&ui, 0, sizeof ui);

	search_input.p = search_input.text;
	search_input.q = search_input.p;
	search_input.end = search_input.p;

	if (!glfwInit()) {
		fprintf(stderr, "cannot initialize glfw\n");
		exit(1);
	}

	video_mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
	screen_w = video_mode->width;
	screen_h = video_mode->height;

	glfwSetErrorCallback(on_error);

	window = glfwCreateWindow(DEFAULT_WINDOW_W, DEFAULT_WINDOW_H, filename, NULL, NULL);
	if (!window) {
		fprintf(stderr, "cannot create glfw window\n");
		exit(1);
	}

	glfwMakeContextCurrent(window);

	ctx = fz_new_context(NULL, NULL, 0);
	fz_register_document_handlers(ctx);

	if (layout_css)
	{
		fz_buffer *buf = fz_read_file(ctx, layout_css);
		fz_write_buffer_byte(ctx, buf, 0);
		fz_set_user_css(ctx, (char*)buf->data);
		fz_drop_buffer(ctx, buf);
	}

	has_ARB_texture_non_power_of_two = glfwExtensionSupported("GL_ARB_texture_non_power_of_two");
	if (!has_ARB_texture_non_power_of_two)
		fz_warn(ctx, "OpenGL implementation does not support non-power of two texture sizes");

	glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_texture_size);

	ui.fontsize = DEFAULT_UI_FONTSIZE;
	ui.baseline = DEFAULT_UI_BASELINE;
	ui.lineheight = DEFAULT_UI_LINEHEIGHT;

	ui_init_fonts(ctx, ui.fontsize);

	doc = fz_open_document(ctx, filename);
	if (fz_needs_password(ctx, doc))
	{
		if (!fz_authenticate_password(ctx, doc, password))
		{
			fprintf(stderr, "Invalid password.\n");
			exit(1);
		}
	}

	outline = fz_load_outline(ctx, doc);
	pdf = pdf_specifics(ctx, doc);
	if (pdf)
		pdf_enable_js(ctx, pdf);

	fz_layout_document(ctx, doc, layout_w, layout_h, layout_em);

	render_page();
	update_title();
	shrinkwrap();

	glfwSetFramebufferSizeCallback(window, on_reshape);
	glfwSetCursorPosCallback(window, on_mouse_motion);
	glfwSetMouseButtonCallback(window, on_mouse_button);
	glfwSetScrollCallback(window, on_scroll);
	glfwSetCharModsCallback(window, on_char);
	glfwSetKeyCallback(window, on_key);
	glfwSetWindowRefreshCallback(window, on_display);

	glfwGetFramebufferSize(window, &window_w, &window_h);

	ui_needs_update = 1;

	while (!glfwWindowShouldClose(window))
	{
		glfwWaitEvents();
		if (ui_needs_update)
			run_main_loop();
	}

	ui_finish_fonts(ctx);

	fz_drop_link(ctx, links);
	fz_drop_page(ctx, page);
	fz_drop_outline(ctx, outline);
	fz_drop_document(ctx, doc);
	fz_drop_context(ctx);

	glfwTerminate();

	return 0;
}
JNIEXPORT jboolean JNICALL
Java_com_artifex_mupdf_MuPDFCore_needsPasswordInternal(JNIEnv * env, jobject thiz)
{
	return fz_needs_password(doc) ? JNI_TRUE : JNI_FALSE;
}
Example #7
0
int muconvert_main(int argc, char **argv)
{
	int i, c;

	while ((c = fz_getopt(argc, argv, "p:A:W:H:S:U:o:F:O:")) != -1)
	{
		switch (c)
		{
		default: usage(); break;

		case 'p': password = fz_optarg; break;
		case 'A': alphabits = atoi(fz_optarg); break;
		case 'W': layout_w = atof(fz_optarg); break;
		case 'H': layout_h = atof(fz_optarg); break;
		case 'S': layout_em = atof(fz_optarg); break;
		case 'U': layout_css = fz_optarg; break;

		case 'o': output = fz_optarg; break;
		case 'F': format = fz_optarg; break;
		case 'O': options = fz_optarg; break;
		}
	}

	if (fz_optind == argc || (!format && !output))
		usage();

	/* Create a context to hold the exception stack and various caches. */
	ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
	if (!ctx)
	{
		fprintf(stderr, "cannot create mupdf context\n");
		return EXIT_FAILURE;
	}

	/* Register the default file types to handle. */
	fz_try(ctx)
		fz_register_document_handlers(ctx);
	fz_catch(ctx)
	{
		fprintf(stderr, "cannot register document handlers: %s\n", fz_caught_message(ctx));
		fz_drop_context(ctx);
		return EXIT_FAILURE;
	}

	fz_set_aa_level(ctx, alphabits);

	if (layout_css)
	{
		fz_buffer *buf = fz_read_file(ctx, layout_css);
		fz_write_buffer_byte(ctx, buf, 0);
		fz_set_user_css(ctx, (char*)buf->data);
		fz_drop_buffer(ctx, buf);
	}

	/* Open the output document. */
	fz_try(ctx)
		out = fz_new_document_writer(ctx, output, format, options);
	fz_catch(ctx)
	{
		fprintf(stderr, "cannot create document: %s\n", fz_caught_message(ctx));
		fz_drop_context(ctx);
		return EXIT_FAILURE;
	}

	for (i = fz_optind; i < argc; ++i)
	{
		doc = fz_open_document(ctx, argv[i]);
		if (fz_needs_password(ctx, doc))
			if (!fz_authenticate_password(ctx, doc, password))
				fz_throw(ctx, FZ_ERROR_GENERIC, "cannot authenticate password: %s", argv[i]);
		fz_layout_document(ctx, doc, layout_w, layout_h, layout_em);
		count = fz_count_pages(ctx, doc);

		if (i+1 < argc && fz_is_page_range(ctx, argv[i+1]))
			runrange(argv[++i]);
		else
			runrange("1-N");

		fz_drop_document(ctx, doc);
	}

	fz_drop_document_writer(ctx, out);
	fz_drop_context(ctx);
	return EXIT_SUCCESS;
}