pdf-metrics.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright (C) 2004-2021 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #include "mupdf/fitz.h"
  23. #include "mupdf/pdf.h"
  24. #include <stdlib.h>
  25. void
  26. pdf_set_font_wmode(fz_context *ctx, pdf_font_desc *font, int wmode)
  27. {
  28. font->wmode = wmode;
  29. }
  30. void
  31. pdf_set_default_hmtx(fz_context *ctx, pdf_font_desc *font, int w)
  32. {
  33. font->dhmtx.w = w;
  34. }
  35. void
  36. pdf_set_default_vmtx(fz_context *ctx, pdf_font_desc *font, int y, int w)
  37. {
  38. font->dvmtx.y = y;
  39. font->dvmtx.w = w;
  40. }
  41. void
  42. pdf_add_hmtx(fz_context *ctx, pdf_font_desc *font, int lo, int hi, int w)
  43. {
  44. if (font->hmtx_len + 1 >= font->hmtx_cap)
  45. {
  46. int new_cap = font->hmtx_cap + 16;
  47. font->hmtx = fz_realloc_array(ctx, font->hmtx, new_cap, pdf_hmtx);
  48. font->hmtx_cap = new_cap;
  49. }
  50. font->hmtx[font->hmtx_len].lo = lo;
  51. font->hmtx[font->hmtx_len].hi = hi;
  52. font->hmtx[font->hmtx_len].w = w;
  53. font->hmtx_len++;
  54. }
  55. void
  56. pdf_add_vmtx(fz_context *ctx, pdf_font_desc *font, int lo, int hi, int x, int y, int w)
  57. {
  58. if (font->vmtx_len + 1 >= font->vmtx_cap)
  59. {
  60. int new_cap = font->vmtx_cap + 16;
  61. font->vmtx = fz_realloc_array(ctx, font->vmtx, new_cap, pdf_vmtx);
  62. font->vmtx_cap = new_cap;
  63. }
  64. font->vmtx[font->vmtx_len].lo = lo;
  65. font->vmtx[font->vmtx_len].hi = hi;
  66. font->vmtx[font->vmtx_len].x = x;
  67. font->vmtx[font->vmtx_len].y = y;
  68. font->vmtx[font->vmtx_len].w = w;
  69. font->vmtx_len++;
  70. }
  71. static int cmph(const void *a0, const void *b0)
  72. {
  73. pdf_hmtx *a = (pdf_hmtx*)a0;
  74. pdf_hmtx *b = (pdf_hmtx*)b0;
  75. return a->lo - b->lo;
  76. }
  77. static int cmpv(const void *a0, const void *b0)
  78. {
  79. pdf_vmtx *a = (pdf_vmtx*)a0;
  80. pdf_vmtx *b = (pdf_vmtx*)b0;
  81. return a->lo - b->lo;
  82. }
  83. void
  84. pdf_end_hmtx(fz_context *ctx, pdf_font_desc *font)
  85. {
  86. if (!font->hmtx)
  87. return;
  88. qsort(font->hmtx, font->hmtx_len, sizeof(pdf_hmtx), cmph);
  89. font->size += font->hmtx_cap * sizeof(pdf_hmtx);
  90. }
  91. void
  92. pdf_end_vmtx(fz_context *ctx, pdf_font_desc *font)
  93. {
  94. if (!font->vmtx)
  95. return;
  96. qsort(font->vmtx, font->vmtx_len, sizeof(pdf_vmtx), cmpv);
  97. font->size += font->vmtx_cap * sizeof(pdf_vmtx);
  98. }
  99. pdf_hmtx
  100. pdf_lookup_hmtx(fz_context *ctx, pdf_font_desc *font, int cid)
  101. {
  102. int l = 0;
  103. int r = font->hmtx_len - 1;
  104. int m;
  105. if (!font->hmtx)
  106. goto notfound;
  107. while (l <= r)
  108. {
  109. m = (l + r) >> 1;
  110. if (cid < font->hmtx[m].lo)
  111. r = m - 1;
  112. else if (cid > font->hmtx[m].hi)
  113. l = m + 1;
  114. else
  115. return font->hmtx[m];
  116. }
  117. notfound:
  118. return font->dhmtx;
  119. }
  120. pdf_vmtx
  121. pdf_lookup_vmtx(fz_context *ctx, pdf_font_desc *font, int cid)
  122. {
  123. pdf_hmtx h;
  124. pdf_vmtx v;
  125. int l = 0;
  126. int r = font->vmtx_len - 1;
  127. int m;
  128. if (!font->vmtx)
  129. goto notfound;
  130. while (l <= r)
  131. {
  132. m = (l + r) >> 1;
  133. if (cid < font->vmtx[m].lo)
  134. r = m - 1;
  135. else if (cid > font->vmtx[m].hi)
  136. l = m + 1;
  137. else
  138. return font->vmtx[m];
  139. }
  140. notfound:
  141. h = pdf_lookup_hmtx(ctx, font, cid);
  142. v = font->dvmtx;
  143. v.x = h.w / 2;
  144. return v;
  145. }