Description: Add support for poppler/0.5; fix linking.
 Additional patches from Étoilé:
  - Fix distorted colours in splash renderer
  - Add missing check for fontconfig
Author: Yen-Ju Chen
Author: Jason Clouse
Author: Rob Burns
Author: Yavor Doganov <yavor@gnu.org>
Forwarded: not-needed
Last-Update: 2012-05-31
---

--- popplerkit.framework.orig/bindings/GNUmakefile
+++ popplerkit.framework/bindings/GNUmakefile
@@ -44,4 +44,12 @@
    bindings_C_FILES += poppler_cairo_img_renderer.c
 endif
 
+ifeq ($(POPPLER_0_4), YES)
+   bindings_CFLAGS += -DPOPPLER_0_4
+endif
+
+ifeq ($(POPPLER_0_5), YES)
+   bindings_CFLAGS += -DPOPPLER_0_5
+endif
+
 include $(GNUSTEP_MAKEFILES)/subproject.make

--- popplerkit.framework.orig/bindings/poppler.cc
+++ popplerkit.framework/bindings/poppler.cc
@@ -124,7 +124,9 @@
       }
 
       globalParams = new GlobalParams(NULL);
+#ifdef POPPLER_0_4
       globalParams->setupBaseFontsFc(NULL);
+#endif
       //dump_fonts(FcConfigGetCurrent());
       fprintf(stderr, "poppler library initialized\n"); fflush(stderr);
    }

--- popplerkit.framework.orig/bindings/poppler_cairo_img_renderer.cc
+++ popplerkit.framework/bindings/poppler_cairo_img_renderer.cc
@@ -49,13 +49,25 @@
 
    if (rotate == 90 || rotate == 270) 
    {
+#ifdef POPPLER_0_4
       width = MAX((int)(page->getHeight() * scale + 0.5), 1);
       height = MAX((int)(page->getWidth() * scale + 0.5), 1);
+#endif
+#ifdef POPPLER_0_5
+      width = MAX((int)(page->getMediaHeight() * scale + 0.5), 1);
+      height = MAX((int)(page->getMediaWidth() * scale + 0.5), 1);
+#endif
    }
    else
    {
+#ifdef POPPLER_0_4
       width = MAX((int)(page->getWidth() * scale + 0.5), 1);
       height = MAX((int)(page->getHeight() * scale + 0.5), 1);
+#endif
+#ifdef POPPLER_0_5
+      width = MAX((int)(page->getMediaWidth() * scale + 0.5), 1);
+      height = MAX((int)(page->getMediaHeight() * scale + 0.5), 1);
+#endif
    }
 
    int rowstride = width * 4;
@@ -64,7 +76,11 @@
 
    cairo_surface_t* surface = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, width, height, rowstride);
 
+#ifdef POPPLER_0_5
+   output_dev->device->setCairo(cairo_create(surface));
+#else
    output_dev->device->setSurface(surface);
+#endif
    output_dev->surface = surface;
    output_dev->data = data;
 }
@@ -122,6 +138,9 @@
    SYNCHRONIZED(PAGE(poppler_page)->displaySlice(CAIRO_DEV_IMG(output_dev)->device,
                                                  (double)hDPI, (double)vDPI,
                                                  rotate,
+#ifdef POPPLER_0_5
+						 gTrue, // use MediaBox
+#endif
                                                  gTrue, // Crop
                                                  (int)sliceX, (int)sliceY,
                                                  (int)sliceW, (int)sliceH,

--- popplerkit.framework.orig/bindings/poppler_page.cc
+++ popplerkit.framework/bindings/poppler_page.cc
@@ -66,7 +66,14 @@
       return -1;
    }
    
+
+#ifdef POPPLER_0_4
    return PAGE(poppler_page)->getWidth();
+#endif
+#ifdef POPPLER_0_5
+   return PAGE(poppler_page)->getMediaWidth();
+#endif
+
 }
 
 double poppler_page_get_height(void* poppler_page)
@@ -76,5 +83,11 @@
       return -1;
    }
    
+#ifdef POPPLER_0_4
    return PAGE(poppler_page)->getHeight();
+#endif
+#ifdef POPPLER_0_5
+   return PAGE(poppler_page)->getMediaHeight();
+#endif
+
 }

--- popplerkit.framework.orig/bindings/poppler_splash_renderer.cc
+++ popplerkit.framework/bindings/poppler_splash_renderer.cc
@@ -35,8 +35,17 @@
 {  
    BEGIN_SYNCHRONIZED;
       SplashColor white;
+#ifdef POPPLER_0_4
       white.rgb8 = splashMakeRGB8(bg_red, bg_green, bg_blue);
       void* splashDevice = new SplashOutputDev(splashModeRGB8, gFalse, white);
+#endif
+#ifdef POPPLER_0_5
+      white[0] = bg_red;
+      white[1] = bg_green;
+      white[2] = bg_blue;
+      // I'm not sure what bitmapRowPad should be, 1 is just a guess.
+      void* splashDevice = new SplashOutputDev(splashModeRGB8, 1, gFalse, white);
+#endif
    END_SYNCHRONIZED;
    
    return splashDevice;
@@ -77,6 +86,9 @@
    SYNCHRONIZED(PAGE(poppler_page)->displaySlice(SPLASH_DEV(output_dev),
                                                  (double)hDPI, (double)vDPI,
                                                  rotate,
+#ifdef POPPLER_0_5
+						 gTrue, // useMediaBox
+#endif
                                                  gTrue, // Crop
                                                  (int)sliceX, (int)sliceY,
                                                  (int)sliceW, (int)sliceH,
@@ -109,22 +121,39 @@
       return 0;
    }
 
+#ifdef POPPLER_0_4
    SplashRGB8*     rgb8;
+#endif
+#ifdef POPPLER_0_5
+   SplashColorPtr  color;
+#endif
    unsigned char*  dataPtr;
 
+#ifdef POPPLER_0_4
    rgb8 = SPLASH_BITMAP(bitmap)->getDataPtr().rgb8;
+#endif
+#ifdef POPPLER_0_5
+   color = SPLASH_BITMAP(bitmap)->getDataPtr();
+#endif
 
    dataPtr = *data;
    for (int row = 0; row < SPLASH_BITMAP(bitmap)->getHeight(); row++)
    {
       for (int col = 0; col < SPLASH_BITMAP(bitmap)->getWidth(); col++)
       {
+#ifdef POPPLER_0_4
          *dataPtr++ = splashRGB8R(*rgb8);
          *dataPtr++ = splashRGB8G(*rgb8);
          *dataPtr++ = splashRGB8B(*rgb8);
          ++rgb8;
+#endif
+#ifdef POPPLER_0_5
+	 *dataPtr++ = splashRGB8R(color);
+	 *dataPtr++ = splashRGB8G(color);
+	 *dataPtr++ = splashRGB8B(color);
+	 color = color + 3;
+#endif
       }
    }
-   
    return 1;
 }

--- popplerkit.framework.orig/bindings/poppler_text.cc
+++ popplerkit.framework/bindings/poppler_text.cc
@@ -49,8 +49,12 @@
    if (!text_device || !poppler_page || !poppler_document)
       return 0;
 
-   SYNCHRONIZED(PAGE(poppler_page)->display(TEXT_DEV(text_device), hDPI, vDPI, rotate, crop,
-                                            NULL, PDF_DOC(poppler_document)->getCatalog()));
+   SYNCHRONIZED(PAGE(poppler_page)->display(TEXT_DEV(text_device), 
+			   hDPI, vDPI, rotate, 
+#ifdef POPPLER_0_5
+			   gTrue, // useMediaBox
+#endif
+			   crop, NULL, PDF_DOC(poppler_document)->getCatalog()));
    return 1;
 }
 
@@ -66,6 +70,9 @@
       int result = TEXT_DEV(text_device)->findText(text_utf32, text_len,
                                                    start_at_top, stop_at_bottom,
                                                    start_at_last, stop_at_last,
+#ifdef POPPLER_0_5
+						   gTrue, gFalse,
+#endif
                                                    x_min, y_min, x_max, y_max);
    END_SYNCHRONIZED;
    

--- popplerkit.framework.orig/config.sh
+++ popplerkit.framework/config.sh
@@ -15,6 +15,14 @@
 POPPLER_CFLAGS=`${PKG_CONFIG} --cflags poppler`
 POPPLER_LIBS="${POPPLER_LDFLAGS} `${PKG_CONFIG} --libs poppler`"
 
+# fontconfig
+${PKG_CONFIG} --exists fontconfig
+if [ $? -ne 0 ]; then
+    echo "fontconfig library required but not found!"
+    exit 1
+fi
+FONTCONFIG_LIBS=`${PKG_CONFIG} --libs fontconfig`
+
 # poppler splash device
 ${PKG_CONFIG} --exists poppler-splash
 if [ $? -ne 0 ]; then
@@ -30,16 +38,31 @@
    echo "poppler-cairo not found, building without cairo rendering"
    HAVE_CAIRO="NO"
 else
-   HAVE_CAIRO="YES"
-   POPPLER_CFLAGS="${POPPLER_CFLAGS} `${PKG_CONFIG} --cflags poppler-cairo`"
-   POPPLER_LIBS="${POPPLER_LDFLAGS} `${PKG_CONFIG} --libs poppler-cairo`"
+#   Disable Cairo support for now to avoid most of problem
+#   HAVE_CAIRO="YES"
+#   POPPLER_CFLAGS="${POPPLER_CFLAGS} `${PKG_CONFIG} --cflags poppler-cairo`"
+#   POPPLER_LIBS="${POPPLER_LDFLAGS} `${PKG_CONFIG} --libs poppler-cairo`"
+   HAVE_CAIRO="NO"
+fi
+
+# check poppler version
+${PKG_CONFIG} --atleast-version=0.4 poppler
+if [ $? -eq 0 ]; then
+  POPPLER_VERSION="POPPLER_0_4"
+else
+  echo "PopplerKit does not support this version of poppler"
+  exit 1
+fi
+
+${PKG_CONFIG} --atleast-version=0.5 poppler
+if [ $? -eq 0 ]; then
+  POPPLER_VERSION="POPPLER_0_5"
 fi
 
 # include freetype, just to be sure
 ${PKG_CONFIG} --exists freetype2
 if [ $? -eq 0 ]; then
    FT_CFLAGS=`${PKG_CONFIG} --cflags freetype2`
-   FT_LIBS=`${PKG_CONFIG} --libs freetype2`
 else
    FT_CONFIG=`which freetype-config 2>/dev/null`
    if [ -z "${FT_CONFIG}" ]; then
@@ -47,17 +70,19 @@
       exit 1
    fi
    FT_CFLAGS=`${FT_CONFIG} --cflags`
-   FT_LIBS=`${FT_CONFIG} --libs`
 fi
 
 # write config.make
 echo "# config.make, generated at `date`" >config.make
 echo "POPPLER_CFLAGS=${POPPLER_CFLAGS}" >>config.make
 echo "POPPLER_LIBS=${POPPLER_LIBS}" >>config.make
+echo "${POPPLER_VERSION}=YES" >> config.make
 echo "FT_CFLAGS=${FT_CFLAGS}" >> config.make
-echo "FT_LIBS=${FT_LIBS}" >> config.make
-echo "ADDITIONAL_CFLAGS=\$(POPPLER_CFLAGS) \$(FT_CFLAGS)" >> config.make
-echo "ADDITIONAL_LDFLAGS=\$(POPPLER_LIBS) \$(POPPLER_LIBS)" >> config.make
+echo "FONTCONFIG_LIBS=${FONTCONFIG_LIBS}" >> config.make
+echo "ADDITIONAL_CFLAGS+=\$(POPPLER_CFLAGS) \$(FT_CFLAGS)" >> config.make
+echo "LIBRARIES_DEPEND_UPON=\$(OBJC_LIBS) \$(FND_LIBS) \$(GUI_LIBS) \$(FONTCONFIG_LIBS) \$(POPPLER_LIBS)" >> config.make
+# we add -I/usr/X11R6/include for older FreeBSD version.
+echo "ADDITIONAL_INCLUDE_DIRS += -I/usr/X11R6/include" >> config.make
 echo "HAVE_CAIRO=${HAVE_CAIRO}" >>config.make
 
 exit 0
