Archive

Archive for the ‘kivy’ Category

Kivy matplotlib backend with right text positioning and rendering.

July 8th, 2015 No comments

In order to fix the problem of the layout of the text inside the canvas, an important function had to be overwritten with the information of the bounding box for each text inside it. The function is get_text_width_height_descent in this, one calculates the width and height and return them. Inside matplotlib these values are important to calculate the positioning of the elements according to the layout set by the user. Additionally, in the draw_text method transformations and styling is applied to the text. At the end we can have a working backend with different fonts and styles that are mapped between matplotlib and kivy capabilities.

Another inconvenient in the rendering of the graphs were some glitches while drawing. I was calculating u and v to create the mesh for the polygons when this was not really necessary. After some debugging and analysis of the vertices the final implementation generate the meshes and lines for all the elements in the canvas as follow:

Screenshot from 2015-07-07 12:18:28 example mpl kivy

As can be seen, now it is getting closer to how the static image implementation looks like from the previous post.

Kivy backend using Line and Mesh graphics instructions

June 26th, 2015 2 comments

This is the first prototype for the backend, points are extracted from the path an transformed into polygons or lines. Line and Mesh are used in the kivy side to render these objects in a widget canvas. Labels are used for displaying text. Below some examples can be found. The lines are not well defined and the next is to optimize this drawing as well as the text. Some attributes should be added to the labels, positioning is another problem.

matplotlib examples kivy matplotlib kivy matplotlib kivy matplotlib kivy

Read more…

Writing a matplotlib renderer for Kivy

June 24th, 2015 No comments

A Renderer is a class that knows all the graphics instructions to draw in a canvas. The first implementation involved to use the canonical agg renderer to get a figure which we could texture in a rectangle, this is fully explained in a previous post. The objective of my implementation is to have a backend implemented using kivy graphics instructions. The RendererBase class defines all the required methods to do so. RendererKivy extends from it and implements draw_path, other 5 different methods can be implemented but the most important is draw_path.

In this method all the required information to draw on the canvas is provided however there is another class as well required to change the style of elements rendered. This class is GraphicsContext which is a middle class between a renderer and an Artist. Artist is one of the layers in the hierarchical architecture of matplotlib, the other two layers are backend and scripting. Basically, everything that can be drawn in a canvas is an Artist object such as Axis, Tick, Figure, etc. A GraphicsContextKivy is defined which extends from GraphicsContextBase and specifically translates matplotlib context instructions such as line width, line style, caps, joints, colors, etc. into the equivalent in Kivy. As can be seen in the figure below the lines are dashed and the foreground of the canvas was changed.

backend kivy

At the moment there are three issues: the first one is that kivy implements caps for lines {‘square’, ’round’, ‘none’} and matplotlib {‘butt’, ‘projecting’, ’round’}. the second issue is that dashes in kivy are supported depending on the line width and finally, kivy does not support dynamic values for dash offset and dash length. The renderer, receives this object GraphicsContext and applies such styles defined there into kivy context and vertex instructions. As can be seen in the figure a very simple drawing of the information received in the draw_path is performed. Basically lines are created with all the vertex received but a path is more complex than that and represents a series of possibly disconnected, possibly closed, line and curve segments. The next post will be about this class Path and which kivy instructions are being used to implement the renderer.

Connecting events between kivy and matplotlib

June 15th, 2015 No comments

Matplotlib provides a list of events that can be connected to an external backend. This list of events can be found in backend_bases.py, the events are:


events = [
'resize_event',
'draw_event',
'key_press_event',
'key_release_event',
'button_press_event',
'button_release_event',
'scroll_event',
'motion_notify_event',
'pick_event',
'idle_event',
'figure_enter_event',
'figure_leave_event',
'axes_enter_event',
'axes_leave_event',
'close_event'
]

In order to connect these events to kivy events, we first need to know the corresponding kivy events that can be match with the ones in the previous list:


widget_events = [

'on_touch_down',
'on_touch_up',

]

keyboard_events = [

'on_key_down',
'on_key_up',

]

window_events = [

'on_close',

]

attribute_events = [
'mouse_pos', #from Window
'size',

]

The definition for each mpl event and how it is connected to each kivy event will be explained below:

– ‘button_press_event’ :
On FigureCanvasKivy ‘on_touch_down’ will be triggered when a user touches the widget. A call to FigureCanvasBase will be performed on ‘button_press_event’ with the corresponding touch.x and touch.y arguments.

Read more…

Categories: GSOC2015, kivy, matplotlib Tags: ,

Backend for kivy in matplotlib, first steps

June 11th, 2015 1 comment

A backend in matplotlib is a class that creates a link between matplotlib and any frontend framework in which the data can be rendered. Matplotlib provides a template which works as an excellent starting point for anyone who would like to develop a backend. It is a class which by itself works as a backend but do nothing. The very first thing I have been doing the last three days is reading the comments on this file and analyzing how backends for other frameworks are implemented.

The first objective towards implementing a fully functional backend in kivy is to write one with a canonical renderer such as Agg. The first class I modified from the list of classes that the file provides is FigureCanvas which I renamed FigureCanvasKivyAgg. This class extends from a FigureCanvasAgg and a Kivy Widget. The reason why it extends from FigureCanvasAgg is because this class returns an Agg renderer which will contain the graphical information of the graph. Additionally, it extends from Widget because it is handy to add a widget when someone wants to embed matplotlib graphs or use it with pyplot to provide a fully functional App.

Example of a graph rendered in kivypython ../../../examples/mpl/test_plt.py -dmodule://backend_kivy

In FigureCanvasKivyAgg two methods were overridden. The first one is draw which defines the context in which the renderer will be placed. From the FigureCanvasAgg I get the rgba buffer in order to place that in a texture which can be then added to a widget, as you can see in the following snippet.


texture = Texture.create(size=(w, h))
texture.blit_buffer(buffer_rgba, colorfmt='rgba', bufferfmt='ubyte')
texture.flip_vertical()
with self.canvas:
    Rectangle(texture=texture, pos=self.pos, size=(w, h))

The second method is blit, in which given a bounding box defines the rectangular area of the graph to be drawn.

self.blitbox = bbox

FigureCanvasKivyAgg can be embed in any Kivy application given it is a Widget

Read more…

Categories: GSOC2015, kivy, matplotlib Tags: ,

Canvas to handle strokes and stroke processing.

June 5th, 2015 No comments

During my milestone 1 period, we came up with 4 new classes for ink processing.
StrokePoint: Class with coordinates x and y. This is the native structure for the StrokeCanvas behavior.
StrokeRect: Logical Rectangle which purpose is to bound a stroke generated in the StrokeCanvas. Provides methods to:
Know if a point is contained in a rectangle.
Know if another rectangle overlap itself.
Stroke: The stroke contains a list of points. The list of points form a line. A stroke have different drawing attributes:
color → change color of Stroke.
width → change the size of the graphic line.
is_highlighter → changes the visibility.
A stroke provides:
get_bounds method to get the enclosed rectangle in which a stroke is.
StrokeCanvas:
A StrokeCanvas contains a list of strokes. A StrokeCanvas provides:
Events to access to the strokes created when they are added or removed.
A StrokeCanvas is the visual representation of a Stroke using Lines.
Event to access to the mode when this is changed.

stroke_canvas_kivyThe branch for this can be found at: https://github.com/andnovar/kivy/tree/mpl_kivy and you need the colors from here: https://github.com/andnovar/kivy/tree/colors_in_utils.py although these colors are going to change eventually for more descriptive ones.

Categories: GSOC2015, kivy, python Tags:

Bonding period Summarize

May 29th, 2015 No comments

During the bonding period I did some work on how kivy graphics pipeline works. Kivy has two implementations for lines. kivy.graphics.Line and kivy.graphics.SmoothLine. Normal line have antialising problems which are being solved in the latest one. SmoothLine is an experimental alternative that I put my hands on. While trying to create an SmoothLine object from python the following error was produced:

Exception TypeError: “object of type ‘NoneType’ has no len()” in ‘kivy.graphics.vertex_instructions.SmoothLine.build_smooth’ ignored

This was fixed by changing the way the constructor invoked the parent class. This issue shows up just when instantiated from python. Once I could instantiate SmoothLine I tested SmoothLine creating lines from points collected from touches. Then I came up with one of the issues of SmoothLine. When a line is nearly 180 degrees in direction a pixel stretch along the horizontal or vertical axis depending on the direction as can be seen on the following figure.

inkcanvas

This is related somehow to my project since I can use smoothlines instead of lines for the gestures section. Three new classes are proposed: Point, Stroke and InkCanvas. There is an implementation in the kivy.graphics for points but stores a list of points I am looking to have a more native representation so a stroke can be handled as a list of points. For later algorithms for ink processing these object representation could make it easier.

The main goal of my project for the google summer of code besides rendering a matplotlib is being able to interact with it in a way that can be differentiated from already existing backends such as gtk, qt, etc.

Over the weekend a post on the MPL workflow implementation will be given. That’s it for now, next week I expect to have more work done and a first draft of the MPL modules implementation.

Categories: GSOC2015, kivy Tags:

Kivy – Multiple Layouts

September 14th, 2011 No comments

I wonder how can I have several layouts and change between them using kv files in kivy. Actually I have the answer.

You have to create an ejercicio.kv file. You have to define the classes you are going to use for each layout such as.

#:kivy 1.0

<MyLabelWithBackground>:
       #definition

<Ejercicio>:
      #definition

<Layout1>:
      #definition

<Layout2>:
      #definition

Read more…

Categories: kivy, multitouch, python Tags: , , ,

Kivy – Label with background

August 3rd, 2011 2 comments

Kivy is a framework for develop multitouch applications, I started to use pyMT, but pyMT evolved into Kivy.

Adding a background to a label is not possible using the common functions in documentation.

A possible solution by @Mathieu Virbel could be:

class MyLabelWithBackground(Label):
  pass
And put in a kv that you load somewhere:
<MyLabelWithBackground>:
  canvas.before:
    Color:
      rgb: 1, 0, 0 # your color here
    Rectangle:
      pos: self.pos
      size: self.size
Categories: kivy, multitouch, python Tags: , , ,