1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 14:38:11 +00:00

LibPDF: Parse graphics commands

This commit is contained in:
Matthew Olsson 2021-05-10 10:39:19 -07:00 committed by Andreas Kling
parent 03649f85e2
commit 101639e526
3 changed files with 190 additions and 0 deletions

View file

@ -19,11 +19,22 @@ static NonnullRefPtr<T> make_object(Args... args) requires(IsBaseOf<Object, T>)
return adopt_ref(*new T(forward<Args>(args)...));
}
Vector<Command> Parser::parse_graphics_commands(const ReadonlyBytes& bytes)
{
Parser parser(bytes);
return parser.parse_graphics_commands();
}
Parser::Parser(Badge<Document>, const ReadonlyBytes& bytes)
: m_reader(bytes)
{
}
Parser::Parser(const ReadonlyBytes& bytes)
: m_reader(bytes)
{
}
bool Parser::perform_validation()
{
return !sloppy_is_linearized() && parse_header();
@ -650,6 +661,41 @@ NonnullRefPtr<StreamObject> Parser::parse_stream(NonnullRefPtr<DictObject> dict)
return make_object<StreamObject>(dict, bytes);
}
Vector<Command> Parser::parse_graphics_commands()
{
Vector<Command> commands;
Vector<Value> command_args;
constexpr static auto is_command_char = [](char ch) {
return isalpha(ch) || ch == '*' || ch == '\'';
};
while (!m_reader.done()) {
auto ch = m_reader.peek();
if (is_command_char(ch)) {
auto command_start = m_reader.offset();
while (is_command_char(ch)) {
consume();
if (m_reader.done())
break;
ch = m_reader.peek();
}
auto command_string = StringView(m_reader.bytes().slice(command_start, m_reader.offset() - command_start));
auto command_type = Command::command_type_from_symbol(command_string);
commands.append(Command(command_type, move(command_args)));
command_args = Vector<Value>();
consume_whitespace();
continue;
}
command_args.append(parse_value());
}
return commands;
}
bool Parser::matches_eol() const
{
return m_reader.matches_any(0xa, 0xd);