home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / evbl0627.zip / everblue_20010627.zip / x11 / Xlib_TextExt.c < prev    next >
C/C++ Source or Header  |  1999-11-02  |  15KB  |  487 lines

  1. /* $XConsortium: TextExt.c /main/21 1996/12/05 10:40:03 swick $ */
  2. /*
  3.  
  4. Copyright (c) 1989  X Consortium
  5.  
  6. Permission is hereby granted, free of charge, to any person obtaining
  7. a copy of this software and associated documentation files (the
  8. "Software"), to deal in the Software without restriction, including
  9. without limitation the rights to use, copy, modify, merge, publish,
  10. distribute, sublicense, and/or sell copies of the Software, and to
  11. permit persons to whom the Software is furnished to do so, subject to
  12. the following conditions:
  13.  
  14. The above copyright notice and this permission notice shall be included
  15. in all copies or substantial portions of the Software.
  16.  
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
  21. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  22. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. OTHER DEALINGS IN THE SOFTWARE.
  24.  
  25. Except as contained in this notice, the name of the X Consortium shall
  26. not be used in advertising or otherwise to promote the sale, use or
  27. other dealings in this Software without prior written authorization
  28. from the X Consortium.
  29.  
  30. */
  31. /*
  32.  * Copyright 1995 by FUJITSU LIMITED
  33.  * This is source code modified by FUJITSU LIMITED under the Joint
  34.  * Development Agreement for the CDE/Motif PST.
  35.  */
  36.  
  37.  
  38. #include "Xlib_private.h"
  39.  
  40. #define min_byte2 min_char_or_byte2
  41. #define max_byte2 max_char_or_byte2
  42.  
  43.  
  44. /* 
  45.  * CI_GET_ROWZERO_CHAR_INFO_2D - do the same thing as CI_GET_CHAR_INFO_1D,
  46.  * except that the font has more than one row.  This is special case of more
  47.  * general version used in XTextExt16.c since row == 0.  This is used when
  48.  * max_byte2 is not zero.  A further optimization would do the check for
  49.  * min_byte1 being zero ahead of time.
  50.  */
  51.  
  52. #define CI_GET_ROWZERO_CHAR_INFO_2D(fs,col,def,cs) \
  53. { \
  54.     cs = def; \
  55.     if (fs->min_byte1 == 0 && \
  56.     col >= fs->min_byte2 && col <= fs->max_byte2) { \
  57.     if (fs->per_char == NULL) { \
  58.         cs = &fs->min_bounds; \
  59.     } else { \
  60.         cs = &fs->per_char[(col - fs->min_byte2)]; \
  61.         if (CI_NONEXISTCHAR(cs)) cs = def; \
  62.     } \
  63.     } \
  64. }
  65.  
  66.  
  67. /*
  68.  * XTextExtents - compute the extents of string given as a sequences of eight
  69.  * bit bytes.  Since we know that the input characters will always be from the
  70.  * first row of the font (i.e. byte1 == 0), we can do some optimizations beyond
  71.  * what is done in XTextExtents16.
  72.  */
  73. #if NeedFunctionPrototypes
  74. int XTextExtents (
  75.     XFontStruct *fs,
  76.     _Xconst char *string,
  77.     int nchars,
  78.     int *dir,           /* RETURN font information */
  79.     int *font_ascent,   /* RETURN font information */
  80.     int *font_descent,  /* RETURN font information */
  81.     register XCharStruct *overall)    /* RETURN character information */
  82. #else
  83. int XTextExtents (fs, string, nchars, dir, font_ascent, font_descent, overall)
  84.     XFontStruct *fs;
  85.     char *string;
  86.     int nchars;
  87.     int *dir, *font_ascent, *font_descent;  /* RETURN font information */
  88.     register XCharStruct *overall;    /* RETURN character information */
  89. #endif
  90. {
  91.     DBUG_ENTER("XTextExtents")
  92.     int i;                /* iterator */
  93.     Bool singlerow = (fs->max_byte1 == 0);  /* optimization */
  94.     int nfound = 0;            /* number of characters found */
  95.     XCharStruct *def;            /* info about default char */
  96.     unsigned char *us;          /* be 8bit clean */
  97.  
  98.     if (singlerow) {            /* optimization */
  99.     CI_GET_DEFAULT_INFO_1D (fs, def);
  100.     } else {
  101.     CI_GET_DEFAULT_INFO_2D (fs, def);
  102.     }
  103.  
  104.     *dir = fs->direction;
  105.     *font_ascent = fs->ascent;
  106.     *font_descent = fs->descent;
  107.  
  108.     /*
  109.      * Iterate over the input string getting the appropriate * char struct.
  110.      * The default (which may be null if there is no def_char) will be returned
  111.      * if the character doesn't exist.  On the first time * through the loop,
  112.      * assign the values to overall; otherwise, compute * the new values.
  113.      */
  114.  
  115.     for (i = 0, us = (unsigned char *) string; i < nchars; i++, us++) {
  116.     register unsigned uc = (unsigned) *us;    /* since about to do macro */
  117.     register XCharStruct *cs;
  118.  
  119.     if (singlerow) {        /* optimization */
  120.         CI_GET_CHAR_INFO_1D (fs, uc, def, cs);
  121.     } else {
  122.         CI_GET_ROWZERO_CHAR_INFO_2D (fs, uc, def, cs);
  123.     }
  124.  
  125.     if (cs) {
  126.         if (nfound++ == 0) {
  127.         *overall = *cs;
  128.         } else {
  129.         overall->ascent = max (overall->ascent, cs->ascent);
  130.         overall->descent = max (overall->descent, cs->descent);
  131.         overall->lbearing = min (overall->lbearing, 
  132.                      overall->width + cs->lbearing);
  133.         overall->rbearing = max (overall->rbearing,
  134.                      overall->width + cs->rbearing);
  135.         overall->width += cs->width;
  136.         }
  137.     }
  138.     }
  139.  
  140.     /*
  141.      * if there were no characters, then set everything to 0
  142.      */
  143.     if (nfound == 0) {
  144.     overall->width = overall->ascent = overall->descent = 
  145.       overall->lbearing = overall->rbearing = 0;
  146.     }
  147.  
  148.     DBUG_RETURN(0);
  149. }
  150.  
  151.  
  152. /*
  153.  * XTextWidth - compute the width of a string of eightbit bytes.  This is a 
  154.  * subset of XTextExtents.
  155.  */
  156. #if NeedFunctionPrototypes
  157. int XTextWidth (
  158.     XFontStruct *fs,
  159.     _Xconst char *string,
  160.     int count)
  161. #else
  162. int XTextWidth (fs, string, count)
  163.     XFontStruct *fs;
  164.     char *string;
  165.     int count;
  166. #endif
  167. {
  168.     DBUG_ENTER("XTextWidth")
  169.     int i;                /* iterator */
  170.     Bool singlerow = (fs->max_byte1 == 0);  /* optimization */
  171.     XCharStruct *def;            /* info about default char */
  172.     unsigned char *us;          /* be 8bit clean */
  173.     int width = 0;            /* RETURN value */
  174.  
  175.     if (singlerow) {            /* optimization */
  176.     CI_GET_DEFAULT_INFO_1D (fs, def);
  177.     } else {
  178.     CI_GET_DEFAULT_INFO_2D (fs, def);
  179.     }
  180.  
  181.     if (def && fs->min_bounds.width == fs->max_bounds.width)
  182.     DBUG_RETURN(fs->min_bounds.width * count);
  183.  
  184.     /*
  185.      * Iterate over all character in the input string; only consider characters
  186.      * that exist.
  187.      */
  188.     for (i = 0, us = (unsigned char *) string; i < count; i++, us++) {
  189.     register unsigned uc = (unsigned) *us;    /* since about to do macro */
  190.     register XCharStruct *cs;
  191.  
  192.     if (singlerow) {        /* optimization */
  193.         CI_GET_CHAR_INFO_1D (fs, uc, def, cs);
  194.     } else {
  195.         CI_GET_ROWZERO_CHAR_INFO_2D (fs, uc, def, cs);
  196.     }
  197.  
  198.     if (cs) width += cs->width;
  199.     }
  200.  
  201.     DBUG_RETURN(width);
  202. }
  203.  
  204.  
  205.  
  206. /*
  207.  * _XTextHeight - compute the height of a string of eightbit bytes.
  208.  */
  209. #if NeedFunctionPrototypes
  210. int _XTextHeight (
  211.     XFontStruct *fs,
  212.     _Xconst char *string,
  213.     int count)
  214. #else
  215. int _XTextHeight (fs, string, count)
  216.     XFontStruct *fs;
  217.     char *string;
  218.     int count;
  219. #endif
  220. {
  221.     DBUG_ENTER("_XTextHeight")
  222.     int i;                /* iterator */
  223.     Bool singlerow = (fs->max_byte1 == 0);  /* optimization */
  224.     XCharStruct *def;            /* info about default char */
  225.     unsigned char *us;          /* be 8bit clean */
  226.     int height = 0;            /* RETURN value */
  227.  
  228.     if (singlerow) {            /* optimization */
  229.     CI_GET_DEFAULT_INFO_1D (fs, def);
  230.     } else {
  231.     CI_GET_DEFAULT_INFO_2D (fs, def);
  232.     }
  233.  
  234.     if (def && (fs->min_bounds.ascent == fs->max_bounds.ascent)
  235.         && (fs->min_bounds.descent == fs->max_bounds.descent))
  236.     DBUG_RETURN((fs->min_bounds.ascent + fs->min_bounds.descent) * count);
  237.  
  238.     /*
  239.      * Iterate over all character in the input string; only consider characters
  240.      * that exist.
  241.      */
  242.     for (i = 0, us = (unsigned char *) string; i < count; i++, us++) {
  243.     register unsigned uc = (unsigned) *us;    /* since about to do macro */
  244.     register XCharStruct *cs;
  245.  
  246.     if (singlerow) {        /* optimization */
  247.         CI_GET_CHAR_INFO_1D (fs, uc, def, cs);
  248.     } else {
  249.         CI_GET_ROWZERO_CHAR_INFO_2D (fs, uc, def, cs);
  250.     }
  251.  
  252.     if (cs) height += (cs->ascent + cs->descent);
  253.     }
  254.  
  255.     DBUG_RETURN(height);
  256. }
  257.  
  258. /* $XConsortium: TextExt16.c /main/16 1996/12/05 10:40:07 swick $ */
  259. /*
  260.  
  261. Copyright (c) 1989  X Consortium
  262.  
  263. Permission is hereby granted, free of charge, to any person obtaining
  264. a copy of this software and associated documentation files (the
  265. "Software"), to deal in the Software without restriction, including
  266. without limitation the rights to use, copy, modify, merge, publish,
  267. distribute, sublicense, and/or sell copies of the Software, and to
  268. permit persons to whom the Software is furnished to do so, subject to
  269. the following conditions:
  270.  
  271. The above copyright notice and this permission notice shall be included
  272. in all copies or substantial portions of the Software.
  273.  
  274. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  275. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  276. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  277. IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
  278. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  279. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  280. OTHER DEALINGS IN THE SOFTWARE.
  281.  
  282. Except as contained in this notice, the name of the X Consortium shall
  283. not be used in advertising or otherwise to promote the sale, use or
  284. other dealings in this Software without prior written authorization
  285. from the X Consortium.
  286.  
  287. */
  288. /*
  289.  * Copyright 1995 by FUJITSU LIMITED
  290.  * This is source code modified by FUJITSU LIMITED under the Joint
  291.  * Development Agreement for the CDE/Motif PST.
  292.  */
  293.  
  294.  
  295.  
  296. /*
  297.  * XTextExtents16 - compute the extents of string given as a sequence of 
  298.  * XChar2bs.
  299.  */
  300. #if NeedFunctionPrototypes
  301. int XTextExtents16 (
  302.     XFontStruct *fs,
  303.     _Xconst XChar2b *string,
  304.     int nchars,
  305.     int *dir,           /* RETURN font information */
  306.     int *font_ascent,   /* RETURN font information */
  307.     int *font_descent,  /* RETURN font information */
  308.     register XCharStruct *overall)    /* RETURN character information */
  309. #else
  310. int XTextExtents16 (fs, string, nchars, dir, font_ascent, font_descent, overall)
  311.     XFontStruct *fs;
  312.     XChar2b *string;
  313.     int nchars;
  314.     int *dir, *font_ascent, *font_descent;  /* RETURN font information */
  315.     register XCharStruct *overall;    /* RETURN character information */
  316. #endif
  317. {
  318.     DBUG_ENTER("XTextExtents16")
  319.     int i;                /* iterator */
  320.     Bool singlerow = (fs->max_byte1 == 0);  /* optimization */
  321.     int nfound = 0;            /* number of characters found */
  322.     XCharStruct *def;            /* info about default char */
  323.  
  324.     if (singlerow) {
  325.     CI_GET_DEFAULT_INFO_1D (fs, def);
  326.     } else {
  327.     CI_GET_DEFAULT_INFO_2D (fs, def);
  328.     }
  329.  
  330.     *dir = fs->direction;
  331.     *font_ascent = fs->ascent;
  332.     *font_descent = fs->descent;
  333.  
  334.     /*
  335.      * Iterate over the input string getting the appropriate * char struct.
  336.      * The default (which may be null if there is no def_char) will be returned
  337.      * if the character doesn't exist.  On the first time * through the loop,
  338.      * assign the values to overall; otherwise, compute * the new values.
  339.      */
  340.  
  341.     for (i = 0; i < nchars; i++, string++) {
  342.     register XCharStruct *cs;
  343.     unsigned int r = (unsigned int) string->byte1;    /* watch for macros */
  344.     unsigned int c = (unsigned int) string->byte2;    /* watch for macros */
  345.  
  346.     if (singlerow) {
  347.         unsigned int ind = ((r << 8) | c);        /* watch for macros */
  348.         CI_GET_CHAR_INFO_1D (fs, ind, def, cs);
  349.     } else {
  350.         CI_GET_CHAR_INFO_2D (fs, r, c, def, cs);
  351.     }
  352.  
  353.     if (cs) {
  354.         if (nfound++ == 0) {
  355.         *overall = *cs;
  356.         } else {
  357.         overall->ascent = max (overall->ascent, cs->ascent);
  358.         overall->descent = max (overall->descent, cs->descent);
  359.         overall->lbearing = min (overall->lbearing, 
  360.                      overall->width + cs->lbearing);
  361.         overall->rbearing = max (overall->rbearing,
  362.                      overall->width + cs->rbearing);
  363.         overall->width += cs->width;
  364.         }
  365.     }
  366.     }
  367.  
  368.     /*
  369.      * if there were no characters, then set everything to 0
  370.      */
  371.     if (nfound == 0) {
  372.     overall->width = overall->ascent = overall->descent = 
  373.       overall->lbearing = overall->rbearing = 0;
  374.     }
  375.  
  376.     DBUG_RETURN(0);
  377. }
  378.  
  379.  
  380. /*
  381.  * XTextWidth16 - compute the width of sequence of XChar2bs.  This is a 
  382.  * subset of XTextExtents16.
  383.  */
  384. #if NeedFunctionPrototypes
  385. int XTextWidth16 (
  386.     XFontStruct *fs,
  387.     _Xconst XChar2b *string,
  388.     int count)
  389. #else
  390. int XTextWidth16 (fs, string, count)
  391.     XFontStruct *fs;
  392.     XChar2b *string;
  393.     int count;
  394. #endif
  395. {
  396.     DBUG_ENTER("XTextWidth16")
  397.     int i;                /* iterator */
  398.     Bool singlerow = (fs->max_byte1 == 0);  /* optimization */
  399.     XCharStruct *def;            /* info about default char */
  400.     int width = 0;            /* RETURN value */
  401.  
  402.     if (singlerow) {
  403.     CI_GET_DEFAULT_INFO_1D (fs, def);
  404.     } else {
  405.     CI_GET_DEFAULT_INFO_2D (fs, def);
  406.     }
  407.  
  408.     if (def && fs->min_bounds.width == fs->max_bounds.width)
  409.     DBUG_RETURN(fs->min_bounds.width * count);
  410.  
  411.     /*
  412.      * Iterate over all character in the input string; only consider characters
  413.      * that exist.
  414.      */
  415.     for (i = 0; i < count; i++, string++) {
  416.     register XCharStruct *cs;
  417.     unsigned int r = (unsigned int) string->byte1;    /* watch for macros */
  418.     unsigned int c = (unsigned int) string->byte2;    /* watch for macros */
  419.  
  420.     if (singlerow) {
  421.         unsigned int ind = ((r << 8) | c);        /* watch for macros */
  422.         CI_GET_CHAR_INFO_1D (fs, ind, def, cs);
  423.     } else {
  424.         CI_GET_CHAR_INFO_2D (fs, r, c, def, cs);
  425.     }
  426.  
  427.     if (cs) width += cs->width;
  428.     }
  429.  
  430.     DBUG_RETURN(width);
  431. }
  432.  
  433.  
  434. /*
  435.  * _XTextHeight16 - compute the height of sequence of XChar2bs.
  436.  */
  437. #if NeedFunctionPrototypes
  438. int _XTextHeight16 (
  439.     XFontStruct *fs,
  440.     _Xconst XChar2b *string,
  441.     int count)
  442. #else
  443. int _XTextHeight16 (fs, string, count)
  444.     XFontStruct *fs;
  445.     XChar2b *string;
  446.     int count;
  447. #endif
  448. {
  449.     DBUG_ENTER("_XTextHeight16")
  450.     int i;                /* iterator */
  451.     Bool singlerow = (fs->max_byte1 == 0);  /* optimization */
  452.     XCharStruct *def;            /* info about default char */
  453.     int height = 0;            /* RETURN value */
  454.  
  455.     if (singlerow) {
  456.     CI_GET_DEFAULT_INFO_1D (fs, def);
  457.     } else {
  458.     CI_GET_DEFAULT_INFO_2D (fs, def);
  459.     }
  460.  
  461.     if (def && (fs->min_bounds.ascent == fs->max_bounds.ascent)
  462.         && (fs->min_bounds.descent == fs->max_bounds.descent))
  463.     DBUG_RETURN((fs->min_bounds.ascent + fs->min_bounds.descent) * count);
  464.  
  465.     /*
  466.      * Iterate over all character in the input string; only consider characters
  467.      * that exist.
  468.      */
  469.     for (i = 0; i < count; i++, string++) {
  470.     register XCharStruct *cs;
  471.     unsigned int r = (unsigned int) string->byte1;    /* watch for macros */
  472.     unsigned int c = (unsigned int) string->byte2;    /* watch for macros */
  473.  
  474.     if (singlerow) {
  475.         unsigned int ind = ((r << 8) | c);        /* watch for macros */
  476.         CI_GET_CHAR_INFO_1D (fs, ind, def, cs);
  477.     } else {
  478.         CI_GET_CHAR_INFO_2D (fs, r, c, def, cs);
  479.     }
  480.  
  481.     if (cs) height += (cs->ascent + cs->descent);
  482.     }
  483.  
  484.     DBUG_RETURN(height);
  485. }
  486.  
  487.