From b5020a95f9f440cf8b09d7088b377688c34b2b75 Mon Sep 17 00:00:00 2001
From: Francesco Abbate <francesco.bbt@gmail.com>
Date: Mon, 2 Jan 2023 00:49:33 +0100
Subject: [PATCH 1/9] Add meson build configuration

---
 config.h.meson |  19 +++++
 meson.build    | 188 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 207 insertions(+)
 create mode 100644 config.h.meson
 create mode 100644 meson.build

diff --git a/config.h.meson b/config.h.meson
new file mode 100644
index 0000000..a08d6c0
--- /dev/null
+++ b/config.h.meson
@@ -0,0 +1,19 @@
+#mesondefine PACKAGE_VERSION
+#mesondefine HAVE_DLFCN_H
+#mesondefine HAVE_INTTYPES_H
+#mesondefine HAVE_STDINT_H
+#mesondefine HAVE_STDLIB_H
+#mesondefine HAVE_STRING_H
+#mesondefine HAVE_SYS_STAT_H
+#mesondefine HAVE_SYS_TYPES_H
+#mesondefine HAVE_SYS_WAIT_H
+#mesondefine HAVE_UNISTD_H
+#mesondefine HAVE_TM_GMTOFF
+#mesondefine HAVE_TIMEZONE
+#mesondefine HAVE_GETENV
+#mesondefine HAVE_MKSTEMP
+
+#ifdef _WIN32
+#define WIN32
+#endif
+
diff --git a/meson.build b/meson.build
new file mode 100644
index 0000000..520eb9e
--- /dev/null
+++ b/meson.build
@@ -0,0 +1,188 @@
+project('libtexpdf', 'c', version : '2.4.6', default_options : ['c_std=gnu11'])
+
+pkg = import('pkgconfig')
+
+compiler = meson.get_compiler('c')
+
+conf = configuration_data()
+conf.set_quoted('PACKAGE_VERSION', meson.project_version())
+
+check_headers = ['dlfcn.h', 'inttypes.h', 'stdint.h', 'stdlib.h', 'string.h', 'sys/stat.h', 'sys/types.h', 'sys/wait.h', 'unistd.h']
+foreach header_name : check_headers
+    if compiler.has_header(header_name)
+        conf.set('HAVE_' + header_name.underscorify().to_upper(), 1)
+    endif
+endforeach
+
+if compiler.has_function('timezone', prefix : '#include <time.h>')
+    conf.set('HAVE_TIMEZONE', 1)
+endif
+
+if compiler.has_function('getenv', prefix : '#include <stdlib.h>')
+    conf.set('HAVE_GETENV', 1)
+endif
+
+if compiler.has_function('mkstemp', prefix : '#include <stdlib.h>')
+    conf.set('HAVE_MKSTEMP', 1)
+endif
+
+if compiler.has_member('struct tm', 'tm_gmtoff', prefix : '#include <time.h>')
+    conf.set('HAVE_TM_GMTOFF', 1)
+endif
+
+configure_file(input: 'config.h.meson', output: 'config.h', configuration: conf)
+
+libm = compiler.find_library('m', required : false)
+
+zlib_dep = dependency('zlib')
+libpng_dep = dependency('libpng')
+
+libtexpdf_deps = [libpng_dep, zlib_dep, libm]
+
+libtexpdf_cargs = ['-DXETEX', '-DBUILDING_LIBTEXPDF', '-DHAVE_CONFIG_H', '-DHAVE_ZLIB', '-DHAVE_ZLIB_COMPRESS2', '-DHAVE_LIBPNG']
+
+libtexpdf_sources = [
+	  'agl.c',
+	  'bmpimage.c',
+	  'cff.c',
+	  'cff_dict.c',
+	  'cid.c',
+	  'cidtype0.c',
+	  'cidtype2.c',
+	  'cmap.c',
+	  'cmap_read.c',
+	  'cmap_write.c',
+	  'cs_type2.c',
+	  'dpxcrypt.c',
+	  'dpxfile.c',
+	  'dpxutil.c',
+	  'epdf.c',
+	  'error.c',
+	  'fontmap.c',
+	  'jp2image.c',
+	  'jpegimage.c',
+	  'mem.c',
+	  'mfileio.c',
+	  'numbers.c',
+	  'otl_conf.c',
+	  'otl_opt.c',
+	  'pdfcolor.c',
+	  'pdfdev.c',
+	  'pdfdoc.c',
+	  'pdfdraw.c',
+	  'pdfencrypt.c',
+	  'pdfencoding.c',
+	  'pdffont.c',
+	  'pdfnames.c',
+	  'pdfobj.c',
+	  'pdfparse.c',
+	  'pdfresource.c',
+	  'pdfximage.c',
+	  'pngimage.c',
+	  'pst.c',
+	  'pst_obj.c',
+	  'sfnt.c',
+	  'subfont.c',
+	  't1_char.c',
+	  't1_load.c',
+	  'truetype.c',
+	  'tt_aux.c',
+	  'tt_cmap.c',
+	  'tt_glyf.c',
+	  'tt_gsub.c',
+	  'tt_post.c',
+	  'tt_table.c',
+	  'type0.c',
+	  'type1.c',
+	  'type1c.c',
+	  'unicode.c',
+	  'pkfont.c',
+	  'tfm.c',
+]
+
+libtexpdf = library('texpdf', libtexpdf_sources,
+    dependencies: libtexpdf_deps,
+    c_args: libtexpdf_cargs,
+    install: true,
+)
+
+install_headers(
+    'agl.h',
+    'bmpimage.h',
+    'cff.h',
+    'cff_dict.h',
+    'cff_limits.h',
+    'cff_stdstr.h',
+    'cff_types.h',
+    'cid_basefont.h',
+    'cid.h',
+    'cid_p.h',
+    'cidtype0.h',
+    'cidtype2.h',
+    'cmap.h',
+    'cmap_p.h',
+    'cmap_read.h',
+    'cmap_write.h',
+    'cs_type2.h',
+    'dpxcrypt.h',
+    'dpxfile.h',
+    'dpxutil.h',
+    'epdf.h',
+    'error.h',
+    'fontmap.h',
+    'jp2image.h',
+    'jpegimage.h',
+    'libtexpdf.h',
+    'mem.h',
+    'mfileio.h',
+    'numbers.h',
+    'otl_conf.h',
+    'otl_opt.h',
+    'pdfcolor.h',
+    'pdfdev.h',
+    'pdfdoc.h',
+    'pdfdraw.h',
+    'pdfencrypt.h',
+    'pdfencoding.h',
+    'pdffont.h',
+    'pdflimits.h',
+    'pdfnames.h',
+    'pdfobj.h',
+    'pdfparse.h',
+    'pdfresource.h',
+    'pdftypes.h',
+    'pdfximage.h',
+    'pngimage.h',
+    'pst.h',
+    'pst_obj.h',
+    'sfnt.h',
+    'subfont.h',
+    't1_char.h',
+    't1_load.h',
+    'truetype.h',
+    'tt_aux.h',
+    'tt_cmap.h',
+    'tt_glyf.h',
+    'tt_gsub.h',
+    'tt_post.h',
+    'tt_table.h',
+    'type0.h',
+    'type1.h',
+    'type1c.h',
+    'unicode.h',
+    'pkfont.h',
+    'tfm.h',
+)
+
+libtexpdf_dep = declare_dependency(
+    dependencies : libtexpdf_deps,
+    link_with : libtexpdf,
+)
+
+pkg.generate(libtexpdf,
+    filebase : 'libtexpdf',
+    name : 'libtexpdf',
+    description : 'A PDF library extracted from TeX\'s dvipdfmx',
+    url : 'https://github.com/sile-typesetter/libtexpdf',
+)
+
-- 
2.41.0


From 4166b40b58fcdf1a1a4fe4a0e8c14f93ed8b0fc9 Mon Sep 17 00:00:00 2001
From: Francesco Abbate <francesco.bbt@gmail.com>
Date: Mon, 2 Jan 2023 12:44:50 +0100
Subject: [PATCH 2/9] Github workflow for meson build

---
 .github/workflows/build.yml | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 4443bf8..69af4b2 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -16,3 +16,23 @@ jobs:
       - name: Make
         run: |
           make
+
+  build_meson:
+    runs-on: ubuntu-20.04
+    steps:
+      - name: Checkout
+        uses: actions/checkout@v2
+      - name: Python Setup
+        uses: actions/setup-python@v2
+        with:
+          python-version: 3.7
+      - name: Setup packages
+        run: |
+          sudo apt-get install -y ninja-build
+          pip3 install meson
+      - name: Setup
+        run: |
+          meson setup .build
+      - name: Build
+        run: |
+          ninja dist -C .build
-- 
2.41.0


From a09b17c8cd5daaa9fe81ddde49357e2bb47d579d Mon Sep 17 00:00:00 2001
From: Francesco Abbate <francesco.bbt@gmail.com>
Date: Tue, 3 Jan 2023 13:26:28 +0100
Subject: [PATCH 3/9] Install headers inside libtexpdf subdir

---
 meson.build | 1 +
 1 file changed, 1 insertion(+)

diff --git a/meson.build b/meson.build
index 520eb9e..aed7e52 100644
--- a/meson.build
+++ b/meson.build
@@ -172,6 +172,7 @@ install_headers(
     'unicode.h',
     'pkfont.h',
     'tfm.h',
+    subdir : 'libtexpdf'
 )
 
 libtexpdf_dep = declare_dependency(
-- 
2.41.0


From 9cdf818d325a6f01df043b42cb1c532aa7148e52 Mon Sep 17 00:00:00 2001
From: Francesco Abbate <francesco.bbt@gmail.com>
Date: Tue, 3 Jan 2023 14:14:55 +0100
Subject: [PATCH 4/9] Fix some header tests in Meson build

---
 config.h.meson | 3 +--
 meson.build    | 2 +-
 2 files changed, 2 insertions(+), 3 deletions(-)

diff --git a/config.h.meson b/config.h.meson
index a08d6c0..5ca9a51 100644
--- a/config.h.meson
+++ b/config.h.meson
@@ -1,13 +1,12 @@
 #mesondefine PACKAGE_VERSION
-#mesondefine HAVE_DLFCN_H
 #mesondefine HAVE_INTTYPES_H
 #mesondefine HAVE_STDINT_H
+#mesondefine HAVE_STDBOOL_H
 #mesondefine HAVE_STDLIB_H
 #mesondefine HAVE_STRING_H
 #mesondefine HAVE_SYS_STAT_H
 #mesondefine HAVE_SYS_TYPES_H
 #mesondefine HAVE_SYS_WAIT_H
-#mesondefine HAVE_UNISTD_H
 #mesondefine HAVE_TM_GMTOFF
 #mesondefine HAVE_TIMEZONE
 #mesondefine HAVE_GETENV
diff --git a/meson.build b/meson.build
index aed7e52..614bca5 100644
--- a/meson.build
+++ b/meson.build
@@ -7,7 +7,7 @@ compiler = meson.get_compiler('c')
 conf = configuration_data()
 conf.set_quoted('PACKAGE_VERSION', meson.project_version())
 
-check_headers = ['dlfcn.h', 'inttypes.h', 'stdint.h', 'stdlib.h', 'string.h', 'sys/stat.h', 'sys/types.h', 'sys/wait.h', 'unistd.h']
+check_headers = ['inttypes.h', 'stdint.h', 'stdbool.h', 'stdlib.h', 'string.h', 'sys/stat.h', 'sys/types.h', 'sys/wait.h']
 foreach header_name : check_headers
     if compiler.has_header(header_name)
         conf.set('HAVE_' + header_name.underscorify().to_upper(), 1)
-- 
2.41.0


From 939a4e2c8df772384d253cd8d72ba8511bc1d906 Mon Sep 17 00:00:00 2001
From: Francesco Abbate <francesco.bbt@gmail.com>
Date: Tue, 3 Jan 2023 17:23:13 +0100
Subject: [PATCH 5/9] Add a simple unit test to create a PDF file

The text does not appear in the document. The page size should be set to some
more sensible values.

We should probably check the output file, at least that it is not empty and
check that the header correspond to a valid PDF file.
---
 .github/workflows/build.yml |  5 +++-
 meson.build                 |  2 ++
 tests/meson.build           |  8 +++++
 tests/test-create.c         | 59 +++++++++++++++++++++++++++++++++++++
 4 files changed, 73 insertions(+), 1 deletion(-)
 create mode 100644 tests/meson.build
 create mode 100644 tests/test-create.c

diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 69af4b2..074a660 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -33,6 +33,9 @@ jobs:
       - name: Setup
         run: |
           meson setup .build
-      - name: Build
+      - name: Dist Check
         run: |
           ninja dist -C .build
+      - name: Unit Tests
+        run: |
+          ninja test -C .build
diff --git a/meson.build b/meson.build
index 614bca5..c2ad26e 100644
--- a/meson.build
+++ b/meson.build
@@ -180,6 +180,8 @@ libtexpdf_dep = declare_dependency(
     link_with : libtexpdf,
 )
 
+subdir('tests')
+
 pkg.generate(libtexpdf,
     filebase : 'libtexpdf',
     name : 'libtexpdf',
diff --git a/tests/meson.build b/tests/meson.build
new file mode 100644
index 0000000..d336a8f
--- /dev/null
+++ b/tests/meson.build
@@ -0,0 +1,8 @@
+libtexpdf_include = include_directories('..')
+
+test_create = executable('test-create', 'test-create.c',
+    dependencies: libtexpdf_dep,
+    include_directories: libtexpdf_include,
+)
+
+test('Create a PDF file', test_create, args : ['pdf-test.pdf'])
diff --git a/tests/test-create.c b/tests/test-create.c
new file mode 100644
index 0000000..0835c75
--- /dev/null
+++ b/tests/test-create.c
@@ -0,0 +1,59 @@
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <libtexpdf.h>
+
+double precision = 65536.0;
+char* producer = "SILE";
+
+pdf_doc *pdf_init (const char *fn, double w, double height) {
+  pdf_doc *p = texpdf_open_document(fn, 0, w, height, 0,0,0);
+  texpdf_init_device(p, 1 / precision, 2, 0);
+
+  pdf_rect mediabox;
+  mediabox.llx = 0.0;
+  mediabox.lly = 0.0;
+  mediabox.urx = w;
+  mediabox.ury = height;
+  texpdf_files_init();
+  texpdf_init_fontmaps();
+  texpdf_tt_aux_set_always_embed();
+  texpdf_doc_set_mediabox(p, 0, &mediabox);
+  texpdf_add_dict(p->info,
+               texpdf_new_name("Producer"),
+               texpdf_new_string(producer, strlen(producer)));
+  return p;
+}
+
+int pdf_finish(pdf_doc *p) {
+  texpdf_files_close();
+  texpdf_close_device();
+  texpdf_close_document(p);
+  texpdf_close_fontmaps();
+  return 0;
+}
+
+int pdf_add_content(pdf_doc *p, const char *input) {
+  int input_l = strlen(input);
+  texpdf_graphics_mode(p); /* Don't be mid-string! */
+  texpdf_doc_add_page_content(p, " ", 1);
+  texpdf_doc_add_page_content(p, input, input_l);
+  texpdf_doc_add_page_content(p, " ", 1);
+  return 0;
+}
+
+int main(int argc, char *argv[]) {
+  if (argc != 2) {
+    fprintf(stderr, "usage: %s <output-file>\n", argv[0]);
+    return 1;
+  }
+  const char *filename = argv[1];
+  const double width = 400.0, height = 600.0;
+  pdf_doc *p = pdf_init(filename, width, height);
+  texpdf_doc_begin_page(p, 1, 0, height);
+  pdf_add_content(p, "Hello world!");
+  texpdf_doc_end_page(p);
+  pdf_finish(p);
+  return 0;
+}
-- 
2.41.0


From 7f2fa33b9746fbaee4172fb1f897db398267cb4a Mon Sep 17 00:00:00 2001
From: Francesco Abbate <francesco.bbt@gmail.com>
Date: Tue, 3 Jan 2023 18:41:25 +0100
Subject: [PATCH 6/9] Add project's C args to test program

Normally the test program should not use the private config variables of
the library but we are temporarily adding them to make the tests succed.

The problem was reported in the github issue:

https://github.com/sile-typesetter/libtexpdf/issues/27
---
 tests/meson.build | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/tests/meson.build b/tests/meson.build
index d336a8f..1f5ea50 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -1,6 +1,13 @@
 libtexpdf_include = include_directories('..')
 
+# FIXME:
+# Above we should not add libtexpdf_cargs but if they are not added
+# compiling may fail because the header inclusion is conditioned to
+# defines like HAVE_CONFIG_H and others.
+# In addition the header files tries to conditionally include config.h
+# but this latter is private and not exported.
 test_create = executable('test-create', 'test-create.c',
+    c_args: libtexpdf_cargs,
     dependencies: libtexpdf_dep,
     include_directories: libtexpdf_include,
 )
-- 
2.41.0


From 60869c796b8e6a26600af59be5fff1700c8da3e8 Mon Sep 17 00:00:00 2001
From: Francesco Abbate <francesco.bbt@gmail.com>
Date: Tue, 3 Jan 2023 19:16:49 +0100
Subject: [PATCH 7/9] Include license when installing

---
 meson.build | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/meson.build b/meson.build
index c2ad26e..c3b5fa6 100644
--- a/meson.build
+++ b/meson.build
@@ -189,3 +189,5 @@ pkg.generate(libtexpdf,
     url : 'https://github.com/sile-typesetter/libtexpdf',
 )
 
+install_data('LICENSE', install_dir : 'share/libtexpdf')
+
-- 
2.41.0


From c716a8008194ec4869bdee24b8ec5ed8a579a03e Mon Sep 17 00:00:00 2001
From: Francesco Abbate <francesco.bbt@gmail.com>
Date: Wed, 4 Jan 2023 11:37:32 +0100
Subject: [PATCH 8/9] Add meson check for compress2 zlib function

---
 meson.build | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/meson.build b/meson.build
index c3b5fa6..310f284 100644
--- a/meson.build
+++ b/meson.build
@@ -37,6 +37,10 @@ libm = compiler.find_library('m', required : false)
 zlib_dep = dependency('zlib')
 libpng_dep = dependency('libpng')
 
+if not compiler.has_function('compress2', prefix : '#include <zlib.h>', dependencies: zlib_dep)
+	  error('zlib with compress2 is required')
+endif
+
 libtexpdf_deps = [libpng_dep, zlib_dep, libm]
 
 libtexpdf_cargs = ['-DXETEX', '-DBUILDING_LIBTEXPDF', '-DHAVE_CONFIG_H', '-DHAVE_ZLIB', '-DHAVE_ZLIB_COMPRESS2', '-DHAVE_LIBPNG']
-- 
2.41.0


From cadaa472a8508d1718d5b253e675154cad82c329 Mon Sep 17 00:00:00 2001
From: Francesco Abbate <francesco.bbt@gmail.com>
Date: Fri, 6 Jan 2023 11:23:03 +0100
Subject: [PATCH 9/9] Fix build on Windows

---
 config.h.meson |  5 -----
 dpxfile.c      | 10 ++++++----
 error.h        |  7 ++++---
 libtexpdf.h    |  8 +-------
 meson.build    |  3 +--
 pdfobj.c       |  2 +-
 win32/win32.h  |  7 +++++--
 7 files changed, 18 insertions(+), 24 deletions(-)

diff --git a/config.h.meson b/config.h.meson
index 5ca9a51..c3bfaff 100644
--- a/config.h.meson
+++ b/config.h.meson
@@ -11,8 +11,3 @@
 #mesondefine HAVE_TIMEZONE
 #mesondefine HAVE_GETENV
 #mesondefine HAVE_MKSTEMP
-
-#ifdef _WIN32
-#define WIN32
-#endif
-
diff --git a/dpxfile.c b/dpxfile.c
index f04fc0b..adfaf48 100644
--- a/dpxfile.c
+++ b/dpxfile.c
@@ -27,8 +27,10 @@
 #if defined(WIN32)
 #include <windows.h>
 #include <wchar.h>
+#ifndef PATH_MAX
 #define PATH_MAX 2048 // do not use MAX_PATH as it's too small
 #endif
+#endif
 
 #include "libtexpdf.h"
 
@@ -48,7 +50,7 @@
 #define MAX_KEY_LEN 16
 
 #include <string.h>
-#if defined(WIN32) && !defined(__MINGW32__)
+#if defined(WIN32) && defined(_MSC_VER)
 #include <io.h>
 #include <process.h>
 #include <wchar.h>
@@ -251,7 +253,7 @@ static int exec_spawn (char *cmd)
       }
     }
     *pp = '\0';
-#if defined(WIN32) && !defined(__MINGW32__)
+#if defined(WIN32) && defined(_MSC_VER)
     if (strchr (buf, ' ') || strchr (buf, '\t')) {
       *qv = xcalloc (strlen(buf) + 3, sizeof (char));
       **qv = '\0';
@@ -425,7 +427,7 @@ dpx_create_temp_file (void)
   }
 #else /* use _tempnam or tmpnam */
   {
-#  if defined(WIN32) && !defined(__MINGW32__)
+#  if defined(WIN32) && defined(_MSC_VER)
     const char *_tmpd;
     char *p;
     _tmpd = dpx_get_tmpdir();
@@ -481,7 +483,7 @@ dpx_create_fix_temp_file (const char *filename)
       sprintf(s, "%02x", digest[i]);
       s += 2;
   }
-#if defined(WIN32) && !defined(__MINGW32__)
+#if defined(WIN32) && defined(_MSC_VER)
   for (p = ret; *p; p++) {
     /*if (IS_KANJI (p))
       p++;
diff --git a/error.h b/error.h
index 96080ae..b4004ad 100644
--- a/error.h
+++ b/error.h
@@ -23,8 +23,10 @@
 #ifndef _ERROR_H_
 #define _ERROR_H_
 
-#if defined(WIN32) 
-#  if defined(__MINGW32__)
+#if defined(WIN32)
+#  if defined(_MSC_VER)
+#    pragma warning(disable : 4101 4018)
+#  else
 #    define CDECL
 #  endif
 #  undef ERROR
@@ -34,7 +36,6 @@
 #  undef SETLINECAP
 #  undef SETLINEJOIN
 #  undef SETMITERLIMIT
-#  pragma warning(disable : 4101 4018)
 #else
 #  ifndef __cdecl
 #  define __cdecl
diff --git a/libtexpdf.h b/libtexpdf.h
index 27836a9..8a7ba8e 100644
--- a/libtexpdf.h
+++ b/libtexpdf.h
@@ -41,14 +41,8 @@ extern int compat_mode;
 #define FOPEN_WBIN_MODE "wb"
 #endif /* not FOPEN_WBIN_MODE */
 
-#if defined(WIN32) && !defined(__MINGW32__)
-#define off64_t int64_t
-#define ftello _ftelli64
-#define fseeko _fseeki64
+#if defined(WIN32)
 #include "win32/win32.h"
-#elif defined(__MINGW32__)
-#define ftello ftello64
-#define fseeko fseeko64
 #endif
 
 #include "agl.h"
diff --git a/meson.build b/meson.build
index 310f284..8dc7ba4 100644
--- a/meson.build
+++ b/meson.build
@@ -107,6 +107,7 @@ libtexpdf_sources = [
 libtexpdf = library('texpdf', libtexpdf_sources,
     dependencies: libtexpdf_deps,
     c_args: libtexpdf_cargs,
+    soversion: '0',
     install: true,
 )
 
@@ -193,5 +194,3 @@ pkg.generate(libtexpdf,
     url : 'https://github.com/sile-typesetter/libtexpdf',
 )
 
-install_data('LICENSE', install_dir : 'share/libtexpdf')
-
diff --git a/pdfobj.c b/pdfobj.c
index 0947c15..518dc96 100644
--- a/pdfobj.c
+++ b/pdfobj.c
@@ -302,7 +302,7 @@ pdf_out_init (const char *filename, int do_encryption)
   output_stream = NULL;
 
   if (filename == NULL) { /* no filename: writing to stdout */
-#if defined(WIN32) && !defined(__MINGW32__)
+#if defined(WIN32) && defined(_MSC_VER)
     setmode(fileno(stdout), _O_BINARY);
 #endif
     pdf_output_file = stdout;
diff --git a/win32/win32.h b/win32/win32.h
index f907e20..0fe7997 100644
--- a/win32/win32.h
+++ b/win32/win32.h
@@ -1,7 +1,10 @@
 #pragma once
-#ifdef WIN32
 #define localtime_r(timep, result) localtime_s(result, timep)
 #define gmtime_r(timep, result) gmtime_s(result, timep)
 #define strncasecmp _strnicmp
+#if defined(_MSC_VER)
+#define off64_t int64_t
+#define ftello _ftelli64
+#define fseeko _fseeki64
 #define stat _stat
-#endif
\ No newline at end of file
+#endif
-- 
2.41.0

