1
2 /*
3 *
4 * (C) Copyright IBM Corp. 1998-2012 - All Rights Reserved
5 *
6 */
7
8 #ifndef __LEFONTINSTANCE_H
9 #define __LEFONTINSTANCE_H
10
11 #include "LETypes.h"
12 /**
13 * \file
14 * \brief C++ API: Layout Engine Font Instance object
15 */
16
17 U_NAMESPACE_BEGIN
18
19 /**
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.
25 *
26 * @stable ICU 3.2
27 */
28 class LECharMapper /* not : public UObject because this is an interface/mixin class */
29 {
30 public:
31 /**
32 * Destructor.
33 * @stable ICU 3.2
34 */
35 virtual ~LECharMapper();
36
37 /**
38 * This method does the adjustments.
39 *
40 * @param ch - the input character
41 *
42 * @return the adjusted character
43 *
44 * @stable ICU 2.8
45 */
46 virtual LEUnicode32 mapChar(LEUnicode32 ch) const = 0;
47 };
48
49 /**
50 * This is a forward reference to the class which holds the per-glyph
51 * storage.
52 *
53 * @stable ICU 3.0
54 */
55 class LEGlyphStorage;
56
57 /**
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)
63 *
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.
68 *
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.
74 *
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.
78 *
79 * @stable ICU 3.0
80 */
81 class U_LAYOUT_API LEFontInstance : public UObject
82 {
83 public:
84
85 /**
86 * This virtual destructor is here so that the subclass
87 * destructors can be invoked through the base class.
88 *
89 * @stable ICU 2.8
90 */
91 virtual ~LEFontInstance();
92
93 /**
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.
98 *
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.
104 *
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.
108 *
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.
115 *
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.
125 *
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.
136 *
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.
139 *
140 * @see LEScripts.h
141 *
142 * @stable ICU 3.2
143 */
144 virtual const LEFontInstance *getSubFont(const LEUnicode chars[], le_int32 *offset, le_int32 limit, le_int32 script, LEErrorCode &success) const;
145
146 //
147 // Font file access
148 //
149
150 /**
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.
156 *
157 * Subclasses which represent composite fonts should always return <code>NULL</code>.
158 *
159 * @param tableTag - the four byte table tag. (e.g. 'cmap')
160 *
161 * @return the address of the table in memory, or <code>NULL</code>
162 * if the table doesn't exist.
163 *
164 * @stable ICU 2.8
165 */
166 virtual const void *getFontTable(LETag tableTag) const = 0;
167
168 #if 0 /* Google Patch: withdraw this function. https://b.corp.google.com/issue?id=8593098 */
169 /**
170 * This method reads a table from the font. Note that in general,
171 * it only makes sense to call this method on an <code>LEFontInstance</code>
172 * which represents a physical font - i.e. one which has been returned by
173 * <code>getSubFont()</code>. This is because each subfont in a composite font
174 * will have different tables, and there's no way to know which subfont to access.
175 *
176 * Subclasses which represent composite fonts should always return <code>NULL</code>.
177 *
178 * This version sets a length, for range checking.
179 *
180 * @param tableTag - the four byte table tag. (e.g. 'cmap')
181 * @param length - ignored on entry, on exit will be the length of the table if known, or -1 if unknown.
182 * @return the address of the table in memory, or <code>NULL</code>
183 * if the table doesn't exist.
184 * @internal
185 */
186 virtual const void* getFontTable(LETag tableTag, size_t &length) const { length=-1; return getFontTable(tableTag); } /* -1 = unknown length */
187 #endif /* End Google Patch */
188
189 /**
190 * This method is used to determine if the font can
191 * render the given character. This can usually be done
192 * by looking the character up in the font's character
193 * to glyph mapping.
194 *
195 * The default implementation of this method will return
196 * <code>TRUE</code> if <code>mapCharToGlyph(ch)</code>
197 * returns a non-zero value.
198 *
199 * @param ch - the character to be tested
200 *
201 * @return <code>TRUE</code> if the font can render ch.
202 *
203 * @stable ICU 3.2
204 */
205 virtual le_bool canDisplay(LEUnicode32 ch) const;
206
207 /**
208 * This method returns the number of design units in
209 * the font's EM square.
210 *
211 * @return the number of design units pre EM.
212 *
213 * @stable ICU 2.8
214 */
215 virtual le_int32 getUnitsPerEM() const = 0;
216
217 /**
218 * This method maps an array of character codes to an array of glyph
219 * indices, using the font's character to glyph map.
220 *
221 * The default implementation iterates over all of the characters and calls
222 * <code>mapCharToGlyph(ch, mapper)</code> on each one. It also handles surrogate
223 * characters, storing the glyph ID for the high surrogate, and a deleted glyph (0xFFFF)
224 * for the low surrogate.
225 *
226 * Most sublcasses will not need to implement this method.
227 *
228 * @param chars - the character array
229 * @param offset - the index of the first character
230 * @param count - the number of characters
231 * @param reverse - if <code>TRUE</code>, store the glyph indices in reverse order.
232 * @param mapper - the character mapper.
233 * @param filterZeroWidth - <code>TRUE</code> if ZWJ / ZWNJ characters should map to a glyph w/ no contours.
234 * @param glyphStorage - the object which contains the output glyph array
235 *
236 * @see LECharMapper
237 *
238 * @stable ICU 3.6
239 */
240 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
242 /**
243 * This method maps a single character to a glyph index, using the
244 * font's character to glyph map. The default implementation of this
245 * method calls the mapper, and then calls <code>mapCharToGlyph(mappedCh)</code>.
246 *
247 * @param ch - the character
248 * @param mapper - the character mapper
249 * @param filterZeroWidth - <code>TRUE</code> if ZWJ / ZWNJ characters should map to a glyph w/ no contours.
250 *
251 * @return the glyph index
252 *
253 * @see LECharMapper
254 *
255 * @stable ICU 3.6
256 */
257 virtual LEGlyphID mapCharToGlyph(LEUnicode32 ch, const LECharMapper *mapper, le_bool filterZeroWidth) const;
258
259 /**
260 * This method maps a single character to a glyph index, using the
261 * font's character to glyph map. The default implementation of this
262 * method calls the mapper, and then calls <code>mapCharToGlyph(mappedCh)</code>.
263 *
264 * @param ch - the character
265 * @param mapper - the character mapper
266 *
267 * @return the glyph index
268 *
269 * @see LECharMapper
270 *
271 * @stable ICU 3.2
272 */
273 virtual LEGlyphID mapCharToGlyph(LEUnicode32 ch, const LECharMapper *mapper) const;
274
275 /**
276 * This method maps a single character to a glyph index, using the
277 * font's character to glyph map. There is no default implementation
278 * of this method because it requires information about the platform
279 * font implementation.
280 *
281 * @param ch - the character
282 *
283 * @return the glyph index
284 *
285 * @stable ICU 3.2
286 */
287 virtual LEGlyphID mapCharToGlyph(LEUnicode32 ch) const = 0;
288
289 //
290 // Metrics
291 //
292
293 /**
294 * This method gets the X and Y advance of a particular glyph, in pixels.
295 *
296 * @param glyph - the glyph index
297 * @param advance - the X and Y pixel values will be stored here
298 *
299 * @stable ICU 3.2
300 */
301 virtual void getGlyphAdvance(LEGlyphID glyph, LEPoint &advance) const = 0;
302
303 /**
304 * This method gets the hinted X and Y pixel coordinates of a particular
305 * point in the outline of the given glyph.
306 *
307 * @param glyph - the glyph index
308 * @param pointNumber - the number of the point
309 * @param point - the point's X and Y pixel values will be stored here
310 *
311 * @return <code>TRUE</code> if the point coordinates could be stored.
312 *
313 * @stable ICU 2.8
314 */
315 virtual le_bool getGlyphPoint(LEGlyphID glyph, le_int32 pointNumber, LEPoint &point) const = 0;
316
317 /**
318 * This method returns the width of the font's EM square
319 * in pixels.
320 *
321 * @return the pixel width of the EM square
322 *
323 * @stable ICU 2.8
324 */
325 virtual float getXPixelsPerEm() const = 0;
326
327 /**
328 * This method returns the height of the font's EM square
329 * in pixels.
330 *
331 * @return the pixel height of the EM square
332 *
333 * @stable ICU 2.8
334 */
335 virtual float getYPixelsPerEm() const = 0;
336
337 /**
338 * This method converts font design units in the
339 * X direction to points.
340 *
341 * @param xUnits - design units in the X direction
342 *
343 * @return points in the X direction
344 *
345 * @stable ICU 3.2
346 */
347 virtual float xUnitsToPoints(float xUnits) const;
348
349 /**
350 * This method converts font design units in the
351 * Y direction to points.
352 *
353 * @param yUnits - design units in the Y direction
354 *
355 * @return points in the Y direction
356 *
357 * @stable ICU 3.2
358 */
359 virtual float yUnitsToPoints(float yUnits) const;
360
361 /**
362 * This method converts font design units to points.
363 *
364 * @param units - X and Y design units
365 * @param points - set to X and Y points
366 *
367 * @stable ICU 3.2
368 */
369 virtual void unitsToPoints(LEPoint &units, LEPoint &points) const;
370
371 /**
372 * This method converts pixels in the
373 * X direction to font design units.
374 *
375 * @param xPixels - pixels in the X direction
376 *
377 * @return font design units in the X direction
378 *
379 * @stable ICU 3.2
380 */
381 virtual float xPixelsToUnits(float xPixels) const;
382
383 /**
384 * This method converts pixels in the
385 * Y direction to font design units.
386 *
387 * @param yPixels - pixels in the Y direction
388 *
389 * @return font design units in the Y direction
390 *
391 * @stable ICU 3.2
392 */
393 virtual float yPixelsToUnits(float yPixels) const;
394
395 /**
396 * This method converts pixels to font design units.
397 *
398 * @param pixels - X and Y pixel
399 * @param units - set to X and Y font design units
400 *
401 * @stable ICU 3.2
402 */
403 virtual void pixelsToUnits(LEPoint &pixels, LEPoint &units) const;
404
405 /**
406 * Get the X scale factor from the font's transform. The default
407 * implementation of <code>transformFunits()</code> will call this method.
408 *
409 * @return the X scale factor.
410 *
411 *
412 * @see transformFunits
413 *
414 * @stable ICU 3.2
415 */
416 virtual float getScaleFactorX() const = 0;
417
418 /**
419 * Get the Y scale factor from the font's transform. The default
420 * implementation of <code>transformFunits()</code> will call this method.
421 *
422 * @return the Yscale factor.
423 *
424 * @see transformFunits
425 *
426 * @stable ICU 3.2
427 */
428 virtual float getScaleFactorY() const = 0;
429
430 /**
431 * This method transforms an X, Y point in font design units to a
432 * pixel coordinate, applying the font's transform. The default
433 * implementation of this method calls <code>getScaleFactorX()</code>
434 * and <code>getScaleFactorY()</code>.
435 *
436 * @param xFunits - the X coordinate in font design units
437 * @param yFunits - the Y coordinate in font design units
438 * @param pixels - the tranformed co-ordinate in pixels
439 *
440 * @see getScaleFactorX
441 * @see getScaleFactorY
442 *
443 * @stable ICU 3.2
444 */
445 virtual void transformFunits(float xFunits, float yFunits, LEPoint &pixels) const;
446
447 /**
448 * This is a convenience method used to convert
449 * values in a 16.16 fixed point format to floating point.
450 *
451 * @param fixed - the fixed point value
452 *
453 * @return the floating point value
454 *
455 * @stable ICU 2.8
456 */
457 static inline float fixedToFloat(le_int32 fixed);
458
459 /**
460 * This is a convenience method used to convert
461 * floating point values to 16.16 fixed point format.
462 *
463 * @param theFloat - the floating point value
464 *
465 * @return the fixed point value
466 *
467 * @stable ICU 2.8
468 */
469 static inline le_int32 floatToFixed(float theFloat);
470
471 //
472 // These methods won't ever be called by the LayoutEngine,
473 // but are useful for clients of <code>LEFontInstance</code> who
474 // need to render text.
475 //
476
477 /**
478 * Get the font's ascent.
479 *
480 * @return the font's ascent, in points. This value
481 * will always be positive.
482 *
483 * @stable ICU 3.2
484 */
485 virtual le_int32 getAscent() const = 0;
486
487 /**
488 * Get the font's descent.
489 *
490 * @return the font's descent, in points. This value
491 * will always be positive.
492 *
493 * @stable ICU 3.2
494 */
495 virtual le_int32 getDescent() const = 0;
496
497 /**
498 * Get the font's leading.
499 *
500 * @return the font's leading, in points. This value
501 * will always be positive.
502 *
503 * @stable ICU 3.2
504 */
505 virtual le_int32 getLeading() const = 0;
506
507 /**
508 * Get the line height required to display text in
509 * this font. The default implementation of this method
510 * returns the sum of the ascent, descent, and leading.
511 *
512 * @return the line height, in points. This vaule will
513 * always be positive.
514 *
515 * @stable ICU 3.2
516 */
517 virtual le_int32 getLineHeight() const;
518
519 /**
520 * ICU "poor man's RTTI", returns a UClassID for the actual class.
521 *
522 * @stable ICU 3.2
523 */
524 virtual UClassID getDynamicClassID() const;
525
526 /**
527 * ICU "poor man's RTTI", returns a UClassID for this class.
528 *
529 * @stable ICU 3.2
530 */
531 static UClassID getStaticClassID();
532
533 };
534
fixedToFloat(le_int32 fixed)535 inline float LEFontInstance::fixedToFloat(le_int32 fixed)
536 {
537 return (float) (fixed / 65536.0);
538 }
539
floatToFixed(float theFloat)540 inline le_int32 LEFontInstance::floatToFixed(float theFloat)
541 {
542 return (le_int32) (theFloat * 65536.0);
543 }
544
545 U_NAMESPACE_END
546 #endif
547
548
549