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 
  42 U_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  */
  53 class LECharMapper /* not : public UObject because this is an interface/mixin class */
  54 {
  55 public:
  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  */
  80 class 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  */
 106 class U_LAYOUT_API LEFontInstance : public UObject
 107 {
 108 public:
 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      * Note that implementing this function does not allow for range checking.
 185      * Subclasses that desire the safety of range checking must implement the
 186      * variation which has a length parameter.
 187      *
 188      * @param tableTag - the four byte table tag. (e.g. 'cmap')
 189      *
 190      * @return the address of the table in memory, or <code>NULL</code>
 191      *         if the table doesn't exist.
 192      *
 193      * @stable ICU 2.8
 194      */
 195     virtual const void *getFontTable(LETag tableTag) const = 0;
 196 
 197     /**
 198      * This method reads a table from the font. Note that in general,
 199      * it only makes sense to call this method on an <code>LEFontInstance</code>
 200      * which represents a physical font - i.e. one which has been returned by
 201      * <code>getSubFont()</code>. This is because each subfont in a composite font
 202      * will have different tables, and there's no way to know which subfont to access.
 203      *
 204      * Subclasses which represent composite fonts should always return <code>NULL</code>.
 205      *
 206      * This version sets a length, for range checking.
 207      * Note that range checking can only be accomplished if this function is
 208      * implemented in subclasses.
 209      *
 210      * @param tableTag - the four byte table tag. (e.g. 'cmap')
 211      * @param length - ignored on entry, on exit will be the length of the table if known, or -1 if unknown.
 212      * @return the address of the table in memory, or <code>NULL</code>
 213      *         if the table doesn't exist.
 214      * @internal
 215      */
 216     virtual const void* getFontTable(LETag tableTag, size_t &length) const { length=-1; return getFontTable(tableTag); }  /* -1 = unknown length */
 217 
 218     virtual void *getKernPairs() const = 0;
 219     virtual void  setKernPairs(void *pairs) const = 0;
 220 
 221     /**
 222      * This method is used to determine if the font can
 223      * render the given character. This can usually be done
 224      * by looking the character up in the font's character
 225      * to glyph mapping.
 226      *
 227      * The default implementation of this method will return
 228      * <code>TRUE</code> if <code>mapCharToGlyph(ch)</code>
 229      * returns a non-zero value.
 230      *
 231      * @param ch - the character to be tested
 232      *
 233      * @return <code>TRUE</code> if the font can render ch.
 234      *
 235      * @stable ICU 3.2
 236      */
 237     virtual le_bool canDisplay(LEUnicode32 ch) const;
 238 
 239     /**
 240      * This method returns the number of design units in
 241      * the font's EM square.
 242      *
 243      * @return the number of design units pre EM.
 244      *
 245      * @stable ICU 2.8
 246      */
 247     virtual le_int32 getUnitsPerEM() const = 0;
 248 
 249     /**
 250      * This method maps an array of character codes to an array of glyph
 251      * indices, using the font's character to glyph map.
 252      *
 253      * The default implementation iterates over all of the characters and calls
 254      * <code>mapCharToGlyph(ch, mapper)</code> on each one. It also handles surrogate
 255      * characters, storing the glyph ID for the high surrogate, and a deleted glyph (0xFFFF)
 256      * for the low surrogate.
 257      *
 258      * Most sublcasses will not need to implement this method.
 259      *
 260      * @param chars - the character array
 261      * @param offset - the index of the first character
 262      * @param count - the number of characters
 263      * @param reverse - if <code>TRUE</code>, store the glyph indices in reverse order.
 264      * @param mapper - the character mapper.
 265      * @param filterZeroWidth - <code>TRUE</code> if ZWJ / ZWNJ characters should map to a glyph w/ no contours.
 266      * @param glyphStorage - the object which contains the output glyph array
 267      *
 268      * @see LECharMapper
 269      *
 270      * @stable ICU 3.6
 271      */
 272     virtual void mapCharsToGlyphs(const LEUnicode chars[], le_int32 offset, le_int32 count, le_bool reverse, const LECharMapper *mapper, le_bool filterZeroWidth, LEGlyphStorage &glyphStorage) const;
 273 
 274     /**
 275      * This method maps a single character to a glyph index, using the
 276      * font's character to glyph map. The default implementation of this
 277      * method calls the mapper, and then calls <code>mapCharToGlyph(mappedCh)</code>.
 278      *
 279      * @param ch - the character
 280      * @param mapper - the character mapper
 281      * @param filterZeroWidth - <code>TRUE</code> if ZWJ / ZWNJ characters should map to a glyph w/ no contours.
 282      *
 283      * @return the glyph index
 284      *
 285      * @see LECharMapper
 286      *
 287      * @stable ICU 3.6
 288      */
 289     virtual LEGlyphID mapCharToGlyph(LEUnicode32 ch, const LECharMapper *mapper, le_bool filterZeroWidth) const;
 290 
 291     /**
 292      * This method maps a single character to a glyph index, using the
 293      * font's character to glyph map. The default implementation of this
 294      * method calls the mapper, and then calls <code>mapCharToGlyph(mappedCh)</code>.
 295      *
 296      * @param ch - the character
 297      * @param mapper - the character mapper
 298      *
 299      * @return the glyph index
 300      *
 301      * @see LECharMapper
 302      *
 303      * @stable ICU 3.2
 304      */
 305     virtual LEGlyphID mapCharToGlyph(LEUnicode32 ch, const LECharMapper *mapper) const;
 306 
 307     /**
 308      * This method maps a single character to a glyph index, using the
 309      * font's character to glyph map. There is no default implementation
 310      * of this method because it requires information about the platform
 311      * font implementation.
 312      *
 313      * @param ch - the character
 314      *
 315      * @return the glyph index
 316      *
 317      * @stable ICU 3.2
 318      */
 319     virtual LEGlyphID mapCharToGlyph(LEUnicode32 ch) const = 0;
 320 
 321     //
 322     // Metrics
 323     //
 324 
 325     /**
 326      * This method gets the X and Y advance of a particular glyph, in pixels.
 327      *
 328      * @param glyph - the glyph index
 329      * @param advance - the X and Y pixel values will be stored here
 330      *
 331      * @stable ICU 3.2
 332      */
 333     virtual void getGlyphAdvance(LEGlyphID glyph, LEPoint &advance) const = 0;
 334 
 335     virtual void getKerningAdjustment(LEPoint &adjustment) const = 0;
 336 
 337     /**
 338      * This method gets the hinted X and Y pixel coordinates of a particular
 339      * point in the outline of the given glyph.
 340      *
 341      * @param glyph - the glyph index
 342      * @param pointNumber - the number of the point
 343      * @param point - the point's X and Y pixel values will be stored here
 344      *
 345      * @return <code>TRUE</code> if the point coordinates could be stored.
 346      *
 347      * @stable ICU 2.8
 348      */
 349     virtual le_bool getGlyphPoint(LEGlyphID glyph, le_int32 pointNumber, LEPoint &point) const = 0;
 350 
 351     /**
 352      * This method returns the width of the font's EM square
 353      * in pixels.
 354      *
 355      * @return the pixel width of the EM square
 356      *
 357      * @stable ICU 2.8
 358      */
 359     virtual float getXPixelsPerEm() const = 0;
 360 
 361     /**
 362      * This method returns the height of the font's EM square
 363      * in pixels.
 364      *
 365      * @return the pixel height of the EM square
 366      *
 367      * @stable ICU 2.8
 368      */
 369     virtual float getYPixelsPerEm() const = 0;
 370 
 371     /**
 372      * This method converts font design units in the
 373      * X direction to points.
 374      *
 375      * @param xUnits - design units in the X direction
 376      *
 377      * @return points in the X direction
 378      *
 379      * @stable ICU 3.2
 380      */
 381     virtual float xUnitsToPoints(float xUnits) const;
 382 
 383     /**
 384      * This method converts font design units in the
 385      * Y direction to points.
 386      *
 387      * @param yUnits - design units in the Y direction
 388      *
 389      * @return points in the Y direction
 390      *
 391      * @stable ICU 3.2
 392      */
 393     virtual float yUnitsToPoints(float yUnits) const;
 394 
 395     /**
 396      * This method converts font design units to points.
 397      *
 398      * @param units - X and Y design units
 399      * @param points - set to X and Y points
 400      *
 401      * @stable ICU 3.2
 402      */
 403     virtual void unitsToPoints(LEPoint &units, LEPoint &points) const;
 404 
 405     /**
 406      * This method converts pixels in the
 407      * X direction to font design units.
 408      *
 409      * @param xPixels - pixels in the X direction
 410      *
 411      * @return font design units in the X direction
 412      *
 413      * @stable ICU 3.2
 414      */
 415     virtual float xPixelsToUnits(float xPixels) const;
 416 
 417     /**
 418      * This method converts pixels in the
 419      * Y direction to font design units.
 420      *
 421      * @param yPixels - pixels in the Y direction
 422      *
 423      * @return font design units in the Y direction
 424      *
 425      * @stable ICU 3.2
 426      */
 427     virtual float yPixelsToUnits(float yPixels) const;
 428 
 429     /**
 430      * This method converts pixels to font design units.
 431      *
 432      * @param pixels - X and Y pixel
 433      * @param units - set to X and Y font design units
 434      *
 435      * @stable ICU 3.2
 436      */
 437     virtual void pixelsToUnits(LEPoint &pixels, LEPoint &units) const;
 438 
 439     /**
 440      * Get the X scale factor from the font's transform. The default
 441      * implementation of <code>transformFunits()</code> will call this method.
 442      *
 443      * @return the X scale factor.
 444      *
 445      *
 446      * @see transformFunits
 447      *
 448      * @stable ICU 3.2
 449      */
 450     virtual float getScaleFactorX() const = 0;
 451 
 452     /**
 453      * Get the Y scale factor from the font's transform. The default
 454      * implementation of <code>transformFunits()</code> will call this method.
 455      *
 456      * @return the Yscale factor.
 457      *
 458      * @see transformFunits
 459      *
 460      * @stable ICU 3.2
 461      */
 462     virtual float getScaleFactorY() const = 0;
 463 
 464     /**
 465      * This method transforms an X, Y point in font design units to a
 466      * pixel coordinate, applying the font's transform. The default
 467      * implementation of this method calls <code>getScaleFactorX()</code>
 468      * and <code>getScaleFactorY()</code>.
 469      *
 470      * @param xFunits - the X coordinate in font design units
 471      * @param yFunits - the Y coordinate in font design units
 472      * @param pixels - the tranformed co-ordinate in pixels
 473      *
 474      * @see getScaleFactorX
 475      * @see getScaleFactorY
 476      *
 477      * @stable ICU 3.2
 478      */
 479     virtual void transformFunits(float xFunits, float yFunits, LEPoint &pixels) const;
 480 
 481     /**
 482      * This is a convenience method used to convert
 483      * values in a 16.16 fixed point format to floating point.
 484      *
 485      * @param fixed - the fixed point value
 486      *
 487      * @return the floating point value
 488      *
 489      * @stable ICU 2.8
 490      */
 491     static inline float fixedToFloat(le_int32 fixed);
 492 
 493     /**
 494      * This is a convenience method used to convert
 495      * floating point values to 16.16 fixed point format.
 496      *
 497      * @param theFloat - the floating point value
 498      *
 499      * @return the fixed point value
 500      *
 501      * @stable ICU 2.8
 502      */
 503     static inline le_int32 floatToFixed(float theFloat);
 504 
 505     //
 506     // These methods won't ever be called by the LayoutEngine,
 507     // but are useful for clients of <code>LEFontInstance</code> who
 508     // need to render text.
 509     //
 510 
 511     /**
 512      * Get the font's ascent.
 513      *
 514      * @return the font's ascent, in points. This value
 515      * will always be positive.
 516      *
 517      * @stable ICU 3.2
 518      */
 519     virtual le_int32 getAscent() const = 0;
 520 
 521     /**
 522      * Get the font's descent.
 523      *
 524      * @return the font's descent, in points. This value
 525      * will always be positive.
 526      *
 527      * @stable ICU 3.2
 528      */
 529     virtual le_int32 getDescent() const = 0;
 530 
 531     /**
 532      * Get the font's leading.
 533      *
 534      * @return the font's leading, in points. This value
 535      * will always be positive.
 536      *
 537      * @stable ICU 3.2
 538      */
 539     virtual le_int32 getLeading() const = 0;
 540 
 541     /**
 542      * Get the line height required to display text in
 543      * this font. The default implementation of this method
 544      * returns the sum of the ascent, descent, and leading.
 545      *
 546      * @return the line height, in points. This vaule will
 547      * always be positive.
 548      *
 549      * @stable ICU 3.2
 550      */
 551     virtual le_int32 getLineHeight() const;
 552 
 553     /**
 554      * ICU "poor man's RTTI", returns a UClassID for the actual class.
 555      *
 556      * @stable ICU 3.2
 557      */
 558     virtual UClassID getDynamicClassID() const;
 559 
 560     /**
 561      * ICU "poor man's RTTI", returns a UClassID for this class.
 562      *
 563      * @stable ICU 3.2
 564      */
 565     static UClassID getStaticClassID();
 566 
 567 };
 568 
 569 inline float LEFontInstance::fixedToFloat(le_int32 fixed)
 570 {
 571     return (float) (fixed / 65536.0);
 572 }
 573 
 574 inline le_int32 LEFontInstance::floatToFixed(float theFloat)
 575 {
 576     return (le_int32) (theFloat * 65536.0);
 577 }
 578 
 579 U_NAMESPACE_END
 580 #endif