Advertisement
bueddl

Untitled

Mar 31st, 2016
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1.  
  2. Area Text::box(const Area &area, double &font_size, Alignment &align) const
  3. {
  4.     PangoFontMap *font_map = pango_ft2_font_map_new();
  5.     PangoContext *ctx = pango_font_map_create_context(font_map);
  6.  
  7.     PangoFontDescription *font_desc = pango_font_description_new();
  8.     pango_font_description_set_family(font_desc, "Font");
  9.     pango_font_description_set_style(font_desc, PANGO_STYLE_NORMAL);
  10.     pango_font_description_set_variant(font_desc, PANGO_VARIANT_NORMAL);
  11.     pango_font_description_set_weight (font_desc, PANGO_WEIGHT_NORMAL);
  12.     pango_font_description_set_stretch(font_desc, PANGO_STRETCH_NORMAL);
  13.     pango_font_description_set_size(font_desc, 100 * PANGO_SCALE);
  14.     pango_context_set_font_description(ctx, font_desc);
  15.  
  16.     PangoLayout *layout = pango_layout_new(ctx);
  17.  
  18.     pango_layout_set_text(layout, m_text.c_str(), m_text.size());
  19.     pango_layout_set_indent(layout, 0);
  20.  
  21.     int width, height;
  22.     pango_layout_get_pixel_size(layout, &width, &height);
  23.     g_object_unref(layout);
  24.  
  25.     double scale;
  26.     Alignment area_align, text_align;
  27.     if (area.width() >= area.height())
  28.     {
  29.         // area is horizontal dimensioned
  30.         scale = area.width();
  31.         area_align = Alignment::HORIZONTAL;
  32.     } else
  33.     {
  34.         // area is vertically dimensioned
  35.         scale = area.height();
  36.         area_align = Alignment::VERTICAL;      
  37.     }
  38.  
  39.     if (width >= height)
  40.     {
  41.         // text box is horizontal dimensioned
  42.         scale /= width;
  43.         text_align = Alignment::HORIZONTAL;
  44.     } else
  45.     {
  46.         // text box is vertically dimensioned
  47.         scale /= height;
  48.         text_align = Alignment::VERTICAL;
  49.     }
  50.  
  51.     width *= scale;
  52.     height *= scale;
  53.     if (area_align == text_align)
  54.         align = HORIZONTAL;
  55.     else
  56.         align = VERTICAL;
  57.  
  58.     Area result;
  59.     result.begin() = area.begin();
  60.     result.end() = area.begin() + Pixel(width, height);
  61.  
  62.     return result;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement