コード例 #1
0
        /// <summary>
        /// Evaluate the CharString for the character with a given name returning the path constructed for the glyph.
        /// </summary>
        /// <param name="name">The name of the character to retrieve the CharString for.</param>
        /// <param name="defaultWidthX">The default width for the glyph from the font's private dictionary.</param>
        /// <param name="nominalWidthX">The nominal width which individual glyph widths are encoded as the difference from.</param>
        /// <returns>A <see cref="PdfPath"/> for the glyph.</returns>
        public Type2Glyph Generate(string name, decimal defaultWidthX, decimal nominalWidthX)
        {
            Type2Glyph glyph;

            lock (locker)
            {
                if (glyphs.TryGetValue(name, out var result))
                {
                    return(result);
                }

                if (!CharStrings.TryGetValue(name, out var sequence))
                {
                    throw new InvalidOperationException($"No charstring sequence with the name /{name} in this font.");
                }

                try
                {
                    glyph = Run(sequence, defaultWidthX, nominalWidthX);

                    glyphs[name] = glyph;
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException($"Failed to interpret charstring for symbol with name: {name}. Commands: {sequence}.", ex);
                }
            }

            return(glyph);
        }
コード例 #2
0
ファイル: Type1CharStrings.cs プロジェクト: Omsaaf/PdfPig
        private CharacterPath Run(CommandSequence sequence)
        {
            var context = new Type1BuildCharContext(Subroutines, i =>
            {
                if (!charStringIndexToName.TryGetValue(i, out var name))
                {
                    throw new InvalidOperationException($"Tried to retrieve Type 1 charstring by index {i} which did not exist.");
                }

                if (glyphs.TryGetValue(name, out var result))
                {
                    return(result);
                }

                if (!CharStrings.TryGetValue(name, out var charstring))
                {
                    throw new InvalidOperationException($"Tried to retrieve Type 1 charstring by index {i} which mapped to name {name} but was not found in the charstrings.");
                }

                var path = Run(charstring);

                glyphs[name] = path;

                return(path);
            });

            foreach (var command in sequence.Commands)
            {
                command.Match(x => context.Stack.Push(x),
                              x => x.Run(context));
            }

            return(context.Path);
        }
コード例 #3
0
ファイル: Type1CharStrings.cs プロジェクト: databill86/PdfPig
        public bool TryGenerate(string name, out PdfPath path)
        {
            path = default(PdfPath);
            lock (locker)
            {
                if (glyphs.TryGetValue(name, out path))
                {
                    return(true);
                }

                if (!CharStrings.TryGetValue(name, out var sequence))
                {
                    return(false);
                }

                try
                {
                    path = Run(sequence);

                    glyphs[name] = path;
                }
                catch
                {
                    return(false);
                }
            }

            return(true);
        }
コード例 #4
0
ファイル: Type1CharStrings.cs プロジェクト: databill86/PdfPig
        private PdfPath Run(CommandSequence sequence)
        {
            var context = new Type1BuildCharContext(Subroutines, i =>
            {
                if (!charStringIndexToName.TryGetValue(i, out var name))
                {
                    throw new InvalidOperationException($"Tried to retrieve Type 1 charstring by index {i} which did not exist.");
                }

                if (glyphs.TryGetValue(name, out var result))
                {
                    return(result);
                }

                if (!CharStrings.TryGetValue(name, out var charstring))
                {
                    throw new InvalidOperationException($"Tried to retrieve Type 1 charstring by index {i} which mapped to name {name} but was not found in the charstrings.");
                }

                var path = Run(charstring);

                glyphs[name] = path;

                return(path);
            }, s =>
            {
                if (glyphs.TryGetValue(s, out var result))
                {
                    return(result);
                }

                if (!CharStrings.TryGetValue(s, out var charstring))
                {
                    throw new InvalidOperationException($"Tried to retrieve Type 1 charstring by name {s} but it was not found in the charstrings.");
                }

                var path = Run(charstring);

                glyphs[s] = path;

                return(path);
            });

            foreach (var command in sequence.Commands)
            {
                if (command.TryGetFirst(out var num))
                {
                    context.Stack.Push(num);
                }
                else if (command.TryGetSecond(out var lazyCommand))
                {
                    lazyCommand.Run(context);
                }
            }

            return(context.Path);
        }
コード例 #5
0
ファイル: Type1CharStrings.cs プロジェクト: Omsaaf/PdfPig
        public CharacterPath Generate(string name)
        {
            CharacterPath glyph;

            lock (locker)
            {
                if (glyphs.TryGetValue(name, out var result))
                {
                    return(result);
                }

                if (!CharStrings.TryGetValue(name, out var sequence))
                {
                    throw new InvalidOperationException($"No charstring sequence with the name /{name} in this font.");
                }

                glyph = Run(sequence);

                glyphs[name] = glyph;
            }

            return(glyph);
        }