summaryrefslogtreecommitdiff
path: root/Userland/Utilities/ttfdisasm.cpp
blob: d21f9b561a7953f7daea463e10a537e7bcfa507b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * Copyright (c) 2023, MacDue <macdue@dueutil.tech>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/Format.h>
#include <AK/Utf8View.h>
#include <LibCore/ArgsParser.h>
#include <LibGfx/Font/OpenType/Font.h>
#include <LibGfx/Font/OpenType/Hinting/Opcodes.h>
#include <LibMain/Main.h>

using namespace OpenType::Hinting;

#define YELLOW "\e[33m"
#define CYAN "\e[36m"
#define PURPLE "\e[95m"
#define GREEN "\e[92m"
#define RESET "\e[0m"
#define GRAY "\e[90m"

struct InstructionPrinter : InstructionHandler {
    InstructionPrinter(bool enable_highlighting)
        : m_enable_highlighting(enable_highlighting)
    {
    }

    void before_operation(InstructionStream& stream, Opcode opcode) override
    {
        if (opcode == Opcode::FDEF && stream.current_position() > 1 && m_indent_level == 1)
            outln();
        switch (opcode) {
        case Opcode::EIF:
        case Opcode::ELSE:
        case Opcode::ENDF:
            m_indent_level--;
            break;
        default:
            break;
        }
        auto digits = int(AK::log10(float(stream.length()))) + 1;
        if (m_enable_highlighting)
            out(GRAY);
        out("{:0{}}:", stream.current_position() - 1, digits);
        if (m_enable_highlighting)
            out(RESET);
        out("{:{}}", ""sv, m_indent_level * 2);
    }

    void after_operation(InstructionStream&, Opcode opcode) override
    {
        switch (opcode) {
        case Opcode::IF:
        case Opcode::ELSE:
        case Opcode::IDEF:
        case Opcode::FDEF:
            m_indent_level++;
            break;
        default:
            break;
        }
    }

    void print_number(u16 value)
    {
        if (m_enable_highlighting)
            return out(GREEN " {}" RESET, value);
        return out(" {}", value);
    }

    void print_bytes(ReadonlyBytes bytes, bool first = true)
    {
        for (auto value : bytes) {
            if (!first)
                out(",");
            print_number(value);
            first = false;
        }
    }

    void print_words(ReadonlyBytes bytes, bool first = true)
    {
        for (size_t i = 0; i < bytes.size(); i += 2) {
            if (!first)
                out(",");
            print_number(bytes[i] << 8 | bytes[i + 1]);
            first = false;
        }
    }

    void default_handler(Context context) override
    {
        auto instruction = context.instruction();
        auto name = opcode_mnemonic(instruction.opcode());
        if (m_enable_highlighting)
            out(YELLOW);
        out(name);
        if (m_enable_highlighting)
            out(CYAN);
        out("[");
        if (m_enable_highlighting)
            out(PURPLE);
        if (instruction.flag_bits() > 0)
            out("{:0{}b}", to_underlying(instruction.opcode()) & ((1 << instruction.flag_bits()) - 1), instruction.flag_bits());
        if (m_enable_highlighting)
            out(CYAN);
        out("]");
        if (m_enable_highlighting)
            out(RESET);
        switch (instruction.opcode()) {
        case Opcode::NPUSHB... Opcode::NPUSHB_MAX:
            print_number(instruction.values().size());
            print_bytes(instruction.values(), false);
            break;
        case Opcode::NPUSHW... Opcode::NPUSHW_MAX:
            print_number(instruction.values().size() / 2);
            print_words(instruction.values(), false);
            break;
        case Opcode::PUSHB... Opcode::PUSHB_MAX:
            print_bytes(instruction.values());
            break;
        case Opcode::PUSHW... Opcode::PUSHW_MAX:
            print_words(instruction.values());
            break;
        default:
            break;
        }
        outln();
    }

private:
    bool m_enable_highlighting;
    u32 m_indent_level { 1 };
};

static bool s_disassembly_attempted = false;

static void print_disassembly(StringView name, Optional<ReadonlyBytes> program, bool enable_highlighting, u32 code_point = 0)
{
    s_disassembly_attempted = true;
    if (!program.has_value()) {
        out(name, code_point);
        outln(": not found");
        return;
    }
    out(name, code_point);
    outln(":    ({} bytes)\n", program->size());
    InstructionPrinter printer { enable_highlighting };
    InstructionStream stream { printer, *program };
    while (!stream.at_end())
        stream.process_next_instruction();
}

ErrorOr<int> serenity_main(Main::Arguments arguments)
{
    Core::ArgsParser args_parser;

    StringView font_path;
    bool no_color = false;
    bool dump_font_program = false;
    bool dump_prep_program = false;
    StringView text;
    args_parser.add_positional_argument(font_path, "Path to font", "FILE");
    args_parser.add_option(dump_font_program, "Disassemble font program (fpgm table)", "disasm-fpgm", 'f');
    args_parser.add_option(dump_prep_program, "Disassemble CVT program (prep table)", "disasm-prep", 'p');
    args_parser.add_option(text, "Disassemble glyph programs", "disasm-glyphs", 'g', "text");
    args_parser.add_option(no_color, "Disable syntax highlighting", "no-color", 'n');
    args_parser.parse(arguments);

    auto font = TRY(OpenType::Font::try_load_from_file(font_path));

    if (dump_font_program)
        print_disassembly("Font program"sv, font->font_program(), !no_color);
    if (dump_prep_program) {
        if (dump_font_program)
            outln();
        print_disassembly("CVT program"sv, font->control_value_program(), !no_color);
    }
    if (!text.is_empty()) {
        Utf8View utf8_view { text };
        bool first = !(dump_font_program || dump_prep_program);
        for (u32 code_point : utf8_view) {
            if (!first)
                outln();
            auto glyph_id = font->glyph_id_for_code_point(code_point);
            print_disassembly("Glyph program for codepoint {}"sv, font->glyph_program(glyph_id), !no_color, code_point);
            first = false;
        }
    }

    if (!s_disassembly_attempted) {
        args_parser.print_usage(stderr, arguments.strings[0]);
        return 1;
    }
    return 0;
}