upgraded to xpdf-3.01pl1
[swftools.git] / pdf2swf / xpdf / Link.cc
index 2d146b5..898ca20 100644 (file)
@@ -117,14 +117,19 @@ GString *LinkAction::getFileSpecName(Object *fileSpecObj) {
 
   // dictionary
   } else if (fileSpecObj->isDict()) {
+#ifdef WIN32
+    if (!fileSpecObj->dictLookup("DOS", &obj1)->isString()) {
+#else
     if (!fileSpecObj->dictLookup("Unix", &obj1)->isString()) {
+#endif
       obj1.free();
       fileSpecObj->dictLookup("F", &obj1);
     }
-    if (obj1.isString())
+    if (obj1.isString()) {
       name = obj1.getString()->copy();
-    else
+    } else {
       error(-1, "Illegal file spec in link");
+    }
     obj1.free();
 
   // error
@@ -132,6 +137,55 @@ GString *LinkAction::getFileSpecName(Object *fileSpecObj) {
     error(-1, "Illegal file spec in link");
   }
 
+  // system-dependent path manipulation
+  if (name) {
+#ifdef WIN32
+    int i, j;
+
+    // "//...."             --> "\...."
+    // "/x/...."            --> "x:\...."
+    // "/server/share/...." --> "\\server\share\...."
+    // convert escaped slashes to slashes and unescaped slashes to backslashes
+    i = 0;
+    if (name->getChar(0) == '/') {
+      if (name->getLength() >= 2 && name->getChar(1) == '/') {
+       name->del(0);
+       i = 0;
+      } else if (name->getLength() >= 2 &&
+                ((name->getChar(1) >= 'a' && name->getChar(1) <= 'z') ||
+                 (name->getChar(1) >= 'A' && name->getChar(1) <= 'Z')) &&
+                (name->getLength() == 2 || name->getChar(2) == '/')) {
+       name->setChar(0, name->getChar(1));
+       name->setChar(1, ':');
+       i = 2;
+      } else {
+       for (j = 2; j < name->getLength(); ++j) {
+         if (name->getChar(j-1) != '\\' &&
+             name->getChar(j) == '/') {
+           break;
+         }
+       }
+       if (j < name->getLength()) {
+         name->setChar(0, '\\');
+         name->insert(0, '\\');
+         i = 2;
+       }
+      }
+    }
+    for (; i < name->getLength(); ++i) {
+      if (name->getChar(i) == '/') {
+       name->setChar(i, '\\');
+      } else if (name->getChar(i) == '\\' &&
+                i+1 < name->getLength() &&
+                name->getChar(i+1) == '/') {
+       name->del(i);
+      }
+    }
+#else
+    // no manipulation needed for Unix
+#endif
+  }
+
   return name;
 }
 
@@ -497,7 +551,7 @@ LinkURI::LinkURI(Object *uriObj, GString *baseURI) {
   uri = NULL;
   if (uriObj->isString()) {
     uri2 = uriObj->getString()->copy();
-    if (baseURI) {
+    if (baseURI && baseURI->getLength() > 0) {
       n = strcspn(uri2->getCString(), "/:");
       if (n == uri2->getLength() || uri2->getChar(n) == '/') {
        uri = baseURI->copy();
@@ -690,7 +744,7 @@ Link::Link(Dict *dict, GString *baseURI) {
     obj2.free();
     if (obj1.dictLookup("D", &obj2)->isArray()) {
       borderDashLength = obj2.arrayGetLength();
-      borderDash = (double *)gmalloc(borderDashLength * sizeof(double));
+      borderDash = (double *)gmallocn(borderDashLength, sizeof(double));
       for (i = 0; i < borderDashLength; ++i) {
        if (obj2.arrayGet(i, &obj3)->isNum()) {
          borderDash[i] = obj3.getNum();
@@ -713,7 +767,7 @@ Link::Link(Dict *dict, GString *baseURI) {
          if (obj1.arrayGet(3, &obj2)->isArray()) {
            borderType = linkBorderDashed;
            borderDashLength = obj2.arrayGetLength();
-           borderDash = (double *)gmalloc(borderDashLength * sizeof(double));
+           borderDash = (double *)gmallocn(borderDashLength, sizeof(double));
            for (i = 0; i < borderDashLength; ++i) {
              if (obj2.arrayGet(i, &obj3)->isNum()) {
                borderDash[i] = obj3.getNum();
@@ -722,6 +776,10 @@ Link::Link(Dict *dict, GString *baseURI) {
              }
              obj3.free();
            }
+         } else {
+           // Adobe draws no border at all if the last element is of
+           // the wrong type.
+           borderWidth = 0;
          }
          obj2.free();
        }
@@ -805,7 +863,7 @@ Links::Links(Object *annots, GString *baseURI) {
          if (link->isOk()) {
            if (numLinks >= size) {
              size += 16;
-             links = (Link **)grealloc(links, size * sizeof(Link *));
+             links = (Link **)greallocn(links, size, sizeof(Link *));
            }
            links[numLinks++] = link;
          } else {