2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: readpcf.c /main/5 1996/06/05 16:42:29 ageorge $ */
25 * (c) Copyright 1995 FUJITSU LIMITED
26 * This is source code modified by FUJITSU LIMITED under the Joint
27 * Development Agreement for the CDEnext PST.
28 * This is unpublished proprietary source code of FUJITSU LIMITED
36 #include <sys/types.h>
42 #ifndef GLYPHPADOPTIONS
43 #define GLYPHPADOPTIONS 4
46 static CARD32 getLSB32();
47 static int getINT32();
48 static int getINT16();
49 static Bool seekToType();
50 static void getMetric();
51 static Bool getAccel();
53 static void ByteSwap();
54 static void repadBits();
56 void falGetPcfGSize();
57 int falInitReadPcfProp() ;
58 int falInitReadSnfProp() ;
59 int falReadGpfProp() ;
61 extern void BitOrderInvert() ;
62 extern void set_errfile_str() ;
71 c |= (CARD32)(*p++) << 8;
72 c |= (CARD32)(*p++) << 16;
73 c |= (CARD32)(*p) << 24;
85 if (PCF_BYTE_ORDER(format) == MSBFirst) {
86 c = (CARD32)(*p++) << 24;
87 c |= (CARD32)(*p++) << 16;
88 c |= (CARD32)(*p++) << 8;
92 c |= (CARD32)(*p++) << 8;
93 c |= (CARD32)(*p++) << 16;
94 c |= (CARD32)(*p) << 24;
107 if (PCF_BYTE_ORDER(format) == MSBFirst) {
108 c = (CARD32)(*p++) << 8;
112 c |= (CARD32)(*p) << 8;
119 seekToType( tables, ntables, type, formatp, sizep, offsetp)
129 for ( i = 0; i < ntables; i++) {
130 if ( getLSB32( (unsigned char *)&tables[i].type ) == type) {
132 *formatp = getLSB32( (unsigned char *)&tables[i].format);
134 *sizep = getLSB32( (unsigned char *)&tables[i].size);
136 *offsetp = getLSB32( (unsigned char *)&tables[i].offset);
146 getMetric( buf, format, metric)
151 metric->leftSideBearing = getINT16( (unsigned char *)buf, (CARD32)format);
153 metric->rightSideBearing = getINT16( (unsigned char *)buf, (CARD32)format);
155 metric->characterWidth = getINT16( (unsigned char *)buf, (CARD32)format);
157 metric->ascent = getINT16( (unsigned char *)buf, (CARD32)format);
159 metric->descent = getINT16( (unsigned char *)buf, (CARD32)format);
161 metric->attributes = getINT16( (unsigned char *)buf, (CARD32)format);
166 getAccel( pFontInfo, maxink, buf_top, tables, ntables, type)
167 FontInfoPtr pFontInfo;
178 if ( !seekToType( tables, ntables, (CARD32)type, &format, (CARD32 *)NULL, &offset))
181 buffer = buf_top + offset;
182 format = getLSB32( (unsigned char *)buffer );
184 if (!PCF_FORMAT_MATCH(format, PCF_DEFAULT_FORMAT) &&
185 !PCF_FORMAT_MATCH(format, PCF_ACCEL_W_INKBOUNDS)) {
188 pFontInfo->noOverlap = *buffer++;
189 pFontInfo->constantMetrics = *buffer++;
190 pFontInfo->terminalFont = *buffer++;
191 pFontInfo->constantWidth = *buffer++;
192 pFontInfo->inkInside = *buffer++;
193 pFontInfo->inkMetrics = *buffer++;
194 pFontInfo->drawDirection = *buffer++;
195 /* pFontInfo->anamorphic = FALSE; */
196 /* natural alignment */ buffer++;
197 pFontInfo->fontAscent = getINT32( (unsigned char *)buffer, (CARD32)format);
199 pFontInfo->fontDescent = getINT32( (unsigned char *)buffer, (CARD32)format);
202 /* pFontInfo->maxOverlap = getINT32( (unsigned char *)buffer, (CARD32)format); */ buffer += 4;
203 getMetric(buffer, format, &pFontInfo->minbounds.metrics);
205 getMetric(buffer, format, &pFontInfo->maxbounds.metrics);
207 if (PCF_FORMAT_MATCH(format, PCF_ACCEL_W_INKBOUNDS)) {
209 getMetric( buffer, format, maxink);
211 *maxink = pFontInfo->maxbounds.metrics;
217 falInitReadPcf( pcfinf, buftop)
218 struct pcf_inf *pcfinf;
227 if ( getLSB32( (unsigned char *)buftop ) != PCF_FILE_VERSION)
230 pcfinf->ntables = getLSB32( (unsigned char *)(buftop + 4) );
232 pcfinf->tables = (PCFTablePtr)(buftop + 8);
234 if ( !getAccel( &pcfinf->info, &maxink, buftop, pcfinf->tables, pcfinf->ntables,
235 (CARD32)PCF_BDF_ACCELERATORS))
236 if ( !getAccel( &pcfinf->info, &maxink, buftop, pcfinf->tables, pcfinf->ntables,
237 (CARD32)PCF_ACCELERATORS))
240 pcfinf->org_bounds = pcfinf->info.maxbounds.metrics;
242 if ( !seekToType( pcfinf->tables, pcfinf->ntables, (CARD32)PCF_BITMAPS,
243 &format, (CARD32 *)NULL, &offset))
246 buffp = buftop + offset;
248 format = getLSB32( (unsigned char *)buffp );
250 if (!PCF_FORMAT_MATCH(format, PCF_DEFAULT_FORMAT))
253 pcfinf->nbitmaps = getINT32( (unsigned char *)buffp, (CARD32)format);
255 pcfinf->offsets = (CARD32 *)buffp;
256 buffp += sizeof( *pcfinf->offsets) * pcfinf->nbitmaps;
258 bitmapSizes = (CARD32 *)buffp;
259 pcfinf->sizebitmaps = getINT32( (unsigned char *)&bitmapSizes[PCF_GLYPH_PAD_INDEX(format)], (CARD32)format);
260 pcfinf->bmp_fmt = format;
261 buffp += sizeof( *bitmapSizes) * GLYPHPADOPTIONS;
262 pcfinf->bitmaps = buffp;
263 buffp += pcfinf->sizebitmaps;
265 if ( !seekToType( pcfinf->tables, pcfinf->ntables, (CARD32)PCF_BDF_ENCODINGS,
266 &format, (CARD32 *)NULL, &offset))
269 buffp = buftop + offset;
270 format = getLSB32( (unsigned char *)buffp );
272 if (!PCF_FORMAT_MATCH(format, PCF_DEFAULT_FORMAT))
275 pcfinf->info.firstCol = getINT16( (unsigned char *)buffp, (CARD32)format);
277 pcfinf->info.lastCol = getINT16( (unsigned char *)buffp, (CARD32)format);
279 pcfinf->info.firstRow = getINT16( (unsigned char *)buffp, (CARD32)format);
281 pcfinf->info.lastRow = getINT16( (unsigned char *)buffp, (CARD32)format);
284 pcfinf->info.defaultCh = getINT16( (unsigned char *)buffp, (CARD32)format); buffp += 2;
286 pcfinf->info.chDefault = getINT16( (unsigned char *)buffp, (CARD32)format);
289 pcfinf->info.allExist = FALSE;
290 pcfinf->enc_fmt = format;
291 pcfinf->encodingOffsets = (CARD16 *)buffp;
324 repadBits( src, format, width, height, dest)
330 int bit, byte, glyph, scan;
331 int src_bytewidth, dest_bytewidth;
335 bit = PCF_BIT_ORDER( format);
336 byte = PCF_BYTE_ORDER( format);
337 glyph = PCF_GLYPH_PAD( format);
338 scan = PCF_SCAN_UNIT( format);
340 src_bytewidth = (( width + ( 8 * glyph ) - 1)/( 8 * glyph)) * glyph;
341 dest_bytewidth = ( width + 7) /8;
343 for ( i = 0; i < height; i++, src += src_bytewidth,
344 dest += dest_bytewidth) {
345 for ( j = 0; j < src_bytewidth; j += scan) {
346 memcpy( work, src + j, scan);
347 if ( bit == LSBFirst)
348 BitOrderInvert( work, scan );
349 if ( byte == LSBFirst)
350 ByteSwap( work, scan);
351 if (( j + scan) >= dest_bytewidth) {
352 memcpy( dest + j, work, dest_bytewidth - j);
355 memcpy( dest + j, work, scan);
361 falPcfGlyph( glyph, finf, code)
370 int codeRow, codeCol;
372 int bmp_adj, ptn_adj;
378 fal_utyerrno = FAL_ERR_PARM ;
383 codeCol = inner_code & 0xff;
384 codeRow = (inner_code >> 8) & 0xff;
388 ((code < finf->start) || (code > finf->end))||
389 ((codeCol < finf->pFinf->firstCol)||(codeCol > finf->pFinf->lastCol))||
390 ((codeRow < finf->pFinf->firstRow)||(codeRow > finf->pFinf->lastRow))
396 encode = (codeRow - finf->pFinf->firstRow) * ( finf->pFinf->lastCol - finf->pFinf->firstCol + 1);
397 encode += codeCol - finf->pFinf->firstCol;
398 encodingOffset = getINT16( (unsigned char *)(finf->pcfinf.encodingOffsets + encode), finf->pcfinf.enc_fmt);
400 if (encodingOffset == 0xFFFF) {
406 bitmap = finf->pcfinf.bitmaps + getINT32( (unsigned char *)(finf->pcfinf.offsets + encodingOffset), finf->pcfinf.bmp_fmt);
408 bmp_height = finf->pFinf->maxbounds.metrics.ascent
409 + finf->pFinf->maxbounds.metrics.descent;
410 if (( adj_hi = finf->pFinf->maxbounds.metrics.ascent
411 - finf->pcfinf.org_bounds.ascent) > 0) {
412 bytewidth = 8 * PCF_GLYPH_PAD( finf->pcfinf.bmp_fmt);
413 bytewidth = (( finf->width + bytewidth - 1)/ bytewidth ) * PCF_GLYPH_PAD( finf->pcfinf.bmp_fmt);
414 bmp_adj = bytewidth * adj_hi;
416 if (( cpy_height = bmp_height - adj_hi) > finf->height)
417 cpy_height = finf->height;
418 } else if ( adj_hi < 0) {
420 bytewidth = ( finf->width + 7) / 8;
422 ptn_adj = bytewidth * adj_hi;
423 if (( cpy_height = finf->height - adj_hi) > bmp_height)
424 cpy_height = bmp_height;
428 cpy_height = finf->height;
431 repadBits( bitmap + bmp_adj , finf->pcfinf.bmp_fmt, finf->width, cpy_height, glyph + ptn_adj);
436 falGetPcfGSize( pcfinf, widthp, heightp)
437 struct pcf_inf *pcfinf;
438 unsigned int *widthp, *heightp;
442 w = pcfinf->org_bounds.rightSideBearing
443 - pcfinf->org_bounds.leftSideBearing;
444 h = pcfinf->org_bounds.ascent + pcfinf->org_bounds.descent;
453 /********************************************************
455 * functions for collect GPF file properties
457 *******************************************************/
459 #include "fontstruct.h"
461 static char *getPcfFontProp();
462 static char *getSnfFontProp();
464 * read properties and get font style and
469 falReadFontProp( file, protect_key_data, databuff, islock )
470 char *file ; /* name of font file */
471 int protect_key_data ;
472 FalFontData *databuff ;
483 openfontfile = file ;
485 /* read a condition of a fontfile */
486 if ( stat( openfontfile, &st ) < 0 ) {
487 set_errfile_str( fal_err_file, openfontfile ) ;
488 fal_utyerror = _FAL_STAT_ER;
489 fal_utyderror = errno;
490 fal_utyerrno = FAL_ERR_STAT ;
494 if ( st.st_size < sizeof( FontInfoRec ) ) {
495 fal_utyerror = _FAL_FONT_ER;
497 fal_utyerrno = FAL_ERR_FONT ;
501 /* open a fontfile */
502 if ( (fd = open( openfontfile, ((islock)? O_RDONLY : O_RDWR) )) < 0 ) {
505 return _FAL_TRY_NEXT ;
507 set_errfile_str( fal_err_file, openfontfile ) ;
508 fal_utyerror = _FAL_OPEN_ER;
509 fal_utyderror = errno;
510 fal_utyerrno = FAL_ERR_FNT_OPN ;
516 buf = (char *)mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
517 if ( buf != (char *)-1 ) {
518 /* if "mmap" is normal end */
521 finf.fsize = st.st_size;
523 /* if "mmap" is abnormal end , try "read()" */
525 if ( !(buf = (char *)malloc(st.st_size)) ) {
526 fal_utyerror = _FAL_MALOC_ER;
528 fal_utyerrno = FAL_ERR_MALLOC ;
532 if ( read(fd, buf, st.st_size) != st.st_size ) {
533 set_errfile_str( fal_err_file, openfontfile ) ;
534 fal_utyerror = _FAL_READ_ER;
535 fal_utyderror = errno;
536 fal_utyerrno = FAL_ERR_FNT_RD ;
544 if ( !(buf = (char *)malloc( st.st_size )) ) {
545 fal_utyerror = _FAL_MALOC_ER;
547 fal_utyerrno = FAL_ERR_MALLOC ;
551 if ( read(fd, buf, st.st_size) != st.st_size ) {
552 set_errfile_str( fal_err_file, openfontfile ) ;
553 fal_utyerror = _FAL_READ_ER;
554 fal_utyderror = errno;
555 fal_utyerrno = FAL_ERR_FNT_RD ;
562 finf.fname = openfontfile ;
568 rtn = falReadGpfProp( FAL_UPDATE_FONTINFO, &finf, protect_key_data, databuff ) ;
570 if ( finf.ismmap == TRUE ) {
571 munmap( buf, finf.fsize );
586 * get properties of GPF format file
589 falReadGpfProp( updflg, finf, protect_key_data, databuff )
592 int protect_key_data ;
593 FalFontData *databuff ;
599 openfontfile = finf->fname ;
601 if( updflg == FAL_UPDATE_FONTINFO ) {
602 /* case of a PCF format font */
603 if ( strcmp( FILE_SUFFIX( openfontfile ), PCFSUFFIX ) == 0 ) {
609 else /* case of a SNF format font */
611 FAL_READ_SNF_HEADER( finf, protect_key_data, fal_utyerror, fal_utyderror ) ;
616 * get font properties
619 /* read SNF format property */
620 if ( rtn = falInitReadSnfProp( finf, (caddr_t)finf->pFinf, databuff ) ) {
621 if( rtn == _FAL_TRY_NEXT ){
624 fal_utyerror = _FAL_FONT_ER;
628 } else if ( finf->isPcf ) {
629 /* read PCF format property */
630 if ( rtn = falInitReadPcfProp( updflg, finf, databuff ) ) {
631 if( rtn == _FAL_TRY_NEXT ){
634 fal_utyerror = _FAL_FONT_ER;
639 if( updflg == FAL_UPDATE_FONTINFO ) {
640 finf->pFinf = &finf->pcfinf.info;
642 finf->pGlyphs = NULL;
645 fal_utyerror = _FAL_FONT_ER;
647 fal_utyerrno = FAL_ERR_FONT ;
656 * get properties of PCF format file
659 falInitReadPcfProp( updflg, finf, databuff )
662 FalFontData *databuff ;
664 struct pcf_inf *pcfinf;
672 pcfinf = &finf->pcfinf ;
675 if ( getLSB32( (unsigned char *)buftop ) != PCF_FILE_VERSION)
676 return _FAL_TRY_NEXT ;
678 if( updflg == FAL_UPDATE_FONTINFO ) {
679 pcfinf->ntables = getLSB32( (unsigned char *)(buftop + 4) );
681 pcfinf->tables = (PCFTablePtr)(buftop + 8);
683 if ( !getAccel( &pcfinf->info, &maxink, buftop, pcfinf->tables,
684 pcfinf->ntables, (CARD32)PCF_BDF_ACCELERATORS)) {
685 if ( !getAccel( &pcfinf->info, &maxink, buftop, pcfinf->tables,
686 pcfinf->ntables, (CARD32)PCF_ACCELERATORS)) {
687 fal_utyerrno = FAL_ERR_FONT ;
698 if( updflg == FAL_UPDATE_FONTINFO ) {
699 pcfinf->org_bounds = pcfinf->info.maxbounds.metrics;
702 lb = pcfinf->org_bounds.leftSideBearing ;
703 rb = pcfinf->org_bounds.rightSideBearing ;
704 as = pcfinf->org_bounds.ascent ;
705 ds = pcfinf->org_bounds.descent ;
707 databuff->size.w = rb - lb ;
708 databuff->size.h = as + ds ;
711 * read property "FONT"
713 if ( buffp = getPcfFontProp( buftop, pcfinf->tables,
714 pcfinf->ntables, "FONT" )) {
715 if( (databuff->xlfdname = (char *)strdup( buffp )) == (char *)NULL ){
716 fal_utyerrno = FAL_ERR_MALLOC ;
720 set_errfile_str( fal_err_file, finf->fname ) ;
721 fal_utyerrno = FAL_ERR_PROP_FONT ;
722 return( _FAL_TRY_NEXT ) ;
726 * read property "FAMILY_NAME"
728 if ( buffp = getPcfFontProp( buftop, pcfinf->tables,
729 pcfinf->ntables, "FAMILY_NAME")) {
730 if( (databuff->style.name = (char *)strdup( buffp )) == NULL ){
731 fal_utyerrno = FAL_ERR_MALLOC ;
735 set_errfile_str( fal_err_file, finf->fname ) ;
736 fal_utyerrno = FAL_ERR_PROP_FNAME ;
737 return( _FAL_TRY_NEXT ) ;
748 getPcfFontProp( buftop, tables, ntables, propname)
759 CARD32 format, offset;
761 if ( !seekToType( tables, ntables, (CARD32)PCF_PROPERTIES, &format, (CARD32 *)NULL, &offset))
764 buffer = buftop + offset;
765 format = getLSB32( (unsigned char *)buffer );
767 if ( !PCF_FORMAT_MATCH( format, PCF_DEFAULT_FORMAT))
770 nprops = getINT32( (unsigned char *)buffer, (CARD32)format);
772 propstr = buffer + (4 * ((nprops * 9 + 3) / 4)) + 4;
774 for ( i=0; i < nprops ; i++ ) {
775 name_ofs = getINT32( (unsigned char *)buffer, (CARD32)format ) ;
776 buffer += 4 ; /* name */
777 if ( strcmp( propstr + name_ofs, propname) == 0) {
778 if ( *buffer) { /* isStringProp */
779 return( propstr + getINT32( (unsigned char *)(buffer + 1), (CARD32)format) );
781 return((char *)(intptr_t)getINT32( (unsigned char *)(buffer + 1), (CARD32)format) );
784 buffer += 5 ; /* isStringProp + value */
792 * get properties of SNF format file
795 falInitReadSnfProp( finf, buftop, databuff )
796 Oak_FontInf *finf; /* pointer to the infomation structure */
797 caddr_t buftop; /* font file */
798 FalFontData *databuff ;
803 char *fnt = NULL, *fam ;
804 int nprops, bitmapSize, num_chars ;
807 /* initialize pointer */
808 nprops = finf->pFinf->nProps ;
809 num_chars = ( finf->pFinf->lastRow - finf->pFinf->firstRow + 1 ) *
810 ( finf->pFinf->lastCol - finf->pFinf->firstCol + 1 ) ;
811 bitmapSize = BYTESOFGLYPHINFO(finf->pFinf) ;
814 stprop += sizeof(FontInfoRec) ;
815 stprop += num_chars * sizeof(CharInfoRec) ;
816 stprop += bitmapSize ;
819 * read property "FONTBOUNDINGBOX"
821 lb = finf->pFinf->maxbounds.metrics.leftSideBearing ;
822 rb = finf->pFinf->maxbounds.metrics.rightSideBearing ;
823 as = finf->pFinf->maxbounds.metrics.ascent ;
824 ds = finf->pFinf->maxbounds.metrics.descent ;
827 * read property "FONT"
829 if ( propptr = getSnfFontProp( stprop, nprops, "FONT" )) {
830 if( (fnt = (char *)strdup( propptr )) == NULL ){
831 fal_utyerrno = FAL_ERR_MALLOC ;
835 set_errfile_str( fal_err_file, finf->fname ) ;
836 fal_utyerrno = FAL_ERR_PROP_FONT ;
837 return( _FAL_TRY_NEXT ) ;
841 * read property "FAMILY_NAME"
843 if ( propptr = getSnfFontProp( stprop, nprops, "FAMILY_NAME")) {
844 if( (fam = (char *)strdup( propptr )) == NULL ){
845 fal_utyerrno = FAL_ERR_MALLOC ;
849 set_errfile_str( fal_err_file, finf->fname ) ;
850 fal_utyerrno = FAL_ERR_PROP_FNAME ;
852 return( _FAL_TRY_NEXT ) ;
859 databuff->size.w = rb - lb ;
860 databuff->size.h = as + ds ;
862 databuff->xlfdname = fnt ;
863 databuff->style.name = fam ;
874 getSnfFontProp( buftop, nprops, propname )
883 FontPropRec *ProcRec ;
885 ProcRec = (FontPropRec *)buftop ;
886 buffer = (char *)buftop ;
887 propstr = buffer + nprops * sizeof(FontPropRec) ;
889 for ( i=0; i < nprops ; i++, ProcRec++ ) {
890 name_ofs = ProcRec->name ;
891 if( strcmp( propstr + name_ofs, propname ) == 0 ){
892 if( ProcRec->indirect ){
893 return( propstr + ProcRec->value ) ;
895 return( (char *) (intptr_t) ProcRec->value ) ;
903 /***********************< end of readpcf.c >********************/