PixelScaleLayoutExtensions.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. //MIT, 2016-present, WinterDev
  2. using System;
  3. using Typography.OpenFont;
  4. namespace Typography.TextLayout
  5. {
  6. /// <summary>
  7. /// scaled glyph plan to specfic font size.
  8. /// offsetX,offsetY,advanceX are adjusted to fit with specific font size
  9. /// </summary>
  10. public readonly struct PxScaledGlyphPlan
  11. {
  12. public readonly ushort input_cp_offset;
  13. public readonly ushort glyphIndex;
  14. public PxScaledGlyphPlan(ushort input_cp_offset, ushort glyphIndex, float advanceW, float offsetX, float offsetY)
  15. {
  16. this.input_cp_offset = input_cp_offset;
  17. this.glyphIndex = glyphIndex;
  18. this.OffsetX = offsetX;
  19. this.OffsetY = offsetY;
  20. this.AdvanceX = advanceW;
  21. }
  22. public readonly float AdvanceX;
  23. /// <summary>
  24. /// x offset from current position
  25. /// </summary>
  26. public readonly float OffsetX;
  27. /// <summary>
  28. /// y offset from current position
  29. /// </summary>
  30. public readonly float OffsetY;
  31. public bool AdvanceMoveForward => this.AdvanceX > 0;
  32. #if DEBUG
  33. public override string ToString()
  34. {
  35. return " adv:" + AdvanceX;
  36. }
  37. #endif
  38. }
  39. /// <summary>
  40. /// scaled glyph plan
  41. /// </summary>
  42. public struct GlyphPlanSequencePixelScaleLayout
  43. {
  44. GlyphPlanSequence _seq;
  45. float _pxscale;
  46. float _accW;
  47. int _index;
  48. int _end;
  49. float _exactX;
  50. float _exactY;
  51. ushort _currentGlyphIndex;
  52. public GlyphPlanSequencePixelScaleLayout(GlyphPlanSequence glyphPlans, float pxscale)
  53. {
  54. _seq = glyphPlans;
  55. _pxscale = pxscale;
  56. _accW = 0;
  57. _index = glyphPlans.startAt;
  58. _end = glyphPlans.startAt + glyphPlans.len;
  59. _exactX = _exactY = 0;
  60. _currentGlyphIndex = 0;
  61. }
  62. //
  63. public int CurrentIndex => _index;
  64. //
  65. public PxScaledGlyphPlan GlyphPlan
  66. {
  67. get
  68. {
  69. UnscaledGlyphPlan unscale = _seq[_index];
  70. float scaled_advW = unscale.AdvanceX * _pxscale;
  71. return new PxScaledGlyphPlan(
  72. unscale.input_cp_offset,
  73. unscale.glyphIndex,
  74. scaled_advW,
  75. unscale.OffsetX * _pxscale,
  76. unscale.OffsetY * _pxscale);
  77. }
  78. }
  79. public float AccumWidth => _accW;
  80. public float ExactX => _exactX;
  81. public float ExactY => _exactY;
  82. public ushort CurrentGlyphIndex => _currentGlyphIndex;
  83. public bool Read()
  84. {
  85. if (_index >= _end)
  86. {
  87. return false;
  88. }
  89. //read current
  90. UnscaledGlyphPlan unscale = _seq[_index];
  91. float scaled_advW = unscale.AdvanceX * _pxscale;
  92. _exactX = _accW + (unscale.AdvanceX + unscale.OffsetX) * _pxscale;
  93. _exactY = unscale.OffsetY * _pxscale;
  94. _accW += scaled_advW;
  95. _currentGlyphIndex = unscale.glyphIndex;
  96. _index++;
  97. return true;
  98. }
  99. }
  100. /// <summary>
  101. /// scaled glyph plan + snap-to-grid
  102. /// </summary>
  103. public struct GlyphPlanSequenceSnapPixelScaleLayout
  104. {
  105. GlyphPlanSequence _seq;
  106. float _pxscale;
  107. int _accW;
  108. int _index;
  109. int _end;
  110. int _exactX;
  111. int _exactY;
  112. bool _rightToLeft;
  113. ushort _currentGlyphIndex;
  114. public GlyphPlanSequenceSnapPixelScaleLayout(GlyphPlanSequence glyphPlans, int start, int len, float pxscale)
  115. {
  116. _seq = glyphPlans;
  117. _pxscale = pxscale;
  118. _accW = 0;
  119. _index = start;
  120. _end = start + len;
  121. _exactX = _exactY = 0;
  122. _currentGlyphIndex = 0;
  123. _limitW = 0;
  124. if (_rightToLeft = glyphPlans.IsRightToLeft)
  125. {
  126. _index = _end - 1;
  127. }
  128. }
  129. public ushort CurrentGlyphIndex => _currentGlyphIndex;
  130. public int CurrentIndex => _index;
  131. //
  132. public bool Read()
  133. {
  134. if (_rightToLeft)
  135. {
  136. if (_index < 0)
  137. {
  138. return false;
  139. }
  140. //read current
  141. UnscaledGlyphPlan unscale = _seq[_index];
  142. short scaled_advW = (short)Math.Round(unscale.AdvanceX * _pxscale);
  143. short scaled_offsetX = (short)Math.Round(unscale.OffsetX * _pxscale);
  144. short scaled_offsetY = (short)Math.Round(unscale.OffsetY * _pxscale);
  145. _exactX = _accW + scaled_offsetX;
  146. _exactY = scaled_offsetY;
  147. _accW += scaled_advW;
  148. _currentGlyphIndex = unscale.glyphIndex;
  149. _index--;
  150. return true;
  151. }
  152. else
  153. {
  154. if (_index >= _end)
  155. {
  156. return false;
  157. }
  158. //read current
  159. UnscaledGlyphPlan unscale = _seq[_index];
  160. short scaled_advW = (short)Math.Round(unscale.AdvanceX * _pxscale);
  161. short scaled_offsetX = (short)Math.Round(unscale.OffsetX * _pxscale);
  162. short scaled_offsetY = (short)Math.Round(unscale.OffsetY * _pxscale);
  163. _exactX = _accW + scaled_offsetX;
  164. _exactY = scaled_offsetY;
  165. _accW += scaled_advW;
  166. _currentGlyphIndex = unscale.glyphIndex;
  167. _index++;
  168. return true;
  169. }
  170. }
  171. public void ReadToEnd()
  172. {
  173. while (Read()) ;
  174. }
  175. public int AccumWidth => _accW;
  176. public int ExactX => _exactX;
  177. public int ExactY => _exactY;
  178. int _limitW;
  179. public void ReadWidthLimitWidth(int limitWidth)
  180. {
  181. _limitW = limitWidth;
  182. while (ReadWidthLimitWidth()) ;
  183. }
  184. bool ReadWidthLimitWidth()
  185. {
  186. if (_rightToLeft)
  187. {
  188. if (_index < 0)
  189. {
  190. return false;
  191. }
  192. //read current
  193. UnscaledGlyphPlan unscale = _seq[_index];
  194. short scaled_advW = (short)Math.Round(unscale.AdvanceX * _pxscale);
  195. short scaled_offsetX = (short)Math.Round(unscale.OffsetX * _pxscale);
  196. short scaled_offsetY = (short)Math.Round(unscale.OffsetY * _pxscale);
  197. if (_accW + scaled_advW > _limitW)
  198. {
  199. //stop
  200. return false;
  201. }
  202. _exactX = _accW + scaled_offsetX;
  203. _exactY = scaled_offsetY;
  204. _accW += scaled_advW;
  205. _currentGlyphIndex = unscale.glyphIndex;
  206. _index--;
  207. return true;
  208. }
  209. else
  210. {
  211. if (_index >= _end)
  212. {
  213. return false;
  214. }
  215. //read current
  216. UnscaledGlyphPlan unscale = _seq[_index];
  217. short scaled_advW = (short)Math.Round(unscale.AdvanceX * _pxscale);
  218. short scaled_offsetX = (short)Math.Round(unscale.OffsetX * _pxscale);
  219. short scaled_offsetY = (short)Math.Round(unscale.OffsetY * _pxscale);
  220. if (_accW + scaled_advW > _limitW)
  221. {
  222. //stop
  223. return false;
  224. }
  225. _exactX = _accW + scaled_offsetX;
  226. _exactY = scaled_offsetY;
  227. _accW += scaled_advW;
  228. _currentGlyphIndex = unscale.glyphIndex;
  229. _index++;
  230. return true;
  231. }
  232. }
  233. }
  234. public static class PixelScaleLayoutExtensions
  235. {
  236. #if DEBUG
  237. public static float dbugSnapToFitInteger(float value)
  238. {
  239. int floor_value = (int)value;
  240. return (value - floor_value >= (1f / 2f)) ? floor_value + 1 : floor_value;
  241. }
  242. public static float dbugSnapHalf(float value)
  243. {
  244. int floor_value = (int)value;
  245. //round to int 0, 0.5,1.0
  246. return (value - floor_value >= (2f / 3f)) ? floor_value + 1 : //else->
  247. (value - floor_value >= (1f / 3f)) ? floor_value + 0.5f : floor_value;
  248. }
  249. static int dbugSnapUpper(float value)
  250. {
  251. int floor_value = (int)value;
  252. return floor_value + 1;
  253. }
  254. #endif
  255. static float MeasureGlyphPlans(this GlyphLayout glyphLayout,
  256. float pxscale,
  257. bool snapToGrid)
  258. {
  259. //user can implement this with some 'PixelScaleEngine'
  260. IGlyphPositions glyphPositions = glyphLayout.ResultUnscaledGlyphPositions;
  261. float accumW = 0; //acummulate Width
  262. if (snapToGrid)
  263. {
  264. int finalGlyphCount = glyphPositions.Count;
  265. for (int i = 0; i < finalGlyphCount; ++i)
  266. {
  267. //all from pen-pos
  268. ushort glyphIndex = glyphPositions.GetGlyph(i,
  269. out ushort input_offset,
  270. out short offsetX,
  271. out short offsetY,
  272. out short advW);
  273. accumW += (short)Math.Round(advW * pxscale);
  274. }
  275. }
  276. else
  277. {
  278. //not snap to grid
  279. //scaled but not snap to grid
  280. int finalGlyphCount = glyphPositions.Count;
  281. for (int i = 0; i < finalGlyphCount; ++i)
  282. {
  283. //all from pen-pos
  284. ushort glyphIndex = glyphPositions.GetGlyph(i,
  285. out ushort input_offset,
  286. out short offsetX,
  287. out short offsetY,
  288. out short advW);
  289. accumW += advW * pxscale;
  290. }
  291. }
  292. return accumW;
  293. }
  294. static float MeasureGlyphPlanWithLimitWidth(this GlyphLayout glyphLayout,
  295. float pxscale,
  296. float limitWidth,
  297. bool snapToGrid,
  298. out int stopAtGlyphIndex)
  299. {
  300. //user can implement this with some 'PixelScaleEngine'
  301. IGlyphPositions glyphPositions = glyphLayout.ResultUnscaledGlyphPositions;
  302. float accumW = 0; //acummulate Width
  303. stopAtGlyphIndex = 0;
  304. if (snapToGrid)
  305. {
  306. int finalGlyphCount = glyphPositions.Count;
  307. for (int i = 0; i < finalGlyphCount; ++i)
  308. {
  309. //all from pen-pos
  310. ushort glyphIndex = glyphPositions.GetGlyph(i,
  311. out ushort input_offset,
  312. out short offsetX,
  313. out short offsetY,
  314. out short advW);
  315. stopAtGlyphIndex = i; //***
  316. //
  317. short w = (short)Math.Round(advW * pxscale);
  318. if (accumW + w > limitWidth)
  319. {
  320. //stop
  321. break;
  322. }
  323. else
  324. {
  325. accumW += w;
  326. }
  327. }
  328. }
  329. else
  330. {
  331. //not snap to grid
  332. //scaled but not snap to grid
  333. int finalGlyphCount = glyphPositions.Count;
  334. for (int i = 0; i < finalGlyphCount; ++i)
  335. {
  336. //all from pen-pos
  337. ushort glyphIndex = glyphPositions.GetGlyph(i,
  338. out ushort input_offset,
  339. out short offsetX,
  340. out short offsetY,
  341. out short advW);
  342. stopAtGlyphIndex = i; //***
  343. float w = advW * pxscale;
  344. if (accumW + w > limitWidth)
  345. {
  346. //stop
  347. break;
  348. }
  349. else
  350. {
  351. accumW += w;
  352. }
  353. }
  354. }
  355. return accumW;
  356. ////measure string
  357. //if (str.Length < 1)
  358. //{
  359. // charFitWidth = 0;
  360. //}
  361. //_reusableMeasureBoxList.Clear(); //reset
  362. //float pxscale = _currentTypeface.CalculateScaleToPixelFromPointSize(_fontSizeInPts);
  363. ////NOET:at this moment, simple operation
  364. ////may not be simple...
  365. ////-------------------
  366. ////input string may contain more than 1 script lang
  367. ////user can parse it by other parser
  368. ////but in this code, we use our Typography' parser
  369. ////-------------------
  370. ////user must setup the CustomBreakerBuilder before use
  371. //int cur_startAt = startAt;
  372. //float accumW = 0;
  373. //float acc_x = 0;//accum_x
  374. //float acc_y = 0;//accum_y
  375. //float g_x = 0;
  376. //float g_y = 0;
  377. //float x = 0;
  378. //float y = 0;
  379. //foreach (Typography.TextLayout.BreakSpan breakSpan in BreakToLineSegments(str, startAt, len))
  380. //{
  381. // //measure string at specific px scale
  382. // _glyphLayout.Layout(str, breakSpan.startAt, breakSpan.len);
  383. // //
  384. // _reusableGlyphPlanList.Clear();
  385. // _glyphLayout.GenerateUnscaledGlyphPlans(_reusableGlyphPlanList);
  386. // //measure ...
  387. // //measure each glyph
  388. // //limit at specific width
  389. // int glyphCount = _reusableGlyphPlanList.Count;
  390. // for (int i = 0; i < glyphCount; ++i)
  391. // {
  392. // UnscaledGlyphPlan glyphPlan = _reusableGlyphPlanList[i];
  393. // float ngx = acc_x + (float)Math.Round(glyphPlan.OffsetX * pxscale);
  394. // float ngy = acc_y + (float)Math.Round(glyphPlan.OffsetY * pxscale);
  395. // //NOTE:
  396. // // -glyphData.TextureXOffset => restore to original pos
  397. // // -glyphData.TextureYOffset => restore to original pos
  398. // //--------------------------
  399. // g_x = (float)(x + (ngx)); //ideal x
  400. // g_y = (float)(y + (ngy));
  401. // float g_w = (float)Math.Round(glyphPlan.AdvanceX * pxscale);
  402. // acc_x += g_w;
  403. // //g_x = (float)Math.Round(g_x);
  404. // g_y = (float)Math.Floor(g_y);
  405. // float right = g_x + g_w;
  406. // if (right >= accumW)
  407. // {
  408. // //stop here at this glyph
  409. // charFit = i - 1;
  410. // //TODO: review this
  411. // charFitWidth = (int)System.Math.Round(accumW);
  412. // return;
  413. // }
  414. // else
  415. // {
  416. // accumW = right;
  417. // }
  418. // }
  419. //}
  420. //charFit = 0;
  421. //charFitWidth = 0;
  422. }
  423. //static void ConcatMeasureBox(ref float accumW, ref float accumH, ref MeasuredStringBox measureBox)
  424. //{
  425. // accumW += measureBox.width;
  426. // float h = measureBox.CalculateLineHeight();
  427. // if (h > accumH)
  428. // {
  429. // accumH = h;
  430. // }
  431. //}
  432. public static MeasuredStringBox LayoutAndMeasureString(
  433. this GlyphLayout glyphLayout,
  434. char[] textBuffer,
  435. int startAt,
  436. int len,
  437. float fontSizeInPoints,
  438. float limitW = -1,//-1 unlimit scaled width (px)
  439. bool snapToGrid = true)
  440. {
  441. //1. unscale layout, in design unit
  442. glyphLayout.Layout(textBuffer, startAt, len);
  443. //2. scale to specific font size
  444. Typeface typeface = glyphLayout.Typeface;
  445. float pxscale = typeface.CalculateScaleToPixelFromPointSize(fontSizeInPoints);
  446. //....
  447. float scaled_accumX = 0;
  448. if (limitW < 0)
  449. {
  450. //no limit
  451. scaled_accumX = MeasureGlyphPlans(
  452. glyphLayout,
  453. pxscale,
  454. snapToGrid);
  455. return new MeasuredStringBox(
  456. scaled_accumX,
  457. typeface.Ascender,
  458. typeface.Descender,
  459. typeface.LineGap,
  460. typeface.ClipedAscender,
  461. typeface.ClipedDescender,
  462. pxscale);
  463. }
  464. else if (limitW > 0)
  465. {
  466. scaled_accumX = MeasureGlyphPlanWithLimitWidth(
  467. glyphLayout,
  468. pxscale,
  469. limitW,
  470. snapToGrid,
  471. out int stopAtChar);
  472. var mstrbox = new MeasuredStringBox(
  473. scaled_accumX,
  474. typeface.Ascender,
  475. typeface.Descender,
  476. typeface.LineGap,
  477. typeface.ClipedAscender,
  478. typeface.ClipedDescender,
  479. pxscale);
  480. mstrbox.StopAt = (ushort)stopAtChar;
  481. return mstrbox;
  482. }
  483. else
  484. {
  485. return new MeasuredStringBox(
  486. 0,
  487. typeface.Ascender,
  488. typeface.Descender,
  489. typeface.LineGap,
  490. typeface.ClipedAscender,
  491. typeface.ClipedDescender,
  492. pxscale);
  493. }
  494. }
  495. }
  496. }