Peter Ward avatar Peter Ward committed 124f9a3

move a text layout rendering function back to primitives

Comments (0)

Files changed (3)

 Primitives
 ~~~~~~~~~~
 
-There are some simple functions provided for rendering text and images:
+.. module:: shinypress.render
 
-.. automodule:: shinypress.render
-    :members:
+Got an image? Let’s render that for you:
+
+.. autofunction:: render_image
+
+Oh, you have some text instead? Well, let’s do that, then:
+
+.. autofunction:: render_text
+.. autoclass:: HAlignment
+.. autoclass:: VAlignment
+
+And there’s also a utility function useful for scaling content, used by those functions above.
+
+.. autofunction:: scale
+.. autoclass:: ScalingMode
 
 Handlers
 ~~~~~~~~
 Single slide layout
 ~~~~~~~~~~~~~~~~~~~
 
+There is a big function for rendering text, which requires three functions, which:
+
+* create a Pango.layout of the text at a given size
+* calculate the cost of a given Pango.layout
+* generate the possible sizes
+
 .. automodule:: shinypress.text.single
     :members:

shinypress/render.py

     :type scaling_mode: shinypress.render.ScalingMode
     """
     pass
+
+class HAlignment(object):
+    """
+    Horizontal alignment.
+    Not an enum in the true sense, just some related constants.
+    """
+    LEFT = 0.0
+    CENTER = 0.5
+    RIGHT = 1.0
+
+class VAlignment(object):
+    """
+    Vertical alignment.
+    Not an enum in the true sense, just some related constants.
+    """
+    TOP = 0.0
+    MIDDLE = 0.5
+    BOTTOM = 1.0
+
+def render_text(
+    ctx, layout, size,
+    halign=HAlignment.LEFT,
+    valign=VAlignment.MIDDLE,
+    scaling_mode=ScalingMode.NONE,
+):
+    """
+    Render a Pango.layout on a Cairo context.
+
+    The text is first scaled according to the given scaling_mode (see the
+    ScalingMode enum for possible values).
+
+    The ``halign`` parameter determines the horizontal alignment (0.0 is left
+    aligned, 0.5 is in the center, 1.0 is right aligned). Similarly, ``valign``
+    determines vertical alignment (0.0 is top, 0.5 is middle, 1.0 is bottom).
+    There are constant values provided in the HAlignment and VAlignment enum
+    which should be used to improve readability.
+
+    :param ctx: the Cairo context to draw on
+    :type ctx: cairo.Context
+    :param layout: the text layout to draw
+    :type layout: Pango.Layout
+    :param halign: how the layout should be horizontally aligned
+    :type halign: float or HAlignment
+    :param valign: how the layout should be vertically aligned
+    :type valign: float or VAlignment
+    :param scaling_mode: how to resize the text to fit the given box
+    :type scaling_mode: shinypress.render.ScalingMode
+    """
+    pass
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.