[Bf-blender-cvs] [db4804690b8] master: BLF: pass code-point to BLF_has_glyph

Campbell Barton noreply at git.blender.org
Thu May 2 03:02:58 CEST 2019


Commit: db4804690b84985b62941833175dd6b540d4cb0f
Author: Campbell Barton
Date:   Thu May 2 10:52:53 2019 +1000
Branches: master
https://developer.blender.org/rBdb4804690b84985b62941833175dd6b540d4cb0f

BLF: pass code-point to BLF_has_glyph

Avoid BLF having to be concerned with decoding the string
(which can fail).

Also remove redundant extra zero byte from strings.

===================================================================

M	source/blender/blenfont/BLF_api.h
M	source/blender/blenfont/intern/blf.c
M	source/blender/windowmanager/intern/wm_keymap.c

===================================================================

diff --git a/source/blender/blenfont/BLF_api.h b/source/blender/blenfont/BLF_api.h
index 359ec9cc49e..448bb0d621a 100644
--- a/source/blender/blenfont/BLF_api.h
+++ b/source/blender/blenfont/BLF_api.h
@@ -53,8 +53,8 @@ int BLF_load_mem_unique(const char *name, const unsigned char *mem, int mem_size
 void BLF_unload(const char *name) ATTR_NONNULL();
 void BLF_unload_id(int fontid);
 
-/* Check if font supports a particular glyph */
-bool BLF_has_glyph(int fontid, const char *utf8);
+/* Check if font supports a particular glyph. */
+bool BLF_has_glyph(int fontid, unsigned int unicode);
 
 /* Attach a file with metrics information from memory. */
 void BLF_metrics_attach(int fontid, unsigned char *mem, int mem_size);
diff --git a/source/blender/blenfont/intern/blf.c b/source/blender/blenfont/intern/blf.c
index 17799aea14d..793e9805899 100644
--- a/source/blender/blenfont/intern/blf.c
+++ b/source/blender/blenfont/intern/blf.c
@@ -41,7 +41,6 @@
 #include "DNA_vec_types.h"
 
 #include "BLI_math.h"
-#include "BLI_string_utf8.h"
 #include "BLI_threads.h"
 
 #include "BLF_api.h"
@@ -189,11 +188,10 @@ int BLF_default(void)
   return global_font_default;
 }
 
-bool BLF_has_glyph(int fontid, const char *utf8)
+bool BLF_has_glyph(int fontid, unsigned int unicode)
 {
   FontBLF *font = blf_get(fontid);
   if (font) {
-    unsigned int unicode = BLI_str_utf8_as_unicode(utf8);
     return FT_Get_Char_Index(font->face, unicode) != 0;
   }
   return false;
diff --git a/source/blender/windowmanager/intern/wm_keymap.c b/source/blender/windowmanager/intern/wm_keymap.c
index 1ea667625ec..8f048c6334f 100644
--- a/source/blender/windowmanager/intern/wm_keymap.c
+++ b/source/blender/windowmanager/intern/wm_keymap.c
@@ -1051,67 +1051,89 @@ static void wm_user_modal_keymap_set_items(wmWindowManager *wm, wmKeyMap *km)
 /* ***************** get string from key events **************** */
 
 /* if try_unicode see if fancy glyph is in the font, otherwise return text fallback */
-static const char *key_event_icon_or_text(const bool try_unicode,
-                                          const int font_id,
-                                          const char *icon,
-                                          const char *text)
+static const char *key_event_icon_or_text(const int font_id, const char *text, const char *icon)
 {
-  return (try_unicode && BLF_has_glyph(font_id, icon)) ? icon : text;
+  BLI_assert(icon == NULL || (BLI_strlen_utf8(icon) == 1));
+  return (icon && BLF_has_glyph(font_id, BLI_str_utf8_as_unicode(icon))) ? icon : text;
 }
 
 const char *WM_key_event_string(const short type, const bool compact)
 {
   if (compact) {
+    /* String storing a single unicode character or NULL. */
+    const char *icon_glyph = NULL;
     int font_id = BLF_default();
-    bool is_macos = false;
-    bool is_windows = false;
-
-#ifdef __APPLE__
-    is_macos = true;
-#endif
-#ifdef WIN32
-    is_windows = true;
+    const enum {
+      UNIX,
+      MACOS,
+      MSWIN,
+    } platform =
+
+#if defined(__APPLE__)
+        MACOS
+#elif defined(_WIN32)
+        MSWIN
+#else
+        UNIX
 #endif
+        ;
 
     switch (type) {
       case LEFTSHIFTKEY:
-      case RIGHTSHIFTKEY:
-        return key_event_icon_or_text(is_macos, font_id, "\xe2\x87\xa7\x00", IFACE_("Shift"));
+      case RIGHTSHIFTKEY: {
+        if (platform == MACOS) {
+          icon_glyph = "\xe2\x87\xa7";
+        }
+        return key_event_icon_or_text(font_id, IFACE_("Shift"), icon_glyph);
+      }
       case LEFTCTRLKEY:
       case RIGHTCTRLKEY:
-        return (is_macos) ? "^" : IFACE_("Ctrl");
+        if (platform == MACOS) {
+          return "^";
+        }
+        return IFACE_("Ctrl");
       case LEFTALTKEY:
-      case RIGHTALTKEY:
-        return key_event_icon_or_text(is_macos, font_id, "\xe2\x8c\xa5\x00", IFACE_("Alt"));
-      case OSKEY: {
-        if (is_macos) {
-          return key_event_icon_or_text(true, font_id, "\xe2\x8c\x98\x00", IFACE_("Cmd"));
+      case RIGHTALTKEY: {
+        if (platform == MACOS) {
+          icon_glyph = "\xe2\x8c\xa5";
         }
-        else if (is_windows) {
-          return key_event_icon_or_text(true, font_id, "\xe2\x8a\x9e\x00", IFACE_("Win"));
+        return key_event_icon_or_text(font_id, IFACE_("Alt"), icon_glyph);
+      }
+      case OSKEY: {
+        if (platform == MACOS) {
+          return key_event_icon_or_text(font_id, IFACE_("Cmd"), "\xe2\x8c\x98");
         }
-        else {
-          return IFACE_("OSkey");
+        else if (platform == MSWIN) {
+          return key_event_icon_or_text(font_id, IFACE_("Win"), "\xe2\x8a\x9e");
         }
+        return IFACE_("OSkey");
       } break;
-      case TABKEY:
-        return key_event_icon_or_text(is_macos, font_id, "\xe2\x86\xb9\x00", IFACE_("Tab"));
+      case TABKEY: {
+        if (platform == MACOS) {
+          icon_glyph = "\xe2\x86\xb9";
+        }
+        return key_event_icon_or_text(font_id, IFACE_("Tab"), icon_glyph);
+      }
       case BACKSPACEKEY:
-        return key_event_icon_or_text(true, font_id, "\xe2\x8c\xab\x00", IFACE_("Bksp"));
+        return key_event_icon_or_text(font_id, IFACE_("Bksp"), "\xe2\x8c\xab");
       case ESCKEY:
-        return key_event_icon_or_text(false, font_id, "\xe2\x8e\x8b\x00", IFACE_("Esc"));
-      case RETKEY:
-        return key_event_icon_or_text(is_macos, font_id, "\xe2\x8f\x8e\x00", IFACE_("Enter"));
+        return key_event_icon_or_text(font_id, IFACE_("Esc"), NULL /* "\xe2\x8e\x8b" */);
+      case RETKEY: {
+        if (platform == MACOS) {
+          icon_glyph = "\xe2\x8f\x8e";
+        }
+        return key_event_icon_or_text(font_id, IFACE_("Enter"), icon_glyph);
+      }
       case SPACEKEY:
-        return key_event_icon_or_text(false, font_id, "\xe2\x90\xa3\x00", IFACE_("Space"));
+        return key_event_icon_or_text(font_id, IFACE_("Space"), NULL /* "\xe2\x90\xa3" */);
       case LEFTARROWKEY:
-        return key_event_icon_or_text(true, font_id, "\xe2\x86\x90\x00", IFACE_("Left"));
+        return key_event_icon_or_text(font_id, IFACE_("Left"), "\xe2\x86\x90");
       case UPARROWKEY:
-        return key_event_icon_or_text(true, font_id, "\xe2\x86\x91\x00", IFACE_("Up"));
+        return key_event_icon_or_text(font_id, IFACE_("Up"), "\xe2\x86\x91");
       case RIGHTARROWKEY:
-        return key_event_icon_or_text(true, font_id, "\xe2\x86\x92\x00", IFACE_("Right"));
+        return key_event_icon_or_text(font_id, IFACE_("Right"), "\xe2\x86\x92");
       case DOWNARROWKEY:
-        return key_event_icon_or_text(true, font_id, "\xe2\x86\x93\x00", IFACE_("Down"));
+        return key_event_icon_or_text(font_id, IFACE_("Down"), "\xe2\x86\x93");
     }
   }



More information about the Bf-blender-cvs mailing list