|
4 | 4 | #include <stdio.h> |
5 | 5 | #include <inttypes.h> |
6 | 6 |
|
| 7 | +// Ghostscript expects a non-standard #define for indicating a Windows environment, apparently. |
| 8 | +// This ensures the Ghostscript API functions are referenced by the linker with the correct |
| 9 | +// calling convention (on x86). |
| 10 | +#ifdef _WIN32 |
| 11 | +#define _WINDOWS_ |
| 12 | +#endif |
| 13 | + |
7 | 14 | #include <psi/iapi.h> |
8 | 15 | #include <devices/gdevdsp.h> |
9 | 16 |
|
| 17 | +#include <config.h> |
| 18 | + |
10 | 19 | static void *shared_ghostscript_instance = NULL; |
11 | 20 |
|
12 | 21 | struct pdf_source { |
@@ -110,29 +119,6 @@ display_callback display = |
110 | 119 | NULL |
111 | 120 | }; |
112 | 121 |
|
113 | | -static int ghostscript_stdin(void *instance, char *buf, int len) |
114 | | -{ |
115 | | - return 0; |
116 | | -} |
117 | | - |
118 | | -static int ghostscript_stdout(void *instance, const char *str, int len) |
119 | | -{ |
120 | | - char buffer[256]; |
121 | | - snprintf(buffer, 256, "gs-out: %.*s", len, str); |
122 | | - blog(LOG_INFO, buffer); |
123 | | - |
124 | | - return len; |
125 | | -} |
126 | | - |
127 | | -static int ghostscript_stderr(void *instance, const char *str, int len) |
128 | | -{ |
129 | | - char buffer[256]; |
130 | | - snprintf(buffer, 256, "gs-err: %.*s", len, str); |
131 | | - blog(LOG_ERROR, buffer); |
132 | | - |
133 | | - return len; |
134 | | -} |
135 | | - |
136 | 122 |
|
137 | 123 | static void pdf_source_load(struct pdf_source *context) |
138 | 124 | { |
@@ -162,13 +148,19 @@ static void pdf_source_load(struct pdf_source *context) |
162 | 148 |
|
163 | 149 | int gs_argc = sizeof(gs_argv) / sizeof(gs_argv[0]); |
164 | 150 |
|
165 | | - int code = gsapi_init_with_args(shared_ghostscript_instance, gs_argc, gs_argv); |
166 | | - |
| 151 | + // Here, we execute the Ghostscript command to parse the file and render the document. The display device |
| 152 | + // callbacks indicated above will handle copying the Ghostscript buffer into an OBS texture if any page is |
| 153 | + // rendered. |
| 154 | + gsapi_init_with_args(shared_ghostscript_instance, gs_argc, gs_argv); |
167 | 155 | gsapi_exit(shared_ghostscript_instance); |
168 | 156 | } |
169 | 157 |
|
170 | 158 | if (!context->cached_anypagesrendered) |
171 | 159 | { |
| 160 | + // If the ghostscript_display_page() callback above was never called, that means the commands issued |
| 161 | + // to Ghostscript did not result in a page in the document being rendered. That is most likely to happen |
| 162 | + // if the document does not contain the requested page number. We simply destroy the texture and render |
| 163 | + // nothing in that case. |
172 | 164 | obs_enter_graphics(); |
173 | 165 | gs_texture_destroy(context->texture); |
174 | 166 | context->texture = NULL; |
@@ -367,7 +359,6 @@ OBS_MODULE_USE_DEFAULT_LOCALE("obs-pdf", "en-US") |
367 | 359 | bool obs_module_load(void) |
368 | 360 | { |
369 | 361 | gsapi_new_instance(&shared_ghostscript_instance, NULL); |
370 | | - gsapi_set_stdio(shared_ghostscript_instance, ghostscript_stdin, ghostscript_stdout, ghostscript_stderr); |
371 | 362 | gsapi_set_display_callback(shared_ghostscript_instance, &display); |
372 | 363 |
|
373 | 364 | obs_register_source(&pdf_source_info); |
|
0 commit comments