summaryrefslogblamecommitdiffstats
path: root/private/sdktools/fontedit/fontload.c
blob: 9b3cea98307a96b97c2260340b75cf212a98dc52 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971


















































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                  
#include "windows.h"
#include <port1632.h>
#include "fontedit.h"
#include "fcntl.h"
#include "memory.h"
#include "stdio.h"
#include "commdlg.h"


/****************************************************************************/
/*              Shared Variables                                            */
/****************************************************************************/
extern CHAR *PASCAL VerifyHeaderContents();/* checks integrity of header */

extern FontHeaderType font;             /* Structure of Font File Header */
extern LONG lSizeOfOldGlyph20;          /* Old packed 2.0 glyph info size. */
extern LONG lSizeOfOldGlyph30;     		/* Old packed 3.0 glyph info size. */
extern LONG lSizeOfOldFontHeader;       /* Old packed font header size. */
extern LONG lSizeOfOldFontHeader30;     /* Old 3.0 packed font header size. */
extern CHAR szFaceName[];               /* Face Name of Font */
extern DWORD offsets[];                 /* Offsets Table */

extern BOOL fReadOnly;
extern BOOL fChanged;                   /* Note if we did anything */
extern BOOL fLoaded;                    /* Set if a font is loaded */
extern INT iFontFormatPrev;             /* Set to the id of prev.font format */
extern INT iFontFormat;             /* Set to the id of current font format */

extern HWND hFont;                      /* Handle to Show window */
extern HWND hBox;                       /* Handle to Edit Window */
extern HDC hMemDC;                      /* Handle to Memory Display Context */
extern HBITMAP hBitmap;                 /* Handle to our work bit map */
extern CHAR matBox [wBoxLim] [kBoxLim]; /* array to hold Box */

extern CHAR *vrgsz[];                   /* string table */
extern OFSTRUCT ofstrFile;
extern BOOL NewFile;                    /* flag indicating if file was opened
                                           by selecting NEW on menu */
FontHeaderType fontBuffer;          /* temporary buffer of Font File Header */
WORD cHeader, cTable;
HFILE nNewFile;                          /* NT file handle */


/****************************************************************************/
/*              Local Variables                                             */
/****************************************************************************/

DWORD Proport(DWORD, DWORD, DWORD, DWORD);  /* Reproportions a value */

CHAR HUGE_T *lpFontBody = NULL;  /* Pointer to Font Body */
CHAR HUGE_T *lpWork = NULL;      /* Pointer to Work Area */
HDC hNewMemDC;
HDC hBoxMemDC;
HBITMAP hNewBitmap;
HBITMAP hBoxBitmap;
HCURSOR hOldCursor = NULL;      /* Holds Arrow while we show Hourglass */
HCURSOR hHourGlass = NULL;
DWORD oldMode;                   /* For StretchBltMode */
BYTE HUGE_T *lp1, HUGE_T *lp2;    /* Pointers to bitmaps in format N */

FontHeader30  font30;

#define ALPHA_CNT        26
#define TOTAL_WEIGHTS  1000
#define FSF_FIXED             0x0001
#define FSF_PROPORTIONAL      0x0002

SHORT widthweights[ALPHA_CNT + 1] =
        {
                 64,        /* a
                 14,        /* b     */
                 27,        /* c     */
                 35,        /* d     */
                100,        /* e     */
                 20,        /* f     */
                 14,        /* g     */
                 42,        /* h     */
                 63,        /* i     */
                 3 ,        /* j     */
                 6 ,        /* k     */
                 35,        /* l     */
                 20,        /* m     */
                 56,        /* n     */
                 56,        /* o     */
                 17,        /* p     */
                 4 ,        /* q     */
                 49,        /* r     */
                 56,        /* s     */
                 71,        /* t     */
                 31,        /* u     */
                 10,        /* v     */
                 18,        /* w     */
                 3 ,        /* x     */
                 18,        /* y     */
                 2 ,        /* z     */
                166,        /* space, must be last, to use with the following
                               code */
        };

/****************************************************************************/
/*              Local Functions                                             */
/****************************************************************************/

VOID NewAverage(VOID);
BOOL GetNewMap(DWORD, DWORD);
VOID UseNewMap(VOID);
VOID ShrinkFont(DWORD, DWORD);

/****************************************************************************
 * WORD ConvertToBitmapFormat(width, phase, height)
 *
 * purpose  : Takes a part of the font file and converts it to a string of
 *            bytes for a single scan of bitmap for a character.
 *
 * params   : WORD width : width of the character in pixels(bits)
 *            WORD phase : current deviation from byte alignment (pixels)
 *            WORD height: height of character(pixels)
 *
 * returns  : WORD phase : new deviation from byte alignment (pixels)
 *
 * side effects : modifies pointers lp1 and lp2 (pointing to work space and
 *                font body respectively)
 *
 ****************************************************************************/
DWORD PASCAL
ConvertToBitmapFormat(
     DWORD width,     /* width of the character in pixels(bits) */
     DWORD phase,     /* current deviation from byte alignment (pixels) */
     DWORD height     /* height of character(pixels) */
     )
{
      INT w;
      WORD j;

      /* in the font file format characters are stored consecutively in
         column-major ordering (columns of char 1 followed by columns of char
         2 ... etc.). lp2 points to start of columns of current character.
         lp1 points to start of row of bitmap for character. */

      for (w = width; w > 0; w -= 8){
          if (phase == 0){
          /*  easy case */
              *lp1++ = *lp2;

              if (w < 8)
                  phase = w;
          }
          else{

              --lp1;
              j = (WORD)*lp1;
              j <<= 8;
              j |= (((WORD)*lp2) << (8 - phase));
              *lp1++ = (BYTE)(j >> 8);
              *lp1++ = (BYTE)j;
              if (w < 8){
                  phase += w;
                  if (phase <= 8)
                      lp1--;          /* back up pointer */
                  phase &= 7;
              }
          }
          lp2 += height;          /* move to next column */
      }
      return phase;
}

/****************************************************************************
 * char * VerifyTableContents()
 *
 * purpose  : scan the offsets table of file just read and check if
 *            width and offsets lie within limits
 *
 * params    : none
 *
 * returns   : char * szError : ptr to Error message if table not OK
 *                              NULL otherwise
 *
 * side effects : none
 *
 ****************************************************************************/
CHAR * PASCAL
VerifyTableContents(
    VOID
    )
{
	 PBYTE		  pjGlyphData;
     GLYPHINFO_20 gi2T20;        /* temp ptr to a 2.0 style table*/
     GLYPHINFO_30 gi3T30;        /* temp ptr to a 2.0 style table*/
     INT i;

     /* separate loops written for the 2.0 and 3.0 font processing because
        a single loop would involve too many checks (of font type) within
        the loop and slow down processing  */
     if (iFontFormat == ID_FORMAT2){
         pjGlyphData = (lpFontBody +1);

		/* view table as 2.0 table */

         /* check that each and every width and offset lie within limits */

         for (i=0; i < (fontBuffer.LastChar - fontBuffer.FirstChar + 1); i++) {

			  vGlyphInfo20FromBuffer (pjGlyphData, &gi2T20);

              if (gi2T20.GIwidth > 64)
                  return vszTableWidthsBad;

              if ((gi2T20.GIoffset > (UINT)WORD_LIMIT) ||
                  (gi2T20.GIoffset < 0) )
                  return vszTableOffsetsBad;

			  pjGlyphData += lSizeOfOldGlyph20;
         }
     }
     else{

         pjGlyphData = lpFontBody;

            /* view table as 3.0 table */
         /* check that each and every width and offset lie within limits */

         for (i=0; i< (fontBuffer.LastChar - fontBuffer.FirstChar +1); i++) {

			  vGlyphInfo30FromBuffer (pjGlyphData, &gi3T30);

              if (gi3T30.GIwidth > 64)
                  return vszTableWidthsBad;

              if (gi3T30.GIoffset < (DWORD)0)
                  return vszTableOffsetsBad;

			  pjGlyphData += lSizeOfOldGlyph30;
         }
     }
     return NULL;
}

/****************************************************************************
 * char * FontLoad()
 *
 * purpose: reads in the specified font file and creates a work bitmap for
 *          the editor. Also creates a local bitmap-offset table for easy
 *          access to the characters in the bitmap
 *
 * params : pszFileName - File Name to open.
 *
 * returns: ptr to NULL string if Open goes off OK
 *          ptr to error message string otherwise
 *
 * side effects: lots
 *
 ****************************************************************************/
CHAR *
FontLoad(
    CHAR 		*pszFileName,
	OFSTRUCT	*pofsReOpenInfo
    )
{

    CHAR HUGE_T *lpFontBodySav = NULL;  /* local pointer to font body */
    DWORD len, fontlen;               /* length of font body (bytes)*/
	UINT i;
    INT mf;                      /* menu function ID */
    DWORD iWorkLen;                   /* length of work area (bytes) */
    HDC hDC;
    CHAR * pszError;                   /* error msg if header is messed up */

    DWORD row, height, width,phase;
    DWORD offset;
    GLYPHINFO_20 gi2GlyphTable20;    /* Pointer into 2.0 style offsets table */
    GLYPHINFO_30 gi3GlyphTable30;    /* Pointer into 3.0 style offsets table */
	PBYTE 	pjGlyphData;			 /* Pointer to Glyph information buffer. */

    BYTE  cDummy [CCHEXTRA];         /* dummy buffer for unneeded 3.0 header
                                    * info
                                    */

    /* Put up an hourglass ... this may take a while */
    if (!hHourGlass)
        hHourGlass = LoadCursor(NULL, IDC_WAIT);        /* Get Hourglass */
    hOldCursor = SetCursor(hHourGlass);         /* Show hourglass */

    /* open file for read. */
	nNewFile = MOpenFile (pszFileName, pofsReOpenInfo, OF_READ);
	
	if (nNewFile < 0) {

		return vszErrorOpeningFile;
	}

	//- ReadFile:  Here is where we need to make some adjustments in order to
	//- read the file in to a new DWORD aligned format.
	{
		BYTE    jBuffer [200];


		/* Read the header */
		if (_lread ((HFILE) nNewFile, (LPSTR)&jBuffer, lSizeOfOldFontHeader) !=
				(UINT)lSizeOfOldFontHeader) {

			M_lclose(nNewFile);
			return vszErrorReadingHdr;
		}

		//
		// Call conversion routine to give us a properly aligned buffer.
		//

		vFontStructFromBuffer (
				 (PBYTE)&jBuffer,
				 &fontBuffer
				);

	}

    /* Check the version number -- make sure it's a font */
    switch (fontBuffer.Version)
        {
        case 0x200:
                iFontFormat = ID_FORMAT2;
                break;
        case 0x300:        /* added 1/19/88 */
                iFontFormat = ID_FORMAT3;
                /* read in the extra fields into a dummy buffer ... they don't
                 * mean anything to us  at this stage.
                 */
                if ((M_lread(nNewFile, (LPSTR)cDummy, CCHEXTRA))
                            != CCHEXTRA){
                    M_lclose(nNewFile);
                    return vszErrorReadingHdr;
                }

                break;
        default:                /* Anything else -- toughies */
                M_lclose(nNewFile);
                return vszUnknownFormat;
        }

    /* check if contents of font header are sensible. If not,
       give an error message and quit */

    if ((pszError = VerifyHeaderContents()) != NULL) {

		M_lclose(nNewFile);
        return pszError;
	}

    /* Ready to load -- Check if we will be overwriting */
    if (fLoaded)
	{
        DeleteBitmap();
        fLoaded = FALSE;
	}

    /* Allocate space for font body and Face Name and read them in */
    len= (fontBuffer.Size - lSizeOfOldFontHeader);  /* Compute size */

    if ((lpFontBody = (LPSTR)GlobalAlloc(GMEM_ZEROINIT, (LONG)len)) == 0) {

		M_lclose(nNewFile);
        return vszNotEnoughMem;                       /* Get Handle to space */
	}

    if (iFontFormat == ID_FORMAT3)
        fontlen = len - CCHEXTRA;
    else
        fontlen = len;

    lpFontBodySav = lpFontBody;                    /* save ptr to font body */
    while (fontlen >= (DWORD)SEGMENT_SIZE) {

        /*  file read method if font file size is 64k bytes or greater.
            file is read in in chunks of 65536 (SEGMENT_SIZE), taking care to
            position buffer ptr at 64k boundary each time */

        /* First read in the maximum number of bytes _lread can read (65534) */
        if ((M_lread(nNewFile, lpFontBodySav, WORD_LIMIT)) == WORD_LIMIT) {

            lpFontBodySav += WORD_LIMIT; /* buffer ptr moved up by 64k bytes */
            fontlen -= WORD_LIMIT;     /* fontlen = no of bytes left to read */

		} else {

			GlobalFree (lpFontBody);
			M_lclose(nNewFile);
            return vszErrorReadingBody;
		}

        /* read in an additional two bytes to reach end of segment */
        if ((M_lread(nNewFile, lpFontBodySav, 2)) == 2) {

            lpFontBodySav += 2; /* buffer ptr moved up by 2 bytes */
            fontlen -= 2;       /* fontlen = no of bytes left to read */

        } else {

			GlobalFree (lpFontBody);
			M_lclose(nNewFile);
            return vszErrorReadingBody;
        }
	}

    /* read the partially filled segment  */
    if ((_lread((HFILE)nNewFile, lpFontBodySav, (DWORD)fontlen)) != (UINT) fontlen) {

        GlobalFree (lpFontBody);
        M_lclose (nNewFile);
        return vszErrorReadingBody;
	}

    /* Close the file */
    M_lclose (nNewFile);

    /* check if the offset table entries are within allowable limits.
       If not give an error message, clean up and quit */
    if ((pszError = VerifyTableContents()) != NULL) {

        GlobalFree (lpFontBody);
        return pszError;
	}

    /* now that everything has been checked, move buffer to font */
    font = fontBuffer;

    /* Make local copies of FaceName and Device Name */
    if (font.Face){

        lstrcpy((LPSTR)szFaceName, lpFontBody + font.Face -
                                         (iFontFormat == ID_FORMAT2 ?
                                                    lSizeOfOldFontHeader   :
                                                    lSizeOfOldFontHeader30-1));
    } else {

        lstrcpy((LPSTR)szFaceName, (LPSTR)"");
	}

    for (i = 0; i <= 256; i++)      /* Zero offsets Table for below */
        offsets[i] = 0;

    /* compute work space needed if a 3.0 file. This has to be done since
       3.0 files are compressed versions of 2.0 files and may need a
       work bitmap bigger than the actual font body size */

    if (iFontFormat == ID_FORMAT3) {

        cTable = (WORD) (6 * (font.LastChar - font.FirstChar + 2));
        iWorkLen = 0;

        pjGlyphData = lpFontBody;

        /* work bitmap size = sum of sizes of all characters in font */

        for (i = font.FirstChar; i <= font.LastChar; i++) {

			 vGlyphInfo30FromBuffer (pjGlyphData, &gi3GlyphTable30);

             iWorkLen += ((gi3GlyphTable30.GIwidth +7) >> 3) * font.PixHeight;

			 pjGlyphData += lSizeOfOldGlyph30;
		}

    } else { /* 2.0 file */

        cTable = (WORD)(4 * (font.LastChar - font.FirstChar + 2));
           /* compute table length */
        iWorkLen = len;  /* work space for a 2.0 file is the same as the
                             length of the font body */
    }

	//- Add some extra space for dword alignment.
	iWorkLen += (font.PixHeight * sizeof (DWORD));
      /* Get work space */

    if ((lpWork = (LPSTR)GlobalAlloc (GMEM_ZEROINIT, (LONG)iWorkLen)) == 0) {

        GlobalFree (lpFontBody);
        return vszNotEnoughMem;
    }

	lp1 = lpWork;

    height = (DWORD) font.PixHeight;
    offset = 0;
    /* put the font file into bitmap format */
    if (iFontFormat == ID_FORMAT2){         /* table in 2.0 format */
        for (row = 0; row < height; row++){

             /* view table as a 2.0 style table */
             pjGlyphData = lpFontBody + 1;

             phase = 0;

             for (i = 0; i < (UINT)(font.LastChar - font.FirstChar + 1); i++) {

				vGlyphInfo20FromBuffer (pjGlyphData, &gi2GlyphTable20);

                width = (DWORD) gi2GlyphTable20.GIwidth;

                /* size of each table element = 4bytes */
                lp2 = lpFontBody + (gi2GlyphTable20.GIoffset -
							lSizeOfOldFontHeader) + row;

				pjGlyphData += lSizeOfOldGlyph20;

                /* offset ends up as the sum of the widths */
                if (row == 0)              /* Once is enough */
						offsets[i + font.FirstChar + 1] = offset += width;

                /* create a single scan of bitmap for character */
                phase = ConvertToBitmapFormat (width, phase, height);
             }
             if ((lp1 - lpWork) & 1)
                 *lp1++ = 0;             /* Round lp1 up to Word Boundary */
#ifdef DWORDROUND
             if ((lp1 - lpWork) & 2) {
                 *lp1++ = 0;             /* Round lp1 up to DWord Boundary */
                 *lp1++ = 0;             /* Round lp1 up to DWord Boundary */
			 }
#endif
             //if (((offset + 7) >> 3) & 1)
                 //*lp1++ = 0;             /* Round lp1 up to Word Boundary */
             //if (((offset + 7) >> 3) & 2) {
                 //*lp1++ = 0;             /* Round lp1 up to DWord Boundary */
                 //*lp1++ = 0;             /* Round lp1 up to DWord Boundary */
			 //}
        }
    }
     /* separate loops written for the 2.0 and 3.0 font processing because
        a single loop would involve too many checks (of font type) within
        the loop and slow down processing  */
    else {                                 /* table in 3.0 format */

        for (row = 0; row < height; row++){

             phase = 0;
             /* view table as a 3.0 style table */
             pjGlyphData = lpFontBody;

             for (i = 0; i < (UINT)(font.LastChar - font.FirstChar + 1); i++) {

				vGlyphInfo30FromBuffer (pjGlyphData, &gi3GlyphTable30);

                width = gi3GlyphTable30.GIwidth;

                  /* size of each table element = 6bytes */
                lp2 = lpFontBody + (gi3GlyphTable30.GIoffset -
						lSizeOfOldFontHeader30 +1) + row;

				pjGlyphData += lSizeOfOldGlyph30;

                 /* offset ends up as the sum of the widths */
                if (row == 0)              /* Once is enough */
                    offsets[i + font.FirstChar + 1] = offset += width;

                 /* create a single scan of bitmap for character */
                phase = ConvertToBitmapFormat (width, phase, height);
             }
             if ((lp1 - lpWork) & 1)
                 *lp1++ = 0;             /* Round lp1 up to Word Boundary */
#ifdef DWORDROUND
             if ((lp1 - lpWork) & 2) {
                 *lp1++ = 0;             /* Round lp1 up to DWord Boundary */
                 *lp1++ = 0;             /* Round lp1 up to DWord Boundary */
			 }
#endif
        }
    }
    font.WidthBytes = (WORD) CJ_DIB_SCAN(offset);
       /* fixup NEWFON error */

    GlobalFree(lpFontBody);
    lpFontBody = lpWork;          /* So that below we free the other buffer */
    /* Create a WINDOWS bitmap to move the font definition bits into */

    hDC = GetDC (hFont);                         /* DC to be compatible with */
    hBitmap = CreateBitmap(
                    (INT)font.WidthBytes << 3, /* Width of font in pixels */
                    (INT)font.PixHeight,
                    1, 1, (LPBYTE)NULL);
    hMemDC = CreateCompatibleDC(hDC);           /* Create a DC */
    SelectObject(hMemDC, hBitmap);              /* Relate the two of them */
    ReleaseDC(hFont, hDC);                      /* Done with font DC */

    /* Move the bits in */
    SetBitmapBits(hBitmap,
          (DWORD)font.WidthBytes * (DWORD)font.PixHeight,(CHAR HUGE_T *)lpWork);

    /* Free up the space we loaded the file into */
    GlobalFree(lpFontBody);
    fLoaded = TRUE;
    {
        HMENU hMenu;

        hMenu = GetMenu(hBox);  /* Gray menu if no clipboard bitmap */
        mf = (font.Family & 1) ? MF_ENABLED : MF_GRAYED;
        EnableMenuItem(hMenu, FONT_SAVE, MF_ENABLED);
        EnableMenuItem(hMenu, FONT_SAVEAS, MF_ENABLED);
        EnableMenuItem(hMenu, 1, MF_BYPOSITION | MF_ENABLED);
        EnableMenuItem(hMenu, 2, MF_BYPOSITION | MF_ENABLED);
        EnableMenuItem(hMenu, 3, MF_BYPOSITION | MF_ENABLED);
        EnableMenuItem(hMenu, 4, MF_BYPOSITION | mf);
        EnableMenuItem(hMenu, 5, MF_BYPOSITION | MF_ENABLED);
        EnableMenuItem(hMenu, 6, MF_BYPOSITION | MF_ENABLED);
        DrawMenuBar(hBox);
    }
    SetCursor(hOldCursor);              /* Restore regular cursor */
    return "";
}

/**************************************
 * compares nBytes bytes of s1 and s2 *
 **************************************/
BOOL
ByteCompare (
    CHAR HUGE_T *s1,
    CHAR HUGE_T *s2,
    DWORD nBytes
    )
{
    for ( ; nBytes > 0; nBytes--)
        if (*s1++ != *s2++)
            return FALSE;
    return TRUE;
}

/***********************************
 * copies nBytes bytes of s2 to s1 *
 ***********************************/
BOOL
ByteCopy(
    CHAR HUGE_T *s1,
    CHAR HUGE_T *s2,
    LONG  nBytes
    )
{
    for ( ; nBytes > 0; nBytes--)
        *s1++ = *s2++;
    return TRUE;
}

/****************************************************************************
 * VOID ConvertToFileFormat(width, phase, height)
 *
 * purpose  : Takes a part of the bitmap (corresponding to a single character)
 *            and converts it to a string of bytes in the font file format
 *
 * params   : WORD width :  width of the character in pixels(bits)
 *            WORD phase :  current deviation from byte alignment (pixels)
 *            WORD height:  height of character(pixels)
 *
 * returns:   WORD phase :  new deviation from byte alignment (pixels)
 *
 * side effects : modifies pointers lp1 and lp2 (pointing to font body and work
 *                space respectively)
 *
 ****************************************************************************/
DWORD PASCAL
ConvertToFileFormat(
     DWORD width,
     DWORD phase,
     DWORD height
     )
{
      INT w;

      for (w = width; w > 0; w -= 8){  /* for each byte of font */
           if (phase == 0){             /* easy case */

               BYTE b;

               b = *lp1++;
               if (w < 8){

                   phase = (DWORD) w;
                   b >>= 8 - w;    /* Clear left side bits */
                   b <<= 8 - w;
               }
               *lp2 = b;
           }
           else{

               DWORD j;

               lp1--;          /* Re-read byte prevously read */
               //j = (DWORD) ((BYTE)*lp1++ << 8) | ((BYTE)*lp1++);
               j = (DWORD)((BYTE)*lp1++ << 8);
               j |= (DWORD) ((BYTE)*lp1++);
               if (w < 8){

                   j >>= 16 - phase - (w & 7);  /* shove it right */
                   j <<= 8 - (w & 7); /* Left justify in low byte */
                   phase += (DWORD) w;
                   if (phase <=  8)
                       lp1--;         /* back up pointer */
                   phase &= 7;
               }
               else
                   j >>= 8 - phase;
               *lp2 = (BYTE)j;
           }
           lp2 += height;          /* move to next column */
       }
       return phase;
}

/****************************************************************************
 * char * FontSave()
 *
 * purpose: saves the work bitmap in the required font file format (2.0 or
 *          3.0 and cleans up
 *
 * params : none
 *
 * returns: ptr to a NULL string if Save goes off OK
 *          ptr to error message string otherwise
 *
 * side effects: lots
 *
 ****************************************************************************/

CHAR *
FontSave(
	CHAR 		*pszFileName,
	OFSTRUCT	*pofsReOpenInfo
    )
{
    DWORD bytecount;       /* number of bytes returned by lwrite */
    DWORD size;            /* total size of font */

    WORD height, row;
    DWORD i, fontlen;
    DWORD cBody, cFont,cFontsav, cFace;
    CHAR 			*lpFont;          /* ponter to font body */
    CHAR * sz;
    DWORD iMax;
    WORD widthsav;
	PBYTE			pjGlyphData;
	PBYTE			pjGlyphSave;
    GLYPHINFO_20 	gi2GlyphTable20;    /* 2.0 style Glyph data struct */
    GLYPHINFO_30 	gi3GlyphTable30;    /* 3.0 style Glyph data struct */

    NewAverage(); /* force ave width to be recomputed 8/17/87 BobM */

    /* reset file pointer */
	nNewFile = MOpenFile (pszFileName, pofsReOpenInfo, OF_WRITE | OF_REOPEN);

	if (nNewFile < (HFILE) 0) {

		return vszErrorOpeningFile;
	}

    /* Put up an houglass ... this may take a while */
    if (!hHourGlass)
        hHourGlass = LoadCursor (NULL, IDC_WAIT);        /* Get Hourglass */
    hOldCursor = SetCursor (hHourGlass);         /* Show hourglass */

    height = font.PixHeight;
    cBody = (DWORD)height * (DWORD)font.WidthBytes;

    /* Recompute file size and update header */
    if (iFontFormat == ID_FORMAT2)
       cHeader = (WORD)(lSizeOfOldFontHeader +1);
    else
       cHeader = (WORD)(lSizeOfOldFontHeader30 - 1);

    /* if of 2.0 type, check if size will exceed 64kbytes. If yes, saving
       in 2.0 format will result in loss of information (because offset table
       of 2.0 file is composed of 16bit words). Warn user and ask if file can
       be saved as a 3.0 file */

    if (iFontFormat == ID_FORMAT2)
    { 	
		if (((DWORD)cHeader + (DWORD)(lSizeOfOldGlyph20 * (font.LastChar -
			font.FirstChar+2)) + (DWORD)cBody) >= WORD_LIMIT)
               if (MessageBox(hBox, vszTooBigFor20, vszWarning,
                              IDOK | IDCANCEL |IDNO) == IDYES)
                    iFontFormat = ID_FORMAT3;
	}

	if (iFontFormat == ID_FORMAT2) {

		/* allocate space for a 2.0 style offsets table */
		cTable = (WORD)(lSizeOfOldGlyph20 *
				(font.LastChar - font.FirstChar + 2));

	} else {

		/* allocate space for a 3.0 style offsets table */
		cTable = (WORD)(lSizeOfOldGlyph30 *
				(font.LastChar - font.FirstChar + 2));
	}

	pjGlyphData = (LPSTR)GlobalAlloc (GMEM_ZEROINIT, (DWORD)cTable);

	if (pjGlyphData == 0) {

		M_lclose (nNewFile);
		return vszNotEnoughMem;
	}

	pjGlyphSave = pjGlyphData;

    size = cHeader + cTable;
    iMax = font.LastChar - font.FirstChar + 1;

#ifdef JAPAN
    // Recreate maximum character width when the font is proportional
    // since the WIDTH.. command may grow maxwidth but may not reduce.
    if (font.Family & 1)
	font.MaxWidth = 0;
#endif
    /* create offsets table of font file */
    for (i = 0; i <= iMax; i++){

         DWORD width, charSize;

         width = offsets[i + font.FirstChar + 1] - offsets[i + font.FirstChar];

         if (i == iMax) {
             width = 8;      /* Sentinal blank */
		 }

         if (iFontFormat == ID_FORMAT2){

            gi2GlyphTable20.GIwidth  = (SHORT)width;
            gi2GlyphTable20.GIoffset = (SHORT)size;

			vBufferFromGlyphInfo20 (&gi2GlyphTable20, pjGlyphData);

			pjGlyphData += lSizeOfOldGlyph20;

         } else {

            gi3GlyphTable30.GIwidth  = (SHORT)width;
            gi3GlyphTable30.GIoffset = (INT)size;

			vBufferFromGlyphInfo30 (&gi3GlyphTable30, pjGlyphData);

			pjGlyphData += lSizeOfOldGlyph30;
         }

#ifdef JAPAN // DBCS_FIX
        // update max width
        if(font.Family & 1 ) {
            if(width > font.MaxWidth )
                font.MaxWidth = (WORD)width ;
        }
        if(i == 0x81 && font.CharSet == SHIFTJIS_CHARSET) {
            if(width * 2 > font.MaxWidth) {
                font.MaxWidth = (WORD)width * 2 ;
            }
        }
#endif
         charSize = height * ((width + 7) >> 3);  /* size in bytes */

         if ((size + charSize) < size){           /* Overflow? */

             GlobalFree (pjGlyphData);
             M_lclose (nNewFile);
             return vszFileTooLarge;

         }
         size += charSize;
    }

    /* Update stuff in the header */

    font.Face = (DWORD)size;
    cFace = (WORD)lstrlen (szFaceName) + 1;   /* Allow for \0 */
    size += cFace;
    font.Size = (DWORD)size;                                /* new file size */
    font.BitsOffset = (DWORD)(cHeader + cTable);
    font.Device = (DWORD)NULL;                   /* Device Name must be NULL */
    cFontsav = size - cHeader - cTable;
    cFont =cFontsav;

	/* alloc extra byte for lp1 in case it needs it */
    if (!(lpFontBody = (LPSTR)GlobalAlloc (GMEM_ZEROINIT, (LONG)cBody +
			height * 4))) {

		M_lclose (nNewFile);
        return vszNotEnoughMem;
	}

    GetBitmapBits (hBitmap, (DWORD)cBody, lpFontBody);

    /* save current WidthBytes */
    widthsav = font.WidthBytes;

    /*  MD - reset WidthBytes from computed width */
    font.WidthBytes = (WORD) (cFont - cFace)/ height;

    /* Allocate a block to put bitmap into */
    if ((lpFont = lpWork = GlobalAlloc (GMEM_ZEROINIT,(LONG)cFont)) == 0)
	{
        GlobalFree (lpFontBody);
		M_lclose (nNewFile);
        return vszNotEnoughMem;
	}

    lp1 = lpFontBody;

    /* convert bitmap to file format */
    if (iFontFormat == ID_FORMAT2){   /* offsets table in 2.0 format */
        INT nChars;
        nChars =  font.LastChar - font.FirstChar +1;

        for (row = 0; row < height; row++){

            DWORD phase;

            phase = 0;

            pjGlyphData = pjGlyphSave;

            for (i = 0; i < (DWORD) nChars; i++) {

                INT width;

				vGlyphInfo20FromBuffer (pjGlyphData, &gi2GlyphTable20);

                width = gi2GlyphTable20.GIwidth;

                lp2 = (BYTE HUGE_T *)(lpWork + (gi2GlyphTable20.GIoffset -
						cHeader - cTable + row));

				pjGlyphData += lSizeOfOldGlyph20;

                phase = ConvertToFileFormat (width, phase, height);
            }
            if ((lp1 - lpWork) & 1)
                 lp1++;             /* Round lp1 up to Word Boundary */
#ifdef DWORDROUND
            if ((lp1 - lpWork) & 2) {
                 lp1++;             /* Round lp1 up to DWord Boundary */
                 lp1++;             /* Round lp1 up to DWord Boundary */
			}
#endif
            //if(((offsets[font.LastChar + 1] + 7) >> 3) & 1)
                //lp1++;          /* Round lp1 up to Word Boundary */
        }
    }
     /* separate loops written for the 2.0 and 3.0 font processing because
        a single loop would involve too many checks (of font type) within
        the loop and slow down processing  */
    else{  /* table in 3.0 format */

        INT nChars;
        nChars =  font.LastChar - font.FirstChar +1;
        for (row = 0; row < height; row++){

            DWORD phase;

            phase = 0;
			pjGlyphData = pjGlyphSave;

            for (i = 0; i < (DWORD) nChars; i++) {

                INT width;

				vGlyphInfo30FromBuffer (pjGlyphData, &gi3GlyphTable30);

                width = gi3GlyphTable30.GIwidth;

                lp2 = (BYTE HUGE_T *)(lpWork + (gi3GlyphTable30.GIoffset -
						cHeader - cTable + row));

				pjGlyphData += lSizeOfOldGlyph30;

                phase = ConvertToFileFormat (width, phase, height);
            }
            if ((lp1 - lpWork) & 1)
                 lp1++;             /* Round lp1 up to Word Boundary */
#ifdef DWORDROUND
            if ((lp1 - lpWork) & 2) {
                 lp1++;             /* Round lp1 up to DWord Boundary */
                 lp1++;             /* Round lp1 up to DWord Boundary */
			}
#endif
            //if(((offsets[font.LastChar + 1] + 7) >> 3) & 1)
                //lp1++;          /* Round lp1 up to Word Boundary */
        }
    }

	/* Restore start of data. */
	pjGlyphData = pjGlyphSave;

    lp2 -= height - 1;              /* Back up to start of character */
    for (i = 0; i < height; i++)
         *lp2++ = 0;             /* Fill in guaranteed blank character */

    font.Version = 0x200;

    /******  code for compaction in 3.0 fmt borrowed from CMPCTFON ******/
    if (iFontFormat== ID_FORMAT3){
        DWORD iDefBitmapSize = 0;     /* size of default char */
        DWORD cch = 0;                /* count of number of default char */
#if 0
        GLYPHINFO_30 HUGE_T *cur_char;  /* current element of offset table */
        GLYPHINFO_30 HUGE_T *move_char;
            /* element from which moving is to be done */
        LONG iDefBitmapOffset;        /* offset of default char */
        INT iDefBitmapWidth;          /* width of default char */
        static LONG iEndOfBitmaps;    /* address of end of font body */
        INT iFirstC;                  /* first char in font */
        INT iLastC;                   /* last char in font */
        INT i,j;
        WORD width;
        CHAR HUGE_T *rgbFont;         /* pointer to font body */
        DWORD Offset;

        GDI seems to understand compressed 2.0 external formats but not
        compressed 3.0 formats, and this is causing it to crash loading a 3.0
        format compressed font. Since compression does not affect the
        validity of the font, This code is disabled temporarily till
        this problem is verified. Again, the font is perfectly usable in this
        form, though a trifle bigger than desired -  LR 20/26/90


		Note that this will now not work after the conversion to NT.
		the glyph table needs to accessed as above.  t-davema 8/20/91

        iFirstC = font.FirstChar & 0x0ff;
        iLastC  = font.LastChar & 0x0ff;

        iEndOfBitmaps = (LONG)font.BitsOffset+((LONG)font.WidthBytes *
                (LONG)font.PixHeight);
        rgbFont = (CHAR HUGE_T *)lpFont;   /* start of font body */
        cur_char =(GLYPHINFO_30 HUGE_T *) lpTable; /* start of offset table */

        /* calculate some parameters for the default char */
        iDefBitmapOffset = cur_char[font.DefaultChar].GIoffset;
        iDefBitmapWidth  = cur_char[font.DefaultChar].GIwidth;
        iDefBitmapSize   = (DWORD)cur_char[font.DefaultChar+1].GIoffset
                                           - (DWORD)iDefBitmapOffset;

        /* scan the font body via the offsets table. If a default char
           is recognised, move all the bytes to it's right left by the
           size of the default char.Make all offset table entries of default
           char point to one image of the char (the earliest occuring image) */
        for (i = iFirstC; i <= iLastC; ++i) {

            /* important: Check for limiting conditions (in case break char
               was modified?)*/
            if (cur_char->GIoffset == iDefBitmapOffset){
                cur_char++;
                continue;
            }

            Offset = cur_char->GIoffset -cHeader -cTable;
            /* proceed to compare images only if widths are equal */
            if ((cur_char->GIwidth  == iDefBitmapWidth) &&
                (ByteCompare (&rgbFont [Offset],
                &rgbFont[iDefBitmapOffset-cHeader-cTable],
                                (DWORD)iDefBitmapSize) == TRUE)){

                /* set offset to earliest occurence of the default char */
                if (cur_char->GIoffset <  iDefBitmapOffset){
                    iDefBitmapOffset = cur_char->GIoffset;
                }
                else    {
                    if (i != iLastC){
                        /* move bytes to right of default char left by the
                           size of the char */
                        ByteCopy (&rgbFont [ Offset],
                                  &rgbFont [ Offset+ iDefBitmapSize],
                                  (LONG)(iEndOfBitmaps -
                                  ((cur_char + 1)->GIoffset)));

                        /* correct the offset table entries */
                        move_char = cur_char + 1;
                        for (j=i; j < iLastC; ++j){
                             move_char->GIoffset -= (LONG)iDefBitmapSize;
                             move_char++;
                        }
                    }
                    iEndOfBitmaps -= iDefBitmapSize;
                    /* move End-of-font to the left */
                    cur_char->GIoffset = iDefBitmapOffset;
                    /* point offset of cuurent char to default char */
                    cch++;
                }
            }
            cur_char++;
        }
#endif
        /* recalculate some font attributes */
        lp2   -= (cch * iDefBitmapSize + height);
        for (i = 0; i < height; i++)
            *lp2++ = 0;             /* Fill in guaranteed blank character */

        cFont -= cch * iDefBitmapSize;
        font.WidthBytes = (WORD) (cFont - cFace)/ height;
        font.Size = (DWORD) (cFont + cHeader + cTable);
        font.Face = font.Size - cFace ;
        font.Version = 0x300;

        /* copy info into 3.0 (new) format header and set the additional
           fields */
        ByteCopy ((LPSTR)&font30, (LPSTR)&font, (DWORD)sizeof (font));
        font30.fsFlags        = 0;
        font30.fsFlags = (font.Family & 1 ? FSF_PROPORTIONAL : FSF_FIXED);
        font30.fsAspace       = 0;
        font30.fsBspace       = 0;
        font30.fsCspace       = 0;
        font30.fsColorPointer = 0L;
        for (i = 0; i < 4 ; i++)
            font30.fsReserved[i] = 0L;
    }

    /* Add the FaceName, if any, to the end of the bitmap */
    lstrcpy((LPSTR)lp2, (LPSTR)szFaceName);

	/*
	 * Again we need to do some tricky things to get the header output
	 * correct.  We want to run it through the conversion backwards until
	 * we get the packed structure.
	 */
	{
		BYTE jOutputBuffer [sizeof (font30)];

		if (iFontFormat == ID_FORMAT2) {

			vBufferFromFontStruct (&font, (PBYTE)&jOutputBuffer);

		} else {

			vBufferFromFont30Struct (&font30, (PBYTE)&jOutputBuffer);
		}

		bytecount = M_lwrite (nNewFile, (PBYTE)&jOutputBuffer, (DWORD)cHeader);
	}

    /* Write out Header Information */
    if (bytecount == cHeader) {

            /* Write out OffsetsTable */
         if (cTable == 0 || (_lwrite((HFILE)nNewFile, pjGlyphData, (DWORD)cTable)
				== (UINT)cTable)){
              /* Write out Body */
              fontlen = cFont;
              while (fontlen >= SEGMENT_SIZE){

                 /* file write method if font size is 64k or greater
                 file is written in chunks of 65536 bytes (SEGMENT_SIZE)-lr */

                 /* First write as many bytes as _lwrite will allow(65534) */
                  if (M_lwrite(nNewFile, (CHAR HUGE_T *)lpFont, WORD_LIMIT) ==
                        WORD_LIMIT){
                      fontlen -= WORD_LIMIT;
                            /* fontlen = no of bytes left to write*/
                      lpFont+= WORD_LIMIT; /* buffer ptr moved up by 64k */
                  }
                  else
                      sz = vszErrorWritingBody;

                  /* write the two bytes remaining in the segment */
                  if (M_lwrite(nNewFile, (CHAR HUGE_T *)lpFont, 2) == 2){
                      fontlen -= 2;  /* fontlen = no of bytes left to write*/
                      lpFont+= 2;    /* buffer ptr moved up by 2 */
                  }
                  else
                      sz = vszErrorWritingBody;
              }
              /* segment only partially filled. Write the remaining bytes */
              if (_lwrite((HFILE)nNewFile, (CHAR HUGE_T *)lpFont, (DWORD)fontlen) ==
                    (UINT) fontlen){
                   fChanged = FALSE;
                   sz= "";
              }
              else
                  sz = vszErrorWritingBody;
         }
         else
             sz = vszErrorWritingOffsets;
    }
    else
         sz = vszErrorWritingHdr;

    /* hack: Restore saved value of widthbytes, eliminating a variety
       of minor scrolling problems that follow after a File/Save */
    font.WidthBytes = widthsav;

    M_lclose (nNewFile);

	/* Tidy up */

    GlobalFree(lpFontBody);
    GlobalFree(pjGlyphData);
    GlobalFree(lpWork);

    SetCursor(hOldCursor);              /* Restore regular cursor */

    return sz;
}

/****************************************************************************
 * BOOL ResizeWidths(wChar)
 *
 * params : WORD wChar : new width of a single character
 *
 * purpose: resize work bitmap according to new character width by stretching/
 *          compressing all characters as neccesary
 *
 * returns: none
 *
 * side effects: Work bitmap changes.Some header info (regarding font dimensions
 *               altered as well
 ****************************************************************************/


BOOL
ResizeWidths(
    DWORD wChar
    )
{
    DWORD width;
    DWORD offset, i;

    /* Create a new bitmap to move the font definition bits into */
    width = (font.LastChar - font.FirstChar + 1) * wChar; /* In pixels */
    width = CJ_DIB_SCAN(width);
    if (!GetNewMap(width, font.PixHeight))
        return (FALSE);

    /* Move the bits in */
    offset = 0;
    oldMode = SetStretchBltMode(hNewMemDC, COLORONCOLOR);
    for (i = font.FirstChar; i <= font.LastChar; i++)
        {

        StretchBlt(hNewMemDC, offset, 0,
                wChar, font.PixHeight,                  /* New character */
                hMemDC, offsets[i], 0,
                font.PixWidth, font.PixHeight,          /* Old character */
                SRCCOPY);
        offsets[i] = offset;
        offset += wChar;
        }
    SetStretchBltMode(hNewMemDC, oldMode);
    offsets[font.LastChar + 1] = offset;

    UseNewMap();                /* Switch Pointers and release the space */

    font.HorizRes = (WORD) Proport(font.HorizRes, wChar, font.PixWidth, 999);
    font.WidthBytes = (WORD) width;            /* Misc. ajustments */
    font.PixWidth = font.AvgWidth = (font.MaxWidth = (WORD) wChar);

#ifdef JAPAN
    font.MaxWidth = (WORD) wChar * 2;	// set MaxWidth as DBCS width.
#endif

    return (TRUE);
}


/****************************************************************************
 * BOOL SpreadWidths(wChar)
 *
 * purpose: spread/compress work bitmap (with variable width characters)
 *          in proportion with the maximum character width
 *
 * params : WORD wChar : new width of a character
 *
 * returns: none
 *
 * side effects: Work bitmap changes.Some header info (regarding font dimensions
 *               altered as well
 ****************************************************************************/

BOOL
SpreadWidths(
    DWORD wChar
    )
{
    DWORD offset, i;
    DWORD width, oldWidth, newWidths[257];

    /* Create a new bitmap to move the font definition bits into */
    width = 0;          /* Compute the new width */
    for (i = (DWORD) font.FirstChar; i <= (DWORD) font.LastChar; i++)
        {
        oldWidth = offsets[i + 1] - offsets[i];
        /* Compute new width to nearest whole number */
        newWidths[i] = Proport(oldWidth, wChar, font.MaxWidth, wBoxLim - 1);
        width += newWidths[i];
        }
    width = CJ_DIB_SCAN(width);
    if (!GetNewMap(width, font.PixHeight))
        return(FALSE);

    /* Move the bits in */
    offset = 0;
    oldMode = SetStretchBltMode(hNewMemDC, COLORONCOLOR);
    for (i = font.FirstChar; i <= font.LastChar; i++)
        {
        oldWidth = offsets[i + 1] - offsets[i];
        StretchBlt(hNewMemDC, offset, 0,
                newWidths[i], font.PixHeight,           /* New character */
                hMemDC, offsets[i], 0,
                oldWidth, font.PixHeight,               /* Old character */
                SRCCOPY);
        offsets[i] = offset;
        offset += newWidths[i];
        }
    SetStretchBltMode(hNewMemDC, oldMode);
    offsets[font.LastChar + 1] = offset;

    UseNewMap();                /* Switch Pointers and release the space */
    font.HorizRes = (WORD) Proport(font.HorizRes, wChar, font.MaxWidth, 999);
    font.WidthBytes = (WORD) width;            /* Misc. ajustments */
    font.MaxWidth = (WORD) wChar;
    NewAverage();                       /* Compute new average width */

    return (TRUE);
}


/****************************************************************************
 * NewAverage()
 *
 * purpose: recalculate average width of a character in font
 *
 * params : none
 *
 * returns: none
 *
 * side effects: alters the average width parameter in font header
 *
 ****************************************************************************/
VOID
NewAverage(
    VOID
    )
{
#ifdef FOO
    WORD i, totalwidth;
    /* 12/23/85 -- use weighted avg of lower case letters (mikecr) */

    /* width of the space */
    totalwidth = (offsets[' ' + 1] - offsets[' ']) * widthweights[ALPHA_CNT];

    for (i = 0; i < ALPHA_CNT; i++)
            totalwidth += (offsets['a' + i + 1] - offsets['a' + i]) *
            widthweights[i];

    font.AvgWidth = totalwidth / TOTAL_WEIGHTS;

    /* round up if necessary */
    if (totalwidth % TOTAL_WEIGHTS >= (TOTAL_WEIGHTS >> 1))
            font.AvgWidth++;

#endif

    /* lets do a simple average here */
    font.AvgWidth = (WORD) (((offsets[font.LastChar+1] -
            offsets[font.FirstChar]) + (font.LastChar - font.FirstChar)/2) /
            (font.LastChar - font.FirstChar + 1));

    if (font.AvgWidth == 0) {
       font.AvgWidth++;
    }
#ifdef JAPAN
    if (font.AvgWidth < 1) {
        font.AvgWidth = 1;
    }
#endif
}


/****************************************************************************
 * BOOL ResizeBody(width, height)
 *
 * purpose: adjust work bitmap according to new specified dimensions
 *
 * params : WORD width  : new width of font in pixels
 *          WORD height : new height of font in pixels
 *
 * returns: none
 *
 * side effects: Work bitmap changes.Some header info (regarding font dimensions
 *               altered as well
 ****************************************************************************/

BOOL
ResizeBody(
    DWORD width,
    DWORD height
    )
{

    /* Create a new bitmap to move the font definition bits into */
    if (!GetNewMap(width, height))
        return(FALSE);

    /* Move the bits in */
    oldMode = SetStretchBltMode(hNewMemDC, COLORONCOLOR);
    StretchBlt(hNewMemDC, 0, 0,
            width << 3, height,         /* New Char. */
            hMemDC, 0, 0,
            width << 3, font.PixHeight, /* Old Char. */
            SRCCOPY);
    SetStretchBltMode(hNewMemDC, oldMode);

    UseNewMap();                /* Switch Pointers and release the space */

    font.ExtLeading = (WORD) Proport(font.ExtLeading, height, font.PixHeight, 999);
    font.IntLeading = (WORD) Proport(font.IntLeading, height, font.PixHeight, 999);
    font.Ascent = (WORD) Proport(font.Ascent, height, font.PixHeight, 32);
    font.VertRes = (WORD) Proport(font.VertRes, height, font.PixHeight, 999);
    font.Points = (WORD) Proport(font.Points, height, font.PixHeight, 999);
    font.PixHeight = (WORD) height;            /* Fix misc. header values */
    font.WidthBytes = (WORD) width;

    return (TRUE);
}


/****************************************************************************
 * BOOL NewFirstChar(first)
 *
 * purpose: redefines first character in font and resizes work bitmap
 *          accordingly
 *
 * params : WORD first : new first character to be defined
 *
 * returns: none
 *
 * side effects: Work bitmap changes.Some header info (regarding font dimensions
 *               altered as well
 ****************************************************************************/

BOOL
NewFirstChar(
    DWORD first
    )
{
    DWORD  width, wDefault;
    DWORD offset, i;
    INT dw;

    if (first > font.FirstChar)         /* Smaller? */
        {
        ShrinkFont(first, font.LastChar);
        font.FirstChar = (BYTE) first;
        /*return(FALSE);*/
        return(TRUE);
        }

    /* If not smaller we must pad with the default character */
    wDefault = offsets[font.DefaultChar + 1] - offsets[font.DefaultChar];
    dw = wDefault * (font.FirstChar - first);           /* Extra width */
    width = offsets[font.LastChar + 1] + dw;    /* New width (pixels) */
    width = CJ_DIB_SCAN(width);
    if (!GetNewMap(width, font.PixHeight))
        return(FALSE);           /* New work area */

    /* Move it in in two parts */
    /* First move in default characters */
    offset = 0;
    for (i = first; i < font.FirstChar; i++)
        {
        BitBlt(hNewMemDC, offset, 0,
                wDefault, font.PixHeight,
                hMemDC, offsets[font.DefaultChar], 0,
                SRCCOPY);
        offsets[i] = offset;
        offset += wDefault;
        }
    /* Now move in the rest */
    BitBlt(hNewMemDC, offset, 0,
            offsets[font.LastChar + 1], font.PixHeight,
            hMemDC, 0, 0,
            SRCCOPY);

    UseNewMap();                /* Switch Pointers and release the space */

    /* Now fix up offsets table */
    for (i = font.FirstChar; i <= (DWORD)(font.LastChar + 1); i++)
        offsets[i] = offsets[i] + dw;           /* Shift the rest right */
    font.WidthBytes = (WORD) width;
    font.FirstChar = (BYTE) first;

    return (TRUE);
}


/****************************************************************************
 * ShrinkFont(first, last)
 *
 * purpose:  redefine the first and last charcter in the font and shrink
 *           work bitmap accordingly
 *
 * params :  WORD first : new first character to be defined
 *           WORD last  : new last character  "  "   "
 *
 * returns:  none
 *
 * side effects: Work bitmap changes.Some header info (regarding font dimensions
 *               altered as well
 ****************************************************************************/

VOID
ShrinkFont(
    DWORD first,
    DWORD last
    )
{
    DWORD width, widthPixels;
    DWORD i;
    INT dw;

    dw = offsets[first] - offsets[font.FirstChar];      /* left shift if any */
    widthPixels = offsets[last + 1] - offsets[first];   /* Width in pixels */
    width = CJ_DIB_SCAN(widthPixels);
    if (!GetNewMap(width, font.PixHeight))
        return;           /* New work area.*/

    /* Now move the font into the reduced space */

    BitBlt(hNewMemDC, 0, 0,
            widthPixels, font.PixHeight,
            hMemDC, offsets[first], 0,
            SRCCOPY);

    UseNewMap();                /* Switch Pointers and release the space */

    if (dw)                             /* Ajust offsets */
        {
        for (i = first; i <= last + 1; i++)
            offsets[i] -= dw;
        }

    font.WidthBytes = (WORD) width;

}


/****************************************************************************
 * BOOL NewLastChar(last)
 *
 * purpose:  redefines the last character in the font
 *
 * params :  WORD last : number of character to be made the last character
 *
 * returns:  none
 *
 * side effects: Work bitmap changes.Some header info (regarding font dimensions
 *               altered as well
 *
 ****************************************************************************/

BOOL
NewLastChar(
    DWORD last
    )
{
    DWORD  width, wDefault;
    DWORD offset, i;
    INT dw;

    if (last < font.LastChar)           /* Smaller? */
        {
        ShrinkFont(font.FirstChar, last);
        font.LastChar = (BYTE) last;
        return(FALSE);
        }

    /* If not smaller we must pad with the default character */
    wDefault = offsets[font.DefaultChar + 1] - offsets[font.DefaultChar];
    dw = wDefault * (last - font.LastChar);             /* Extra width */
    offset = offsets[font.LastChar + 1];        /* Current end */
    width = offset + dw;                        /* New width (pixels) */
    width = CJ_DIB_SCAN(width);
    if (!GetNewMap(width, font.PixHeight))
        return(FALSE);           /* New work area */

    /* Move it in in two parts */
    /* First move in the existing font */
    BitBlt(hNewMemDC, 0, 0,
            offset, font.PixHeight,
            hMemDC, 0, 0,
            SRCCOPY);
    /* Then move in default characters */
    for (i = font.LastChar + 1; i <= last;)
        {
        BitBlt(hNewMemDC, offset, 0,
                wDefault, font.PixHeight,
                hMemDC, offsets[font.DefaultChar], 0,
                SRCCOPY);
        offset += wDefault;
        offsets[++i] = offset;
        }

    UseNewMap();                /* Switch Pointers and release the space */

    font.WidthBytes = (WORD) width;
    font.LastChar = (BYTE) last;

    return (TRUE);
}


/****************************************************************************
 * BOOL CharWidth(iChar, wBox)
 *
 * purpose: resizes selected char according to new dimensions. (only for
 *          variable pitch)
 *
 * params : BYTE iChar : character to resize
 *          WORD wBox  : new width of char in pixels
 *
 * returns: none
 *
 * side effects: work bitmap pixel values and header info(regarding font
 *               dimensions) altered
 *
 ****************************************************************************/

BOOL
CharWidth(
    BYTE iChar,                             /* Character to change */
    DWORD wBox                               /* New width */
    )
{
    DWORD  width, nChars;
    DWORD  w1, w2, i;
    INT dw;

    nChars = font.LastChar - font.FirstChar + 1;        /* Character count */
    dw = wBox - (offsets[iChar + 1] - offsets[iChar]);  /* Width change */
    width = offsets[font.LastChar + 1] + dw;            /* New width (pixels) */
    width = CJ_DIB_SCAN(width);
    if (!GetNewMap(width, font.PixHeight))
        return(FALSE);                   /* New work area */

    /* Move it in in two parts */
    /* First move up to and including iChar */
    w1 = offsets[iChar + 1];            /* Width (in pixels) to move */
    BitBlt(hNewMemDC, 0, 0,
            w1 + dw, font.PixHeight,
            hMemDC, 0, 0,
            SRCCOPY);
    /* Now move in the rest */
    if (iChar < (BYTE) font.LastChar)        /* Part to right of elision */
        {
        w2 = offsets[font.LastChar + 1] - offsets[iChar + 1];
        BitBlt(hNewMemDC, offsets[iChar] + wBox, 0,
                w2, font.PixHeight,
                hMemDC, offsets[iChar + 1], 0,
                SRCCOPY);
        }

    UseNewMap();                /* Switch Pointers and release the space */

    /* Now fix up offsets table */
    for (i = iChar + 1;                         /* Where changes start */
        i <= (DWORD)(font.LastChar + 1); i++)            /* Ajust offsets */
        offsets[i] = offsets[i] + dw;           /*  .. by adding dw */
    font.WidthBytes = (WORD) width;
    NewAverage();

    return (TRUE);
}

/****************************************************************************
 * BoxToClipboard(ptA, width, height)
 *
 * purpose: write char (or part of it) to clipboard
 *
 * params : POINT ptA    : upper left coordinate
 *          DWORD width  : width of char in pixels
 *          DWORD height : height of char in pixels
 * returns: none
 *
 * side effects: none
 *
 ****************************************************************************/
VOID
BoxToClipboard(
    POINT ptA,                               /* Upper left point */
    DWORD width,
    DWORD height                      /* Size */
    )
{
    HDC hDC;
    DWORD x, y;

    hDC = GetDC(hFont);                         /* DC to be compatible with */
    hNewBitmap = CreateBitmap(
                    width, height,
                    1, 1, (LPBYTE)NULL);
    hNewMemDC = CreateCompatibleDC(hDC);                /* Create a DC */
    SelectObject(hNewMemDC, hNewBitmap);                /* Relate them */
    ReleaseDC(hFont, hDC);                      /* Done with font DC */

    for (x = 0; x < width; x++)
        for (y = 0; y < height; y++)
            SetPixel(hNewMemDC, x, y, matBox[x + ptA.x][y + ptA.y] == TRUE ?
            BLACK : WHITE);

    /* Now wake up Clipboard and empty it */
    if (!OpenClipboard(hFont))
        ErrorBox(hBox, vszCannotOpenClip); // , vszCopyingToClip);
    else        /* Ok: We got the Clipboard */
        {
        EmptyClipboard();
        SetClipboardData(CF_BITMAP, hNewBitmap);        /* Tell Clipboard */
        }

    /* Tidy things up */
    CloseClipboard();
    DeleteDC(hNewMemDC);
}


/****************************************************************************
 * WORD ClipboardToBox(ptA, width, height, fFit)
 *
 * purpose: copies char (or part of char ) from clipboard to work bitmap
 *          stretching it if need be
 *
 * params : PIONT ptA    : upper left coordinate
 *          DWORD width  : width of char in pixels
 *          DWORD height : height of char in pixels
 *          BOOL fFit   : flag to indicate if default width is to be used
 * returns: none
 *
 * side effects: pixel values of bitmap may change for char
 *
 ****************************************************************************/

DWORD
ClipboardToBox(
    POINT ptA,                               /* Upper left point */
    DWORD width,
    DWORD height,                     /* Size */
    BOOL fFit                               /* Use default width if TRUE */
    )
{
    BITMAP bitmap;
    HDC hDC;
    DWORD x, y;
    HANDLE hT;

    if (!OpenClipboard(hFont)) {
        ErrorBox(hBox, vszCannotOpenClip);
        return 0;
    }
    hNewBitmap = GetClipboardData(CF_BITMAP);

    /* Check if we got something like a character */
    if (GetObject(hNewBitmap, sizeof(BITMAP), (LPSTR)&bitmap) != sizeof(BITMAP))
        {   /* What did we get */
        ErrorBox(hBox, vszErrorClip);
        CloseClipboard();
        return 0;
    }


    if (fFit && ((WORD)bitmap.bmWidth <= font.MaxWidth))
        width = bitmap.bmWidth;

    hDC = GetDC(hFont);                         /* DC to be compatible with */
    hBoxBitmap = CreateBitmap(
                    width, height,
                    1, 1, (LPBYTE)NULL);
    hBoxMemDC = CreateCompatibleDC(hDC);                /* Create a DC */
    hT = SelectObject(hBoxMemDC, hBoxBitmap);           /* Relate them */
    if (hT == NULL || hDC == NULL || hBoxBitmap == NULL || hBoxMemDC == NULL) {
        ErrorBox(hBox, vszErrorClip);
	CloseClipboard();
	DeleteDC(hBoxMemDC);
	DeleteObject(hBoxBitmap);
        return 0;
    }

    /* Get a DC to relate to the Bitmap we just got */
    hNewMemDC = CreateCompatibleDC(hDC);                /* Create a DC */
    hT = SelectObject(hNewMemDC, hNewBitmap);           /* Relate them */
    ReleaseDC(hFont, hDC);                      /* Done with font DC */
    if (hT == NULL || hNewMemDC == NULL) {
        ErrorBox(hBox, vszErrorClip);
	CloseClipboard();
	DeleteDC(hNewMemDC);
	DeleteDC(hBoxMemDC);
	DeleteObject(hBoxBitmap);
        return 0;
    }

    /* Now StretchBlt whatever was on the clipboard into the character */
    oldMode = SetStretchBltMode(hBoxMemDC, COLORONCOLOR);
    fFit = StretchBlt(hBoxMemDC, 0, 0, width, height,
                hNewMemDC, 0, 0, bitmap.bmWidth, bitmap.bmHeight, SRCCOPY);
    if (!fFit || !oldMode) {
        ErrorBox(hBox, vszErrorClip);
	CloseClipboard();
	DeleteDC(hNewMemDC);
	DeleteDC(hBoxMemDC);
	DeleteObject(hBoxBitmap);
        return 0;
    }
    (void)SetStretchBltMode(hBoxMemDC, oldMode);
    for (x = 0; x < width; x++)
        for (y = 0; y < height; y++)
            matBox[x + ptA.x] [y + ptA.y] = (CHAR)(GetPixel(hBoxMemDC, x, y) ?
                0 : 1);
    /* Tidy things up */
    DeleteDC(hNewMemDC);
    DeleteDC(hBoxMemDC);
    DeleteObject(hBoxBitmap);
    CloseClipboard();
    return(width);
}


/****************************************************************************
 * ToClipboard(iChar, width, height)
 *
 * purpose: write char in edit box to clipboard
 *
 * params : BYTE iChar  : number of char to be copied to clipboard
 *          DWORD width  : width of char in pixels
 *          DWORD height : height of char in pixels
 *
 * returns: none
 *
 * side effects: none
 *
 ****************************************************************************/

VOID
ToClipboard(
    BYTE iChar,
    DWORD width,             /* Here in Pixels */
    DWORD height             /* Also in Pixels */
    )
{
    HDC hDC;

    hDC = GetDC(hFont);                         /* DC to be compatible with */
    hNewBitmap = CreateBitmap(
                    width,                      /* Width of font in pixels */
                    height,
                    1, 1, (LPBYTE)NULL);
    hNewMemDC = CreateCompatibleDC(hDC);                /* Create a DC */
    SelectObject(hNewMemDC, hNewBitmap);                /* Relate them */
    ReleaseDC(hFont, hDC);                      /* Done with font DC */

    BitBlt(hNewMemDC, 0, 0, width, height,      /* Move Character in */
            hMemDC, offsets[iChar], 0, NOTSRCCOPY);

    /* Now wake up Clipboard and empty it */
    if (!OpenClipboard(hFont))
        ErrorBox(hBox, vszCannotOpenClip); // , vszCopyingToClip);
    else        /* Ok: We got the Clipboard */
        {
        EmptyClipboard();
        SetClipboardData(CF_BITMAP, hNewBitmap);        /* Tell Clipboard */
        }

    /* Tidy things up */
    CloseClipboard();
    DeleteDC(hNewMemDC);
}


/****************************************************************************
 * GetNewMap(width, height)
 *
 * purpose: create new bitmap of the given width and height.
 *
 * params : WORD width  : width of bitmap in pixels
 *          WORD height : height of bitmap in pixels
 *
 * returns: TRUE if successful, FALSE otherwise
 *
 * side effects: Handle and DC values of new DC assigned
 *
 ****************************************************************************/

BOOL
GetNewMap(
    DWORD width,
    DWORD height              /* New size */
    )
{
    HDC hDC;

    if (height==0) /* Check if something stupid is happening */
        height=font.PixHeight; /* Fix it */

    /* Put up an houglass ... this may take a while */
    if (!hHourGlass)
        hHourGlass = LoadCursor(NULL, IDC_WAIT);        /* Get Hourglass */
    hOldCursor = SetCursor(hHourGlass);         /* Show hourglass */

    /* Create a new bitmap to move the font definition bits into */
    hDC = GetDC(hFont);                         /* DC to be compatible with */
    hNewBitmap = CreateBitmap(
                    width << 3,                 /* Width of font in pixels */
                    height,
                    1, 1, (LPBYTE)NULL);
    if (!hNewBitmap)
        {
        ErrorBox(hBox, vszNotEnoughMem); // , vszAllocatingSpace);
        ReleaseDC(hFont, hDC);    /* bug# 2380 */
        return FALSE;
        }

    hNewMemDC = CreateCompatibleDC(hDC);                /* Create a DC */
    SelectObject(hNewMemDC, hNewBitmap);                /* Relate them */
    ReleaseDC(hFont, hDC);                      /* Done with font DC */
    PatBlt(hNewMemDC, 0, 0, width << 3, height, BLACKNESS);     /* Clear it */
    return TRUE;
}


/****************************************************************************
 * UseNewMap()
 *
 * params : none
 *
 * purpose: discard old bitmap and replace it with new one
 *
 * returns: none
 *
 * side effects: Handle to old bitmap and handle to old bitmap DC replaced
 *               by those of new bitmap respectively
 *
 ****************************************************************************/

VOID
UseNewMap(
    VOID
    )
{
    DeleteDC(hMemDC);
    DeleteObject(hBitmap);              /* Release old space */
    hBitmap = hNewBitmap;
    hMemDC = hNewMemDC;              /* Release old space */
    SetCursor(hOldCursor);              /* Restore regular cursor */
}



VOID
DeleteBitmap(
    VOID
    )
{
    if (hMemDC)
        DeleteDC(hMemDC);
    if (hBitmap)
        DeleteObject(hBitmap);
}


DWORD
Proport(
    DWORD value,
    DWORD top,
    DWORD bottom,
    DWORD limit
    )
    /* Reproportion a value by the ratio top/bottom to the nearest integer
     * and make sure we are still in range */
{
    return min(limit, (DWORD)((1 + 2 * value * top) / (2 * bottom)));
}