home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / NOTEPAD2.ZIP / MPIX.C < prev    next >
C/C++ Source or Header  |  1989-02-08  |  5KB  |  167 lines

  1. /*
  2.  * mpix.c -- Line index <-> pixel mappings
  3.  *
  4.  * Created by Microsoft Corporation, 1989
  5.  */
  6.  
  7. #define INCL_WIN
  8. #include <os2.h>
  9. #include "mle.h"
  10. #include "mtypes.h"
  11. #include "mfuncs.h"
  12.  
  13. /***********************************************************************
  14. * Forward declarations
  15. ***********************************************************************/
  16.  
  17.  
  18. /* public function
  19.  *
  20.  * PIX TabWidth(PED ped, PIX pix)
  21.  *
  22.  * Returns the width of a tab, in pixels, which is assumed to happen at
  23.  * pix pixels into the display.
  24.  */
  25. public PIX TabWidth(PED ped, PIX pix)
  26. {
  27.         PIX pixTab = ped->TabSize * ped->AveCharWidth;
  28.         return(pixTab - (pix % pixTab));
  29. }
  30.  
  31. /* public function
  32.  *
  33.  * PIX PixOfChar(PED ped, PIX pix, CHAR ch)
  34.  *
  35.  * Returns the width, in pixels, of a given character, being drawn at pix
  36.  */
  37. public PIX PixOfChar(PED ped, PIX pix, CHAR ch)
  38. {
  39.     return((ch == '\t')
  40.             ? TabWidth(ped, pix)
  41.             : (PIX) ped->pcwt[(USHORT)ch]);
  42. }
  43.  
  44. /* public function
  45.  *
  46.  * PIX SetTabWidth(PED ped, PIX TabSize)
  47.  *
  48.  * Sets the size of a tab to be TabSize ave char widths.
  49.  */
  50. public PIX SetTabWidth(PED ped, PIX TabSize)
  51. {
  52.         return(ped->TabSize = TabSize);
  53. }
  54.  
  55. /* public function
  56.  *
  57.  * PIX QueryTabWidth(PED ped)
  58.  *
  59.  * Returns the size of a tab in ave char widths.
  60.  */
  61. public PIX QueryTabWidth(PED ped)
  62. {
  63.         return(ped->TabSize);
  64. }
  65.  
  66. /* public function
  67.  *
  68.  * PIX PixFromText(PED ped, PPPR pppr, POFFSET poff, OFFSET cch, PIX pixStart)
  69.  *
  70.  * Finds the final pixel-index of a run of text, given that the run starts at
  71.  * *pppr / *poff in the text and is being drawn at pixStart.  Updates the
  72.  * piece and offset values to point to the next piece/offset that would be
  73.  * scanned.
  74.  */
  75.  
  76. public PIX PixFromText(PED ped, PPPR pppr, POFFSET poff, OFFSET cch, PIX pixStart)
  77. {
  78.         register CHAR ch;
  79.         register LONG FAR *pcwt = ped->pcwt;
  80.         PIX pix = pixStart;            // Returned pixel index
  81.  
  82.         ch = CharOfPointer(*pppr, *poff);
  83.         while (cch) {
  84.                 if (!fIsMarker(*pppr)) {
  85.                         pix += PixOfChar(ped, pix, ch);
  86.                         cch--;
  87.                 }
  88.                 ch = AdvancePointer(pppr, poff);
  89.         }
  90.  
  91.         return(pix);
  92. }
  93.  
  94.  
  95.  
  96. /* public function
  97.  *
  98.  * IPT TextFromPix(PED ped, PPPR pppr, POFFSET poff, PPIX ppix,
  99.  *                 USHORT usMode)
  100.  *
  101.  * Given the pixel index *ppix into the line beginning at *pppr/*poff,
  102.  * this function finds the ppr/off and distance in characters of a
  103.  * corresponding insertion point.  If mode is ROUND_DOWN or
  104.  * ROUND_UP, it rounds down or up to the nearest insertion point; and if
  105.  * mode is ROUND_CLOSEST, it chooses the closest one on either side.
  106.  *
  107.  * The function returns the number of characters, which is guaranteed to be in
  108.  * [0, line length].  It also sets *ppix to the
  109.  * actual pixel index of that insertion point (which the original *ppix
  110.  * approximated).  The function stops when it hits a line break.
  111.  */
  112. public IPT TextFromPix(PED ped, PPPR pppr, POFFSET poff,
  113.                         PPIX ppix, USHORT usMode)
  114. {
  115.         PIX pixGoal = *ppix;
  116.         PIX pix = 0;
  117.         PIX pixCh;
  118.         CHAR ch;
  119.         IPT cch = 0;
  120.         PPR pprSave = *pppr;
  121.         OFFSET offSave = *poff;
  122.  
  123.         ch = CharOfPointer(*pppr,*poff);
  124.         while (pix < pixGoal) {
  125.                 if (fIsMarker(*pppr)) {
  126.                         if ((((PMR)(*pppr))->fFlags&MARKER_LINE)) {
  127.                                 break;
  128.                         }
  129.                 } else {
  130.                         pprSave = *pppr;
  131.                         offSave = *poff;
  132.                         pix += pixCh = PixOfChar(ped, pix, ch);
  133.                         cch++;
  134.                 }
  135.                 ch = AdvancePointer(pppr, poff);
  136.         }
  137.  
  138.         if (pix < pixGoal) {    // reached end of line
  139.                 *ppix = pix;
  140.                 *pppr = pprSave;
  141.                 *poff = offSave;
  142.         } else if (pix != pixGoal) {  // pixel is in mid-char
  143.                 // if *ppix was originally > 0, we must have iterated at least
  144.                 // once; and if it was originally 0, then it would still be,
  145.                 // and we would have failed the above test
  146.  
  147.                 // convert to ROUND_UP or ROUND_DOWN
  148.                 if (usMode == ROUND_CLOSEST) {
  149.                         usMode = ((2*(pix-pixGoal))>pixCh)
  150.                                 ? ROUND_DOWN
  151.                                 : ROUND_UP;
  152.                 }
  153.  
  154.                 if (usMode==ROUND_UP) {
  155.                         // move forward to the end of the character
  156.                         *ppix = pix;
  157.                 } else {
  158.                         // move back to the beginning of the character
  159.                         *ppix = pix - pixCh;
  160.                         *pppr = pprSave;
  161.                         *poff = offSave;
  162.                         cch--;
  163.                 }
  164.         }
  165.         return(cch);
  166. }
  167.