示例#1
0
    def __init__(self, famname):
        if isinstance(famname, Font):
            famname = famname.family

        self._name = family_name(famname)
        self._faces = odict( (f.psname,f) for f in family_members(self._name) )
        self.encoding = font_encoding(self._faces.keys()[0])
    def __init__(self, famname):
        if isinstance(famname, Font):
            famname = famname.family

        self._name = family_name(famname)
        self._faces = odict( (f.psname,f) for f in family_members(self._name) )
        self.encoding = font_encoding(self._faces.keys()[0])
示例#3
0
    def __init__(self, famname=None, of=None):
        if of:
            famname = font_family(of)
        elif not famname:
            badarg = 'Family: requires either a name or a Font object' % famname
            raise DeviceError(badarg)

        q = famname.strip().lower().replace(' ', '')
        matches = [
            fam for fam in family_names() if q == fam.lower().replace(' ', '')
        ]
        if not matches:
            notfound = 'Unknown font family "%s"' % famname
            raise DeviceError(notfound)
        self._name = matches[0]

        faces = family_members(self._name)
        self.encoding = font_encoding(faces[0].psname)
        self._faces = odict((f.psname, f) for f in faces)

        fam = {"weights": [], "widths": [], "variants": []}
        has_italic = False
        for f in sorted(faces, key=attrgetter('wgt')):
            for axis in ('weights', 'variants'):
                old, new = fam[axis], getattr(f, axis[:-1])
                if new not in old:
                    fam[axis].append(new)
            # has_italic = has_italic or 'italic' in f.traits
            has_italic = has_italic or f.italic
        self.has_italic = has_italic

        for f in sorted(faces, key=attrgetter('wid')):
            if f.width in fam['widths']: continue
            fam['widths'].append(f.width)

        for axis, vals in fam.items():
            if axis in ('widths', 'variants') and any(vals) and None in vals:
                if axis == 'widths':
                    pass  # for widths, the default should be preserved in sort order
                else:
                    # for variants, default should be first
                    fam[axis] = [None] + filter(None, vals)
            else:
                fam[axis] = filter(None,
                                   vals)  # otherwise wipe out the sole None
            setattr(self, axis, tuple(fam[axis]))
示例#4
0
    def select(self, spec):
        current = spec.get('face', _ctx._typestyle.face)
        if isinstance(current, basestring):
            current = font_face(current)

        axes = ('weight','width','italic','variant')
        opts = {k:v for k,v in spec.items() if k in axes}
        defaults = dict( (k, getattr(current, k)) for k in axes + ('wid', 'wgt'))

        # map the requested weight/width onto what's available in the family
        w_spans = {"wgt":[1,14], "wid":[-15,15]}
        for axis, num_axis in dict(weight='wgt', width='wid').items():
            w_vals = [getattr(f, num_axis) for f in self._faces.values()]
            w_spans[num_axis] = [min(w_vals), max(w_vals)]
            dst = opts if axis in opts else defaults
            wname, wval = self._closest(axis, opts.get(axis, getattr(current,axis)))
            dst.update({axis:wname, num_axis:wval})

        def score(axis, f):
            bonus = 2 if axis in opts else 1
            val = opts[axis] if axis in opts else defaults.get(axis)
            vs = getattr(f,axis)
            if axis in ('wgt','wid'):
                w_min, w_max = w_spans[axis]
                agree = 1 if val==vs else -abs(val-vs) / float(max(w_max-w_min, 1))
            elif axis in ('weight','width'):
                # agree = 1 if (val or None) == (vs or None) else 0
                agree = 0
            else:
                agree = 1 if (val or None) == (vs or None) else -1
            return agree * bonus

        scores = ddict(int)
        for f in self.faces.values():
            # consider = 'italic', 'weight', 'width', 'variant', 'wgt', 'wid'
            # print [score(axis,f) for axis in consider], [getattr(f,axis) for axis in consider]
            scores[f] += sum([score(axis,f) for axis in 'italic', 'weight', 'width', 'variant', 'wgt', 'wid'])

        candidates = [dict(score=s, face=f, ps=f.psname) for f,s in scores.items()]
        candidates.sort(key=itemgetter('ps'))
        candidates.sort(key=itemgetter('score'), reverse=True)
        # for c in candidates[:10]:
        #     print "  %0.2f"%c['score'], c['face']
        return odict( (c['face'],c['score']) for c in candidates)
示例#5
0
    def __init__(self, famname=None, of=None):
        if of:
            famname = font_family(of)
        elif not famname:
            badarg = 'Family: requires either a name or a Font object'%famname
            raise DeviceError(badarg)

        q = famname.strip().lower().replace(' ','')
        matches = [fam for fam in family_names() if q==fam.lower().replace(' ','')]
        if not matches:
            notfound = 'Unknown font family "%s"'%famname
            raise DeviceError(notfound)
        self._name = matches[0]

        faces = family_members(self._name)
        self.encoding = font_encoding(faces[0].psname)
        self._faces = odict( (f.psname,f) for f in faces )

        fam = {"weights":[], "widths":[], "variants":[]}
        has_italic = False
        for f in sorted(faces, key=attrgetter('wgt')):
            for axis in ('weights','variants'):
                old, new = fam[axis], getattr(f,axis[:-1])
                if new not in old:
                    fam[axis].append(new)
            # has_italic = has_italic or 'italic' in f.traits
            has_italic = has_italic or f.italic
        self.has_italic = has_italic

        for f in sorted(faces, key=attrgetter('wid')):
            if f.width in fam['widths']: continue
            fam['widths'].append(f.width)

        for axis, vals in fam.items():
            if axis in ('widths','variants') and any(vals) and None in vals:
                if axis=='widths':
                    pass # for widths, the default should be preserved in sort order
                else:
                    # for variants, default should be first
                    fam[axis] = [None] + filter(None,vals)
            else:
                fam[axis] = filter(None,vals) # otherwise wipe out the sole None
            setattr(self, axis, tuple(fam[axis]))
 def fonts(self):
     return odict( (k,Font(face=v.psname)) for k,v in self._faces.items())
 def faces(self):
     return odict(self._faces)
示例#8
0
 def fonts(self):
     return odict( (k,Font(v)) for k,v in self._faces.items())
示例#9
0
 def fonts(self):
     return odict((k, Font(v)) for k, v in self._faces.items())
示例#10
0
 def faces(self):
     return odict(self._faces)
示例#11
0
 def fonts(self):
     return odict( (k,Font(face=v.psname)) for k,v in self._faces.items())