コード例 #1
0
    def __missing__(self, U):
        F = Counter(U)
        # iterate through stack
        projection = Textstyle.BASE.copy()
        
        for TS in (TS for TS, tags in self._P if tags <= F):
            projection.update(TS)

        # set up fonts
        upem, hb_face, projection['__hb_font__'], projection['font'] = get_ot_font(projection['path'])
        projection['__factor__'] = projection['fontsize']/upem
        projection['__gridfont__'] = Grid_font(projection['__hb_font__'], upem)
        
        self[U] = projection
        return projection
コード例 #2
0
ファイル: styles.py プロジェクト: kelvin13/Knockout
    def project_t(self, BLOCK, F, CHAR_STYLES):
        if BLOCK.implicit_ is None:
            P = BLOCK["class"]
        else:
            P = BLOCK["class"] + BLOCK.implicit_

        H = 22 * hash(frozenset(P.items())) + hash(
            frozenset(F.items())
        )  # we must give paragraph a different factor if a style has the same name as a fontstyle
        if CHAR_STYLES:
            H += 13 * sum(fp.stylehash for fp in CHAR_STYLES)

        try:
            return self.text_projections[H]
        except KeyError:
            # iterate through stack
            projection = _Layer(self._text_default)
            iter_blockstyles = (b.content for b in self.content if b.content is not None and b["class"] <= P)
            for TS in chain(
                (
                    c["textstyle"]
                    for c in chain.from_iterable(iter_blockstyles)
                    if c["textstyle"] is not None and c["class"] <= F
                ),
                CHAR_STYLES,
            ):
                projection.overlay(TS)

            # text font
            try:
                upem, hb_face, projection["__hb_font__"], projection["font"] = get_ot_font(projection["path"])
            except FileNotFoundError:
                path = Textstyle.BASE["path"]
                projection["color"] = (1, 0.15, 0.2, 1)
                upem, hb_face, projection["__hb_font__"], projection["font"] = get_ot_font(path)

            projection["__factor__"] = factor = projection["fontsize"] / upem
            hmetrics = hb.font_get_h_extents(projection["__hb_font__"])[1]
            projection["__fontmetrics__"] = (
                hmetrics.ascender * factor + projection["shift"],
                hmetrics.descender * factor + projection["shift"],
            )
            projection["__spacemetrics__"] = get_ot_space_metrics(
                projection["__hb_font__"], projection["fontsize"], factor
            )

            # emoji font
            try:
                e_upem, projection["__hb_emoji__"], projection["__emoji__"] = get_emoji_font(projection["path_emoji"])
            except FileNotFoundError:
                e_upem, projection["__hb_emoji__"], projection["__emoji__"] = get_emoji_font(
                    Textstyle.BASE["path_emoji"]
                )
            projection["__factor_emoji__"] = projection["fontsize"] / e_upem
            ###

            # opentype features
            projection["__ot_features__"] = [
                feature_map[feature][mode == 1] if mode < 2 else feature_map[feature][2][mode]
                for mode, feature in ((projection[feature], feature) for feature in common_features)
                if mode
            ]

            projection["hash"] = H

            self.text_projections[H] = projection
            return projection
コード例 #3
0
    def project_t(self, BLOCK, F, CHAR_STYLES):
        if BLOCK.implicit_ is None:
            P = BLOCK['class']
        else:
            P = BLOCK['class'] + BLOCK.implicit_

        H = 22 * hash(frozenset(P.items())) + hash(
            frozenset(F.items())
        )  # we must give paragraph a different factor if a style has the same name as a fontstyle
        if CHAR_STYLES:
            H += 13 * sum(fp.stylehash for fp in CHAR_STYLES)

        try:
            return self.text_projections[H]
        except KeyError:
            # iterate through stack
            projection = _Layer(self._text_default)
            iter_blockstyles = (b.content for b in self.content
                                if b.content is not None and b['class'] <= P)
            for TS in chain(
                (c['textstyle'] for c in chain.from_iterable(iter_blockstyles)
                 if c['textstyle'] is not None and c['class'] <= F),
                    CHAR_STYLES):
                projection.overlay(TS)

            # text font
            try:
                upem, hb_face, projection['__hb_font__'], projection[
                    'font'] = get_ot_font(projection['path'])
            except FileNotFoundError:
                path = Textstyle.BASE['path']
                projection['color'] = (1, 0.15, 0.2, 1)
                upem, hb_face, projection['__hb_font__'], projection[
                    'font'] = get_ot_font(path)

            projection['__factor__'] = factor = projection['fontsize'] / upem
            hmetrics = hb.font_get_h_extents(projection['__hb_font__'])[1]
            projection[
                '__fontmetrics__'] = hmetrics.ascender * factor + projection[
                    'shift'], hmetrics.descender * factor + projection['shift']
            projection['__spacemetrics__'] = get_ot_space_metrics(
                projection['__hb_font__'], projection['fontsize'], factor)

            # emoji font
            try:
                e_upem, projection['__hb_emoji__'], projection[
                    '__emoji__'] = get_emoji_font(projection['path_emoji'])
            except FileNotFoundError:
                e_upem, projection['__hb_emoji__'], projection[
                    '__emoji__'] = get_emoji_font(Textstyle.BASE['path_emoji'])
            projection['__factor_emoji__'] = projection['fontsize'] / e_upem
            ###

            # opentype features
            projection['__ot_features__'] = [
                feature_map[feature][mode == 1]
                if mode < 2 else feature_map[feature][2][mode]
                for mode, feature in ((projection[feature], feature)
                                      for feature in common_features) if mode
            ]

            projection['hash'] = H

            self.text_projections[H] = projection
            return projection