Пример #1
0
def drawText(self):
    fontPath = os.path.join(Utils.prgpath, 'fonts')
    fontFileName = join(fontPath, self.pfont.fontCombo.get())
    if fontFileName == "":
        self.setStatus(_("Text abort: please select a font file"))
        return
    depth = -float(self.engrave.depth.get())
    retractZ = float(self.engrave.retractZ.get())
    clearanceZ = float(self.engrave.clearanceZ.get())
    CNC.vars["safe"] = clearanceZ

    #clear out any old tag information
    self.initializeGcodeforText(self.localtags.origin['X'],
                                self.localtags.origin['Y'],
                                self.localtags.origin['Z'])

    try:
        import ttf
        font = ttf.TruetypeInfo(fontFileName)
    except:
        self.setStatus(
            _("Text abort: That embarrassing, I can't read this font file!"))
        return

    for plate in self.itemstoEngrave:

        t = self.localtags.tag[plate.numTag]
        ##Get inputs
        tagHeight = abs(t['y1'] - t['y0'])
        tagWidth = abs(t['x1'] - t['x0'])
        textToWrite = plate.text.get()

        #charsWidth    = self["CharsWidth"]
        #charsWidth     = int(self.font.charspacing.get())

        #Check parameters!!!
        if textToWrite == "":
            textToWrite = "Nel mezzo del cammin di nostra vita..."
            continue

        #Init blocks
        blocks = []
        n = textToWrite
        if not n or n == "default": n = "Text"
        block = Block(n)
        if (u'\n' in textToWrite):
            block.append("(Text:)")
            for line in textToWrite.splitlines():
                block.append("(%s)" % line)
        else:
            block.append("(Text: %s)" % textToWrite)

        cmap = font.get_character_map()
        kern = None
        try:
            kern = font.get_glyph_kernings()
        except:
            pass
        adv = font.get_glyph_advances()

        #xOffset = 0
        #yOffset = 0
        dx = float(t['x0'])
        dy = float(t['y0'])
        #If there are only a few characters,
        #the fontsize being the height of the tag works
        #however, longer tags need to be resized
        glyphLength = 0
        for c in textToWrite:
            if c == u'\n':
                xOffset = 0.0
                yOffset -= 1  #offset for new line
                continue
            if c in cmap:
                glyphIndx = cmap[c]
            glyphLength += adv[glyphIndx]

        fontSize = 0
        if (glyphLength * tagHeight) > tagWidth:
            fontSize = tagWidth / glyphLength
        else:
            fontSize = tagHeight
        #offset the height difference
        dy = dy + (tagHeight - fontSize) / 2
        #offset to center the text
        dx = dx + (tagWidth - glyphLength * fontSize) / 2

        glyphIndxLast = cmap[' ']
        xOffset = dx / fontSize
        yOffset = dy / fontSize
        #create the characters
        for c in textToWrite:
            #New line
            if c == u'\n':
                xOffset = 0.0
                yOffset -= 1  #offset for new line
                continue

            if c in cmap:
                glyphIndx = cmap[c]

                if (kern and (glyphIndx, glyphIndxLast) in kern):
                    k = kern[(glyphIndx,
                              glyphIndxLast)]  #FIXME: use kern for offset??

#Get glyph contours as line segmentes and draw them
                gc = font.get_glyph_contours(glyphIndx)
                if (not gc):
                    gc = font.get_glyph_contours(
                        0)  #standard glyph for missing glyphs (complex glyph)
                if (
                        gc and not c == ' '
                ):  #FIXME: for some reason space is not mapped correctly!!!
                    self.writeGlyphContour(block, font, gc, fontSize, depth,
                                           xOffset, yOffset, retractZ)
                if glyphIndx < len(adv):
                    xOffset += adv[glyphIndx]
                else:
                    xOffset += 1
                glyphIndxLast = glyphIndx
        #Gcode Zsafe
        block.append(CNC.zexit(clearanceZ))

        #self.gcode.moveLines(block.path, dx, dy)
        blocks.append(block)
        #active = self.activeBlock()
        #if active==0: active=1
        if (len(self.gcode.blocks) == 0):
            index = 1
        else:
            index = len(self.gcode.blocks) - 1
        self.gcode.insBlocks(index, blocks, "Text")
        self.refresh()

    #Remember to close Font
    font.close()
    self.notebook.select(1)
    self.canvas.fit2Screen()
    self.refresh()
Пример #2
0
    def execute(self, app):

        #Get inputs
        fontSize = self["FontSize"]
        depth = self["Depth"]
        textToWrite = self["Text"]
        fontFileName = self["FontFile"]
        imageFileName = self["ImageToAscii"]
        charsWidth = self["CharsWidth"]

        #Check parameters!!!
        if fontSize <= 0:
            app.setStatus(_("Text abort: please input a Font size > 0"))
            return
        if fontFileName == "":
            app.setStatus(_("Text abort: please select a font file"))
            return
        if imageFileName != "":
            try:
                textToWrite = self.asciiArt(imageFileName, charsWidth)
            except:
                pass
        if textToWrite == "":
            textToWrite = "Nel mezzo del cammin di nostra vita..."
            return

        #Init blocks
        blocks = []
        n = self["name"]
        if not n or n == "default": n = "Text"
        block = Block(n)
        if (u'\n' in textToWrite):
            block.append("(Text:)")
            for line in textToWrite.splitlines():
                block.append("(%s)" % line)
        else:
            block.append("(Text: %s)" % textToWrite)

        try:
            import ttf
            font = ttf.TruetypeInfo(fontFileName)
        except:
            app.setStatus(
                _("Text abort: That embarrassing, I can't read this font file!"
                  ))
            return
        cmap = font.get_character_map()

        kern = None
        try:
            kern = font.get_glyph_kernings()
        except:
            pass
        adv = font.get_glyph_advances()

        xOffset = 0
        yOffset = 0
        glyphIndxLast = cmap[' ']
        for c in textToWrite:
            #New line
            if c == u'\n':
                xOffset = 0.0
                yOffset -= 1  #offset for new line
                continue

            if c in cmap:
                glyphIndx = cmap[c]

                if (kern and (glyphIndx, glyphIndxLast) in kern):
                    k = kern[(glyphIndx,
                              glyphIndxLast)]  #FIXME: use kern for offset??

                #Get glyph contours as line segmentes and draw them
                gc = font.get_glyph_contours(glyphIndx)
                if (not gc):
                    gc = font.get_glyph_contours(
                        0)  #standard glyph for missing glyphs (complex glyph)
                if (
                        gc and not c == ' '
                ):  #FIXME: for some reason space is not mapped correctly!!!
                    self.writeGlyphContour(block, font, gc, fontSize, depth,
                                           xOffset, yOffset)

                if glyphIndx < len(adv):
                    xOffset += adv[glyphIndx]
                else:
                    xOffset += 1
                glyphIndxLast = glyphIndx

        #Remeber to close Font
        font.close()

        #Gcode Zsafe
        block.append(CNC.zsafe())

        blocks.append(block)
        active = app.activeBlock()
        app.gcode.insBlocks(active, blocks, "Text")
        app.refresh()
        app.setStatus("Generated Text")
Пример #3
0
class Tool(Plugin):
	__doc__ =  _("Create text using a ttf font")
	def __init__(self, master):
		Plugin.__init__(self, master, "Text")
		self.icon  = "text"
		self.group = "Generator"

		self.variables = [("name",      "db" ,    "", _("Name")),
				("Text",        "text", "Write this!", _("Text to generate")),
				("Depth",       "mm",    0.0, _("Working Depth")),
				("FontSize",    "mm",   10.0, _("Font size")),
				("FontFile",    "file",   "", _("Font file")),
                                ("Closed",      "bool", True, _("Close Contours")),
				("ImageToAscii","file",   "", _("Image to Ascii")),
				("CharsWidth",  "int",    80, _("Image chars width"))]
		self.buttons.append("exe")

	# ----------------------------------------------------------------------
	def execute(self, app):

		#Get inputs
		fontSize      = self.fromMm("FontSize")
		depth         = self.fromMm("Depth")
		textToWrite   = self["Text"]
		fontFileName  = self["FontFile"]
        closed        = self["Closed"]
		imageFileName = self["ImageToAscii"]
		charsWidth    = self["CharsWidth"]

		#Check parameters!!!
		if fontSize <=0:
			app.setStatus(_("Text abort: please input a Font size > 0"))
			return
		if fontFileName == "":
			app.setStatus(_("Text abort: please select a font file"))
			return
		if imageFileName != "":
			try:
				textToWrite = self.asciiArt(imageFileName,charsWidth)
			except:
				pass
		if textToWrite == "":
			textToWrite = "Nel mezzo del cammin di nostra vita..."
			return

		#Init blocks
		blocks = []
		n = self["name"]
		if not n or n == "default": n = "Text"
		block = Block(n)
		if(u'\n' in  textToWrite):
			block.append("(Text:)")
			for line in textToWrite.splitlines():
				block.append("(%s)" % line)
		else:
			block.append("(Text: %s)" % textToWrite)

		try:
			import ttf
			font = ttf.TruetypeInfo(fontFileName)
		except:
			app.setStatus(_("Text abort: That embarrassing, I can't read this font file!"))
			return
		cmap = font.get_character_map()

		kern = None
		try:
			kern = font.get_glyph_kernings()
		except:
			pass
		adv = font.get_glyph_advances()

		xOffset = 0
		yOffset = 0
		glyphIndxLast = cmap[' ']
		for c in textToWrite:
			#New line
			if c == u'\n':
				xOffset = 0.0
				yOffset -= 1#offset for new line
				continue

			if c in cmap:
				glyphIndx = cmap[c]

				if (kern and (glyphIndx,glyphIndxLast) in kern):
					k = kern[(glyphIndx,glyphIndxLast)] #FIXME: use kern for offset??

				#Get glyph contours as line segmentes and draw them
				gc = font.get_glyph_contours(glyphIndx,closed)
				if(not gc):
					gc = font.get_glyph_contours(0,closed)#standard glyph for missing glyphs (complex glyph)
				if(gc and not c==' '): #FIXME: for some reason space is not mapped correctly!!!
					self.writeGlyphContour(block, font, gc, fontSize, depth, xOffset, yOffset)

				if glyphIndx < len(adv):
					xOffset += adv[glyphIndx]
				else:
					xOffset += 1
				glyphIndxLast = glyphIndx

		#Remeber to close Font
		font.close()

		#Gcode Zsafe
		block.append(CNC.zsafe())

		blocks.append(block)
		active = app.activeBlock()
		if active==0: active=1
		app.gcode.insBlocks(active, blocks, "Text")
		app.refresh()
		app.setStatus("Generated Text")