Blame cairomm/scaledfont.h

Packit 908522
/* Copyright (C) 2006 The cairomm Development Team
Packit 908522
 *
Packit 908522
 * This library is free software; you can redistribute it and/or
Packit 908522
 * modify it under the terms of the GNU Library General Public
Packit 908522
 * License as published by the Free Software Foundation; either
Packit 908522
 * version 2 of the License, or (at your option) any later version.
Packit 908522
 *
Packit 908522
 * This library is distributed in the hope that it will be useful,
Packit 908522
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 908522
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 908522
 * Library General Public License for more details.
Packit 908522
 *
Packit 908522
 * You should have received a copy of the GNU Library General Public
Packit 908522
 * License along with this library; if not, write to the Free Software
Packit 908522
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit 908522
 * 02110-1301, USA.
Packit 908522
 */
Packit 908522
Packit 908522
#ifndef __CAIROMM_SCALEDFONT_H
Packit 908522
#define __CAIROMM_SCALEDFONT_H
Packit 908522
Packit 908522
#include <cairomm/refptr.h>
Packit 908522
#include <cairomm/fontoptions.h>
Packit 908522
#include <cairomm/fontface.h>
Packit 908522
#include <cairomm/matrix.h>
Packit 908522
#include <cairomm/types.h>
Packit 908522
#include <vector>
Packit 908522
Packit 908522
#ifdef CAIRO_HAS_FT_FONT
Packit 908522
#include <cairo-ft.h>
Packit 908522
#endif // CAIRO_HAS_FT_FONT
Packit 908522
Packit 908522
namespace Cairo
Packit 908522
{
Packit 908522
Packit 908522
/** A ScaledFont is a font scaled to a particular size and device resolution. It
Packit 908522
 * is most useful for low-level font usage where a library or application wants
Packit 908522
 * to cache a reference to a scaled font to speed up the computation of metrics.
Packit 908522
 */
Packit 908522
class ScaledFont
Packit 908522
{
Packit 908522
Packit 908522
public: 
Packit 908522
  /** The underlying C cairo object type */
Packit 908522
  typedef cairo_scaled_font_t cobject;
Packit 908522
Packit 908522
  /** Provides acces to the underlying C cairo object */
Packit 908522
  inline cobject* cobj() { return m_cobject; }
Packit 908522
Packit 908522
  /** Provides acces to the underlying C cairo object */
Packit 908522
  inline const cobject* cobj() const { return m_cobject; }
Packit 908522
Packit 908522
#ifndef DOXYGEN_IGNORE_THIS
Packit 908522
  // For use only by the cairomm implementation.
Packit 908522
  inline ErrorStatus get_status() const
Packit 908522
  { return cairo_scaled_font_status(const_cast<cairo_scaled_font_t*>(cobj())); }
Packit 908522
Packit 908522
  // for RefPtr
Packit 908522
  void reference() const { cairo_scaled_font_reference(m_cobject); }
Packit 908522
  void unreference() const { cairo_scaled_font_destroy(m_cobject); }
Packit 908522
#endif //DOXYGEN_IGNORE_THIS
Packit 908522
Packit 908522
  /** Create a C++ wrapper object from the C instance.  This C++ object should
Packit 908522
   * then be given to a RefPtr.
Packit 908522
   */
Packit 908522
  explicit ScaledFont(cobject* cobj, bool has_reference = false);
Packit 908522
Packit 908522
  ScaledFont(const ScaledFont&) = delete;
Packit 908522
  ScaledFont& operator=(const ScaledFont&) = delete;
Packit 908522
Packit 908522
  virtual ~ScaledFont();
Packit 908522
Packit 908522
  /** Creates a ScaledFont object from a font face and matrices that describe
Packit 908522
   * the size of the font and the environment in which it will be used.
Packit 908522
   *
Packit 908522
   * @param font_face A font face.
Packit 908522
   * @param font_matrix font space to user space transformation matrix for the
Packit 908522
   * font. In the simplest case of a N point font, this matrix is just a scale
Packit 908522
   * by N, but it can also be used to shear the font or stretch it unequally
Packit 908522
   * along the two axes. See Context::set_font_matrix().
Packit 908522
   * @param ctm user to device transformation matrix with which the font will be
Packit 908522
   * used.
Packit 908522
   * @param options options to use when getting metrics for the font and
Packit 908522
   * rendering with it.
Packit 908522
   */
Packit 908522
  static RefPtr<ScaledFont> create(const RefPtr<FontFace>& font_face, const Matrix& font_matrix,
Packit 908522
      const Matrix& ctm, const FontOptions& options = FontOptions());
Packit 908522
  /* To keep 1.6.x ABI  */
Packit 908522
  static RefPtr<ScaledFont> create(const RefPtr<FontFace>& font_face, const cairo_matrix_t& font_matrix,
Packit 908522
      const cairo_matrix_t& ctm, const FontOptions& options = FontOptions());
Packit 908522
  // NOTE: the constructor doesn't take a RefPtr<const FontFace> because the
Packit 908522
  // FontFace object can be changed in this constructor (in the case of user
Packit 908522
  // fonts, the FontFace becomes immutable, i.e. you can't call any set_*_func()
Packit 908522
  // functions any longer)
Packit 908522
Packit 908522
  //We use an output paramter instead of the return value,
Packit 908522
  //for consistency with other get_*extents() methods in other classes,
Packit 908522
  //though they should probably all use the return value instead.
Packit 908522
  //but it is too late to change that now. murrayc:
Packit 908522
Packit 908522
  /** Gets the metrics for a ScaledFont
Packit 908522
   * @since 1.8
Packit 908522
   * */
Packit 908522
  void get_extents(FontExtents& extents) const;
Packit 908522
Packit 908522
  /** @deprecated Use get_extents() instead
Packit 908522
   * @since 1.2
Packit 908522
   * */
Packit 908522
  void extents(FontExtents& extents) const;
Packit 908522
Packit 908522
  /** Gets the extents for a string of text. The extents describe a user-space
Packit 908522
   * rectangle that encloses the "inked" portion of the text drawn at the origin
Packit 908522
   * (0,0) (as it would be drawn by Context::show_text() if the cairo graphics
Packit 908522
   * state were set to the same font_face, font_matrix, ctm, and font_options as
Packit 908522
   * the ScaledFont object).  Additionally, the x_advance and y_advance values
Packit 908522
   * indicate the amount by which the current point would be advanced by
Packit 908522
   * Context::show_text().
Packit 908522
   *
Packit 908522
   * Note that whitespace characters do not directly contribute to the size of
Packit 908522
   * the rectangle (extents.width and extents.height). They do contribute
Packit 908522
   * indirectly by changing the position of non-whitespace characters. In
Packit 908522
   * particular, trailing whitespace characters are likely to not affect the
Packit 908522
   * size of the rectangle, though they will affect the x_advance and y_advance
Packit 908522
   * values.
Packit 908522
   *
Packit 908522
   * @param utf8  a string of text, encoded in UTF-8.
Packit 908522
   * @param extents Returns the extents of the given string.
Packit 908522
   *
Packit 908522
   * @since 1.8
Packit 908522
   */
Packit 908522
  void get_text_extents(const std::string& utf8, TextExtents& extents) const;
Packit 908522
  /** @deprecated Use get_text_extents() instead
Packit 908522
   * @since 1.2
Packit 908522
   * */
Packit 908522
  void text_extents(const std::string& utf8, TextExtents& extents) const;
Packit 908522
Packit 908522
  /** Gets the extents for an array of glyphs. The extents describe a user-space
Packit 908522
   * rectangle that encloses the "inked" portion of the glyphs, (as they would
Packit 908522
   * be drawn by Context::show_glyphs() if the cairo graphics state were set to the
Packit 908522
   * same font_face, font_matrix, ctm, and font_options as the ScaledFont
Packit 908522
   * object).  Additionally, the x_advance and y_advance values indicate the
Packit 908522
   * amount by which the current point would be advanced by Context::show_glyphs().
Packit 908522
   *
Packit 908522
   * Note that whitespace glyphs do not contribute to the size of the rectangle
Packit 908522
   * (extents.width and extents.height).
Packit 908522
   *
Packit 908522
   * @param glyphs A vector of glyphs to calculate the extents of.
Packit 908522
   * @param extents Returns the extents for the array of glyphs.
Packit 908522
   *
Packit 908522
   * @since 1.8
Packit 908522
   **/
Packit 908522
  void get_glyph_extents(const std::vector<Glyph>& glyphs, TextExtents& extents);
Packit 908522
Packit 908522
  /** @deprecated Use get_glyph_extents() instead
Packit 908522
   * @since 1.2
Packit 908522
   * */
Packit 908522
  void glyph_extents(const std::vector<Glyph>& glyphs, TextExtents& extents);
Packit 908522
Packit 908522
  /** The FontFace with which this ScaledFont was created.
Packit 908522
   * @since 1.2
Packit 908522
   */
Packit 908522
  RefPtr<FontFace> get_font_face() const;
Packit 908522
Packit 908522
  /** Gets the FontOptions with which the ScaledFont was created.
Packit 908522
   * @since 1.2
Packit 908522
   */
Packit 908522
  void get_font_options(FontOptions& options) const;
Packit 908522
Packit 908522
  /** Gets the font matrix with which the ScaledFont was created.
Packit 908522
   * @since 1.2
Packit 908522
   */
Packit 908522
  void get_font_matrix(Matrix& font_matrix) const;
Packit 908522
  /* To keep 1.6.x ABI  */
Packit 908522
  void get_font_matrix(cairo_matrix_t& font_matrix) const;
Packit 908522
Packit 908522
  /** Gets the CTM with which the ScaledFont was created.
Packit 908522
   * @since 1.2
Packit 908522
   */
Packit 908522
  void get_ctm(Matrix& ctm) const;
Packit 908522
  /* To keep 1.6.x ABI  */
Packit 908522
  void get_ctm(cairo_matrix_t& ctm) const;
Packit 908522
Packit 908522
  /** Gets the type of scaled Font
Packit 908522
   * @since 1.2
Packit 908522
   */
Packit 908522
  FontType get_type() const;
Packit 908522
Packit 908522
  // FIXME: it'd be really nice not to assume a specific container (e.g.
Packit 908522
  // std::vector) here
Packit 908522
  /**
Packit 908522
   * @param x X position to place first glyph.
Packit 908522
   * @param y Y position to place first glyph.
Packit 908522
   * @param utf8 a string of text encoded in UTF-8.
Packit 908522
   * @param glyphs pointer to array of glyphs to fill.
Packit 908522
   * @param clusters pointer to array of cluster mapping information to fill.
Packit 908522
   * @cluster_flags cluster mapping flags
Packit 908522
   *
Packit 908522
   * Converts UTF-8 text to an array of glyphs, with cluster mapping, that can be
Packit 908522
   * used to render later.
Packit 908522
   *
Packit 908522
   * For details of how (@a clusters and @a cluster_flags map input
Packit 908522
   * UTF-8 text to the output glyphs see Context::show_text_glyphs().
Packit 908522
   *
Packit 908522
   * The output values can be readily passed to Context::show_text_glyphs()
Packit 908522
   * Context::show_glyphs(), or related functions, assuming that the exact
Packit 908522
   * same scaled font is used for the operation.
Packit 908522
   *
Packit 908522
   * @since 1.8
Packit 908522
   **/
Packit 908522
  void text_to_glyphs(double x,
Packit 908522
                      double y,
Packit 908522
                      const std::string& utf8,
Packit 908522
                      std::vector<Glyph>& glyphs,
Packit 908522
                      std::vector<TextCluster>& clusters,
Packit 908522
                      TextClusterFlags& cluster_flags);
Packit 908522
Packit 908522
  /** Stores the scale matrix of this scaled font into matrix. The scale matrix
Packit 908522
   * is product of the font matrix and the ctm associated with the scaled font,
Packit 908522
   * and hence is the matrix mapping from font space to device space.
Packit 908522
   *
Packit 908522
   * @param scale_matrix return value for the matrix.
Packit 908522
   *
Packit 908522
   * @since 1.8
Packit 908522
   */
Packit 908522
  void get_scale_matrix(Matrix& scale_matrix) const;
Packit 908522
Packit 908522
protected:
Packit 908522
  /* Cairo::Matrix parameters changed to cairo_matrix_t */
Packit 908522
  ScaledFont(const RefPtr<FontFace>& font_face, const cairo_matrix_t& font_matrix,
Packit 908522
             const cairo_matrix_t& ctm, const FontOptions& options = FontOptions());
Packit 908522
  /** The underlying C cairo object that is wrapped by this ScaledFont */
Packit 908522
  cobject* m_cobject;
Packit 908522
};
Packit 908522
Packit 908522
#ifdef CAIRO_HAS_FT_FONT
Packit 908522
Packit 908522
//TODO: Documentation.
Packit 908522
/**
Packit 908522
 * @since 1.8
Packit 908522
 */
Packit 908522
class FtScaledFont : public ScaledFont
Packit 908522
{
Packit 908522
public:
Packit 908522
  /** Creates a ScaledFont From a FtFontFace.
Packit 908522
   *
Packit 908522
   * @since 1.8
Packit 908522
   */
Packit 908522
  static RefPtr<FtScaledFont> create(const RefPtr<FtFontFace>& font_face, const Matrix& font_matrix,
Packit 908522
      const Matrix& ctm, const FontOptions& options = FontOptions());
Packit 908522
Packit 908522
  /** Gets the FT_Face object from a FreeType backend font and scales it
Packit 908522
   * appropriately for the font. You must release the face with
Packit 908522
   * unlock_face() when you are done using it. Since the FT_Face object can be
Packit 908522
   * shared between multiple ScaledFont objects, you must not lock any other
Packit 908522
   * font objects until you unlock this one. A count is kept of the number of
Packit 908522
   * times lock_face() is called.  unlock_face() must be called the same number
Packit 908522
   * of times.
Packit 908522
   *
Packit 908522
   * You must be careful when using this function in a library or in a threaded
Packit 908522
   * application, because freetype's design makes it unsafe to call freetype
Packit 908522
   * functions simultaneously from multiple threads, (even if using distinct
Packit 908522
   * FT_Face objects). Because of this, application code that acquires an
Packit 908522
   * FT_Face object with this call must add it's own locking to protect any use
Packit 908522
   * of that object, (and which also must protect any other calls into cairo as
Packit 908522
   * almost any cairo function might result in a call into the freetype
Packit 908522
   * library).
Packit 908522
   *
Packit 908522
   * @return The FT_Face object for font, scaled appropriately, or NULL if
Packit 908522
   * scaled_font is in an error state or there is insufficient memory.
Packit 908522
   *
Packit 908522
   * @since 1.8
Packit 908522
   */
Packit 908522
  FT_Face lock_face();
Packit 908522
Packit 908522
  /** Releases a face obtained with lock_face().
Packit 908522
   *
Packit 908522
   * @since 1.8
Packit 908522
   */
Packit 908522
  void unlock_face();
Packit 908522
Packit 908522
protected:
Packit 908522
  FtScaledFont(const RefPtr<FtFontFace>& font_face, const Matrix& font_matrix,
Packit 908522
      const Matrix& ctm, const FontOptions& options = FontOptions());
Packit 908522
};
Packit 908522
#endif // CAIRO_HAS_FT_FONT
Packit 908522
Packit 908522
}
Packit 908522
Packit 908522
#endif // __CAIROMM_SCALEDFONT_H
Packit 908522
// vim: ts=2 sw=2 et