René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

Font [Ming wrapper class]

The source

Font.h
#ifndef FONT_H
#define FONT_H

#include <string>

#include "ming.h"

class Font {
  friend class Text;
  friend class TextField;
  public:
    Font(std::string const& fdb);

    float Width     (std::string const& t, int size) const;
    float Ascent    (int size) const;
    float Leading   (int size) const;
    float Descent   (int size) const;

  	float LineHeight(int size) const;
  private:
    SWFFont font_;
};

#endif
Font.cpp
#include "Font.h"

Font::Font(std::string const& fdb) :
  font_(0)
{
  font_ = loadSWFFontFromFile(fopen(fdb.c_str(), "rb"));
}

float Font::Width (std::string const& t, int size) const {
  return SWFFont_getStringWidth(font_, reinterpret_cast<const unsigned char*>(t.c_str())) / 1024.0 * size * Ming_getScale();
}

float Font::Ascent (int size) const {
  return SWFFont_getAscent(font_) /1024.0 * size*Ming_getScale();
}

float Font::Leading(int size) const {
  return SWFFont_getLeading(font_) /1024.0 * size*Ming_getScale();
}

float Font::Descent(int size) const {
  return SWFFont_getDescent(font_) /1024.0 * size*Ming_getScale();
}

float Font::LineHeight(int size) const {
  return Ascent(size) + Descent(size);
}

//float TextHeight(std::string const& t, int size) {
//}

Examples

The following examples demonstrate this class: