Example #1
0
    def on_layout(self, cvs, system):
        HBox.on_layout(self, cvs, system)
        Dia.on_layout(self, cvs, system)
        dias = self.boxs

        # steal layout variables from the children
        x_top = []
        x_bot = []
        for dia in dias:
            x_top += dia.x_top
            x_bot += dia.x_bot
        y_left = dias[0].y_left
        y_right = dias[-1].y_right
        assert len(x_top) == self.n_top
        assert len(x_bot) == self.n_bot
        assert len(y_left) == self.n_left
        assert len(y_right) == self.n_right
        self.x_top = x_top
        self.x_bot = x_bot
        self.y_left = y_left
        self.y_right = y_right

        # join children left to right
        i = 0
        while i + 1 < len(dias):
            left = dias[i]
            right = dias[i + 1]
            n = left.n_right  # == right.n_left
            for j in range(n):
                system.add(left.y_right[j] == right.y_left[j])
            i += 1
Example #2
0
def test_snake():

    # Composing diagrams and box's
    # -----------------------------
    #
    # The base class for a diagram is `Dia`. Diagrams are
    # also `Box`'s so we can stick them anywhere we can use a `Box`.

    from huygens import config, canvas
    from huygens.box import Box, HBox
    from huygens.diagram import HDia, VDia, VWire, Cap, Cup, SIZE
    Box.DEBUG = False

    config(text="pdftex")

    top = HDia([VWire(), Cap()])
    #mid = HDia([VWire(), VWire(), VWire()])
    bot = HDia([Cup(), VWire()])
    lsnake = VDia([top, bot])

    top = HDia([Cap(), VWire()])
    bot = HDia([VWire(), Cup()])
    rsnake = VDia([top, bot])

    boxs = [lsnake, "$=$", VWire(min_height=SIZE), "$=$", rsnake]
    dia = HBox(boxs, align="center")

    yield dia

    # If we do this again with DEBUG you can see how
    # the underlying `Box`s are put together.

    Box.DEBUG = True

    # Currently, the anchor inside each `Dia` is not constrained and
    # so is free to wander around inside the `Box`.
    # Maybe this will change in the future.

    yield dia

    # If we use a `StrictHBox` it will stretch the `VWire()`
    # but then we need to put the text in a `SlackBox`.

    from huygens.box import SlackBox, StrictHBox
    boxs = [lsnake, SlackBox("$=$"), VWire(), SlackBox("$=$"), rsnake]
    dia = StrictHBox(boxs, align="center")

    yield dia, "hbox-slack-dia"
Example #3
0
def test_yang_baxter():

    from huygens.box import Box, HBox
    from huygens.diagram import VWire, Braid
    Box.DEBUG = False

    Id = VWire

    scale = 2.0
    w = 1.5 * scale
    h = 0.5 * scale
    s12 = lambda: Braid(min_width=w, min_height=h) @ Id(min_height=h,
                                                        min_width=h)
    s23 = lambda: Id(min_height=h, min_width=h) @ Braid(min_width=w,
                                                        min_height=h)

    lhs = s12() * s23() * s12()
    #lhs = lhs @ Braid()
    rhs = s23() * s12() * s23()
    box = HBox([lhs, "$=$", rhs], align="center")

    yield box, "yang-baxter"
Example #4
0
def test_braid_3():
    from huygens.box import Box, HBox
    from huygens.diagram import VWire, Braid
    Box.DEBUG = False

    Id = VWire

    scale = 1.0
    w = 0.8 * scale
    h = 0.5 * scale
    s1 = lambda: Braid(min_width=w, min_height=h) @ Id(min_height=h,
                                                       min_width=h)
    s2 = lambda: Id(min_height=h, min_width=h) @ Braid(min_width=w,
                                                       min_height=h)

    from operator import mul
    from functools import reduce

    word = [[s1, s2][i % 2]() for i in range(6)]
    box = reduce(mul, word)

    box = HBox(["$Z =$", box], align="center")

    yield box, "braid-Z"
Example #5
0
def test_box():

    #
    # [<<< table of contents](index.html)
    #
    #  ---
    #
    # Layout with Box's
    # =================
    #

    from random import random, seed
    seed(0)
    from huygens.front import canvas, path
    from huygens.box import (Box, EmptyBox, CanBox, TextBox, HBox, VBox, OBox,
                             TableBox, FillBox, MarginBox, AlignBox)

    # First we set a debug flag so we can see the shape of every box
    #

    Box.DEBUG = True

    # Every Box has an anchor point, shown with a cross.
    # Then, the distance to the four sides of the box
    # are stored as attributes `top`, `bot`, `left`, and `right`.

    box = EmptyBox(top=0.5, bot=1.0, left=0.2, right=2.)

    # To render a box onto a canvas:
    cvs = canvas.canvas()
    box.render(cvs)

    # We can then call `cvs.writeSVGfile("output.svg")` to save an svg file:

    yield box, "empty"

    #--------------------------------------------------

    box = TextBox("Hey there!")
    yield box, "text"

    #--------------------------------------------------

    cvs = canvas.canvas()
    cvs.stroke(path.line(0., 0., 1., 1.))
    cvs.text(0., 0., "hello everyone")
    box = CanBox(cvs)
    yield box, 'canbox'

    #--------------------------------------------------

    # You cannot use the same box more than once in a container Box:

    box = TextBox("hello")
    box = HBox([box, box])  # FAIL
    #yield box, "hbox-fail" # raises assert error

    #--------------------------------------------------

    box = HBox("geghh xxde xyeey".split())
    yield box, "hbox-text"

    #--------------------------------------------------

    box = VBox("geghh xxde xyeey".split())
    yield box, "vbox-text"

    #--------------------------------------------------

    r = 1.0
    a = EmptyBox(top=r, bot=r)
    b = EmptyBox(top=r, bot=r)
    c = EmptyBox(left=r, right=r)
    #box = StrictVBox([a, c])
    box = VBox([a, c])
    yield box, 'vbox-empty'

    #--------------------------------------------------

    box = OBox([
        EmptyBox(.4, .1, 0., 2.2),
        EmptyBox(.3, 0., .5, 2.5),
        EmptyBox(1., .5, .5, .5),
        FillBox(.2, .2),
    ])
    yield box, "obox"

    #--------------------------------------------------

    box = HBox([
        VBox([TextBox(text) for text in "xxx1 ggg2 xxx3 xx4".split()]),
        VBox([TextBox(text) for text in "123 xfdl sdal".split()]),
    ])
    yield box, "hbox-vbox"

    #--------------------------------------------------

    box = TableBox([[
        EmptyBox(.4, .1, 0.2, 2.2),
        EmptyBox(.3, 1.2, .5, 2.5),
    ], [
        EmptyBox(.8, .1, 0.4, 1.2),
        EmptyBox(.5, 0.4, .5, 1.5),
    ]])
    yield box, "table"

    #--------------------------------------------------

    def rnd(a, b):
        return (b - a) * random() + a

    a, b = 0.2, 1.0
    rows = []
    for row in range(3):
        row = []
        for col in range(3):
            box = EmptyBox(rnd(a, b), rnd(a, b), rnd(a, b), rnd(a, b))
            row.append(box)
        rows.append(row)

    box = TableBox(rows)
    yield box, "table-2"

    #--------------------------------------------------

    rows = []
    for i in range(3):
        row = []
        for j in range(3):
            box = TextBox(("xbcgef"[i + j]) * (i + 1) * (j + 1))
            box = MarginBox(box, 0.1)
            box = AlignBox(box, "north")
            row.append(box)
        row.append(EmptyBox(bot=1.))
        rows.append(row)
    box = TableBox(rows)
    yield box, "table-3"

    #--------------------------------------------------

    a, b = 0.2, 2.0
    rows = []
    for row in range(2):
        boxs = []
        for col in range(2):
            top = rnd(a, b)
            bot = rnd(a, b)
            left = rnd(a, b)
            right = rnd(a, b)
            box = EmptyBox(top, bot, left, right)
            boxs.append(box)
        boxs.append(TextBox("Hig%d !" % row))
        box = HBox(boxs)
        rows.append(box)
    box = VBox(rows)
    yield box, "table-4"