Skip to content

Commit

Permalink
Move kiva.image to kiva.oldagg (#1054)
Browse files Browse the repository at this point in the history
This is a simple PR that reverses the dependency of `kiva.image` and `kiva.oldagg`.  This is a prepatory PR for eventually replacing the old agg backend with celiagg.
  • Loading branch information
corranwebster committed May 15, 2023
1 parent bb63034 commit 7b2be2b
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 95 deletions.
52 changes: 9 additions & 43 deletions enable/qt/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,12 @@
#
# Thanks for using Enthought open source!

from pyface.qt import QtCore, QtGui
from kiva.agg import CompiledPath, GraphicsContextSystem as GraphicsContext

from .base_window import BaseWindow
from .scrollbar import NativeScrollBar


class Window(BaseWindow):
def _create_gc(self, size, pix_format="bgra32"):
gc = GraphicsContext(
(size[0] + 1, size[1] + 1),
pix_format=pix_format,
base_pixel_scale=self.base_pixel_scale,
# We have to set bottom_up=0 or otherwise the PixelMap will
# appear upside down in the QImage.
bottom_up=0,
)
gc.translate_ctm(0.5, 0.5)

return gc

def _window_paint(self, event):
if self.control is None:
return

# self._gc is an image context
w = self._gc.width()
h = self._gc.height()
data = self._gc.pixel_map.convert_to_argb32string()
image = QtGui.QImage(data, w, h, QtGui.QImage.Format_ARGB32)
rect = QtCore.QRectF(
0, 0, w / self._gc.base_scale, h / self._gc.base_scale
)
painter = QtGui.QPainter(self.control)
painter.drawImage(rect, image)


def font_metrics_provider():
from kiva.api import Font

gc = GraphicsContext((1, 1))
gc.set_font(Font())
return gc
from .oldagg import (
CompiledPath, font_metrics_provider, GraphicsContext, NativeScrollBar,
Window
)

__all__ = [
"CompiledPath", "GraphicsContext", "NativeScrollBar", "Window",
"font_metrics_provider"
]
53 changes: 44 additions & 9 deletions enable/qt/oldagg.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,47 @@
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
from .image import (
CompiledPath, font_metrics_provider, GraphicsContext, NativeScrollBar,
Window
)

__all__ = [
"CompiledPath", "GraphicsContext", "NativeScrollBar", "Window",
"font_metrics_provider"
]

from pyface.qt import QtCore, QtGui
from kiva.agg import CompiledPath, GraphicsContextSystem as GraphicsContext

from .base_window import BaseWindow
from .scrollbar import NativeScrollBar


class Window(BaseWindow):
def _create_gc(self, size, pix_format="bgra32"):
gc = GraphicsContext(
(size[0] + 1, size[1] + 1),
pix_format=pix_format,
base_pixel_scale=self.base_pixel_scale,
# We have to set bottom_up=0 or otherwise the PixelMap will
# appear upside down in the QImage.
bottom_up=0,
)
gc.translate_ctm(0.5, 0.5)

return gc

def _window_paint(self, event):
if self.control is None:
return

# self._gc is an image context
w = self._gc.width()
h = self._gc.height()
data = self._gc.pixel_map.convert_to_argb32string()
image = QtGui.QImage(data, w, h, QtGui.QImage.Format_ARGB32)
rect = QtCore.QRectF(
0, 0, w / self._gc.base_scale, h / self._gc.base_scale
)
painter = QtGui.QPainter(self.control)
painter.drawImage(rect, image)


def font_metrics_provider():
from kiva.api import Font

gc = GraphicsContext((1, 1))
gc.set_font(Font())
return gc
2 changes: 1 addition & 1 deletion kiva/agg/tests/test_font_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# Thanks for using Enthought open source!
import time

from kiva.image import font_metrics_provider as FMP
from kiva.oldagg import font_metrics_provider as FMP
from kiva.api import Font

counts = (500,)
Expand Down
36 changes: 8 additions & 28 deletions kiva/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,13 @@
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
""" This backend supports Kiva drawing into memory buffers.

Though this can be used to perform drawing in non-GUI applications,
the Image backend is also used by many of the GUI backends to draw into
the memory space of the graphics contexts.
"""
from kiva.oldagg import (
CompiledPath, font_metrics_provider, FontType, GraphicsContext,
GraphicsContextSystem, Image
)

# Soon the Agg subpackage will be renamed for real. For now, just
# proxy the imports.
from .agg import GraphicsContextArray as GraphicsContext

# FontType will be unified with the Kiva FontType soon.
from .agg import AggFontType as FontType

# GraphicsContextSystem wraps up platform- and toolkit- specific low
# level calls with a GraphicsContextArray. Eventually this low-level code
# will be moved out of the Agg subpackage, and we won't have be importing
# this here.
from .agg import GraphicsContextSystem, Image

# CompiledPath is an object that can efficiently store paths to be reused
# multiple times.
from .agg import CompiledPath


def font_metrics_provider():
""" Create an object to be used for querying font metrics.
"""

return GraphicsContext((1, 1))
__all__ = [
"CompiledPath", "font_metrics_provider", "FontType", "GraphicsContext",
"GraphicsContextSystem", "Image"
]
39 changes: 30 additions & 9 deletions kiva/oldagg.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,33 @@
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
from kiva.image import (
CompiledPath, font_metrics_provider, FontType, GraphicsContext,
GraphicsContextSystem, Image
)

__all__ = [
"CompiledPath", "font_metrics_provider", "FontType", "GraphicsContext",
"GraphicsContextSystem", "Image"
]
""" This backend supports Kiva drawing into memory buffers.
Though this can be used to perform drawing in non-GUI applications,
the Image backend is also used by many of the GUI backends to draw into
the memory space of the graphics contexts.
"""

# Soon the Agg subpackage will be renamed for real. For now, just
# proxy the imports.
from .agg import GraphicsContextArray as GraphicsContext

# FontType will be unified with the Kiva FontType soon.
from .agg import AggFontType as FontType

# GraphicsContextSystem wraps up platform- and toolkit- specific low
# level calls with a GraphicsContextArray. Eventually this low-level code
# will be moved out of the Agg subpackage, and we won't have be importing
# this here.
from .agg import GraphicsContextSystem, Image

# CompiledPath is an object that can efficiently store paths to be reused
# multiple times.
from .agg import CompiledPath


def font_metrics_provider():
""" Create an object to be used for querying font metrics.
"""

return GraphicsContext((1, 1))
7 changes: 4 additions & 3 deletions kiva/tests/agg/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def assert_close(desired, actual, diff_allowed=2):
# ----------------------------------------------------------------------------


class test_text_image(unittest.TestCase):
class TestTextImage(unittest.TestCase):
def test_antialias(self):
gc = agg.GraphicsContextArray((200, 50), pix_format="bgra32")
gc.set_antialias(1)
Expand Down Expand Up @@ -151,7 +151,7 @@ def test_rotate(self):
save(gc)


class test_sun(unittest.TestCase):
class TestSun(unittest.TestCase):
def generic_sun(self, scheme):
img = sun(scheme)
sz = array((img.width(), img.height()))
Expand Down Expand Up @@ -224,7 +224,8 @@ def bench(stmt="pass", setup="pass", repeat=5, adjust_runs=True):
#
#
# ----------------------------------------------------------------------------
class test_interpolation_image(unittest.TestCase):
class TestInterpolationImage(unittest.TestCase):

size = (1000, 1000)
color = 0.0

Expand Down
2 changes: 1 addition & 1 deletion kiva/tests/test_agg_drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import numpy as np

from kiva.tests.drawing_tester import DrawingImageTester
from kiva.image import GraphicsContext
from kiva.oldagg import GraphicsContext


class TestAggDrawing(DrawingImageTester, unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion kiva/tests/test_graphics_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from PIL import Image as PILImage


from kiva.image import GraphicsContext
from kiva.oldagg import GraphicsContext

# alpha blending is approximate in agg, so we allow some "slop" between
# desired and actual results, allow channel differences of to 2.
Expand Down

0 comments on commit 7b2be2b

Please sign in to comment.