Java Matrix2D

PDF-XChange Viewer SDK for Developer's
(ActiveX and Simple DLL Versions)

Moderators: PDF-XChange Support, Daniel - PDF-XChange, Chris - PDF-XChange, Sean - PDF-XChange, Vasyl - PDF-XChange, Ivan - Tracker Software, Stefan - PDF-XChange

tdonovan
User
Posts: 61
Joined: Tue Jun 12, 2007 9:21 pm

Java Matrix2D

Post by tdonovan »

I realize this is an undocumented feature of Java. On my development machine I can use it just fine with the Java I create and run with RunJavaScript.

On a test machine I get an error Matrix2D is not defined. Does anyone know why and what I should/could do about this.

Thanks very much for the help.
tdonovan
User
Posts: 61
Joined: Tue Jun 12, 2007 9:21 pm

Re: Java Matrix2D

Post by tdonovan »

I think I have found my answer. I found the definition of Matrix2D. You have to add the definition of two functions to the Java script you are going to run.

I am now using this as the first part of any Java Script that I need to call Matrix2D

Private Function GetMatrix2D() As String
Dim sb As New StringBuilder
sb.AppendLine("function CBdef(a, b) {")
sb.AppendLine("return typeof a == ""undefined"" ? b : a;")
sb.AppendLine("}")
sb.AppendLine()
sb.AppendLine("function Matrix2D(a, b, c, d, h, v) {")
sb.AppendLine("this.a = CBdef(a, 1);")
sb.AppendLine("this.b = CBdef(b, 0);")
sb.AppendLine("this.c = CBdef(c, 0);")
sb.AppendLine("this.d = CBdef(d, 1);")
sb.AppendLine("this.h = CBdef(h, 0);")
sb.AppendLine("this.v = CBdef(v, 0);")
sb.AppendLine("this.fromRotated = (function (doc, page) {page = CBdef(page, 0);var cropBox = doc.getPageBox(""Crop"", page);var mediaBox = doc.getPageBox(""Media"", page);var mbHeight = mediaBox[1] - mediaBox[3];var mbWidth = mediaBox[2] - mediaBox[0];var rotation = doc.getPageRotation(page);var m = new Matrix2D(1, 0, 0, 1, cropBox[0] - mediaBox[0], cropBox[3] - mediaBox[3]);if (rotation == 90) {return this.concat(m.rotate(Math.asin(1)).translate(mbHeight, 0));} else {if (rotation == 180) {return this.concat(m.rotate(2 * -Math.asin(1)).translate(mbWidth, mbHeight));} else {if (rotation == 270) {return this.concat(m.rotate(-Math.asin(1)).translate(0, mbWidth));}}}return this.concat(m);});")
sb.AppendLine("this.transform = (function (pts) {var result = new Array(pts.length);if (typeof pts[0] == ""object"") {for (var n = 0; n < pts.length; n++) {")
sb.AppendLine("result[n] = this.transform(pts[n]);}} else {for (var n = 0; n + 1 < pts.length; n += 2) {")
sb.AppendLine("result[n] = this.a * pts[n] + this.c * pts[n + 1] + this.h;result[n + 1] = this.b * pts[n] + this.d * pts[n + 1] + this.v;}}return result;});")
sb.AppendLine("this.concat = (function (m) {return new Matrix2D((this.a * m.a) + (this.b * m.c), (this.a * m.b) + (this.b * m.d), (this.c * m.a) + (this.d * m.c), (this.c * m.b) + (this.d * m.d), (this.h * m.a) + (this.v * m.c) + m.h, (this.h * m.b) + (this.v * m.d) + m.v);});")
sb.AppendLine("this.invert = (function () {var result = new Matrix2D();var q = this.b * this.c - this.a * this.d;if (q) {result.a = -this.d / q;result.b = this.b / q;result.c = this.c / q;result.d = -this.a / q;result.h = -(this.h * result.a + this.v * result.c);result.v = -(this.h * result.b + this.v * result.d);}return result;});")
sb.AppendLine("this.translate = (function (dx, dy) {return this.concat(new Matrix2D(1, 0, 0, 1, CBdef(dx, 0), CBdef(dy, 0)));});")
sb.AppendLine("this.scale = (function (sx, sy) {return this.concat(new Matrix2D(CBdef(sx, 1), 0, 0, CBdef(sy, 1), 0, 0));});")
sb.AppendLine("this.rotate = (function (t) {t = CBdef(t, 0);return this.concat(new Matrix2D(Math.cos(t), Math.sin(t), -Math.sin(t), Math.cos(t), 0, 0));});")
sb.AppendLine("}")
sb.AppendLine()
Return sb.ToString
End Function

Seems to be working for me.
User avatar
Ivan - Tracker Software
Site Admin
Posts: 3586
Joined: Thu Jul 08, 2004 10:36 pm

Re: Java Matrix2D

Post by Ivan - Tracker Software »

Matrix2D is defined.
to check this, open JS console (Ctrl + J) and run this piece of code:

Code: Select all

var a = new Matrix2D;
a.toSource();
P.S. Please check if there is resource.dat file into the same folder where Viewer's .exe file is located.
PDF-XChange Co Ltd. (Project Director)

When attaching files to any message - please ensure they are archived and posted as a .ZIP, .RAR or .7z format - or they will not be posted - thanks.