Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

803 lines
23 KiB

  1. /*++
  2. Copyright (c) 1990-1999 Microsoft Corporation
  3. All Rights Reserved
  4. // @@BEGIN_DDKSPLIT
  5. Module Name:
  6. windows\spooler\prtprocs\winprint\text.c
  7. // @@END_DDKSPLIT
  8. Abstract:
  9. Routines to facilitate printing of text jobs.
  10. --*/
  11. #include "local.h"
  12. #define FLAG_CR_STATE 0x1
  13. #define FLAG_TAB_STATE 0x2
  14. #define FLAG_DBCS_SPLIT 0x8
  15. #define FLAG_FF 0x10
  16. #define FLAG_LF 0x20
  17. #define FLAG_CR 0x40
  18. #define FLAG_TRANSLATE_LF 0x80
  19. #define FLAG_TRANSLATE_CR 0x100
  20. const WCHAR gszNoTranslateCRLF[] = L"Winprint_TextNoTranslation";
  21. const WCHAR gszNoTranslateCR[] = L"Winprint_TextNoCRTranslation";
  22. const WCHAR gszTransparency[] = L"Transparency";
  23. /** Prototypes for functions in this file **/
  24. PBYTE
  25. GetTabbedLineFromBuffer(
  26. IN PBYTE pSrcBuffer,
  27. IN PBYTE pSrcBufferEnd,
  28. IN PBYTE pDestBuffer,
  29. IN ULONG CharsPerLine,
  30. IN ULONG TabExpansionSize,
  31. IN ULONG Encoding,
  32. IN OUT PULONG pLength,
  33. IN OUT PULONG pTabBase,
  34. IN OUT PDWORD pfdwFlags
  35. );
  36. /*++
  37. *******************************************************************
  38. P r i n t T e x t J o b
  39. Routine Description:
  40. Prints a text data job.
  41. Arguments:
  42. pData => Data structure for this job
  43. pDocumentName => Name of this document
  44. Return Value:
  45. TRUE if successful
  46. FALSE if failed - GetLastError() will return reason.
  47. *******************************************************************
  48. --*/
  49. BOOL
  50. PrintTextJob(
  51. IN PPRINTPROCESSORDATA pData,
  52. IN LPWSTR pDocumentName)
  53. {
  54. DOCINFO DocInfo;
  55. LOGFONT LogFont;
  56. CHARSETINFO CharSetInfo;
  57. HFONT hOldFont, hFont;
  58. DWORD Copies;
  59. BOOL rc;
  60. DWORD NoRead;
  61. DWORD CurrentLine;
  62. DWORD CurrentCol;
  63. HANDLE hPrinter = NULL;
  64. BYTE *ReadBufferStart = NULL;
  65. PBYTE pLineBuffer = NULL;
  66. PBYTE pReadBuffer = NULL;
  67. PBYTE pReadBufferEnd = NULL;
  68. ULONG CharHeight, CharWidth, CharsPerLine, LinesPerPage;
  69. ULONG PageWidth, PageHeight;
  70. ULONG Length, TabBase;
  71. BOOL ReadAll;
  72. TEXTMETRIC tm;
  73. DWORD fdwFlags;
  74. DWORD Encoding;
  75. DWORD SplitSize;
  76. BOOL ReturnValue = FALSE;
  77. BOOL bAbortDoc = FALSE;
  78. DWORD dwNeeded;
  79. DWORD dwNoTranslate = 0;
  80. DWORD dwNoTranslateCR = 0;
  81. DWORD dwTransparent = 0;
  82. INT iBkMode;
  83. DocInfo.lpszDocName = pData->pDocument; /* Document name */
  84. DocInfo.lpszOutput = NULL; /* Output file */
  85. DocInfo.lpszDatatype = NULL; /* Datatype */
  86. DocInfo.cbSize = sizeof(DOCINFO); /* Size of the structure */
  87. //
  88. // Go figure out the size of the form on the printer. We do this
  89. // by calling GetTextMetrics, which gives us the font size of the
  90. // printer font, then getting the form size and calculating the
  91. // number of characters that will fit. In other cases we treat it as ANSI text.
  92. // Currently the codepage context is fixed to the system default codepage.
  93. //
  94. Encoding = GetACP();
  95. //
  96. // Create FIXED PITCH font and select
  97. //
  98. hOldFont = 0;
  99. ZeroMemory(&CharSetInfo, sizeof(CHARSETINFO));
  100. if (TranslateCharsetInfo((PDWORD)UIntToPtr(Encoding), &CharSetInfo, TCI_SRCCODEPAGE))
  101. {
  102. ZeroMemory(&LogFont, sizeof(LOGFONT));
  103. LogFont.lfWeight = 400;
  104. LogFont.lfCharSet = (BYTE)CharSetInfo.ciCharset;
  105. LogFont.lfPitchAndFamily = FIXED_PITCH;
  106. hFont = CreateFontIndirect(&LogFont);
  107. hOldFont = SelectObject(pData->hDC, hFont);
  108. }
  109. if (!GetTextMetrics(pData->hDC, &tm)) {
  110. // Essential text processing computation failed
  111. goto Done;
  112. }
  113. CharHeight = tm.tmHeight + tm.tmExternalLeading;
  114. CharWidth = tm.tmAveCharWidth;
  115. if (!CharWidth || !CharHeight) {
  116. // Essential text processing computation failed
  117. goto Done;
  118. }
  119. //
  120. // Calculate most fittable characters' number to one line.
  121. //
  122. PageWidth = GetDeviceCaps(pData->hDC, DESKTOPHORZRES);
  123. PageHeight = GetDeviceCaps(pData->hDC, DESKTOPVERTRES);
  124. CharsPerLine = PageWidth / CharWidth;
  125. LinesPerPage = PageHeight / CharHeight;
  126. if (!CharsPerLine || !LinesPerPage) {
  127. // Essential text processing computation failed
  128. goto Done;
  129. }
  130. /** Allocate a buffer for one line of text **/
  131. pLineBuffer = AllocSplMem(CharsPerLine + 5);
  132. if (!pLineBuffer) {
  133. goto Done;
  134. }
  135. /** Let the printer know we are starting a new document **/
  136. if (!StartDoc(pData->hDC, (LPDOCINFO)&DocInfo)) {
  137. goto Done;
  138. }
  139. ReadBufferStart = AllocSplMem(READ_BUFFER_SIZE);
  140. if (!ReadBufferStart) {
  141. goto Done;
  142. }
  143. /** Print the data pData->Copies times **/
  144. Copies = pData->Copies;
  145. while (Copies--) {
  146. /**
  147. Loop, getting data and sending it to the printer. This also
  148. takes care of pausing and cancelling print jobs by checking
  149. the processor's status flags while printing. The way we do
  150. this is to read in some data from the printer. We will then
  151. pull data, one tabbed line at a time from there and print
  152. it. If the last bit of data in the buffer does not make up
  153. a whole line, we call GetTabbedLineFromBuffer() with a non-
  154. zero Length, which indicates that there are chars left
  155. from the previous read.
  156. **/
  157. TabBase = 0;
  158. Length = 0;
  159. fdwFlags = FLAG_TRANSLATE_CR | FLAG_TRANSLATE_LF;
  160. CurrentLine = 0;
  161. CurrentCol = 0;
  162. /**
  163. Open the printer. If it fails, return. This also sets up the
  164. pointer for the ReadPrinter calls.
  165. **/
  166. if (!OpenPrinter(pDocumentName, &hPrinter, NULL)) {
  167. hPrinter = NULL;
  168. bAbortDoc = TRUE;
  169. goto Done;
  170. }
  171. //
  172. // Call GetPrinterData to see if the queue wants no LF/CR processing.
  173. //
  174. if( GetPrinterData( hPrinter,
  175. (LPWSTR)gszNoTranslateCRLF,
  176. NULL,
  177. (PBYTE)&dwNoTranslate,
  178. sizeof( dwNoTranslate ),
  179. &dwNeeded ) == ERROR_SUCCESS ){
  180. if( dwNoTranslate ){
  181. fdwFlags &= ~( FLAG_TRANSLATE_CR | FLAG_TRANSLATE_LF );
  182. }
  183. }
  184. //
  185. // Call GetPrinterData to see if the queue wants no CR processing.
  186. //
  187. if( GetPrinterData( hPrinter,
  188. (LPWSTR)gszNoTranslateCR,
  189. NULL,
  190. (PBYTE)&dwNoTranslateCR,
  191. sizeof( dwNoTranslateCR ),
  192. &dwNeeded ) == ERROR_SUCCESS ){
  193. if( dwNoTranslateCR ){
  194. fdwFlags &= ~FLAG_TRANSLATE_CR;
  195. if( GetPrinterData( hPrinter,
  196. (LPWSTR)gszTransparency,
  197. NULL,
  198. (PBYTE)&dwTransparent,
  199. sizeof( dwTransparent ),
  200. &dwNeeded ) == ERROR_SUCCESS ){
  201. if( dwTransparent ){
  202. iBkMode = SetBkMode( pData->hDC, TRANSPARENT );
  203. }
  204. }
  205. }
  206. }
  207. if (StartPage(pData->hDC) == SP_ERROR) {
  208. bAbortDoc = TRUE;
  209. goto Done;
  210. }
  211. /** ReadAll indicates if we are on the last line of the file **/
  212. ReadAll = FALSE;
  213. /**
  214. This next do loop continues until we have read all of the
  215. data for the print job.
  216. **/
  217. do {
  218. if (fdwFlags & FLAG_DBCS_SPLIT) {
  219. SplitSize = (DWORD)(pReadBufferEnd - pReadBuffer);
  220. memcpy(ReadBufferStart, pReadBuffer, SplitSize);
  221. fdwFlags &= ~FLAG_DBCS_SPLIT;
  222. }
  223. else {
  224. SplitSize = 0;
  225. }
  226. rc = ReadPrinter(hPrinter,
  227. (ReadBufferStart + SplitSize),
  228. (READ_BUFFER_SIZE - SplitSize),
  229. &NoRead);
  230. if (!rc || !NoRead) {
  231. ReadAll = TRUE;
  232. } else {
  233. /** Pick up a pointer to the end of the data **/
  234. pReadBuffer = ReadBufferStart;
  235. pReadBufferEnd = ReadBufferStart + SplitSize + NoRead;
  236. }
  237. /**
  238. This loop will process all the data that we have
  239. just read from the printer.
  240. **/
  241. do {
  242. if (!ReadAll) {
  243. /**
  244. Length on entry holds the length of any
  245. residual chars from the last line that we couldn't
  246. print out because we ran out of characters on
  247. the ReadPrinter buffer.
  248. **/
  249. pReadBuffer = GetTabbedLineFromBuffer(
  250. pReadBuffer,
  251. pReadBufferEnd,
  252. pLineBuffer,
  253. CharsPerLine - CurrentCol,
  254. pData->TabSize,
  255. Encoding,
  256. &Length,
  257. &TabBase,
  258. &fdwFlags );
  259. /**
  260. If pReadBuffer == NULL, then we have
  261. exhausted the read buffer and we need to ReadPrinter
  262. again and save the last line chars. Length holds
  263. the number of characters on this partial line,
  264. so the next time we call ReadPrinter we will
  265. pickup where we left off.
  266. The only time we'll get residual chars is if:
  267. 1. The last line ends w/o ff/lf/cr ("Hello\EOF")
  268. In this case we should TextOutA the last line
  269. and then quit.
  270. (In this case, don't break here; go ahead and
  271. print, then we'll break out below in the do..while.)
  272. 2. The ReadPrinter last byte is in the middle of a line.
  273. Here we should read the next chunk and add the
  274. new characters at the end of the chars we just read.
  275. (In this case, we should break and leave Length
  276. as it is so we will read again and append to the
  277. buffer, beginning at Length.)
  278. **/
  279. if (!pReadBuffer || (fdwFlags & FLAG_DBCS_SPLIT))
  280. break;
  281. }
  282. /** If the print processor is paused, wait for it to be resumed **/
  283. if (pData->fsStatus & PRINTPROCESSOR_PAUSED) {
  284. WaitForSingleObject(pData->semPaused, INFINITE);
  285. }
  286. /** If the job has been aborted, clean up and leave **/
  287. if (pData->fsStatus & PRINTPROCESSOR_ABORTED) {
  288. ReturnValue = TRUE;
  289. bAbortDoc = TRUE;
  290. goto Done;
  291. }
  292. /** Write the data to the printer **/
  293. /** Make sure Length is not zero **/
  294. /** TextOut will fail if Length == 0 **/
  295. if (Length) {
  296. /**
  297. We may have a number of newlines pending, that
  298. may push us to the next page (or even next-next
  299. page).
  300. **/
  301. while (CurrentLine >= LinesPerPage) {
  302. /**
  303. We need a new page; always defer this to the
  304. last second to prevent extra pages from coming out.
  305. **/
  306. if (EndPage(pData->hDC) == SP_ERROR ||
  307. StartPage(pData->hDC) == SP_ERROR) {
  308. bAbortDoc = TRUE;
  309. goto Done;
  310. }
  311. CurrentLine -= LinesPerPage;
  312. }
  313. if (TextOutA(pData->hDC,
  314. CurrentCol * CharWidth,
  315. CurrentLine * CharHeight,
  316. pLineBuffer,
  317. Length) == FALSE) {
  318. ODS(("TextOut() failed\n"));
  319. bAbortDoc = TRUE;
  320. goto Done;
  321. }
  322. CurrentCol += Length;
  323. }
  324. /**
  325. Even if the length is zero, increment the line.
  326. Should happen when the character is only 0x0D or 0x0A.
  327. **/
  328. if (fdwFlags & FLAG_CR) {
  329. CurrentCol=0;
  330. fdwFlags &= ~FLAG_CR;
  331. }
  332. if (fdwFlags & FLAG_LF) {
  333. CurrentLine++;
  334. fdwFlags &= ~FLAG_LF;
  335. }
  336. /**
  337. We need a new page. Set the current line to the
  338. end of the page. We could do a End/StartPage
  339. sequence, but this may cause a blank page to get
  340. ejected.
  341. Note: this code will avoid printing out pages that
  342. consist of formfeeds only (if you have a page with a
  343. space in it, that counts as text).
  344. **/
  345. if (fdwFlags & FLAG_FF) {
  346. CurrentLine = LinesPerPage;
  347. CurrentCol = 0;
  348. fdwFlags &= ~FLAG_FF;
  349. }
  350. /**
  351. We have done the text out, so these characters have
  352. been successfully printed. Zero out Length
  353. so these characters won't be printed again
  354. **/
  355. Length = 0;
  356. /**
  357. We only terminate this loop if we run out of chars
  358. or we run out of read buffer.
  359. **/
  360. } while (pReadBuffer && pReadBuffer != pReadBufferEnd);
  361. /** Keep going until we get the last line **/
  362. } while (!ReadAll);
  363. if (EndPage(pData->hDC) == SP_ERROR) {
  364. bAbortDoc = TRUE;
  365. goto Done;
  366. }
  367. /**
  368. Close the printer - we open/close the printer for each
  369. copy so the data pointer will rewind.
  370. **/
  371. ClosePrinter(hPrinter);
  372. hPrinter = NULL;
  373. } /* While copies to print */
  374. /** Let the printer know that we are done printing **/
  375. EndDoc(pData->hDC);
  376. ReturnValue = TRUE;
  377. Done:
  378. if (dwTransparent)
  379. SetBkMode( pData->hDC, iBkMode );
  380. if (hPrinter)
  381. ClosePrinter(hPrinter);
  382. if (bAbortDoc)
  383. AbortDoc(pData->hDC);
  384. if (pLineBuffer)
  385. FreeSplMem(pLineBuffer);
  386. if (hOldFont)
  387. {
  388. SelectObject(pData->hDC, hOldFont);
  389. DeleteObject(hFont);
  390. }
  391. if (ReadBufferStart)
  392. {
  393. FreeSplMem(ReadBufferStart);
  394. }
  395. return ReturnValue;
  396. }
  397. /*++
  398. *******************************************************************
  399. G e t T a b b e d L i n e F r o m B u f f e r
  400. Routine Description:
  401. This routine, given a buffer of text, will pull out a
  402. line of tab-expanded text. This is used for tab
  403. expansion of text data jobs.
  404. Arguments:
  405. pSrcBuffer => Start of source buffer.
  406. pSrcBufferEnd => End of source buffer
  407. pDestBuffer => Start of destination buffer
  408. CharsPerLine => Number of characters on a line
  409. TabExpansionSize=> Number of spaces in a tab
  410. Encoding => Code page
  411. pLength => Length of chars from prev line, rets current
  412. pTabBase => New 0 offset for tabbing
  413. pfdwFlags => State
  414. Return Value:
  415. PBYTE => Place left off in the source buffer. This should
  416. be passed in on the next call. If we ran out of
  417. data in the source, this will be unchanged.
  418. *******************************************************************
  419. --*/
  420. PBYTE
  421. GetTabbedLineFromBuffer(
  422. IN PBYTE pSrcBuffer,
  423. IN PBYTE pSrcBufferEnd,
  424. IN PBYTE pDestBuffer,
  425. IN ULONG CharsPerLine,
  426. IN ULONG TabExpansionSize,
  427. IN ULONG Encoding,
  428. IN OUT PULONG pLength,
  429. IN OUT PULONG pTabBase,
  430. IN OUT PDWORD pfdwFlags
  431. )
  432. {
  433. ULONG current_pos;
  434. ULONG expand, i;
  435. ULONG TabBase = *pTabBase;
  436. ULONG TabBaseLeft = TabExpansionSize-TabBase;
  437. PBYTE pDestBufferEnd = pDestBuffer + CharsPerLine;
  438. /**
  439. If the tab pushed us past the end of the last line, then we need to
  440. add it back to the next one.
  441. **/
  442. if (TabBase && ( *pfdwFlags & FLAG_TAB_STATE )) {
  443. current_pos = 0;
  444. i=TabBase;
  445. while (i-- && (pDestBuffer < pDestBufferEnd)) {
  446. *pDestBuffer++ = ' ';
  447. current_pos++;
  448. }
  449. /**
  450. If we ran out of room again, return. This means that
  451. the tab expansion size is greater than we can fit on
  452. one line.
  453. **/
  454. if (pDestBuffer >= pDestBufferEnd) {
  455. *pLength = current_pos;
  456. pTabBase -= CharsPerLine;
  457. //
  458. // We need to move to the next line.
  459. //
  460. *pfdwFlags |= FLAG_LF | FLAG_CR;
  461. return pSrcBuffer;
  462. }
  463. *pfdwFlags &= ~FLAG_TAB_STATE;
  464. } else {
  465. /** We may have some chars from the previous ReadPrinter **/
  466. current_pos = *pLength;
  467. pDestBuffer += current_pos;
  468. }
  469. while (pSrcBuffer < pSrcBufferEnd) {
  470. /** Now process other chars **/
  471. switch (*pSrcBuffer) {
  472. case 0x0C:
  473. /** Found a FF. Quit and indicate we need to start a new page **/
  474. *pTabBase = 0;
  475. *pfdwFlags |= FLAG_FF;
  476. *pfdwFlags &= ~FLAG_CR_STATE;
  477. pSrcBuffer++;
  478. break;
  479. case '\t':
  480. *pfdwFlags &= ~FLAG_CR_STATE;
  481. /**
  482. Handle TAB case. If we are really out of buffer,
  483. then defer now so that the tab will be saved for
  484. the next line.
  485. **/
  486. if (pDestBuffer >= pDestBufferEnd) {
  487. goto ShiftTab;
  488. }
  489. pSrcBuffer++;
  490. /** Figure out how far to expand the tabs **/
  491. expand = TabExpansionSize -
  492. (current_pos + TabBaseLeft) % TabExpansionSize;
  493. /** Expand the tabs **/
  494. for (i = 0; (i < expand) && (pDestBuffer < pDestBufferEnd); i++) {
  495. *pDestBuffer++ = ' ';
  496. }
  497. /**
  498. If we reached the end of our dest buffer,
  499. return and set the number of spaces we have left.
  500. **/
  501. if (pDestBuffer >= pDestBufferEnd) {
  502. *pfdwFlags |= FLAG_TAB_STATE;
  503. goto ShiftTab;
  504. }
  505. /** Update our position counter **/
  506. current_pos += expand;
  507. continue;
  508. case 0x0A:
  509. pSrcBuffer++;
  510. /** If the last char was a CR, ignore this guy **/
  511. if (*pfdwFlags & FLAG_CR_STATE) {
  512. *pfdwFlags &= ~FLAG_CR_STATE;
  513. //
  514. // We are translating CRLF, so if we saw a CR
  515. // immediately before this, then don't do anything.
  516. //
  517. continue;
  518. }
  519. if( *pfdwFlags & FLAG_TRANSLATE_LF ){
  520. //
  521. // If we are translating, then treat a LF as a CRLF pair.
  522. //
  523. *pfdwFlags |= FLAG_LF | FLAG_CR;
  524. /** Found a linefeed. That's it for this line. **/
  525. *pTabBase = 0;
  526. } else {
  527. *pfdwFlags |= FLAG_LF;
  528. }
  529. break;
  530. case 0x0D:
  531. /** Found a carriage return. That's it for this line. **/
  532. *pTabBase = 0;
  533. pSrcBuffer++;
  534. if (*pfdwFlags & FLAG_TRANSLATE_CR) {
  535. //
  536. // If we are translating CRLF, then make the newline
  537. // occur now. This handles the case where we have a
  538. // CR all by itself. Also set the CR flag so if there
  539. // happens to be a LF immediately after this, we don't
  540. // move down another line.
  541. //
  542. *pfdwFlags |= FLAG_CR_STATE | FLAG_LF | FLAG_CR;
  543. } else {
  544. *pfdwFlags |= FLAG_CR;
  545. }
  546. break;
  547. default:
  548. /** Not tab or carriage return, must be simply data **/
  549. *pfdwFlags &= ~FLAG_CR_STATE;
  550. //
  551. // We always check before we are adding a character
  552. // (instead of after) since we may be at the end of a line,
  553. // but we can still process chars like 0x0d 0x0a.
  554. // This happens in MS-DOS printscreen.
  555. //
  556. if (pDestBuffer >= pDestBufferEnd ||
  557. (pDestBuffer + 1 >= pDestBufferEnd) &&
  558. IsDBCSLeadByteEx(Encoding, *pSrcBuffer)) {
  559. ShiftTab:
  560. //
  561. // We must shift the tab over since we are on the
  562. // same line.
  563. //
  564. *pTabBase = (*pTabBase + TabExpansionSize -
  565. (CharsPerLine % TabExpansionSize))
  566. % TabExpansionSize;
  567. *pfdwFlags |= FLAG_LF | FLAG_CR;
  568. break;
  569. }
  570. if (IsDBCSLeadByteEx(Encoding, *pSrcBuffer)) {
  571. // Check if we have trail byte also.
  572. if (pSrcBuffer + 1 >= pSrcBufferEnd) {
  573. *pfdwFlags |= FLAG_DBCS_SPLIT;
  574. break;
  575. }
  576. // Advance source pointer (for lead byte).
  577. *pDestBuffer++ = *pSrcBuffer++;
  578. current_pos++;
  579. }
  580. *pDestBuffer++ = *pSrcBuffer++;
  581. current_pos++;
  582. continue;
  583. }
  584. *pLength = current_pos;
  585. return pSrcBuffer;
  586. }
  587. /** We ran out of source buffer before getting to the EOL **/
  588. *pLength = current_pos;
  589. return NULL;
  590. }