Exemplo n.º 1
0
    def parse_newvalue(self, new_value):
        """
            New_value can be a string like "+5cm" or a float 0.4 or a new dict
            like {"shift": 0, "align": 'left'}
        """
        #print(type(new_value))
        if type(new_value) == type(str()):
            self.position['shift'] = float( convert_unit(new_value) )
            self.position['unit'] = 'px'

        elif type(new_value) == type(float()) or type(new_value) == type(int()):
            self.position['shift'] = new_value

        elif type(new_value) == type(dict()):
            self.position = dict_deep_update(new_value.copy(), self.position)

        else:
            print('Invalid relative coordinate type!')
Exemplo n.º 2
0
    def parse_newvalue(self, new_value):
        """
            New_value can be a string like "+5cm" or a float 0.4 or a new dict
            like {"shift": 0, "align": 'left'}
        """
        #print(type(new_value))
        if isinstance(new_value, str):
            self.position['shift'] = float(convert_unit(new_value))
            self.position['unit'] = 'px'

        elif isinstance(new_value, float) or isinstance(new_value, int):
            self.position['shift'] = new_value

        elif isinstance(new_value, dict):
            self.position = dict_deep_update(new_value.copy(), self.position)

        elif isinstance(new_value, Length):
            new_value.process_value()
            self.position['shift'] = float(new_value.value)
            self.position['unit'] = 'px'

        else:
            print('Invalid relative coordinate type!')
Exemplo n.º 3
0
    def convert_position(self):

        # Function to convert position of an element
        tmpx = DEFAULT_X.copy()
        tmpy = DEFAULT_Y.copy()
        slidects = document._slides[self.slideid].contents

        # Get the previous content if it exist (to us "+xx" or "-yy" in x, y coords)
        if self.id_index > 0:
            prev_ct = slidects[document._slides[self.slideid].element_keys[
                self.id_index - 1]]
        else:
            prev_ct = None

        # Check if x or y are only floats
        if isinstance(self.x, float) or isinstance(self.x, int):
            tmpx['shift'] = self.x

        elif isinstance(self.x, dict):
            tmpx = dict_deep_update(tmpx, self.x)

        elif isinstance(self.x, str):

            converted = False

            if '+' in self.x:
                newx = convert_unit(self.x.replace('+', ''))
                # Make relative placement
                if prev_ct is not None:
                    dict_old = prev_ct.positionner.right + newx
                    tmpx = dict_deep_update(tmpx, dict_old)
                else:
                    tmpx['shift'] = newx

                tmpx['unit'] = 'px'
                converted = True

            if '-' in self.x:
                newx = convert_unit(self.x.replace('-', ''))
                # Make relative placement
                if prev_ct is not None:
                    dict_old = prev_ct.positionner.left - newx
                    tmpx = dict_deep_update(tmpx, dict_old)
                else:
                    tmpx['shift'] = newx
                tmpx['unit'] = 'px'
                converted = True

            if self.x in ['auto', 'center']:
                tmpx['shift'] = 0
                tmpx['align'] = self.x
                converted = True

            if not converted:
                try:
                    tmpx['shift'] = float(convert_unit(self.x))
                    tmpx['unit'] = 'px'
                except:
                    print('[Error] x position is incorect string format')
                    print(self.x)

        elif isinstance(self.x, Length):
            if self.x.value is None:
                self.x.run_render()

            tmpx['shift'] = self.x.value
            tmpx['unit'] = 'px'

        else:
            print("[Error] x position need to be a float or a dict")

        if isinstance(self.y, float) or isinstance(self.y, int):
            tmpy['shift'] = self.y

        elif isinstance(self.y, dict):
            tmpy = dict_deep_update(tmpy, self.y)

        elif isinstance(self.y, str):

            converted = False
            if '+' in self.y:
                newy = convert_unit(self.y.replace('+', ''))
                # Make relative placement
                if prev_ct is not None:
                    dict_old = prev_ct.positionner.bottom + newy
                    tmpy = dict_deep_update(tmpy, dict_old)
                else:
                    tmpy['shift'] = newy

                tmpy['unit'] = 'px'
                converted = True

            if '-' in self.y:
                newy = convert_unit(self.y.replace('-', ''))
                # Make relative placement
                if prev_ct is not None:
                    dict_old = prev_ct.positionner.top - newy
                    tmpy = dict_deep_update(tmpy, dict_old)
                else:
                    tmpy['shift'] = newy
                tmpy['unit'] = 'px'
                converted = True

            if self.y in ['auto', 'center']:
                tmpy['shift'] = 0
                tmpy['align'] = self.y
                converted = True

            if not converted:
                try:
                    tmpy['shift'] = convert_unit(self.y)
                    tmpy['unit'] = 'px'
                except:
                    print('[Error] y position is incorect string format')
                    print(self.y)

        elif isinstance(self.y, Length):
            if self.y.value is None:
                self.y.run_render()

            tmpy['shift'] = self.y.value
            tmpy['unit'] = 'px'

        else:
            print("[Error] y position need to be a float or an int or a dict")

        # Store the dict for positions
        self.x = tmpx
        self.y = tmpy

        # Force unit to be pixel for x > 1
        if self.x['shift'] > 1.0 and self.x['unit'] in ('width', 'height'):
            self.x['unit'] = 'px'

        # Force unit to be pixel for y > 1
        if self.y['shift'] > 1.0 and self.y['unit'] in ('width', 'height'):
            self.y['unit'] = 'px'

        # Convert position unit to px
        if self.x['unit'] in ['cm', 'pt', 'mm']:
            self.x['shift'] = float(
                convert_unit('%f%s' % (self.x['shift'], self.x['unit'])))

        if self.y['unit'] in ['cm', 'pt', 'mm']:
            self.y['shift'] = float(
                convert_unit('%f%s' % (self.y['shift'], self.y['unit'])))
Exemplo n.º 4
0
    def convert_position(self):

        #Function to convert position of an element
        tmpx = DEFAULT_X.copy()
        tmpy = DEFAULT_Y.copy()
        slidects = document._contents[gcs()]['contents']

        #Get the previous content if it exist (to us "+xx" or "-yy" in x, y coords)
        if self.id_index > 0:
            prev_ct = slidects[document._contents[gcs()]['element_keys'][self.id_index - 1]]
        else:
            prev_ct = None

        #Check if x or y are only floats
        if type(self.x) == type(float()) or type(self.x) == type(int()):
            tmpx['shift'] = self.x

        elif type(self.x) == type(dict()):
            tmpx = dict_deep_update(tmpx, self.x)

        elif type(self.x) == type(str()):

            converted = False

            if '+' in self.x:
                self.x = convert_unit( self.x.replace('+','') )
                #Make relative placement
                if prev_ct != None:
                    dict_old = prev_ct['positionner'].right + float( self.x )
                    tmpx = dict_deep_update(tmpx, dict_old)
                else:
                    tmpx['shift'] = float( self.x )

                tmpx['unit'] = 'px'
                converted = True

            if '-' in self.x:
                self.x = convert_unit( self.x.replace('-','') )
                #Make relative placement
                if prev_ct != None:
                    dict_old = prev_ct['positionner'].left - float( self.x )
                    tmpx = dict_deep_update(tmpx, dict_old)
                else:
                    tmpx['shift'] = float( self.x )
                tmpx['unit'] = 'px'
                converted = True


            if self.x in ['auto', 'center']:
                tmpx['shift'] = 0
                tmpx['align'] = self.x
                converted = True

            if not converted:
                try:
                    tmpx['shift'] = float( convert_unit(self.x) )
                    tmpx['unit'] = 'px'
                except:
                    print('[Error] x position is incorect string format')
                    print(self.x)

        else:
            print("[Error] x position need to be a float or a dict")


        if type(self.y) == type(float()) or type(self.y) == type(int()):
            tmpy['shift'] = self.y

        elif type(self.y) == type(dict()):
            tmpy = dict_deep_update(tmpy, self.y)

        elif type(self.y) == type(str()):

            converted = False
            if '+' in self.y:
                self.y = convert_unit( self.y.replace('+','') )
                #Make relative placement
                if prev_ct != None:
                    dict_old = prev_ct['positionner'].bottom + float(self.y)
                    tmpy = dict_deep_update(tmpy, dict_old)
                else:
                    tmpy['shift'] = float( self.y )

                tmpy['unit'] = 'px'
                converted = True

            if '-' in self.y:
                self.y = convert_unit( self.y.replace('-','') )
                #Make relative placement
                if prev_ct != None :
                    dict_old = prev_ct['positionner'].top - float(self.y)
                    tmpy = dict_deep_update(tmpy, dict_old)
                else:
                    tmpy['shift'] = float( self.y )
                tmpy['unit'] = 'px'
                converted = True

            if self.y in ['auto', 'center']:
                tmpy['shift'] = 0
                tmpy['align'] = self.y
                converted = True

            if not converted:
                try:
                    tmpy['shift'] = float( convert_unit(self.y) )
                    tmpy['unit'] = 'px'
                except:
                    print('[Error] y position is incorect string format')
                    print self.y
        else:
            print("[Error] y position need to be a float or an int or a dict")


        #Store the dict for positions
        self.x = tmpx
        self.y = tmpy

        #Convert position unit to pt
        if self.x['unit'] in ['cm', 'pt', 'mm']:
            self.x['shift'] = float( convert_unit( '%f%s'%(self.x['shift'], self.x['unit']) ) )

        if self.y['unit'] in ['cm', 'pt', 'mm']:
            self.y['shift'] = float( convert_unit( '%f%s'%(self.y['shift'], self.y['unit']) ) )

        if type(self.width) == type(str()):
            self.width = float( convert_unit(self.width) )

        if type(self.height) == type(str()):
            self.height = float( convert_unit(self.height) )