Issue 346: Basic support for multi-line text with bitmap fonts: use alt-enter to type carriage returns (like Excel)
git-svn-id: svn://pulkomandy.tk/GrafX2/trunk@1737 416bcca6-2ee7-4201-b75f-2eb2f807beb1
| 
		 Before Width: | Height: | Size: 2.6 KiB After Width: | Height: | Size: 2.6 KiB  | 
| 
		 Before Width: | Height: | Size: 2.6 KiB After Width: | Height: | Size: 2.6 KiB  | 
| 
		 Before Width: | Height: | Size: 2.6 KiB After Width: | Height: | Size: 2.6 KiB  | 
| 
		 Before Width: | Height: | Size: 2.7 KiB After Width: | Height: | Size: 2.7 KiB  | 
| 
		 Before Width: | Height: | Size: 2.7 KiB After Width: | Height: | Size: 2.7 KiB  | 
| 
		 Before Width: | Height: | Size: 2.5 KiB After Width: | Height: | Size: 2.5 KiB  | 
| 
		 Before Width: | Height: | Size: 19 KiB After Width: | Height: | Size: 19 KiB  | 
| 
		 Before Width: | Height: | Size: 20 KiB After Width: | Height: | Size: 20 KiB  | 
							
								
								
									
										41
									
								
								src/SFont.c
									
									
									
									
									
								
							
							
						
						@ -145,8 +145,13 @@ void SFont_Write(SDL_Surface *Surface, const SFont_Font *Font,
 | 
				
			|||||||
    srcrect.h = dstrect.h = Font->Surface->h - 1;
 | 
					    srcrect.h = dstrect.h = Font->Surface->h - 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for(c = text; *c != '\0' && x <= Surface->w ; c++) {
 | 
					    for(c = text; *c != '\0' && x <= Surface->w ; c++) {
 | 
				
			||||||
 | 
					        if (*c == '\n') {
 | 
				
			||||||
 | 
					          dstrect.y += Font->Surface->h-1;
 | 
				
			||||||
 | 
					          x=0;
 | 
				
			||||||
 | 
					          continue;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
        // skip spaces and nonprintable characters
 | 
					        // skip spaces and nonprintable characters
 | 
				
			||||||
        if (*c == ' ' || Font->CharWidth[(int)*c]==0) {
 | 
					        else if (*c == ' ' || Font->CharWidth[(int)*c]==0) {
 | 
				
			||||||
            x += Font->Space;
 | 
					            x += Font->Space;
 | 
				
			||||||
            continue;
 | 
					            continue;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@ -166,13 +171,23 @@ int SFont_TextWidth(const SFont_Font *Font, const char *text)
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
    const char* c;
 | 
					    const char* c;
 | 
				
			||||||
    int width = 0;
 | 
					    int width = 0;
 | 
				
			||||||
 | 
					    int previous_width = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if(text == NULL)
 | 
					    if(text == NULL)
 | 
				
			||||||
        return 0;
 | 
					        return 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for(c = text; *c != '\0'; c++) {
 | 
					    for(c = text; *c != '\0'; c++)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        if (*c == '\n')
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					          if (previous_width<width)
 | 
				
			||||||
 | 
					            previous_width=width;
 | 
				
			||||||
 | 
					          width=0;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
        // skip spaces and nonprintable characters
 | 
					        // skip spaces and nonprintable characters
 | 
				
			||||||
        if (*c == ' ' || Font->CharWidth[(int)*c]==0) {
 | 
					        if (*c == ' ' || Font->CharWidth[(int)*c]==0)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
            width += Font->Space;
 | 
					            width += Font->Space;
 | 
				
			||||||
            continue;
 | 
					            continue;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@ -180,18 +195,30 @@ int SFont_TextWidth(const SFont_Font *Font, const char *text)
 | 
				
			|||||||
        width += Font->CharWidth[(int)*c];
 | 
					        width += Font->CharWidth[(int)*c];
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return width;
 | 
					    return previous_width<width ? width : previous_width;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int SFont_TextHeight(const SFont_Font* Font)
 | 
					int SFont_TextHeight(const SFont_Font* Font, const char *text)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    return Font->Surface->h - 1;
 | 
					    // Count occurences of '\n'
 | 
				
			||||||
 | 
					    int nb_cr=0;
 | 
				
			||||||
 | 
					    while (*text!='\0')
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      if (*text=='\n')
 | 
				
			||||||
 | 
					        nb_cr++;
 | 
				
			||||||
 | 
					      text++;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    return (Font->Surface->h - 1) * (nb_cr+1);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					// Do not use: Doesn't implement carriage returns
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void SFont_WriteCenter(SDL_Surface *Surface, const SFont_Font *Font,
 | 
					void SFont_WriteCenter(SDL_Surface *Surface, const SFont_Font *Font,
 | 
				
			||||||
                       int y, const char *text)
 | 
					                       int y, const char *text)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    SFont_Write(Surface, Font, Surface->w/2 - SFont_TextWidth(Font, text)/2,
 | 
					    SFont_Write(Surface, Font, Surface->w/2 - SFont_TextWidth(Font, text)/2,
 | 
				
			||||||
                y, text);
 | 
					                y, text);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					*/
 | 
				
			||||||
 | 
				
			|||||||
@ -87,7 +87,7 @@ void SFont_Write(SDL_Surface *Surface, const SFont_Font *Font, int x, int y,
 | 
				
			|||||||
/// Returns the width of "text" in pixels
 | 
					/// Returns the width of "text" in pixels
 | 
				
			||||||
int SFont_TextWidth(const SFont_Font* Font, const char *text);
 | 
					int SFont_TextWidth(const SFont_Font* Font, const char *text);
 | 
				
			||||||
/// Returns the height of "text" in pixels (which is always equal to Font->Surface->h)
 | 
					/// Returns the height of "text" in pixels (which is always equal to Font->Surface->h)
 | 
				
			||||||
int SFont_TextHeight(const SFont_Font* Font);
 | 
					int SFont_TextHeight(const SFont_Font* Font, const char *text);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Blits a string to Surface with centered x position
 | 
					/// Blits a string to Surface with centered x position
 | 
				
			||||||
void SFont_WriteCenter(SDL_Surface *Surface, const SFont_Font* Font, int y,
 | 
					void SFont_WriteCenter(SDL_Surface *Surface, const SFont_Font* Font, int y,
 | 
				
			||||||
 | 
				
			|||||||
@ -601,6 +601,7 @@ word Keysym_to_ANSI(SDL_keysym keysym)
 | 
				
			|||||||
  #if !(defined(__macosx__) || defined(__FreeBSD__))
 | 
					  #if !(defined(__macosx__) || defined(__FreeBSD__))
 | 
				
			||||||
  if ( keysym.unicode == 0)
 | 
					  if ( keysym.unicode == 0)
 | 
				
			||||||
  {
 | 
					  {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    switch(keysym.sym)
 | 
					    switch(keysym.sym)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
      case SDLK_DELETE:
 | 
					      case SDLK_DELETE:
 | 
				
			||||||
@ -610,7 +611,11 @@ word Keysym_to_ANSI(SDL_keysym keysym)
 | 
				
			|||||||
      case SDLK_END:
 | 
					      case SDLK_END:
 | 
				
			||||||
      case SDLK_BACKSPACE:
 | 
					      case SDLK_BACKSPACE:
 | 
				
			||||||
      case KEY_ESC:
 | 
					      case KEY_ESC:
 | 
				
			||||||
 | 
					        return keysym.sym;
 | 
				
			||||||
      case SDLK_RETURN:
 | 
					      case SDLK_RETURN:
 | 
				
			||||||
 | 
					        // Case alt-enter
 | 
				
			||||||
 | 
					        if (SDL_GetModState() & (KMOD_ALT|KMOD_META))
 | 
				
			||||||
 | 
					          return '\n';
 | 
				
			||||||
        return keysym.sym;
 | 
					        return keysym.sym;
 | 
				
			||||||
      default:
 | 
					      default:
 | 
				
			||||||
        return 0;      
 | 
					        return 0;      
 | 
				
			||||||
 | 
				
			|||||||
@ -509,7 +509,7 @@ byte Readline_ex(word x_pos,word y_pos,char * str,byte visible_size,byte max_siz
 | 
				
			|||||||
          switch(input_type)
 | 
					          switch(input_type)
 | 
				
			||||||
          {
 | 
					          {
 | 
				
			||||||
            case INPUT_TYPE_STRING :
 | 
					            case INPUT_TYPE_STRING :
 | 
				
			||||||
              if (input_key>=' ' && input_key<= 255)
 | 
					              if ((input_key>=' ' && input_key<= 255)||input_key=='\n')
 | 
				
			||||||
                is_authorized=1;
 | 
					                is_authorized=1;
 | 
				
			||||||
              break;
 | 
					              break;
 | 
				
			||||||
            case INPUT_TYPE_INTEGER :
 | 
					            case INPUT_TYPE_INTEGER :
 | 
				
			||||||
 | 
				
			|||||||
@ -567,7 +567,7 @@ byte *Render_text_SFont(const char *str, int font_number, int *width, int *heigh
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
  
 | 
					  
 | 
				
			||||||
  // Calcul des dimensions
 | 
					  // Calcul des dimensions
 | 
				
			||||||
  *height=SFont_TextHeight(font);
 | 
					  *height=SFont_TextHeight(font, str);
 | 
				
			||||||
  *width=SFont_TextWidth(font, str);
 | 
					  *width=SFont_TextWidth(font, str);
 | 
				
			||||||
  // Allocation d'une surface SDL
 | 
					  // Allocation d'une surface SDL
 | 
				
			||||||
  text_surface=SDL_CreateRGBSurface(SDL_SWSURFACE, *width, *height, 8, 0, 0, 0, 0);
 | 
					  text_surface=SDL_CreateRGBSurface(SDL_SWSURFACE, *width, *height, 8, 0, 0, 0, 0);
 | 
				
			||||||
 | 
				
			|||||||