LayoutEngine.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*
  2. * (C) Copyright IBM Corp. and others 1998-2014 - All Rights Reserved
  3. */
  4. #ifndef __LAYOUTENGINE_H
  5. #define __LAYOUTENGINE_H
  6. #include "LETypes.h"
  7. /**
  8. * \file
  9. * \brief C++ API: DEPRECATED: Virtual base class for complex text layout.
  10. */
  11. U_NAMESPACE_BEGIN
  12. class LEFontInstance;
  13. class LEGlyphFilter;
  14. class LEGlyphStorage;
  15. /**
  16. * NOTE: This class is deprecated, please instead use HarfBuzz.
  17. * See: http://www.freedesktop.org/wiki/Software/HarfBuzz/
  18. * and http://userguide.icu-project.org/layoutengine
  19. *
  20. * This is a virtual base class used to do complex text layout. The text must all
  21. * be in a single font, script, and language. An instance of a LayoutEngine can be
  22. * created by calling the layoutEngineFactory method. Fonts are identified by
  23. * instances of the LEFontInstance class. Script and language codes are identified
  24. * by integer codes, which are defined in ScriptAndLanuageTags.h.
  25. *
  26. * Note that this class is not public API. It is declared public so that it can be
  27. * exported from the library that it is a part of.
  28. *
  29. * The input to the layout process is an array of characters in logical order,
  30. * and a starting X, Y position for the text. The output is an array of glyph indices,
  31. * an array of character indices for the glyphs, and an array of glyph positions.
  32. * These arrays are protected members of LayoutEngine which can be retreived by a
  33. * public method. The reset method can be called to free these arrays so that the
  34. * LayoutEngine can be reused.
  35. *
  36. * The layout process is done in three steps. There is a protected virtual method
  37. * for each step. These methods have a default implementation which only does
  38. * character to glyph mapping and default positioning using the glyph's advance
  39. * widths. Subclasses can override these methods for more advanced layout.
  40. * There is a public method which invokes the steps in the correct order.
  41. *
  42. * The steps are:
  43. *
  44. * 1) Glyph processing - character to glyph mapping and any other glyph processing
  45. * such as ligature substitution and contextual forms.
  46. *
  47. * 2) Glyph positioning - position the glyphs based on their advance widths.
  48. *
  49. * 3) Glyph position adjustments - adjustment of glyph positions for kerning,
  50. * accent placement, etc.
  51. *
  52. * NOTE: in all methods below, output parameters are references to pointers so
  53. * the method can allocate and free the storage as needed. All storage allocated
  54. * in this way is owned by the object which created it, and will be freed when it
  55. * is no longer needed, or when the object's destructor is invoked.
  56. *
  57. * @see LEFontInstance
  58. * @see ScriptAndLanguageTags.h
  59. *
  60. * @deprecated ICU 54. See {@link icu::LayoutEngine}
  61. * Instead, please use HarfBuzz.
  62. * see http://www.freedesktop.org/wiki/Software/HarfBuzz/
  63. * and http://userguide.icu-project.org/layoutengine
  64. */
  65. class U_LAYOUT_API LayoutEngine : public UObject {
  66. public:
  67. #ifndef U_HIDE_INTERNAL_API
  68. /** @internal Flag to request kerning. Use LE_Kerning_FEATURE_FLAG instead. */
  69. static const le_int32 kTypoFlagKern;
  70. /** @internal Flag to request ligatures. Use LE_Ligatures_FEATURE_FLAG instead. */
  71. static const le_int32 kTypoFlagLiga;
  72. #endif /* U_HIDE_INTERNAL_API */
  73. protected:
  74. /**
  75. * The object which holds the glyph storage
  76. *
  77. * @internal
  78. */
  79. LEGlyphStorage *fGlyphStorage;
  80. /**
  81. * The font instance for the text font.
  82. *
  83. * @see LEFontInstance
  84. *
  85. * @internal
  86. */
  87. const LEFontInstance *fFontInstance;
  88. /**
  89. * The script code for the text
  90. *
  91. * @see ScriptAndLanguageTags.h for script codes.
  92. *
  93. * @internal
  94. */
  95. le_int32 fScriptCode;
  96. /**
  97. * The langauge code for the text
  98. *
  99. * @see ScriptAndLanguageTags.h for language codes.
  100. *
  101. * @internal
  102. */
  103. le_int32 fLanguageCode;
  104. /**
  105. * The typographic control flags
  106. *
  107. * @internal
  108. */
  109. le_int32 fTypoFlags;
  110. /**
  111. * <code>TRUE</code> if <code>mapCharsToGlyphs</code> should replace ZWJ / ZWNJ with a glyph
  112. * with no contours.
  113. *
  114. * @internal
  115. */
  116. le_bool fFilterZeroWidth;
  117. #ifndef U_HIDE_INTERNAL_API
  118. /**
  119. * This constructs an instance for a given font, script and language. Subclass constructors
  120. * must call this constructor.
  121. *
  122. * @param fontInstance - the font for the text
  123. * @param scriptCode - the script for the text
  124. * @param languageCode - the language for the text
  125. * @param typoFlags - the typographic control flags for the text (a bitfield). Use kTypoFlagKern
  126. * if kerning is desired, kTypoFlagLiga if ligature formation is desired. Others are reserved.
  127. * @param success - set to an error code if the operation fails
  128. *
  129. * @see LEFontInstance
  130. * @see ScriptAndLanguageTags.h
  131. *
  132. * @internal
  133. */
  134. LayoutEngine(const LEFontInstance *fontInstance,
  135. le_int32 scriptCode,
  136. le_int32 languageCode,
  137. le_int32 typoFlags,
  138. LEErrorCode &success);
  139. #endif /* U_HIDE_INTERNAL_API */
  140. // Do not enclose the protected default constructor with #ifndef U_HIDE_INTERNAL_API
  141. // or else the compiler will create a public default constructor.
  142. /**
  143. * This overrides the default no argument constructor to make it
  144. * difficult for clients to call it. Clients are expected to call
  145. * layoutEngineFactory.
  146. *
  147. * @internal
  148. */
  149. LayoutEngine();
  150. /**
  151. * This method does any required pre-processing to the input characters. It
  152. * may generate output characters that differ from the input charcters due to
  153. * insertions, deletions, or reorderings. In such cases, it will also generate an
  154. * output character index array reflecting these changes.
  155. *
  156. * Subclasses must override this method.
  157. *
  158. * Input parameters:
  159. * @param chars - the input character context
  160. * @param offset - the index of the first character to process
  161. * @param count - the number of characters to process
  162. * @param max - the number of characters in the input context
  163. * @param rightToLeft - TRUE if the characters are in a right to left directional run
  164. * @param outChars - the output character array, if different from the input
  165. * @param glyphStorage - the object that holds the per-glyph storage. The character index array may be set.
  166. * @param success - set to an error code if the operation fails
  167. *
  168. * @return the output character count (input character count if no change)
  169. *
  170. * @internal
  171. */
  172. virtual le_int32 characterProcessing(const LEUnicode chars[], le_int32 offset, le_int32 count, le_int32 max, le_bool rightToLeft,
  173. LEUnicode *&outChars, LEGlyphStorage &glyphStorage, LEErrorCode &success);
  174. /**
  175. * This method does the glyph processing. It converts an array of characters
  176. * into an array of glyph indices and character indices. The characters to be
  177. * processed are passed in a surrounding context. The context is specified as
  178. * a starting address and a maximum character count. An offset and a count are
  179. * used to specify the characters to be processed.
  180. *
  181. * The default implementation of this method only does character to glyph mapping.
  182. * Subclasses needing more elaborate glyph processing must override this method.
  183. *
  184. * Input parameters:
  185. * @param chars - the character context
  186. * @param offset - the offset of the first character to process
  187. * @param count - the number of characters to process
  188. * @param max - the number of characters in the context.
  189. * @param rightToLeft - TRUE if the text is in a right to left directional run
  190. * @param glyphStorage - the object which holds the per-glyph storage. The glyph and char indices arrays
  191. * will be set.
  192. *
  193. * Output parameters:
  194. * @param success - set to an error code if the operation fails
  195. *
  196. * @return the number of glyphs in the glyph index array
  197. *
  198. * @internal
  199. */
  200. virtual le_int32 computeGlyphs(const LEUnicode chars[], le_int32 offset, le_int32 count, le_int32 max, le_bool rightToLeft, LEGlyphStorage &glyphStorage, LEErrorCode &success);
  201. /**
  202. * This method does basic glyph positioning. The default implementation positions
  203. * the glyphs based on their advance widths. This is sufficient for most uses. It
  204. * is not expected that many subclasses will override this method.
  205. *
  206. * Input parameters:
  207. * @param glyphStorage - the object which holds the per-glyph storage. The glyph position array will be set.
  208. * @param x - the starting X position
  209. * @param y - the starting Y position
  210. * @param success - set to an error code if the operation fails
  211. *
  212. * @internal
  213. */
  214. virtual void positionGlyphs(LEGlyphStorage &glyphStorage, float x, float y, LEErrorCode &success);
  215. /**
  216. * This method does positioning adjustments like accent positioning and
  217. * kerning. The default implementation does nothing. Subclasses needing
  218. * position adjustments must override this method.
  219. *
  220. * Note that this method has both characters and glyphs as input so that
  221. * it can use the character codes to determine glyph types if that information
  222. * isn't directly available. (e.g. Some Arabic OpenType fonts don't have a GDEF
  223. * table)
  224. *
  225. * @param chars - the input character context
  226. * @param offset - the offset of the first character to process
  227. * @param count - the number of characters to process
  228. * @param reverse - <code>TRUE</code> if the glyphs in the glyph array have been reordered
  229. * @param glyphStorage - the object which holds the per-glyph storage. The glyph positions will be
  230. * adjusted as needed.
  231. * @param success - output parameter set to an error code if the operation fails
  232. *
  233. * @internal
  234. */
  235. virtual void adjustGlyphPositions(const LEUnicode chars[], le_int32 offset, le_int32 count, le_bool reverse, LEGlyphStorage &glyphStorage, LEErrorCode &success);
  236. /**
  237. * This method gets a table from the font associated with
  238. * the text. The default implementation gets the table from
  239. * the font instance. Subclasses which need to get the tables
  240. * some other way must override this method.
  241. *
  242. * @param tableTag - the four byte table tag.
  243. * @param length - length to use
  244. *
  245. * @return the address of the table.
  246. *
  247. * @internal
  248. */
  249. virtual const void *getFontTable(LETag tableTag, size_t &length) const;
  250. /**
  251. * @deprecated ICU 54. See {@link icu::LayoutEngine}
  252. */
  253. virtual const void *getFontTable(LETag tableTag) const { size_t ignored; return getFontTable(tableTag, ignored); }
  254. /**
  255. * This method does character to glyph mapping. The default implementation
  256. * uses the font instance to do the mapping. It will allocate the glyph and
  257. * character index arrays if they're not already allocated. If it allocates the
  258. * character index array, it will fill it it.
  259. *
  260. * This method supports right to left
  261. * text with the ability to store the glyphs in reverse order, and by supporting
  262. * character mirroring, which will replace a character which has a left and right
  263. * form, such as parens, with the opposite form before mapping it to a glyph index.
  264. *
  265. * Input parameters:
  266. * @param chars - the input character context
  267. * @param offset - the offset of the first character to be mapped
  268. * @param count - the number of characters to be mapped
  269. * @param reverse - if <code>TRUE</code>, the output will be in reverse order
  270. * @param mirror - if <code>TRUE</code>, do character mirroring
  271. * @param glyphStorage - the object which holds the per-glyph storage. The glyph and char
  272. * indices arrays will be filled in.
  273. * @param success - set to an error code if the operation fails
  274. *
  275. * @see LEFontInstance
  276. *
  277. * @internal
  278. */
  279. virtual void mapCharsToGlyphs(const LEUnicode chars[], le_int32 offset, le_int32 count, le_bool reverse, le_bool mirror, LEGlyphStorage &glyphStorage, LEErrorCode &success);
  280. #ifndef U_HIDE_INTERNAL_API
  281. /**
  282. * This is a convenience method that forces the advance width of mark
  283. * glyphs to be zero, which is required for proper selection and highlighting.
  284. *
  285. * @param glyphStorage - the object containing the per-glyph storage. The positions array will be modified.
  286. * @param markFilter - used to identify mark glyphs
  287. * @param success - output parameter set to an error code if the operation fails
  288. *
  289. * @see LEGlyphFilter
  290. *
  291. * @internal
  292. */
  293. static void adjustMarkGlyphs(LEGlyphStorage &glyphStorage, LEGlyphFilter *markFilter, LEErrorCode &success);
  294. /**
  295. * This is a convenience method that forces the advance width of mark
  296. * glyphs to be zero, which is required for proper selection and highlighting.
  297. * This method uses the input characters to identify marks. This is required in
  298. * cases where the font does not contain enough information to identify them based
  299. * on the glyph IDs.
  300. *
  301. * @param chars - the array of input characters
  302. * @param charCount - the number of input characers
  303. * @param glyphStorage - the object containing the per-glyph storage. The positions array will be modified.
  304. * @param reverse - <code>TRUE</code> if the glyph array has been reordered
  305. * @param markFilter - used to identify mark glyphs
  306. * @param success - output parameter set to an error code if the operation fails
  307. *
  308. * @see LEGlyphFilter
  309. *
  310. * @internal
  311. */
  312. static void adjustMarkGlyphs(const LEUnicode chars[], le_int32 charCount, le_bool reverse, LEGlyphStorage &glyphStorage, LEGlyphFilter *markFilter, LEErrorCode &success);
  313. #endif /* U_HIDE_INTERNAL_API */
  314. public:
  315. /**
  316. * The destructor. It will free any storage allocated for the
  317. * glyph, character index and position arrays by calling the reset
  318. * method. It is declared virtual so that it will be invoked by the
  319. * subclass destructors.
  320. *
  321. * @deprecated ICU 54. See {@link icu::LayoutEngine}
  322. */
  323. virtual ~LayoutEngine();
  324. /**
  325. * This method will invoke the layout steps in their correct order by calling
  326. * the computeGlyphs, positionGlyphs and adjustGlyphPosition methods. It will
  327. * compute the glyph, character index and position arrays.
  328. *
  329. * @param chars - the input character context
  330. * @param offset - the offset of the first character to process
  331. * @param count - the number of characters to process
  332. * @param max - the number of characters in the input context
  333. * @param rightToLeft - TRUE if the characers are in a right to left directional run
  334. * @param x - the initial X position
  335. * @param y - the initial Y position
  336. * @param success - output parameter set to an error code if the operation fails
  337. *
  338. * @return the number of glyphs in the glyph array
  339. *
  340. * Note: The glyph, character index and position array can be accessed
  341. * using the getter methods below.
  342. *
  343. * Note: If you call this method more than once, you must call the reset()
  344. * method first to free the glyph, character index and position arrays
  345. * allocated by the previous call.
  346. *
  347. * @deprecated ICU 54. See {@link icu::LayoutEngine}
  348. */
  349. virtual le_int32 layoutChars(const LEUnicode chars[], le_int32 offset, le_int32 count, le_int32 max, le_bool rightToLeft, float x, float y, LEErrorCode &success);
  350. /**
  351. * This method returns the number of glyphs in the glyph array. Note
  352. * that the number of glyphs will be greater than or equal to the number
  353. * of characters used to create the LayoutEngine.
  354. *
  355. * @return the number of glyphs in the glyph array
  356. *
  357. * @deprecated ICU 54. See {@link icu::LayoutEngine}
  358. */
  359. le_int32 getGlyphCount() const;
  360. /**
  361. * This method copies the glyph array into a caller supplied array.
  362. * The caller must ensure that the array is large enough to hold all
  363. * the glyphs.
  364. *
  365. * @param glyphs - the destiniation glyph array
  366. * @param success - set to an error code if the operation fails
  367. *
  368. * @deprecated ICU 54. See {@link icu::LayoutEngine}
  369. */
  370. void getGlyphs(LEGlyphID glyphs[], LEErrorCode &success) const;
  371. /**
  372. * This method copies the glyph array into a caller supplied array,
  373. * ORing in extra bits. (This functionality is needed by the JDK,
  374. * which uses 32 bits pre glyph idex, with the high 16 bits encoding
  375. * the composite font slot number)
  376. *
  377. * @param glyphs - the destination (32 bit) glyph array
  378. * @param extraBits - this value will be ORed with each glyph index
  379. * @param success - set to an error code if the operation fails
  380. *
  381. * @deprecated ICU 54. See {@link icu::LayoutEngine}
  382. */
  383. virtual void getGlyphs(le_uint32 glyphs[], le_uint32 extraBits, LEErrorCode &success) const;
  384. /**
  385. * This method copies the character index array into a caller supplied array.
  386. * The caller must ensure that the array is large enough to hold a
  387. * character index for each glyph.
  388. *
  389. * @param charIndices - the destiniation character index array
  390. * @param success - set to an error code if the operation fails
  391. *
  392. * @deprecated ICU 54. See {@link icu::LayoutEngine}
  393. */
  394. void getCharIndices(le_int32 charIndices[], LEErrorCode &success) const;
  395. /**
  396. * This method copies the character index array into a caller supplied array.
  397. * The caller must ensure that the array is large enough to hold a
  398. * character index for each glyph.
  399. *
  400. * @param charIndices - the destiniation character index array
  401. * @param indexBase - an offset which will be added to each index
  402. * @param success - set to an error code if the operation fails
  403. *
  404. * @deprecated ICU 54. See {@link icu::LayoutEngine}
  405. */
  406. void getCharIndices(le_int32 charIndices[], le_int32 indexBase, LEErrorCode &success) const;
  407. /**
  408. * This method copies the position array into a caller supplied array.
  409. * The caller must ensure that the array is large enough to hold an
  410. * X and Y position for each glyph, plus an extra X and Y for the
  411. * advance of the last glyph.
  412. *
  413. * @param positions - the destiniation position array
  414. * @param success - set to an error code if the operation fails
  415. *
  416. * @deprecated ICU 54. See {@link icu::LayoutEngine}
  417. */
  418. void getGlyphPositions(float positions[], LEErrorCode &success) const;
  419. /**
  420. * This method returns the X and Y position of the glyph at
  421. * the given index.
  422. *
  423. * Input parameters:
  424. * @param glyphIndex - the index of the glyph
  425. *
  426. * Output parameters:
  427. * @param x - the glyph's X position
  428. * @param y - the glyph's Y position
  429. * @param success - set to an error code if the operation fails
  430. *
  431. * @deprecated ICU 54. See {@link icu::LayoutEngine}
  432. */
  433. void getGlyphPosition(le_int32 glyphIndex, float &x, float &y, LEErrorCode &success) const;
  434. /**
  435. * This method frees the glyph, character index and position arrays
  436. * so that the LayoutEngine can be reused to layout a different
  437. * characer array. (This method is also called by the destructor)
  438. *
  439. * @deprecated ICU 54. See {@link icu::LayoutEngine}
  440. */
  441. virtual void reset();
  442. /**
  443. * This method returns a LayoutEngine capable of laying out text
  444. * in the given font, script and langauge. Note that the LayoutEngine
  445. * returned may be a subclass of LayoutEngine.
  446. *
  447. * @param fontInstance - the font of the text
  448. * @param scriptCode - the script of the text
  449. * @param languageCode - the language of the text
  450. * @param success - output parameter set to an error code if the operation fails
  451. *
  452. * @return a LayoutEngine which can layout text in the given font.
  453. *
  454. * @see LEFontInstance
  455. *
  456. * @deprecated ICU 54. See {@link icu::LayoutEngine}
  457. */
  458. static LayoutEngine *layoutEngineFactory(const LEFontInstance *fontInstance, le_int32 scriptCode, le_int32 languageCode, LEErrorCode &success);
  459. /**
  460. * Override of existing call that provides flags to control typography.
  461. * @deprecated ICU 54. See {@link icu::LayoutEngine}
  462. */
  463. static LayoutEngine *layoutEngineFactory(const LEFontInstance *fontInstance, le_int32 scriptCode, le_int32 languageCode, le_int32 typo_flags, LEErrorCode &success);
  464. /**
  465. * ICU "poor man's RTTI", returns a UClassID for the actual class.
  466. *
  467. * @deprecated ICU 54. See {@link icu::LayoutEngine}
  468. */
  469. virtual UClassID getDynamicClassID() const;
  470. /**
  471. * ICU "poor man's RTTI", returns a UClassID for this class.
  472. *
  473. * @deprecated ICU 54. See {@link icu::LayoutEngine}
  474. */
  475. static UClassID getStaticClassID();
  476. };
  477. U_NAMESPACE_END
  478. #endif