Beispiel #1
0
def _test_imports():
    print("start_test_import")

    app = QApplication([])
    import freetype
    import napari
    from packaging.version import parse

    if parse(napari.__version__) < parse("0.4.5"):
        from napari._qt.widgets.qt_console import QtConsole
    else:
        from napari_console.qt_console import QtConsole

    from PartSeg import plugins
    from PartSeg._launcher.main_window import MainWindow
    from PartSeg._roi_analysis.main_window import MainWindow as AnalysisMain
    from PartSeg._roi_mask.main_window import MainWindow as MaskMain
    from PartSeg.common_backend.base_argparser import _setup_sentry

    _setup_sentry()
    freetype.get_handle()
    plugins.register()
    w1 = AnalysisMain("test")
    w2 = MaskMain("test")
    w3 = MainWindow("test")
    console = QtConsole(napari.Viewer())
    del w1
    del w2
    del w3
    del app
    del console
    print("end_test_import")
Beispiel #2
0
    def __init__(self, generator, char):
        self.char = char
        self.font = generator.font
        self.glyph = glyph = generator.font.glyph

        bold = 0
        if generator.bold:
            bold = BOLD_AMOUNT

        self.advance = ((glyph.advance.x + bold) / 64.0,
                        glyph.advance.y / 64.0)
        box = self.get_cbox()
        self.x1 = box.xMin / 64.0
        self.y1 = box.yMin / 64.0
        self.x2 = box.xMax / 64.0
        self.y2 = box.yMax / 64.0
        real_glyph = glyph.get_glyph()
        if generator.monochrome:
            mode = freetype.FT_RENDER_MODE_MONO
        else:
            mode = freetype.FT_RENDER_MODE_NORMAL
        bitmap_glyph = real_glyph.to_bitmap(mode, 0, True)
        self.corner = (bitmap_glyph.left, bitmap_glyph.top)
        bitmap = bitmap_glyph.bitmap

        destroy_bitmaps = []
        if generator.monochrome:
            new_bitmap = FT_Bitmap()
            destroy_bitmaps.append(new_bitmap)
            FT_Bitmap_New(byref(new_bitmap))
            FT_Bitmap_Convert(freetype.get_handle(), byref(bitmap._FT_Bitmap),
                              byref(new_bitmap), 1)
            bitmap = freetype.Bitmap(new_bitmap)

        if generator.bold:
            new_bitmap = FT_Bitmap()
            destroy_bitmaps.append(new_bitmap)
            FT_Bitmap_Copy(freetype.get_handle(), byref(bitmap._FT_Bitmap),
                           byref(new_bitmap))
            err = FT_Bitmap_Embolden(freetype.get_handle(),
                                     byref(new_bitmap),
                                     BOLD_AMOUNT, 0)
            bitmap = freetype.Bitmap(new_bitmap)

        self.width = bitmap.width
        self.height = bitmap.rows
        self.buf = ''
        if generator.monochrome:
            for c in bitmap.buffer:
                self.buf += chr(c * 255)
        else:
            for c in bitmap.buffer:
                self.buf += chr(c)


        for bitmap in destroy_bitmaps:
            FT_Bitmap_Done(freetype.get_handle(), byref(bitmap))
Beispiel #3
0
    def __init__(self, generator, char):
        self.char = char
        self.font = generator.font
        self.glyph = glyph = generator.font.glyph

        bold = 0
        if generator.bold:
            bold = BOLD_AMOUNT

        self.advance = ((glyph.advance.x + bold) / 64.0,
                        glyph.advance.y / 64.0)
        box = self.get_cbox()
        self.x1 = box.xMin / 64.0
        self.y1 = box.yMin / 64.0
        self.x2 = box.xMax / 64.0
        self.y2 = box.yMax / 64.0
        real_glyph = glyph.get_glyph()
        if generator.monochrome:
            mode = freetype.FT_RENDER_MODE_MONO
        else:
            mode = freetype.FT_RENDER_MODE_NORMAL
        bitmap_glyph = real_glyph.to_bitmap(mode, 0, True)
        self.corner = (bitmap_glyph.left, bitmap_glyph.top)
        bitmap = bitmap_glyph.bitmap

        destroy_bitmaps = []
        if generator.monochrome:
            new_bitmap = FT_Bitmap()
            destroy_bitmaps.append(new_bitmap)
            FT_Bitmap_New(byref(new_bitmap))
            FT_Bitmap_Convert(freetype.get_handle(), byref(bitmap._FT_Bitmap),
                              byref(new_bitmap), 1)
            bitmap = freetype.Bitmap(new_bitmap)

        if generator.bold:
            new_bitmap = FT_Bitmap()
            destroy_bitmaps.append(new_bitmap)
            FT_Bitmap_Copy(freetype.get_handle(), byref(bitmap._FT_Bitmap),
                           byref(new_bitmap))
            err = FT_Bitmap_Embolden(freetype.get_handle(), byref(new_bitmap),
                                     BOLD_AMOUNT, 0)
            bitmap = freetype.Bitmap(new_bitmap)

        self.width = bitmap.width
        self.height = bitmap.rows
        self.buf = ''
        if generator.monochrome:
            for c in bitmap.buffer:
                self.buf += chr(c * 255)
        else:
            for c in bitmap.buffer:
                self.buf += chr(c)

        for bitmap in destroy_bitmaps:
            FT_Bitmap_Done(freetype.get_handle(), byref(bitmap))
Beispiel #4
0
    def buffer(self,
               width=None,
               height=None,
               transform=None,
               contain=False,
               evenOdd=False):
        """Renders the current contours within a bitmap buffer.

        Args:
            width: Image width of the bitmap in pixels. If omitted, it
                automatically fits to the bounding box of the contours.
            height: Image height of the bitmap in pixels. If omitted, it
                automatically fits to the bounding box of the contours.
            transform: A optional 6-tuple containing an affine transformation,
                or a ``Transform`` object from the ``fontTools.misc.transform``
                module. The bitmap size is not affected by this matrix.
            contain: If ``True``, the image size will be automatically expanded
                so that it fits to the bounding box of the paths. Useful for
                rendering glyphs with negative sidebearings without clipping.
            evenOdd: Pass ``True`` for even-odd fill instead of non-zero.

        Returns:
            A tuple of ``(buffer, size)``, where ``buffer`` is a ``bytes``
            object of the resulted bitmap and ``size`` is a 2-tuple of its
            dimension.

        :Example:
            .. code-block::
                
                >> pen = FreeTypePen(None)
                >> glyph.draw(pen)
                >> buf, size = pen.buffer(width=500, height=1000)
                >> type(buf), len(buf), size
                (<class 'bytes'>, 500000, (500, 1000))
        
        """
        transform = transform or Transform()
        if not hasattr(transform, 'transformPoint'):
            transform = Transform(*transform)
        contain_x, contain_y = contain or width is None, contain or height is None
        width, height = width or 0, height or 0
        if contain_x or contain_y:
            bbox = self.bbox
            bbox = transform.transformPoints((bbox[0:2], bbox[2:4]))
            bbox = (*bbox[0], *bbox[1])
            bbox_size = bbox[2] - bbox[0], bbox[3] - bbox[1]
            dx, dy = transform.dx, transform.dy
            if contain_x:
                dx = min(-dx, bbox[0]) * -1.0
                width = max(width, bbox_size[0])
            if contain_y:
                dy = min(-dy, bbox[1]) * -1.0
                height = max(height, bbox_size[1])
            transform = Transform(*transform[:4], dx, dy)
        width, height = math.ceil(width), math.ceil(height)
        buf = ctypes.create_string_buffer(width * height)
        bitmap = FT_Bitmap(
            (ctypes.c_int)(height), (ctypes.c_int)(width),
            (ctypes.c_int)(width), (ctypes.POINTER(ctypes.c_ubyte))(buf),
            (ctypes.c_short)(256), (ctypes.c_ubyte)(FT_PIXEL_MODE_GRAY),
            (ctypes.c_char)(0), (ctypes.c_void_p)(None))
        outline = self.outline(transform=transform, evenOdd=evenOdd)
        err = FT_Outline_Get_Bitmap(freetype.get_handle(),
                                    ctypes.byref(outline),
                                    ctypes.byref(bitmap))
        if err != 0:
            raise FT_Exception(err)
        return buf.raw, (width, height)
Beispiel #5
0
    def buffer(self,
               width=None,
               height=None,
               transform=None,
               contain=False,
               evenOdd=False):
        """Renders the current contours within a bitmap buffer.

        Args:
            width: Image width of the bitmap in pixels. If omitted, it
                automatically fits to the bounding box of the contours.
            height: Image height of the bitmap in pixels. If omitted, it
                automatically fits to the bounding box of the contours.
            transform: An optional 6-tuple containing an affine transformation,
                or a ``Transform`` object from the ``fontTools.misc.transform``
                module. The bitmap size is not affected by this matrix.
            contain: If ``True``, the image size will be automatically expanded
                so that it fits to the bounding box of the paths. Useful for
                rendering glyphs with negative sidebearings without clipping.
            evenOdd: Pass ``True`` for even-odd fill instead of non-zero.

        Returns:
            A tuple of ``(buffer, size)``, where ``buffer`` is a ``bytes``
            object of the resulted bitmap and ``size`` is a 2-tuple of its
            dimension.

        :Notes:
            The image size should always be given explicitly if you need to get
            a proper glyph image. When ``width`` and ``height`` are omitted, it
            forcifully fits to the bounding box and the side bearings get
            cropped. If you pass ``0`` to both ``width`` and ``height`` and set
            ``contain`` to ``True``, it expands to the bounding box while
            maintaining the origin of the contours, meaning that LSB will be
            maintained but RSB won’t. The difference between the two becomes
            more obvious when rotate or skew transformation is applied.

        :Example:
            .. code-block::

                >> pen = FreeTypePen(None)
                >> glyph.draw(pen)
                >> buf, size = pen.buffer(width=500, height=1000)
                >> type(buf), len(buf), size
                (<class 'bytes'>, 500000, (500, 1000))

        """
        transform = transform or Transform()
        if not hasattr(transform, "transformPoint"):
            transform = Transform(*transform)
        contain_x, contain_y = contain or width is None, contain or height is None
        if contain_x or contain_y:
            dx, dy = transform.dx, transform.dy
            bbox = self.bbox
            p1, p2, p3, p4 = (
                transform.transformPoint((bbox[0], bbox[1])),
                transform.transformPoint((bbox[2], bbox[1])),
                transform.transformPoint((bbox[0], bbox[3])),
                transform.transformPoint((bbox[2], bbox[3])),
            )
            px, py = (p1[0], p2[0], p3[0], p4[0]), (p1[1], p2[1], p3[1], p4[1])
            if contain_x:
                if width is None:
                    dx = dx - min(*px)
                    width = max(*px) - min(*px)
                else:
                    dx = dx - min(min(*px), 0.0)
                    width = max(width, max(*px) - min(min(*px), 0.0))
            if contain_y:
                if height is None:
                    dy = dy - min(*py)
                    height = max(*py) - min(*py)
                else:
                    dy = dy - min(min(*py), 0.0)
                    height = max(height, max(*py) - min(min(*py), 0.0))
            transform = Transform(*transform[:4], dx, dy)
        width, height = math.ceil(width), math.ceil(height)
        buf = ctypes.create_string_buffer(width * height)
        bitmap = FT_Bitmap(
            (ctypes.c_int)(height),
            (ctypes.c_int)(width),
            (ctypes.c_int)(width),
            (ctypes.POINTER(ctypes.c_ubyte))(buf),
            (ctypes.c_short)(256),
            (ctypes.c_ubyte)(FT_PIXEL_MODE_GRAY),
            (ctypes.c_char)(0),
            (ctypes.c_void_p)(None),
        )
        outline = self.outline(transform=transform, evenOdd=evenOdd)
        err = FT_Outline_Get_Bitmap(freetype.get_handle(),
                                    ctypes.byref(outline),
                                    ctypes.byref(bitmap))
        if err != 0:
            raise FT_Exception(err)
        return buf.raw, (width, height)