Skip to content

Commit db8ef26

Browse files
committed
adjust_to_aspect_ratio and divide_padding
1 parent cbdf6b9 commit db8ef26

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

impressive.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,29 @@ def size(self):
553553
def offset(self):
554554
return (self.offset_x, self.offset_y)
555555

556+
def divide_padding(self, amount, ratio):
557+
sum = ratio[0] + ratio[1]
558+
if sum > 0:
559+
return amount * ratio[0] / sum
560+
else:
561+
return 0
562+
563+
def adjust_to_aspect_ratio(self, aspect_ratio, vertical_padding_ratio=(1,1), horizontal_padding_ratio=(1,1)):
564+
"""The new box will completely fit into the old one, have desired aspect ratio,
565+
and will be padded with white space in required proportion.
566+
Ratio parameters are two-element-tuples
567+
(1,1) for padding means 'center!'
568+
(1,2) for horizontal_padding means one third left and two thirds right
569+
vertical ratio is bottom to top (upside down), horizontal is left to right """
570+
new_width = self.height * aspect_ratio[0] / aspect_ratio[1]
571+
new_height = self.width * aspect_ratio[1] / aspect_ratio[0]
572+
if new_width <= self.width:
573+
self.offset_x += self.divide_padding(self.width-new_width, horizontal_padding_ratio)
574+
self.width = new_width
575+
else:
576+
self.offset_y += self.divide_padding(self.height-new_height, vertical_padding_ratio)
577+
self.height = new_height
578+
556579
def glViewport(self):
557580
glViewport(self.offset_x, self.offset_y, self.width, self.height)
558581

@@ -2634,13 +2657,6 @@ def TransitionTo(page):
26342657
PageEntered()
26352658
if not PreloadNextPage(GetNextPage(Pcurrent, 1)):
26362659
PreloadNextPage(GetNextPage(Pcurrent, -1))
2637-
if DualHead: # show next page
2638-
print "PrompterNextFrame=", PrompterNextFrame
2639-
PrompterNextFrame.glViewport()
2640-
glClearColor(255,0,0,0)
2641-
glClear(GL_COLOR_BUFFER_BIT)
2642-
DrawCurrentPage()
2643-
WholeWindow.glViewport()
26442660
return 1
26452661

26462662
# zoom mode animation
@@ -4068,11 +4084,15 @@ def ParseOptions(argv):
40684084
projection, prompter = arg.split(",")
40694085
ProjectionFrame = FrameCoordinates.parse(projection)
40704086
PrompterWholeFrame = FrameCoordinates.parse(prompter)
4087+
print "PrompterWholeFrame: ", PrompterWholeFrame
4088+
prompter_width = PrompterWholeFrame.width*9/10/2
40714089
PrompterCurrentFrame = FrameCoordinates(
4072-
PrompterWholeFrame.width/2, PrompterWholeFrame.height/2)
4090+
prompter_width, PrompterWholeFrame.height)
40734091
PrompterNextFrame = FrameCoordinates(
4074-
PrompterWholeFrame.width/2, PrompterWholeFrame.height/2,
4075-
PrompterWholeFrame.width/2)
4092+
prompter_width, PrompterCurrentFrame.height,
4093+
PrompterWholeFrame.width-PrompterCurrentFrame.width)
4094+
PrompterCurrentFrame.adjust_to_aspect_ratio((4,3), (5,3), (0,1))
4095+
PrompterNextFrame.adjust_to_aspect_ratio((4,3), (5,3), (1,0))
40764096
UseAutoScreenSize = False
40774097
except:
40784098
opterr("invalid parameter for --dual-head")

impressive_tests.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ def testOffsetIsOptional(self):
2525
def testStr(self):
2626
c = impressive.FrameCoordinates.parse("800x600")
2727
self.assertEquals("size 800,600 offset 0,0", str(c))
28+
29+
def testAspectRatio(self):
30+
c = impressive.FrameCoordinates.parse("400x600")
31+
c.adjust_to_aspect_ratio((4,3), (1,2), (2,1))
32+
self.assertEquals(400, c.width)
33+
self.assertEquals(300, c.height)
34+
self.assertEquals(0, c.offset_x)
35+
self.assertEquals(100, c.offset_y)
36+
2837

2938
if __name__ == "__main__":
3039
unittest.main()

0 commit comments

Comments
 (0)