All pastes #227737 Raw Edit

Anonymous

public text v1 · immutable
#227737 ·published 2006-10-29 14:57 UTC
rendered paste body
Index: core/utils.cpp
===================================================================
--- core/utils.cpp	(revision 600069)
+++ core/utils.cpp	(working copy)
@@ -9,6 +9,17 @@
 
 #include "utils.h"
 
+#ifdef Q_WS_X11
+#include <QX11Info>
+#endif
+
+#ifdef Q_WS_MAC
+#include <ApplicationServices/ApplicationServices.h>
+#include <IOKit/graphics/IOGraphicsLib.h>
+#endif
+
+
+
 using namespace Okular;
 
 QRect Utils::rotateRect( const QRect & source, int width, int height, int orientation )
@@ -37,3 +48,90 @@
 
     return ret;
 }
+
+#if defined(Q_WS_X11)
+double Utils::getDpiX() {
+    return QX11Info::appDpiX();
+}
+
+double Utils::getDpiY() {
+    return QX11Info::appDpiY();
+}
+#elif defined(Q_WS_MAC)
+    /*
+     * Code copied from http://developer.apple.com/qa/qa2001/qa1217.html
+     */
+    //    Handy utility function for retrieving an int from a CFDictionaryRef
+    static int GetIntFromDictionaryForKey( CFDictionaryRef desc, CFStringRef key )
+    {
+        CFNumberRef value;
+        int num = 0;
+        if ( (value = (CFNumberRef)CFDictionaryGetValue(desc, key)) == NULL || CFGetTypeID(value) != CFNumberGetTypeID())
+            return 0;
+        CFNumberGetValue(value, kCFNumberIntType, &num);
+        return num;
+    }
+
+    CGDisplayErr GetDisplayDPI(
+        CFDictionaryRef displayModeDict,
+        CGDirectDisplayID displayID,
+        double *horizontalDPI, double *verticalDPI )
+    {
+        CGDisplayErr err = kCGErrorFailure;
+        io_connect_t displayPort;
+        CFDictionaryRef displayDict;
+
+        //    Grab a connection to IOKit for the requested display
+        displayPort = CGDisplayIOServicePort( displayID );
+        if ( displayPort != MACH_PORT_NULL )
+        {
+            //    Find out what IOKit knows about this display
+            displayDict = IODisplayCreateInfoDictionary(displayPort, 0);
+            if ( displayDict != NULL )
+            {
+                const double mmPerInch = 25.4;
+                double horizontalSizeInInches =
+                    (double)GetIntFromDictionaryForKey(displayDict,
+                                                       CFSTR(kDisplayHorizontalImageSize)) / mmPerInch;
+                double verticalSizeInInches =
+                    (double)GetIntFromDictionaryForKey(displayDict,
+                                                       CFSTR(kDisplayVerticalImageSize)) / mmPerInch;
+
+                //    Make sure to release the dictionary we got from IOKit
+                CFRelease(displayDict);
+
+                // Now we can calculate the actual DPI
+                // with information from the displayModeDict
+                *horizontalDPI =
+                    (double)GetIntFromDictionaryForKey( displayModeDict, kCGDisplayWidth )
+                    / horizontalSizeInInches;
+                *verticalDPI = (double)GetIntFromDictionaryForKey( displayModeDict,
+                        kCGDisplayHeight ) / verticalSizeInInches;
+                err = CGDisplayNoErr;
+            }
+        }
+        return err;
+    }
+
+double Utils::getDpiX() {
+    double x,y;
+    CGDisplayErr err = GetDisplayDPI( CGDisplayCurrentMode(kCGDirectMainDisplay),
+                                      kCGDirectMainDisplay,
+                                      &x, &y );
+
+    return err == CGDisplayNoErr ? x : 72.0;
+}
+
+double Utils::getDpiY() {
+    double x,y;
+    CGDisplayErr err = GetDisplayDPI( CGDisplayCurrentMode(kCGDirectMainDisplay),
+                                      kCGDirectMainDisplay,
+                                      &x, &y );
+
+    return err == CGDisplayNoErr ? y : 72.0;
+}
+#else
+#error "Not yet contributed"
+#endif
+
+
Index: core/utils.h
===================================================================
--- core/utils.h	(revision 600069)
+++ core/utils.h	(working copy)
@@ -28,6 +28,16 @@
      * specified \p orientation .
      */
     static QRect rotateRect( const QRect & source, int width, int height, int orientation );
+
+    /**
+     * Return the horizontal DPI of the main display
+     */
+    static double getDpiX();
+
+    /**
+     * Return the vertical DPI of the main display
+     */
+    static double getDpiY();
 };
 
 }
Index: generators/dvi/generator_dvi.cpp
===================================================================
--- generators/dvi/generator_dvi.cpp	(revision 600069)
+++ generators/dvi/generator_dvi.cpp	(working copy)
@@ -17,7 +17,6 @@
 #include "pageSize.h"
 
 #include <qapplication.h>
-#include <QX11Info>
 #include <qstring.h>
 #include <qurl.h>
 #include <qvector.h>
@@ -45,7 +44,7 @@
 
     kDebug() << "# of pages: " << m_dviRenderer->dviFile->total_pages << endl;
 
-    m_resolution = QX11Info::appDpiY();
+    m_resolution = Okular::Utils::getDpiY();
     loadPages( pagesVector, 0 );
 
     ready = true;
Index: CMakeLists.txt
===================================================================
--- CMakeLists.txt	(revision 600069)
+++ CMakeLists.txt	(working copy)
@@ -62,8 +62,13 @@
 
 kde4_add_library(okularcore SHARED ${okularcore_SRCS})
 
-target_link_libraries(okularcore  ${KDE4_KPARTS_LIBS} ${QT_QTCORE_LIBRARY} ${KDE4_KDEUI_LIBS} ${QT_QTGUI_LIBRARY} m )
+# Special handling for linking okularcore on OSX/Apple
+IF(APPLE)
+    SET(OKULAR_IOKIT "-framework IOKit" CACHE STRING "Apple IOKit framework")
+ENDIF(APPLE)
 
+target_link_libraries(okularcore  ${OKULAR_IOKIT} ${KDE4_KPARTS_LIBS} ${QT_QTCORE_LIBRARY} ${KDE4_KDEUI_LIBS} ${QT_QTGUI_LIBRARY} m )
+
 install(TARGETS okularcore DESTINATION ${LIB_INSTALL_DIR} )
 
 # okularpart