void draw_text(const font_t* font, color_t color, int x, int y, text_align_t alignment, const char* text) { bool is_draw_held; int cp; if (alignment == TEXT_ALIGN_CENTER) x -= get_text_width(font, text) / 2; else if (alignment == TEXT_ALIGN_RIGHT) x -= get_text_width(font, text); is_draw_held = al_is_bitmap_drawing_held(); al_hold_bitmap_drawing(true); while ((cp = *text++) != '\0') { draw_image_masked(font->glyphs[cp].image, color, x, y); x += font->glyphs[cp].width; } al_hold_bitmap_drawing(is_draw_held); }
void draw_text(const font_t* font, color_t color, int x, int y, text_align_t alignment, const char* text) { uint8_t ch_byte; uint32_t cp; int tab_width; uint32_t utf8state; if (alignment == TEXT_ALIGN_CENTER) x -= get_text_width(font, text) / 2; else if (alignment == TEXT_ALIGN_RIGHT) x -= get_text_width(font, text); tab_width = font->glyphs[' '].width * 3; al_hold_bitmap_drawing(true); for (;;) { utf8state = UTF8_ACCEPT; while (utf8decode(&utf8state, &cp, ch_byte = *text++) > UTF8_REJECT); if (utf8state == UTF8_REJECT && ch_byte == '\0') --text; // don't eat NUL terminator cp = cp == 0x20AC ? 128 : cp == 0x201A ? 130 : cp == 0x0192 ? 131 : cp == 0x201E ? 132 : cp == 0x2026 ? 133 : cp == 0x2020 ? 134 : cp == 0x2021 ? 135 : cp == 0x02C6 ? 136 : cp == 0x2030 ? 137 : cp == 0x0160 ? 138 : cp == 0x2039 ? 139 : cp == 0x0152 ? 140 : cp == 0x017D ? 142 : cp == 0x2018 ? 145 : cp == 0x2019 ? 146 : cp == 0x201C ? 147 : cp == 0x201D ? 148 : cp == 0x2022 ? 149 : cp == 0x2013 ? 150 : cp == 0x2014 ? 151 : cp == 0x02DC ? 152 : cp == 0x2122 ? 153 : cp == 0x0161 ? 154 : cp == 0x203A ? 155 : cp == 0x0153 ? 156 : cp == 0x017E ? 158 : cp == 0x0178 ? 159 : cp; cp = utf8state == UTF8_ACCEPT ? cp < (uint32_t)font->num_glyphs ? cp : 0x1A : 0x1A; if (cp == '\0') break; else if (cp == '\t') x += tab_width; else { draw_image_masked(font->glyphs[cp].image, color, x, y); x += font->glyphs[cp].width; } } al_hold_bitmap_drawing(false); }