home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / x / x11p-13.zip / do_text.c < prev    next >
C/C++ Source or Header  |  1991-05-08  |  11KB  |  410 lines

  1. /*****************************************************************************
  2. Copyright 1988, 1989 by Digital Equipment Corporation, Maynard, Massachusetts.
  3.  
  4.                         All Rights Reserved
  5.  
  6. Permission to use, copy, modify, and distribute this software and its 
  7. documentation for any purpose and without fee is hereby granted, 
  8. provided that the above copyright notice appear in all copies and that
  9. both that copyright notice and this permission notice appear in 
  10. supporting documentation, and that the name of Digital not be
  11. used in advertising or publicity pertaining to distribution of the
  12. software without specific, written prior permission.  
  13.  
  14. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  15. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  16. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  17. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  18. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  19. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  20. SOFTWARE.
  21.  
  22. ******************************************************************************/
  23.  
  24. #include "x11perf.h"
  25.  
  26. static char **charBuf;
  27. static XFontStruct *font, *bfont;
  28. static int height, ypos;
  29. static XTextItem *items;
  30. static int charsPerLine, totalLines;
  31.  
  32. #define XPOS 20
  33. #define SEGS 3
  34.  
  35.  
  36. int InitText(xp, p, reps)
  37.     XParms  xp;
  38.     Parms   p;
  39.     int     reps;
  40. {
  41.     int        i, j;
  42.     char    ch;
  43.     XGCValues   gcv;
  44.  
  45.     font = XLoadQueryFont(xp->d, p->font);
  46.     if (font == NULL) {
  47.     printf("Could not load font '%s', benchmark omitted\n", p->font);
  48.     return 0;
  49.     }
  50.  
  51.     bfont = NULL;
  52.     if (p->bfont != NULL) {
  53.     bfont = XLoadQueryFont(xp->d, p->bfont);
  54.     if (bfont == NULL) {
  55.         printf("Could not load font '%s', benchmark omitted\n", p->bfont);
  56.         return 0;
  57.     }
  58.     }
  59.  
  60.     ypos = XPOS;
  61.     height = (font->max_bounds.ascent + font->max_bounds.descent) + 1;
  62.     if (bfont != NULL) {
  63.     int     h = (bfont->max_bounds.ascent + bfont->max_bounds.descent) + 1;
  64.     if (h > height)
  65.         height = h;
  66.     }
  67.     gcv.font = font->fid;
  68.     XChangeGC(xp->d, xp->fggc, GCFont, &gcv);
  69.     XChangeGC(xp->d, xp->bggc, GCFont, &gcv);
  70.  
  71.     charsPerLine = p->objects;
  72.     charsPerLine = (charsPerLine + 3) & ~3;
  73.     p->objects = charsPerLine;
  74.  
  75.     totalLines = '\177' - ' ' + 1;
  76.     if (totalLines > reps) totalLines = reps;
  77.  
  78.     charBuf = (char **) malloc(totalLines*sizeof (char *));
  79.     if (p->special)
  80.     items = (XTextItem *) malloc(totalLines*SEGS*sizeof (XTextItem));
  81.  
  82.     for (i = 0; i != totalLines; i++) {
  83.     charBuf[i] = (char *) malloc (sizeof (char)*charsPerLine);
  84.     ch = i + ' ';
  85.     for (j = 0; j != charsPerLine; j++) {
  86.         charBuf[i][j] = ch;
  87.         if (ch == '\177') ch = ' '; else ch++;
  88.     }
  89.     if (p->special) {
  90.         items[i*SEGS+0].chars = &(charBuf[i][0]);
  91.         items[i*SEGS+0].nchars = charsPerLine/4;
  92.         items[i*SEGS+0].delta = 0;
  93.         items[i*SEGS+0].font = font->fid;
  94.         items[i*SEGS+1].chars = &(charBuf[i][charsPerLine/4]);
  95.         items[i*SEGS+1].nchars = charsPerLine/2;
  96.         items[i*SEGS+1].delta = 3;
  97.         items[i*SEGS+1].font = bfont->fid;
  98.         items[i*SEGS+2].chars = &(charBuf[i][3*charsPerLine/4]);
  99.         items[i*SEGS+2].nchars = charsPerLine/4;
  100.         items[i*SEGS+2].delta = 3;
  101.         items[i*SEGS+2].font = font->fid;
  102.     }
  103.     }
  104.     return reps;
  105. }
  106.  
  107.  
  108. #define GetRealChar(font, totalChars, ch)                \
  109. {                                    \
  110.     XCharStruct *pci;                            \
  111.     do {                                \
  112.     ch--;                                \
  113.     if (ch < 0) {                            \
  114.         ch = totalChars-1;                        \
  115.     }                                \
  116.     if (font->per_char == NULL) break;                \
  117.     pci = &(font->per_char[ch]);                    \
  118.     } while ( (pci->lbearing | pci->rbearing | pci->width        \
  119.              | pci->ascent | pci->descent | pci->attributes) == 0);     \
  120. } /* GetRealChar */
  121.  
  122. int InitText16(xp, p, reps)
  123.     XParms  xp;
  124.     Parms   p;
  125.     int     reps;
  126. {
  127.     register int    i, j;
  128.     register char    *pbuf0, *pbuf1, *pbuf2;
  129.     XGCValues       gcv;
  130.     int            charsPerSeg;
  131.     int            rows, columns, totalChars, ch;
  132.     int            brows, bcolumns, btotalChars, bch;
  133.  
  134.     font = XLoadQueryFont(xp->d, p->font);
  135.     if (font == NULL) {
  136.     printf("Could not load font '%s', benchmark omitted\n", p->font);
  137.     return 0;
  138.     }
  139.     rows = font->max_byte1 - font->min_byte1 + 1;
  140.     columns = font->max_char_or_byte2 - font->min_char_or_byte2 + 1;
  141.     totalChars = rows * columns;
  142.     totalLines = rows;
  143.     ch = totalChars;
  144.  
  145.     bfont = NULL;
  146.     if (p->bfont != NULL) {
  147.     bfont = XLoadQueryFont(xp->d, p->bfont);
  148.     if (bfont == NULL) {
  149.         printf("Could not load font '%s', benchmark omitted\n", p->bfont);
  150.         return 0;
  151.     }
  152.     brows = bfont->max_byte1 - bfont->min_byte1 + 1;
  153.     bcolumns = bfont->max_char_or_byte2 - bfont->min_char_or_byte2 + 1;
  154.     btotalChars = brows * bcolumns;
  155.     bch = btotalChars;
  156.     if (brows > totalLines) totalLines = brows;
  157.     }
  158.  
  159.     ypos = XPOS;
  160.     height = (font->max_bounds.ascent + font->max_bounds.descent) + 1;
  161.     if (bfont != NULL) {
  162.     int     h = (bfont->max_bounds.ascent + bfont->max_bounds.descent) + 1;
  163.     if (h > height)
  164.         height = h;
  165.     }
  166.     gcv.font = font->fid;
  167.     XChangeGC(xp->d, xp->fggc, GCFont, &gcv);
  168.     XChangeGC(xp->d, xp->bggc, GCFont, &gcv);
  169.  
  170.     charsPerLine = p->objects;
  171.  
  172.     if (totalLines > reps) totalLines = reps;
  173.  
  174.     if (p->special) {
  175.     charsPerLine = (charsPerLine + 3) & ~3;    /* make a multiple of four */
  176.     p->objects = charsPerLine;
  177.  
  178.     items = (XTextItem *) malloc(totalLines*SEGS*sizeof (XTextItem));
  179.  
  180.     for (i = 0; i < totalLines; i++) {
  181.         pbuf0 = items[i*SEGS+0].chars =
  182.                 (char *) malloc (sizeof (char)*charsPerLine/2);
  183.         items[i*SEGS+0].nchars = charsPerLine/4;
  184.         items[i*SEGS+0].delta = 0;
  185.         items[i*SEGS+0].font = font->fid;
  186.         pbuf1 = items[i*SEGS+1].chars =
  187.                 (char *) malloc (sizeof (char)*charsPerLine);
  188.         items[i*SEGS+1].nchars = charsPerLine/2;
  189.         items[i*SEGS+1].delta = 3;
  190.         items[i*SEGS+1].font = bfont->fid;
  191.         pbuf2 = items[i*SEGS+2].chars =
  192.                 (char *) malloc (sizeof (char)*charsPerLine/2);
  193.         items[i*SEGS+2].nchars = charsPerLine/4;
  194.         items[i*SEGS+2].delta = 3;
  195.         items[i*SEGS+2].font = font->fid;
  196.         for (j = 0; j < charsPerLine/4; j++) {
  197.         GetRealChar(font, totalChars, ch);
  198.         *pbuf0++ = ch / columns + font->min_byte1;
  199.         *pbuf0++ = ch % columns + font->min_char_or_byte2;
  200.         GetRealChar(font, totalChars, ch);
  201.         *pbuf2++ = ch / columns + font->min_byte1;
  202.         *pbuf2++ = ch % columns + font->min_char_or_byte2;
  203.         }
  204.         for (j = 0; j < charsPerLine/2; j++) {
  205.         GetRealChar(bfont, btotalChars, bch);
  206.         *pbuf1++ = bch / bcolumns + bfont->min_byte1;
  207.         *pbuf1++ = bch % bcolumns + bfont->min_char_or_byte2;
  208.         }
  209.     }
  210.     } else {
  211.     charBuf = (char **) malloc(totalLines*sizeof (char *));
  212.     for (i = 0; i < totalLines; i++) {
  213.         pbuf0 = charBuf[i] = (char *) malloc (sizeof (char)*charsPerLine*2);
  214.         for (j = 0; j < charsPerLine; j++) {
  215.         GetRealChar(font, totalChars, ch);
  216.         *pbuf0++ = ch / columns + font->min_byte1;
  217.         *pbuf0++ = ch % columns + font->min_char_or_byte2;
  218.         }
  219.     }
  220.     }
  221.     return reps;
  222. }
  223.  
  224. void DoText(xp, p, reps)
  225.     XParms  xp;
  226.     Parms   p;
  227.     int     reps;
  228. {
  229.     int     i, line, startLine;
  230.  
  231.     startLine = 0;
  232.     line = 0;
  233.     for (i = 0; i != reps; i++) {
  234.     XDrawString(
  235.         xp->d, xp->w, xp->fggc, XPOS, ypos, charBuf[line], charsPerLine);
  236.     ypos += height;
  237.     if (ypos > HEIGHT - height) {
  238.         /* Wraparound to top of window */
  239.         ypos = XPOS;
  240.         line = startLine;
  241.         startLine = (startLine + 1) % totalLines;
  242.     }
  243.     line = (line + 1) % totalLines;
  244.     }
  245. }
  246.  
  247. void DoText16(xp, p, reps)
  248.     XParms  xp;
  249.     Parms   p;
  250.     int     reps;
  251. {
  252.     int     i, line, startLine;
  253.  
  254.     startLine = 0;
  255.     line = 0;
  256.     for (i = 0; i < reps; i++) {
  257.     XDrawString16(
  258.         xp->d, xp->w, xp->fggc, XPOS, ypos, charBuf[line], charsPerLine);
  259.     ypos += height;
  260.     if (ypos > HEIGHT - height) {
  261.         /* Wraparound to top of window */
  262.         ypos = XPOS;
  263.         line = startLine;
  264.         startLine = (startLine + 1) % totalLines;
  265.     }
  266.     line = (line + 1) % totalLines;
  267.     }
  268. }
  269.  
  270. void DoPolyText(xp, p, reps)
  271.     XParms  xp;
  272.     Parms   p;
  273.     int     reps;
  274. {
  275.     int     i, line, startLine;
  276.  
  277.     startLine = 0;
  278.     line = 0;
  279.     for (i = 0; i != reps; i++) {
  280.     XDrawText(
  281.         xp->d, xp->w, xp->fggc, XPOS, ypos, &items[line*SEGS], SEGS);
  282.     ypos += height;
  283.     if (ypos > HEIGHT - height) {
  284.         /* Wraparound to top of window */
  285.         ypos = XPOS;
  286.         line = startLine;
  287.         startLine = (startLine + 1) % totalLines;
  288.     }
  289.     line = (line + 1) % totalLines;
  290.     }
  291. }
  292.  
  293. void DoPolyText16(xp, p, reps)
  294.     XParms  xp;
  295.     Parms   p;
  296.     int     reps;
  297. {
  298.     int     i, line, startLine;
  299.  
  300.     startLine = 0;
  301.     line = 0;
  302.     for (i = 0; i != reps; i++) {
  303.     XDrawText16(
  304.         xp->d, xp->w, xp->fggc, XPOS, ypos, &items[line*SEGS], SEGS);
  305.     ypos += height;
  306.     if (ypos > HEIGHT - height) {
  307.         /* Wraparound to top of window */
  308.         ypos = XPOS;
  309.         line = startLine;
  310.         startLine = (startLine + 1) % totalLines;
  311.     }
  312.     line = (line + 1) % totalLines;
  313.     }
  314. }
  315.  
  316. void DoImageText(xp, p, reps)
  317.     XParms  xp;
  318.     Parms   p;
  319.     int     reps;
  320. {
  321.     int     i, line, startLine;
  322.  
  323.     startLine = 0;
  324.     line = 0;
  325.     for (i = 0; i != reps; i++) {
  326.     XDrawImageString(
  327.         xp->d, xp->w, xp->fggc, XPOS, ypos, charBuf[line], charsPerLine);
  328.     ypos += height;
  329.     if (ypos > HEIGHT - height) {
  330.         /* Wraparound to top of window */
  331.         ypos = XPOS;
  332.         startLine = (startLine + 17) % totalLines;
  333.         line = startLine;
  334.     }
  335.     line = (line + 1) % totalLines;
  336.     }
  337. }
  338.  
  339. void DoImageText16(xp, p, reps)
  340.     XParms  xp;
  341.     Parms   p;
  342.     int     reps;
  343. {
  344.     int     i, line, startLine;
  345.  
  346.     startLine = 0;
  347.     line = 0;
  348.     for (i = 0; i != reps; i++) {
  349.     XDrawImageString16(
  350.         xp->d, xp->w, xp->fggc, XPOS, ypos, charBuf[line], charsPerLine);
  351.     ypos += height;
  352.     if (ypos > HEIGHT - height) {
  353.         /* Wraparound to top of window */
  354.         ypos = XPOS;
  355.         startLine = (startLine + 17) % totalLines;
  356.         line = startLine;
  357.     }
  358.     line = (line + 1) % totalLines;
  359.     }
  360. }
  361.  
  362. void ClearTextWin(xp, p)
  363.     XParms  xp;
  364.     Parms   p;
  365. {
  366.     XClearWindow(xp->d, xp->w);
  367. }
  368.  
  369. void EndText(xp, p)
  370.     XParms  xp;
  371.     Parms   p;
  372. {
  373.     int i;
  374.  
  375.     for (i = 0; i != totalLines; i++)
  376.     free(charBuf[i]);
  377.     free(charBuf);
  378.     if (p->special)
  379.     free(items);
  380.     XFreeFont(xp->d, font);
  381.     if (bfont != NULL)
  382.     XFreeFont(xp->d, bfont);
  383. }
  384.  
  385. void EndText16(xp, p)
  386.     XParms  xp;
  387.     Parms   p;
  388. {
  389.     int i;
  390.  
  391.     if (p->special) {
  392.     for (i = 0; i < totalLines; i++) {
  393.         free(items[i*SEGS+0].chars);
  394.         free(items[i*SEGS+1].chars);
  395.         free(items[i*SEGS+2].chars);
  396.     }
  397.     free(items);
  398.     } else {
  399.     for (i = 0; i < totalLines; i++) {
  400.         free(charBuf[i]);
  401.     }
  402.     free(charBuf);
  403.     }
  404.     XFreeFont(xp->d, font);
  405.     if(bfont != NULL) {
  406.     XFreeFont(xp->d, bfont);
  407.     }
  408. }
  409.  
  410.