1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This code is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 only, as
6 * published by the Free Software Foundation.  Oracle designates this
7 * particular file as subject to the "Classpath" exception as provided
8 * by Oracle in the LICENSE file that accompanied this code.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25
26
27/*
28 *
29 * (C) Copyright IBM Corp. 1998-2007 - All Rights Reserved
30 *
31 */
32
33#ifndef __LEFONTINSTANCE_H
34#define __LEFONTINSTANCE_H
35
36#include "LETypes.h"
37/**
38 * \file
39 * \brief C++ API: Layout Engine Font Instance object
40 */
41
42U_NAMESPACE_BEGIN
43
44/**
45 * Instances of this class are used by <code>LEFontInstance::mapCharsToGlyphs</code> and
46 * <code>LEFontInstance::mapCharToGlyph</code> to adjust character codes before the character
47 * to glyph mapping process. Examples of this are filtering out control characters
48 * and character mirroring - replacing a character which has both a left and a right
49 * hand form with the opposite form.
50 *
51 * @stable ICU 3.2
52 */
53class LECharMapper /* not : public UObject because this is an interface/mixin class */
54{
55public:
56    /**
57     * Destructor.
58     * @stable ICU 3.2
59     */
60    virtual ~LECharMapper();
61
62    /**
63     * This method does the adjustments.
64     *
65     * @param ch - the input character
66     *
67     * @return the adjusted character
68     *
69     * @stable ICU 2.8
70     */
71    virtual LEUnicode32 mapChar(LEUnicode32 ch) const = 0;
72};
73
74/**
75 * This is a forward reference to the class which holds the per-glyph
76 * storage.
77 *
78 * @stable ICU 3.0
79 */
80class LEGlyphStorage;
81
82/**
83 * This is a virtual base class that serves as the interface between a LayoutEngine
84 * and the platform font environment. It allows a LayoutEngine to access font tables, do
85 * character to glyph mapping, and obtain metrics information without knowing any platform
86 * specific details. There are also a few utility methods for converting between points,
87 * pixels and funits. (font design units)
88 *
89 * An instance of an <code>LEFontInstance</code> represents a font at a particular point
90 * size. Each instance can represent either a single physical font, or a composite font.
91 * A composite font is a collection of physical fonts, each of which contains a subset of
92 * the characters contained in the composite font.
93 *
94 * Note: with the exception of <code>getSubFont</code>, the methods in this class only
95 * make sense for a physical font. If you have an <code>LEFontInstance</code> which
96 * represents a composite font you should only call the methods below which have
97 * an <code>LEGlyphID</code>, an <code>LEUnicode</code> or an <code>LEUnicode32</code>
98 * as one of the arguments because these can be used to select a particular subfont.
99 *
100 * Subclasses which implement composite fonts should supply an implementation of these
101 * methods with some default behavior such as returning constant values, or using the
102 * values from the first subfont.
103 *
104 * @stable ICU 3.0
105 */
106class U_LAYOUT_API LEFontInstance : public UObject
107{
108public:
109
110    /**
111     * This virtual destructor is here so that the subclass
112     * destructors can be invoked through the base class.
113     *
114     * @stable ICU 2.8
115     */
116    virtual ~LEFontInstance();
117
118    /**
119     * Get a physical font which can render the given text. For composite fonts,
120     * if there is no single physical font which can render all of the text,
121     * return a physical font which can render an initial substring of the text,
122     * and set the <code>offset</code> parameter to the end of that substring.
123     *
124     * Internally, the LayoutEngine works with runs of text all in the same
125     * font and script, so it is best to call this method with text which is
126     * in a single script, passing the script code in as a hint. If you don't
127     * know the script of the text, you can use zero, which is the script code
128     * for characters used in more than one script.
129     *
130     * The default implementation of this method is intended for instances of
131     * <code>LEFontInstance</code> which represent a physical font. It returns
132     * <code>this</code> and indicates that the entire string can be rendered.
133     *
134     * This method will return a valid <code>LEFontInstance</code> unless you
135     * have passed illegal parameters, or an internal error has been encountered.
136     * For composite fonts, it may return the warning <code>LE_NO_SUBFONT_WARNING</code>
137     * to indicate that the returned font may not be able to render all of
138     * the text. Whenever a valid font is returned, the <code>offset</code> parameter
139     * will be advanced by at least one.
140     *
141     * Subclasses which implement composite fonts must override this method.
142     * Where it makes sense, they should use the script code as a hint to render
143     * characters from the COMMON script in the font which is used for the given
144     * script. For example, if the input text is a series of Arabic words separated
145     * by spaces, and the script code passed in is <code>arabScriptCode</code> you
146     * should return the font used for Arabic characters for all of the input text,
147     * including the spaces. If, on the other hand, the input text contains characters
148     * which cannot be rendered by the font used for Arabic characters, but which can
149     * be rendered by another font, you should return that font for those characters.
150     *
151     * @param chars   - the array of Unicode characters.
152     * @param offset  - a pointer to the starting offset in the text. On exit this
153     *                  will be set the the limit offset of the text which can be
154     *                  rendered using the returned font.
155     * @param limit   - the limit offset for the input text.
156     * @param script  - the script hint.
157     * @param success - set to an error code if the arguments are illegal, or no font
158     *                  can be returned for some reason. May also be set to
159     *                  <code>LE_NO_SUBFONT_WARNING</code> if the subfont which
160     *                  was returned cannot render all of the text.
161     *
162     * @return an <code>LEFontInstance</code> for the sub font which can render the characters, or
163     *         <code>NULL</code> if there is an error.
164     *
165     * @see LEScripts.h
166     *
167     * @stable ICU 3.2
168     */
169    virtual const LEFontInstance *getSubFont(const LEUnicode chars[], le_int32 *offset, le_int32 limit, le_int32 script, LEErrorCode &success) const;
170
171    //
172    // Font file access
173    //
174
175    /**
176     * This method reads a table from the font. Note that in general,
177     * it only makes sense to call this method on an <code>LEFontInstance</code>
178     * which represents a physical font - i.e. one which has been returned by
179     * <code>getSubFont()</code>. This is because each subfont in a composite font
180     * will have different tables, and there's no way to know which subfont to access.
181     *
182     * Subclasses which represent composite fonts should always return <code>NULL</code>.
183     *
184     * This version sets a length, for range checking.
185     * Note that range checking can only be accomplished if this function is
186     * implemented in subclasses.
187     *
188     * @param tableTag - the four byte table tag. (e.g. 'cmap')
189     * @param length - ignored on entry, on exit will be the length of the table if known, or -1 if unknown.
190     * @return the address of the table in memory, or <code>NULL</code>
191     *         if the table doesn't exist.
192     * @internal
193     */
194    virtual const void* getFontTable(LETag tableTag, size_t &length) const = 0;
195
196    virtual void *getKernPairs() const = 0;
197    virtual void  setKernPairs(void *pairs) const = 0;
198
199    /**
200     * This method is used to determine if the font can
201     * render the given character. This can usually be done
202     * by looking the character up in the font's character
203     * to glyph mapping.
204     *
205     * The default implementation of this method will return
206     * <code>TRUE</code> if <code>mapCharToGlyph(ch)</code>
207     * returns a non-zero value.
208     *
209     * @param ch - the character to be tested
210     *
211     * @return <code>TRUE</code> if the font can render ch.
212     *
213     * @stable ICU 3.2
214     */
215    virtual le_bool canDisplay(LEUnicode32 ch) const;
216
217    /**
218     * This method returns the number of design units in
219     * the font's EM square.
220     *
221     * @return the number of design units pre EM.
222     *
223     * @stable ICU 2.8
224     */
225    virtual le_int32 getUnitsPerEM() const = 0;
226
227    /**
228     * This method maps an array of character codes to an array of glyph
229     * indices, using the font's character to glyph map.
230     *
231     * The default implementation iterates over all of the characters and calls
232     * <code>mapCharToGlyph(ch, mapper)</code> on each one. It also handles surrogate
233     * characters, storing the glyph ID for the high surrogate, and a deleted glyph (0xFFFF)
234     * for the low surrogate.
235     *
236     * Most sublcasses will not need to implement this method.
237     *
238     * @param chars - the character array
239     * @param offset - the index of the first character
240     * @param count - the number of characters
241     * @param reverse - if <code>TRUE</code>, store the glyph indices in reverse order.
242     * @param mapper - the character mapper.
243     * @param filterZeroWidth - <code>TRUE</code> if ZWJ / ZWNJ characters should map to a glyph w/ no contours.
244     * @param glyphStorage - the object which contains the output glyph array
245     *
246     * @see LECharMapper
247     *
248     * @stable ICU 3.6
249     */
250    virtual void mapCharsToGlyphs(const LEUnicode chars[], le_int32 offset, le_int32 count, le_bool reverse, const LECharMapper *mapper, le_bool filterZeroWidth, LEGlyphStorage &glyphStorage) const;
251
252    /**
253     * This method maps a single character to a glyph index, using the
254     * font's character to glyph map. The default implementation of this
255     * method calls the mapper, and then calls <code>mapCharToGlyph(mappedCh)</code>.
256     *
257     * @param ch - the character
258     * @param mapper - the character mapper
259     * @param filterZeroWidth - <code>TRUE</code> if ZWJ / ZWNJ characters should map to a glyph w/ no contours.
260     *
261     * @return the glyph index
262     *
263     * @see LECharMapper
264     *
265     * @stable ICU 3.6
266     */
267    virtual LEGlyphID mapCharToGlyph(LEUnicode32 ch, const LECharMapper *mapper, le_bool filterZeroWidth) const;
268
269    /**
270     * This method maps a single character to a glyph index, using the
271     * font's character to glyph map. The default implementation of this
272     * method calls the mapper, and then calls <code>mapCharToGlyph(mappedCh)</code>.
273     *
274     * @param ch - the character
275     * @param mapper - the character mapper
276     *
277     * @return the glyph index
278     *
279     * @see LECharMapper
280     *
281     * @stable ICU 3.2
282     */
283    virtual LEGlyphID mapCharToGlyph(LEUnicode32 ch, const LECharMapper *mapper) const;
284
285    /**
286     * This method maps a single character to a glyph index, using the
287     * font's character to glyph map. There is no default implementation
288     * of this method because it requires information about the platform
289     * font implementation.
290     *
291     * @param ch - the character
292     *
293     * @return the glyph index
294     *
295     * @stable ICU 3.2
296     */
297    virtual LEGlyphID mapCharToGlyph(LEUnicode32 ch) const = 0;
298
299    //
300    // Metrics
301    //
302
303    /**
304     * This method gets the X and Y advance of a particular glyph, in pixels.
305     *
306     * @param glyph - the glyph index
307     * @param advance - the X and Y pixel values will be stored here
308     *
309     * @stable ICU 3.2
310     */
311    virtual void getGlyphAdvance(LEGlyphID glyph, LEPoint &advance) const = 0;
312
313    virtual void getKerningAdjustment(LEPoint &adjustment) const = 0;
314
315    /**
316     * This method gets the hinted X and Y pixel coordinates of a particular
317     * point in the outline of the given glyph.
318     *
319     * @param glyph - the glyph index
320     * @param pointNumber - the number of the point
321     * @param point - the point's X and Y pixel values will be stored here
322     *
323     * @return <code>TRUE</code> if the point coordinates could be stored.
324     *
325     * @stable ICU 2.8
326     */
327    virtual le_bool getGlyphPoint(LEGlyphID glyph, le_int32 pointNumber, LEPoint &point) const = 0;
328
329    /**
330     * This method returns the width of the font's EM square
331     * in pixels.
332     *
333     * @return the pixel width of the EM square
334     *
335     * @stable ICU 2.8
336     */
337    virtual float getXPixelsPerEm() const = 0;
338
339    /**
340     * This method returns the height of the font's EM square
341     * in pixels.
342     *
343     * @return the pixel height of the EM square
344     *
345     * @stable ICU 2.8
346     */
347    virtual float getYPixelsPerEm() const = 0;
348
349    /**
350     * This method converts font design units in the
351     * X direction to points.
352     *
353     * @param xUnits - design units in the X direction
354     *
355     * @return points in the X direction
356     *
357     * @stable ICU 3.2
358     */
359    virtual float xUnitsToPoints(float xUnits) const;
360
361    /**
362     * This method converts font design units in the
363     * Y direction to points.
364     *
365     * @param yUnits - design units in the Y direction
366     *
367     * @return points in the Y direction
368     *
369     * @stable ICU 3.2
370     */
371    virtual float yUnitsToPoints(float yUnits) const;
372
373    /**
374     * This method converts font design units to points.
375     *
376     * @param units - X and Y design units
377     * @param points - set to X and Y points
378     *
379     * @stable ICU 3.2
380     */
381    virtual void unitsToPoints(LEPoint &units, LEPoint &points) const;
382
383    /**
384     * This method converts pixels in the
385     * X direction to font design units.
386     *
387     * @param xPixels - pixels in the X direction
388     *
389     * @return font design units in the X direction
390     *
391     * @stable ICU 3.2
392     */
393    virtual float xPixelsToUnits(float xPixels) const;
394
395    /**
396     * This method converts pixels in the
397     * Y direction to font design units.
398     *
399     * @param yPixels - pixels in the Y direction
400     *
401     * @return font design units in the Y direction
402     *
403     * @stable ICU 3.2
404     */
405    virtual float yPixelsToUnits(float yPixels) const;
406
407    /**
408     * This method converts pixels to font design units.
409     *
410     * @param pixels - X and Y pixel
411     * @param units - set to X and Y font design units
412     *
413     * @stable ICU 3.2
414     */
415    virtual void pixelsToUnits(LEPoint &pixels, LEPoint &units) const;
416
417    /**
418     * Get the X scale factor from the font's transform. The default
419     * implementation of <code>transformFunits()</code> will call this method.
420     *
421     * @return the X scale factor.
422     *
423     *
424     * @see transformFunits
425     *
426     * @stable ICU 3.2
427     */
428    virtual float getScaleFactorX() const = 0;
429
430    /**
431     * Get the Y scale factor from the font's transform. The default
432     * implementation of <code>transformFunits()</code> will call this method.
433     *
434     * @return the Yscale factor.
435     *
436     * @see transformFunits
437     *
438     * @stable ICU 3.2
439     */
440    virtual float getScaleFactorY() const = 0;
441
442    /**
443     * This method transforms an X, Y point in font design units to a
444     * pixel coordinate, applying the font's transform. The default
445     * implementation of this method calls <code>getScaleFactorX()</code>
446     * and <code>getScaleFactorY()</code>.
447     *
448     * @param xFunits - the X coordinate in font design units
449     * @param yFunits - the Y coordinate in font design units
450     * @param pixels - the tranformed co-ordinate in pixels
451     *
452     * @see getScaleFactorX
453     * @see getScaleFactorY
454     *
455     * @stable ICU 3.2
456     */
457    virtual void transformFunits(float xFunits, float yFunits, LEPoint &pixels) const;
458
459    /**
460     * This is a convenience method used to convert
461     * values in a 16.16 fixed point format to floating point.
462     *
463     * @param fixed - the fixed point value
464     *
465     * @return the floating point value
466     *
467     * @stable ICU 2.8
468     */
469    static inline float fixedToFloat(le_int32 fixed);
470
471    /**
472     * This is a convenience method used to convert
473     * floating point values to 16.16 fixed point format.
474     *
475     * @param theFloat - the floating point value
476     *
477     * @return the fixed point value
478     *
479     * @stable ICU 2.8
480     */
481    static inline le_int32 floatToFixed(float theFloat);
482
483    //
484    // These methods won't ever be called by the LayoutEngine,
485    // but are useful for clients of <code>LEFontInstance</code> who
486    // need to render text.
487    //
488
489    /**
490     * Get the font's ascent.
491     *
492     * @return the font's ascent, in points. This value
493     * will always be positive.
494     *
495     * @stable ICU 3.2
496     */
497    virtual le_int32 getAscent() const = 0;
498
499    /**
500     * Get the font's descent.
501     *
502     * @return the font's descent, in points. This value
503     * will always be positive.
504     *
505     * @stable ICU 3.2
506     */
507    virtual le_int32 getDescent() const = 0;
508
509    /**
510     * Get the font's leading.
511     *
512     * @return the font's leading, in points. This value
513     * will always be positive.
514     *
515     * @stable ICU 3.2
516     */
517    virtual le_int32 getLeading() const = 0;
518
519    /**
520     * Get the line height required to display text in
521     * this font. The default implementation of this method
522     * returns the sum of the ascent, descent, and leading.
523     *
524     * @return the line height, in points. This vaule will
525     * always be positive.
526     *
527     * @stable ICU 3.2
528     */
529    virtual le_int32 getLineHeight() const;
530
531    /**
532     * ICU "poor man's RTTI", returns a UClassID for the actual class.
533     *
534     * @stable ICU 3.2
535     */
536    virtual UClassID getDynamicClassID() const;
537
538    /**
539     * ICU "poor man's RTTI", returns a UClassID for this class.
540     *
541     * @stable ICU 3.2
542     */
543    static UClassID getStaticClassID();
544
545};
546
547inline float LEFontInstance::fixedToFloat(le_int32 fixed)
548{
549    return (float) (fixed / 65536.0);
550}
551
552inline le_int32 LEFontInstance::floatToFixed(float theFloat)
553{
554    return (le_int32) (theFloat * 65536.0);
555}
556
557U_NAMESPACE_END
558#endif
559