]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/pango-font.cc
Imported Upstream version 2.14.2
[lilypond.git] / lily / pango-font.cc
index d02b63ae7a01ac0a63358f637616d4733af0f4a6..a4fea75109bcb15141db97da016cd30d291375a2 100644 (file)
@@ -1,12 +1,26 @@
 /*
-  pango-font.cc -- implement Pango_font
+  This file is part of LilyPond, the GNU music typesetter.
 
-  source file of the GNU LilyPond music typesetter
+  Copyright (C) 2004--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
 
-  (c) 2004--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
+  LilyPond is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  LilyPond is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#define PANGO_ENABLE_BACKEND // ugh, why necessary?
+// Necessary for supporting pango_context_new() and
+// pango_context_set_font_map() in Pango < 1.22
+#define PANGO_ENABLE_BACKEND
+
 #include <pango/pangoft2.h>
 #include <freetype/ftxf86.h>
 
 #if HAVE_PANGO_FT2
 #include "stencil.hh"
 
-Pango_font::Pango_font (PangoFT2FontMap * /* fontmap */,
+Pango_font::Pango_font (PangoFT2FontMap *fontmap,
                        PangoFontDescription const *description,
                        Real output_scale)
 {
   physical_font_tab_ = scm_c_make_hash_table (11);
   PangoDirection pango_dir = PANGO_DIRECTION_LTR;
-  context_ = pango_ft2_get_context (PANGO_RESOLUTION,
-                                   PANGO_RESOLUTION);
+  context_ = pango_context_new ();
+  pango_context_set_font_map (context_, PANGO_FONT_MAP (fontmap));
 
   pango_description_ = pango_font_description_copy (description);
   attribute_list_ = pango_attr_list_new ();
@@ -97,35 +111,27 @@ get_unicode_name (char *s,
 }
 
 Stencil
-Pango_font::pango_item_string_stencil (PangoItem const *item,
-                                      string str,
-                                      bool tight_bbox) const
+Pango_font::pango_item_string_stencil (PangoGlyphItem const *glyph_item) const
 {
   const int GLYPH_NAME_LEN = 256;
   char glyph_name[GLYPH_NAME_LEN];
-  PangoAnalysis const *pa = &(item->analysis);
-  PangoGlyphString *pgs = pango_glyph_string_new ();
 
-  pango_shape (str.c_str () + item->offset,
-              item->length, (PangoAnalysis*) pa, pgs);
+  PangoAnalysis const *pa = &(glyph_item->item->analysis);
+  PangoGlyphString *pgs = glyph_item->glyphs;
 
   PangoRectangle logical_rect;
   PangoRectangle ink_rect;
   pango_glyph_string_extents (pgs, pa->font, &ink_rect, &logical_rect);
 
-  PangoFcFont *fcfont = G_TYPE_CHECK_INSTANCE_CAST (pa->font,
-                                                   PANGO_TYPE_FC_FONT,
-                                                   PangoFcFont);
+  PangoFcFont *fcfont = PANGO_FC_FONT (pa->font);
 
   FT_Face ftface = pango_fc_font_lock_face (fcfont);
 
-  PangoRectangle const *which_rect = tight_bbox ? &ink_rect
-                                               : &logical_rect;
-
   Box b (Interval (PANGO_LBEARING (logical_rect),
                   PANGO_RBEARING (logical_rect)),
-        Interval (-PANGO_DESCENT (*which_rect),
-                  PANGO_ASCENT (*which_rect)));
+        Interval (-PANGO_DESCENT (ink_rect),
+                  PANGO_ASCENT (ink_rect)));
+
   b.scale (scale_);
 
   char const *ps_name_str0 = FT_Get_Postscript_Name (ftface);
@@ -164,11 +170,18 @@ Pango_font::pango_item_string_stencil (PangoItem const *item,
       PangoGlyph pg = pgi->glyph;
       PangoGlyphGeometry ggeo = pgi->geometry;
 
+      /*
+       Zero-width characters are valid Unicode characters,
+       but glyph lookups need to be skipped.
+      */
+      if (!(pg ^ PANGO_GLYPH_EMPTY))
+       continue;
+
       glyph_name[0] = '\0';
       if (has_glyph_names)
        {
-         int errorcode = FT_Get_Glyph_Name (ftface, pg, glyph_name,
-                                            GLYPH_NAME_LEN);
+         FT_Error errorcode = FT_Get_Glyph_Name (ftface, pg, glyph_name,
+                                                 GLYPH_NAME_LEN);
          if (errorcode)
            programming_error (
              _f ("FT_Get_Glyph_Name () error: %s",
@@ -212,7 +225,7 @@ Pango_font::pango_item_string_stencil (PangoItem const *item,
 
       *tail = scm_cons (scm_list_4 (scm_from_double (ggeo.width * scale_),
                                    scm_from_double (ggeo.x_offset * scale_),
-                                   scm_from_double (ggeo.y_offset * scale_),
+                                   scm_from_double (ggeo.y_offset * scale_),
                                    char_id),
                        SCM_EOL);
       tail = SCM_CDRLOC (*tail);
@@ -284,72 +297,80 @@ Pango_font::physical_font_tab () const
   return physical_font_tab_;
 }
 
-Stencil
-Pango_font::word_stencil (string str) const
-{
-  return text_stencil (str, true);
-}
+extern bool music_strings_to_paths;
 
 Stencil
-Pango_font::text_stencil (string str) const
+Pango_font::text_stencil (Output_def * /* state */,
+                         string str, bool music_string) const
 {
-  return text_stencil (str, false);
-}
-
-Stencil
-Pango_font::text_stencil (string str,
-                         bool tight) const
-{
-  GList *items
-    = pango_itemize (context_,
-                    str.c_str (),
-                    0, str.length (), attribute_list_,
-                    NULL);
+  /*
+    The text assigned to a PangoLayout is automatically divided
+    into sections and reordered according to the Unicode
+    Bidirectional Algorithm, if necessary.
+  */
+  PangoLayout *layout = pango_layout_new (context_);
+  pango_layout_set_text (layout, str.c_str (), -1);
+  GSList *lines = pango_layout_get_lines (layout);
 
   Stencil dest;
-
   Real last_x = 0.0;
 
-  Direction text_dir = RIGHT;
-  for (GList *p = items; p; p = p->next)
-    {
-      PangoItem *item = (PangoItem *) p->data;
-      if (item->analysis.level == PANGO_DIRECTION_RTL)
-       text_dir = LEFT;
-    }
-
-  for (GList *ptr = items; ptr; ptr = ptr->next)
+  for (GSList *l = lines; l; l = l->next)
     {
-      PangoItem *item = (PangoItem *) ptr->data;
+      PangoLayoutLine *line = (PangoLayoutLine *) l->data;
+      GSList *layout_runs = line->runs;
 
-      Stencil item_stencil = pango_item_string_stencil (item, str, tight);
-
-      if (text_dir == RIGHT)
+      for (GSList *p = layout_runs; p; p = p->next)
        {
+         PangoGlyphItem *item = (PangoGlyphItem *) p->data;
+         Stencil item_stencil = pango_item_string_stencil (item);
+
          item_stencil.translate_axis (last_x, X_AXIS);
          last_x = item_stencil.extent (X_AXIS)[RIGHT];
-       }
-      else if (text_dir == LEFT)
-       dest.translate_axis (item_stencil.extent (X_AXIS)[RIGHT], X_AXIS);
 
 #if 0 // Check extents.
-      if (!item_stencil.extent_box ()[X_AXIS].is_empty ())
-       {
-         Stencil frame = Lookup::frame (item_stencil.extent_box (), 0.1, 0.1);
-         Box empty;
-         empty.set_empty ();
-         Stencil dimless_frame (empty, frame.expr ());
-         dest.add_stencil (frame);
-       }
+         if (!item_stencil.extent_box ()[X_AXIS].is_empty ())
+           {
+             Stencil frame = Lookup::frame (item_stencil.extent_box (), 0.1, 0.1);
+             Box empty;
+             empty.set_empty ();
+             Stencil dimless_frame (empty, frame.expr ());
+             dest.add_stencil (frame);
+           }
 #endif
 
-      dest.add_stencil (item_stencil);
+         dest.add_stencil (item_stencil);
+       }
     }
 
-  // UGH. Should have flags per output format signifying supported
-  // options.
   string name = get_output_backend_name ();
-  if (name != "ps" && name != "eps")
+  string output_mod = "scm output-" + name;
+  SCM mod = scm_c_resolve_module (output_mod.c_str ());
+
+  bool has_utf8_string = false;
+
+  if (ly_is_module (mod))
+    {
+      SCM utf8_string = ly_module_lookup (mod, ly_symbol2scm ("utf-8-string"));
+      /*
+       has_utf8_string should only be true when utf8_string is a
+       variable that is bound to a *named* procedure, i.e. not a
+       lambda expression.
+      */
+      if (utf8_string != SCM_BOOL_F
+         && scm_procedure_name (SCM_VARIABLE_REF (utf8_string)) != SCM_BOOL_F)
+       has_utf8_string = true;
+    }
+
+  bool to_paths = music_strings_to_paths;
+
+  /*
+    Backends with the utf-8-string expression use it when
+      1) the -dmusic-strings-to-paths option is set
+         and `str' is not a music string, or
+      2) the -dmusic-strings-to-paths option is not set.
+  */
+  if (has_utf8_string && ((to_paths && !music_string) || !to_paths))
     {
       // For Pango based backends, we take a shortcut.
       SCM exp = scm_list_3 (ly_symbol2scm ("utf-8-string"),
@@ -361,8 +382,6 @@ Pango_font::text_stencil (string str,
       return Stencil (b, exp);
     }
 
-  g_list_free (items);
-
   return dest;
 }