int yOffset = model.getYOffset();
Box boundingBox = model.getContainer().getConsumableBounds();
TextLocation caretLocation = model.getCaretLocation();
int absoluteCaretY = model.getAbsoluteY(caretLocation);
int relativeCaretY = absoluteCaretY + yOffset;
TypedLayout caretLine = model.getLines().get(caretLocation.line);
int caretHeight = caretLine.getHeight();
if(caretHeight > boundingBox.height)
yOffset = (caretHeight - boundingBox.height) / -2;
else if(relativeCaretY + caretHeight >= boundingBox.height)
yOffset = -absoluteCaretY - caretHeight + boundingBox.height;