nux-1.16.0
nux::CairoGraphics Class Reference

A cairo graphics container. More...

#include <NuxImage/CairoGraphics.h>

List of all members.

Public Types

enum  Alignment { ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT, ALIGN_JUSTIFY }
 Enum used to specify horizontal alignment.
enum  VAlignment { VALIGN_TOP, VALIGN_MIDDLE, VALIGN_BOTTOM }
 Enum used to specify vertical alignment.
enum  Trimming {
  TRIMMING_NONE, TRIMMING_CHARACTER, TRIMMING_WORD, TRIMMING_CHARACTER_ELLIPSIS,
  TRIMMING_WORD_ELLIPSIS, TRIMMING_PATH_ELLIPSIS
}
 Enum used to specify trimming type.
enum  TextFlag { TEXT_FLAGS_NONE = 0, TEXT_FLAGS_UNDERLINE = 1, TEXT_FLAGS_STRIKEOUT = 2, TEXT_FLAGS_WORDWRAP = 4 }
 Enum used to specify text flags.

Public Member Functions

 CairoGraphics (cairo_format_t format, int width, int height)
cairo_t * GetContext ()
 Return a cairo context to the encapsulated surface.
cairo_t * GetInternalContext ()
 Return an internal cairo context to the encapsulated surface. Should not be destroyed.
cairo_surface_t * GetSurface ()
NBitmapDataGetBitmap ()
 Create a NBitmapData pointer to a 2D texture data.
int GetWidth () const
int GetHeight () const
bool PushState ()
bool PopState ()
bool ClearCanvas ()
bool ClearRect (double x, double y, double w, double h)
bool DrawLine (double x0, double y0, double x1, double y1, double width, const Color &c)
bool DrawFilledRect (double x, double y, double w, double h, const Color &c)
bool DrawCanvas (double x, double y, CairoGraphics *cg)
bool DrawRoundedRectangle (cairo_t *cr, double aspect, double x, double y, double cornerRadius, double width, double height, bool align=false)
bool BlurSurface (unsigned int radius, cairo_surface_t *surf=NULL)
bool IntersectRectClipRegion (double x, double y, double w, double h)
bool IntersectGeneralClipRegion (std::list< Rect > &region)

Detailed Description

A cairo graphics container.

CairoGraphics encapsulates a cairo surface and context.

Definition at line 60 of file CairoGraphics.h.


Member Function Documentation

NBitmapData * nux::CairoGraphics::GetBitmap ( )

Create a NBitmapData pointer to a 2D texture data.

The returned data must be destroyed with delete.

Returns:
A pointer to a 2D texture data.

Definition at line 86 of file CairoGraphics.cpp.

References nux::ImageSurface::GetPitch().

  {
    if ( (_width <= 0) || (_height <= 0) )
    {
      nuxDebugMsg (TEXT ("[CairoGraphics::GetBitmap] Invalid surface.") );
    }

    NUX_RETURN_VALUE_IF_NULL (_width, 0);
    NUX_RETURN_VALUE_IF_NULL (_height, 0);

    BitmapFormat bitmap_format = BITFMT_UNKNOWN;

    if (m_surface_format == CAIRO_FORMAT_ARGB32)
    {
      // Each pixel is a 32-bit quantity, with alpha in the upper 8 bits,
      // then red, then green, then blue. The 32-bit quantities are stored native-endian.
      // Pre-multiplied alpha is used. (That is, 50% transparent red is 0x80800000, not 0x80ff0000.)
      bitmap_format = BITFMT_B8G8R8A8;
    }

    if (m_surface_format == CAIRO_FORMAT_RGB24)
    {
      // Each pixel is a 32-bit quantity, with the upper 8 bits unused.
      // Red, Green, and Blue are stored in the remaining 24 bits in that order.
      bitmap_format = BITFMT_B8G8R8A8;
    }

    if (m_surface_format == CAIRO_FORMAT_A8)
    {
      // Each pixel is a 8-bit quantity holding an alpha value.
      bitmap_format = BITFMT_A8;
    }

    if (m_surface_format == CAIRO_FORMAT_A1)
      bitmap_format = BITFMT_A8;

    NTextureData *bitmap_data = new NTextureData (bitmap_format, _width, _height, 1);
    t_u8 *ptr = cairo_image_surface_get_data (_cairo_surface);
    int stride = cairo_image_surface_get_stride (_cairo_surface);

    if (ptr == NULL || stride == 0)
    {
      // _cairo_surface is not a valid surface
      nuxError (TEXT ("[CairoGraphics::GetBitmap] Invalid surface"));
      return bitmap_data; // just returns because we will segfault otherwise
    }

    if (m_surface_format == CAIRO_FORMAT_A1)
    {
      t_u8 *temp = new t_u8[bitmap_data->GetSurface (0).GetPitch() ];

      for (int j = 0; j < _height; j++)
      {
        for (int i = 0; i < _width; i++)
        {
          // Get the byte
          int a = ptr[j * stride + i/8];
          // Get the position in the byte
          int b = (i - 8 * (i / 8) );
          // Shift the byte and get the last bit
          int c = (a >> b) & 0x1;
          // If the last bit is set, put 1, otherwise put 0
          temp[i] = c ? 0xFF : 0x0;
        }

        Memcpy ( bitmap_data->GetSurface (0).GetPtrRawData() + j * bitmap_data->GetSurface (0).GetPitch(),
                 (const void *) (&temp[0]),
                 _width);
      }
    }
    else
    {
      for (int j = 0; j < _height; j++)
      {
        Memcpy (bitmap_data->GetSurface (0).GetPtrRawData() + j * bitmap_data->GetSurface (0).GetPitch(),
                (const void *) (&ptr[j * stride]),
                _width * GPixelFormats[bitmap_format].NumComponents);
      }
    }

    return bitmap_data;
  }
cairo_t * nux::CairoGraphics::GetContext ( )

Return a cairo context to the encapsulated surface.

Return the cairo context of this object. Call cairo_destroy to destroy the context when you are done with it.

Returns:
A cairo context.

Definition at line 66 of file CairoGraphics.cpp.

  { 
    cairo_t *cr = cairo_create (_cairo_surface);
    if (cairo_status (cr) == CAIRO_STATUS_NO_MEMORY)
    {
      nuxAssertMsg (0, TEXT ("[CairoGraphics::GetContext] Cairo context error.") );
    }
    return cr;
  }
cairo_t * nux::CairoGraphics::GetInternalContext ( )

Return an internal cairo context to the encapsulated surface. Should not be destroyed.

Return the cairo context of this object. This cairo context should not be destroyed with cairo_destroy.

Returns:
A cairo context.

Definition at line 76 of file CairoGraphics.cpp.

Referenced by nux::TextEntry::CreateLayout(), and nux::TextEntry::DrawText().

  { 
    return _cr;
  }

The documentation for this class was generated from the following files:
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends