Re: Add a RAII wrapper for extracting FT_Face from PangoFcFont (issue 549560043 by hanwenn@gmail.com)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Re: Add a RAII wrapper for extracting FT_Face from PangoFcFont (issue 549560043 by hanwenn@gmail.com)

Han-Wen Nienhuys-3
Reviewers: hahnjo, lemzwerg,

Message:
commit 9cf8d35e8c0ebf0f2b56ec74d3b0187b2cd493b8
Author: Han-Wen Nienhuys <[hidden email]>
Date:   Thu Feb 13 14:26:55 2020 +0100

    Add a RAII wrapper for extracting FT_Face from PangoFcFont
   


Description:
Add a RAII wrapper for extracting FT_Face from PangoFcFont

This reduces the number of deprecation warnings for
pango_fc_font_[un]lock_face().

Please review this at https://codereview.appspot.com/549560043/

Affected files (+28, -10 lines):
  M lily/pango-font.cc


Index: lily/pango-font.cc
diff --git a/lily/pango-font.cc b/lily/pango-font.cc
index c99e0f92ed421057fdd4948bb8becc26f3e320a4..30df01a199f4bbeb51cc05f9eb0b3680d1627b67 100644
--- a/lily/pango-font.cc
+++ b/lily/pango-font.cc
@@ -48,6 +48,29 @@
 
 using std::string;
 
+// RAII for extracting FT_Face from PangoFcFont
+class FTFace_accessor
+{
+  PangoFcFont *pango_font_;
+  FT_Face face_;
+
+public:
+  operator FT_Face () { return face_; }
+  FT_Face operator-> () { return face_; }
+  FTFace_accessor (PangoFcFont *pango_font)
+  {
+    pango_font_ = pango_font;
+    // This is deprecated in Pango 1.44, but we still support 1.36.
+    face_ = pango_fc_font_lock_face (pango_font);
+  }
+
+  ~FTFace_accessor ()
+  {
+    // Idem.
+    pango_fc_font_unlock_face (pango_font_);
+  }
+};
+
 Preinit_Pango_font::Preinit_Pango_font ()
 {
   physical_font_tab_ = SCM_EOL;
@@ -102,10 +125,9 @@ size_t
 Pango_font::name_to_index (string nm) const
 {
   PangoFcFont *fcfont = PANGO_FC_FONT (pango_context_load_font (context_, pango_description_));
-  FT_Face face = pango_fc_font_lock_face (fcfont);
+  FTFace_accessor face (fcfont);
   char *nm_str = (char *) nm.c_str ();
   FT_UInt idx = FT_Get_Name_Index (face, nm_str);
-  pango_fc_font_unlock_face (fcfont);
   return (idx != 0) ? idx : GLYPH_INDEX_INVALID;
 }
 
@@ -151,9 +173,8 @@ Box
 Pango_font::get_unscaled_indexed_char_dimensions (size_t signed_idx) const
 {
   PangoFcFont *fcfont = PANGO_FC_FONT (pango_context_load_font (context_, pango_description_));
-  FT_Face face = pango_fc_font_lock_face (fcfont);
+  FTFace_accessor face (fcfont);
   Box b = ly_FT_get_unscaled_indexed_char_dimensions (face, signed_idx);
-  pango_fc_font_unlock_face (fcfont);
   return b;
 }
 
@@ -177,9 +198,8 @@ Box
 Pango_font::get_glyph_outline_bbox (size_t signed_idx) const
 {
   PangoFcFont *fcfont = PANGO_FC_FONT (pango_context_load_font (context_, pango_description_));
-  FT_Face face = pango_fc_font_lock_face (fcfont);
+  FTFace_accessor face (fcfont);
   Box b = ly_FT_get_glyph_outline_bbox (face, signed_idx);
-  pango_fc_font_unlock_face (fcfont);
   return b;
 }
 
@@ -187,9 +207,8 @@ SCM
 Pango_font::get_glyph_outline (size_t signed_idx) const
 {
   PangoFcFont *fcfont = PANGO_FC_FONT (pango_context_load_font (context_, pango_description_));
-  FT_Face face = pango_fc_font_lock_face (fcfont);
+  FTFace_accessor face (fcfont);
   SCM s = ly_FT_get_glyph_outline (face, signed_idx);
-  pango_fc_font_unlock_face (fcfont);
   return s;
 }
 
@@ -207,7 +226,7 @@ Pango_font::pango_item_string_stencil (PangoGlyphItem const *glyph_item) const
   pango_glyph_string_extents (pgs, pa->font, &ink_rect, &logical_rect);
 
   PangoFcFont *fcfont = PANGO_FC_FONT (pa->font);
-  FT_Face ftface = pango_fc_font_lock_face (fcfont);
+  FTFace_accessor ftface (fcfont);
 
   Box b (Interval (PANGO_LBEARING (logical_rect),
                    PANGO_RBEARING (logical_rect)),
@@ -329,7 +348,6 @@ Pango_font::pango_item_string_stencil (PangoGlyphItem const *glyph_item) const
                         SCM_EOL);
       tail = SCM_CDRLOC (*tail);
     }
-  pango_fc_font_unlock_face (fcfont);
   pango_glyph_string_free (pgs);
   pgs = 0;
   PangoFontDescription *descr = pango_font_describe (pa->font);