Exemple #1
0
    def __init__(self, f, sampleText=None, **kwargs):
        """
        >>> from pagebot.fonttoolbox.objects.font import findFont
        >>> from pagebot.document import Document
        >>> from pagebot.constants import Letter, RIGHT
        >>> from pagebot.contexts.drawbotcontext import DrawBotContext
        >>> from pagebot.conditions import *
        >>> from pagebot.toolbox.units import em
        >>> from pagebot.toolbox.color import color
        >>> c = DrawBotContext()
        >>> w, h = Letter
        >>> doc = Document(w=w, h=h, padding=80, originTop=False, autoPages=2, context=c)
        >>> style = dict(fill=color(0.95), leading=em(1.4), fontSize=48, xTextAlign=RIGHT)
        >>> conditions = [Fit()] # FIX: Does not seem to work for TextBox
        >>> page = doc[1]
        >>> font1 = findFont('AmstelvarAlpha-VF')
        >>> gs = Sampler(font1, parent=page, conditions=conditions, padding=20, style=style, w=page.pw, h=page.ph, context=c)
        >>> style = dict(stroke=0, strokeWidth=0.25, leading=em(1.4), fontSize=48, xTextAlign=RIGHT)
        >>> page = doc[2]
        >>> font2 = findFont('RobotoDelta-VF')
        >>> #font2 = findFont('Upgrade-Regular')
        >>> #font2 = findFont('Escrow-Bold')
        >>> gs = Sampler(font2, parent=page, conditions=conditions, style=style, w=page.pw, h=page.ph, padding=20, context=c)
        >>> score = doc.solve()
        >>> doc.export('_export/%sSampler.pdf' % font1.info.familyName)

        TODO: Make self.css('xTextAlign') work for CENTER
        """
        TextBox.__init__(self, **kwargs)
        c = self.context
        style = self.style.copy()
        style['font'] = f.path
        sampleText = c.newString(sampleText or self.SAMPLE, style=style)
        self.bs = sampleText
        self.f = f  # Save font instance for later usage.
Exemple #2
0
 def build(self, view, origin, drawElements=True):
     """Run the code block. If the view.showSourceCode is True, then just export the code
     for debugging."""
     if not view.showSourceCode:
         self.run()
     else:
         TextBox.build(self, view, origin, drawElements)
Exemple #3
0
 def __init__(self, code, tryExcept=True, fill=None, style=None, **kwargs):
     if fill is None:
         fill = color(0.9)
         if style is None:
             style = self.DEFAULT_CODE_STYLE
     TextBox.__init__(self, bs=code, fill=fill, style=style, **kwargs)
     assert isinstance(code, str)
     self.code = code
     self.tryExcept = tryExcept
Exemple #4
0
    def __init__(self,
                 f,
                 foundryName=None,
                 designer=None,
                 foundryStyle=None,
                 fontNameStyle=None,
                 designerStyle=None,
                 **kwargs):
        u"""    
        >>> from pagebot.fonttoolbox.objects.font import findFont
        >>> from pagebot.document import Document
        >>> from pagebot.constants import Letter
        >>> from pagebot.contexts.drawbotcontext import DrawBotContext
        >>> from pagebot.contributions.filibuster.blurb import Blurb
        >>> blurb = Blurb()
        >>> c = DrawBotContext()
        >>> w, h = Letter
        >>> m = 80
        >>> doc = Document(w=w, h=h, padding=30, originTop=False, context=c)
        >>> page = doc[1]
        >>> #font = findFont('RobotoDelta-VF')
        >>> font = findFont('AmstelvarAlpha-VF')
        >>> foundryName = 'Google Fonts'
        >>> designer = 'David Berlow'
        >>> title = Title(font, x=m, w=w-2*m, foundryName=foundryName, designer=designer, parent=page, context=c)
        >>> tw, th = title.getTextSize()
        >>> title.y = (h - th)*2/3
        >>> doc.export('_export/%sTitle.pdf' % font.info.familyName)
        """
        TextBox.__init__(self, '', **kwargs)

        c = self.context
        if foundryStyle is None:
            foundryStyle = dict(font=f.path,
                                fontsize=self.BODY_SIZE,
                                rLeading=0.6)
        if fontNameStyle is None:
            fontNameStyle = dict(font=f.path, rLeading=1.9)
        if designerStyle is None:
            designerStyle = dict(font=f.path,
                                 fontsize=self.NAME_SIZE,
                                 rLeading=1.4,
                                 xTextAlign=RIGHT)

        self.f = f  # Font instance
        foundryName = foundryName or f.info.designer or 'Unknown foundry'
        familyName = f.info.familyName or 'Unknown family'
        designerName = 'by ' + designer or f.info.designer or 'Unknown designer'

        title = c.newString(foundryName + '\n', style=foundryStyle)
        title += c.newString(familyName + '\n', style=fontNameStyle, w=self.w)
        title += c.newString(designerName, style=designerStyle)
        self.bs = title
Exemple #5
0
    def __init__(self,
                 f,
                 foundryName=None,
                 description=None,
                 foundryStyle=None,
                 fontNameStyle=None,
                 bodyStyle=None,
                 **kwargs):
        """
        >>> from pagebot.fonttoolbox.objects.font import findFont
        >>> from pagebot.document import Document
        >>> from pagebot.constants import Letter
        >>> from pagebot.contexts.drawbotcontext import DrawBotContext
        >>> from pagebot.contributions.filibuster.blurb import Blurb
        >>> blurb = Blurb()
        >>> c = DrawBotContext()
        >>> w, h = Letter
        >>> m = 80
        >>> doc = Document(w=w, h=h, padding=30, originTop=False, context=c)
        >>> page = doc[1]
        >>> font = findFont('AmstelvarAlpha-VF')
        >>> foundryName = 'Google Fonts'
        >>> description = blurb.getBlurb('article', newLines=True, cnt=120)
        >>> bio = Bio(font, x=m, w=w-2*m, foundryName=foundryName, description=description, parent=page, context=c)
        >>> tw, th = bio.getTextSize()
        >>> bio.y = (h - th)*2/3
        >>> doc.export('_export/TypeNetworkBio.pdf')
        """
        TextBox.__init__(self, '', **kwargs)

        c = self.context
        if foundryStyle is None:
            foundryStyle = dict(font=f.path,
                                fontSize=self.BODY_SIZE,
                                leading=em(0.6))
        if fontNameStyle is None:
            fontNameStyle = dict(font=f.path, leading=em(1.9))
        if bodyStyle is None:
            bodyStyle = dict(font=f.path,
                             fontSize=self.BODY_SIZE,
                             leading=em(1.4))

        self.f = f  # Font instance
        foundryName = foundryName or f.info.designer or 'Unknown foundry'
        familyName = f.info.familyName or 'Unknown family'
        bio = c.newString(foundryName + '\n', style=foundryStyle)
        bio += c.newString(familyName + '\n',
                           style=fontNameStyle,
                           w=self.w * 2 / 3)
        bio += c.newString(description or f.info.description
                           or 'Unknown description ' * 40,
                           style=bodyStyle)
        self.bs = bio
Exemple #6
0
    def __init__(self,
                 f,
                 showLabel=True,
                 labelSize=7,
                 sampleText=None,
                 factor=0.9,
                 location=None,
                 useOpsz=True,
                 **kwargs):
        """
        >>> from pagebot.fonttoolbox.objects.font import findFont
        >>> from pagebot.document import Document
        >>> from pagebot.constants import Letter, RIGHT
        >>> from pagebot.contexts.drawbotcontext import DrawBotContext
        >>> from pagebot.conditions import *
        >>> from pagebot.toolbox.color import color        
        >>> c = DrawBotContext()
        >>> w, h = Letter
        >>> doc = Document(w=w, h=h, padding=80, originTop=False, autoPages=2, context=c)
        >>> style = dict(fill=color(0.95), leading=em(1.3), fontSize=48, xTextAlign=RIGHT)
        >>> conditions = [Fit()] # FIX: Does not seem to work for TextBox
        >>> page = doc[1]
        >>> font1 = findFont('AmstelvarAlpha-VF')
        >>> loc = dict(wght=1)
        >>> useOpsz = False
        >>> page.pw, page.w, 500
        (6.28", 8.50", 500)
        >>> gs = Waterfall(font1, parent=page, conditions=conditions, padding=20, style=style, w=page.pw, h=page.ph, location=loc, useOpsz=useOpsz, context=c)
        >>> style = dict(stroke=0, strokeWidth=0.25, leading=em(1.3), fontSize=48, xTextAlign=RIGHT)
        >>> page = doc[2]
        >>> font2 = findFont('RobotoDelta-VF')
        >>> #font2 = findFont('Upgrade-Regular')
        >>> #font2 = findFont('Escrow-Bold')
        >>> gs = Waterfall(font2, parent=page, conditions=conditions, style=style, w=page.pw, h=page.ph, padding=20, location=loc, useOpsz=useOpsz, context=c)
        >>> #score = doc.solve()
        >>> doc.export('_export/%sWaterfall_opsz_%s.pdf' % (font1.info.familyName, useOpsz))

        TODO: Make self.css('xTextAlign') work for CENTER
        """
        TextBox.__init__(self, **kwargs)
        c = self.context
        self.useOpsz = useOpsz  # Only for the sample lines. Labels always have opsz.
        self.f = f
        if not location:
            location = {}
        self.factor = factor  # Decreasing multiplication factor for fontSize
        style = self.style.copy()
        labelStyle = self.style.copy()
        labelStyle['font'] = self.getInstance(f, dict(opsz=labelSize)).path
        labelStyle['fontSize'] = labelSize
        labelStyle['leading'] = em(1)

        w = self.pw  # Initial with to fit top sample
        location['opsz'] = None
        style['font'] = self.getInstance(self.f,
                                         self.getLocation(self.f,
                                                          location)).path
        sampleText = sampleText or self.SAMPLE
        matchingLine = c.newString(sampleText + '\n', style=style, w=w)
        style['fontSize'] = fontSize = matchingLine.fittingFontSize // 8 * 8

        bs = c.newString('', style=style)
        while fontSize >= 12:
            # Still fitting? Otherwise stop the loop
            # TODO: Measure both lines (label + samleText) for fitting.
            tw, th = bs.size
            if th > self.ph:
                break
            # Make the optional label
            if showLabel:
                label = '%s %spt | opsz = %s\n' % (
                    self.f.info.familyName, asFormatted(fontSize,
                                                        '%0.1f'), self.useOpsz)
                bs += c.newString(label, style=labelStyle)
            style['fontSize'] = fontSize = int(round(fontSize * self.factor))
            location['opsz'] = {True: fontSize, False: None}[self.useOpsz]
            style['font'] = self.getInstance(
                self.f, self.getLocation(self.f, location)).path
            bs += c.newString(sampleText + '\n', style=style)

        self.bs = bs