Exemplo n.º 1
0
    def __init__(self, fontConfPath):
        self.offsetX = 0
        self.offsetY = 0
        self.offsetXUnit = "abs"
        self.offsetYUnit = "abs"
        self.perCharConfigs = {}
        self.scale = 0.75
        self.scaleUnit = "cellHeight"
        self.autoShrink = False

        if not os.path.exists(fontConfPath):
            return

        fontConf = ScriptReader(fontConfPath)

        while fontConf.more():
            if fontConf.getCmd() == "offset":
                [
                    self.offsetX, self.offsetY, self.offsetXUnit,
                    self.offsetYUnit
                ] = fontConf.getArgs([float, float, str, str])

            elif fontConf.getCmd() == "scale":
                [self.scale, self.scaleUnit] = fontConf.getArgs([float, str])

            elif fontConf.getCmd() == "enableAutoShrink":
                self.autoShrink = True

            elif fontConf.getCmd() == "charSettings":
                args = fontConf.getArgs([unicode, float, float, float, float])
                self.perCharConfigs[args[0]] = CharConfig(args[1:])
Exemplo n.º 2
0
    def __init__(self, fontConfPath):
        self.offsetX = 0
        self.offsetY = 0
        self.offsetXUnit = "abs"
        self.offsetYUnit = "abs"
        self.perCharConfigs = {}
        self.scale = 0.75
        self.scaleUnit = "cellHeight"
        self.autoShrink = False

        if not os.path.exists(fontConfPath):
            return

        fontConf = ScriptReader(fontConfPath)

        while fontConf.more():
            if fontConf.getCmd() == "offset":
                [self.offsetX, self.offsetY, self.offsetXUnit, self.offsetYUnit] = fontConf.getArgs([float, float, str, str])

            elif fontConf.getCmd() == "scale":
                [self.scale, self.scaleUnit] = fontConf.getArgs([float, str])

            elif fontConf.getCmd() == "enableAutoShrink":
                self.autoShrink = True
            
            elif fontConf.getCmd() == "charSettings":
                args = fontConf.getArgs([unicode, float, float, float, float])
                self.perCharConfigs[args[0]] = CharConfig(args[1:])
Exemplo n.º 3
0
    def runScript(self, scriptName, defaultFont):
        scriptPath = os.path.join(os.path.dirname(defaultFont), scriptName)
        if not os.path.exists(scriptPath):
            # Then try the default scripts directory.
            scriptPath = os.path.join(SCRIPTS_DIR, scriptName)
            if not os.path.exists(scriptPath):
                print("      WARNING: Script \"" + scriptName +
                      "\" not found.")
                return False

        script = ScriptReader(scriptPath)

        while script.more():
            if script.getCmd() == "runScript":
                if not self.runScript(script.getRawArg(), defaultFont):
                    return False

            elif script.getCmd() == "noBuild":
                return False

            elif script.getCmd() == "setOutputName":
                self.outputName = script.getRawArg()

            elif script.getCmd() == "setOutputNameSuffix":
                self.outputName = self.defaultOutputName + script.getRawArg()

            elif script.getCmd() == "init":
                self.init(script.getArgs([int, int, float, float]))

            elif script.getCmd() == "setCellSize":
                self.gridConfig.cellSize = script.getArgs([int, int])

            elif script.getCmd() == "setCellOffset":
                self.gridConfig.begin = script.getArgs([int, int])

            elif script.getCmd() == "setCellSpacing":
                self.gridConfig.spacing = script.getArgs([int, int])

            elif script.getCmd() == "setCellCount":
                self.gridConfig.cellCount = script.getArgs([int, int])

            elif script.getCmd() == "setBaselineOffset":
                args = script.getArgs([float, str])
                self.gridConfig.baselineOffset = args[
                    0] * self.convertScaleUnit(args[1])

            elif script.getCmd() == "drawLineBetweenCells":
                self.lineBetweenCells(*[
                    int(arg) for arg in script.getArgs(
                        [int, int, int, int, int, int, int, int, int])
                ])

            elif script.getCmd() == "drawRectBetweenCells":
                self.rectangleBetweenCells(*[
                    int(arg) for arg in script.getArgs(
                        [int, int, int, int, int, int, int, int, int])
                ])

            elif script.getCmd() == "drawFilledRectBetweenCells":
                self.rectangleBetweenCells(*[
                    int(arg) for arg in script.getArgs(
                        [int, int, int, int, int, int, int, int, int])
                ],
                                           filled=True)

            elif script.getCmd() == "drawChars":
                for char in script.getRawArg():
                    self.drawChar(char)

            elif script.getCmd() == "loadFont":
                fontName = script.getRawArg()
                fontPath = ""
                if fontName == "":
                    fontPath = defaultFont
                else:
                    fontFilePaths = glob.glob(os.path.join(FONTS_DIR, fontName, "*.[o,t]tf")) \
                                  + glob.glob(os.path.join(FONTS_DIR, fontName, "*.[O,T]TF"))
                    if len(fontFilePaths) < 1:
                        raise Exception("No font file found for font \"" +
                                        fontName + "\".")
                    fontPath = fontFilePaths[0]
                self.loadFont(fontPath)

            elif script.getCmd() == "jumpToCell":
                self.jumpToCell(*script.getArgs([int, int]))

            elif script.getCmd() == "drawDebugGrid":
                self.debugGrid = True

                self.cairoContext.save()
                self.cairoContext.set_source_rgb(0, 0, 0)
                self.cairoContext.set_matrix(
                    cairo.Matrix(xx=self.gridConfig.imageSize[0],
                                 yy=self.gridConfig.imageSize[1]))
                self.cairoContext.rectangle(0, 0, 1, 1)
                self.cairoContext.fill()
                self.cairoContext.restore()

                self.cairoContext.save()
                self.cairoContext.set_source_rgb(0.5, 0.5, 0.5)
                thickness = 2
                for y in range(0, self.gridConfig.cellCount[1]):
                    for x in range(0, self.gridConfig.cellCount[0]):
                        dx = self.gridConfig.cellSize[0] / 2 + thickness / 2
                        dy = self.gridConfig.cellSize[1] / 2 + thickness / 2
                        self.rectangleBetweenCells(thickness, x, y, x, y, -dx,
                                                   -dy, dx, dy)
                self.cairoContext.restore()

            else:
                print("      ERROR: Unknown script command \"" +
                      script.getCmd() + "\".")
                return False

        return True
Exemplo n.º 4
0
    def runScript(self, scriptName, defaultFont):
        scriptPath = os.path.join(os.path.dirname(defaultFont), scriptName)
        if not os.path.exists(scriptPath):
            # Then try the default scripts directory.
            scriptPath = os.path.join(SCRIPTS_DIR, scriptName)
            if not os.path.exists(scriptPath):
                print("      WARNING: Script \"" + scriptName + "\" not found.")
                return False
        
        script = ScriptReader(scriptPath)
        
        while script.more():
            if script.getCmd() == "runScript":
                if not self.runScript(script.getRawArg(), defaultFont):
                    return False
            
            elif script.getCmd() == "noBuild":
                return False
            
            elif script.getCmd() == "setOutputName":
                self.outputName = script.getRawArg()
            
            elif script.getCmd() == "setOutputNameSuffix":
                self.outputName = self.defaultOutputName + script.getRawArg()
            
            elif script.getCmd() == "init":
                self.init(script.getArgs([int, int, float, float]))
            
            elif script.getCmd() == "setCellSize":
                self.gridConfig.cellSize = script.getArgs([int, int])
            
            elif script.getCmd() == "setCellOffset":
                self.gridConfig.begin = script.getArgs([int, int])
            
            elif script.getCmd() == "setCellSpacing":
                self.gridConfig.spacing = script.getArgs([int, int])
            
            elif script.getCmd() == "setCellCount":
                self.gridConfig.cellCount = script.getArgs([int, int])
            
            elif script.getCmd() == "setBaselineOffset":
                args = script.getArgs([float, str])
                self.gridConfig.baselineOffset = args[0] * self.convertScaleUnit(args[1])

            elif script.getCmd() == "drawLineBetweenCells":
                self.lineBetweenCells(*[int(arg) for arg in script.getArgs([int, int, int, int, int, int, int, int, int])])
            
            elif script.getCmd() == "drawRectBetweenCells":
                self.rectangleBetweenCells(*[int(arg) for arg in script.getArgs([int, int, int, int, int, int, int, int, int])])
            
            elif script.getCmd() == "drawFilledRectBetweenCells":
                self.rectangleBetweenCells(*[int(arg) for arg in script.getArgs([int, int, int, int, int, int, int, int, int])], filled = True)
            
            elif script.getCmd() == "drawChars":
                for char in script.getRawArg():
                    self.drawChar(char)
            
            elif script.getCmd() == "loadFont":
                fontName = script.getRawArg()
                fontPath = ""
                if fontName == "":
                    fontPath = defaultFont
                else:
                    fontFilePaths = glob.glob(os.path.join(FONTS_DIR, fontName, "*.[o,t]tf")) \
                                  + glob.glob(os.path.join(FONTS_DIR, fontName, "*.[O,T]TF"))
                    if len(fontFilePaths) < 1:
                        raise Exception("No font file found for font \"" + fontName + "\".")
                    fontPath = fontFilePaths[0]
                self.loadFont(fontPath)
            
            elif script.getCmd() == "jumpToCell":
                self.jumpToCell(*script.getArgs([int, int]))
            
            elif script.getCmd() == "drawDebugGrid":
                self.debugGrid = True
                
                self.cairoContext.save()
                self.cairoContext.set_source_rgb(0,0,0)
                self.cairoContext.set_matrix(cairo.Matrix(
                    xx = self.gridConfig.imageSize[0],
                    yy = self.gridConfig.imageSize[1]
                ))
                self.cairoContext.rectangle(0,0,1,1)
                self.cairoContext.fill()
                self.cairoContext.restore()

                self.cairoContext.save()
                self.cairoContext.set_source_rgb(0.5,0.5,0.5)
                thickness = 2
                for y in range(0, self.gridConfig.cellCount[1]):
                    for x in range(0, self.gridConfig.cellCount[0]):
                        dx = self.gridConfig.cellSize[0]/2 + thickness/2
                        dy = self.gridConfig.cellSize[1]/2 + thickness/2
                        self.rectangleBetweenCells(thickness, x, y, x, y, -dx, -dy, dx, dy)
                self.cairoContext.restore()
            
            else:
                print("      ERROR: Unknown script command \"" + script.getCmd() + "\".")
                return False
            
        return True