+static int ResolutionSorter(const void *e1, const void *e2)
+{
+ const PMResolution *res1 = (const PMResolution *)e1;
+ const PMResolution *res2 = (const PMResolution *)e2;
+ const double area1 = res1->hRes * res1->vRes;
+ const double area2 = res2->hRes * res2->vRes;
+
+ if (area1 < area2)
+ return -1;
+ else if (area1 > area2)
+ return 1;
+ else
+ return 0;
+}
+
+static PMResolution *GetSupportedResolutions(PMPrinter printer, UInt32 *count)
+{
+ PMResolution res, *resolutions = NULL;
+ OSStatus status = PMPrinterGetPrinterResolutionCount(printer, count);
+ if (status == kPMNotImplemented)
+ {
+#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
+ resolutions = (PMResolution *)malloc(sizeof(PMResolution) * 4);
+ *count = 0;
+ if (PMPrinterGetPrinterResolution(printer, kPMMinRange, &res) == noErr)
+ resolutions[(*count)++] = res;
+ if (PMPrinterGetPrinterResolution(printer, kPMMinSquareResolution, &res) == noErr)
+ resolutions[(*count)++] = res;
+ if (PMPrinterGetPrinterResolution(printer, kPMMaxSquareResolution, &res) == noErr)
+ resolutions[(*count)++] = res;
+ if (PMPrinterGetPrinterResolution(printer, kPMMaxRange, &res) == noErr)
+ resolutions[(*count)++] = res;
+ if (*count == 0)
+ {
+ if (PMPrinterGetPrinterResolution(printer, kPMDefaultResolution, &res) == noErr)
+ resolutions[(*count)++] = res;
+ }
+#endif
+ }
+ else if (status == noErr)
+ {
+ resolutions = (PMResolution *)malloc(sizeof(PMResolution) * (*count));
+ UInt32 realCount = 0;
+ for (UInt32 i = 0; i < *count; i++)
+ {
+ if (PMPrinterGetIndexedPrinterResolution(printer, i + 1, &res) == noErr)
+ resolutions[realCount++] = res;
+ }
+ qsort(resolutions, realCount, sizeof(PMResolution), ResolutionSorter);
+
+ *count = realCount;
+ }
+ if ((*count == 0) && (resolutions))
+ {
+ free(resolutions);
+ resolutions = NULL;
+ }
+ return resolutions;
+}
+
+
+