Author: cazfi
Date: Thu Jan 22 20:01:32 2015
New Revision: 27776

URL: http://svn.gna.org/viewcvs/freeciv?rev=27776&view=rev
Log:
Added more utility functions for handling utf8 to sdl2-client.

See patch #5726

Modified:
    branches/S2_6/client/gui-sdl2/gui_string.c
    branches/S2_6/client/gui-sdl2/gui_string.h

Modified: branches/S2_6/client/gui-sdl2/gui_string.c
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/client/gui-sdl2/gui_string.c?rev=27776&r1=27775&r2=27776&view=diff
==============================================================================
--- branches/S2_6/client/gui-sdl2/gui_string.c  (original)
+++ branches/S2_6/client/gui-sdl2/gui_string.c  Thu Jan 22 20:01:32 2015
@@ -258,11 +258,45 @@
 }
 
 /**************************************************************************
+  Create utf8_str struct with ptsize font.
+  Font will be loaded or aliased with existing font of that size.
+  pInTextString must be allocated in memory (MALLOC/fc_calloc)
+**************************************************************************/
+utf8_str *create_utf8_str(char *in_text, size_t n_alloc, Uint16 ptsize)
+{
+  utf8_str *str = fc_calloc(1, sizeof(utf8_str));
+
+  if (!ptsize) {
+    str->ptsize = theme_default_font_size(theme);
+  } else {
+    str->ptsize = ptsize;
+  }
+
+  if ((str->font = load_font(str->ptsize)) == NULL) {
+    log_error("create_utf8_str(): load_font failed");
+    FC_FREE(str);
+
+    return NULL;
+  }
+
+  str->style = TTF_STYLE_NORMAL;
+  str->bgcol = (SDL_Color) {0, 0, 0, 0};
+  str->fgcol = *get_theme_color(COLOR_THEME_TEXT);
+  str->render = 2;
+
+  /* pInTextString must be allocated in memory (MALLOC/fc_calloc) */
+  str->text = in_text;
+  str->n_alloc = n_alloc;
+
+  return str;
+}
+
+/**************************************************************************
   Convert char array to SDL_String16. Pointer to target string is needed
   as parameter, but also returned for convenience.
 **************************************************************************/
-SDL_String16 * copy_chars_to_string16(SDL_String16 *pString,
-                                      const char *pCharString)
+SDL_String16 *copy_chars_to_string16(SDL_String16 *pString,
+                                     const char *pCharString)
 {
   size_t n;
 
@@ -282,6 +316,32 @@
   convertcopy_to_utf16(pString->text, pString->n_alloc, pCharString);
 
   return pString;
+}
+
+/**************************************************************************
+  Convert char array to utf8_str. Pointer to target string is needed
+  as parameter, but also returned for convenience.
+**************************************************************************/
+utf8_str *copy_chars_to_utf8_str(utf8_str *pstr, const char *pchars)
+{
+  size_t n;
+
+  fc_assert_ret_val(pstr != NULL, NULL);
+  fc_assert_ret_val(pchars != NULL, NULL);
+
+  n = (strlen(pchars) + 1) * 2;
+
+  if (n > pstr->n_alloc) {
+    /* allocated more if this is only a small increase on before: */
+    size_t n1 = (3 * pstr->n_alloc) / 2;
+
+    pstr->n_alloc = (n > n1) ? n : n1;
+    pstr->text = fc_realloc(pstr->text, pstr->n_alloc);
+  }
+
+  fc_snprintf(pstr->text, pstr->n_alloc, "%s", pchars);
+
+  return pstr;
 }
 
 /**************************************************************************

Modified: branches/S2_6/client/gui-sdl2/gui_string.h
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/client/gui-sdl2/gui_string.h?rev=27776&r1=27775&r2=27776&view=diff
==============================================================================
--- branches/S2_6/client/gui-sdl2/gui_string.h  (original)
+++ branches/S2_6/client/gui-sdl2/gui_string.h  Thu Jan 22 20:01:32 2015
@@ -79,6 +79,8 @@
 SDL_Rect str16size(SDL_String16 *pString16);
 void change_ptsize16(SDL_String16 *pString, Uint16 new_ptsize);
 
+utf8_str *create_utf8_str(char *in_text, size_t n_alloc, Uint16 ptsize);
+utf8_str *copy_chars_to_utf8_str(utf8_str *pstr, const char *pchars);
 bool convert_utf8_str_to_const_surface_width(utf8_str *pstr,
                                              int width);
 int write_utf8(SDL_Surface *dest, Sint16 x, Sint16 y,
@@ -101,8 +103,10 @@
 #define str16len(pString16) str16size(pString16).w
 #define str16height(pString16) str16size(pString16).h
 
+#define utf8_str_height(pstr) utf8_str_size(pstr).h
+
 /*
- *     here we use ordinary free( ... ) becouse check is made 
+ *     here we use ordinary free( ... ) because check is made 
  *     on start.
  */
 #define FREESTRING16( pString16 )              \
@@ -115,7 +119,27 @@
        }                                       \
 } while (FALSE)
 
+/*
+ *     here we use ordinary free( ... ) because check is made 
+ *     on start.
+ */
+#define FREEUTF8STR( pstr )             \
+  do {                                  \
+    if (pstr != NULL) {                 \
+      FC_FREE(pstr->text);              \
+      unload_font(pstr->ptsize);        \
+      free(pstr);                       \
+      pstr = NULL;                      \
+    }                                   \
+} while (FALSE)
+
 #define create_str16_from_char(pInCharString, iPtsize) \
-  copy_chars_to_string16(create_string16(NULL, 0,iPtsize), pInCharString)
+  copy_chars_to_string16(create_string16(NULL, 0, iPtsize), pInCharString)
+
+#define create_utf8_from_char(string_in, ptsize) \
+  (string_in) == NULL ?                          \
+    create_utf8_str(NULL, 0, ptsize) :           \
+    copy_chars_to_utf8_str(create_utf8_str(NULL, 0, ptsize), string_in)
+      
 
 #endif /* FC__GUISTRING_H */


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to