void SoFont_PutStringCleverCursor (SoFont * font, SDL_Surface * Surface, const char *text, int cursPos, SDL_Rect * r, SDL_Rect * clip, int showCurs) { int w1, w2; if ((!font->picture) || (!text)) return; w1 = SoFont_TextWidth_MinMax (font, text, 0, cursPos); w2 = SoFont_TextWidth (font, text); if ((w2 < r->w) || (w1 < r->w / 2)) SoFont_PutStringWithCursor (font, Surface, r->x, r->y + (r->h - font->height) / 2, text, cursPos, clip, showCurs); else if (w1 < w2 - r->w / 2) SoFont_PutStringWithCursor (font, Surface, r->x - w1 + r->w / 2, r->y + (r->h - font->height) / 2, text, cursPos, clip, showCurs); else SoFont_PutStringWithCursor (font, Surface, r->x - w2 + r->w, r->y + (r->h - font->height) / 2, text, cursPos, clip, showCurs); }
void SoFont_CenteredString_XY (SoFont * font, SDL_Surface * Surface, int x, int y, const char *text, SDL_Rect * clip) { if (!font->picture) return; SoFont_PutString (font, Surface, x - SoFont_TextWidth (font, text) / 2, y - font->height / 2, text, clip); }
void SoFont_XCenteredString (SoFont * font, SDL_Surface * Surface, int y, const char *text, SDL_Rect * clip) { if (!font->picture) return; SoFont_PutString (font, Surface, Surface->w / 2 - SoFont_TextWidth (font, text) / 2, y, text, clip); }
int SoFont_CleverTextCursorAt (SoFont * font, const char *text, int px, int cursPos, SDL_Rect * r) { int w1, w2; if ((!font->picture) || (!text)) return 0; w1 = SoFont_TextWidth_MinMax (font, text, 0, cursPos); w2 = SoFont_TextWidth (font, text); if ((w2 < r->w) || (w1 < r->w / 2)) return SoFont_TextCursorAt (font, text, px); else if (w1 < w2 - r->w / 2) return SoFont_TextCursorAt (font, text, px + w1 - (r->w / 2)); else return SoFont_TextCursorAt (font, text, px + w2 - r->w); }
int SoFont_load (SoFont * font, IIM_Surface * FontSurface) { int x = 0, i = 0, p = 0, s = 0; int cursBegin = 0; int cursWidth = 0; int _CharPos[256]; int _Spacing[256]; if (!FontSurface) { fprintf (stderr, "SoFont recieved a NULL SDL_Surface\n"); return 0; } font->picture = FontSurface; font->height = font->picture->h - 1; while (x < font->picture->w) { if (SoFont_DoStartNewChar (font, x)) { if (i) _Spacing[i - 1] = 1 + x - s; p = x; while ((x < font->picture->w - 1) && (SoFont_DoStartNewChar (font, x))) x++; s = x; // CharPos[i++] = (p + x) / 2; _CharPos[i++] = (p + x + 1) / 2; // David, Kobo Deluxe } x++; } // Note that spacing is not needed for the last char, // as it's just used for the blit width calculation. if (i) _Spacing[i - 1] = 1 + x - s; _Spacing[i] = 0; _CharPos[i++] = font->picture->w; font->max_i = START_CHAR + i - 1; font->background = SoFontGetPixel (font->picture->surf, 0, font->height); SDL_SetColorKey (font->picture->surf, SDL_SRCCOLORKEY, font->background); SoFont_CleanSurface (font); font->CharPos = (int *) malloc (i * sizeof (int)); font->Spacing = (int *) malloc (i * sizeof (int)); memcpy (font->CharPos, _CharPos, i * sizeof (int)); memcpy (font->Spacing, _Spacing, i * sizeof (int)); // We search for a smart space width: // Changed from "a", "A", "0" for Kobo Deluxe. // Spaces were *way* to wide! //David font->spacew = 0; if (!font->spacew) font->spacew = SoFont_TextWidth (font, "i") * 3 / 2; if (!font->spacew) font->spacew = SoFont_TextWidth (font, "I") * 3 / 2; if (!font->spacew) font->spacew = SoFont_TextWidth (font, ".") * 3 / 2; if (!font->spacew) font->spacew = font->CharPos[1] - font->CharPos[0]; // We search for a smart cursor position: font->cursShift = 0; if ('|' > font->max_i) return 1; // No bar in this font! if (font->background == SoFontGetPixel (font->picture->surf, font->CharPos['|' - START_CHAR], font->height / 2)) { // Up to the first | color for (cursBegin = 0; cursBegin <= SoFont_TextWidth (font, "|"); cursBegin++) if (font->background != SoFontGetPixel (font->picture->surf, font->CharPos['|' - START_CHAR] + cursBegin, font->height / 2)) break; // Up to the end of the | color for (cursWidth = 0; cursWidth <= SoFont_TextWidth (font, "|"); cursWidth++) if (font->background == SoFontGetPixel (font->picture->surf, font->CharPos['|' - START_CHAR] + cursBegin + cursWidth, font->height / 2)) break; } else { // Up to the end of the | color for (cursWidth = 0; cursWidth <= SoFont_TextWidth (font, "|"); cursWidth++) if (font->background == SoFontGetPixel (font->picture->surf, font->CharPos['|' - START_CHAR] + cursWidth, font->height / 2)) break; } font->cursShift = cursBegin + 1; // cursWidth could be used if // image format changes. return 1; }
void menu_draw (Menu * menu, SDL_Surface * surf) { if (menu->visible) { int c; // compteur d'items int center = surf->w / 2; int hauteur = (surf->h - menu->hauteur) / 2; hauteur += menu->hide; for (c = 0; c < menu->nb_items; c++) { SDL_Surface *item_surf = menu->items[c].bitmap; SoFont *item_font = menu->items[c].font; if (item_surf || item_font) { int center_x; center_x = (int)(center + menu->items[c].priv_x); if (item_surf != NULL) { SDL_Rect rect; rect.x = center_x - item_surf->w / 2; rect.y = hauteur; rect.w = item_surf->w; rect.h = item_surf->h; SDL_BlitSurface (item_surf, NULL, surf, &rect); hauteur += item_surf->h; } else { // font.. static char item_name[1024]; if (c == menu->active_item) { SDL_Rect rect; if (menu->menuselector==NULL) { rect.x = center - 140; rect.w = 280; rect.y = hauteur; rect.h = SoFont_FontHeight (item_font); SDL_FillRect (surf, &rect, 0); rect.x = center - 150; rect.w = 300; rect.y = hauteur + 3; rect.h = SoFont_FontHeight (item_font) - 6; SDL_FillRect (surf, &rect, 0); } else { rect.x = center - 150; rect.w = menu->menuselector->w; rect.y = hauteur - 3; rect.h = menu->menuselector->h; SDL_BlitSurface (menu->menuselector->surf, NULL, surf, &rect); } } strcpy (item_name, menu->items[c].name); if (menu->items[c].value) { strcat (item_name, menu->items[c].value); } char *pc = item_name; int nbTab = 0; for (; *pc; pc++) { if (*pc == '\t') { while (*pc == '\t') { *pc = 0; pc ++; nbTab ++; } break; } } if (nbTab > 0) { int w = 80 + nbTab * 50; SoFont_PutString (item_font, surf, center_x-w, hauteur, item_name, NULL); SoFont_PutString (item_font, surf, center_x+w - SoFont_TextWidth(item_font, pc), hauteur, pc, NULL); } else { SoFont_PutString (item_font, surf, center_x - SoFont_TextWidth (item_font, item_name) / 2, hauteur, item_name, NULL); } hauteur += SoFont_FontHeight (item_font); } } else hauteur += menu->sep_size; } } }