4 * (C) Copyright IBM Corp. 1998-2012 - All Rights Reserved
8 #ifndef __LEFONTINSTANCE_H
9 #define __LEFONTINSTANCE_H
14 * \brief C++ API: Layout Engine Font Instance object
20 * Instances of this class are used by <code>LEFontInstance::mapCharsToGlyphs</code> and
21 * <code>LEFontInstance::mapCharToGlyph</code> to adjust character codes before the character
22 * to glyph mapping process. Examples of this are filtering out control characters
23 * and character mirroring - replacing a character which has both a left and a right
24 * hand form with the opposite form.
28 class LECharMapper
/* not : public UObject because this is an interface/mixin class */
35 virtual ~LECharMapper();
38 * This method does the adjustments.
40 * @param ch - the input character
42 * @return the adjusted character
46 virtual LEUnicode32
mapChar(LEUnicode32 ch
) const = 0;
50 * This is a forward reference to the class which holds the per-glyph
58 * This is a virtual base class that serves as the interface between a LayoutEngine
59 * and the platform font environment. It allows a LayoutEngine to access font tables, do
60 * character to glyph mapping, and obtain metrics information without knowing any platform
61 * specific details. There are also a few utility methods for converting between points,
62 * pixels and funits. (font design units)
64 * An instance of an <code>LEFontInstance</code> represents a font at a particular point
65 * size. Each instance can represent either a single physical font, or a composite font.
66 * A composite font is a collection of physical fonts, each of which contains a subset of
67 * the characters contained in the composite font.
69 * Note: with the exception of <code>getSubFont</code>, the methods in this class only
70 * make sense for a physical font. If you have an <code>LEFontInstance</code> which
71 * represents a composite font you should only call the methods below which have
72 * an <code>LEGlyphID</code>, an <code>LEUnicode</code> or an <code>LEUnicode32</code>
73 * as one of the arguments because these can be used to select a particular subfont.
75 * Subclasses which implement composite fonts should supply an implementation of these
76 * methods with some default behavior such as returning constant values, or using the
77 * values from the first subfont.
81 class U_LAYOUT_API LEFontInstance
: public UObject
86 * This virtual destructor is here so that the subclass
87 * destructors can be invoked through the base class.
91 virtual ~LEFontInstance();
94 * Get a physical font which can render the given text. For composite fonts,
95 * if there is no single physical font which can render all of the text,
96 * return a physical font which can render an initial substring of the text,
97 * and set the <code>offset</code> parameter to the end of that substring.
99 * Internally, the LayoutEngine works with runs of text all in the same
100 * font and script, so it is best to call this method with text which is
101 * in a single script, passing the script code in as a hint. If you don't
102 * know the script of the text, you can use zero, which is the script code
103 * for characters used in more than one script.
105 * The default implementation of this method is intended for instances of
106 * <code>LEFontInstance</code> which represent a physical font. It returns
107 * <code>this</code> and indicates that the entire string can be rendered.
109 * This method will return a valid <code>LEFontInstance</code> unless you
110 * have passed illegal parameters, or an internal error has been encountered.
111 * For composite fonts, it may return the warning <code>LE_NO_SUBFONT_WARNING</code>
112 * to indicate that the returned font may not be able to render all of
113 * the text. Whenever a valid font is returned, the <code>offset</code> parameter
114 * will be advanced by at least one.
116 * Subclasses which implement composite fonts must override this method.
117 * Where it makes sense, they should use the script code as a hint to render
118 * characters from the COMMON script in the font which is used for the given
119 * script. For example, if the input text is a series of Arabic words separated
120 * by spaces, and the script code passed in is <code>arabScriptCode</code> you
121 * should return the font used for Arabic characters for all of the input text,
122 * including the spaces. If, on the other hand, the input text contains characters
123 * which cannot be rendered by the font used for Arabic characters, but which can
124 * be rendered by another font, you should return that font for those characters.
126 * @param chars - the array of Unicode characters.
127 * @param offset - a pointer to the starting offset in the text. On exit this
128 * will be set the the limit offset of the text which can be
129 * rendered using the returned font.
130 * @param limit - the limit offset for the input text.
131 * @param script - the script hint.
132 * @param success - set to an error code if the arguments are illegal, or no font
133 * can be returned for some reason. May also be set to
134 * <code>LE_NO_SUBFONT_WARNING</code> if the subfont which
135 * was returned cannot render all of the text.
137 * @return an <code>LEFontInstance</code> for the sub font which can render the characters, or
138 * <code>NULL</code> if there is an error.
144 virtual const LEFontInstance
*getSubFont(const LEUnicode chars
[], le_int32
*offset
, le_int32 limit
, le_int32 script
, LEErrorCode
&success
) const;
151 * This method reads a table from the font. Note that in general,
152 * it only makes sense to call this method on an <code>LEFontInstance</code>
153 * which represents a physical font - i.e. one which has been returned by
154 * <code>getSubFont()</code>. This is because each subfont in a composite font
155 * will have different tables, and there's no way to know which subfont to access.
157 * Subclasses which represent composite fonts should always return <code>NULL</code>.
159 * @param tableTag - the four byte table tag. (e.g. 'cmap')
161 * @return the address of the table in memory, or <code>NULL</code>
162 * if the table doesn't exist.
166 virtual const void *getFontTable(LETag tableTag
) const = 0;
169 * This method reads a table from the font. Note that in general,
170 * it only makes sense to call this method on an <code>LEFontInstance</code>
171 * which represents a physical font - i.e. one which has been returned by
172 * <code>getSubFont()</code>. This is because each subfont in a composite font
173 * will have different tables, and there's no way to know which subfont to access.
175 * Subclasses which represent composite fonts should always return <code>NULL</code>.
177 * This version sets a length, for range checking.
179 * @param tableTag - the four byte table tag. (e.g. 'cmap')
180 * @param length - ignored on entry, on exit will be the length of the table if known, or -1 if unknown.
181 * @return the address of the table in memory, or <code>NULL</code>
182 * if the table doesn't exist.
185 virtual const void* getFontTable(LETag tableTag
, size_t &length
) const { length
=-1; return getFontTable(tableTag
); } /* -1 = unknown length */
188 * This method is used to determine if the font can
189 * render the given character. This can usually be done
190 * by looking the character up in the font's character
193 * The default implementation of this method will return
194 * <code>TRUE</code> if <code>mapCharToGlyph(ch)</code>
195 * returns a non-zero value.
197 * @param ch - the character to be tested
199 * @return <code>TRUE</code> if the font can render ch.
203 virtual le_bool
canDisplay(LEUnicode32 ch
) const;
206 * This method returns the number of design units in
207 * the font's EM square.
209 * @return the number of design units pre EM.
213 virtual le_int32
getUnitsPerEM() const = 0;
216 * This method maps an array of character codes to an array of glyph
217 * indices, using the font's character to glyph map.
219 * The default implementation iterates over all of the characters and calls
220 * <code>mapCharToGlyph(ch, mapper)</code> on each one. It also handles surrogate
221 * characters, storing the glyph ID for the high surrogate, and a deleted glyph (0xFFFF)
222 * for the low surrogate.
224 * Most sublcasses will not need to implement this method.
226 * @param chars - the character array
227 * @param offset - the index of the first character
228 * @param count - the number of characters
229 * @param reverse - if <code>TRUE</code>, store the glyph indices in reverse order.
230 * @param mapper - the character mapper.
231 * @param filterZeroWidth - <code>TRUE</code> if ZWJ / ZWNJ characters should map to a glyph w/ no contours.
232 * @param glyphStorage - the object which contains the output glyph array
238 virtual void mapCharsToGlyphs(const LEUnicode chars
[], le_int32 offset
, le_int32 count
, le_bool reverse
, const LECharMapper
*mapper
, le_bool filterZeroWidth
, LEGlyphStorage
&glyphStorage
) const;
241 * This method maps a single character to a glyph index, using the
242 * font's character to glyph map. The default implementation of this
243 * method calls the mapper, and then calls <code>mapCharToGlyph(mappedCh)</code>.
245 * @param ch - the character
246 * @param mapper - the character mapper
247 * @param filterZeroWidth - <code>TRUE</code> if ZWJ / ZWNJ characters should map to a glyph w/ no contours.
249 * @return the glyph index
255 virtual LEGlyphID
mapCharToGlyph(LEUnicode32 ch
, const LECharMapper
*mapper
, le_bool filterZeroWidth
) const;
258 * This method maps a single character to a glyph index, using the
259 * font's character to glyph map. The default implementation of this
260 * method calls the mapper, and then calls <code>mapCharToGlyph(mappedCh)</code>.
262 * @param ch - the character
263 * @param mapper - the character mapper
265 * @return the glyph index
271 virtual LEGlyphID
mapCharToGlyph(LEUnicode32 ch
, const LECharMapper
*mapper
) const;
274 * This method maps a single character to a glyph index, using the
275 * font's character to glyph map. There is no default implementation
276 * of this method because it requires information about the platform
277 * font implementation.
279 * @param ch - the character
281 * @return the glyph index
285 virtual LEGlyphID
mapCharToGlyph(LEUnicode32 ch
) const = 0;
292 * This method gets the X and Y advance of a particular glyph, in pixels.
294 * @param glyph - the glyph index
295 * @param advance - the X and Y pixel values will be stored here
299 virtual void getGlyphAdvance(LEGlyphID glyph
, LEPoint
&advance
) const = 0;
302 * This method gets the hinted X and Y pixel coordinates of a particular
303 * point in the outline of the given glyph.
305 * @param glyph - the glyph index
306 * @param pointNumber - the number of the point
307 * @param point - the point's X and Y pixel values will be stored here
309 * @return <code>TRUE</code> if the point coordinates could be stored.
313 virtual le_bool
getGlyphPoint(LEGlyphID glyph
, le_int32 pointNumber
, LEPoint
&point
) const = 0;
316 * This method returns the width of the font's EM square
319 * @return the pixel width of the EM square
323 virtual float getXPixelsPerEm() const = 0;
326 * This method returns the height of the font's EM square
329 * @return the pixel height of the EM square
333 virtual float getYPixelsPerEm() const = 0;
336 * This method converts font design units in the
337 * X direction to points.
339 * @param xUnits - design units in the X direction
341 * @return points in the X direction
345 virtual float xUnitsToPoints(float xUnits
) const;
348 * This method converts font design units in the
349 * Y direction to points.
351 * @param yUnits - design units in the Y direction
353 * @return points in the Y direction
357 virtual float yUnitsToPoints(float yUnits
) const;
360 * This method converts font design units to points.
362 * @param units - X and Y design units
363 * @param points - set to X and Y points
367 virtual void unitsToPoints(LEPoint
&units
, LEPoint
&points
) const;
370 * This method converts pixels in the
371 * X direction to font design units.
373 * @param xPixels - pixels in the X direction
375 * @return font design units in the X direction
379 virtual float xPixelsToUnits(float xPixels
) const;
382 * This method converts pixels in the
383 * Y direction to font design units.
385 * @param yPixels - pixels in the Y direction
387 * @return font design units in the Y direction
391 virtual float yPixelsToUnits(float yPixels
) const;
394 * This method converts pixels to font design units.
396 * @param pixels - X and Y pixel
397 * @param units - set to X and Y font design units
401 virtual void pixelsToUnits(LEPoint
&pixels
, LEPoint
&units
) const;
404 * Get the X scale factor from the font's transform. The default
405 * implementation of <code>transformFunits()</code> will call this method.
407 * @return the X scale factor.
410 * @see transformFunits
414 virtual float getScaleFactorX() const = 0;
417 * Get the Y scale factor from the font's transform. The default
418 * implementation of <code>transformFunits()</code> will call this method.
420 * @return the Yscale factor.
422 * @see transformFunits
426 virtual float getScaleFactorY() const = 0;
429 * This method transforms an X, Y point in font design units to a
430 * pixel coordinate, applying the font's transform. The default
431 * implementation of this method calls <code>getScaleFactorX()</code>
432 * and <code>getScaleFactorY()</code>.
434 * @param xFunits - the X coordinate in font design units
435 * @param yFunits - the Y coordinate in font design units
436 * @param pixels - the tranformed co-ordinate in pixels
438 * @see getScaleFactorX
439 * @see getScaleFactorY
443 virtual void transformFunits(float xFunits
, float yFunits
, LEPoint
&pixels
) const;
446 * This is a convenience method used to convert
447 * values in a 16.16 fixed point format to floating point.
449 * @param fixed - the fixed point value
451 * @return the floating point value
455 static inline float fixedToFloat(le_int32 fixed
);
458 * This is a convenience method used to convert
459 * floating point values to 16.16 fixed point format.
461 * @param theFloat - the floating point value
463 * @return the fixed point value
467 static inline le_int32
floatToFixed(float theFloat
);
470 // These methods won't ever be called by the LayoutEngine,
471 // but are useful for clients of <code>LEFontInstance</code> who
472 // need to render text.
476 * Get the font's ascent.
478 * @return the font's ascent, in points. This value
479 * will always be positive.
483 virtual le_int32
getAscent() const = 0;
486 * Get the font's descent.
488 * @return the font's descent, in points. This value
489 * will always be positive.
493 virtual le_int32
getDescent() const = 0;
496 * Get the font's leading.
498 * @return the font's leading, in points. This value
499 * will always be positive.
503 virtual le_int32
getLeading() const = 0;
506 * Get the line height required to display text in
507 * this font. The default implementation of this method
508 * returns the sum of the ascent, descent, and leading.
510 * @return the line height, in points. This vaule will
511 * always be positive.
515 virtual le_int32
getLineHeight() const;
518 * ICU "poor man's RTTI", returns a UClassID for the actual class.
522 virtual UClassID
getDynamicClassID() const;
525 * ICU "poor man's RTTI", returns a UClassID for this class.
529 static UClassID
getStaticClassID();
533 inline float LEFontInstance::fixedToFloat(le_int32 fixed
)
535 return (float) (fixed
/ 65536.0);
538 inline le_int32
LEFontInstance::floatToFixed(float theFloat
)
540 return (le_int32
) (theFloat
* 65536.0);