/nx-3.5.0/nx-X11/lib/font/bitmap/bitmaputil.c

# · C · 232 lines · 187 code · 15 blank · 30 comment · 32 complexity · d01ba3c7cd03679b651e8d492d81a690 MD5 · raw file

  1. /* $Xorg: bitmaputil.c,v 1.5 2001/02/09 02:04:02 xorgcvs Exp $ */
  2. /*
  3. Copyright 1990, 1994, 1998 The Open Group
  4. Permission to use, copy, modify, distribute, and sell this software and its
  5. documentation for any purpose is hereby granted without fee, provided that
  6. the above copyright notice appear in all copies and that both that
  7. copyright notice and this permission notice appear in supporting
  8. documentation.
  9. The above copyright notice and this permission notice shall be included
  10. in all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  12. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  13. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  14. IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
  15. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  16. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  17. OTHER DEALINGS IN THE SOFTWARE.
  18. Except as contained in this notice, the name of The Open Group shall
  19. not be used in advertising or otherwise to promote the sale, use or
  20. other dealings in this Software without prior written authorization
  21. from The Open Group.
  22. */
  23. /* $XFree86: xc/lib/font/bitmap/bitmaputil.c,v 1.10 2002/09/24 20:52:48 tsi Exp $ */
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. #include <X11/fonts/fntfilst.h>
  28. #include <X11/fonts/bitmap.h>
  29. #include <X11/fonts/bdfint.h>
  30. #ifndef MAXSHORT
  31. #define MAXSHORT 32767
  32. #endif
  33. #ifndef MINSHORT
  34. #define MINSHORT -32768
  35. #endif
  36. static xCharInfo initMinMetrics = {
  37. MAXSHORT, MAXSHORT, MAXSHORT, MAXSHORT, MAXSHORT, 0xFFFF};
  38. static xCharInfo initMaxMetrics = {
  39. MINSHORT, MINSHORT, MINSHORT, MINSHORT, MINSHORT, 0x0000};
  40. #define MINMAX(field,ci) \
  41. if (minbounds->field > (ci)->field) \
  42. minbounds->field = (ci)->field; \
  43. if (maxbounds->field < (ci)->field) \
  44. maxbounds->field = (ci)->field;
  45. #define COMPUTE_MINMAX(ci) \
  46. if ((ci)->ascent || (ci)->descent || \
  47. (ci)->leftSideBearing || (ci)->rightSideBearing || \
  48. (ci)->characterWidth) \
  49. { \
  50. MINMAX(ascent, (ci)); \
  51. MINMAX(descent, (ci)); \
  52. MINMAX(leftSideBearing, (ci)); \
  53. MINMAX(rightSideBearing, (ci)); \
  54. MINMAX(characterWidth, (ci)); \
  55. }
  56. void
  57. bitmapComputeFontBounds(FontPtr pFont)
  58. {
  59. BitmapFontPtr bitmapFont = (BitmapFontPtr) pFont->fontPrivate;
  60. int nchars;
  61. int r,
  62. c;
  63. CharInfoPtr ci;
  64. int maxOverlap;
  65. int overlap;
  66. xCharInfo *minbounds,
  67. *maxbounds;
  68. int i;
  69. int numneg = 0, numpos = 0;
  70. if (bitmapFont->bitmapExtra) {
  71. minbounds = &bitmapFont->bitmapExtra->info.minbounds;
  72. maxbounds = &bitmapFont->bitmapExtra->info.maxbounds;
  73. } else {
  74. minbounds = &pFont->info.minbounds;
  75. maxbounds = &pFont->info.maxbounds;
  76. }
  77. *minbounds = initMinMetrics;
  78. *maxbounds = initMaxMetrics;
  79. maxOverlap = MINSHORT;
  80. nchars = bitmapFont->num_chars;
  81. for (i = 0, ci = bitmapFont->metrics; i < nchars; i++, ci++) {
  82. COMPUTE_MINMAX(&ci->metrics);
  83. if (ci->metrics.characterWidth < 0)
  84. numneg++;
  85. else
  86. numpos++;
  87. minbounds->attributes &= ci->metrics.attributes;
  88. maxbounds->attributes |= ci->metrics.attributes;
  89. overlap = ci->metrics.rightSideBearing - ci->metrics.characterWidth;
  90. if (maxOverlap < overlap)
  91. maxOverlap = overlap;
  92. }
  93. if (bitmapFont->bitmapExtra) {
  94. if (numneg > numpos)
  95. bitmapFont->bitmapExtra->info.drawDirection = RightToLeft;
  96. else
  97. bitmapFont->bitmapExtra->info.drawDirection = LeftToRight;
  98. bitmapFont->bitmapExtra->info.maxOverlap = maxOverlap;
  99. minbounds = &pFont->info.minbounds;
  100. maxbounds = &pFont->info.maxbounds;
  101. *minbounds = initMinMetrics;
  102. *maxbounds = initMaxMetrics;
  103. i = 0;
  104. maxOverlap = MINSHORT;
  105. for (r = pFont->info.firstRow; r <= pFont->info.lastRow; r++) {
  106. for (c = pFont->info.firstCol; c <= pFont->info.lastCol; c++) {
  107. ci = ACCESSENCODING(bitmapFont->encoding, i);
  108. if (ci) {
  109. COMPUTE_MINMAX(&ci->metrics);
  110. if (ci->metrics.characterWidth < 0)
  111. numneg++;
  112. else
  113. numpos++;
  114. minbounds->attributes &= ci->metrics.attributes;
  115. maxbounds->attributes |= ci->metrics.attributes;
  116. overlap = ci->metrics.rightSideBearing -
  117. ci->metrics.characterWidth;
  118. if (maxOverlap < overlap)
  119. maxOverlap = overlap;
  120. }
  121. i++;
  122. }
  123. }
  124. }
  125. if (numneg > numpos)
  126. pFont->info.drawDirection = RightToLeft;
  127. else
  128. pFont->info.drawDirection = LeftToRight;
  129. pFont->info.maxOverlap = maxOverlap;
  130. }
  131. void
  132. bitmapComputeFontInkBounds(FontPtr pFont)
  133. {
  134. BitmapFontPtr bitmapFont = (BitmapFontPtr) pFont->fontPrivate;
  135. int nchars;
  136. int r,
  137. c;
  138. CharInfoPtr cit;
  139. xCharInfo *ci;
  140. int offset;
  141. xCharInfo *minbounds,
  142. *maxbounds;
  143. int i;
  144. if (!bitmapFont->ink_metrics) {
  145. if (bitmapFont->bitmapExtra) {
  146. bitmapFont->bitmapExtra->info.ink_minbounds = bitmapFont->bitmapExtra->info.minbounds;
  147. bitmapFont->bitmapExtra->info.ink_maxbounds = bitmapFont->bitmapExtra->info.maxbounds;
  148. }
  149. pFont->info.ink_minbounds = pFont->info.minbounds;
  150. pFont->info.ink_maxbounds = pFont->info.maxbounds;
  151. } else {
  152. if (bitmapFont->bitmapExtra) {
  153. minbounds = &bitmapFont->bitmapExtra->info.ink_minbounds;
  154. maxbounds = &bitmapFont->bitmapExtra->info.ink_maxbounds;
  155. } else {
  156. minbounds = &pFont->info.ink_minbounds;
  157. maxbounds = &pFont->info.ink_maxbounds;
  158. }
  159. *minbounds = initMinMetrics;
  160. *maxbounds = initMaxMetrics;
  161. nchars = bitmapFont->num_chars;
  162. for (i = 0, ci = bitmapFont->ink_metrics; i < nchars; i++, ci++) {
  163. COMPUTE_MINMAX(ci);
  164. minbounds->attributes &= ci->attributes;
  165. maxbounds->attributes |= ci->attributes;
  166. }
  167. if (bitmapFont->bitmapExtra) {
  168. minbounds = &pFont->info.ink_minbounds;
  169. maxbounds = &pFont->info.ink_maxbounds;
  170. *minbounds = initMinMetrics;
  171. *maxbounds = initMaxMetrics;
  172. i=0;
  173. for (r = pFont->info.firstRow; r <= pFont->info.lastRow; r++) {
  174. for (c = pFont->info.firstCol; c <= pFont->info.lastCol; c++) {
  175. cit = ACCESSENCODING(bitmapFont->encoding, i);
  176. if (cit) {
  177. offset = cit - bitmapFont->metrics;
  178. ci = &bitmapFont->ink_metrics[offset];
  179. COMPUTE_MINMAX(ci);
  180. minbounds->attributes &= ci->attributes;
  181. maxbounds->attributes |= ci->attributes;
  182. }
  183. i++;
  184. }
  185. }
  186. }
  187. }
  188. }
  189. Bool
  190. bitmapAddInkMetrics(FontPtr pFont)
  191. {
  192. BitmapFontPtr bitmapFont;
  193. int i;
  194. bitmapFont = (BitmapFontPtr) pFont->fontPrivate;
  195. bitmapFont->ink_metrics = (xCharInfo *) xalloc(bitmapFont->num_chars * sizeof(xCharInfo));
  196. if (!bitmapFont->ink_metrics) {
  197. fprintf(stderr, "Error: Couldn't allocate ink_metrics (%d*%ld)\n",
  198. bitmapFont->num_chars, (unsigned long)sizeof(xCharInfo));
  199. return FALSE;
  200. }
  201. for (i = 0; i < bitmapFont->num_chars; i++)
  202. FontCharInkMetrics(pFont, &bitmapFont->metrics[i], &bitmapFont->ink_metrics[i]);
  203. pFont->info.inkMetrics = TRUE;
  204. return TRUE;
  205. }
  206. /* ARGSUSED */
  207. int
  208. bitmapComputeWeight(FontPtr pFont)
  209. {
  210. return 10;
  211. }