String[] m = pos.split("-");
String p1 = m[0];
String p2 = m[1];
Point a1 = getAnchorXY(elem, p1, true);
Point a2 = getAnchorXY(align, p2, false);
if (offsets == null) {
offsets = new int[] {0, 0};
}
int x = a2.x - a1.x + offsets[0];
int y = a2.y - a1.y + offsets[1];
if (m.length == 3) {
int w = getComputedWidth(elem);
int h = getHeight(elem);
int dw = Window.getClientWidth() - 5;
int dh = Window.getClientHeight() - 5;
Region r = getRegion(elem);
char p1y = p1.charAt(0), p1x = p1.charAt(p1.length() - 1);
char p2y = p2.charAt(0), p2x = p2.charAt(p2.length() - 1);
boolean swapY = ((p1y == 't' && p2y == 'b') || (p1y == 'b' && p2y == 't'));
boolean swapX = ((p1x == 'r' && p2x == 'l') || (p1x == 'l' && p2x == 'r'));
int scrollX = getBodyScrollLeft();
int scrollY = getBodyScrollTop();
if ((x + w) > dw + scrollX) {
x = swapX ? r.left - w : dw + scrollX - w;
}
if (x < scrollX) {
x = swapX ? r.right : scrollX;
}
if ((y + h) > dh + scrollY) {
y = swapY ? r.top - h : dh + scrollY - h;
}
if (y < scrollY) {
y = swapY ? r.bottom : scrollY;
}
}
return new Point(x, y);
}