Example #1
0
def drawOS2Label(varFamily, fonts, weight, width):
    c.fill(Color(1, 0, 0))
    c.stroke(noColor)
    R = 20
    x, y = weight, width * 100  # Scale OS/2 width to 1000
    oval(x - R / 2, y - R / 2, R, R)
    if varFamily.originFont in fonts:
        # If one of these is the guessed origin font, then draw marker
        c.fill(noColor)
        c.stroke(Color(1, 0, 0), 2)
        R = 27
        oval(x - R / 2, y - R / 2, R, R)
    return x, y
Example #2
0
    def drawAnimatedFrame(self, view, origin):
        """Draw the content of the element, responding to size, styles, font and content.
            Create 2 columns for the self.fontSizes ranges that show the text with and without [opsz]
            if the axis exists.

            """
        ox, oy, _ = origin
        c = self.context
        style = self.style.copy()

        instance = self.f.getInstance({})  # Get neutral instance
        style['font'] = instance.path
        style['textFill'] = Color(0.5, 0.5, 0.5,
                                  0.7)  # Opaque gray as background
        bs = c.newString(self.sampleText, style=style)
        tw, th = bs.size
        c.text(bs, (self.w / 2 - tw / 2, self.h / 3 + 20))

        # Now make instance and draw over regular and add to new copy of the style
        style = self.style.copy()
        instance = self.f.getInstance(self.style['location'])
        style['font'] = instance.path
        #print(self.frameIndex, style['font'])
        #style['fontSize'] = self.h/3
        bs = c.newString(self.sampleText, style=style)
        tw, th = bs.size
        c.text(bs, (self.w / 2 - tw / 2, self.h / 3 + 20))
def getColorRange(l):
    colorRange = []
    for i in range(l):
        v = i * 1.0 / l
        c = Color(0.7, 0.7, v)
        colorRange.append(c)
    return colorRange
Example #4
0
def makeDocument():
    # Create new document with (w,h) size and fixed amount of pages.
    # Note that most of the rootStyle is cascading through the e.css('name') call,
    # except that values of x, y, z, w, h, d

    # Just to show here how to get the root style. If not altered, it can be omitted.
    # as Document( ) will create a RootStyle by default.
    rootStyle = getRootStyle()

    doc = Document(rootStyle, originTop=False, w=W, h=H, autoPages=1)

    page = doc[1]  # Get the first/single page of the document.
    page.padding = 40  # TODO: order if 4 values?

    # Make rect as page element centered with centered origin.
    if RedRect:
        c = Color(1, 0, 0)
    else:
        c = Color(0.5)

    conditions = (Center2Center(), Middle2Middle())
    newRect(fill=c,
            parent=page,
            w=RectSize,
            h=RectSize,
            conditions=conditions,
            xAlign=CENTER,
            yAlign=MIDDLE)
    # Solve the layout conditions of the red rectangle.
    # Show if one of the conditions failed to solve.
    score = page.solve()
    if score.fails:
        print('Failed conditions', score.fails)

    # Set the view parameters for the required output.
    view = doc.getView()
    view.w = view.h = W, H
    view.padding = 40  # Make view padding to show crop marks and frame
    view.showFrame = True  # Show frame of the page in blue
    view.showPadding = False
    view.showCropMarks = True  # Show crop marks
    view.showOrigin = ShowOrigins  # Show origin alignment markers on each element.
    view.showDimensions = ShowOrigins
    view.showElementInfo = ShowElementInfo  # Show baxes with element info element.

    return doc
Example #5
0
def drawSierpinskiSquare(px, py, w, size, i):
    if i >= MAX_I:
        return

    i += 1

    if w < 1:
        return

    for x in range(3):
        for y in range(3):
            if x == 1 and y == 1:
                col = Color(max(0, 0.75 - w / 100))
                context.fill(col)
                context.rect(pt(px + w), pt(py + w), pt(w), pt(w))
            elif px <= size and py <= size:
                drawSierpinskiSquare(px + x * w, py + y * w, w / 3, size, i)
Example #6
0
def testBabelStrings():
    c.newPage(pt(W), pt(H))
    style = dict(font='Helvetica', fontSize=pt(100), textFill=color(1, 1, 0))

    # Formattted string using append.
    print(' * Testing with append')
    bs = c.newString(txt0)  # NOTE: style isn't initiated, bug later on.
    #print(bs.s._font)
    #print(bs.s._fontSize)
    #print(bs.s._fill)
    c.text(bs, (10, 10))

    # Contains a DrawBot FormattedString.
    aa = bs.s
    aa.append("123", font="Helvetica", fontSize=100, fill=color(1, 0, 1).rgb)
    c.text(bs,
           (pt(20),
            pt(20)))  # FIXME: no output, `bs` not initiated with style arg.

    # Formattted string using append.
    print(' * Testing with append')
    bs = c.newString(txt1, style=style)

    # Contains a DrawBot FormattedString.
    aa = bs.s
    aa.append(txt2, font="Helvetica", fontSize=100, fill=color(1, 0, 1).rgb)
    c.text(bs, (pt(200), pt(100)))

    # Formatted string without append.
    print(' * Testing without append')
    bs = c.newString(txt3, style=style)
    print('style: %s' % bs.style)
    aa = bs.s
    print(aa._font)
    print(aa._fontSize)
    c.fill(Color(1, 1, 0).rgb)
    print(aa._fill)
    text(aa, (100, 200))  # DrawBot
    c.text(bs, (pt(300), pt(200)))  # PageBot
Example #7
0
def drawFamilyOverview(path):
    varFamily = guessVarFamilyFromPaths(path)
    if not varFamily:  # No TTF fonts there?
        #print 'No TTF fonts found in', path
        return None

    # As we can guess the origin font, there is a reference for the other
    # masters to test against.
    #print '=== Guessed origin font:', path2Name(varFamily.originFont.path)
    #print checkInterpolation(varFamily.fonts)
    #print '=== Parametric axis fonts:', varFamily.parametricAxisFonts
    #print '=== Parametric axis metrics:', varFamily.parametricAxisMetrics

    newPage(1200, 1200)
    # Draw design space for width/weight
    c.translate(100, 100)
    c.stroke(color(0.5), 1)
    c.fill(color(0.9))
    c.rect(0, 0, 1000, 1000)
    for x in range(0, 1100, 100):
        if 0 < x < 1000:
            c.line((x, 0), (x, 1000))
        bs = c.newString(str(x), style=dict(fontSize=12))
        tw, th = c.textSize(bs)
        c.text(bs, (x - tw / 2, -20))

    for y in range(11):
        if 0 < y < 11:
            c.line((0, 1000 / 10 * y), (1000, 1000 / 10 * y))
        if y > 0:
            bs = c.newString(str(y),
                             style=dict(fontSize=12, fill=Color(1, 0, 0)))
            bs += c.newString(' %s' % (y * 100),
                              style=dict(fontSize=12, fill=Color(0, 0.5, 0)))
            tw, th = c.textSize(bs)
        else:
            bs = c.newString(str(y), style=dict(fontSize=12, fill=blackColor))
            tw, th = c.textSize(bs)
        c.text(bs, (-10 - tw, 1000 / 10 * y - th / 2))

    # Draw axis labels
    bs = c.newString('OS/2 weight class ',
                     style=dict(fontSize=12, fill=Color(1, 0, 0)))
    bs += c.newString('&', style=dict(fontSize=12, fill=Color(0)))
    bs += c.newString(' XTRA Axis (H-stem width)',
                      style=dict(fontSize=12, fill=Color(0, 0.5, 0)))
    tw, bh = c.textSize(bs)
    c.text(bs, (0, -50 - th / 2))

    c.save()
    c.rotate(90)
    bs = c.newString('OS/2 width class ',
                     style=dict(fontSize=12, fill=Color(1, 0, 0)))
    bs += c.newString('&', style=dict(fontSize=12, fill=Color(0)))
    bs += c.newString(' XOPQ Axis + stem (H.width - H.stem - H.lsb - H.rsb)',
                      style=dict(fontSize=12, fill=Color(0, 0.5, 0)))
    tw, th = c.textSize(bs)
    c.text(bs, (0, 60))
    c.restore()

    # Draw family name title
    bs = c.newString(varFamily.name, style=dict(fontSize=24))
    c.text(bs, (0, 1000 + 30))

    fIndex = 1
    # Find widths and weights as defined by OS/2 and plot them as dots + names
    for (weight, width), fonts in varFamily.getOS2WeightWidthClasses().items():

        # Draw positions according to OS/2 values
        x, y = drawOS2Label(varFamily, fonts, weight, width)

        # Draw H and labels, stacked under the dot.
        for f in fonts:
            drawFontLabel((x, y), varFamily, f, fIndex)
            fIndex += 1

            # Calculate the percentage of width and weight and their position in the graph
            #px, py, pWeight, pWidth = getCalculatedWeightWidth(varFamily, font, weight, width)
            #drawCalculatedWeightWidthArrow((x, y), (px, py), pWeight, pWidth, varFamily, f, fIndex)

    if varFamily is not None:
        xtraMinFont, xtraMaxFont = varFamily.makeParametricFonts(
            varFamily.XTRA)
        #print xtraMinFont, xtraMaxFont
        x, y = drawOS2Label(varFamily, [xtraMinFont],
                            xtraMinFont.info.weightClass,
                            xtraMinFont.info.widthClass)
        drawFontLabel((x, y),
                      varFamily,
                      xtraMinFont,
                      fAxis=varFamily.XTRA + '_min')

        x, y = drawOS2Label(varFamily, [xtraMaxFont],
                            xtraMaxFont.info.weightClass,
                            xtraMaxFont.info.widthClass)
        drawFontLabel((x, y),
                      varFamily,
                      xtraMaxFont,
                      fAxis=varFamily.XTRA + '_max')

    return varFamily
Example #8
0
def drawFontLabel(p, varFamily, f, fIndex=None, fAxis=None):
    x, y = p
    #print f.info.styleName, f.info.weightClass, f.info.widthClass

    if not GLYPH in f:
        print('###', GLYPH, 'not in font', f.path)
        return

    glyphH = f[GLYPH]
    if not glyphH.width:
        print('###', GLYPH, 'No width', f.path)
        return

    stems = glyphH.analyzer.stems
    # Draw marker on actual position of H.stem and H.weight as green dot
    stemValues = stems.keys()

    s = 0.05 * 1000 / f.info.unitsPerEm
    leading = 2048 / f.info.unitsPerEm
    c.stroke(noColor)
    c.fill(Color(0))
    save()
    translate(x - glyphH.width / 2 * s, y - leading - 50)
    scale(s)
    drawPath(glyphH.path)
    restore()
    y -= leading + 50

    save()
    pathLabel = '-'.join(path2Name(f.path).split('-')[1:])
    #label = path2Name(f.path)
    if fAxis is not None:
        label = '@' + fAxis
    elif fIndex is None:
        label = ''
    else:
        label = '#%d ' % fIndex
    label += '%s\n(%s)\n%d' % (pathLabel.replace('.ttf', '').replace(
        '_', '\n').replace('-', '\n'), f.info.styleName, f.info.weightClass)
    fs = FormattedString(label, style=dict(fontSize=10, align=CENTER))
    tw, th = textSize(fs)
    text(fs, (x - tw / 2, y - 14))
    restore()
    y -= leading + th - 22

    if stemValues:  # Cannot find H-stem, skip this marker
        stem = min(stemValues)
        stemS = '%d' % stem
        normalizedStemS = '%0.2f' % (stem * 1000 / f.info.unitsPerEm)

        # XOPQ (counter) + H.stem == H.width - H.stem - H.lsb - H.rsb
        width = glyphH.width - stem - glyphH.leftMargin - glyphH.rightMargin

        c.fill(Color(0, 0.5, 0))
        c.stroke(noColor)
        R = 16
        weightLoc, widthLoc = stem, width / 2
        c.oval(weightLoc - R / 2, widthLoc - R / 2, R, R)
        if fAxis is not None:
            label = '@' + fAxis
        elif fIndex is None:
            label = ''
        else:
            label = '#%d\n' % fIndex
        bs = c.newString(label + ('S:%d\nW:%d\n%d' %
                                  (weightLoc, widthLoc, f.info.weightClass)),
                         style=dict(fontSize=10,
                                    xTextAlign='center',
                                    textFill=Color(0)))
        tw, th = c.textSize(bs)
        c.text(bs, (weightLoc - tw / 2, widthLoc - 24))

        if varFamily.originFont is f:
            # If one of these is the guessed origin font, then draw marker
            c.fill(noColor)
            c.stroke(Color(0, 0.5, 0), 2)  # Stroke color and width
            R = 23
            c.oval(weightLoc - R / 2, widthLoc - R / 2, R, R)

        # Find distance between left side of 2 stems of the H
        eqStems = glyphH.analyzer.stems.values()
        x1 = 10000000000
        x2 = 0
        for stems in eqStems:
            for stem in stems:
                x1 = min(x1, stem.parent[0])
                x2 = max(x2, stem.parent[0])
    else:
        stemS = 'No stem'
        normalizedStemS = '-'
        x1 = x2 = 0
        #print 'No stem for', glyphH.font

    exportCSV.write('%s,%d,%s,%s,%s,%d,%d,%d,%d,%d\n' % (
        f.path.split('/')[-1],
        f.info.unitsPerEm,
        stemS,
        normalizedStemS,
        x2 - x1,
        (x2 - x1) * 1000 / f.info.unitsPerEm,
        #normalizedWidth,
        #normalizedWeight,
        #guessedOS2Width,
        #guessedOS2Weight,
        f.info.widthClass,
        f.info.weightClass,
        f.info.capHeight,
        f.info.capHeight * 1000 / f.info.unitsPerEm))
Example #9
0
import sys
from pagebot.contexts.platform import getContext
from pagebot.toolbox.units import *
from pagebot.toolbox.color import Color

context = getContext()

for p in range(10):
    context.newPage(pt(1000), pt(1000))
    for n in range(50):
        col = Color(random(), 0, random(), 0.5 + random() * 0.2)
        context.fill(col)
        ch = random()
        x = 20 + random() * 800
        y = 20 + random() * 800

        if ch < 0.2:
            context.oval(pt(x), pt(y), pt(80), pt(80))
        elif ch < 0.4:
            context.rect(pt(x), pt(y), pt(80), pt(80))
        else:
            context.fontSize(pt(24))
            context.text('Hello world on %d,%d' % (x, y), (x, y))

context.saveImage('_export/HelloCircleSquare.gif')
Example #10
0
def makeDocument():
    """Demo random book cover generator."""

    # Create new document with (w,h) and fixed amount of pages.
    # Make number of pages with default document size.
    # Initially make all pages default with template
    # One page, just the cover.
    doc = Document(w=W,
                   h=H,
                   title='A Demo Book Cover',
                   autoPages=1,
                   originTop=False)

    page = doc[1]  # Get the first/single page of the document.
    page.name = 'Cover'

    # Get the current view of the document. This allows setting of
    # parameters how the document is represented on output.
    view = doc.view
    view.w, view.h = W, H
    # Set view options. Full list is in elements/views/baseviews.py
    view.padding = 40  # Showing cropmarks and registration marks
    # need >= 20 padding of the view.
    view.showRegistrationMarks = True
    view.showCropMarks = True
    view.showFrame = False
    view.showPadding = False
    view.showNameInfo = True
    view.showTextOverflowMarker = False

    context = view.context

    C1 = Color(r=0.2 + random() * 0.8,
               g=random() * 0.2,
               b=0.4 + random() * 0.2)

    # Make background element, filling the page color and bleed.
    colorRect1 = newRect(z=-10,
                         name='Page area',
                         parent=page,
                         pt=40,
                         conditions=[
                             Top2TopSide(),
                             Left2LeftSide(),
                             Fit2RightSide(),
                             Fit2BottomSide()
                         ],
                         fill=C1)
    colorRect1.bleed = BLEED
    colorRect1.solve()  # Solve element position, before we can make
    # other elements depend on position and size.

    M = 64
    colorRect2 = newRect(
        z=-10,
        name='Frame 2',
        parent=colorRect1,
        conditions=[Center2Center(), Middle2Middle()],
        fill=C1.darker(0.5),  # Default parameter:
        # 50% between background color and white
        stroke=noColor,
        w=colorRect1.w - M - BLEED,
        h=colorRect1.h - M - BLEED,
        xAlign=CENTER,
        yAlign=MIDDLE)

    colorRect3 = newRect(
        z=-10,
        name='Frame 3',
        parent=colorRect2,
        conditions=[Center2Center(), Middle2Middle()],
        fill=C1.darker(0.3),  # Default parameter:
        # 50% between background color and white
        stroke=noColor,
        w=colorRect1.w - 2 * M,
        h=colorRect1.h - 2 * M,
        xAlign=CENTER,
        yAlign=MIDDLE)

    # Make random blurb name and titles
    title = blurb.getBlurb('book_phylosophy_title')
    subTitle = blurb.getBlurb('book_pseudoscientific').capitalize()
    if random() < 0.2:  # 1/5 chance to add editions text
        subTitle += '\nEdition ' + blurb.getBlurb('edition')
    authorName = blurb.getBlurb('name', noTags=True)
    if random() < 0.33:  # 1/3 chance for a second author name
        authorName += '\n' + blurb.getBlurb('name')

    # Add some title (same width, different height) at the "wrongOrigin" position.
    # They will be repositioned by solving the colorConditions.
    titleS = context.newString('')
    for word in title.split(' '):
        titleS += context.newString(' ' + word,
                                    style=dict(font=fontRegular.path,
                                               fontSize=50,
                                               w=page.pw,
                                               leading=em(1.2),
                                               xTextAlign=CENTER,
                                               textFill=whiteColor))
    #title += context.newString(subTitle + '\n\n', style=dict(font=fontRegular.path, fontSize=32, xTextAlign=CENTER, textFill=(1, 1, 1,0.5)))
    #title += context.newString(authorName, style=dict(font=fontItalic.path, fontSize=24, tracking=em(0.025), xTextAlign=CENTER, textFill=(1, 0.5, 1,0.7)))
    newTextBox(titleS,
               parent=colorRect2,
               name='Title',
               conditions=[Fit2Width(),
                           Center2Center(),
                           Top2Top()],
               xAlign=CENTER,
               yAlign=TOP)

    score = page.evaluate()
    if score.fails:
        page.solve()

    # Evaluate again, result should now be >= 0
    return doc
Example #11
0
#
# Test InDesign Scripting.
#

from pagebot.contexts.indesigncontext import InDesignContext
from pagebot.toolbox.units import pt
from pagebot.toolbox.color import Color
from pagebot.constants import A4Rounded

context = InDesignContext()
path = context.getInDesignScriptPath() + 'test.jsx'
#print(path)
H, W = A4Rounded

# CMYK by default?
f = Color(c=0, m=0, y=1, k=0)
s = Color(c=0, m=1, y=0, k=0)

# FIXME: synchronize newDocument() and newDrawing() across contexts.
context.newDocument(W, H)
#context.newPage(w=W, h=H)
print(context.fill)
context.fill(f)
context.stroke(s)
context.text('bla', (pt(100), pt(100)))
#p1 = (100, 100)
#p2 = (200, 200)
#context.line(pt(p1), pt(p2))
context.oval(pt(100), pt(100), pt(200), pt(200))
context.rect(pt(100), pt(200), pt(110), pt(120))
print(context.b)
Example #12
0
from pagebot.toolbox.color import color, Color

c = getContext()
W = H = 500

c.newPage(pt(W), pt(H))

# Formattted string using append.
print(' * Testing with append')
bs = c.newString('')

# Contains a DrawBot FormattedString.
aa = bs.s
aa.append("123", font="Helvetica", fontSize=100, fill=color(1, 0, 1).rgb)
print(aa._font)
print(aa._fontSize)
print(aa._fill)
#c.text(bs, (pt(100), pt(100)))

# Formatted string without append.
style = dict(font='Helvetica', fontSize=pt(100), textFill=color(1, 1, 0))
print(' * Testing without append')
bs = c.newString('bla', style=style)
print('style: %s' % bs.style)
aa = bs.s
print(aa._font)
print(aa._fontSize)
c.fill(Color(1, 1, 0).rgb)
print(aa._fill)
text(aa, (100, 200))
c.text(bs, (pt(100), pt(200)))
Example #13
0
import traceback
from random import random
from pagebot import getAllContexts, getResourcesPath
from pagebot.toolbox.color import Color
from pagebot.constants import A4Rounded
from pagebot.contexts.base.babelstring import BabelString
from pagebot import getContext
from pagebot.toolbox.units import pt
from pagebot.document import Document
from pagebot.fonttoolbox.objects.font import findFont
H, W = A4Rounded
W = pt(W)
H = pt(H)

f = Color(0, 1, 0)
s = Color(1, 0, 0)

def testContexts():
    contexts = getAllContexts()

    for i, c in enumerate(contexts):
        if i in (0, 1):
            #print(c)
            try:
                testContext(c)
            except Exception as e:
                    print('Context errors', traceback.format_exc())

def getRandom():
    x = (W - 100) * random()
Example #14
0
class Galley(Element):
    u"""A Galley is sticky sequential flow of elements, where the parts can have
    different widths (like headlines, images and tables) or responsive width, such as images
    and formatted text volumes. Size is calculated dynamically, since one of the enclosed
    elements may change width/height at any time during the composition process.
    Also the sequence may change by slicing, adding or removing elements by the Composer.
    Since the Galley is a full compatible Element, it can contain other galley instances
    recursively."""
    from pagebot.elements.pbtextbox import TextBox
    from pagebot.elements.pbruler import Ruler
    TEXTBOX_CLASS = TextBox
    RULER_CLASS = Ruler

    OLD_PAPER_COLOR = Color(rgb=0xF8ECC2).css  # Color of old paper: #F8ECC2

    def __init__(self, **kwargs):
        Element.__init__(self, **kwargs)
        # Make sure that this is a formatted string. Otherwise create it with the current style.
        # Note that in case there is potential clash in the double usage of fill and stroke.
        self.lastTextBox = None

    def append(self, bs):
        u"""Add the string to the last text box. Create a new textbox if not found."""
        if self.lastTextBox is None:
            self.newTextBox(bs)  # Also sets self.lastTextBox
        else:
            self.lastTextBox.append(bs)

    def getMinSize(self):
        u"""Cumulation of the maximum minSize of all enclosed elements."""
        minW = minH = 0  # Let's see if we need bigger than this.
        for e in self.elements:
            eMinW, eMinH = e.getMinSize()
            minW = max(minW, eMinW)
            minH += eMinH
        return minW, minH

    def appendElement(self, e):
        u"""Add element to the list of child elements. Note that elements can be added multiple times.
        If the element is alread placed in another container, then remove it from its current parent.
        This relation and position is lost. The position e is supposed to be filled already in local position."""
        eParent = e.parent
        if not eParent is None:
            eParent.removeElement(
                e)  # Remove from current parent, if there is one.
        self._elements.append(
            e
        )  # Possibly add to self again, will move it to the top of the element stack.
        e.setParent(
            self)  # Set parent of element without calling this method again.
        if e.eId:  # Store the element by unique element id, if it is defined.
            self._eIds[e.eId] = e
        # If this is a text box, then set self.lastTextBox
        if e.isTextBox:
            self.lastTextBox = e
        return len(self._elements) - 1  # Answer the element index for e.

    def getSize(self):
        u"""Answer the enclosing rectangle of all elements in the galley."""
        w = self.w or 0
        h = self.h or 0
        if w and h:  # Galley has fixed/forced size:
            return w, h
        # No fixed size set. Calculate required size from contained elements.
        for e in self.elements:
            ew, eh = e.getSize()
            w = max(w, ew)
            h += eh
        return w, h

    def getWidth(self):
        return self.getSize()[0]

    def getHeight(self):
        return self.getSize()[1]

    def getLastElement(self):
        u"""Answer the last element in the sequence."""
        elements = self.elements
        if not elements:
            return None
        return elements[-1]

    def newTextBox(self, fs, html=None):
        u"""Create a new *self.TEXTBOX_CLASS* instance, filled with the *fs* FormattedString.
        Append the element to *self* (also setting self.lastTextBox) and answer the element."""
        tb = self.TEXTBOX_CLASS('', parent=self, html=html)
        self.appendElement(
            tb
        )  # Will set the self.lastTextBox by local self.appendElement(tb)
        return tb

    def newRuler(self, style):
        u"""Add a new Ruler instance, depending on style."""
        ruler = self.RULER_CLASS(style=style)
        self.appendElement(ruler)

    #   D R A W B O T / F L A T  S U P P O R T

    def build(self, view, origin=ORIGIN, drawElements=True):
        u"""Like "rolled pasteboard" galleys can draw themselves, if the Composer decides to keep
        them in tact, instead of select, pick & choose elements, until the are all
        part of a page. In that case the w/h must have been set by the Composer to fit the
        containing page."""

        context = self.context  # Get current context and builder.
        b = context.b  # This is a bit more efficient than self.b once we got context

        p = pointOffset(self.oPoint, origin)
        p = self._applyScale(view, p)
        px, py, _ = self._applyAlignment(p)  # Ignore z-axis for now.

        # Let the view draw frame info for debugging, in case view.showElementFrame == True
        view.drawElementFrame(self, p)

        if self.drawBefore is not None:  # Call if defined
            self.drawBefore(self, view, p)

        context.setFillColor(
            self.OLD_PAPER_COLOR)  # Color of old paper: #F8ECC2
        gw, gh = self.getSize()
        b.rect(px, py, gw, gh)
        if drawElements:
            hook = 'build_' + self.context.b.PB_ID
            # Don't call self.buildElements, as we want to track the vertical positions
            gy = 0
            for e in self.elements:
                if not e.show:
                    continue
                # @@@ Find space and do more composition
                if hasattr(e, hook):
                    getattr(e, hook)(view, (px, py + gy))
                else:  # No implementation for this context, call default building method for this element.
                    e.build(view, (px, py + gy))
                gy += e.h

        if self.drawAfter is not None:  # Call if defined
            self.drawAfter(self, view, p)

        self._restoreScale(view)
        view.drawElementMetaInfo(self, origin)

    #   H T M L  /  C S S  S U P P O R T

    def build_html(self, view, origin=None, drawElements=True):

        if self.drawBefore is not None:  # Call if defined
            self.drawBefore(self, view)

        if drawElements:
            self.buildElements(self, view)

        if self.drawAfter is not None:  # Call if defined
            self.drawAfter(self, view)
Example #15
0
def makeDocument():
    u"""Demo random book cover generator."""

    # Create new document with (w,h) and fixed amount of pages.
    # Make number of pages with default document size.
    # Initially make all pages default with template
    # One page, just the cover.
    doc = Document(w=W,
                   h=H,
                   title='A Demo Book Cover',
                   autoPages=1,
                   originTop=False)

    page = doc[1]  # Get the first/single page of the document.
    page.name = 'Cover'

    # Get the current view of the document. This allows setting of
    # parameters how the document is represented on output.
    view = doc.view
    view.w, view.h = W, H
    # Set view options. Full list is in elements/views/baseviews.py
    view.padding = 40  # Showing cropmarks and registration marks
    # need >= 20 padding of the view.
    view.showPageRegistrationMarks = True
    view.showPageCropMarks = True
    view.showPageFrame = True
    view.showPagePadding = False
    view.showPageNameInfo = True
    view.showTextOverflowMarker = False

    context = view.context

    C1 = Color(r=random() * 0.2, g=random() * 0.2, b=random() * 0.9)

    # Make background element, filling the page color and bleed.
    colorRect1 = newRect(z=-10,
                         name='Page area',
                         parent=page,
                         conditions=[
                             Top2TopSide(),
                             Left2LeftSide(),
                             Fit2RightSide(),
                             Fit2BottomSide()
                         ],
                         fill=C1)
    colorRect1.bleed = BLEED
    colorRect1.solve()  # Solve element position, before we can make
    # other elements depend on position and size.

    M = BLEED + 64
    newRect(
        z=-10,
        name='Frame 2',
        parent=colorRect1,
        conditions=[Center2Center(), Middle2Middle()],
        fill=C1.darker(0.5),  # Default parameter:
        # 50% between background color and white
        stroke=None,
        w=colorRect1.w - M,
        h=colorRect1.h - M,
        xAlign=CENTER,
        yAlign=MIDDLE)

    # Make random blurb name and titles
    title = blurb.getBlurb('book_phylosophy_title')
    subTitle = blurb.getBlurb('book_pseudoscientific').capitalize()
    if random() < 0.2:  # 1/5 chance to add editions text
        subTitle += '\nEdition ' + blurb.getBlurb('edition')
    authorName = blurb.getBlurb('name', noTags=True)
    if random() < 0.33:  # 1/3 chance for a second author name
        authorName += '\n' + blurb.getBlurb('name')

    page.pt = 100  # Now the rectangles positioned automatic, alter the paddings
    page.pl = page.pr = 80
    page.pb = 20
    # Add some title (same width, different height) at the "wrongOrigin" position.
    # They will be repositioned by solving the colorConditions.
    title = context.newString(title + '\n\n',
                              style=dict(font=fontBold.path,
                                         fontSize=40,
                                         rLeading=1.2,
                                         xTextAlign=CENTER,
                                         textFill=1))
    title += context.newString(subTitle + '\n\n',
                               style=dict(font=fontRegular.path,
                                          fontSize=32,
                                          xTextAlign=CENTER,
                                          textFill=(1, 1, 1, 0.5)))
    title += context.newString(authorName,
                               style=dict(font=fontItalic.path,
                                          fontSize=24,
                                          rTracking=0.025,
                                          xTextAlign=CENTER,
                                          textFill=(1, 0.5, 1, 0.7)))
    newTextBox(title,
               parent=page,
               name='Other element',
               conditions=[Fit2Width(),
                           Center2Center(),
                           Top2Top()],
               xAlign=CENTER,
               yAlign=TOP)

    typoIllustration = context.newString('&',
                                         style=dict(font=ampersandFont.path,
                                                    fontSize=300,
                                                    xTextAlign=CENTER,
                                                    textFill=(1, 0.5, 1, 0.7)))
    newTextBox(typoIllustration,
               parent=page,
               conditions=[Fit2Width(),
                           Center2Center(),
                           Bottom2Bottom()],
               xAlign=CENTER,
               yAlign=TOP)

    # Evaluate again, result should now be >= 0
    score = page.evaluate()
    if score.fails:  # There is new "failing" elements. Solve their layout.
        page.solve()

    return doc
Example #16
0
from pagebot.toolbox.transformer import json2Dict
from pagebot.document import Document
from pagebot.fonttoolbox.objects.font import findFont
from pagebot.elements import newTextBox, newImage

W = 652
H = 850
W = pt(W)
H = pt(H)
PADDING = pt(83, 42, 19, 33)
ACC_GUTTER = pt(32) # Distance between accommodations
IMG_GUTTER = pt(3) # Vertical distance between images
COL_GUTTER = pt(12) # Distance between columns

#f = Color(0, 0, 0)
s = Color(1, 0, 0)

drawBotContext = getContext('DrawBot')
flatContext = getContext('Flat')
boldFontName = 'PageBot-Bold'
boldFont = findFont(boldFontName)
regularFontName = 'Roboto-Regular'
regularFont = findFont(regularFontName)
LINE = 12
TEXTSIZE = 12
HEADSIZE = 14

def loadJSON(context):
    doc = Document(w=W, h=H, originTop=False, context=context)
    #doc = Document(w=W, h=H, originTop=True, context=context)
    view = doc.getView()
Example #17
0
# -----------------------------------------------------------------------------
#
#     pdflibstring.py
#

import os
import re

from pagebot.contexts.strings.babelstring import BabelString
from pagebot.style import css
from pagebot.constants import LEFT, DEFAULT_FONT_SIZE, DEFAULT_LEADING
from pagebot.paths import DEFAULT_FONT_PATH
from pagebot.toolbox.units import upt
from pagebot.toolbox.color import Color

DEFAULT_COLOR = Color(0, 0, 0)


class PdfLibString(BabelString):
    """FlatString is a wrapper around the Flat string."""

    BABEL_STRING_TYPE = 'flat'
    UNITS = 'pt'

    def __init__(self, s, context, style=None):
        """Constructor of the DrawBotString, which is a wrapper around
        DrawBot.FormattedString. Optionally stores the (latest) style that was
        used to produce the formatted string.

        >>> from pagebot.contexts.flatcontext import FlatContext
        >>> context = FlatContext()