Back to index

nux  3.0.0
Public Member Functions | Public Attributes
nux::TextLoader::Impl Struct Reference
Collaboration diagram for nux::TextLoader::Impl:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 Impl (TextLoader *parent)
 ~Impl ()
void RasterizeText (void *cairo_context, Color color)
Size ComputeTextSize ()
std::string GetPangoFontName ()

Public Attributes

TextLoaderparent_
float padding_x_
float padding_y_

Detailed Description

Definition at line 29 of file TextLoader.cpp.


Constructor & Destructor Documentation

Definition at line 43 of file TextLoader.cpp.

  : parent_(parent)
  , padding_x_(0)
  , padding_y_(0)
{

}

Definition at line 51 of file TextLoader.cpp.

{

}

Member Function Documentation

Definition at line 63 of file TextLoader.cpp.

{
  cairo_surface_t*      pango_surface = NULL;
  cairo_t*              cairo_ctx     = NULL;
  PangoLayout*          pango_layout  = NULL;
  PangoFontDescription* font_desc     = NULL;
  PangoContext*         pango_ctx     = NULL;
  PangoRectangle        ink_rect      = {0, 0, 0, 0};
  PangoRectangle        logical_rect  = {0, 0, 0, 0};
  int                   dpi           = 96;

  std::string pango_font_name = GetPangoFontName();

  // Create Cairo surface.
  pango_surface = cairo_image_surface_create(CAIRO_FORMAT_A1, 1, 1);
  
  // Create Cairo context.
  cairo_ctx = cairo_create(pango_surface);
  
  // Create layout.
  pango_layout = pango_cairo_create_layout(cairo_ctx);
  {
    pango_layout_set_wrap     (pango_layout, PANGO_WRAP_WORD_CHAR);
    pango_layout_set_ellipsize(pango_layout, PANGO_ELLIPSIZE_END);
    pango_layout_set_markup   (pango_layout, parent_->text().c_str(), -1);
    pango_layout_set_height   (pango_layout, -parent_->lines());

    // Sets the width to which the lines of the PangoLayout should wrap or ellipsized. The default value is -1: no width set.
    pango_layout_set_width(pango_layout, parent_->width * PANGO_SCALE);
  }

  // Create font description: "[FAMILY-LIST] [STYLE-OPTIONS] [SIZE]"
  font_desc = pango_font_description_from_string(pango_font_name.c_str());
  {
    pango_font_description_set_weight(font_desc, PANGO_WEIGHT_NORMAL);
    pango_layout_set_font_description(pango_layout, font_desc);
  }

  // Get Pango context
  pango_ctx = pango_layout_get_context(pango_layout); // is not ref'ed

  // Set font options
  CairoFontOptions font_options;
  {
    cairo_font_options_set_antialias      (font_options, CAIRO_ANTIALIAS_DEFAULT);
    cairo_font_options_set_subpixel_order(font_options, CAIRO_SUBPIXEL_ORDER_DEFAULT);
    cairo_font_options_set_hint_style     (font_options, CAIRO_HINT_STYLE_DEFAULT);
    cairo_font_options_set_hint_metrics   (font_options, CAIRO_HINT_METRICS_ON);
    cairo_set_font_options(cairo_ctx, font_options);

    pango_cairo_context_set_font_options(pango_ctx, font_options);
  }

  // use some default DPI-value
  pango_cairo_context_set_resolution(pango_ctx, dpi);

  pango_layout_context_changed(pango_layout);
  pango_layout_get_extents(pango_layout, &ink_rect, &logical_rect);

  int text_width = std::ceil((float)logical_rect.width / PANGO_SCALE);
  int text_height = std::ceil((float)logical_rect.height / PANGO_SCALE);

  padding_x_ = text_width - logical_rect.width / PANGO_SCALE;
  padding_y_ = text_height - logical_rect.height / PANGO_SCALE;

  text_width = std::min<int>(text_width, parent_->width());

  // clean up
  pango_font_description_free(font_desc);
  g_object_unref(pango_layout);
  cairo_destroy(cairo_ctx);
  cairo_surface_destroy(pango_surface);

  return Size(text_width, text_height);
}

Here is the caller graph for this function:

Definition at line 56 of file TextLoader.cpp.

{
  std::ostringstream os;
  os << parent_->font_name() << " " << parent_->font_size();
  return std::string(os.str());
}
void nux::TextLoader::Impl::RasterizeText ( void *  cairo_context,
Color  color 
)

Definition at line 139 of file TextLoader.cpp.

{
  cairo_t* cairo_ctx = (cairo_t*) cairo_context;

  PangoLayout*          pango_layout  = NULL;
  PangoFontDescription* font_desc     = NULL;
  PangoContext*         pango_ctx     = NULL;
  int                   dpi            = 96;

  // Create layout.
  pango_layout = pango_cairo_create_layout(cairo_ctx);
  {
    pango_layout_set_wrap     (pango_layout, PANGO_WRAP_WORD_CHAR);
    pango_layout_set_ellipsize(pango_layout, PANGO_ELLIPSIZE_END);
    pango_layout_set_alignment(pango_layout, (PangoAlignment)parent_->alignment());
    pango_layout_set_markup   (pango_layout, parent_->text().c_str(), -1);
    pango_layout_set_height   (pango_layout, -parent_->lines());

    // Sets the width to which the lines of the PangoLayout should wrap or ellipsized. The default value is -1: no width set.
    pango_layout_set_width(pango_layout, parent_->width * PANGO_SCALE);
  }

  // Create font description: "[FAMILY-LIST] [STYLE-OPTIONS] [SIZE]"
  font_desc = pango_font_description_from_string(GetPangoFontName().c_str());
  {
    pango_font_description_set_weight(font_desc, PANGO_WEIGHT_NORMAL);
    pango_layout_set_font_description(pango_layout, font_desc);
  }

  // Get Pango context
  pango_ctx = pango_layout_get_context(pango_layout); // is not ref'ed

  // Set font options
  CairoFontOptions font_options;
  {
    cairo_font_options_set_antialias      (font_options, CAIRO_ANTIALIAS_DEFAULT);
    cairo_font_options_set_subpixel_order (font_options, CAIRO_SUBPIXEL_ORDER_DEFAULT);
    cairo_font_options_set_hint_style     (font_options, CAIRO_HINT_STYLE_DEFAULT);
    cairo_font_options_set_hint_metrics   (font_options, CAIRO_HINT_METRICS_ON);
    cairo_set_font_options(cairo_ctx, font_options);

    pango_cairo_context_set_font_options(pango_ctx, font_options);
  }

  pango_cairo_context_set_resolution(pango_ctx, dpi);

  cairo_set_source_rgba(cairo_ctx, 1.0, 1.0, 1.0, 1.0);

  pango_layout_context_changed(pango_layout);

  cairo_move_to(cairo_ctx, padding_x_, padding_y_);
  pango_cairo_show_layout(cairo_ctx, pango_layout);

  // clean up
  pango_font_description_free(font_desc);
  g_object_unref(pango_layout);
}

Here is the caller graph for this function:


Member Data Documentation

Definition at line 39 of file TextLoader.cpp.

Definition at line 40 of file TextLoader.cpp.

Definition at line 38 of file TextLoader.cpp.


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