[poppler] poppler/Annot.cc poppler/Gfx.cc poppler/GfxState.cc poppler/Object.h poppler/Sound.cc

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Wed Oct 30 21:40:21 UTC 2019


 poppler/Annot.cc    |   97 +++++++----------------
 poppler/Gfx.cc      |    4 
 poppler/GfxState.cc |  214 +++++++++++++++++-----------------------------------
 poppler/Object.h    |    7 +
 poppler/Sound.cc    |    5 -
 5 files changed, 111 insertions(+), 216 deletions(-)

New commits:
commit 647274f118222cce978b3a79a50ad222d003d133
Author: Albert Astals Cid <aacid at kde.org>
Date:   Sun Oct 27 17:37:58 2019 +0100

    Introduce Object::getNumWithDefaultValue
    
    Is like getNum but instead of asserting if Object is not a num it
    returns the given default value
    
    I find it much easier to read
        rect->x1 = obj1.arrayGet(0).getNumWithDefaultValue(0);
    than
        (obj2 = obj1.arrayGet(0), obj2.isNum() ? rect->x1 = obj2.getNum() : rect->x1 = 0);
    
    On top of it has the benefit of being slightly faster

diff --git a/poppler/Annot.cc b/poppler/Annot.cc
index 8b718d97..5ce75a8b 100644
--- a/poppler/Annot.cc
+++ b/poppler/Annot.cc
@@ -187,11 +187,10 @@ static AnnotExternalDataType parseAnnotExternalData(Dict* dict) {
 static std::unique_ptr<PDFRectangle> parseDiffRectangle(Array *array, PDFRectangle *rect) {
   if (array->getLength() == 4) {
     // deltas
-    Object obj1;
-    double dx1 = (obj1 = array->get(0), obj1.isNum() ? obj1.getNum() : 0);
-    double dy1 = (obj1 = array->get(1), obj1.isNum() ? obj1.getNum() : 0);
-    double dx2 = (obj1 = array->get(2), obj1.isNum() ? obj1.getNum() : 0);
-    double dy2 = (obj1 = array->get(3), obj1.isNum() ? obj1.getNum() : 0);
+    const double dx1 = array->get(0).getNumWithDefaultValue(0);
+    const double dy1 = array->get(1).getNumWithDefaultValue(0);
+    const double dx2 = array->get(2).getNumWithDefaultValue(0);
+    const double dy2 = array->get(3).getNumWithDefaultValue(0);
 
     // checking that the numbers are valid (i.e. >= 0),
     // and that applying the differences still give us a valid rect
@@ -260,9 +259,8 @@ AnnotBorderEffect::AnnotBorderEffect(Dict *dict) {
     effectType = borderEffectNoEffect;
   }
 
-  obj1 = dict->lookup("I");
-  if (obj1.isNum() && effectType == borderEffectCloudy) {
-    intensity = obj1.getNum();
+  if (effectType == borderEffectCloudy) {
+    intensity = dict->lookup("I").getNumWithDefaultValue(0);
   } else {
     intensity = 0;
   }
@@ -844,9 +842,8 @@ AnnotIconFit::AnnotIconFit(Dict* dict) {
 
   obj1 = dict->lookup("A");
   if (obj1.isArray() && obj1.arrayGetLength() == 2) {
-    Object obj2;
-    (obj2 = obj1.arrayGet(0), obj2.isNum() ? left = obj2.getNum() : left = 0);
-    (obj2 = obj1.arrayGet(1), obj2.isNum() ? bottom = obj2.getNum() : bottom = 0);
+    left = obj1.arrayGet(0).getNumWithDefaultValue(0);
+    bottom = obj1.arrayGet(1).getNumWithDefaultValue(0);
 
     if (left < 0 || left > 1)
       left = 0.5;
@@ -1185,11 +1182,10 @@ void Annot::initialize(PDFDoc *docA, Dict *dict) {
   rect = std::make_unique<PDFRectangle>();
   obj1 = dict->lookup("Rect");
   if (obj1.isArray() && obj1.arrayGetLength() == 4) {
-    Object obj2;
-    (obj2 = obj1.arrayGet(0), obj2.isNum() ? rect->x1 = obj2.getNum() : rect->x1 = 0);
-    (obj2 = obj1.arrayGet(1), obj2.isNum() ? rect->y1 = obj2.getNum() : rect->y1 = 0);
-    (obj2 = obj1.arrayGet(2), obj2.isNum() ? rect->x2 = obj2.getNum() : rect->x2 = 1);
-    (obj2 = obj1.arrayGet(3), obj2.isNum() ? rect->y2 = obj2.getNum() : rect->y2 = 1);
+    rect->x1 = obj1.arrayGet(0).getNumWithDefaultValue(0);
+    rect->y1 = obj1.arrayGet(1).getNumWithDefaultValue(0);
+    rect->x2 = obj1.arrayGet(2).getNumWithDefaultValue(1);
+    rect->y2 = obj1.arrayGet(3).getNumWithDefaultValue(1);
 
     if (rect->x1 > rect->x2) {
       double t = rect->x1;
@@ -1980,12 +1976,7 @@ void AnnotMarkup::initialize(PDFDoc *docA, Dict *dict) {
     popup = std::make_unique<AnnotPopup>(docA, std::move(popupObj), &obj2);
   }
 
-  obj1 = dict->lookup("CA");
-  if (obj1.isNum()) {
-    opacity = obj1.getNum();
-  } else {
-    opacity = 1.0;
-  }
+  opacity = dict->lookup("CA").getNumWithDefaultValue(1.0);
 
   obj1 = dict->lookup("CreationDate");
   if (obj1.isString()) {
@@ -2666,18 +2657,14 @@ void AnnotFreeText::initialize(PDFDoc *docA, Dict *dict) {
 
   obj1 = dict->lookup("CL");
   if (obj1.isArray() && obj1.arrayGetLength() >= 4) {
-    double x1, y1, x2, y2;
-    Object obj2;
-
-    (obj2 = obj1.arrayGet(0), obj2.isNum() ? x1 = obj2.getNum() : x1 = 0);
-    (obj2 = obj1.arrayGet(1), obj2.isNum() ? y1 = obj2.getNum() : y1 = 0);
-    (obj2 = obj1.arrayGet(2), obj2.isNum() ? x2 = obj2.getNum() : x2 = 0);
-    (obj2 = obj1.arrayGet(3), obj2.isNum() ? y2 = obj2.getNum() : y2 = 0);
+    const double x1 = obj1.arrayGet(0).getNumWithDefaultValue(0);
+    const double y1 = obj1.arrayGet(1).getNumWithDefaultValue(0);
+    const double x2 = obj1.arrayGet(2).getNumWithDefaultValue(0);
+    const double y2 = obj1.arrayGet(3).getNumWithDefaultValue(0);
 
     if (obj1.arrayGetLength() == 6) {
-      double x3, y3;
-      (obj2 = obj1.arrayGet(4), obj2.isNum() ? x3 = obj2.getNum() : x3 = 0);
-      (obj2 = obj1.arrayGet(5), obj2.isNum() ? y3 = obj2.getNum() : y3 = 0);
+      const double x3 = obj1.arrayGet(4).getNumWithDefaultValue(0);
+      const double y3 = obj1.arrayGet(5).getNumWithDefaultValue(0);
       calloutLine = std::make_unique<AnnotCalloutMultiLine>(x1, y1, x2, y2, x3, y3);
     } else {
       calloutLine = std::make_unique<AnnotCalloutLine>(x1, y1, x2, y2);
@@ -3002,13 +2989,10 @@ void AnnotLine::initialize(PDFDoc *docA, Dict *dict) {
 
   obj1 = dict->lookup("L");
   if (obj1.isArray() && obj1.arrayGetLength() == 4) {
-    Object obj2;
-    double x1, y1, x2, y2;
-
-    (obj2 = obj1.arrayGet(0), obj2.isNum() ? x1 = obj2.getNum() : x1 = 0);
-    (obj2 = obj1.arrayGet(1), obj2.isNum() ? y1 = obj2.getNum() : y1 = 0);
-    (obj2 = obj1.arrayGet(2), obj2.isNum() ? x2 = obj2.getNum() : x2 = 0);
-    (obj2 = obj1.arrayGet(3), obj2.isNum() ? y2 = obj2.getNum() : y2 = 0);
+    const double x1 = obj1.arrayGet(0).getNumWithDefaultValue(0);
+    const double y1 = obj1.arrayGet(1).getNumWithDefaultValue(0);
+    const double x2 = obj1.arrayGet(2).getNumWithDefaultValue(0);
+    const double y2 = obj1.arrayGet(3).getNumWithDefaultValue(0);
 
     coord1 = std::make_unique<AnnotCoord>(x1, y1);
     coord2 = std::make_unique<AnnotCoord>(x2, y2);
@@ -3046,22 +3030,11 @@ void AnnotLine::initialize(PDFDoc *docA, Dict *dict) {
     interiorColor = std::make_unique<AnnotColor>(obj1.getArray());
   }
 
-  obj1 = dict->lookup("LL");
-  if (obj1.isNum()) {
-    leaderLineLength = obj1.getNum();
-  } else {
-    leaderLineLength = 0;
-  }
+  leaderLineLength = dict->lookup("LL").getNumWithDefaultValue(0);
 
-  obj1 = dict->lookup("LLE");
-  if (obj1.isNum()) {
-    leaderLineExtension = obj1.getNum();
-
-    if (leaderLineExtension < 0)
-      leaderLineExtension = 0;
-  } else {
+  leaderLineExtension = dict->lookup("LLE").getNumWithDefaultValue(0);
+  if (leaderLineExtension < 0)
     leaderLineExtension = 0;
-  }
 
   obj1 = dict->lookup("Cap");
   if (obj1.isBool()) {
@@ -3085,15 +3058,9 @@ void AnnotLine::initialize(PDFDoc *docA, Dict *dict) {
     intent = intentLineArrow;
   }
 
-  obj1 = dict->lookup("LLO");
-  if (obj1.isNum()) {
-    leaderLineOffset = obj1.getNum();
-
-    if (leaderLineOffset < 0)
-      leaderLineOffset = 0;
-  } else {
+  leaderLineOffset = dict->lookup("LLO").getNumWithDefaultValue(0);
+  if (leaderLineOffset < 0)
     leaderLineOffset = 0;
-  }
 
   obj1 = dict->lookup("CP");
   if (obj1.isName()) {
@@ -3119,12 +3086,8 @@ void AnnotLine::initialize(PDFDoc *docA, Dict *dict) {
 
   obj1 = dict->lookup("CO");
   if (obj1.isArray() && (obj1.arrayGetLength() == 2)) {
-    Object obj2;
-
-    obj2 = obj1.arrayGet(0);
-    captionTextHorizontal = obj2.isNum() ? obj2.getNum() : 0;
-    obj2 = obj1.arrayGet(1);
-    captionTextVertical = obj2.isNum() ? obj2.getNum() : 0;
+    captionTextHorizontal = obj1.arrayGet(0).getNumWithDefaultValue(0);
+    captionTextVertical = obj1.arrayGet(1).getNumWithDefaultValue(0);
   } else {
     captionTextHorizontal = captionTextVertical = 0;
   }
diff --git a/poppler/Gfx.cc b/poppler/Gfx.cc
index ec2b4b35..c5d47443 100644
--- a/poppler/Gfx.cc
+++ b/poppler/Gfx.cc
@@ -946,10 +946,8 @@ void Gfx::opSetDash(Object args[], int numArgs) {
     dash = nullptr;
   } else {
     dash = (double *)gmallocn(length, sizeof(double));
-    bool dummyOk;
     for (i = 0; i < length; ++i) {
-      const Object obj = a->get(i);
-      dash[i] = obj.getNum(&dummyOk);
+      dash[i] = a->get(i).getNumWithDefaultValue(0);
     }
   }
   state->setLineDash(dash, length, args[1].getNum());
diff --git a/poppler/GfxState.cc b/poppler/GfxState.cc
index 43c91319..b7347d35 100644
--- a/poppler/GfxState.cc
+++ b/poppler/GfxState.cc
@@ -796,33 +796,19 @@ GfxColorSpace *GfxCalGrayColorSpace::parse(Array *arr, GfxState *state) {
   cs = new GfxCalGrayColorSpace();
   obj2 = obj1.dictLookup("WhitePoint");
   if (obj2.isArray() && obj2.arrayGetLength() == 3) {
-    Object obj3 = obj2.arrayGet(0);
-    if (likely(obj3.isNum()))
-      cs->whiteX = obj3.getNum();
-    obj3 = obj2.arrayGet(1);
-    if (likely(obj3.isNum()))
-      cs->whiteY = obj3.getNum();
-    obj3 = obj2.arrayGet(2);
-    if (likely(obj3.isNum()))
-      cs->whiteZ = obj3.getNum();
+    cs->whiteX = obj2.arrayGet(0).getNumWithDefaultValue(1);
+    cs->whiteY = obj2.arrayGet(1).getNumWithDefaultValue(1);
+    cs->whiteZ = obj2.arrayGet(2).getNumWithDefaultValue(1);
   }
   obj2 = obj1.dictLookup("BlackPoint");
   if (obj2.isArray() && obj2.arrayGetLength() == 3) {
-    Object obj3 = obj2.arrayGet(0);
-    if (likely(obj3.isNum()))
-      cs->blackX = obj3.getNum();
-    obj3 = obj2.arrayGet(1);
-    if (likely(obj3.isNum()))
-      cs->blackY = obj3.getNum();
-    obj3 = obj2.arrayGet(2);
-    if (likely(obj3.isNum()))
-      cs->blackZ = obj3.getNum();
-  }
-  obj2 = obj1.dictLookup("Gamma");
-  if (obj2.isNum()) {
-    cs->gamma = obj2.getNum();
+    cs->blackX = obj2.arrayGet(0).getNumWithDefaultValue(0);
+    cs->blackY = obj2.arrayGet(1).getNumWithDefaultValue(0);
+    cs->blackZ = obj2.arrayGet(2).getNumWithDefaultValue(0);
   }
 
+  cs->gamma = obj1.dictLookup("Gamma").getNumWithDefaultValue(1);
+
   cs->kr = 1 / (xyzrgb[0][0] * cs->whiteX +
 		xyzrgb[0][1] * cs->whiteY +
 		xyzrgb[0][2] * cs->whiteZ);
@@ -1166,39 +1152,21 @@ GfxColorSpace *GfxCalRGBColorSpace::parse(Array *arr, GfxState *state) {
   cs = new GfxCalRGBColorSpace();
   obj2 = obj1.dictLookup("WhitePoint");
   if (obj2.isArray() && obj2.arrayGetLength() == 3) {
-    Object obj3 = obj2.arrayGet(0);
-    if (likely(obj3.isNum()))
-      cs->whiteX = obj3.getNum();
-    obj3 = obj2.arrayGet(1);
-    if (likely(obj3.isNum()))
-      cs->whiteY = obj3.getNum();
-    obj3 = obj2.arrayGet(2);
-    if (likely(obj3.isNum()))
-      cs->whiteZ = obj3.getNum();
+    cs->whiteX = obj2.arrayGet(0).getNumWithDefaultValue(1);
+    cs->whiteY = obj2.arrayGet(1).getNumWithDefaultValue(1);
+    cs->whiteZ = obj2.arrayGet(2).getNumWithDefaultValue(1);
   }
   obj2 = obj1.dictLookup("BlackPoint");
   if (obj2.isArray() && obj2.arrayGetLength() == 3) {
-    Object obj3 = obj2.arrayGet(0);
-    if (likely(obj3.isNum()))
-      cs->blackX = obj3.getNum();
-    obj3 = obj2.arrayGet(1);
-    if (likely(obj3.isNum()))
-      cs->blackY = obj3.getNum();
-    obj3 = obj2.arrayGet(2);
-    if (likely(obj3.isNum()))
-      cs->blackZ = obj3.getNum();
+    cs->blackX = obj2.arrayGet(0).getNumWithDefaultValue(0);
+    cs->blackY = obj2.arrayGet(1).getNumWithDefaultValue(0);
+    cs->blackZ = obj2.arrayGet(2).getNumWithDefaultValue(0);
   }
   obj2 = obj1.dictLookup("Gamma");
   if (obj2.isArray() && obj2.arrayGetLength() == 3) {
-    Object obj3 = obj2.arrayGet(0);
-    if (likely(obj3.isNum()))
-      cs->gammaR = obj3.getNum();
-    obj3 = obj2.arrayGet(1);
-    if (likely(obj3.isNum()))
-      cs->gammaG = obj3.getNum();
-    obj3 = obj2.arrayGet(2);
-    if (likely(obj3.isNum()))
-      cs->gammaB = obj3.getNum();
+    cs->gammaR = obj2.arrayGet(0).getNumWithDefaultValue(1);
+    cs->gammaG = obj2.arrayGet(1).getNumWithDefaultValue(1);
+    cs->gammaB = obj2.arrayGet(2).getNumWithDefaultValue(1);
   }
   obj2 = obj1.dictLookup("Matrix");
   if (obj2.isArray() && obj2.arrayGetLength() == 9) {
@@ -1537,32 +1505,22 @@ GfxColorSpace *GfxLabColorSpace::parse(Array *arr, GfxState *state) {
   bool ok = true;
   obj2 = obj1.dictLookup("WhitePoint");
   if (obj2.isArray() && obj2.arrayGetLength() == 3) {
-    Object obj3 = obj2.arrayGet(0);
-    cs->whiteX = obj3.getNum(&ok);
-    obj3 = obj2.arrayGet(1);
-    cs->whiteY = obj3.getNum(&ok);
-    obj3 = obj2.arrayGet(2);
-    cs->whiteZ = obj3.getNum(&ok);
+    cs->whiteX = obj2.arrayGet(0).getNum(&ok);
+    cs->whiteY = obj2.arrayGet(1).getNum(&ok);
+    cs->whiteZ = obj2.arrayGet(2).getNum(&ok);
   }
   obj2 = obj1.dictLookup("BlackPoint");
   if (obj2.isArray() && obj2.arrayGetLength() == 3) {
-    Object obj3 = obj2.arrayGet(0);
-    cs->blackX = obj3.getNum(&ok);
-    obj3 = obj2.arrayGet(1);
-    cs->blackY = obj3.getNum(&ok);
-    obj3 = obj2.arrayGet(2);
-    cs->blackZ = obj3.getNum(&ok);
+    cs->blackX = obj2.arrayGet(0).getNum(&ok);
+    cs->blackY = obj2.arrayGet(1).getNum(&ok);
+    cs->blackZ = obj2.arrayGet(2).getNum(&ok);
   }
   obj2 = obj1.dictLookup("Range");
   if (obj2.isArray() && obj2.arrayGetLength() == 4) {
-    Object obj3 = obj2.arrayGet(0);
-    cs->aMin = obj3.getNum(&ok);
-    obj3 = obj2.arrayGet(1);
-    cs->aMax = obj3.getNum(&ok);
-    obj3 = obj2.arrayGet(2);
-    cs->bMin = obj3.getNum(&ok);
-    obj3 = obj2.arrayGet(3);
-    cs->bMax = obj3.getNum(&ok);
+    cs->aMin = obj2.arrayGet(0).getNum(&ok);
+    cs->aMax = obj2.arrayGet(1).getNum(&ok);
+    cs->bMin = obj2.arrayGet(2).getNum(&ok);
+    cs->bMax = obj2.arrayGet(3).getNum(&ok);
   }
 
   if (!ok) {
@@ -1818,7 +1776,7 @@ GfxColorSpace *GfxICCBasedColorSpace::parse(Array *arr, OutputDev *out, GfxState
   int nCompsA;
   GfxColorSpace *altA;
   Dict *dict;
-  Object obj1, obj2, obj3;
+  Object obj1, obj2;
   int i;
 
   if (arr->getLength() < 2) {
@@ -1897,14 +1855,9 @@ GfxColorSpace *GfxICCBasedColorSpace::parse(Array *arr, OutputDev *out, GfxState
   cs = new GfxICCBasedColorSpace(nCompsA, altA);
   obj2 = dict->lookup("Range");
   if (obj2.isArray() && obj2.arrayGetLength() == 2 * nCompsA) {
-    Object obj4;
     for (i = 0; i < nCompsA; ++i) {
-      obj3 = obj2.arrayGet(2*i);
-      obj4 = obj2.arrayGet(2*i+1);
-      if (obj3.isNum() && obj4.isNum()) {
-        cs->rangeMin[i] = obj3.getNum();
-        cs->rangeMax[i] = obj4.getNum();
-      }
+      cs->rangeMin[i] = obj2.arrayGet(2*i).getNumWithDefaultValue(0);
+      cs->rangeMax[i] = obj2.arrayGet(2*i+1).getNumWithDefaultValue(1);
     }
   }
 
@@ -3624,18 +3577,12 @@ bool GfxShading::init(GfxResources *res, Dict *dict, OutputDev *out, GfxState *s
   obj1 = dict->lookup("BBox");
   if (obj1.isArray()) {
     if (obj1.arrayGetLength() == 4) {
-      Object obj2 = obj1.arrayGet(0);
-      Object obj3 = obj1.arrayGet(1);
-      Object obj4 = obj1.arrayGet(2);
-      Object obj5 = obj1.arrayGet(3);
-      if (obj2.isNum() && obj3.isNum() && obj4.isNum() && obj5.isNum())
-      {
-        hasBBox = true;
-        bbox_xMin = obj2.getNum();
-        bbox_yMin = obj3.getNum();
-        bbox_xMax = obj4.getNum();
-        bbox_yMax = obj5.getNum();
-      } else {
+      hasBBox = true;
+      bbox_xMin = obj1.arrayGet(0).getNum(&hasBBox);
+      bbox_yMin = obj1.arrayGet(1).getNum(&hasBBox);
+      bbox_xMax = obj1.arrayGet(2).getNum(&hasBBox);
+      bbox_yMax = obj1.arrayGet(3).getNum(&hasBBox);
+      if (!hasBBox) {
         error(errSyntaxWarning, -1, "Bad BBox in shading dictionary (Values not numbers)");
       }
     } else {
@@ -3711,11 +3658,10 @@ GfxFunctionShading *GfxFunctionShading::parse(GfxResources *res, Dict *dict, Out
   obj1 = dict->lookup("Domain");
   if (obj1.isArray() && obj1.arrayGetLength() == 4) {
     bool decodeOk = true;
-    Object obj2;
-    x0A = (obj2 = obj1.arrayGet(0), obj2.getNum(&decodeOk));
-    x1A = (obj2 = obj1.arrayGet(1), obj2.getNum(&decodeOk));
-    y0A = (obj2 = obj1.arrayGet(2), obj2.getNum(&decodeOk));
-    y1A = (obj2 = obj1.arrayGet(3), obj2.getNum(&decodeOk));
+    x0A = obj1.arrayGet(0).getNum(&decodeOk);
+    x1A = obj1.arrayGet(1).getNum(&decodeOk);
+    y0A = obj1.arrayGet(2).getNum(&decodeOk);
+    y1A = obj1.arrayGet(3).getNum(&decodeOk);
 
     if (!decodeOk) {
       error(errSyntaxWarning, -1, "Invalid Domain array in function shading dictionary");
@@ -3729,13 +3675,12 @@ GfxFunctionShading *GfxFunctionShading::parse(GfxResources *res, Dict *dict, Out
   obj1 = dict->lookup("Matrix");
   if (obj1.isArray() && obj1.arrayGetLength() == 6) {
     bool decodeOk = true;
-    Object obj2;
-    matrixA[0] = (obj2 = obj1.arrayGet(0), obj2.getNum(&decodeOk));
-    matrixA[1] = (obj2 = obj1.arrayGet(1), obj2.getNum(&decodeOk));
-    matrixA[2] = (obj2 = obj1.arrayGet(2), obj2.getNum(&decodeOk));
-    matrixA[3] = (obj2 = obj1.arrayGet(3), obj2.getNum(&decodeOk));
-    matrixA[4] = (obj2 = obj1.arrayGet(4), obj2.getNum(&decodeOk));
-    matrixA[5] = (obj2 = obj1.arrayGet(5), obj2.getNum(&decodeOk));
+    matrixA[0] = obj1.arrayGet(0).getNum(&decodeOk);
+    matrixA[1] = obj1.arrayGet(1).getNum(&decodeOk);
+    matrixA[2] = obj1.arrayGet(2).getNum(&decodeOk);
+    matrixA[3] = obj1.arrayGet(3).getNum(&decodeOk);
+    matrixA[4] = obj1.arrayGet(4).getNum(&decodeOk);
+    matrixA[5] = obj1.arrayGet(5).getNum(&decodeOk);
 
     if (!decodeOk) {
       error(errSyntaxWarning, -1, "Invalid Matrix array in function shading dictionary");
@@ -4039,16 +3984,10 @@ GfxAxialShading *GfxAxialShading::parse(GfxResources *res, Dict *dict, OutputDev
   x0A = y0A = x1A = y1A = 0;
   obj1 = dict->lookup("Coords");
   if (obj1.isArray() && obj1.arrayGetLength() == 4) {
-    Object obj2 = obj1.arrayGet(0);
-    Object obj3 = obj1.arrayGet(1);
-    Object obj4 = obj1.arrayGet(2);
-    Object obj5 = obj1.arrayGet(3);
-    if (obj2.isNum() && obj3.isNum() && obj4.isNum() && obj5.isNum()) {
-      x0A = obj2.getNum();
-      y0A = obj3.getNum();
-      x1A = obj4.getNum();
-      y1A = obj5.getNum();
-    }
+    x0A = obj1.arrayGet(0).getNumWithDefaultValue(0);
+    y0A = obj1.arrayGet(1).getNumWithDefaultValue(0);
+    x1A = obj1.arrayGet(2).getNumWithDefaultValue(0);
+    y1A = obj1.arrayGet(3).getNumWithDefaultValue(0);
   } else {
     error(errSyntaxWarning, -1, "Missing or invalid Coords in shading dictionary");
     return nullptr;
@@ -4058,12 +3997,8 @@ GfxAxialShading *GfxAxialShading::parse(GfxResources *res, Dict *dict, OutputDev
   t1A = 1;
   obj1 = dict->lookup("Domain");
   if (obj1.isArray() && obj1.arrayGetLength() == 2) {
-    Object obj2 = obj1.arrayGet(0);
-    Object obj3 = obj1.arrayGet(1);
-    if (obj2.isNum() && obj3.isNum()) {
-      t0A = obj2.getNum();
-      t1A = obj3.getNum();
-    }
+    t0A = obj1.arrayGet(0).getNumWithDefaultValue(0);
+    t1A = obj1.arrayGet(1).getNumWithDefaultValue(1);
   }
 
   obj1 = dict->lookup("Function");
@@ -4234,14 +4169,12 @@ GfxRadialShading *GfxRadialShading::parse(GfxResources *res, Dict *dict, OutputD
   x0A = y0A = r0A = x1A = y1A = r1A = 0;
   obj1 = dict->lookup("Coords");
   if (obj1.isArray() && obj1.arrayGetLength() == 6) {
-    Object obj2;
-    bool dummy; // just so that we can use the getNum that returns 0 on obj2 not being a num instead of aborting
-    x0A = (obj2 = obj1.arrayGet(0), obj2.getNum(&dummy));
-    y0A = (obj2 = obj1.arrayGet(1), obj2.getNum(&dummy));
-    r0A = (obj2 = obj1.arrayGet(2), obj2.getNum(&dummy));
-    x1A = (obj2 = obj1.arrayGet(3), obj2.getNum(&dummy));
-    y1A = (obj2 = obj1.arrayGet(4), obj2.getNum(&dummy));
-    r1A = (obj2 = obj1.arrayGet(5), obj2.getNum(&dummy));
+    x0A = obj1.arrayGet(0).getNumWithDefaultValue(0);
+    y0A = obj1.arrayGet(1).getNumWithDefaultValue(0);
+    r0A = obj1.arrayGet(2).getNumWithDefaultValue(0);
+    x1A = obj1.arrayGet(3).getNumWithDefaultValue(0);
+    y1A = obj1.arrayGet(4).getNumWithDefaultValue(0);
+    r1A = obj1.arrayGet(5).getNumWithDefaultValue(0);
   } else {
     error(errSyntaxWarning, -1, "Missing or invalid Coords in shading dictionary");
     return nullptr;
@@ -4251,9 +4184,8 @@ GfxRadialShading *GfxRadialShading::parse(GfxResources *res, Dict *dict, OutputD
   t1A = 1;
   obj1 = dict->lookup("Domain");
   if (obj1.isArray() && obj1.arrayGetLength() == 2) {
-    Object obj2;
-    t0A = (obj2 = obj1.arrayGet(0), obj2.isNum() ? obj2.getNum() : 0);
-    t1A = (obj2 = obj1.arrayGet(1), obj2.isNum() ? obj2.getNum() : 1);
+    t0A = obj1.arrayGet(0).getNumWithDefaultValue(0);
+    t1A = obj1.arrayGet(1).getNumWithDefaultValue(1);
   }
 
   obj1 = dict->lookup("Function");
@@ -4792,17 +4724,16 @@ GfxGouraudTriangleShading *GfxGouraudTriangleShading::parse(GfxResources *res, i
   }
   obj1 = dict->lookup("Decode");
   if (obj1.isArray() && obj1.arrayGetLength() >= 6) {
-    Object obj2;
     bool decodeOk = true;
-    xMin = (obj2 = obj1.arrayGet(0), obj2.getNum(&decodeOk));
-    xMax = (obj2 = obj1.arrayGet(1), obj2.getNum(&decodeOk));
+    xMin = obj1.arrayGet(0).getNum(&decodeOk);
+    xMax = obj1.arrayGet(1).getNum(&decodeOk);
     xMul = (xMax - xMin) / (pow(2.0, coordBits) - 1);
-    yMin = (obj2 = obj1.arrayGet(2), obj2.getNum(&decodeOk));
-    yMax = (obj2 = obj1.arrayGet(3), obj2.getNum(&decodeOk));
+    yMin = obj1.arrayGet(2).getNum(&decodeOk);
+    yMax = obj1.arrayGet(3).getNum(&decodeOk);
     yMul = (yMax - yMin) / (pow(2.0, coordBits) - 1);
     for (i = 0; 5 + 2*i < obj1.arrayGetLength() && i < gfxColorMaxComps; ++i) {
-      cMin[i] = (obj2 = obj1.arrayGet(4 + 2*i), obj2.getNum(&decodeOk));
-      cMax[i] = (obj2 = obj1.arrayGet(5 + 2*i), obj2.getNum(&decodeOk));
+      cMin[i] = obj1.arrayGet(4 + 2*i).getNum(&decodeOk);
+      cMax[i] = obj1.arrayGet(5 + 2*i).getNum(&decodeOk);
       cMul[i] = (cMax[i] - cMin[i]) / (double)((1u << compBits) - 1);
     }
     nComps = i;
@@ -5144,16 +5075,15 @@ GfxPatchMeshShading *GfxPatchMeshShading::parse(GfxResources *res, int typeA, Di
   obj1 = dict->lookup("Decode");
   if (obj1.isArray() && obj1.arrayGetLength() >= 6) {
     bool decodeOk = true;
-    Object obj2;
-    xMin = (obj2 = obj1.arrayGet(0), obj2.getNum(&decodeOk));
-    xMax = (obj2 = obj1.arrayGet(1), obj2.getNum(&decodeOk));
+    xMin = obj1.arrayGet(0).getNum(&decodeOk);
+    xMax = obj1.arrayGet(1).getNum(&decodeOk);
     xMul = (xMax - xMin) / (pow(2.0, coordBits) - 1);
-    yMin = (obj2 = obj1.arrayGet(2), obj2.getNum(&decodeOk));
-    yMax = (obj2 = obj1.arrayGet(3), obj2.getNum(&decodeOk));
+    yMin = obj1.arrayGet(2).getNum(&decodeOk);
+    yMax = obj1.arrayGet(3).getNum(&decodeOk);
     yMul = (yMax - yMin) / (pow(2.0, coordBits) - 1);
     for (i = 0; 5 + 2*i < obj1.arrayGetLength() && i < gfxColorMaxComps; ++i) {
-      cMin[i] = (obj2 = obj1.arrayGet(4 + 2*i), obj2.getNum(&decodeOk));
-      cMax[i] = (obj2 = obj1.arrayGet(5 + 2*i), obj2.getNum(&decodeOk));
+      cMin[i] = obj1.arrayGet(4 + 2*i).getNum(&decodeOk);
+      cMax[i] = obj1.arrayGet(5 + 2*i).getNum(&decodeOk);
       cMul[i] = (cMax[i] - cMin[i]) / (double)((1u << compBits) - 1);
     }
     nComps = i;
diff --git a/poppler/Object.h b/poppler/Object.h
index 77ae34bf..c01a191f 100644
--- a/poppler/Object.h
+++ b/poppler/Object.h
@@ -307,6 +307,13 @@ public:
   const char *getTypeName() const;
   void print(FILE *f = stdout) const;
 
+  double getNumWithDefaultValue(double defaultValue) const {
+    if (unlikely(type != objInt && type != objInt64 && type != objReal)) {
+      return defaultValue;
+    }
+    return type == objInt ? (double)intg : type == objInt64 ? (double)int64g : real;
+  }
+
 private:
   // Free object contents.
   void free();
diff --git a/poppler/Sound.cc b/poppler/Sound.cc
index aa2ac8f2..c42051c8 100644
--- a/poppler/Sound.cc
+++ b/poppler/Sound.cc
@@ -74,10 +74,7 @@ Sound::Sound(const Object *obj, bool readAttrs)
       kind = soundEmbedded;
     }
     // sampling rate
-    tmp = dict->lookup("R");
-    if (tmp.isNum()) {
-      samplingRate = tmp.getNum();
-    }
+    samplingRate = dict->lookup("R").getNumWithDefaultValue(0);
     // sound channels
     tmp = dict->lookup("C");
     if (tmp.isInt()) {


More information about the poppler mailing list