예제 #1
0
    def fromwalker(cls, w, **kwArgs):
        """
        Creates and returns a new Format7 object from the specified walker.
        
        >>> obj = _testingValues[1]
        >>> obj == Format7.frombytes(
        ...   obj.binaryString(),
        ...   bitDepth = obj.image.bitDepth)
        True
        
        >>> obj = _testingValues[2]
        >>> obj == Format7.frombytes(
        ...   obj.binaryString(),
        ...   bitDepth = obj.image.bitDepth)
        True
        """

        assert 'bitDepth' in kwArgs

        m = bigglyphmetrics.BigGlyphMetrics.fromwalker(w, **kwArgs)
        bitDepth = kwArgs['bitDepth']
        v = [None] * m.height

        for row in range(m.height):
            v[row] = list(w.unpackBitsGroup(bitDepth, m.width))

        b = bitmap.Bitmap(v,
                          lo_x=m.horiBearingX,
                          hi_y=m.horiBearingY,
                          bitDepth=bitDepth)

        return cls(metrics=m, image=b)
예제 #2
0
    def fromscalerdata(cls, glyphbitmap, bitDepth):
        """
        Initializes a Format6 from glyphbitmap, which should be an object
        similar to ScalerInterface's 'GlyphBitmap', having 'metrics' and 'bits'
        attributes.
        """

        mtx = glyphbitmap.metrics

        m = bigglyphmetrics.BigGlyphMetrics(
            height=mtx.height,
            width=mtx.width,
            horiBearingX=mtx.lo_x,
            horiBearingY=mtx.hi_y + 1,  # convert from pixel to edge
            horiAdvance=mtx.i_dx)

        w = walker.StringWalker(glyphbitmap.bits)

        v = [
            utilities.explodeRow(w.chunk(mtx.bpl), bitDepth, m.width)
            for _ in range(m.height)
        ]

        b = bitmap.Bitmap(v,
                          lo_x=m.horiBearingX,
                          hi_y=m.horiBearingY,
                          bitDepth=bitDepth)

        r = cls(image=b, metrics=m)

        return r
예제 #3
0
    def fromwalker(cls, w, **kwArgs):
        """
        Creates and returns a new Format2 object from the specified walker.
        
        >>> obj = _testingValues[1]
        >>> obj == Format2.frombytes(
        ...   obj.binaryString(),
        ...   isHorizontal = obj.metrics.isHorizontal,
        ...   bitDepth = obj.image.bitDepth)
        True
        
        >>> obj = _testingValues[2]
        >>> obj == Format2.frombytes(
        ...   obj.binaryString(),
        ...   isHorizontal = obj.metrics.isHorizontal,
        ...   bitDepth = obj.image.bitDepth)
        True
        """

        assert 'isHorizontal' in kwArgs
        assert 'bitDepth' in kwArgs

        sm = smallglyphmetrics.SmallGlyphMetrics.fromwalker(w, **kwArgs)
        bitDepth = kwArgs['bitDepth']
        v = [None] * sm.height

        for row in range(sm.height):
            v[row] = list(w.unpackBitsGroup(bitDepth, sm.width))

        b = bitmap.Bitmap(v,
                          lo_x=sm.bearingX,
                          hi_y=sm.bearingY,
                          bitDepth=bitDepth)

        return cls(metrics=sm, image=b)
예제 #4
0
    def fromvalidatedwalker(cls, w, **kwArgs):
        """
        Like fromwalker(), this method returns a new Format5. However, it also
        does extensive validation via the logging module (the client should
        have done a logging.basicConfig call prior to calling this method,
        unless a logger is passed in via the 'logger' keyword argument).
        
        >>> logger = utilities.makeDoctestLogger('test.sbit')
        >>> s = _testingValues[1].binaryString()
        >>> fvb = Format5.fromvalidatedbytes
        >>> obj = fvb(
        ...   s,
        ...   logger = logger,
        ...   bigMetrics = _testingValues[1].metrics,
        ...   bitDepth = 1)
        test.sbit.format5 - DEBUG - Walker has 7 remaining bytes.
        
        >>> obj = fvb(
        ...   s[:-1],
        ...   logger = logger,
        ...   bigMetrics = _testingValues[1].metrics,
        ...   bitDepth = 1)
        test.sbit.format5 - DEBUG - Walker has 6 remaining bytes.
        test.sbit.format5 - ERROR - Insufficient data for bitmap
        """

        logger = kwArgs.pop('logger', None)

        if logger is None:
            logger = logging.getLogger().getChild('format5')
        else:
            logger = logger.getChild('format5')

        endOfWalker = w.length()

        logger.debug(
            ('V0001', (endOfWalker, ), "Walker has %d remaining bytes."))

        assert 'bitDepth' in kwArgs
        assert 'bigMetrics' in kwArgs

        m = kwArgs['bigMetrics']
        bitDepth = kwArgs['bitDepth']

        if w.bitLength() < (bitDepth * m.width * m.height):
            logger.error(('V0221', (), "Insufficient data for bitmap"))

            return None

        v = [None] * m.height

        for row in range(m.height):
            v[row] = list(w.unpackBitsGroup(bitDepth, m.width))

        b = bitmap.Bitmap(v,
                          lo_x=m.horiBearingX,
                          hi_y=m.horiBearingY,
                          bitDepth=bitDepth)

        return cls(metrics=m, image=b)
예제 #5
0
    def __________________():
        pass


if __debug__:
    from fontio3 import utilities

    BGM = bigglyphmetrics.BigGlyphMetrics

    _testingValues = (Format7(),
                      Format7(
                          metrics=BGM(7, 7, 1, 0, 10, -3, -1, 9),
                          image=bitmap.Bitmap(
                              [[1, 0, 0, 0, 0, 0, 1], [0, 1, 0, 0, 0, 1, 0],
                               [0, 0, 1, 0, 1, 0, 0], [1, 0, 0, 1, 0, 0, 0],
                               [0, 0, 1, 0, 1, 0, 0], [0, 1, 0, 0, 0, 1, 0],
                               [1, 0, 0, 0, 0, 0, 1]],
                              lo_x=1,
                              hi_y=0,
                              bitDepth=1)),
                      Format7(metrics=BGM(3, 3, 1, 3, 5, -1, -1, 5),
                              image=bitmap.Bitmap(
                                  [[7, 4, 7], [4, 1, 4], [7, 4, 7]],
                                  lo_x=1,
                                  hi_y=3,
                                  bitDepth=4)))

    del BGM


def _test():
    import doctest
예제 #6
0
    def fromvalidatedwalker(cls, w, **kwArgs):
        """
        Like fromwalker(), this method returns a new Format1. However, it also
        does extensive validation via the logging module (the client should
        have done a logging.basicConfig call prior to calling this method,
        unless a logger is passed in via the 'logger' keyword argument).
        
        >>> logger = utilities.makeDoctestLogger('test.sbit')
        >>> s = _testingValues[1].binaryString()
        >>> fvb = Format1.fromvalidatedbytes
        >>> obj = fvb(s, logger=logger, isHorizontal=True, bitDepth=1)
        test.sbit.format1 - DEBUG - Walker has 12 remaining bytes.
        test.sbit.format1.smallglyphmetrics - DEBUG - Walker has 12 remaining bytes.
        
        >>> obj = fvb(s[:-1], logger=logger, isHorizontal=True, bitDepth=1)
        test.sbit.format1 - DEBUG - Walker has 11 remaining bytes.
        test.sbit.format1.smallglyphmetrics - DEBUG - Walker has 11 remaining bytes.
        test.sbit.format1 - ERROR - Insufficient data for bitmap
        """

        logger = kwArgs.pop('logger', None)

        if logger is None:
            logger = logging.getLogger().getChild('format1')
        else:
            logger = logger.getChild('format1')

        endOfWalker = w.length()

        logger.debug(
            ('V0001', (endOfWalker, ), "Walker has %d remaining bytes."))

        assert 'isHorizontal' in kwArgs
        assert 'bitDepth' in kwArgs

        sm = smallglyphmetrics.SmallGlyphMetrics.fromvalidatedwalker(
            w, logger=logger, **kwArgs)

        if sm is None:
            return None

        bitDepth = kwArgs['bitDepth']
        bpr = (bitDepth * sm.width + 7) // 8

        if w.length() < (bpr * sm.height):
            logger.error(('V0219', (), "Insufficient data for bitmap"))

            return None

        v = [None] * sm.height

        for row in range(sm.height):
            v[row] = list(w.unpackBitsGroup(bitDepth, sm.width))
            w.align(1)

        b = bitmap.Bitmap(v,
                          lo_x=sm.bearingX,
                          hi_y=sm.bearingY,
                          bitDepth=bitDepth)

        return cls(metrics=sm, image=b)