LibPDF: Rename Command to Operator

This is the correct name, according to the spec
This commit is contained in:
Matthew Olsson 2022-03-25 15:00:11 -07:00 committed by Andreas Kling
parent 49cb040c27
commit 468ceb1b48
5 changed files with 65 additions and 65 deletions

View file

@ -12,7 +12,7 @@
#include <AK/Vector.h>
#include <LibPDF/Value.h>
#define ENUMERATE_COMMANDS(V) \
#define ENUMERATE_OPERATORS(V) \
V(SaveState, save_state, q) \
V(RestoreState, restore_state, Q) \
V(ConcatenateMatrix, concatenate_matrix, cm) \
@ -87,76 +87,76 @@
namespace PDF {
enum class CommandType {
enum class OperatorType {
#define V(name, snake_name, symbol) name,
ENUMERATE_COMMANDS(V)
ENUMERATE_OPERATORS(V)
#undef V
TextNextLineShowString,
TextNextLineShowStringSetSpacing,
};
class Command {
class Operator {
public:
static CommandType command_type_from_symbol(StringView symbol_string)
static OperatorType operator_type_from_symbol(StringView symbol_string)
{
#define V(name, snake_name, symbol) \
if (symbol_string == #symbol) \
return CommandType::name;
ENUMERATE_COMMANDS(V)
return OperatorType::name;
ENUMERATE_OPERATORS(V)
#undef V
if (symbol_string == "'")
return CommandType::TextNextLineShowString;
return OperatorType::TextNextLineShowString;
if (symbol_string == "''")
return CommandType::TextNextLineShowStringSetSpacing;
return OperatorType::TextNextLineShowStringSetSpacing;
dbgln("unsupported graphics symbol {}", symbol_string);
VERIFY_NOT_REACHED();
}
static const char* command_name(CommandType command_name)
static const char* operator_name(OperatorType operator_type)
{
#define V(name, snake_name, symbol) \
if (command_name == CommandType::name) \
if (operator_type == OperatorType::name) \
return #name;
ENUMERATE_COMMANDS(V)
ENUMERATE_OPERATORS(V)
#undef V
if (command_name == CommandType::TextNextLineShowString)
if (operator_type == OperatorType::TextNextLineShowString)
return "TextNextLineShowString";
if (command_name == CommandType::TextNextLineShowStringSetSpacing)
if (operator_type == OperatorType::TextNextLineShowStringSetSpacing)
return "TextNextLineShowStringSetSpacing";
VERIFY_NOT_REACHED();
}
static const char* command_symbol(CommandType command_name)
static const char* operator_symbol(OperatorType operator_type)
{
#define V(name, snake_name, symbol) \
if (command_name == CommandType::name) \
if (operator_type == OperatorType::name) \
return #symbol;
ENUMERATE_COMMANDS(V)
ENUMERATE_OPERATORS(V)
#undef V
if (command_name == CommandType::TextNextLineShowString)
if (operator_type == OperatorType::TextNextLineShowString)
return "'";
if (command_name == CommandType::TextNextLineShowStringSetSpacing)
if (operator_type == OperatorType::TextNextLineShowStringSetSpacing)
return "''";
VERIFY_NOT_REACHED();
}
Command(CommandType command_type, Vector<Value> arguments)
: m_command_type(command_type)
Operator(OperatorType operator_type, Vector<Value> arguments)
: m_operator_type(operator_type)
, m_arguments(move(arguments))
{
}
[[nodiscard]] ALWAYS_INLINE CommandType command_type() const { return m_command_type; }
[[nodiscard]] ALWAYS_INLINE OperatorType type() const { return m_operator_type; }
[[nodiscard]] ALWAYS_INLINE Vector<Value> const& arguments() const { return m_arguments; }
private:
CommandType m_command_type;
OperatorType m_operator_type;
Vector<Value> m_arguments;
};
@ -165,17 +165,17 @@ private:
namespace AK {
template<>
struct Formatter<PDF::Command> : Formatter<StringView> {
ErrorOr<void> format(FormatBuilder& format_builder, PDF::Command const& command)
struct Formatter<PDF::Operator> : Formatter<StringView> {
ErrorOr<void> format(FormatBuilder& format_builder, PDF::Operator const& op)
{
StringBuilder builder;
builder.appendff("{} ({})",
PDF::Command::command_name(command.command_type()),
PDF::Command::command_symbol(command.command_type()));
PDF::Operator::operator_name(op.type()),
PDF::Operator::operator_symbol(op.type()));
if (!command.arguments().is_empty()) {
if (!op.arguments().is_empty()) {
builder.append(" [");
for (auto& argument : command.arguments())
for (auto& argument : op.arguments())
builder.appendff(" {}", argument);
builder.append(" ]");
}

View file

@ -22,11 +22,11 @@ static NonnullRefPtr<T> make_object(Args... args) requires(IsBaseOf<Object, T>)
return adopt_ref(*new T(forward<Args>(args)...));
}
PDFErrorOr<Vector<Command>> Parser::parse_graphics_commands(Document* document, ReadonlyBytes bytes)
PDFErrorOr<Vector<Operator>> Parser::parse_operators(Document* document, ReadonlyBytes bytes)
{
auto parser = adopt_ref(*new Parser(document, bytes));
parser->m_disable_encryption = true;
return parser->parse_graphics_commands();
return parser->parse_operators();
}
Parser::Parser(Document* document, ReadonlyBytes bytes)
@ -1029,39 +1029,39 @@ PDFErrorOr<NonnullRefPtr<StreamObject>> Parser::parse_stream(NonnullRefPtr<DictO
return stream_object;
}
PDFErrorOr<Vector<Command>> Parser::parse_graphics_commands()
PDFErrorOr<Vector<Operator>> Parser::parse_operators()
{
Vector<Command> commands;
Vector<Value> command_args;
Vector<Operator> operators;
Vector<Value> operator_args;
constexpr static auto is_command_char = [](char ch) {
constexpr static auto is_operator_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)) {
if (is_operator_char(ch)) {
auto operator_start = m_reader.offset();
while (is_operator_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>();
auto operator_string = StringView(m_reader.bytes().slice(operator_start, m_reader.offset() - operator_start));
auto operator_type = Operator::operator_type_from_symbol(operator_string);
operators.append(Operator(operator_type, move(operator_args)));
operator_args = Vector<Value>();
consume_whitespace();
continue;
}
command_args.append(TRY(parse_value()));
operator_args.append(TRY(parse_value()));
}
return commands;
return operators;
}
bool Parser::matches_eol() const

View file

@ -9,8 +9,8 @@
#include <AK/NonnullRefPtrVector.h>
#include <AK/SourceLocation.h>
#include <AK/WeakPtr.h>
#include <LibPDF/Command.h>
#include <LibPDF/Object.h>
#include <LibPDF/Operator.h>
#include <LibPDF/Reader.h>
#include <LibPDF/XRefTable.h>
@ -25,7 +25,7 @@ public:
Linearized,
};
static PDFErrorOr<Vector<Command>> parse_graphics_commands(Document*, ReadonlyBytes);
static PDFErrorOr<Vector<Operator>> parse_operators(Document*, ReadonlyBytes);
Parser(Document*, ReadonlyBytes);
@ -115,7 +115,7 @@ private:
PDFErrorOr<NonnullRefPtr<DictObject>> parse_dict();
PDFErrorOr<NonnullRefPtr<StreamObject>> parse_stream(NonnullRefPtr<DictObject> dict);
PDFErrorOr<Vector<Command>> parse_graphics_commands();
PDFErrorOr<Vector<Operator>> parse_operators();
void push_reference(Reference const& ref) { m_current_reference_stack.append(ref); }
void pop_reference() { m_current_reference_stack.take_last(); }

View file

@ -78,28 +78,28 @@ PDFErrorOr<void> Renderer::render()
byte_buffer.append(bytes.data(), bytes.size());
}
auto commands = TRY(Parser::parse_graphics_commands(m_document, byte_buffer));
auto operators = TRY(Parser::parse_operators(m_document, byte_buffer));
for (auto& command : commands)
TRY(handle_command(command));
for (auto& op : operators)
TRY(handle_operator(op));
return {};
}
PDFErrorOr<void> Renderer::handle_command(Command const& command)
PDFErrorOr<void> Renderer::handle_operator(Operator const& op)
{
switch (command.command_type()) {
switch (op.type()) {
#define V(name, snake_name, symbol) \
case CommandType::name: \
TRY(handle_##snake_name(command.arguments())); \
case OperatorType::name: \
TRY(handle_##snake_name(op.arguments())); \
break;
ENUMERATE_COMMANDS(V)
ENUMERATE_OPERATORS(V)
#undef V
case CommandType::TextNextLineShowString:
TRY(handle_text_next_line_show_string(command.arguments()));
case OperatorType::TextNextLineShowString:
TRY(handle_text_next_line_show_string(op.arguments()));
break;
case CommandType::TextNextLineShowStringSetSpacing:
TRY(handle_text_next_line_show_string_set_spacing(command.arguments()));
case OperatorType::TextNextLineShowStringSetSpacing:
TRY(handle_text_next_line_show_string_set_spacing(op.arguments()));
break;
}

View file

@ -89,16 +89,16 @@ private:
PDFErrorOr<void> render();
PDFErrorOr<void> handle_command(Command const&);
PDFErrorOr<void> handle_operator(Operator const&);
#define V(name, snake_name, symbol) \
PDFErrorOr<void> handle_##snake_name(Vector<Value> const& args);
ENUMERATE_COMMANDS(V)
ENUMERATE_OPERATORS(V)
#undef V
PDFErrorOr<void> handle_text_next_line_show_string(Vector<Value> const& args);
PDFErrorOr<void> handle_text_next_line_show_string_set_spacing(Vector<Value> const& args);
PDFErrorOr<void> set_graphics_state_from_dict(NonnullRefPtr<DictObject>);
// shift is the manual advance given in the TJ command array
// shift is the manual advance given in the TJ operator array
void show_text(String const&, float shift = 0.0f);
PDFErrorOr<NonnullRefPtr<ColorSpace>> get_color_space(Value const&);