Based on !535 (merged) with the addition of one commit in order to fix size calculation in portrait mode.