Back to index

unity  6.0.0
Public Member Functions | Static Public Member Functions | Public Attributes
nux::StaticCairoText::Impl Struct Reference
Collaboration diagram for nux::StaticCairoText::Impl:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 Impl (StaticCairoText *parent, std::string const &text)
 ~Impl ()
PangoEllipsizeMode GetPangoEllipsizeMode () const
PangoAlignment GetPangoAlignment () const
std::string GetEffectiveFont () const
Size GetTextExtents () const
void DrawText (cairo_t *cr, int width, int height, Color const &color)
void UpdateTexture ()
void OnFontChanged ()

Static Public Member Functions

static void FontChanged (GObject *gobject, GParamSpec *pspec, gpointer data)

Public Attributes

StaticCairoTextparent_
bool accept_key_nav_focus_
bool need_new_extent_cache_
Size cached_extent_
Size cached_base_
int baseline_
std::string text_
Color text_color_
EllipsizeState ellipsize_
AlignState align_
AlignState valign_
std::string font_
BaseTexturePtr texture2D_
Size pre_layout_size_
int lines_
int actual_lines_

Detailed Description

Definition at line 50 of file StaticCairoText.cpp.


Constructor & Destructor Documentation

nux::StaticCairoText::Impl::Impl ( StaticCairoText parent,
std::string const &  text 
)

Definition at line 93 of file StaticCairoText.cpp.

  : parent_(parent)
  , accept_key_nav_focus_(false)
  , need_new_extent_cache_(true)
  , baseline_(0)
  , text_(text)
  , text_color_(color::White)
  , ellipsize_(NUX_ELLIPSIZE_END)
  , align_(NUX_ALIGN_LEFT)
  , valign_(NUX_ALIGN_TOP)
  , lines_(-2)  // should find out why -2...
    // the desired height of the layout in Pango units if positive, or desired
    // number of lines if negative.
  , actual_lines_(0)
{
  GtkSettings* settings = gtk_settings_get_default();  // not ref'ed
  g_signal_connect(settings, "notify::gtk-font-name",
                   (GCallback)FontChanged, this);
  g_signal_connect(settings, "notify::gtk-xft-dpi",
                   (GCallback)FontChanged, this);
}

Here is the call graph for this function:

Definition at line 115 of file StaticCairoText.cpp.

{
  GtkSettings* settings = gtk_settings_get_default();  // not ref'ed
  g_signal_handlers_disconnect_by_func(settings,
                                       (void*)FontChanged,
                                       this);
}

Member Function Documentation

void nux::StaticCairoText::Impl::DrawText ( cairo_t *  cr,
int  width,
int  height,
Color const &  color 
)

Definition at line 442 of file StaticCairoText.cpp.

{
  PangoLayout*          layout     = NULL;
  PangoFontDescription* desc       = NULL;
  PangoContext*         pangoCtx   = NULL;
  int                   dpi        = 0;
  GdkScreen*            screen     = gdk_screen_get_default();    // not ref'ed
  GtkSettings*          settings   = gtk_settings_get_default();  // not ref'ed

  std::string font(GetEffectiveFont());

  cairo_set_font_options(cr, gdk_screen_get_font_options(screen));
  layout = pango_cairo_create_layout(cr);
  desc = pango_font_description_from_string(font.c_str());

  pango_layout_set_font_description(layout, desc);
  pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
  pango_layout_set_ellipsize(layout, GetPangoEllipsizeMode());
  pango_layout_set_alignment(layout, GetPangoAlignment());
  pango_layout_set_markup(layout, text_.c_str(), -1);
  pango_layout_set_width(layout, width * PANGO_SCALE);
  pango_layout_set_height(layout, height * PANGO_SCALE);

  pango_layout_set_height(layout, lines_);
  pangoCtx = pango_layout_get_context(layout);  // is not ref'ed
  pango_cairo_context_set_font_options(pangoCtx,
                                       gdk_screen_get_font_options(screen));
  g_object_get(settings, "gtk-xft-dpi", &dpi, NULL);
  if (dpi == -1)
  {
    // use some default DPI-value
    pango_cairo_context_set_resolution(pangoCtx, 96.0f);
  }
  else
  {
    pango_cairo_context_set_resolution(pangoCtx,
                                       (float) dpi / (float) PANGO_SCALE);
  }

  cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
  cairo_paint(cr);

  cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
  cairo_set_source_rgba(cr, color.red, color.green, color.blue, color.alpha);

  pango_layout_context_changed(layout);

  cairo_move_to(cr, 0.0f, 0.0f);
  pango_cairo_show_layout(cr, layout);

  actual_lines_ = pango_layout_get_line_count(layout);

  // clean up
  pango_font_description_free(desc);
  g_object_unref(layout);
}
void nux::StaticCairoText::Impl::FontChanged ( GObject *  gobject,
GParamSpec *  pspec,
gpointer  data 
) [static]

Definition at line 518 of file StaticCairoText.cpp.

{
  StaticCairoText::Impl* self = static_cast<StaticCairoText::Impl*>(data);
  self->OnFontChanged();
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 357 of file StaticCairoText.cpp.

{
  if (font_.empty())
  {
    GtkSettings* settings = gtk_settings_get_default();  // not ref'ed
    glib::String font_name;
    g_object_get(settings, "gtk-font-name", &font_name, NULL);
    return font_name.Str();
  }

  return font_;
}

Here is the call graph for this function:

Definition at line 138 of file StaticCairoText.cpp.

{
  switch (align_)
  {
  case NUX_ALIGN_LEFT:
    return PANGO_ALIGN_LEFT;
  case NUX_ALIGN_CENTRE:
    return PANGO_ALIGN_CENTER;
  default:
    return PANGO_ALIGN_RIGHT;
  }
}

Definition at line 123 of file StaticCairoText.cpp.

{
  switch (ellipsize_)
  {
  case NUX_ELLIPSIZE_START:
    return PANGO_ELLIPSIZE_START;
  case NUX_ELLIPSIZE_MIDDLE:
    return PANGO_ELLIPSIZE_MIDDLE;
  case NUX_ELLIPSIZE_END:
    return PANGO_ELLIPSIZE_END;
  default:
    return PANGO_ELLIPSIZE_NONE;
  }
}

Definition at line 370 of file StaticCairoText.cpp.

{
  cairo_surface_t*      surface  = NULL;
  cairo_t*              cr       = NULL;
  PangoLayout*          layout   = NULL;
  PangoFontDescription* desc     = NULL;
  PangoContext*         pangoCtx = NULL;
  PangoRectangle        inkRect  = {0, 0, 0, 0};
  PangoRectangle        logRect  = {0, 0, 0, 0};
  int                   dpi      = 0;
  GdkScreen*            screen   = gdk_screen_get_default();    // is not ref'ed
  GtkSettings*          settings = gtk_settings_get_default();  // is not ref'ed

  if (!need_new_extent_cache_)
  {
    return cached_extent_;
  }

  Size result;
  std::string font = GetEffectiveFont();

  int maxwidth = parent_->GetMaximumWidth();

  surface = cairo_image_surface_create(CAIRO_FORMAT_A1, 1, 1);
  cr = cairo_create(surface);
  cairo_set_font_options(cr, gdk_screen_get_font_options(screen));

  layout = pango_cairo_create_layout(cr);
  desc = pango_font_description_from_string(font.c_str());
  pango_layout_set_font_description(layout, desc);
  pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
  pango_layout_set_ellipsize(layout, GetPangoEllipsizeMode());
  pango_layout_set_alignment(layout, GetPangoAlignment());
  pango_layout_set_markup(layout, text_.c_str(), -1);
  pango_layout_set_height(layout, lines_);
  pango_layout_set_width(layout, maxwidth * PANGO_SCALE);

  pangoCtx = pango_layout_get_context(layout);  // is not ref'ed
  pango_cairo_context_set_font_options(pangoCtx,
                                       gdk_screen_get_font_options(screen));
  g_object_get(settings, "gtk-xft-dpi", &dpi, NULL);
  if (dpi == -1)
  {
    // use some default DPI-value
    pango_cairo_context_set_resolution(pangoCtx, 96.0f);
  }
  else
  {
    pango_cairo_context_set_resolution(pangoCtx,
                                       (float) dpi / (float) PANGO_SCALE);
  }
  pango_layout_context_changed(layout);
  pango_layout_get_extents(layout, &inkRect, &logRect);

  // logRect has some issues using italic style
  if (inkRect.x + inkRect.width > logRect.x + logRect.width)
    result.width = std::ceil(static_cast<float>(inkRect.x + inkRect.width - logRect.x) / PANGO_SCALE);
  else
    result.width  = std::ceil(static_cast<float>(logRect.width) / PANGO_SCALE);

  result.height = std::ceil(static_cast<float>(logRect.height) / PANGO_SCALE);
  cached_extent_ = result;
  baseline_ = pango_layout_get_baseline(layout) / PANGO_SCALE;

  // clean up
  pango_font_description_free(desc);
  g_object_unref(layout);
  cairo_destroy(cr);
  cairo_surface_destroy(surface);
  return result;
}

Here is the caller graph for this function:

Definition at line 526 of file StaticCairoText.cpp.

Here is the caller graph for this function:

Definition at line 502 of file StaticCairoText.cpp.

{
  Size size = GetTextExtents();
  parent_->SetBaseSize(size.width, size.height);
  // Now reget the internal geometry as it is clipped by the max size.
  Geometry const& geo = parent_->GetGeometry();

  CairoGraphics cairo_graphics(CAIRO_FORMAT_ARGB32,
                               geo.width, geo.height);

  DrawText(cairo_graphics.GetInternalContext(),
           geo.width, geo.height, text_color_);

  texture2D_ = texture_ptr_from_cairo_graphics(cairo_graphics);
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 69 of file StaticCairoText.cpp.

Definition at line 90 of file StaticCairoText.cpp.

Definition at line 80 of file StaticCairoText.cpp.

Definition at line 74 of file StaticCairoText.cpp.

Definition at line 73 of file StaticCairoText.cpp.

Definition at line 72 of file StaticCairoText.cpp.

Definition at line 79 of file StaticCairoText.cpp.

Definition at line 83 of file StaticCairoText.cpp.

Definition at line 89 of file StaticCairoText.cpp.

Definition at line 70 of file StaticCairoText.cpp.

Definition at line 68 of file StaticCairoText.cpp.

Definition at line 87 of file StaticCairoText.cpp.

Definition at line 76 of file StaticCairoText.cpp.

Definition at line 77 of file StaticCairoText.cpp.

Definition at line 85 of file StaticCairoText.cpp.

Definition at line 81 of file StaticCairoText.cpp.


The documentation for this struct was generated from the following file: