4 * (C) Copyright IBM Corp. 1998-2004 - All Rights Reserved
8 #ifndef __LEFONTINSTANCE_H
9 #define __LEFONTINSTANCE_H
16 * Instances of this class are used by <code>LEFontInstance::mapCharsToGlyphs</code> and
17 * <code>LEFontInstance::mapCharToGlyph</code> to adjust character codes before the character
18 * to glyph mapping process. Examples of this are filtering out control characters
19 * and character mirroring - replacing a character which has both a left and a right
20 * hand form with the opposite form.
24 class LECharMapper
/* not : public UObject because this is an interface/mixin class */
31 virtual inline ~LECharMapper() {};
34 * This method does the adjustments.
36 * @param ch - the input character
38 * @return the adjusted character
42 virtual LEUnicode32
mapChar(LEUnicode32 ch
) const = 0;
46 * This is a forward reference to the class which holds the per-glyph
54 * This is a virtual base class that serves as the interface between a LayoutEngine
55 * and the platform font environment. It allows a LayoutEngine to access font tables, do
56 * character to glyph mapping, and obtain metrics information without knowing any platform
57 * specific details. There are also a few utility methods for converting between points,
58 * pixels and funits. (font design units)
60 * An instance of an <code>LEFontInstance</code> represents a font at a particular point
61 * size. Each instance can represent either a single physical font, or a composite font.
62 * A composite font is a collection of physical fonts, each of which contains a subset of
63 * the characters contained in the composite font.
65 * Note: with the exception of <code>getSubFont</code>, the methods in this class only
66 * make sense for a physical font. If you have an <code>LEFontInstance</code> which
67 * represents a composite font you should only call the methods below which have
68 * an <code>LEGlyphID</code>, an <code>LEUnicode</code> or an <code>LEUnicode32</code>
69 * as one of the arguments because these can be used to select a particular subfont.
71 * Subclasses which implement composite fonts should supply an implementation of these
72 * methods with some default behavior such as returning constant values, or using the
73 * values from the first subfont.
77 class U_LAYOUT_API LEFontInstance
: public UObject
82 * This virtual destructor is here so that the subclass
83 * destructors can be invoked through the base class.
87 virtual inline ~LEFontInstance() {};
90 * Get a physical font which can render the given text. For composite fonts,
91 * if there is no single physical font which can render all of the text,
92 * return a physical font which can render an initial substring of the text,
93 * and set the <code>offset</code> parameter to the end of that substring.
95 * Internally, the LayoutEngine works with runs of text all in the same
96 * font and script, so it is best to call this method with text which is
97 * in a single script, passing the script code in as a hint. If you don't
98 * know the script of the text, you can use zero, which is the script code
99 * for characters used in more than one script.
101 * The default implementation of this method is intended for instances of
102 * <code>LEFontInstance</code> which represent a physical font. It returns
103 * <code>this</code> and indicates that the entire string can be rendered.
105 * This method will return a valid <code>LEFontInstance</code> unless you
106 * have passed illegal parameters, or an internal error has been encountered.
107 * For composite fonts, it may return the warning <code>LE_NO_SUBFONT_WARNING</code>
108 * to indicate that the returned font may not be able to render all of
109 * the text. Whenever a valid font is returned, the <code>offset</code> parameter
110 * will be advanced by at least one.
112 * Subclasses which implement composite fonts must override this method.
113 * Where it makes sense, they should use the script code as a hint to render
114 * characters from the COMMON script in the font which is used for the given
115 * script. For example, if the input text is a series of Arabic words separated
116 * by spaces, and the script code passed in is <code>arabScriptCode</code> you
117 * should return the font used for Arabic characters for all of the input text,
118 * including the spaces. If, on the other hand, the input text contains characters
119 * which cannot be rendered by the font used for Arabic characters, but which can
120 * be rendered by another font, you should return that font for those characters.
122 * @param chars - the array of Unicode characters.
123 * @param offset - a pointer to the starting offset in the text. On exit this
124 * will be set the the limit offset of the text which can be
125 * rendered using the returned font.
126 * @param limit - the limit offset for the input text.
127 * @param script - the script hint.
128 * @param success - set to an error code if the arguments are illegal, or no font
129 * can be returned for some reason. May also be set to
130 * <code>LE_NO_SUBFONT_WARNING</code> if the subfont which
131 * was returned cannot render all of the text.
133 * @return an <code>LEFontInstance</code> for the sub font which can render the characters, or
134 * <code>NULL</code> if there is an error.
140 virtual const LEFontInstance
*getSubFont(const LEUnicode chars
[], le_int32
*offset
, le_int32 limit
, le_int32 script
, LEErrorCode
&success
) const;
147 * This method reads a table from the font. Note that in general,
148 * it only makes sense to call this method on an <code>LEFontInstance</code>
149 * which represents a physical font - i.e. one which has been returned by
150 * <code>getSubFont()</code>. This is because each subfont in a composite font
151 * will have different tables, and there's no way to know which subfont to access.
153 * Subclasses which represent composite fonts should always return <code>NULL</code>.
155 * @param tableTag - the four byte table tag. (e.g. 'cmap')
157 * @return the address of the table in memory, or <code>NULL</code>
158 * if the table doesn't exist.
162 virtual const void *getFontTable(LETag tableTag
) const = 0;
165 * This method is used to determine if the font can
166 * render the given character. This can usually be done
167 * by looking the character up in the font's character
170 * The default implementation of this method will return
171 * <code>TRUE</code> if <code>mapCharToGlyph(ch)</code>
172 * returns a non-zero value.
174 * @param ch - the character to be tested
176 * @return <code>TRUE</code> if the font can render ch.
180 virtual le_bool
canDisplay(LEUnicode32 ch
) const;
183 * This method returns the number of design units in
184 * the font's EM square.
186 * @return the number of design units pre EM.
190 virtual le_int32
getUnitsPerEM() const = 0;
193 * This method maps an array of character codes to an array of glyph
194 * indices, using the font's character to glyph map.
196 * The default implementation iterates over all of the characters and calls
197 * <code>mapCharToGlyph(ch, mapper)</code> on each one. It also handles surrogate
198 * characters, storing the glyph ID for the high surrogate, and a deleted glyph (0xFFFF)
199 * for the low surrogate.
201 * Most sublcasses will not need to implement this method.
203 * @param chars - the character array
204 * @param offset - the index of the first character
205 * @param count - the number of characters
206 * @param reverse - if <code>TRUE</code>, store the glyph indices in reverse order.
207 * @param mapper - the character mapper.
208 * @param glyphStorage - the object which contains the output glyph array
214 virtual void mapCharsToGlyphs(const LEUnicode chars
[], le_int32 offset
, le_int32 count
, le_bool reverse
, const LECharMapper
*mapper
, LEGlyphStorage
&glyphStorage
) const;
217 * This method maps a single character to a glyph index, using the
218 * font's character to glyph map. The default implementation of this
219 * method calls the mapper, and then calls <code>mapCharToGlyph(mappedCh)</code>.
221 * @param ch - the character
222 * @param mapper - the character mapper
224 * @return the glyph index
230 virtual LEGlyphID
mapCharToGlyph(LEUnicode32 ch
, const LECharMapper
*mapper
) const;
233 * This method maps a single character to a glyph index, using the
234 * font's character to glyph map. There is no default implementation
235 * of this method because it requires information about the platform
236 * font implementation.
238 * @param ch - the character
240 * @return the glyph index
244 virtual LEGlyphID
mapCharToGlyph(LEUnicode32 ch
) const = 0;
251 * This method gets the X and Y advance of a particular glyph, in pixels.
253 * @param glyph - the glyph index
254 * @param advance - the X and Y pixel values will be stored here
258 virtual void getGlyphAdvance(LEGlyphID glyph
, LEPoint
&advance
) const = 0;
261 * This method gets the hinted X and Y pixel coordinates of a particular
262 * point in the outline of the given glyph.
264 * @param glyph - the glyph index
265 * @param pointNumber - the number of the point
266 * @param point - the point's X and Y pixel values will be stored here
268 * @return <code>TRUE</code> if the point coordinates could be stored.
272 virtual le_bool
getGlyphPoint(LEGlyphID glyph
, le_int32 pointNumber
, LEPoint
&point
) const = 0;
275 * This method returns the width of the font's EM square
278 * @return the pixel width of the EM square
282 virtual float getXPixelsPerEm() const = 0;
285 * This method returns the height of the font's EM square
288 * @return the pixel height of the EM square
292 virtual float getYPixelsPerEm() const = 0;
295 * This method converts font design units in the
296 * X direction to points.
298 * @param xUnits - design units in the X direction
300 * @return points in the X direction
304 virtual float xUnitsToPoints(float xUnits
) const;
307 * This method converts font design units in the
308 * Y direction to points.
310 * @param yUnits - design units in the Y direction
312 * @return points in the Y direction
316 virtual float yUnitsToPoints(float yUnits
) const;
319 * This method converts font design units to points.
321 * @param units - X and Y design units
322 * @param points - set to X and Y points
326 virtual void unitsToPoints(LEPoint
&units
, LEPoint
&points
) const;
329 * This method converts pixels in the
330 * X direction to font design units.
332 * @param xPixels - pixels in the X direction
334 * @return font design units in the X direction
338 virtual float xPixelsToUnits(float xPixels
) const;
341 * This method converts pixels in the
342 * Y direction to font design units.
344 * @param yPixels - pixels in the Y direction
346 * @return font design units in the Y direction
350 virtual float yPixelsToUnits(float yPixels
) const;
353 * This method converts pixels to font design units.
355 * @param pixels - X and Y pixel
356 * @param units - set to X and Y font design units
360 virtual void pixelsToUnits(LEPoint
&pixels
, LEPoint
&units
) const;
363 * Get the X scale factor from the font's transform. The default
364 * implementation of <code>transformFunits()</code> will call this method.
366 * @return the X scale factor.
369 * @see transformFunits
373 virtual float getScaleFactorX() const = 0;
376 * Get the Y scale factor from the font's transform. The default
377 * implementation of <code>transformFunits()</code> will call this method.
379 * @return the Yscale factor.
381 * @see transformFunits
385 virtual float getScaleFactorY() const = 0;
388 * This method transforms an X, Y point in font design units to a
389 * pixel coordinate, applying the font's transform. The default
390 * implementation of this method calls <code>getScaleFactorX()</code>
391 * and <code>getScaleFactorY()</code>.
393 * @param xFunits - the X coordinate in font design units
394 * @param yFunits - the Y coordinate in font design units
395 * @param pixels - the tranformed co-ordinate in pixels
397 * @see getScaleFactorX
398 * @see getScaleFactorY
402 virtual void transformFunits(float xFunits
, float yFunits
, LEPoint
&pixels
) const;
405 * This is a convenience method used to convert
406 * values in a 16.16 fixed point format to floating point.
408 * @param fixed - the fixed point value
410 * @return the floating point value
414 static float fixedToFloat(le_int32 fixed
);
417 * This is a convenience method used to convert
418 * floating point values to 16.16 fixed point format.
420 * @param theFloat - the floating point value
422 * @return the fixed point value
426 static le_int32
floatToFixed(float theFloat
);
429 // These methods won't ever be called by the LayoutEngine,
430 // but are useful for clients of <code>LEFontInstance</code> who
431 // need to render text.
435 * Get the font's ascent.
437 * @return the font's ascent, in points. This value
438 * will always be positive.
442 virtual le_int32
getAscent() const = 0;
445 * Get the font's descent.
447 * @return the font's descent, in points. This value
448 * will always be positive.
452 virtual le_int32
getDescent() const = 0;
455 * Get the font's leading.
457 * @return the font's leading, in points. This value
458 * will always be positive.
462 virtual le_int32
getLeading() const = 0;
465 * Get the line height required to display text in
466 * this font. The default implementation of this method
467 * returns the sum of the ascent, descent, and leading.
469 * @return the line height, in points. This vaule will
470 * always be positive.
474 virtual le_int32
getLineHeight() const;
477 * ICU "poor man's RTTI", returns a UClassID for the actual class.
481 virtual UClassID
getDynamicClassID() const;
484 * ICU "poor man's RTTI", returns a UClassID for this class.
488 static UClassID
getStaticClassID();
492 inline le_bool
LEFontInstance::canDisplay(LEUnicode32 ch
) const
494 return LE_GET_GLYPH(mapCharToGlyph(ch
)) != 0;
497 inline float LEFontInstance::xUnitsToPoints(float xUnits
) const
499 return (xUnits
* getXPixelsPerEm()) / (float) getUnitsPerEM();
502 inline float LEFontInstance::yUnitsToPoints(float yUnits
) const
504 return (yUnits
* getYPixelsPerEm()) / (float) getUnitsPerEM();
507 inline void LEFontInstance::unitsToPoints(LEPoint
&units
, LEPoint
&points
) const
509 points
.fX
= xUnitsToPoints(units
.fX
);
510 points
.fY
= yUnitsToPoints(units
.fY
);
513 inline float LEFontInstance::xPixelsToUnits(float xPixels
) const
515 return (xPixels
* getUnitsPerEM()) / (float) getXPixelsPerEm();
518 inline float LEFontInstance::yPixelsToUnits(float yPixels
) const
520 return (yPixels
* getUnitsPerEM()) / (float) getYPixelsPerEm();
523 inline void LEFontInstance::pixelsToUnits(LEPoint
&pixels
, LEPoint
&units
) const
525 units
.fX
= xPixelsToUnits(pixels
.fX
);
526 units
.fY
= yPixelsToUnits(pixels
.fY
);
529 inline void LEFontInstance::transformFunits(float xFunits
, float yFunits
, LEPoint
&pixels
) const
531 pixels
.fX
= xUnitsToPoints(xFunits
) * getScaleFactorX();
532 pixels
.fY
= yUnitsToPoints(yFunits
) * getScaleFactorY();
535 inline float LEFontInstance::fixedToFloat(le_int32 fixed
)
537 return (float) (fixed
/ 65536.0);
540 inline le_int32
LEFontInstance::floatToFixed(float theFloat
)
542 return (le_int32
) (theFloat
* 65536.0);
545 inline le_int32
LEFontInstance::getLineHeight() const
547 return getAscent() + getDescent() + getLeading();