PageRenderTime 55ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/nx-3.5.0/nx-X11/lib/font/Speedo/spglyph.c

#
C | 399 lines | 269 code | 48 blank | 82 comment | 47 complexity | f8aa0476ca187942cd0143d492306498 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, LGPL-2.0
  1. /* $Xorg: spglyph.c,v 1.4 2001/02/09 02:04:00 xorgcvs Exp $ */
  2. /*
  3. * Copyright 1990, 1991 Network Computing Devices;
  4. * Portions Copyright 1987 by Digital Equipment Corporation
  5. *
  6. * Permission to use, copy, modify, distribute, and sell this software and
  7. * its documentation for any purpose is hereby granted without fee, provided
  8. * that the above copyright notice appear in all copies and that both that
  9. * copyright notice and this permission notice appear in supporting
  10. * documentation, and that the names of Network Computing Devices or Digital
  11. * not be used in advertising or publicity pertaining to distribution of
  12. * the software without specific, written prior permission.
  13. *
  14. * NETWORK COMPUTING DEVICES AND DIGITAL DISCLAIM ALL WARRANTIES WITH
  15. * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  16. * AND FITNESS, IN NO EVENT SHALL NETWORK COMPUTING DEVICES OR DIGITAL BE
  17. * LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  18. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  19. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  20. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21. *
  22. * Author: Dave Lemke, Network Computing Devices Inc
  23. */
  24. /*
  25. Copyright 1987, 1998 The Open Group
  26. Permission to use, copy, modify, distribute, and sell this software and its
  27. documentation for any purpose is hereby granted without fee, provided that
  28. the above copyright notice appear in all copies and that both that
  29. copyright notice and this permission notice appear in supporting
  30. documentation.
  31. The above copyright notice and this permission notice shall be included
  32. in all copies or substantial portions of the Software.
  33. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  34. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  35. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  36. IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
  37. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  38. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  39. OTHER DEALINGS IN THE SOFTWARE.
  40. Except as contained in this notice, the name of The Open Group shall
  41. not be used in advertising or otherwise to promote the sale, use or
  42. other dealings in this Software without prior written authorization
  43. from The Open Group.
  44. */
  45. /* $XFree86: xc/lib/font/Speedo/spglyph.c,v 1.6 2001/01/17 19:43:20 dawes Exp $ */
  46. #ifdef HAVE_CONFIG_H
  47. #include <config.h>
  48. #endif
  49. #include <X11/X.h> /* for bit order #defines */
  50. #include "spint.h"
  51. #include <X11/fonts/fontutil.h>
  52. #undef CLIP_BBOX_NOISE
  53. static CurrentFontValuesRec current_font_values;
  54. static CurrentFontValuesPtr cfv = &current_font_values;
  55. static int bit_order,
  56. byte_order,
  57. scan;
  58. unsigned long
  59. sp_compute_data_size(
  60. FontPtr pfont,
  61. int mappad,
  62. int scanlinepad,
  63. unsigned long start,
  64. unsigned long end)
  65. {
  66. unsigned long ch;
  67. unsigned long size = 0;
  68. int bpr;
  69. SpeedoFontPtr spf = (SpeedoFontPtr) pfont->fontPrivate;
  70. FontInfoPtr pinfo = &pfont->info;
  71. int firstChar;
  72. firstChar = spf->master->first_char_id;
  73. /* allocate the space */
  74. switch (mappad) {
  75. int charsize;
  76. CharInfoPtr ci;
  77. xCharInfo *cim;
  78. case BitmapFormatImageRectMin:
  79. cfv->bpr = 0;
  80. for (ch = start; ch <= end; ch++) {
  81. ci = &spf->encoding[ch - firstChar];
  82. if (!ci)
  83. ci = spf->pDefault;
  84. cim = &ci->metrics;
  85. charsize = GLYPH_SIZE(ci, scanlinepad);
  86. charsize *= cim->ascent + cim->descent;
  87. size += charsize;
  88. }
  89. break;
  90. case BitmapFormatImageRectMaxWidth:
  91. bpr = GLWIDTHBYTESPADDED(FONT_MAX_WIDTH(pinfo), scanlinepad);
  92. cfv->bpr = bpr;
  93. for (ch = start; ch <= end; ch++) {
  94. ci = &spf->encoding[ch - firstChar];
  95. if (!ci)
  96. ci = spf->pDefault;
  97. cim = &ci->metrics;
  98. charsize = bpr * (cim->ascent + cim->descent);
  99. size += charsize;
  100. }
  101. break;
  102. case BitmapFormatImageRectMax:
  103. bpr = GLWIDTHBYTESPADDED(FONT_MAX_WIDTH(pinfo), scanlinepad);
  104. cfv->bpr = bpr;
  105. size = (end - start + 1) * bpr * FONT_MAX_HEIGHT(pinfo);
  106. break;
  107. default:
  108. assert(0);
  109. }
  110. return size;
  111. }
  112. static void
  113. finish_line(SpeedoFontPtr spf)
  114. {
  115. int bpr = cfv->bpr;
  116. CharInfoPtr ci = &spf->encoding[cfv->char_id - spf->master->first_char_id];
  117. if (bpr == 0) {
  118. bpr = GLYPH_SIZE(ci, cfv->scanpad);
  119. }
  120. if (bpr) { /* char may not have any metrics... */
  121. cfv->bp = (char *)cfv->bp + bpr;
  122. }
  123. assert(cfv->bp - sp_fp_cur->bitmaps <= sp_fp_cur->bitmap_size);
  124. }
  125. void
  126. sp_set_bitmap_bits(fix15 y, fix15 xbit1, fix15 xbit2)
  127. {
  128. int nmiddle;
  129. CARD8 startmask,
  130. endmask;
  131. CARD8 *dst;
  132. if (xbit1 > cfv->bit_width) {
  133. #ifdef CLIP_BBOX_NOISE
  134. SpeedoErr("Run wider than bitmap width -- truncated\n");
  135. #endif
  136. xbit1 = cfv->bit_width;
  137. }
  138. if (xbit2 > cfv->bit_width) {
  139. #ifdef CLIP_BBOX_NOISE
  140. SpeedoErr("Run wider than bitmap width -- truncated\n");
  141. #endif
  142. xbit2 = cfv->bit_width;
  143. }
  144. if (xbit2 < xbit1) {
  145. xbit2 = xbit1;
  146. }
  147. while (cfv->cur_y != y) {
  148. finish_line(sp_fp_cur);
  149. cfv->cur_y++;
  150. }
  151. cfv->last_y = y;
  152. if (y >= cfv->bit_height) {
  153. #ifdef CLIP_BBOX_NOISE
  154. SpeedoErr("Y larger than bitmap height -- truncated\n");
  155. #endif
  156. cfv->trunc = 1;
  157. return;
  158. }
  159. if (xbit1 < 0) /* XXX this is more than a little bit rude... */
  160. xbit1 = 0;
  161. nmiddle = (xbit1 >> 3);
  162. dst = (CARD8 *)cfv->bp + nmiddle;
  163. xbit2 -= (xbit1 & ~7);
  164. nmiddle = (xbit2 >> 3);
  165. xbit1 &= 7;
  166. xbit2 &= 7;
  167. if (bit_order == MSBFirst) {
  168. startmask = ((CARD8) ~0) >> xbit1;
  169. endmask = ~(((CARD8) ~0) >> xbit2);
  170. } else {
  171. startmask = ((CARD8) ~0) << xbit1;
  172. endmask = ~(((CARD8) ~0) << xbit2);
  173. }
  174. if (nmiddle == 0)
  175. *dst |= endmask & startmask;
  176. else {
  177. *dst++ |= startmask;
  178. while (--nmiddle)
  179. *dst++ = (CARD8)~0;
  180. *dst |= endmask;
  181. }
  182. }
  183. /* ARGSUSED */
  184. void
  185. sp_open_bitmap(fix31 x_set_width, fix31 y_set_width, fix31 xorg, fix31 yorg,
  186. fix15 xsize, fix15 ysize)
  187. {
  188. CharInfoPtr ci = &sp_fp_cur->encoding[cfv->char_id - sp_fp_cur->master->first_char_id];
  189. /*-
  190. * this is set to provide better quality bitmaps. since the Speedo
  191. * sp_get_bbox() function returns an approximate (but guarenteed to contain)
  192. * set of metrics, some of the bitmaps can be place poorly inside and
  193. * look bad.
  194. *
  195. * with this set, the actual bitmap values are used instead of the bboxes.
  196. * it makes things look better, but causes two possible problems:
  197. *
  198. * 1 - the reported min & max bounds may not correspond to the extents
  199. * reported
  200. * 2 - if the extents are reported before the character is generated,
  201. * a client could see them change. this currently never happens,
  202. * but will when a desired enhancement (don't reneder till needed)
  203. * is made.
  204. */
  205. #define BBOX_FIXUP 1
  206. #ifdef BBOX_FIXUP
  207. int off_horz;
  208. int off_vert;
  209. if (xorg < 0)
  210. off_horz = (fix15) ((xorg - 32768L) / 65536);
  211. else
  212. off_horz = (fix15) ((xorg + 32768L) / 65536);
  213. if (yorg < 0)
  214. off_vert = (fix15) ((yorg - 32768L) / 65536);
  215. else
  216. off_vert = (fix15) ((yorg + 32768L) / 65536);
  217. if (xsize != 0 || ysize != 0 || ci->metrics.characterWidth)
  218. {
  219. ci->metrics.leftSideBearing = off_horz;
  220. ci->metrics.descent = -off_vert;
  221. ci->metrics.rightSideBearing = xsize + off_horz;
  222. ci->metrics.ascent = ysize + off_vert;
  223. }
  224. else
  225. {
  226. /* If setting the proper size would cause the character to appear to
  227. be non-existent, fudge things by giving it a pixel to occupy. */
  228. xsize = ysize = 1;
  229. ci->metrics.leftSideBearing = ci->metrics.descent = 0;
  230. ci->metrics.rightSideBearing = ci->metrics.ascent = 1;
  231. }
  232. cfv->bit_width = xsize;
  233. cfv->bit_height = ysize;
  234. #else
  235. cfv->bit_width = ci->metrics.rightSideBearing -
  236. ci->metrics.leftSideBearing;
  237. cfv->bit_height = ci->metrics.ascent + ci->metrics.descent;
  238. #endif
  239. assert(cfv->bp - sp_fp_cur->bitmaps <= sp_fp_cur->bitmap_size);
  240. ci->bits = (char *) cfv->bp;
  241. cfv->cur_y = 0;
  242. }
  243. void
  244. sp_close_bitmap()
  245. {
  246. CharInfoPtr ci = &sp_fp_cur->encoding[cfv->char_id - sp_fp_cur->master->first_char_id];
  247. int bpr = cfv->bpr;
  248. if (bpr == 0)
  249. bpr = GLYPH_SIZE(ci, cfv->scanpad);
  250. if (!cfv->trunc)
  251. finish_line(sp_fp_cur);
  252. cfv->trunc = 0;
  253. cfv->last_y++;
  254. while (cfv->last_y < cfv->bit_height) {
  255. finish_line(sp_fp_cur);
  256. cfv->last_y++;
  257. }
  258. if (byte_order != bit_order) {
  259. switch (scan) {
  260. case 1:
  261. break;
  262. case 2:
  263. TwoByteSwap(cfv->bp, bpr * cfv->bit_height);
  264. break;
  265. case 4:
  266. FourByteSwap(cfv->bp, bpr * cfv->bit_height);
  267. break;
  268. }
  269. }
  270. }
  271. int
  272. sp_build_all_bitmaps(
  273. FontPtr pfont,
  274. fsBitmapFormat format,
  275. fsBitmapFormatMask fmask)
  276. {
  277. int ret,
  278. glyph = 1,
  279. image = BitmapFormatImageRectMin;
  280. unsigned long glyph_size;
  281. SpeedoFontPtr spf = (SpeedoFontPtr) pfont->fontPrivate;
  282. SpeedoMasterFontPtr spmf = spf->master;
  283. pointer bitmaps;
  284. int start,
  285. end,
  286. i;
  287. scan = 1;
  288. ret = CheckFSFormat(format, fmask,
  289. &bit_order, &byte_order, &scan, &glyph, &image);
  290. pfont->bit = bit_order;
  291. pfont->byte = byte_order;
  292. pfont->glyph = glyph;
  293. pfont->scan = scan;
  294. if (ret != Successful)
  295. return BadFontFormat;
  296. start = spmf->first_char_id;
  297. end = spmf->max_id;
  298. glyph_size = sp_compute_data_size(pfont, image, glyph, start, end);
  299. /* XXX -- MONDO KLUDGE -- add some slop */
  300. /*
  301. * not sure why this is wanted, but it keeps the packer from going off the
  302. * end and toasting us down the line
  303. */
  304. glyph_size += 20;
  305. #ifdef DEBUG
  306. spf->bitmap_size = glyph_size;
  307. #endif
  308. bitmaps = (pointer) xalloc(glyph_size);
  309. if (!bitmaps)
  310. return AllocError;
  311. bzero((char *) bitmaps, glyph_size);
  312. /* set up some state */
  313. sp_fp_cur = spf;
  314. spf->bitmaps = bitmaps;
  315. cfv->format = format;
  316. cfv->scanpad = glyph;
  317. cfv->bp = bitmaps;
  318. for (i = 0; i < spmf->num_chars; i++) {
  319. int j;
  320. cfv->char_index = spmf->enc[i * 2 + 1];
  321. cfv->char_id = spmf->enc[i * 2];
  322. #ifdef DEBUG
  323. fprintf(stderr, "build_all_sp_bitmaps:i = %d, Char ID = %d\n", i, cfv->char_id);
  324. #endif
  325. if (!cfv->char_id)
  326. continue;
  327. /*
  328. * See if this character is in the list of ranges specified in the
  329. * XLFD name
  330. */
  331. for (j = 0; j < spf->vals.nranges; j++)
  332. if (cfv->char_id >= mincharno(spf->vals.ranges[j]) &&
  333. cfv->char_id <= maxcharno(spf->vals.ranges[j]))
  334. break;
  335. /* If not, don't realize it. */
  336. if (spf->vals.nranges && j == spf->vals.nranges)
  337. continue;
  338. if (!sp_make_char(cfv->char_index)) {
  339. #ifdef DEBUG /* can be very common with some encodings */
  340. SpeedoErr("Can't make char %d\n", cfv->char_index);
  341. #endif
  342. }
  343. }
  344. return Successful;
  345. }