예제 #1
0
파일: Layer.py 프로젝트: twardoch/TypeRig
    def transform(self, shapes=False):
        if self.aux.doCheck() and len(self.aux.lst_layers.selectedItems()):

            # - Init
            wGlyph = self.aux.glyph

            inpShift = self.edt_shift.text.split(',') if len(
                self.edt_shift.text
            ) and ',' in self.edt_shift.text else '0.0, 0.0'
            inpScale = self.edt_scale.text.split(',') if len(
                self.edt_scale.text
            ) and ',' in self.edt_scale.text else '100, 100'

            wSift_x = float(inpShift[0].strip())
            wSift_y = float(inpShift[1].strip())

            wScale_x = float(inpScale[0].strip()) / 100
            wScale_y = float(inpScale[1].strip()) / 100

            wSlant = radians(float(self.edt_slant.text.strip())) if len(
                self.edt_slant.text) else 0.
            wRotate = -float(self.edt_rotate.text.strip()) if len(
                self.edt_rotate.text) else 0.

            # m11, m12, m13, m21, m22, m23, m31, m32, m33 = 1
            # ! Note: wrong but will do...
            new_transform = QtGui.QTransform().scale(
                wScale_x,
                wScale_y).rotate(wRotate).shear(wSlant,
                                                0).translate(wSift_x, wSift_y)

            for item in self.aux.lst_layers.selectedItems():
                wLayer = wGlyph.layer(item.text())

                if not shapes:
                    # - Transform at origin
                    wBBox = wLayer.boundingBox
                    wCenter = (wBBox.width() / 2 + wBBox.x(),
                               wBBox.height() / 2 + wBBox.y())
                    transform_to_origin = QtGui.QTransform().translate(
                        -wCenter[0], -wCenter[1])
                    transform_from_origin = QtGui.QTransform().translate(
                        *wCenter)

                    # - Transform
                    wLayer.applyTransform(transform_to_origin)
                    wLayer.applyTransform(new_transform)
                    wLayer.applyTransform(transform_from_origin)
                else:
                    wShapes = wGlyph.shapes(item.text())

                    for shape in wShapes:
                        # - Transform at origin and move to new location according to transformation
                        wBBox = shape.boundingBox
                        wCenter = (wBBox.width() / 2 + wBBox.x(),
                                   wBBox.height() / 2 + wBBox.y())
                        newCenter = new_transform.map(QtCore.QPointF(*wCenter))

                        transform_to_origin = QtGui.QTransform().translate(
                            -wCenter[0], -wCenter[1])
                        transform_from_origin = QtGui.QTransform().translate(
                            newCenter.x(), wCenter[1])
                        #transform_from_origin = QtGui.QTransform().translate(*wCenter)

                        # - Transform
                        shape.applyTransform(transform_to_origin)
                        shape.applyTransform(new_transform)
                        shape.applyTransform(transform_from_origin)

            self.aux.glyph.updateObject(
                self.aux.glyph.fl, ' Glyph: %s; Transform Layers: %s' %
                (self.aux.glyph.fl.name, '; '.join([
                    item.text()
                    for item in self.aux.lst_layers.selectedItems()
                ])))
            self.aux.glyph.update()
예제 #2
0
파일: Element.py 프로젝트: twardoch/TypeRig
    def process_insert(self):
        # - Init
        self.active_font = pFont()
        current_glyph = eGlyph()
        getUniGlyph = lambda c: self.active_font.fl.findUnicode(ord(
            c)).name if all(['uni' not in c, '.' not in c, '_' not in c]
                            ) else c

        # - Parse input ------------------------------------------------------------
        for line in self.txt_editor.toPlainText().splitlines():
            # - Init
            process_glyphs = {}
            dst_store, src_store = [], []
            w_layer = syn_passlayer  # Pass all commands - no specific layer selected

            if syn_insert in line and syn_comment not in line:
                init_parse = line.split(syn_insert)

                if len(init_parse) == 2:  # No specific layer given
                    left, rigth = init_parse

                elif len(init_parse) == 3:  # Layer explicitly set
                    w_layer, left, rigth = init_parse
                    w_layer = w_layer.strip()

                else:
                    print 'ERROR:\tInvalid syntax! Skipping Line: %s\n' % line
                    continue

                # - Set basics
                #dst_store = [getUniGlyph(name) if syn_currglyph not in name else current_glyph.name for name in rigth.split()]
                dst_store = [
                    name if syn_currglyph not in name else current_glyph.name
                    for name in rigth.split()
                ]
                src_temp = [
                    item.strip().split(syn_pos) for item in left.split()
                ]
                src_temp = [[item[0], item[1].split(syn_transform)]
                            if len(item) > 1 else item for item in src_temp]

                process_glyphs = {glyph: src_temp for glyph in dst_store}

                # - Process ------------------------------------------------------------
                for glyph_name, insert_command in process_glyphs.iteritems():

                    # - Set working glyph
                    w_glyph = eGlyph(self.active_font.glyph(glyph_name).fl)
                    process_layers = w_glyph._prepareLayers(pLayers)

                    for layer in process_layers:
                        # - Process insertions
                        for insert in insert_command:
                            if len(insert):
                                # - Init
                                # -- Shape retrieval and origin determination
                                if len(insert[0]):
                                    if syn_bboxBL in insert[
                                            0]:  # Shape origin: measured at Shapes BBox Bottom Left
                                        insert_name = insert[0].replace(
                                            syn_bboxBL, '')
                                        w_shape = self.active_font.findShape(
                                            insert_name, layer)
                                        insert_origin = Coord(
                                            w_shape.boundingBox.x(),
                                            w_shape.boundingBox.y())

                                    elif syn_bboxBR in insert[
                                            0]:  # Shape origin: measured at Shapes BBox Bottom Right
                                        insert_name = insert[0].replace(
                                            syn_bboxBR, '')
                                        w_shape = self.active_font.findShape(
                                            insert_name, layer)
                                        insert_origin = Coord(
                                            w_shape.boundingBox.x() +
                                            w_shape.boundingBox.width(),
                                            w_shape.boundingBox.y())

                                    elif syn_bboxTL in insert[
                                            0]:  # Shape origin: measured at Shapes BBox Top Left
                                        insert_name = insert[0].replace(
                                            syn_bboxTL, '')
                                        w_shape = self.active_font.findShape(
                                            insert_name, layer)
                                        insert_origin = Coord(
                                            w_shape.boundingBox.x(),
                                            w_shape.boundingBox.y() +
                                            w_shape.boundingBox.height())

                                    elif syn_bboxTR in insert[
                                            0]:  # Shape origin: measured at Shapes BBox Top Right
                                        insert_name = insert[0].replace(
                                            syn_bboxTR, '')
                                        w_shape = self.active_font.findShape(
                                            insert_name, layer)
                                        insert_origin = Coord(
                                            w_shape.boundingBox.x() +
                                            w_shape.boundingBox.height(),
                                            w_shape.boundingBox.y() +
                                            w_shape.boundingBox.width())

                                    elif syn_label in insert[
                                            0]:  # Shape origin: At source Glyphs Labeled Node
                                        insert_name, node_label = insert[
                                            0].split(syn_label)
                                        for glyph in self.active_font.pGlyphs(
                                        ):
                                            w_shape = glyph.findShape(
                                                insert_name, layer)

                                            if w_shape is not None:
                                                insert_origin = Coord(
                                                    glyph.findNodeCoords(
                                                        node_label, layer))
                                                break

                                    else:  # Shape origin: Not set
                                        insert_name = insert[0]
                                        w_shape = self.active_font.findShape(
                                            insert_name, layer)
                                        insert_origin = Coord(0, 0)
                                else:
                                    print 'ERROR:\tInvalid command! Skipping insertion command: %s\n' % insert
                                    continue

                                # -- In-glyph positioning
                                insert_position = None

                                if len(
                                        insert
                                ) == 1:  # Position: Simplest case no positional tags
                                    insert_coord = Coord((0, 0))
                                else:
                                    if len(insert[1]):
                                        w_bbox = w_glyph.getBounds(layer)

                                        if syn_currnode == insert[1][
                                                0]:  # Position: Destination Glyphs Currently selected node
                                            position = w_glyph.selectedCoords(
                                                layer, applyTransform=True)
                                            insert_position = position[
                                                0] if len(position) else None

                                        elif syn_bboxBL == insert[1][
                                                0]:  # Position: Destination Glyphs BBox Bottom Left
                                            insert_position = (w_bbox.x(),
                                                               w_bbox.y())

                                        elif syn_bboxBR == insert[1][
                                                0]:  # Position: Destination Glyphs BBox Bottom Right
                                            insert_position = (w_bbox.x() +
                                                               w_bbox.width(),
                                                               w_bbox.y())

                                        elif syn_bboxTL == insert[1][
                                                0]:  # Position: Destination Glyphs BBox Top Left
                                            insert_position = (w_bbox.x(),
                                                               w_bbox.y() +
                                                               w_bbox.height())

                                        elif syn_bboxTR == insert[1][
                                                0]:  # Position: Destination Glyphs BBox Top Right
                                            insert_position = (w_bbox.x() +
                                                               w_bbox.height(),
                                                               w_bbox.y() +
                                                               w_bbox.width())

                                        elif syn_label in insert[1][
                                                0]:  # Position: Destination Glyphs Labeled Node
                                            insert_position = w_glyph.findNodeCoords(
                                                insert[1][0].strip(syn_label),
                                                layer)

                                        elif syn_anchor in insert[1][
                                                0]:  # Position: Destination Glyphs Anchor
                                            insert_position = w_glyph.findAnchorCoords(
                                                insert[1][0].strip(syn_anchor),
                                                layer)

                                        elif syn_coordsep in insert[1][
                                                0]:  # Position: Destination Glyphs Coordinates
                                            insert_position = eval(
                                                '(%s)' % insert[1][0])

                                        if len(
                                                insert[1]
                                        ) > 1:  # Positional correction in format (x,y)
                                            insert_correction = Coord(
                                                eval('(%s)' % insert[1][1]))
                                        else:
                                            insert_correction = Coord((0, 0))

                                    if insert_position is None:
                                        print 'ERROR:\tInvalid positional tags! Skipping insertion command: %s\n' % insert
                                        continue

                                    # - Set insertion coordinates
                                    insert_coord = Coord(
                                        insert_position) + insert_correction

                                # - Insert and reposition
                                # !!! A quirky way of adding shapes follows
                                # !!! This is so very wrong - adding the shape twice and removing the first,
                                # !!! forces FL to make a proper clone of the shape!?
                                temp_shape = w_glyph.addShape(
                                    w_shape, layer)  # A dummy that helps ??!
                                new_shape = w_glyph.addShape(w_shape, layer)
                                w_glyph.layer(layer).removeShape(temp_shape)

                                new_shape.assignStyle(
                                    w_shape
                                )  # The only way to copy the 'non-spacing' property for now

                                new_position = insert_coord - insert_origin
                                new_transform = QtGui.QTransform(
                                    1, 0, 0, 0, 1, 0, new_position.x,
                                    new_position.y, 1)
                                new_shape.transform = new_transform

                                w_glyph.update()
                                #print 'New: %s; Insert: %s; Origin: %s' %(new_position, insert_coord, insert_origin)

                    # - Finish
                    w_glyph.updateObject(
                        w_glyph.fl,
                        'Shapes inserted to glyph: %s' % w_glyph.name)

            print 'DONE:\t Glyphs processed: %s' % ' '.join(dst_store)

        print 'Done.'