#!/usr/bin/env python3 ''' Defintion of Common functions and variables used by stroke font extensions Copyright (C) 2019 Shrinivas Kulkarni This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ''' import sys, os, fileinput, re, locale from inkex import errormsg, addNS, NSS from xml.dom.minidom import parse, Document from math import ceil from lxml import etree from inkex import Style, Boolean from inkex.paths import Path, CubicSuperPath, Transform from inkex import bezier # sys path already includes the module folder from stroke_font_manager import CharData, getFontNames, xAscent, \ xDescent, xCapHeight, xXHeight, xSpaceROff, xFontId, xSize class CommonDefs: # inx filed that have the font list to be synchronized inxFilesWithDynFont = ['render_stroke_font_text.inx', 'edit_stroke_font.inx'] vgScaleFact = 2. lineT = .005 idAttribName = 'id' hGuideIDPrefix = 'h_' lvGuideIDPrefix = 'lv_' rvGuideIDPrefix = 'rv_' fontOtherInfo = 'otherInfo' encoding = sys.stdin.encoding if(encoding == 'cp0' or encoding is None): encoding = locale.getpreferredencoding() def getPartsFromCubicSuper(csp): parts = [] for subpath in csp: part = [] prevBezPt = None for i, bezierPt in enumerate(subpath): if(prevBezPt != None): seg = [prevBezPt[1], prevBezPt[2], bezierPt[0], bezierPt[1]] part.append(seg) prevBezPt = bezierPt parts.append(part) return parts def formatStyle(styleStr): return str(Style(styleStr)) def getCubicSuperPath(d = None): if(d == None): return CubicSuperPath([]) return CubicSuperPath(Path(d).to_superpath()) def getCubicLength(csp): return bezier.csplength(csp)[1] def getCubicBoundingBox(csp): bbox = csp.to_path().bounding_box() return bbox.left, bbox.right, bbox.top, bbox.bottom def formatSuperPath(csp): return csp.__str__() def getParsedPath(d): return [[seg.letter, list(seg.args)] for seg in Path(d).to_absolute()] def applyTransform(mat, csp): csp.transform(mat) def getTranslatedPath(d, posX, posY): path = Path(d) path.translate(posX, posY, inplace = True) return path.to_superpath().__str__() def getTransformMat(matAttr): return Transform(matAttr) def getCurrentLayer(effect): return effect.svg.get_current_layer() def getViewCenter(effect): return effect.svg.namedview.center def computePtInNode(vc, layer): return (-layer.transform).apply_to_point(vc) def getSelectedElements(effect): return effect.svg.selected def getEtree(): return etree def getAddFnTypes(effect): addFn = effect.arg_parser.add_argument typeFloat = float typeInt = int typeString = str typeBool = Boolean return addFn, typeFloat, typeInt, typeString, typeBool def runEffect(effect): effect.run() def getDecodedChars(chars): return chars def indentStr(cnt): ostr = '' for i in range(0, cnt): ostr += ' ' return ostr def getXMLItemsStr(sectMarkerLine, sectMarker, fontNames): lSpaces = sectMarkerLine.find(sectMarker) outStr = indentStr(lSpaces) + sectMarker + ' [start] -->\n' for fName in fontNames: outStr += indentStr(lSpaces + 4) + '' + fName + '\n' outStr += indentStr(lSpaces) + sectMarker + ' [end] -->\n' return outStr def syncFontList(extPath): sectMarker = '