예제 #1
0
        public static ShxGlyph Process(string glyphName, IEnumerable <ShxGlyphCommand> commands, IReadOnlyDictionary <ushort, IEnumerable <ShxGlyphCommand> > characters)
        {
            var state = new ShxCommandProcessorState();

            Process(state, commands, characters);
            var glyph = state.CreateGlyph(glyphName);

            return(glyph);
        }
예제 #2
0
        private static void Process(ShxCommandProcessorState state, IEnumerable <ShxGlyphCommand> commands, IReadOnlyDictionary <ushort, IEnumerable <ShxGlyphCommand> > characters)
        {
            foreach (var command in commands)
            {
                if (state.MoveNext())
                {
                    switch (command)
                    {
                    case ShxGlyphCommandPenDown _:
                        state.SetDrawingState(true);
                        break;

                    case ShxGlyphCommandPenUp _:
                        state.SetDrawingState(false);
                        break;

                    case ShxGlyphCommandUpdateScaleVector sc:
                        state.ApplyVectorScale(sc.Scale);
                        break;

                    case ShxGlyphCommandPushPoint _:
                        state.PushPoint();
                        break;

                    case ShxGlyphCommandPopPoint _:
                        state.PopPoint();
                        break;

                    case ShxGlyphCommandReplayCharacter r:
                        if (characters.TryGetValue(r.Character, out var subCommands))
                        {
                            // TODO: handle offset/scale
                            Process(state, subCommands, characters);
                        }
                        break;

                    case ShxGlyphCommandMoveCursor m:
                        state.ProcessNewPosition(new ShxPoint(m.DeltaX, m.DeltaY));
                        break;

                    case ShxGlyphCommandOctantArc a:
                        state.ProcessArc(a);
                        break;

                    case ShxGlyphCommandFractionalArc a:
                        state.ProcessArc(a);
                        break;

                    case ShxGlyphCommandArc a:
                        state.ProcessArc(a);
                        break;

                    case ShxGlyphCommandSkipNextIfHorizontal _:
                        // always assuming horizontal mode
                        state.SkipNextCommand();
                        break;

                    default:
                        throw new NotSupportedException($"Unexpected glyph command '{command?.GetType().Name}'");
                    }
                }
                else if (command is ShxGlyphCommandMoveCursor move)
                {
                    // heuristic: the vertical command skip usually preceeds a move that corresponds to the negative
                    // height and half the width of the character
                    state.SetSize(Math.Abs(move.DeltaX), Math.Abs(move.DeltaY));
                }
            }
        }