home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 5 Edit
/
05-Edit.zip
/
NOTEPAD2.ZIP
/
MPIX.C
< prev
next >
Wrap
C/C++ Source or Header
|
1989-02-08
|
5KB
|
167 lines
/*
* mpix.c -- Line index <-> pixel mappings
*
* Created by Microsoft Corporation, 1989
*/
#define INCL_WIN
#include <os2.h>
#include "mle.h"
#include "mtypes.h"
#include "mfuncs.h"
/***********************************************************************
* Forward declarations
***********************************************************************/
/* public function
*
* PIX TabWidth(PED ped, PIX pix)
*
* Returns the width of a tab, in pixels, which is assumed to happen at
* pix pixels into the display.
*/
public PIX TabWidth(PED ped, PIX pix)
{
PIX pixTab = ped->TabSize * ped->AveCharWidth;
return(pixTab - (pix % pixTab));
}
/* public function
*
* PIX PixOfChar(PED ped, PIX pix, CHAR ch)
*
* Returns the width, in pixels, of a given character, being drawn at pix
*/
public PIX PixOfChar(PED ped, PIX pix, CHAR ch)
{
return((ch == '\t')
? TabWidth(ped, pix)
: (PIX) ped->pcwt[(USHORT)ch]);
}
/* public function
*
* PIX SetTabWidth(PED ped, PIX TabSize)
*
* Sets the size of a tab to be TabSize ave char widths.
*/
public PIX SetTabWidth(PED ped, PIX TabSize)
{
return(ped->TabSize = TabSize);
}
/* public function
*
* PIX QueryTabWidth(PED ped)
*
* Returns the size of a tab in ave char widths.
*/
public PIX QueryTabWidth(PED ped)
{
return(ped->TabSize);
}
/* public function
*
* PIX PixFromText(PED ped, PPPR pppr, POFFSET poff, OFFSET cch, PIX pixStart)
*
* Finds the final pixel-index of a run of text, given that the run starts at
* *pppr / *poff in the text and is being drawn at pixStart. Updates the
* piece and offset values to point to the next piece/offset that would be
* scanned.
*/
public PIX PixFromText(PED ped, PPPR pppr, POFFSET poff, OFFSET cch, PIX pixStart)
{
register CHAR ch;
register LONG FAR *pcwt = ped->pcwt;
PIX pix = pixStart; // Returned pixel index
ch = CharOfPointer(*pppr, *poff);
while (cch) {
if (!fIsMarker(*pppr)) {
pix += PixOfChar(ped, pix, ch);
cch--;
}
ch = AdvancePointer(pppr, poff);
}
return(pix);
}
/* public function
*
* IPT TextFromPix(PED ped, PPPR pppr, POFFSET poff, PPIX ppix,
* USHORT usMode)
*
* Given the pixel index *ppix into the line beginning at *pppr/*poff,
* this function finds the ppr/off and distance in characters of a
* corresponding insertion point. If mode is ROUND_DOWN or
* ROUND_UP, it rounds down or up to the nearest insertion point; and if
* mode is ROUND_CLOSEST, it chooses the closest one on either side.
*
* The function returns the number of characters, which is guaranteed to be in
* [0, line length]. It also sets *ppix to the
* actual pixel index of that insertion point (which the original *ppix
* approximated). The function stops when it hits a line break.
*/
public IPT TextFromPix(PED ped, PPPR pppr, POFFSET poff,
PPIX ppix, USHORT usMode)
{
PIX pixGoal = *ppix;
PIX pix = 0;
PIX pixCh;
CHAR ch;
IPT cch = 0;
PPR pprSave = *pppr;
OFFSET offSave = *poff;
ch = CharOfPointer(*pppr,*poff);
while (pix < pixGoal) {
if (fIsMarker(*pppr)) {
if ((((PMR)(*pppr))->fFlags&MARKER_LINE)) {
break;
}
} else {
pprSave = *pppr;
offSave = *poff;
pix += pixCh = PixOfChar(ped, pix, ch);
cch++;
}
ch = AdvancePointer(pppr, poff);
}
if (pix < pixGoal) { // reached end of line
*ppix = pix;
*pppr = pprSave;
*poff = offSave;
} else if (pix != pixGoal) { // pixel is in mid-char
// if *ppix was originally > 0, we must have iterated at least
// once; and if it was originally 0, then it would still be,
// and we would have failed the above test
// convert to ROUND_UP or ROUND_DOWN
if (usMode == ROUND_CLOSEST) {
usMode = ((2*(pix-pixGoal))>pixCh)
? ROUND_DOWN
: ROUND_UP;
}
if (usMode==ROUND_UP) {
// move forward to the end of the character
*ppix = pix;
} else {
// move back to the beginning of the character
*ppix = pix - pixCh;
*pppr = pprSave;
*poff = offSave;
cch--;
}
}
return(cch);
}