]> git.saurik.com Git - apple/icu.git/blob - icuSources/layout/LEFontInstance.h
ICU-6.2.4.tar.gz
[apple/icu.git] / icuSources / layout / LEFontInstance.h
1
2 /*
3 *
4 * (C) Copyright IBM Corp. 1998-2004 - All Rights Reserved
5 *
6 */
7
8 #ifndef __LEFONTINSTANCE_H
9 #define __LEFONTINSTANCE_H
10
11 #include "LETypes.h"
12
13 U_NAMESPACE_BEGIN
14
15 /**
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.
21 *
22 * @draft ICU 2.2
23 */
24 class LECharMapper /* not : public UObject because this is an interface/mixin class */
25 {
26 public:
27 /**
28 * Destructor.
29 * @draft ICU 2.4
30 */
31 virtual inline ~LECharMapper() {};
32
33 /**
34 * This method does the adjustments.
35 *
36 * @param ch - the input character
37 *
38 * @return the adjusted character
39 *
40 * @stable ICU 2.8
41 */
42 virtual LEUnicode32 mapChar(LEUnicode32 ch) const = 0;
43 };
44
45 /**
46 * This is a forward reference to the class which holds the per-glyph
47 * storage.
48 *
49 * @draft ICU 3.0
50 */
51 class LEGlyphStorage;
52
53 /**
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)
59 *
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.
64 *
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.
70 *
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.
74 *
75 * @draft ICU 3.0
76 */
77 class U_LAYOUT_API LEFontInstance : public UObject
78 {
79 public:
80
81 /**
82 * This virtual destructor is here so that the subclass
83 * destructors can be invoked through the base class.
84 *
85 * @stable ICU 2.8
86 */
87 virtual inline ~LEFontInstance() {};
88
89 /**
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.
94 *
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.
100 *
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.
104 *
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.
111 *
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.
121 *
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.
132 *
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.
135 *
136 * @see LEScripts.h
137 *
138 * @draft ICU 2.6
139 */
140 virtual const LEFontInstance *getSubFont(const LEUnicode chars[], le_int32 *offset, le_int32 limit, le_int32 script, LEErrorCode &success) const;
141
142 //
143 // Font file access
144 //
145
146 /**
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.
152 *
153 * Subclasses which represent composite fonts should always return <code>NULL</code>.
154 *
155 * @param tableTag - the four byte table tag. (e.g. 'cmap')
156 *
157 * @return the address of the table in memory, or <code>NULL</code>
158 * if the table doesn't exist.
159 *
160 * @stable ICU 2.8
161 */
162 virtual const void *getFontTable(LETag tableTag) const = 0;
163
164 /**
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
168 * to glyph mapping.
169 *
170 * The default implementation of this method will return
171 * <code>TRUE</code> if <code>mapCharToGlyph(ch)</code>
172 * returns a non-zero value.
173 *
174 * @param ch - the character to be tested
175 *
176 * @return <code>TRUE</code> if the font can render ch.
177 *
178 * @draft ICU 2.6
179 */
180 virtual le_bool canDisplay(LEUnicode32 ch) const;
181
182 /**
183 * This method returns the number of design units in
184 * the font's EM square.
185 *
186 * @return the number of design units pre EM.
187 *
188 * @stable ICU 2.8
189 */
190 virtual le_int32 getUnitsPerEM() const = 0;
191
192 /**
193 * This method maps an array of character codes to an array of glyph
194 * indices, using the font's character to glyph map.
195 *
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.
200 *
201 * Most sublcasses will not need to implement this method.
202 *
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
209 *
210 * @see LECharMapper
211 *
212 * @draft ICU 3.0
213 */
214 virtual void mapCharsToGlyphs(const LEUnicode chars[], le_int32 offset, le_int32 count, le_bool reverse, const LECharMapper *mapper, LEGlyphStorage &glyphStorage) const;
215
216 /**
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>.
220 *
221 * @param ch - the character
222 * @param mapper - the character mapper
223 *
224 * @return the glyph index
225 *
226 * @see LECharMapper
227 *
228 * @draft ICU 2.6
229 */
230 virtual LEGlyphID mapCharToGlyph(LEUnicode32 ch, const LECharMapper *mapper) const;
231
232 /**
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.
237 *
238 * @param ch - the character
239 *
240 * @return the glyph index
241 *
242 * @draft ICU 2.6
243 */
244 virtual LEGlyphID mapCharToGlyph(LEUnicode32 ch) const = 0;
245
246 //
247 // Metrics
248 //
249
250 /**
251 * This method gets the X and Y advance of a particular glyph, in pixels.
252 *
253 * @param glyph - the glyph index
254 * @param advance - the X and Y pixel values will be stored here
255 *
256 * @draft ICU 2.2
257 */
258 virtual void getGlyphAdvance(LEGlyphID glyph, LEPoint &advance) const = 0;
259
260 /**
261 * This method gets the hinted X and Y pixel coordinates of a particular
262 * point in the outline of the given glyph.
263 *
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
267 *
268 * @return <code>TRUE</code> if the point coordinates could be stored.
269 *
270 * @stable ICU 2.8
271 */
272 virtual le_bool getGlyphPoint(LEGlyphID glyph, le_int32 pointNumber, LEPoint &point) const = 0;
273
274 /**
275 * This method returns the width of the font's EM square
276 * in pixels.
277 *
278 * @return the pixel width of the EM square
279 *
280 * @stable ICU 2.8
281 */
282 virtual float getXPixelsPerEm() const = 0;
283
284 /**
285 * This method returns the height of the font's EM square
286 * in pixels.
287 *
288 * @return the pixel height of the EM square
289 *
290 * @stable ICU 2.8
291 */
292 virtual float getYPixelsPerEm() const = 0;
293
294 /**
295 * This method converts font design units in the
296 * X direction to points.
297 *
298 * @param xUnits - design units in the X direction
299 *
300 * @return points in the X direction
301 *
302 * @draft ICU 2.6
303 */
304 virtual float xUnitsToPoints(float xUnits) const;
305
306 /**
307 * This method converts font design units in the
308 * Y direction to points.
309 *
310 * @param yUnits - design units in the Y direction
311 *
312 * @return points in the Y direction
313 *
314 * @draft ICU 2.6
315 */
316 virtual float yUnitsToPoints(float yUnits) const;
317
318 /**
319 * This method converts font design units to points.
320 *
321 * @param units - X and Y design units
322 * @param points - set to X and Y points
323 *
324 * @draft ICU 2.6
325 */
326 virtual void unitsToPoints(LEPoint &units, LEPoint &points) const;
327
328 /**
329 * This method converts pixels in the
330 * X direction to font design units.
331 *
332 * @param xPixels - pixels in the X direction
333 *
334 * @return font design units in the X direction
335 *
336 * @draft ICU 2.6
337 */
338 virtual float xPixelsToUnits(float xPixels) const;
339
340 /**
341 * This method converts pixels in the
342 * Y direction to font design units.
343 *
344 * @param yPixels - pixels in the Y direction
345 *
346 * @return font design units in the Y direction
347 *
348 * @draft ICU 2.6
349 */
350 virtual float yPixelsToUnits(float yPixels) const;
351
352 /**
353 * This method converts pixels to font design units.
354 *
355 * @param pixels - X and Y pixel
356 * @param units - set to X and Y font design units
357 *
358 * @draft ICU 2.6
359 */
360 virtual void pixelsToUnits(LEPoint &pixels, LEPoint &units) const;
361
362 /**
363 * Get the X scale factor from the font's transform. The default
364 * implementation of <code>transformFunits()</code> will call this method.
365 *
366 * @return the X scale factor.
367 *
368 *
369 * @see transformFunits
370 *
371 * @draft ICU 2.6
372 */
373 virtual float getScaleFactorX() const = 0;
374
375 /**
376 * Get the Y scale factor from the font's transform. The default
377 * implementation of <code>transformFunits()</code> will call this method.
378 *
379 * @return the Yscale factor.
380 *
381 * @see transformFunits
382 *
383 * @draft ICU 2.6
384 */
385 virtual float getScaleFactorY() const = 0;
386
387 /**
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>.
392 *
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
396 *
397 * @see getScaleFactorX
398 * @see getScaleFactorY
399 *
400 * @draft ICU 2.6
401 */
402 virtual void transformFunits(float xFunits, float yFunits, LEPoint &pixels) const;
403
404 /**
405 * This is a convenience method used to convert
406 * values in a 16.16 fixed point format to floating point.
407 *
408 * @param fixed - the fixed point value
409 *
410 * @return the floating point value
411 *
412 * @stable ICU 2.8
413 */
414 static float fixedToFloat(le_int32 fixed);
415
416 /**
417 * This is a convenience method used to convert
418 * floating point values to 16.16 fixed point format.
419 *
420 * @param theFloat - the floating point value
421 *
422 * @return the fixed point value
423 *
424 * @stable ICU 2.8
425 */
426 static le_int32 floatToFixed(float theFloat);
427
428 //
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.
432 //
433
434 /**
435 * Get the font's ascent.
436 *
437 * @return the font's ascent, in points. This value
438 * will always be positive.
439 *
440 * @draft ICU 2.6
441 */
442 virtual le_int32 getAscent() const = 0;
443
444 /**
445 * Get the font's descent.
446 *
447 * @return the font's descent, in points. This value
448 * will always be positive.
449 *
450 * @draft ICU 2.6
451 */
452 virtual le_int32 getDescent() const = 0;
453
454 /**
455 * Get the font's leading.
456 *
457 * @return the font's leading, in points. This value
458 * will always be positive.
459 *
460 * @draft ICU 2.6
461 */
462 virtual le_int32 getLeading() const = 0;
463
464 /**
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.
468 *
469 * @return the line height, in points. This vaule will
470 * always be positive.
471 *
472 * @draft ICU 2.6
473 */
474 virtual le_int32 getLineHeight() const;
475
476 /**
477 * ICU "poor man's RTTI", returns a UClassID for the actual class.
478 *
479 * @draft ICU 2.6
480 */
481 virtual UClassID getDynamicClassID() const;
482
483 /**
484 * ICU "poor man's RTTI", returns a UClassID for this class.
485 *
486 * @draft ICU 2.6
487 */
488 static UClassID getStaticClassID();
489
490 };
491
492 inline le_bool LEFontInstance::canDisplay(LEUnicode32 ch) const
493 {
494 return LE_GET_GLYPH(mapCharToGlyph(ch)) != 0;
495 }
496
497 inline float LEFontInstance::xUnitsToPoints(float xUnits) const
498 {
499 return (xUnits * getXPixelsPerEm()) / (float) getUnitsPerEM();
500 }
501
502 inline float LEFontInstance::yUnitsToPoints(float yUnits) const
503 {
504 return (yUnits * getYPixelsPerEm()) / (float) getUnitsPerEM();
505 }
506
507 inline void LEFontInstance::unitsToPoints(LEPoint &units, LEPoint &points) const
508 {
509 points.fX = xUnitsToPoints(units.fX);
510 points.fY = yUnitsToPoints(units.fY);
511 }
512
513 inline float LEFontInstance::xPixelsToUnits(float xPixels) const
514 {
515 return (xPixels * getUnitsPerEM()) / (float) getXPixelsPerEm();
516 }
517
518 inline float LEFontInstance::yPixelsToUnits(float yPixels) const
519 {
520 return (yPixels * getUnitsPerEM()) / (float) getYPixelsPerEm();
521 }
522
523 inline void LEFontInstance::pixelsToUnits(LEPoint &pixels, LEPoint &units) const
524 {
525 units.fX = xPixelsToUnits(pixels.fX);
526 units.fY = yPixelsToUnits(pixels.fY);
527 }
528
529 inline void LEFontInstance::transformFunits(float xFunits, float yFunits, LEPoint &pixels) const
530 {
531 pixels.fX = xUnitsToPoints(xFunits) * getScaleFactorX();
532 pixels.fY = yUnitsToPoints(yFunits) * getScaleFactorY();
533 }
534
535 inline float LEFontInstance::fixedToFloat(le_int32 fixed)
536 {
537 return (float) (fixed / 65536.0);
538 }
539
540 inline le_int32 LEFontInstance::floatToFixed(float theFloat)
541 {
542 return (le_int32) (theFloat * 65536.0);
543 }
544
545 inline le_int32 LEFontInstance::getLineHeight() const
546 {
547 return getAscent() + getDescent() + getLeading();
548 }
549
550 U_NAMESPACE_END
551 #endif
552
553