emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master 3ea1a6672b: Default to currently selected font in Haiku font dial


From: Po Lu
Subject: master 3ea1a6672b: Default to currently selected font in Haiku font dialogs
Date: Mon, 2 May 2022 01:50:10 -0400 (EDT)

branch: master
commit 3ea1a6672b1cc8c7ea505585e8687500014e524b
Author: Po Lu <luangruo@yahoo.com>
Commit: Po Lu <luangruo@yahoo.com>

    Default to currently selected font in Haiku font dialogs
    
    * src/haiku_font_support.cc (be_find_font_indices): New
    function.
    * src/haiku_support.cc (class EmacsFontSelectionDialog)
    (UpdateStylesForIndex, EmacsFontSelectionDialog): Allow
    specifying an initial font family and style.
    (be_select_font): New parameters `initial_family' and
    `initial_style'.
    * src/haiku_support.h: Update prototypes.
    
    * src/haikufont.c (haikufont_lisp_to_weight)
    (haikufont_lisp_to_slant, haikufont_lisp_to_width): Handle
    `regular'.
    (haikufont_pattern_from_object): New function.
    (haikufont_spec_or_entity_to_pattern): Fix coding style.
    (Fx_select_font): Compute indices based on currently selected
    font.
    (syms_of_haikufont): New defsyms.
---
 src/haiku_font_support.cc |  35 ++++++++++++++++
 src/haiku_support.cc      |  27 ++++++++++--
 src/haiku_support.h       |   4 +-
 src/haikufont.c           | 103 +++++++++++++++++++++++++++++++++++++++-------
 4 files changed, 149 insertions(+), 20 deletions(-)

diff --git a/src/haiku_font_support.cc b/src/haiku_font_support.cc
index 95a0db8ae6..de55ad2001 100644
--- a/src/haiku_font_support.cc
+++ b/src/haiku_font_support.cc
@@ -815,3 +815,38 @@ be_font_style_to_flags (char *style, struct 
haiku_font_pattern *pattern)
 
   font_style_to_flags (style, pattern);
 }
+
+int
+be_find_font_indices (struct haiku_font_pattern *pattern,
+                     int *family_index, int *style_index)
+{
+  int32 i, j, n_families, n_styles;
+  font_family family;
+  font_style style;
+  uint32 flags;
+
+  n_families = count_font_families ();
+
+  for (i = 0; i < n_families; ++i)
+    {
+      if (get_font_family (i, &family, &flags) == B_OK)
+       {
+         n_styles = count_font_styles (family);
+
+         for (j = 0; j < n_styles; ++j)
+           {
+             if (get_font_style (family, j, &style, &flags) == B_OK
+                 && font_family_style_matches_p (family, style,
+                                                 flags, pattern))
+               {
+                 *family_index = i;
+                 *style_index = j;
+
+                 return 0;
+               }
+           }
+       }
+    }
+
+  return 1;
+}
diff --git a/src/haiku_support.cc b/src/haiku_support.cc
index 9e31e1b870..12934faa1c 100644
--- a/src/haiku_support.cc
+++ b/src/haiku_support.cc
@@ -2448,6 +2448,7 @@ class EmacsFontSelectionDialog : public BWindow
   BTextControl size_entry;
   port_id comm_port;
   bool allow_monospace_only;
+  int pending_selection_idx;
 
   void
   UpdateStylesForIndex (int idx)
@@ -2479,6 +2480,13 @@ class EmacsFontSelectionDialog : public BWindow
          }
       }
 
+    if (pending_selection_idx >= 0)
+      {
+       font_style_pane.Select (pending_selection_idx);
+       font_style_pane.ScrollToSelection ();
+      }
+
+    pending_selection_idx = -1;
     UpdateForSelectedStyle ();
   }
 
@@ -2559,7 +2567,9 @@ public:
       delete_port (comm_port);
   }
 
-  EmacsFontSelectionDialog (bool monospace_only)
+  EmacsFontSelectionDialog (bool monospace_only,
+                           int initial_family_idx,
+                           int initial_style_idx)
     : BWindow (BRect (0, 0, 500, 500),
               "Select font from list",
               B_TITLED_WINDOW_LOOK,
@@ -2583,7 +2593,8 @@ public:
                     new BMessage (B_CANCEL)),
       ok_button ("OK", "OK", new BMessage (B_OK)),
       size_entry (NULL, "Size:", NULL, NULL),
-      allow_monospace_only (monospace_only)
+      allow_monospace_only (monospace_only),
+      pending_selection_idx (initial_style_idx)
   {
     BStringItem *family_item;
     int i, n_families;
@@ -2638,6 +2649,12 @@ public:
          }
       }
 
+    if (initial_family_idx >= 0)
+      {
+       font_family_pane.Select (initial_family_idx);
+       font_family_pane.ScrollToSelection ();
+      }
+
     size_text = size_entry.TextView ();
 
     for (c = 0; c <= 47; ++c)
@@ -4701,7 +4718,8 @@ be_select_font (void (*process_pending_signals_function) 
(void),
                bool (*should_quit_function) (void),
                haiku_font_family_or_style *family,
                haiku_font_family_or_style *style,
-               int *size, bool allow_monospace_only)
+               int *size, bool allow_monospace_only,
+               int initial_family, int initial_style)
 {
   EmacsFontSelectionDialog *dialog;
   struct font_selection_dialog_message msg;
@@ -4709,7 +4727,8 @@ be_select_font (void (*process_pending_signals_function) 
(void),
   font_family family_buffer;
   font_style style_buffer;
 
-  dialog = new EmacsFontSelectionDialog (allow_monospace_only);
+  dialog = new EmacsFontSelectionDialog (allow_monospace_only,
+                                        initial_family, initial_style);
   dialog->CenterOnScreen ();
 
   if (dialog->InitCheck () < B_OK)
diff --git a/src/haiku_support.h b/src/haiku_support.h
index 5522468fb3..efce63b478 100644
--- a/src/haiku_support.h
+++ b/src/haiku_support.h
@@ -661,7 +661,9 @@ extern bool be_replay_menu_bar_event (void *, struct 
haiku_menu_bar_click_event
 extern bool be_select_font (void (*) (void), bool (*) (void),
                            haiku_font_family_or_style *,
                            haiku_font_family_or_style *,
-                           int *, bool);
+                           int *, bool, int, int);
+
+extern int be_find_font_indices (struct haiku_font_pattern *, int *, int *);
 #ifdef __cplusplus
 }
 
diff --git a/src/haikufont.c b/src/haikufont.c
index 7f676b8727..3607012f6c 100644
--- a/src/haikufont.c
+++ b/src/haikufont.c
@@ -253,7 +253,7 @@ haikufont_lisp_to_weight (Lisp_Object weight)
     return HAIKU_LIGHT;
   if (EQ (weight, Qsemi_light))
     return HAIKU_SEMI_LIGHT;
-  if (EQ (weight, Qnormal))
+  if (EQ (weight, Qnormal) || EQ (weight, Qregular))
     return HAIKU_REGULAR;
   if (EQ (weight, Qsemi_bold))
     return HAIKU_SEMI_BOLD;
@@ -274,7 +274,7 @@ haikufont_lisp_to_weight (Lisp_Object weight)
   if (EQ (weight, Qmedium))
     return HAIKU_MEDIUM;
 
-  emacs_abort ();
+  return HAIKU_REGULAR;
 }
 
 static Lisp_Object
@@ -297,15 +297,16 @@ haikufont_slant_to_lisp (enum haiku_font_slant slant)
 static enum haiku_font_slant
 haikufont_lisp_to_slant (Lisp_Object slant)
 {
-  if (EQ (slant, Qitalic) ||
-      EQ (slant, Qreverse_italic))
+  if (EQ (slant, Qitalic)
+      || EQ (slant, Qreverse_italic))
     return SLANT_ITALIC;
-  if (EQ (slant, Qoblique) ||
-      EQ (slant, Qreverse_oblique))
+  if (EQ (slant, Qoblique)
+      || EQ (slant, Qreverse_oblique))
     return SLANT_OBLIQUE;
-  if (EQ (slant, Qnormal))
+  if (EQ (slant, Qnormal) || EQ (slant, Qregular))
     return SLANT_REGULAR;
-  emacs_abort ();
+
+  return SLANT_REGULAR;
 }
 
 static Lisp_Object
@@ -349,7 +350,7 @@ haikufont_lisp_to_width (Lisp_Object lisp)
     return CONDENSED;
   if (EQ (lisp, Qsemi_condensed))
     return SEMI_CONDENSED;
-  if (EQ (lisp, Qnormal))
+  if (EQ (lisp, Qnormal) || EQ (lisp, Qregular))
     return NORMAL_WIDTH;
   if (EQ (lisp, Qexpanded))
     return EXPANDED;
@@ -357,7 +358,8 @@ haikufont_lisp_to_width (Lisp_Object lisp)
     return EXTRA_EXPANDED;
   if (EQ (lisp, Qultra_expanded))
     return ULTRA_EXPANDED;
-  emacs_abort ();
+
+  return NORMAL_WIDTH;
 }
 
 static int
@@ -423,6 +425,56 @@ haikufont_pattern_to_entity (struct haiku_font_pattern 
*ptn)
   return ent;
 }
 
+static void
+haikufont_pattern_from_object (struct haiku_font_pattern *pattern,
+                              Lisp_Object font_object)
+{
+  Lisp_Object val;
+
+  pattern->specified = 0;
+
+  val = AREF (font_object, FONT_FAMILY_INDEX);
+  if (!NILP (val))
+    {
+      pattern->specified |= FSPEC_FAMILY;
+      strncpy ((char *) &pattern->family,
+              SSDATA (SYMBOL_NAME (val)),
+              sizeof pattern->family - 1);
+      pattern->family[sizeof pattern->family - 1] = '\0';
+    }
+
+  val = AREF (font_object, FONT_ADSTYLE_INDEX);
+  if (!NILP (val))
+    {
+      pattern->specified |= FSPEC_STYLE;
+      strncpy ((char *) &pattern->style,
+              SSDATA (SYMBOL_NAME (val)),
+              sizeof pattern->style - 1);
+      pattern->style[sizeof pattern->style - 1] = '\0';
+    }
+
+  val = FONT_WEIGHT_FOR_FACE (font_object);
+  if (!NILP (val) && !EQ (val, Qunspecified))
+    {
+      pattern->specified |= FSPEC_WEIGHT;
+      pattern->weight = haikufont_lisp_to_weight (val);
+    }
+
+  val = FONT_SLANT_FOR_FACE (font_object);
+  if (!NILP (val) && !EQ (val, Qunspecified))
+    {
+      pattern->specified |= FSPEC_SLANT;
+      pattern->slant = haikufont_lisp_to_slant (font_object);
+    }
+
+  val = FONT_WIDTH_FOR_FACE (font_object);
+  if (!NILP (val) && !EQ (val, Qunspecified))
+    {
+      pattern->specified |= FSPEC_WIDTH;
+      pattern->width = haikufont_lisp_to_width (font_object);
+    }
+}
+
 static void
 haikufont_spec_or_entity_to_pattern (Lisp_Object ent, int list_p,
                                     struct haiku_font_pattern *ptn)
@@ -469,8 +521,9 @@ haikufont_spec_or_entity_to_pattern (Lisp_Object ent, int 
list_p,
     }
 
   tem = AREF (ent, FONT_FAMILY_INDEX);
-  if (!NILP (tem) && !EQ (tem, Qunspecified) &&
-      (list_p && !haikufont_maybe_handle_special_family (tem, ptn)))
+  if (!NILP (tem) && !EQ (tem, Qunspecified)
+      && (list_p
+         && !haikufont_maybe_handle_special_family (tem, ptn)))
     {
       ptn->specified |= FSPEC_FAMILY;
       strncpy ((char *) &ptn->family,
@@ -1098,22 +1151,41 @@ If EXCLUDE-PROPORTIONAL is non-nil, exclude 
proportional fonts
 in the font selection dialog.  */)
   (Lisp_Object frame, Lisp_Object exclude_proportional)
 {
+  struct frame *f;
+  struct font *font;
+  Lisp_Object font_object;
   haiku_font_family_or_style family, style;
-  int rc, size;
+  int rc, size, initial_family, initial_style;
   struct haiku_font_pattern pattern;
   Lisp_Object lfamily, lweight, lslant, lwidth, ladstyle, lsize;
 
-  decode_window_system_frame (frame);
+  f = decode_window_system_frame (frame);
 
   if (popup_activated_p)
     error ("Trying to use a menu from within a menu-entry");
 
+  initial_style = -1;
+  initial_family = -1;
+
+  font = FRAME_FONT (f);
+
+  if (font)
+    {
+      XSETFONT (font_object, font);
+
+      haikufont_pattern_from_object (&pattern, font_object);
+      be_find_font_indices (&pattern, &initial_family,
+                           &initial_style);
+      haikufont_done_with_query_pattern (&pattern);
+    }
+
   popup_activated_p++;
   unrequest_sigio ();
   rc = be_select_font (process_pending_signals,
                       haikufont_should_quit_popup,
                       &family, &style, &size,
-                      !NILP (exclude_proportional));
+                      !NILP (exclude_proportional),
+                      initial_family, initial_style);
   request_sigio ();
   popup_activated_p--;
 
@@ -1161,6 +1233,7 @@ syms_of_haikufont (void)
   DEFSYM (Qexpanded, "expanded");
   DEFSYM (Qextra_expanded, "extra-expanded");
   DEFSYM (Qultra_expanded, "ultra-expanded");
+  DEFSYM (Qregular, "regular");
   DEFSYM (Qzh, "zh");
   DEFSYM (Qko, "ko");
   DEFSYM (Qjp, "jp");



reply via email to

[Prev in Thread] Current Thread [Next in Thread]