fofi/FoFiTrueType.cc | 55 ++++++++++++++++++++++--------------------------- poppler/GfxFont.cc | 24 ++++++++++++++------- splash/SplashScreen.cc | 12 +++++++--- splash/SplashXPath.cc | 44 +++++++++++++++++---------------------- 4 files changed, 69 insertions(+), 66 deletions(-)
New commits: commit 1df3489392a77e2b75adbafcc2fa10de829c172e Author: Carlos Garcia Campos <[email protected]> Date: Tue Aug 30 16:21:40 2011 +0200 xpdf303: Use std::sort (with functors) in place of qsort It can be significantly faster. Not included changes in SplashXPathScanner.cc since they depend on other changes not yet merged. diff --git a/fofi/FoFiTrueType.cc b/fofi/FoFiTrueType.cc index 4793acf..79d2fe2 100644 --- a/fofi/FoFiTrueType.cc +++ b/fofi/FoFiTrueType.cc @@ -32,6 +32,7 @@ #include <stdlib.h> #include <string.h> +#include <algorithm> #include "goo/gtypes.h" #include "goo/gmem.h" #include "goo/GooString.h" @@ -137,29 +138,26 @@ struct TrueTypeLoca { #define vrt2Tag 0x76727432 #define vertTag 0x76657274 -static int cmpTrueTypeLocaOffset(const void *p1, const void *p2) { - TrueTypeLoca *loca1 = (TrueTypeLoca *)p1; - TrueTypeLoca *loca2 = (TrueTypeLoca *)p2; - - if (loca1->origOffset == loca2->origOffset) { - return loca1->idx - loca2->idx; +struct cmpTrueTypeLocaOffsetFunctor { + bool operator()(const TrueTypeLoca &loca1, const TrueTypeLoca &loca2) { + if (loca1.origOffset == loca2.origOffset) { + return loca1.idx < loca2.idx; + } + return loca1.origOffset < loca2.origOffset; } - return loca1->origOffset - loca2->origOffset; -} - -static int cmpTrueTypeLocaIdx(const void *p1, const void *p2) { - TrueTypeLoca *loca1 = (TrueTypeLoca *)p1; - TrueTypeLoca *loca2 = (TrueTypeLoca *)p2; - - return loca1->idx - loca2->idx; -} +}; -static int cmpTrueTypeTableTag(const void *p1, const void *p2) { - TrueTypeTable *tab1 = (TrueTypeTable *)p1; - TrueTypeTable *tab2 = (TrueTypeTable *)p2; +struct cmpTrueTypeLocaIdxFunctor { + bool operator()(const TrueTypeLoca &loca1, const TrueTypeLoca &loca2) { + return loca1.idx < loca2.idx; + } +}; - return (int)tab1->tag - (int)tab2->tag; -} +struct cmpTrueTypeTableTagFunctor { + bool operator()(const TrueTypeTable &tab1, const TrueTypeTable &tab2) { + return tab1.tag < tab2.tag; + } +}; //------------------------------------------------------------------------ @@ -997,14 +995,13 @@ void FoFiTrueType::writeTTF(FoFiOutputFunc outputFunc, // the same pos value remain in the same order) glyfLen = 0; // make gcc happy if (unsortedLoca) { - qsort(locaTable, nGlyphs + 1, sizeof(TrueTypeLoca), - &cmpTrueTypeLocaOffset); + std::sort(locaTable, locaTable + nGlyphs + 1, + cmpTrueTypeLocaOffsetFunctor()); for (i = 0; i < nGlyphs; ++i) { locaTable[i].len = locaTable[i+1].origOffset - locaTable[i].origOffset; } locaTable[nGlyphs].len = 0; - qsort(locaTable, nGlyphs + 1, sizeof(TrueTypeLoca), - &cmpTrueTypeLocaIdx); + std::sort(locaTable, locaTable + nGlyphs + 1, cmpTrueTypeLocaIdxFunctor()); pos = 0; for (i = 0; i <= nGlyphs; ++i) { locaTable[i].newOffset = pos; @@ -1272,8 +1269,7 @@ void FoFiTrueType::writeTTF(FoFiOutputFunc outputFunc, newTables[j].len = sizeof(os2Tab); ++j; } - qsort(newTables, nNewTables, sizeof(TrueTypeTable), - &cmpTrueTypeTableTag); + std::sort(newTables, newTables + nNewTables, cmpTrueTypeTableTagFunctor()); pos = 12 + nNewTables * 16; for (i = 0; i < nNewTables; ++i) { newTables[i].offset = pos; @@ -1568,14 +1564,13 @@ void FoFiTrueType::cvtSfnts(FoFiOutputFunc outputFunc, locaTable[i].origOffset = 2 * getU16BE(pos + i*2, &ok); } } - qsort(locaTable, nGlyphs + 1, sizeof(TrueTypeLoca), - &cmpTrueTypeLocaOffset); + std::sort(locaTable, locaTable + nGlyphs + 1, + cmpTrueTypeLocaOffsetFunctor()); for (i = 0; i < nGlyphs; ++i) { locaTable[i].len = locaTable[i+1].origOffset - locaTable[i].origOffset; } locaTable[nGlyphs].len = 0; - qsort(locaTable, nGlyphs + 1, sizeof(TrueTypeLoca), - &cmpTrueTypeLocaIdx); + std::sort(locaTable, locaTable + nGlyphs + 1, cmpTrueTypeLocaIdxFunctor()); pos = 0; for (i = 0; i <= nGlyphs; ++i) { locaTable[i].newOffset = pos; diff --git a/poppler/GfxFont.cc b/poppler/GfxFont.cc index d77047d..f4ff78c 100644 --- a/poppler/GfxFont.cc +++ b/poppler/GfxFont.cc @@ -1307,13 +1307,19 @@ Dict *Gfx8BitFont::getResources() { // GfxCIDFont //------------------------------------------------------------------------ -static bool cmpWidthExcep(const GfxFontCIDWidthExcep &w1, const GfxFontCIDWidthExcep &w2) { - return w1.first < w2.first; -} +struct cmpWidthExcepFunctor { + bool operator()(const GfxFontCIDWidthExcep &w1, + const GfxFontCIDWidthExcep &w2) { + return w1.first < w2.first; + } +}; -static bool cmpWidthExcepV(const GfxFontCIDWidthExcepV &w1, const GfxFontCIDWidthExcepV &w2) { - return w1.first < w2.first; -} +struct cmpWidthExcepVFunctor { + bool operator()(const GfxFontCIDWidthExcepV &w1, + const GfxFontCIDWidthExcepV &w2) { + return w1.first < w2.first; + } +}; GfxCIDFont::GfxCIDFont(XRef *xref, const char *tagA, Ref idA, GooString *nameA, Dict *fontDict): @@ -1573,7 +1579,8 @@ GfxCIDFont::GfxCIDFont(XRef *xref, const char *tagA, Ref idA, GooString *nameA, obj3.free(); obj2.free(); } - std::sort(widths.exceps, widths.exceps + widths.nExceps, &cmpWidthExcep); + std::sort(widths.exceps, widths.exceps + widths.nExceps, + cmpWidthExcepFunctor()); } obj1.free(); @@ -1656,7 +1663,8 @@ GfxCIDFont::GfxCIDFont(XRef *xref, const char *tagA, Ref idA, GooString *nameA, obj3.free(); obj2.free(); } - std::sort(widths.excepsV, widths.excepsV + widths.nExcepsV, &cmpWidthExcepV); + std::sort(widths.excepsV, widths.excepsV + widths.nExcepsV, + cmpWidthExcepVFunctor()); } obj1.free(); diff --git a/splash/SplashScreen.cc b/splash/SplashScreen.cc index 6b75c0c..40f2466 100644 --- a/splash/SplashScreen.cc +++ b/splash/SplashScreen.cc @@ -26,6 +26,7 @@ #include <stdlib.h> #include <string.h> +#include <algorithm> #include "goo/gmem.h" #include "SplashMath.h" #include "SplashScreen.h" @@ -46,9 +47,12 @@ struct SplashScreenPoint { int dist; }; -static int cmpDistances(const void *p0, const void *p1) { - return ((SplashScreenPoint *)p0)->dist - ((SplashScreenPoint *)p1)->dist; -} + +struct cmpDistancesFunctor { + bool operator()(const SplashScreenPoint &p0, const SplashScreenPoint &p1) { + return p0.dist < p1.dist; + } +}; //------------------------------------------------------------------------ // SplashScreen @@ -360,7 +364,7 @@ void SplashScreen::buildSCDMatrix(int r) { } } } - qsort(pts, n, sizeof(SplashScreenPoint), &cmpDistances); + std::sort(pts, pts + n, cmpDistancesFunctor()); for (j = 0; j < n; ++j) { // map values in [0 .. n-1] --> [255 .. 1] mat[pts[j].y * size + pts[j].x] = 255 - (254 * j) / (n - 1); diff --git a/splash/SplashXPath.cc b/splash/SplashXPath.cc index a896323..37d164d 100644 --- a/splash/SplashXPath.cc +++ b/splash/SplashXPath.cc @@ -412,31 +412,27 @@ void SplashXPath::addSegment(SplashCoord x0, SplashCoord y0, ++length; } -static bool cmpXPathSegs(const SplashXPathSeg &seg0, const SplashXPathSeg &seg1) { - SplashCoord x0, y0, x1, y1; +struct cmpXPathSegsFunctor { + bool operator()(const SplashXPathSeg &seg0, const SplashXPathSeg &seg1) { + SplashCoord x0, y0, x1, y1; - if (seg0.flags & splashXPathFlip) { - x0 = seg0.x1; - y0 = seg0.y1; - } else { - x0 = seg0.x0; - y0 = seg0.y0; - } - if (seg1.flags & splashXPathFlip) { - x1 = seg1.x1; - y1 = seg1.y1; - } else { - x1 = seg1.x0; - y1 = seg1.y0; - } - if (y0 != y1) { - return (y0 < y1) ? true : false; - } - if (x0 != x1) { - return (x0 < x1) ? true : false; + if (seg0.flags & splashXPathFlip) { + x0 = seg0.x1; + y0 = seg0.y1; + } else { + x0 = seg0.x0; + y0 = seg0.y0; + } + if (seg1.flags & splashXPathFlip) { + x1 = seg1.x1; + y1 = seg1.y1; + } else { + x1 = seg1.x0; + y1 = seg1.y0; + } + return (y0 != y1) ? (y0 < y1) : (x0 < x1); } - return false; -} +}; void SplashXPath::aaScale() { SplashXPathSeg *seg; @@ -451,5 +447,5 @@ void SplashXPath::aaScale() { } void SplashXPath::sort() { - std::sort(segs, segs + length, &cmpXPathSegs); + std::sort(segs, segs + length, cmpXPathSegsFunctor()); } _______________________________________________ poppler mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/poppler
