Exemplo n.º 1
0
        private bool TryReadFile(string fileName, bool readNameFirst, string fontName, out TrueTypeFont font)
        {
            font = null;

            var bytes = File.ReadAllBytes(fileName);

            var data = new TrueTypeDataBytes(new ByteArrayInputBytes(bytes));

            if (readNameFirst)
            {
                var name = TrueTypeFontParser.GetNameTable(data);

                if (name == null)
                {
                    lock (readFilesLock)
                    {
                        readFiles.Add(fileName);
                    }

                    return(false);
                }

                var fontNameFromFile = name.GetPostscriptName() ?? name.FontName;

                nameToFileNameMap.TryAdd(fontNameFromFile, fileName);

                if (!string.Equals(fontNameFromFile, fontName, StringComparison.OrdinalIgnoreCase))
                {
                    lock (readFilesLock)
                    {
                        readFiles.Add(fileName);
                    }

                    return(false);
                }
            }

            data.Seek(0);
            font = TrueTypeFontParser.Parse(data);
            var psName = font.TableRegister.NameTable?.GetPostscriptName() ?? font.Name;

            lock (CacheLock)
            {
                if (!Cache.ContainsKey(psName))
                {
                    Cache[psName] = font;
                }
            }

            lock (readFilesLock)
            {
                readFiles.Add(fileName);
            }

            return(true);
        }
Exemplo n.º 2
0
        private bool TryReadFile(string fileName, bool readNameFirst, string fontName, out TrueTypeFontProgram font)
        {
            font = null;
            readFiles.Add(fileName);

            using (var fileStream = File.OpenRead(fileName))
            {
                var input = new StreamInputBytes(fileStream);
                var data  = new TrueTypeDataBytes(input);

                if (readNameFirst)
                {
                    var name = trueTypeFontParser.GetNameTable(data);

                    if (name == null)
                    {
                        return(false);
                    }

                    var fontNameFromFile = name.GetPostscriptName() ?? name.FontName;

                    nameToFileNameMap[fontNameFromFile] = fileName;

                    if (!string.Equals(fontNameFromFile, fontName, StringComparison.OrdinalIgnoreCase))
                    {
                        return(false);
                    }
                }

                data.Seek(0);
                font = trueTypeFontParser.Parse(data);
                var psName = font.TableRegister.NameTable?.GetPostscriptName() ?? font.Name;
                if (!cache.ContainsKey(psName))
                {
                    cache[psName] = font;
                }

                return(true);
            }
        }