aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


client/runtime/linux_gdi.c (14462B)


      1 #include "linux_gdi.h"
      2 #ifndef __EMSCRIPTEN__
      3 #include <fontconfig/fontconfig.h>
      4 #endif
      5 #include <ft2build.h>
      6 #include FT_FREETYPE_H
      7 #include FT_TRUETYPE_TABLES_H
      8 
      9 /* GDI objects contain only guest tokens; font faces and raster buffers stay native. */
     10 static struct font {
     11     uint32_t handle;
     12     FT_Face face;
     13     int ascent, descent, weight, italic;
     14     struct font *next;
     15 } *fonts;
     16 static struct bitmap {
     17     uint32_t handle;
     18     unsigned width, height, pitch;
     19     unsigned char *pixels;
     20     struct bitmap *next;
     21 } *bitmaps;
     22 static struct context {
     23     uint32_t handle, font, bitmap;
     24     struct context *next;
     25 } *contexts;
     26 static FT_Library library;
     27 static uint32_t next_handle = 0xf5000002;
     28 
     29 static struct font *find_font(uint32_t handle)
     30 {
     31     for (struct font *font = fonts; font; font = font->next)
     32         if (font->handle == handle) return font;
     33     fprintf(stderr, "Unknown GDI font %08x\n", handle);
     34     abort();
     35 }
     36 
     37 static struct context *find_context(uint32_t handle)
     38 {
     39     for (struct context *context = contexts; context; context = context->next)
     40         if (context->handle == handle) return context;
     41     fprintf(stderr, "Unknown GDI context %08x\n", handle);
     42     abort();
     43 }
     44 
     45 static struct bitmap *find_bitmap(uint32_t handle)
     46 {
     47     for (struct bitmap *bitmap = bitmaps; bitmap; bitmap = bitmap->next)
     48         if (bitmap->handle == handle) return bitmap;
     49     fprintf(stderr, "Unknown GDI bitmap %08x\n", handle);
     50     abort();
     51 }
     52 
     53 static void check_font(FT_Error error)
     54 {
     55     if (error) { fprintf(stderr, "FreeType operation failed: %d\n", error); abort(); }
     56 }
     57 
     58 static unsigned big_endian_word(const unsigned char *data)
     59 {
     60     return (unsigned)data[0] * 256 + data[1];
     61 }
     62 
     63 static void size_font(struct font *font, int height)
     64 {
     65     TT_OS2 *os2 = FT_Get_Sfnt_Table(font->face, ft_sfnt_os2);
     66     unsigned em = font->face->units_per_EM;
     67     if (!os2 || !em || !height || height == INT32_MIN) {
     68         fprintf(stderr, "Unsupported font metrics or default height\n"); abort();
     69     }
     70     unsigned pixels = height < 0 ? -height : (uint64_t)height * em / (os2->usWinAscent + os2->usWinDescent);
     71     if (!pixels) pixels = 1;
     72     font->ascent = (os2->usWinAscent * pixels + em / 2) / em;
     73     font->descent = (os2->usWinDescent * pixels + em / 2) / em;
     74 
     75     /* OpenType VDMX describes the hinted cell bounds used by GDI. Select the
     76      * largest fitting cell for positive heights, or the exact em size for negative. */
     77     FT_ULong length = 0;
     78     if (!FT_Load_Sfnt_Table(font->face, FT_MAKE_TAG('V','D','M','X'), 0, NULL, &length)) {
     79         unsigned char *table = malloc(length);
     80         if (!table) abort();
     81         check_font(FT_Load_Sfnt_Table(font->face, FT_MAKE_TAG('V','D','M','X'), 0, table, &length));
     82         unsigned ratios = length >= 6 ? big_endian_word(table + 4) : 0;
     83         if (6 + ratios * 6 > length) abort();
     84         for (unsigned i = 0; i < ratios; ++i) {
     85             const unsigned char *ratio = table + 6 + i * 4;
     86             if (ratio[1] && (ratio[1] < ratio[2] || ratio[1] > ratio[3])) continue;
     87             unsigned group = big_endian_word(table + 6 + ratios * 4 + i * 2);
     88             if (group + 4 > length) abort();
     89             unsigned records = big_endian_word(table + group);
     90             if (group + 4 + records * 6 > length) abort();
     91             for (unsigned j = 0; j < records; ++j) {
     92                 const unsigned char *record = table + group + 4 + j * 6;
     93                 unsigned ppem = big_endian_word(record);
     94                 int top = (int16_t)big_endian_word(record + 2), bottom = (int16_t)big_endian_word(record + 4);
     95                 if (height < 0 ? ppem == (unsigned)-height : top - bottom <= height) {
     96                     pixels = ppem; font->ascent = top; font->descent = -bottom;
     97                     if (height < 0 || top - bottom == height) break;
     98                 } else if (height > 0) break;
     99             }
    100             break;
    101         }
    102         free(table);
    103     }
    104     check_font(FT_Set_Pixel_Sizes(font->face, 0, pixels));
    105 }
    106 
    107 static void create_font(void)
    108 {
    109     int height = MEM32(g_esp + 4), weight = MEM32(g_esp + 20);
    110     uint32_t italic = MEM32(g_esp + 24), face = MEM32(g_esp + 56);
    111     if (MEM32(g_esp + 8) || MEM32(g_esp + 12) || MEM32(g_esp + 16) ||
    112         MEM32(g_esp + 28) || MEM32(g_esp + 32) || MEM32(g_esp + 48) != 3 ||
    113         MEM32(g_esp + 36) > 1) {
    114         fprintf(stderr, "Unsupported GDI font rotation, decoration, charset or quality\n"); abort();
    115     }
    116     if (!library) check_font(FT_Init_FreeType(&library));
    117     struct font *font = calloc(1, sizeof(*font));
    118     if (!font) abort();
    119 #ifdef __EMSCRIPTEN__
    120     if (strcasecmp((const char *)ADDR(face), "Verdana")) {
    121         fprintf(stderr, "Unsupported browser font: %s\n", (const char *)ADDR(face));
    122         abort();
    123     }
    124     const char *path = weight >= 700 ? (italic ? "fonts/verdanaz.ttf" : "fonts/verdanab.ttf") :
    125                                      (italic ? "fonts/verdanai.ttf" : "fonts/verdana.ttf");
    126     check_font(FT_New_Face(library, path, 0, &font->face));
    127 #else
    128     FcPattern *pattern = FcPatternCreate();
    129     FcPatternAddString(pattern, FC_FAMILY, (const FcChar8 *)ADDR(face));
    130     FcPatternAddInteger(pattern, FC_WEIGHT, weight >= 700 ? FC_WEIGHT_BOLD : FC_WEIGHT_REGULAR);
    131     FcPatternAddInteger(pattern, FC_SLANT, italic ? FC_SLANT_ITALIC : FC_SLANT_ROMAN);
    132     FcConfigSubstitute(NULL, pattern, FcMatchPattern);
    133     FcDefaultSubstitute(pattern);
    134     FcResult result;
    135     FcPattern *match = FcFontMatch(NULL, pattern, &result);
    136     FcPatternDestroy(pattern);
    137     FcChar8 *path;
    138     int index = 0;
    139     if (!match || FcPatternGetString(match, FC_FILE, 0, &path) != FcResultMatch) abort();
    140     FcPatternGetInteger(match, FC_INDEX, 0, &index);
    141     check_font(FT_New_Face(library, (const char *)path, index, &font->face));
    142     FcPatternDestroy(match);
    143 #endif
    144     font->handle = next_handle++;
    145     font->weight = weight ? weight : 400;
    146     font->italic = italic != 0;
    147     size_font(font, height);
    148     font->next = fonts; fonts = font;
    149     g_eax = font->handle;
    150     g_esp += 60;
    151 }
    152 
    153 static void create_context(unsigned arguments)
    154 {
    155     struct context *context = calloc(1, sizeof(*context));
    156     if (!context) abort();
    157     context->handle = next_handle++;
    158     context->font = 0xf5000000;
    159     context->bitmap = 0xf5000001;
    160     context->next = contexts; contexts = context;
    161     g_eax = context->handle;
    162     g_esp += (arguments + 1) * 4;
    163 }
    164 
    165 static void create_ic(void) { create_context(4); }
    166 static void create_compatible_dc(void) { create_context(1); }
    167 
    168 static void delete_dc(void)
    169 {
    170     uint32_t handle = MEM32(g_esp + 4);
    171     struct context **link = &contexts;
    172     while (*link && (*link)->handle != handle) link = &(*link)->next;
    173     g_eax = *link != NULL;
    174     if (*link) { struct context *context = *link; *link = context->next; free(context); }
    175     g_esp += 8;
    176 }
    177 
    178 static void select_object(void)
    179 {
    180     struct context *context = find_context(MEM32(g_esp + 4));
    181     uint32_t handle = MEM32(g_esp + 8);
    182     g_eax = 0;
    183     int is_font = handle == 0xf5000000, is_bitmap = handle == 0xf5000001;
    184     for (struct font *font = fonts; font; font = font->next) if (font->handle == handle) is_font = 1;
    185     for (struct bitmap *bitmap = bitmaps; bitmap; bitmap = bitmap->next) if (bitmap->handle == handle) is_bitmap = 1;
    186     if (is_font) { g_eax = context->font; context->font = handle; }
    187     else if (is_bitmap) { g_eax = context->bitmap; context->bitmap = handle; }
    188     g_esp += 12;
    189 }
    190 
    191 static void delete_object(void)
    192 {
    193     uint32_t handle = MEM32(g_esp + 4);
    194     struct font **font = &fonts;
    195     while (*font && (*font)->handle != handle) font = &(*font)->next;
    196     struct bitmap **bitmap = &bitmaps;
    197     while (*bitmap && (*bitmap)->handle != handle) bitmap = &(*bitmap)->next;
    198     g_eax = 0;
    199     if (*font) {
    200         struct font *object = *font; *font = object->next;
    201         check_font(FT_Done_Face(object->face)); free(object); g_eax = 1;
    202     } else if (*bitmap) {
    203         struct bitmap *object = *bitmap; *bitmap = object->next;
    204         free(object->pixels); free(object); g_eax = 1;
    205     }
    206     g_esp += 8;
    207 }
    208 
    209 static void text_metrics(void)
    210 {
    211     struct font *font = find_font(find_context(MEM32(g_esp + 4))->font);
    212     uint32_t output = MEM32(g_esp + 8);
    213     TT_OS2 *os2 = FT_Get_Sfnt_Table(font->face, ft_sfnt_os2);
    214     unsigned ppem = font->face->size->metrics.y_ppem, em = font->face->units_per_EM;
    215     int height = font->ascent + font->descent;
    216     const uint32_t values[] = {height, font->ascent, font->descent, height > ppem ? height - ppem : 0,
    217         0, (os2->xAvgCharWidth * ppem + em / 2) / em,
    218         ((font->face->bbox.xMax - font->face->bbox.xMin) * ppem + em / 2) / em,
    219         font->weight, 0, 96, 96};
    220     memcpy((void *)ADDR(output), values, sizeof(values));
    221     const unsigned char chars[] = {30, 255, 31, 32, font->italic ? 255 : 0, 0, 0, 39, 0};
    222     memcpy((void *)ADDR(output + 44), chars, sizeof(chars));
    223     g_eax = 1;
    224     g_esp += 12;
    225 }
    226 
    227 static void text_extent(int wide)
    228 {
    229     struct font *font = find_font(find_context(MEM32(g_esp + 4))->font);
    230     uint32_t text = MEM32(g_esp + 8), count = MEM32(g_esp + 12), output = MEM32(g_esp + 16);
    231     int width = 0;
    232     for (uint32_t i = 0; i < count; ++i) {
    233         uint32_t character = wide ? MEM16(text + i * 2) : MEM8(text + i);
    234         check_font(FT_Load_Char(font->face, character, FT_LOAD_TARGET_MONO));
    235         width += font->face->glyph->advance.x / 64;
    236     }
    237     MEM32(output) = width;
    238     MEM32(output + 4) = count ? font->ascent + font->descent : 0;
    239     g_eax = 1;
    240     g_esp += 20;
    241 }
    242 
    243 static void text_extent_a(void) { text_extent(0); }
    244 static void text_extent_w(void) { text_extent(1); }
    245 
    246 static void create_bitmap(void)
    247 {
    248     int32_t width = MEM32(g_esp + 4), height = MEM32(g_esp + 8);
    249     uint32_t planes = MEM32(g_esp + 12), bits = MEM32(g_esp + 16), input = MEM32(g_esp + 20);
    250     g_esp += 24;
    251     if (planes != 1 || bits != 1) { fprintf(stderr, "Only monochrome GDI bitmaps are implemented\n"); abort(); }
    252     g_eax = 0;
    253     if (width <= 0 || height <= 0) return;
    254     struct bitmap *bitmap = calloc(1, sizeof(*bitmap));
    255     if (!bitmap) abort();
    256     bitmap->width = width; bitmap->height = height;
    257     bitmap->pitch = ((uint64_t)width + 15) / 16 * 2;
    258     bitmap->pixels = calloc(height, bitmap->pitch);
    259     if (!bitmap->pixels) { free(bitmap); return; }
    260     if (input) memcpy(bitmap->pixels, (void *)ADDR(input), (size_t)height * bitmap->pitch);
    261     bitmap->handle = next_handle++;
    262     bitmap->next = bitmaps; bitmaps = bitmap;
    263     g_eax = bitmap->handle;
    264 }
    265 
    266 static void text_out_w(void)
    267 {
    268     struct context *context = find_context(MEM32(g_esp + 4));
    269     struct font *font = find_font(context->font);
    270     struct bitmap *bitmap = find_bitmap(context->bitmap);
    271     int32_t x = MEM32(g_esp + 8), y = MEM32(g_esp + 12);
    272     uint32_t text = MEM32(g_esp + 16), count = MEM32(g_esp + 20);
    273     int width = 0;
    274     for (uint32_t i = 0; i < count; ++i) {
    275         check_font(FT_Load_Char(font->face, MEM16(text + i * 2), FT_LOAD_TARGET_MONO));
    276         width += font->face->glyph->advance.x / 64;
    277     }
    278 
    279     /* MM_TEXT defaults: black text, opaque white background, top-left alignment. */
    280     for (int row = y < 0 ? 0 : y; row < y + font->ascent + font->descent && row < bitmap->height; ++row)
    281         for (int column = x < 0 ? 0 : x; column < x + width && column < bitmap->width; ++column)
    282             bitmap->pixels[row * bitmap->pitch + column / 8] |= 0x80 >> (column & 7);
    283     int pen = x;
    284     for (uint32_t i = 0; i < count; ++i) {
    285         check_font(FT_Load_Char(font->face, MEM16(text + i * 2), FT_LOAD_RENDER | FT_LOAD_TARGET_MONO));
    286         FT_GlyphSlot glyph = font->face->glyph;
    287         for (unsigned row = 0; row < glyph->bitmap.rows; ++row) {
    288             int target_y = y + font->ascent - glyph->bitmap_top + row;
    289             if (target_y < 0 || target_y >= bitmap->height) continue;
    290             const unsigned char *line = glyph->bitmap.buffer + row * glyph->bitmap.pitch;
    291             for (unsigned column = 0; column < glyph->bitmap.width; ++column) {
    292                 int target_x = pen + glyph->bitmap_left + column;
    293                 if (target_x < 0 || target_x >= bitmap->width) continue;
    294                 if (line[column / 8] & (0x80 >> (column & 7)))
    295                     bitmap->pixels[target_y * bitmap->pitch + target_x / 8] &= ~(0x80 >> (target_x & 7));
    296             }
    297         }
    298         pen += glyph->advance.x / 64;
    299     }
    300     g_eax = 1;
    301     g_esp += 24;
    302 }
    303 
    304 static void get_di_bits(void)
    305 {
    306     find_context(MEM32(g_esp + 4));
    307     struct bitmap *bitmap = find_bitmap(MEM32(g_esp + 8));
    308     uint32_t start = MEM32(g_esp + 12), count = MEM32(g_esp + 16), output = MEM32(g_esp + 20);
    309     uint32_t info = MEM32(g_esp + 24), usage = MEM32(g_esp + 28);
    310     g_esp += 32;
    311     if (usage || MEM32(info) != 40 || MEM16(info + 12) != 1 || MEM16(info + 14) != 1 ||
    312         MEM32(info + 4) != bitmap->width || MEM32(info + 16)) {
    313         fprintf(stderr, "Unsupported GDI DIB conversion\n"); abort();
    314     }
    315     int32_t height = MEM32(info + 8);
    316     if ((int64_t)height != bitmap->height && -(int64_t)height != bitmap->height) abort();
    317     unsigned pitch = (bitmap->width + 31) / 32 * 4;
    318     MEM32(info + 20) = pitch * bitmap->height;
    319     MEM32(info + 40) = 0; MEM32(info + 44) = 0xffffff;
    320     g_eax = 0;
    321     if (start >= bitmap->height) return;
    322     if (count > bitmap->height - start) count = bitmap->height - start;
    323     if (output) {
    324         for (unsigned i = 0; i < count; ++i) {
    325             unsigned row = height < 0 ? start + i : bitmap->height - start - i - 1;
    326             memset((void *)ADDR(output + i * pitch), 0, pitch);
    327             memcpy((void *)ADDR(output + i * pitch), bitmap->pixels + row * bitmap->pitch, (bitmap->width + 7) / 8);
    328         }
    329     }
    330     g_eax = count;
    331 }
    332 
    333 recomp_func_t linux_gdi_lookup(const char *name)
    334 {
    335     static const struct { const char *name; recomp_func_t function; } functions[] = {
    336         {"CreateFontA", create_font}, {"CreateICA", create_ic},
    337         {"CreateCompatibleDC", create_compatible_dc}, {"DeleteDC", delete_dc},
    338         {"SelectObject", select_object}, {"DeleteObject", delete_object},
    339         {"GetTextMetricsA", text_metrics}, {"GetTextExtentPoint32A", text_extent_a},
    340         {"GetTextExtentPoint32W", text_extent_w},
    341         {"CreateBitmap", create_bitmap}, {"TextOutW", text_out_w}, {"GetDIBits", get_di_bits},
    342     };
    343     for (unsigned i = 0; i < sizeof(functions) / sizeof(functions[0]); ++i)
    344         if (!strcmp(name, functions[i].name)) return functions[i].function;
    345     return NULL;
    346 }