allow Imager to be loaded on Windows 98
authorTony Cook <tony@develop=help.com>
Wed, 1 Aug 2007 10:05:57 +0000 (10:05 +0000)
committerTony Cook <tony@develop=help.com>
Wed, 1 Aug 2007 10:05:57 +0000 (10:05 +0000)
Imager.xs
imager.h
t/t37w32font.t
win32.c

index f1268b02592a91b9914af940bad25158eae156f2..c44841c1575970c3522a4e96df9820a48fff6df2 100644 (file)
--- a/Imager.xs
+++ b/Imager.xs
@@ -4258,6 +4258,10 @@ undef_int
 i_wf_addfont(font)
         char *font
 
+undef_int
+i_wf_delfont(font)
+        char *font
+
 #endif
 
 #ifdef HAVE_FT2
index 3a3ea76c829ddb9f07d5a692a7b809fa4b077460..6595267dd680f0d6f8d55e09040a2c0bcda4d9e3 100644 (file)
--- a/imager.h
+++ b/imager.h
@@ -284,6 +284,7 @@ extern int i_wf_text(const char *face, i_img *im, int tx, int ty, const i_color
 extern int i_wf_cp(const char *face, i_img *im, int tx, int ty, int channel, 
                   int size, const char *text, int len, int align, int aa, int utf8);
 extern int i_wf_addfont(char const *file);
+extern int i_wf_delfont(char const *file);
 
 #endif
 
index ef9c1888bc22653bb52fc79a21c15a3a2b89b7a8..603390f1d56e2cd6d68198f0e077b5a995662721 100644 (file)
@@ -128,6 +128,8 @@ SKIP:
     $im->setpixel(x => 20+$bbox->neg_width, y => 100-$bbox->ascent, color => 'red');
     $im->setpixel(x => 20+$bbox->advance_width - $bbox->right_bearing, y => 100-$bbox->descent, color => 'red');
     $im->write(file=>'testout/t37w32_bang.ppm');
+
+    Imager::i_wf_delfont("fontfiles/ExistenceTest.ttf");
   }
 
  SKIP:
diff --git a/win32.c b/win32.c
index 25b0072591ef885b3a7997122236e6da3995335b..b076b2c12a50c6e4bd65a86a4fea5b6555cc1f22 100644 (file)
--- a/win32.c
+++ b/win32.c
@@ -289,12 +289,21 @@ i_wf_cp(const char *face, i_img *im, int tx, int ty, int channel, int size,
   return 1;
 }
 
+static HMODULE gdi_dll;
+typedef BOOL (CALLBACK *AddFontResourceExA_t)(LPCSTR, DWORD, PVOID);
+static AddFontResourceExA_t AddFontResourceExAp;
+typedef BOOL (CALLBACK *RemoveFontResourceExA_t)(LPCSTR, DWORD, PVOID);
+static RemoveFontResourceExA_t RemoveFontResourceExAp;
+
 /*
 =item i_wf_addfont(char const *filename, char const *resource_file)
 
 Adds a TTF font file as usable by the application.
 
-The font is always added as private to the application.
+Where possible the font is added as private to the application.
+
+Under Windows 95/98/ME the font is added globally, since we don't have
+any choice.  In either case call i_wf_delfont() to remove it.
 
 =cut
  */
@@ -302,7 +311,19 @@ int
 i_wf_addfont(char const *filename) {
   i_clear_error();
 
-  if (AddFontResourceEx(filename, FR_PRIVATE, 0)) {
+  if (!gdi_dll) {
+    gdi_dll = GetModuleHandle("GDI32");
+    if (gdi_dll) {
+      AddFontResourceExAp = (AddFontResourceExA_t)GetProcAddress(gdi_dll, "AddFontResourceExA");
+      RemoveFontResourceExAp = (RemoveFontResourceExA_t)GetProcAddress(gdi_dll, "RemoveFontResourceExA");
+    }
+  }
+
+  if (AddFontResourceExAp && RemoveFontResourceExAp
+      && AddFontResourceExAp(filename, FR_PRIVATE, 0)) {
+    return 1;
+  }
+  else if (AddFontResource(filename)) {
     return 1;
   }
   else {
@@ -311,6 +332,30 @@ i_wf_addfont(char const *filename) {
   }
 }
 
+/*
+=item i_wf_delfont(char const *filename, char const *resource_file)
+
+Deletes a TTF font file as usable by the application.
+
+=cut
+ */
+int
+i_wf_delfont(char const *filename) {
+  i_clear_error();
+
+  if (AddFontResourceExAp && RemoveFontResourceExAp
+      && RemoveFontResourceExAp(filename, FR_PRIVATE, 0)) {
+    return 1;
+  }
+  else if (RemoveFontResource(filename)) {
+    return 1;
+  }
+  else {
+    i_push_errorf(0, "Could not remove resource: %ld", GetLastError());
+    return 0;
+  }
+}
+
 /*
 =back