Segment.h 19 KB

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