예제 #1
0
    def preprocess(filename):
        # directory? (output)
        if os.path.isdir(filename):
            return ('dir', filename)

        # DVI file? (input)
        dir, fname = os.path.split(filename)
        if dir == '': dir = '.'

        def dvipred(p, f):
            return f==fname or \
                   f==fname + '.dvi' or \
                   f==fname + '.DVI' or \
                   f==fname + '.Dvi'

        dvi = findfile.find_file(dir, dvipred, enterdir=lambda p, l: False)
        if dvi is not None:
            return ('dvi', dvi)

        # SVG file? (output)
        if filename.lower().endswith('.svg'):
            return ('svg', filename[:-4])

        # none, skipping
        log.info("File '%s' not found, skipping" % filename)
        return None
예제 #2
0
	def preprocess(filename):
		# directory? (output)
		if os.path.isdir(filename):
			return ('dir', filename)

		# DVI file? (input)
		dir, fname = os.path.split(filename)
		if dir == '': dir = '.'
		def dvipred(p, f):
			return f==fname or \
			       f==fname + '.dvi' or \
			       f==fname + '.DVI' or \
			       f==fname + '.Dvi'
		dvi = findfile.find_file(dir, dvipred, enterdir=lambda p, l: False)
		if dvi is not None:
			return ('dvi', dvi)

		# SVG file? (output)
		if filename.lower().endswith('.svg'):
			return ('svg', filename[:-4])

		# none, skipping
		log.info("File '%s' not found, skipping" % filename)
		return None
예제 #3
0
def make_cache_file(fontname):

    font = Font()
    font.name = fontname

    #
    # 1. Locate/create SVG font
    #

    svgname = fontname.lower() + '.svg'

    def svgpred(path, filename):
        return filename.lower() == svgname

    filename = findfile.find_file(setup.svg_font_path, svgpred)
    if not filename:
        type1file = findfile.locate(fontname + '.pfb') or \
                    findfile.locate(fontname + '.pfa')

        if not type1file:
            # there is no pfa/pfb named fontname,
            # try to find pfa/pfb with some prefix as fontname

            # XXX: rewrite
            import string
            prefix = fontname.translate(
                string.maketrans("0123456789", " " * 10)).split()[0]
            if prefix:

                def pred(path, filename):
                    f = filename.lower()
                    return f.startswith(prefix) and\
                           (f.endswith(".pfa") or f.endswith(".pfb"))

                type1file = findfile.find_file(setup.tex_paths, pred)

        if type1file:
            log.info("Found Type1 font: %s" % type1file)
            if setup.options.use_fontforge and fontforge_available:
                log.info("... trying FontForge")
                if fontforge_convert(type1file):
                    filename = findfile.find_file(setup.svg_font_path, svgpred)
                    log.info("... ... conversion successful!")
                else:
                    log.info("... ... conversion failed")

            if not filename and setup.options.use_fnt2meta and fnt2meta_available:
                log.info("... trying fnt2meta")
                meta = load_metadata(type1file)
                if meta:
                    font = parse_metadata(meta)
                    font.designsize = get_designsize(fontname)
                    log.debug("Font '%s' designed at %spt" %
                              (fontname, str(font.designsize)))
                    log.info("... conversion successful")

                    # pickle
                    f = open(setup.cache_path + fontname + '.cache', 'wb')
                    cPickle.dump(font, f, protocol=cPickle.HIGHEST_PROTOCOL)
                    f.close()
                    return
                else:
                    log.info("... ... conversion failed")
        #fi

    if not filename and not type1file:
        mffile = findfile.locate(fontname + '.mf')
        if mffile:
            log.info("METAFONT source found, trying mftrace...")
            if mftrace_convert(mffile):
                filename = findfile.find_file(setup.svg_font_path, svgpred)
                if filename:
                    log.info("... ... conversion successful")
                else:
                    log.info("... ... conversion failed")
            else:
                log.info("... ... conversion failed")

    if filename:
        log.debug("Using SVG font '%s' as '%s'", filename, font.name)
    else:
        if type1file:
            raise FontError(
                "%s: suitable vector font found, but conversion failed (are FontForge or/and fnt2meta installed?)"
                % font.name)
        else:
            raise FontError("%s: can't find SVG font" % font.name)

    # get designsize
    font.designsize = get_designsize(fontname)
    log.debug("Font '%s' designed at %spt" % (fontname, str(font.designsize)))

    #
    # 2. Process SVG file
    #

    # a. load file
    from xml.dom.minidom import parse
    data = parse(filename)

    # b. get font element
    try:
        fontnode = data.getElementsByTagName('font')[0]
        # get default horizontal advance (if any)
        if fontnode.hasAttribute('horiz-adv-x'):
            default_hadvx = float(fontnode.getAttribute('horiz-adv-x'))
        else:
            default_hadvx = None
    except IndexError:
        raise FontError(
            "There should be at least one <font> element in SVG file")

    # d. get font face name
    if fontnode.getAttribute('font-family'):
        font.fontfamily = fontnode.getAttribute('font-family')
    else:
        font.fontfamily = fontname

    try:
        fontface = data.getElementsByTagName('font-face')[0]
        if fontface.getAttribute('bbox') and fontface.getAttribute(
                'units-per-em'):
            xmin, ymin, xmax, ymax = map(
                float,
                fontface.getAttribute('bbox').replace(",", " ").split())
            upem = float(fontface.getAttribute('units-per-em'))


#			raise str((upem, (ymax-ymin)))
    except IndexError:
        pass

    # e. load fonts
    font.glyphs_dict = {}
    for node in fontnode.getElementsByTagName('glyph'):

        # 1. read path info
        glyph = Glyph()
        if node.hasAttribute('d'):  # is defined as <glyph> attribute
            glyph.path = node.getAttribute('d')
        else:
            # XXX: glyph is defined with path child element and I assume
            #      there is just one element
            path_elements = node.getElementsByTagName('path')
            if len(path_elements) == 0:  # no path elements at all
                pass
            elif len(path_elements) == 1:  # one path element
                glyph.path = path_elements[0].getAttribute('d')
            else:  # more paths...
                pass  # XXX: join them?

        # 2. get character name
        glyph.name = node.getAttribute('glyph-name')
        if glyph.name == '':
            log.error("There is a glyph without name, skipping it")
            continue

        # 2a. transcode
        if glyph.name not in name_lookup:
            try:
                glyph.name = transcode[glyph.name]
            except KeyError:
                log.error("Don't know what is '%s', skipping it", glyph.name)
                continue

        # 3. get horiz-adv-x
        if node.hasAttribute('horiz-adv-x'):
            glyph.hadv = float(node.getAttribute('horiz-adv-x'))
        elif default_hadvx != None:
            glyph.hadv = default_hadvx
        else:
            # XXX: calculate glyphs extends?
            raise FontError("Can't determine width of character '%s'",
                            glyph.name)

        if glyph.name in font.glyphs_dict:
            log.error("Character '%s' already defined, skipping", glyph.name)
        else:
            font.glyphs_dict[glyph.name] = glyph
    #rof

    # f. write the cache file
    cPickle.dump(font,
                 open(setup.cache_path + fontname + '.cache', 'wb'),
                 protocol=cPickle.HIGHEST_PROTOCOL)
예제 #4
0
def make_cache_file(fontname):

	font = Font()
	font.name = fontname

	#
	# 1. Locate/create SVG font
	#

	svgname = fontname.lower() + '.svg'
	def svgpred(path, filename):
		return filename.lower() == svgname

	filename = findfile.find_file(setup.svg_font_path, svgpred)
	if not filename:
		type1file = findfile.locate(fontname + '.pfb') or \
		            findfile.locate(fontname + '.pfa')

		if not type1file:
			# there is no pfa/pfb named fontname,
			# try to find pfa/pfb with some prefix as fontname
			
			# XXX: rewrite
			import string
			prefix = fontname.translate(string.maketrans("0123456789", " "*10)).split()[0]
			if prefix:
				def pred(path, filename):
					f = filename.lower()
					return f.startswith(prefix) and\
					       (f.endswith(".pfa") or f.endswith(".pfb"))
				type1file = findfile.find_file(setup.tex_paths, pred)

		if type1file:
			log.info("Found Type1 font: %s" % type1file)
			if setup.options.use_fontforge and fontforge_available:
				log.info("... trying FontForge")
				if fontforge_convert(type1file):
					filename = findfile.find_file(setup.svg_font_path, svgpred)
					log.info("... ... conversion successful!")
				else:
					log.info("... ... conversion failed")

			if not filename and setup.options.use_fnt2meta and fnt2meta_available:
				log.info("... trying fnt2meta")
				meta = load_metadata(type1file)
				if meta:
					font = parse_metadata(meta)
					font.designsize = get_designsize(fontname)
					log.debug("Font '%s' designed at %spt" % (fontname, str(font.designsize)))
					log.info("... conversion successful")
				
					# pickle
					f = open(setup.cache_path + fontname + '.cache', 'wb')
					cPickle.dump(font, f, protocol=cPickle.HIGHEST_PROTOCOL)
					f.close()
					return
				else:
					log.info("... ... conversion failed")
		#fi
	
	if not filename and not type1file:
		mffile = findfile.locate(fontname + '.mf')
		if mffile:
			log.info("METAFONT source found, trying mftrace...")
			if mftrace_convert(mffile):
				filename = findfile.find_file(setup.svg_font_path, svgpred)
				if filename:
					log.info("... ... conversion successful")
				else:
					log.info("... ... conversion failed")
			else:
				log.info("... ... conversion failed")
				
		

	if filename:
		log.debug("Using SVG font '%s' as '%s'", filename, font.name)
	else:
		if type1file:
			raise FontError("%s: suitable vector font found, but conversion failed (are FontForge or/and fnt2meta installed?)" % font.name)
		else:
			raise FontError("%s: can't find SVG font" % font.name)

	# get designsize
	font.designsize = get_designsize(fontname)
	log.debug("Font '%s' designed at %spt" % (fontname, str(font.designsize)))

	#
	# 2. Process SVG file
	#

	# a. load file
	from xml.dom.minidom import parse
	data = parse(filename)

	# b. get font element
	try:
		fontnode = data.getElementsByTagName('font')[0]
		# get default horizontal advance (if any)
		if fontnode.hasAttribute('horiz-adv-x'):
			default_hadvx = float(fontnode.getAttribute('horiz-adv-x'))
		else:
			default_hadvx = None
	except IndexError:
		raise FontError("There should be at least one <font> element in SVG file")

	# d. get font face name
	if fontnode.getAttribute('font-family'):
		font.fontfamily = fontnode.getAttribute('font-family')
	else:
		font.fontfamily = fontname

	try:
		fontface = data.getElementsByTagName('font-face')[0]
		if fontface.getAttribute('bbox') and fontface.getAttribute('units-per-em'):
			xmin,ymin, xmax,ymax = map(float, fontface.getAttribute('bbox').replace(",", " ").split())
			upem = float(fontface.getAttribute('units-per-em'))
#			raise str((upem, (ymax-ymin)))
	except IndexError:
		pass

	# e. load fonts
	font.glyphs_dict = {}
	for node in fontnode.getElementsByTagName('glyph'):

		# 1. read path info
		glyph = Glyph()
		if node.hasAttribute('d'):	# is defined as <glyph> attribute
			glyph.path = node.getAttribute('d')
		else:
			# XXX: glyph is defined with path child element and I assume
			#      there is just one element
			path_elements = node.getElementsByTagName('path')
			if len(path_elements) == 0:		# no path elements at all
				pass
			elif len(path_elements) == 1:	# one path element
				glyph.path = path_elements[0].getAttribute('d')
			else: # more paths...
				pass # XXX: join them?

		# 2. get character name
		glyph.name = node.getAttribute('glyph-name')
		if glyph.name == '':
			log.error("There is a glyph without name, skipping it")
			continue

		# 2a. transcode
		if glyph.name not in name_lookup:
			try:
				glyph.name = transcode[glyph.name]
			except KeyError:
				log.error("Don't know what is '%s', skipping it", glyph.name)
				continue

		# 3. get horiz-adv-x
		if node.hasAttribute('horiz-adv-x'):
			glyph.hadv = float(node.getAttribute('horiz-adv-x'))
		elif default_hadvx != None:
			glyph.hadv = default_hadvx
		else:
			# XXX: calculate glyphs extends?
			raise FontError("Can't determine width of character '%s'", glyph.name)

		if glyph.name in font.glyphs_dict:
			log.error("Character '%s' already defined, skipping", glyph.name)
		else:
			font.glyphs_dict[glyph.name] = glyph
	#rof

	# f. write the cache file
	cPickle.dump(font, open(setup.cache_path + fontname + '.cache', 'wb'), protocol=cPickle.HIGHEST_PROTOCOL)
예제 #5
0
 def test_find_file(self):
     findfile.find_file(self.dir_path, self.find_name)