Segment.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /* GRAPHITE2 LICENSING
  2. Copyright 2010, SIL International
  3. All rights reserved.
  4. This library is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation; either version 2.1 of License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should also have received a copy of the GNU Lesser General Public
  13. License along with this library in the file named "LICENSE".
  14. If not, write to the Free Software Foundation, 51 Franklin Street,
  15. Suite 500, Boston, MA 02110-1335, USA or visit their web page on the
  16. internet at http://www.fsf.org/licenses/lgpl.html.
  17. Alternatively, the contents of this file may be used under the terms
  18. of the Mozilla Public License (http://mozilla.org/MPL) or the GNU
  19. General Public License, as published by the Free Software Foundation,
  20. either version 2 of the License or (at your option) any later version.
  21. */
  22. #pragma once
  23. #include "graphite2/Types.h"
  24. #include "graphite2/Font.h"
  25. #ifdef __cplusplus
  26. extern "C"
  27. {
  28. #endif
  29. enum gr_break_weight {
  30. gr_breakNone = 0,
  31. /* after break weights */
  32. gr_breakWhitespace = 10,
  33. gr_breakWord = 15,
  34. gr_breakIntra = 20,
  35. gr_breakLetter = 30,
  36. gr_breakClip = 40,
  37. /* before break weights */
  38. gr_breakBeforeWhitespace = -10,
  39. gr_breakBeforeWord = -15,
  40. gr_breakBeforeIntra = -20,
  41. gr_breakBeforeLetter = -30,
  42. gr_breakBeforeClip = -40
  43. };
  44. enum gr_justFlags {
  45. /// Indicates that this segment is a complete line
  46. gr_justCompleteLine = 0,
  47. /// Indicates that the start of the slot list is not at the start of a line
  48. gr_justStartInline = 1,
  49. /// Indicates that the end of the slot list is not at the end of a line
  50. gr_justEndInline = 2
  51. };
  52. /** Used for looking up slot attributes. Most are already available in other functions **/
  53. enum gr_attrCode {
  54. /// adjusted glyph advance in x direction in design units
  55. gr_slatAdvX = 0,
  56. /// adjusted glyph advance in y direction (usually 0) in design units
  57. gr_slatAdvY,
  58. /// returns 0. Deprecated.
  59. gr_slatAttTo,
  60. /// This slot attaches to its parent at the given design units in the x direction
  61. gr_slatAttX,
  62. /// This slot attaches to its parent at the given design units in the y direction
  63. gr_slatAttY,
  64. /// This slot attaches to its parent at the given glyph point (not implemented)
  65. gr_slatAttGpt,
  66. /// x-direction adjustment from the given glyph point (not implemented)
  67. gr_slatAttXOff,
  68. /// y-direction adjustment from the given glyph point (not implemented)
  69. gr_slatAttYOff,
  70. /// Where on this glyph should align with the attachment point on the parent glyph in the x-direction.
  71. gr_slatAttWithX,
  72. /// Where on this glyph should align with the attachment point on the parent glyph in the y-direction
  73. gr_slatAttWithY,
  74. /// Which glyph point on this glyph should align with the attachment point on the parent glyph (not implemented).
  75. gr_slatWithGpt,
  76. /// Adjustment to gr_slatWithGpt in x-direction (not implemented)
  77. gr_slatAttWithXOff,
  78. /// Adjustment to gr_slatWithGpt in y-direction (not implemented)
  79. gr_slatAttWithYOff,
  80. /// Attach at given nesting level (not implemented)
  81. gr_slatAttLevel,
  82. /// Line break breakweight for this glyph
  83. gr_slatBreak,
  84. /// Ligature component reference (not implemented)
  85. gr_slatCompRef,
  86. /// bidi directionality of this glyph (not implemented)
  87. gr_slatDir,
  88. /// Whether insertion is allowed before this glyph
  89. gr_slatInsert,
  90. /// Final positioned position of this glyph relative to its parent in x-direction in pixels
  91. gr_slatPosX,
  92. /// Final positioned position of this glyph relative to its parent in y-direction in pixels
  93. gr_slatPosY,
  94. /// Amount to shift glyph by in x-direction design units
  95. gr_slatShiftX,
  96. /// Amount to shift glyph by in y-direction design units
  97. gr_slatShiftY,
  98. /// attribute user1
  99. gr_slatUserDefnV1,
  100. /// not implemented
  101. gr_slatMeasureSol,
  102. /// not implemented
  103. gr_slatMeasureEol,
  104. /// Amount this slot can stretch (not implemented)
  105. gr_slatJStretch,
  106. /// Amount this slot can shrink (not implemented)
  107. gr_slatJShrink,
  108. /// Granularity by which this slot can stretch or shrink (not implemented)
  109. gr_slatJStep,
  110. /// Justification weight for this glyph (not implemented)
  111. gr_slatJWeight,
  112. /// Amount this slot mush shrink or stretch in design units
  113. gr_slatJWidth = 29,
  114. /// SubSegment split point
  115. gr_slatSegSplit = gr_slatJStretch + 29,
  116. /// User defined attribute, see subattr for user attr number
  117. gr_slatUserDefn,
  118. /// Bidi level
  119. gr_slatBidiLevel = 56,
  120. /// Collision flags
  121. gr_slatColFlags,
  122. /// Collision constraint rectangle left (bl.x)
  123. gr_slatColLimitblx,
  124. /// Collision constraint rectangle lower (bl.y)
  125. gr_slatColLimitbly,
  126. /// Collision constraint rectangle right (tr.x)
  127. gr_slatColLimittrx,
  128. /// Collision constraint rectangle upper (tr.y)
  129. gr_slatColLimittry,
  130. /// Collision shift x
  131. gr_slatColShiftx,
  132. /// Collision shift y
  133. gr_slatColShifty,
  134. /// Collision margin
  135. gr_slatColMargin,
  136. /// Margin cost weight
  137. gr_slatColMarginWt,
  138. // Additional glyph that excludes movement near this one:
  139. gr_slatColExclGlyph,
  140. gr_slatColExclOffx,
  141. gr_slatColExclOffy,
  142. // Collision sequence enforcing attributes:
  143. gr_slatSeqClass,
  144. gr_slatSeqProxClass,
  145. gr_slatSeqOrder,
  146. gr_slatSeqAboveXoff,
  147. gr_slatSeqAboveWt,
  148. gr_slatSeqBelowXlim,
  149. gr_slatSeqBelowWt,
  150. gr_slatSeqValignHt,
  151. gr_slatSeqValignWt,
  152. /// not implemented
  153. gr_slatMax,
  154. /// not implemented
  155. gr_slatNoEffect = gr_slatMax + 1
  156. };
  157. enum gr_bidirtl {
  158. /// Underlying paragraph direction is RTL
  159. gr_rtl = 1,
  160. /// Set this to not run the bidi pass internally, even if the font asks for it.
  161. /// This presumes that the segment is in a single direction. Most of the time
  162. /// this bit should be set unless you know you are passing full paragraphs of text.
  163. gr_nobidi = 2,
  164. /// Disable auto mirroring for rtl text
  165. gr_nomirror = 4
  166. };
  167. typedef struct gr_char_info gr_char_info;
  168. typedef struct gr_segment gr_segment;
  169. typedef struct gr_slot gr_slot;
  170. /** Returns Unicode character for a charinfo.
  171. *
  172. * @param p Pointer to charinfo to return information on.
  173. */
  174. GR2_API unsigned int gr_cinfo_unicode_char(const gr_char_info* p/*not NULL*/);
  175. /** Returns breakweight for a charinfo.
  176. *
  177. * @return Breakweight is a number between -50 and 50 indicating the cost of a
  178. * break before or after this character. If the value < 0, the absolute value
  179. * is this character's contribution to the overall breakweight before it. If the value
  180. * > 0, then the value is this character's contribution to the overall breakweight after it.
  181. * The overall breakweight between two characters is the maximum of the breakweight
  182. * contributions from the characters either side of it. If a character makes no
  183. * contribution to the breakweight on one side of it, the contribution is considered
  184. * to be 0.
  185. * @param p Pointer to charinfo to return information on.
  186. */
  187. GR2_API int gr_cinfo_break_weight(const gr_char_info* p/*not NULL*/);
  188. /** Returns the slot index that after this character is after in the slot stream
  189. *
  190. * In effect each character is associated with a set of slots and this returns
  191. * the index of the last slot in the segment this character is associated with.
  192. *
  193. * @return after slot index between 0 and gr_seg_n_slots()
  194. * @param p Pointer to charinfo to return information on.
  195. */
  196. GR2_API int gr_cinfo_after(const gr_char_info* p/*not NULL*/);
  197. /** Returns the slot index that before this character is before in the slot stream
  198. *
  199. * In effect each character is associated with a set of slots and this returns
  200. * the index of the first slot in the segment this character is associated with.
  201. *
  202. * @return before slot index between 0 and gr_seg_n_slots()
  203. * @param p Pointer to charinfo to return information on.
  204. */
  205. GR2_API int gr_cinfo_before(const gr_char_info* p/*not NULL*/);
  206. /** Returns the code unit index of this character in the input string
  207. *
  208. * @return code unit index between 0 and the end of the string
  209. * @param p Pointer to charinfo to return information on.
  210. */
  211. GR2_API size_t gr_cinfo_base(const gr_char_info* p/*not NULL*/);
  212. /** Returns the number of unicode characters in a string.
  213. *
  214. * @return number of characters in the string
  215. * @param enc Specifies the type of data in the string: utf8, utf16, utf32
  216. * @param buffer_begin The start of the string
  217. * @param buffer_end Measure up to the first nul or when end is reached, whichever is earliest.
  218. * This parameter may be NULL.
  219. * @param pError If there is a structural fault in the string, the location is returned
  220. * in this variable. If no error occurs, pError will contain NULL. NULL
  221. * may be passed for pError if no such information is required.
  222. */
  223. GR2_API size_t gr_count_unicode_characters(enum gr_encform enc, const void* buffer_begin, const void* buffer_end, const void** pError);
  224. /** Creates and returns a segment.
  225. *
  226. * @return a segment that needs seg_destroy called on it. May return NULL if bad problems
  227. * in segment processing.
  228. * @param font Gives the size of the font in pixels per em for final positioning. If
  229. * NULL, positions are returned in design units, i.e. at a ppm of the upem
  230. * of the face.
  231. * @param face The face containing all the non-size dependent information.
  232. * @param script This is a tag containing a script identifier that is used to choose
  233. * which graphite table within the font to use. Maybe 0. Tag may be 4 chars
  234. * NULL padded in LSBs or space padded in LSBs.
  235. * @param pFeats Pointer to a feature values to be used for the segment. Only one
  236. * feature values may be used for a segment. If NULL the default features
  237. * for the font will be used.
  238. * @param enc Specifies what encoding form the string is in (utf8, utf16, utf32)
  239. * @param pStart Start of the string
  240. * @param nChars Number of unicode characters to process in the string. The string will
  241. * be processed either up to the first NULL or until nChars have been
  242. * processed. nChars is also used to initialise the internal memory
  243. * allocations of the segment. So it is wise not to make nChars too much
  244. * greater than the actual number of characters being processed.
  245. * @param dir Specifies whether the segment is processed right to left (1) or left to
  246. * right (0) and whether to run the internal bidi pass, if a font requests it.
  247. * See enum gr_bidirtl for details.
  248. */
  249. GR2_API gr_segment* gr_make_seg(const gr_font* font, const gr_face* face, gr_uint32 script, const gr_feature_val* pFeats, enum gr_encform enc, const void* pStart, size_t nChars, int dir);
  250. /** Destroys a segment, freeing the memory.
  251. *
  252. * @param p The segment to destroy
  253. */
  254. GR2_API void gr_seg_destroy(gr_segment* p);
  255. /** Returns the advance for the whole segment.
  256. *
  257. * Returns the width of the segment up to the next glyph origin after the segment
  258. */
  259. GR2_API float gr_seg_advance_X(const gr_segment* pSeg/*not NULL*/);
  260. /** Returns the height advance for the segment. **/
  261. GR2_API float gr_seg_advance_Y(const gr_segment* pSeg/*not NULL*/);
  262. /** Returns the number of gr_char_infos in the segment. **/
  263. GR2_API unsigned int gr_seg_n_cinfo(const gr_segment* pSeg/*not NULL*/);
  264. /** Returns a gr_char_info at a given index in the segment. **/
  265. GR2_API const gr_char_info* gr_seg_cinfo(const gr_segment* pSeg/*not NULL*/, unsigned int index/*must be <number_of_CharInfo*/);
  266. /** Returns the number of glyph gr_slots in the segment. **/
  267. GR2_API unsigned int gr_seg_n_slots(const gr_segment* pSeg/*not NULL*/); //one slot per glyph
  268. /** Returns the first gr_slot in the segment.
  269. *
  270. * The first slot in a segment has a gr_slot_prev_in_segment() of NULL. Slots are owned
  271. * by their segment and are destroyed along with the segment.
  272. */
  273. GR2_API const gr_slot* gr_seg_first_slot(gr_segment* pSeg/*not NULL*/); //may give a base slot or a slot which is attached to another
  274. /** Returns the last gr_slot in the segment.
  275. *
  276. * The last slot in a segment has a gr_slot_next_in_segment() of NULL
  277. */
  278. GR2_API const gr_slot* gr_seg_last_slot(gr_segment* pSeg/*not NULL*/); //may give a base slot or a slot which is attached to another
  279. /** Justifies a linked list of slots for a line to a given width
  280. *
  281. * Passed a pointer to the start of a linked list of slots corresponding to a line, as
  282. * set up by gr_slot_linebreak_before, this function will position the glyphs in the line
  283. * to take up the given width. It is possible to specify a subrange within the line to process.
  284. * This allows skipping of line initial or final whitespace, for example. While this will ensure
  285. * that the subrange fits width, the line will still be positioned with the first glyph of the
  286. * line at 0. So the resulting positions may be beyond width.
  287. *
  288. * @return float The resulting width of the range of slots justified.
  289. * @param pSeg Pointer to the segment
  290. * @param pStart Pointer to the start of the line linked list (including skipped characters)
  291. * @param pFont Font to use for positioning
  292. * @param width Width in pixels in which to fit the line. If < 0. don't adjust natural width, just run justification passes
  293. * to handle line end contextuals, if there are any.
  294. * @param flags Indicates line ending types. Default is linked list is a full line
  295. * @param pFirst If not NULL, the first slot in the list to be considered part of the line (so can skip)
  296. * @param pLast If not NULL, the last slot to process in the line (allow say trailing whitespace to be skipped)
  297. */
  298. GR2_API float gr_seg_justify(gr_segment* pSeg/*not NULL*/, const gr_slot* pStart/*not NULL*/, const gr_font *pFont, double width, enum gr_justFlags flags, const gr_slot* pFirst, const gr_slot* pLast);
  299. /** Returns the next slot along in the segment.
  300. *
  301. * Slots are held in a linked list. This returns the next in the linked list. The slot
  302. * may or may not be attached to another slot. Returns NULL at the end of the segment.
  303. */
  304. GR2_API const gr_slot* gr_slot_next_in_segment(const gr_slot* p);
  305. /** Returns the previous slot along in the segment.
  306. *
  307. * Slots are held in a doubly linked list. This returns the previos slot in the linked
  308. * list. This slot may or may not be attached to it. Returns NULL at the start of the
  309. * segment.
  310. */
  311. GR2_API const gr_slot* gr_slot_prev_in_segment(const gr_slot* p);
  312. /** Returns the attachment parent slot of this slot.
  313. *
  314. * Attached slots form a tree. This returns the parent of this slot in that tree. A
  315. * base glyph which is not attached to another glyph, always returns NULL.
  316. */
  317. GR2_API const gr_slot* gr_slot_attached_to(const gr_slot* p);
  318. /** Returns the first slot attached to this slot.
  319. *
  320. * Attached slots form a singly linked list from the parent. This returns the first
  321. * slot in that list. Note that this is a reference to another slot that is also in
  322. * the main segment doubly linked list.
  323. *
  324. * if gr_slot_first_attachment(p) != NULL then gr_slot_attached_to(gr_slot_first_attachment(p)) == p.
  325. */
  326. GR2_API const gr_slot* gr_slot_first_attachment(const gr_slot* p);
  327. /** Returns the next slot attached to our attachment parent.
  328. *
  329. * This returns the next slot in the singly linked list of slots attached to this
  330. * slot's parent. If there are no more such slots, NULL is returned. If there is
  331. * no parent, i.e. the passed slot is a cluster base, then the next cluster base
  332. * in graphical order (ltr, even for rtl text) is returned.
  333. *
  334. * if gr_slot_next_sibling_attachment(p) != NULL then gr_slot_attached_to(gr_slot_next_sibling_attachment(p)) == gr_slot_attached_to(p).
  335. */
  336. GR2_API const gr_slot* gr_slot_next_sibling_attachment(const gr_slot* p);
  337. /** Returns glyph id of the slot
  338. *
  339. * Each slot has a glyphid which is rendered at the position given by the slot. This
  340. * glyphid is the real glyph to be rendered and never a pseudo glyph.
  341. */
  342. GR2_API unsigned short gr_slot_gid(const gr_slot* p);
  343. /** Returns X offset of glyph from start of segment **/
  344. GR2_API float gr_slot_origin_X(const gr_slot* p);
  345. /** Returns Y offset of glyph from start of segment **/
  346. GR2_API float gr_slot_origin_Y(const gr_slot* p);
  347. /** Returns the glyph advance for this glyph as adjusted for kerning
  348. *
  349. * @param p Slot to give results for
  350. * @param face gr_face of the glyphs. May be NULL if unhinted advances used
  351. * @param font gr_font to scale for pixel results. If NULL returns design
  352. * units advance. If not NULL then returns pixel advance based
  353. * on hinted or scaled glyph advances in the font. face must be
  354. * passed for hinted advances to be used.
  355. */
  356. GR2_API float gr_slot_advance_X(const gr_slot* p, const gr_face* face, const gr_font *font);
  357. /** Returns the vertical advance for the glyph in the slot adjusted for kerning
  358. *
  359. * Returns design units unless font is not NULL in which case the pixel value
  360. * is returned scaled for the given font
  361. */
  362. GR2_API float gr_slot_advance_Y(const gr_slot* p, const gr_face* face, const gr_font *font);
  363. /** Returns the gr_char_info index before us
  364. *
  365. * Returns the index of the gr_char_info that a cursor before this slot, would put
  366. * an underlying cursor before. This may also be interpretted as each slot holding
  367. * a set of char_infos that it is associated with and this function returning the
  368. * index of the char_info with lowest index, from this set.
  369. */
  370. GR2_API int gr_slot_before(const gr_slot* p/*not NULL*/);
  371. /** Returns the gr_char_info index after us
  372. *
  373. * Returns the index of the gr_char_info that a cursor after this slot would put an
  374. * underlying cursor after. This may also be interpretted as each slot holding a set
  375. * of char_infos that it is associated with and this function returning the index of
  376. * the char_info with the highest index, from this set.
  377. */
  378. GR2_API int gr_slot_after(const gr_slot* p/*not NULL*/);
  379. /** Returns the index of this slot in the segment
  380. *
  381. * Returns the index given to this slot during final positioning. This corresponds
  382. * to the value returned br gr_cinfo_before() and gr_cinfo_after()
  383. */
  384. GR2_API unsigned int gr_slot_index(const gr_slot* p/*not NULL*/);
  385. /** Return a slot attribute value
  386. *
  387. * Given a slot and an attribute along with a possible subattribute, return the
  388. * corresponding value in the slot. See enum gr_attrCode for details of each attribute.
  389. */
  390. GR2_API int gr_slot_attr(const gr_slot* p/*not NULL*/, const gr_segment* pSeg/*not NULL*/, enum gr_attrCode index, gr_uint8 subindex); //tbd - do we need to expose this?
  391. /** Returns whether text may be inserted before this glyph.
  392. *
  393. * This indicates whether a cursor can be put before this slot. It applies to
  394. * base glyphs that have no parent as well as attached glyphs that have the
  395. * .insert attribute explicitly set to true. This is the primary mechanism
  396. * for identifying contiguous sequences of base plus diacritics.
  397. */
  398. GR2_API int gr_slot_can_insert_before(const gr_slot* p);
  399. /** Returns the original gr_char_info index this slot refers to.
  400. *
  401. * Each Slot has a gr_char_info that it originates from. This is that gr_char_info.
  402. * The index is passed to gr_seg_cinfo(). This information is useful for testing.
  403. */
  404. GR2_API int gr_slot_original(const gr_slot* p/*not NULL*/);
  405. /** Breaks a segment into lines.
  406. *
  407. * Breaks the slot linked list at the given point in the linked list. It is up
  408. * to the application to keep track of the first slot on each line.
  409. */
  410. GR2_API void gr_slot_linebreak_before(gr_slot *p/*not NULL*/);
  411. #ifdef __cplusplus
  412. }
  413. #endif