sl@0: 
sl@0: /*
sl@0:  *
sl@0:  * (C) Copyright IBM Corp. 1998-2005 - All Rights Reserved
sl@0:  *
sl@0:  */
sl@0: 
sl@0: #ifndef __LEFONTINSTANCE_H
sl@0: #define __LEFONTINSTANCE_H
sl@0: 
sl@0: #include "LETypes.h"
sl@0: /**
sl@0:  * \file 
sl@0:  * \brief C++ API: Layout Engine Font Instance object
sl@0:  */
sl@0: 
sl@0: U_NAMESPACE_BEGIN
sl@0: 
sl@0: /**
sl@0:  * Instances of this class are used by <code>LEFontInstance::mapCharsToGlyphs</code> and
sl@0:  * <code>LEFontInstance::mapCharToGlyph</code> to adjust character codes before the character
sl@0:  * to glyph mapping process. Examples of this are filtering out control characters
sl@0:  * and character mirroring - replacing a character which has both a left and a right
sl@0:  * hand form with the opposite form.
sl@0:  *
sl@0:  * @stable ICU 3.2
sl@0:  */
sl@0: class LECharMapper /* not : public UObject because this is an interface/mixin class */
sl@0: {
sl@0: public:
sl@0:     /**
sl@0:      * Destructor.
sl@0:      * @stable ICU 3.2
sl@0:      */
sl@0:     virtual inline ~LECharMapper() {};
sl@0: 
sl@0:     /**
sl@0:      * This method does the adjustments.
sl@0:      *
sl@0:      * @param ch - the input character
sl@0:      *
sl@0:      * @return the adjusted character
sl@0:      *
sl@0:      * @stable ICU 2.8
sl@0:      */
sl@0:     virtual LEUnicode32 mapChar(LEUnicode32 ch) const = 0;
sl@0: };
sl@0: 
sl@0: /**
sl@0:  * This is a forward reference to the class which holds the per-glyph
sl@0:  * storage.
sl@0:  *
sl@0:  * @draft ICU 3.0
sl@0:  */
sl@0: class LEGlyphStorage;
sl@0: 
sl@0: /**
sl@0:  * This is a virtual base class that serves as the interface between a LayoutEngine
sl@0:  * and the platform font environment. It allows a LayoutEngine to access font tables, do
sl@0:  * character to glyph mapping, and obtain metrics information without knowing any platform
sl@0:  * specific details. There are also a few utility methods for converting between points,
sl@0:  * pixels and funits. (font design units)
sl@0:  *
sl@0:  * An instance of an <code>LEFontInstance</code> represents a font at a particular point
sl@0:  * size. Each instance can represent either a single physical font, or a composite font.
sl@0:  * A composite font is a collection of physical fonts, each of which contains a subset of
sl@0:  * the characters contained in the composite font.
sl@0:  *
sl@0:  * Note: with the exception of <code>getSubFont</code>, the methods in this class only
sl@0:  * make sense for a physical font. If you have an <code>LEFontInstance</code> which
sl@0:  * represents a composite font you should only call the methods below which have
sl@0:  * an <code>LEGlyphID</code>, an <code>LEUnicode</code> or an <code>LEUnicode32</code>
sl@0:  * as one of the arguments because these can be used to select a particular subfont.
sl@0:  *
sl@0:  * Subclasses which implement composite fonts should supply an implementation of these
sl@0:  * methods with some default behavior such as returning constant values, or using the
sl@0:  * values from the first subfont.
sl@0:  *
sl@0:  * @draft ICU 3.0
sl@0:  */
sl@0: class U_LAYOUT_API LEFontInstance : public UObject
sl@0: {
sl@0: public:
sl@0: 
sl@0:     /**
sl@0:      * This virtual destructor is here so that the subclass
sl@0:      * destructors can be invoked through the base class.
sl@0:      *
sl@0:      * @stable ICU 2.8
sl@0:      */
sl@0:     virtual inline ~LEFontInstance() {};
sl@0: 
sl@0:     /**
sl@0:      * Get a physical font which can render the given text. For composite fonts,
sl@0:      * if there is no single physical font which can render all of the text,
sl@0:      * return a physical font which can render an initial substring of the text,
sl@0:      * and set the <code>offset</code> parameter to the end of that substring.
sl@0:      *
sl@0:      * Internally, the LayoutEngine works with runs of text all in the same
sl@0:      * font and script, so it is best to call this method with text which is
sl@0:      * in a single script, passing the script code in as a hint. If you don't
sl@0:      * know the script of the text, you can use zero, which is the script code
sl@0:      * for characters used in more than one script.
sl@0:      *
sl@0:      * The default implementation of this method is intended for instances of
sl@0:      * <code>LEFontInstance</code> which represent a physical font. It returns
sl@0:      * <code>this</code> and indicates that the entire string can be rendered.
sl@0:      *
sl@0:      * This method will return a valid <code>LEFontInstance</code> unless you
sl@0:      * have passed illegal parameters, or an internal error has been encountered. 
sl@0:      * For composite fonts, it may return the warning <code>LE_NO_SUBFONT_WARNING</code>
sl@0:      * to indicate that the returned font may not be able to render all of
sl@0:      * the text. Whenever a valid font is returned, the <code>offset</code> parameter
sl@0:      * will be advanced by at least one.
sl@0:      *
sl@0:      * Subclasses which implement composite fonts must override this method.
sl@0:      * Where it makes sense, they should use the script code as a hint to render
sl@0:      * characters from the COMMON script in the font which is used for the given
sl@0:      * script. For example, if the input text is a series of Arabic words separated
sl@0:      * by spaces, and the script code passed in is <code>arabScriptCode</code> you
sl@0:      * should return the font used for Arabic characters for all of the input text,
sl@0:      * including the spaces. If, on the other hand, the input text contains characters
sl@0:      * which cannot be rendered by the font used for Arabic characters, but which can
sl@0:      * be rendered by another font, you should return that font for those characters.
sl@0:      *
sl@0:      * @param chars   - the array of Unicode characters.
sl@0:      * @param offset  - a pointer to the starting offset in the text. On exit this
sl@0:      *                  will be set the the limit offset of the text which can be
sl@0:      *                  rendered using the returned font.
sl@0:      * @param limit   - the limit offset for the input text.
sl@0:      * @param script  - the script hint.
sl@0:      * @param success - set to an error code if the arguments are illegal, or no font
sl@0:      *                  can be returned for some reason. May also be set to
sl@0:      *                  <code>LE_NO_SUBFONT_WARNING</code> if the subfont which
sl@0:      *                  was returned cannot render all of the text.
sl@0:      *
sl@0:      * @return an <code>LEFontInstance</code> for the sub font which can render the characters, or
sl@0:      *         <code>NULL</code> if there is an error.
sl@0:      *
sl@0:      * @see LEScripts.h
sl@0:      *
sl@0:      * @stable ICU 3.2
sl@0:      */
sl@0:     virtual const LEFontInstance *getSubFont(const LEUnicode chars[], le_int32 *offset, le_int32 limit, le_int32 script, LEErrorCode &success) const;
sl@0: 
sl@0:     //
sl@0:     // Font file access
sl@0:     //
sl@0: 
sl@0:     /**
sl@0:      * This method reads a table from the font. Note that in general,
sl@0:      * it only makes sense to call this method on an <code>LEFontInstance</code>
sl@0:      * which represents a physical font - i.e. one which has been returned by
sl@0:      * <code>getSubFont()</code>. This is because each subfont in a composite font
sl@0:      * will have different tables, and there's no way to know which subfont to access.
sl@0:      *
sl@0:      * Subclasses which represent composite fonts should always return <code>NULL</code>.
sl@0:      *
sl@0:      * @param tableTag - the four byte table tag. (e.g. 'cmap') 
sl@0:      *
sl@0:      * @return the address of the table in memory, or <code>NULL</code>
sl@0:      *         if the table doesn't exist.
sl@0:      *
sl@0:      * @stable ICU 2.8
sl@0:      */
sl@0:     virtual const void *getFontTable(LETag tableTag) const = 0;
sl@0: 
sl@0:     /**
sl@0:      * This method is used to determine if the font can
sl@0:      * render the given character. This can usually be done
sl@0:      * by looking the character up in the font's character
sl@0:      * to glyph mapping.
sl@0:      *
sl@0:      * The default implementation of this method will return
sl@0:      * <code>TRUE</code> if <code>mapCharToGlyph(ch)</code>
sl@0:      * returns a non-zero value.
sl@0:      *
sl@0:      * @param ch - the character to be tested
sl@0:      *
sl@0:      * @return <code>TRUE</code> if the font can render ch.
sl@0:      *
sl@0:      * @stable ICU 3.2
sl@0:      */
sl@0:     virtual inline le_bool canDisplay(LEUnicode32 ch) const;
sl@0: 
sl@0:     /**
sl@0:      * This method returns the number of design units in
sl@0:      * the font's EM square.
sl@0:      *
sl@0:      * @return the number of design units pre EM.
sl@0:      *
sl@0:      * @stable ICU 2.8
sl@0:      */
sl@0:     virtual le_int32 getUnitsPerEM() const = 0;
sl@0: 
sl@0:     /**
sl@0:      * This method maps an array of character codes to an array of glyph
sl@0:      * indices, using the font's character to glyph map.
sl@0:      *
sl@0:      * The default implementation iterates over all of the characters and calls
sl@0:      * <code>mapCharToGlyph(ch, mapper)</code> on each one. It also handles surrogate
sl@0:      * characters, storing the glyph ID for the high surrogate, and a deleted glyph (0xFFFF)
sl@0:      * for the low surrogate.
sl@0:      *
sl@0:      * Most sublcasses will not need to implement this method.
sl@0:      *
sl@0:      * @param chars - the character array
sl@0:      * @param offset - the index of the first character
sl@0:      * @param count - the number of characters
sl@0:      * @param reverse - if <code>TRUE</code>, store the glyph indices in reverse order.
sl@0:      * @param mapper - the character mapper.
sl@0:      * @param glyphStorage - the object which contains the output glyph array
sl@0:      *
sl@0:      * @see LECharMapper
sl@0:      *
sl@0:      * @draft ICU 3.0
sl@0:      */
sl@0:     virtual void mapCharsToGlyphs(const LEUnicode chars[], le_int32 offset, le_int32 count, le_bool reverse, const LECharMapper *mapper, LEGlyphStorage &glyphStorage) const;
sl@0: 
sl@0:     /**
sl@0:      * This method maps a single character to a glyph index, using the
sl@0:      * font's character to glyph map. The default implementation of this
sl@0:      * method calls the mapper, and then calls <code>mapCharToGlyph(mappedCh)</code>.
sl@0:      *
sl@0:      * @param ch - the character
sl@0:      * @param mapper - the character mapper
sl@0:      *
sl@0:      * @return the glyph index
sl@0:      *
sl@0:      * @see LECharMapper
sl@0:      *
sl@0:      * @stable ICU 3.2
sl@0:      */
sl@0:     virtual LEGlyphID mapCharToGlyph(LEUnicode32 ch, const LECharMapper *mapper) const;
sl@0: 
sl@0:     /**
sl@0:      * This method maps a single character to a glyph index, using the
sl@0:      * font's character to glyph map. There is no default implementation
sl@0:      * of this method because it requires information about the platform
sl@0:      * font implementation.
sl@0:      *
sl@0:      * @param ch - the character
sl@0:      *
sl@0:      * @return the glyph index
sl@0:      *
sl@0:      * @stable ICU 3.2
sl@0:      */
sl@0:     virtual LEGlyphID mapCharToGlyph(LEUnicode32 ch) const = 0;
sl@0: 
sl@0:     //
sl@0:     // Metrics
sl@0:     //
sl@0: 
sl@0:     /**
sl@0:      * This method gets the X and Y advance of a particular glyph, in pixels.
sl@0:      *
sl@0:      * @param glyph - the glyph index
sl@0:      * @param advance - the X and Y pixel values will be stored here
sl@0:      *
sl@0:      * @stable ICU 3.2
sl@0:      */
sl@0:     virtual void getGlyphAdvance(LEGlyphID glyph, LEPoint &advance) const = 0;
sl@0: 
sl@0:     /**
sl@0:      * This method gets the hinted X and Y pixel coordinates of a particular
sl@0:      * point in the outline of the given glyph.
sl@0:      *
sl@0:      * @param glyph - the glyph index
sl@0:      * @param pointNumber - the number of the point
sl@0:      * @param point - the point's X and Y pixel values will be stored here
sl@0:      *
sl@0:      * @return <code>TRUE</code> if the point coordinates could be stored.
sl@0:      *
sl@0:      * @stable ICU 2.8
sl@0:      */
sl@0:     virtual le_bool getGlyphPoint(LEGlyphID glyph, le_int32 pointNumber, LEPoint &point) const = 0;
sl@0: 
sl@0:     /**
sl@0:      * This method returns the width of the font's EM square
sl@0:      * in pixels.
sl@0:      *
sl@0:      * @return the pixel width of the EM square
sl@0:      *
sl@0:      * @stable ICU 2.8
sl@0:      */
sl@0:     virtual float getXPixelsPerEm() const = 0;
sl@0: 
sl@0:     /**
sl@0:      * This method returns the height of the font's EM square
sl@0:      * in pixels.
sl@0:      *
sl@0:      * @return the pixel height of the EM square
sl@0:      *
sl@0:      * @stable ICU 2.8
sl@0:      */
sl@0:     virtual float getYPixelsPerEm() const = 0;
sl@0: 
sl@0:     /**
sl@0:      * This method converts font design units in the
sl@0:      * X direction to points.
sl@0:      *
sl@0:      * @param xUnits - design units in the X direction
sl@0:      *
sl@0:      * @return points in the X direction
sl@0:      *
sl@0:      * @stable ICU 3.2
sl@0:      */
sl@0:     virtual inline float xUnitsToPoints(float xUnits) const;
sl@0: 
sl@0:     /**
sl@0:      * This method converts font design units in the
sl@0:      * Y direction to points.
sl@0:      *
sl@0:      * @param yUnits - design units in the Y direction
sl@0:      *
sl@0:      * @return points in the Y direction
sl@0:      *
sl@0:      * @stable ICU 3.2
sl@0:      */
sl@0:     virtual inline float yUnitsToPoints(float yUnits) const;
sl@0: 
sl@0:     /**
sl@0:      * This method converts font design units to points.
sl@0:      *
sl@0:      * @param units - X and Y design units
sl@0:      * @param points - set to X and Y points
sl@0:      *
sl@0:      * @stable ICU 3.2
sl@0:      */
sl@0:     virtual inline void unitsToPoints(LEPoint &units, LEPoint &points) const;
sl@0: 
sl@0:     /**
sl@0:      * This method converts pixels in the
sl@0:      * X direction to font design units.
sl@0:      *
sl@0:      * @param xPixels - pixels in the X direction
sl@0:      *
sl@0:      * @return font design units in the X direction
sl@0:      *
sl@0:      * @stable ICU 3.2
sl@0:      */
sl@0:     virtual inline float xPixelsToUnits(float xPixels) const;
sl@0: 
sl@0:     /**
sl@0:      * This method converts pixels in the
sl@0:      * Y direction to font design units.
sl@0:      *
sl@0:      * @param yPixels - pixels in the Y direction
sl@0:      *
sl@0:      * @return font design units in the Y direction
sl@0:      *
sl@0:      * @stable ICU 3.2
sl@0:      */
sl@0:     virtual inline float yPixelsToUnits(float yPixels) const;
sl@0: 
sl@0:     /**
sl@0:      * This method converts pixels to font design units.
sl@0:      *
sl@0:      * @param pixels - X and Y pixel
sl@0:      * @param units - set to X and Y font design units
sl@0:      *
sl@0:      * @stable ICU 3.2
sl@0:      */
sl@0:     virtual inline void pixelsToUnits(LEPoint &pixels, LEPoint &units) const;
sl@0: 
sl@0:     /**
sl@0:      * Get the X scale factor from the font's transform. The default
sl@0:      * implementation of <code>transformFunits()</code> will call this method.
sl@0:      *
sl@0:      * @return the X scale factor.
sl@0:      *
sl@0:      *
sl@0:      * @see transformFunits
sl@0:      *
sl@0:      * @stable ICU 3.2
sl@0:      */
sl@0:     virtual float getScaleFactorX() const = 0;
sl@0: 
sl@0:     /**
sl@0:      * Get the Y scale factor from the font's transform. The default
sl@0:      * implementation of <code>transformFunits()</code> will call this method.
sl@0:      *
sl@0:      * @return the Yscale factor.
sl@0:      *
sl@0:      * @see transformFunits
sl@0:      *
sl@0:      * @stable ICU 3.2
sl@0:      */
sl@0:     virtual float getScaleFactorY() const = 0;
sl@0: 
sl@0:     /**
sl@0:      * This method transforms an X, Y point in font design units to a
sl@0:      * pixel coordinate, applying the font's transform. The default
sl@0:      * implementation of this method calls <code>getScaleFactorX()</code>
sl@0:      * and <code>getScaleFactorY()</code>.
sl@0:      *
sl@0:      * @param xFunits - the X coordinate in font design units
sl@0:      * @param yFunits - the Y coordinate in font design units
sl@0:      * @param pixels - the tranformed co-ordinate in pixels
sl@0:      *
sl@0:      * @see getScaleFactorX
sl@0:      * @see getScaleFactorY
sl@0:      *
sl@0:      * @stable ICU 3.2
sl@0:      */
sl@0:     virtual inline void transformFunits(float xFunits, float yFunits, LEPoint &pixels) const;
sl@0: 
sl@0:     /**
sl@0:      * This is a convenience method used to convert
sl@0:      * values in a 16.16 fixed point format to floating point.
sl@0:      *
sl@0:      * @param fixed - the fixed point value
sl@0:      *
sl@0:      * @return the floating point value
sl@0:      *
sl@0:      * @stable ICU 2.8
sl@0:      */
sl@0:     static inline float fixedToFloat(le_int32 fixed);
sl@0: 
sl@0:     /**
sl@0:      * This is a convenience method used to convert
sl@0:      * floating point values to 16.16 fixed point format.
sl@0:      *
sl@0:      * @param theFloat - the floating point value
sl@0:      *
sl@0:      * @return the fixed point value
sl@0:      *
sl@0:      * @stable ICU 2.8
sl@0:      */
sl@0:     static inline le_int32 floatToFixed(float theFloat);
sl@0: 
sl@0:     //
sl@0:     // These methods won't ever be called by the LayoutEngine,
sl@0:     // but are useful for clients of <code>LEFontInstance</code> who
sl@0:     // need to render text.
sl@0:     //
sl@0: 
sl@0:     /**
sl@0:      * Get the font's ascent.
sl@0:      *
sl@0:      * @return the font's ascent, in points. This value
sl@0:      * will always be positive.
sl@0:      *
sl@0:      * @stable ICU 3.2
sl@0:      */
sl@0:     virtual le_int32 getAscent() const = 0;
sl@0: 
sl@0:     /**
sl@0:      * Get the font's descent.
sl@0:      *
sl@0:      * @return the font's descent, in points. This value
sl@0:      * will always be positive.
sl@0:      *
sl@0:      * @stable ICU 3.2
sl@0:      */
sl@0:     virtual le_int32 getDescent() const = 0;
sl@0: 
sl@0:     /**
sl@0:      * Get the font's leading.
sl@0:      *
sl@0:      * @return the font's leading, in points. This value
sl@0:      * will always be positive.
sl@0:      *
sl@0:      * @stable ICU 3.2
sl@0:      */
sl@0:     virtual le_int32 getLeading() const = 0;
sl@0: 
sl@0:     /**
sl@0:      * Get the line height required to display text in
sl@0:      * this font. The default implementation of this method
sl@0:      * returns the sum of the ascent, descent, and leading.
sl@0:      *
sl@0:      * @return the line height, in points. This vaule will
sl@0:      * always be positive.
sl@0:      *
sl@0:      * @stable ICU 3.2
sl@0:      */
sl@0:     virtual le_int32 getLineHeight() const;
sl@0: 
sl@0:     /**
sl@0:      * ICU "poor man's RTTI", returns a UClassID for the actual class.
sl@0:      *
sl@0:      * @stable ICU 3.2
sl@0:      */
sl@0:     virtual UClassID getDynamicClassID() const;
sl@0: 
sl@0:     /**
sl@0:      * ICU "poor man's RTTI", returns a UClassID for this class.
sl@0:      *
sl@0:      * @stable ICU 3.2
sl@0:      */
sl@0:     static UClassID getStaticClassID();
sl@0: 
sl@0: };
sl@0: 
sl@0: inline le_bool LEFontInstance::canDisplay(LEUnicode32 ch) const
sl@0: {
sl@0:     return LE_GET_GLYPH(mapCharToGlyph(ch)) != 0;
sl@0: }
sl@0: 
sl@0: inline float LEFontInstance::xUnitsToPoints(float xUnits) const
sl@0: {
sl@0:     return (xUnits * getXPixelsPerEm()) / (float) getUnitsPerEM();
sl@0: }
sl@0: 
sl@0: inline float LEFontInstance::yUnitsToPoints(float yUnits) const
sl@0: {
sl@0:     return (yUnits * getYPixelsPerEm()) / (float) getUnitsPerEM();
sl@0: }
sl@0: 
sl@0: inline void LEFontInstance::unitsToPoints(LEPoint &units, LEPoint &points) const
sl@0: {
sl@0:     points.fX = xUnitsToPoints(units.fX);
sl@0:     points.fY = yUnitsToPoints(units.fY);
sl@0: }
sl@0: 
sl@0: inline float LEFontInstance::xPixelsToUnits(float xPixels) const
sl@0: {
sl@0:     return (xPixels * getUnitsPerEM()) / (float) getXPixelsPerEm();
sl@0: }
sl@0: 
sl@0: inline float LEFontInstance::yPixelsToUnits(float yPixels) const
sl@0: {
sl@0:     return (yPixels * getUnitsPerEM()) / (float) getYPixelsPerEm();
sl@0: }
sl@0: 
sl@0: inline void LEFontInstance::pixelsToUnits(LEPoint &pixels, LEPoint &units) const
sl@0: {
sl@0:     units.fX = xPixelsToUnits(pixels.fX);
sl@0:     units.fY = yPixelsToUnits(pixels.fY);
sl@0: }
sl@0: 
sl@0: inline void LEFontInstance::transformFunits(float xFunits, float yFunits, LEPoint &pixels) const
sl@0: {
sl@0:     pixels.fX = xUnitsToPoints(xFunits) * getScaleFactorX();
sl@0:     pixels.fY = yUnitsToPoints(yFunits) * getScaleFactorY();
sl@0: }
sl@0: 
sl@0: inline float LEFontInstance::fixedToFloat(le_int32 fixed)
sl@0: {
sl@0:     return (float) (fixed / 65536.0);
sl@0: }
sl@0: 
sl@0: inline le_int32 LEFontInstance::floatToFixed(float theFloat)
sl@0: {
sl@0:     return (le_int32) (theFloat * 65536.0);
sl@0: }
sl@0: 
sl@0: inline le_int32 LEFontInstance::getLineHeight() const
sl@0: {
sl@0:     return getAscent() + getDescent() + getLeading();
sl@0: }
sl@0: 
sl@0: U_NAMESPACE_END
sl@0: #endif
sl@0: 
sl@0: