fint parseNameTable()

in modules/awt/src/main/native/fontlib/shared/ParsingTables.cpp [158:434]


fint parseNameTable(FILE* tt_file, fwchar_t** familyName, fwchar_t** psName, StyleName* fontStyle)
{
    uflong dwTable = *(uflong*)NAME_TABLE;
    uflong offset;
    Table_name tableName;
    Name_Entry nameRecord;
    flong curPos;
	ufshort *subFamilyName;

    fint i, j;
    fint size;
	bool inFamilyNameCase = false, inSubfamilyNameCase = false, inPSNameCase = false;

    if (searchTable(dwTable, &offset, tt_file))
	{
        /* move position to the 'name' table */
        size = fseek(tt_file, offset, SEEK_SET);
        if (size != 0){
#ifdef DEBUG
            printf("Error executing fseek() for 'name' table.");
#endif
            return -1;
        }

		/* read 'name' table header */
        size = (fint)fread(&tableName, sizeof(Table_name) - sizeof(Name_Entry), 1, tt_file);
        if (size != 1){
#ifdef DEBUG
            printf("Error reading Table 'Name' from file.");
#endif
            return -1;
        }

        tableName.num_name_records = wReverse(tableName.num_name_records);
        tableName.storage_offset = wReverse(tableName.storage_offset);

        /* enumerating NameRecords and finding Family Name value */
        for(i=0; i < tableName.num_name_records; i++)
		{
            size = (fint)fread(&nameRecord, sizeof(Name_Entry), 1, tt_file);
            if (size != 1)
			{
#ifdef DEBUG
                printf("Error reading Name Record from file.");
#endif
                return -1;
            }

            nameRecord.nameID = wReverse(nameRecord.nameID);
			nameRecord.platformID = wReverse(nameRecord.platformID);
            if(nameRecord.platformID == WINDOWS_PLATFORM_ID)
				switch (nameRecord.nameID)
				{
				case FAMILY_NAME_ID:
					if (familyName != NULL && !inFamilyNameCase)
					{
						nameRecord.string_length = wReverse(nameRecord.string_length);
		                nameRecord.string_offset = wReverse(nameRecord.string_offset);

						/* Save current position if someting wrong with family name */
						curPos = ftell(tt_file);
						size = fseek(   tt_file,
							    offset + tableName.storage_offset + nameRecord.string_offset,
								SEEK_SET);
				        if (size != 0){
#ifdef DEBUG
				            printf("Error executing fseek() for 'name' table.");
#endif
							return -1;
						}


//		                ZeroMemory(&fontFamilyName, nameRecord.string_length/2 + sizeof(ufshort));

						ufshort *fontFamilyName = new ufshort[nameRecord.string_length/2+1];

//						ZeroMemory(&(fontName[nameRecord.string_length]),1);

						size = (fint)fread(fontFamilyName, sizeof(ufshort), nameRecord.string_length/2, tt_file);
						if (size != nameRecord.string_length/2)
						{
#ifdef DEBUG
						    printf("Error reading Family Name from file.");
#endif
							delete[] fontFamilyName;
							fontFamilyName = NULL;
							return -1;
						}

						for(j=0; j < nameRecord.string_length/2; j++)
						{
							(fontFamilyName)[j] = wReverse((fontFamilyName)[j]);
//printf("%c",(fchar)(fontFamilyName)[j]);
						}
//printf("\n");
						(fontFamilyName)[j] = 0;
						inFamilyNameCase = true;

//#ifdef WIN32
						*familyName = (fwchar_t*)fontFamilyName;
/*#else
//TODO: To unify this cycle and previous
						*familyName = new fwchar_t[nameRecord.string_length/2+1];

						for(j=0; j < nameRecord.string_length/2+1; j++)
						{
                            (*familyName)[j] = (fwchar_t)fontFamilyName[j];
						}
						delete fontFamilyName;
#endif*/

						size = fseek( tt_file, curPos, SEEK_SET);
				        if (size != 0){
#ifdef DEBUG
							printf("Error executing fseek() for 'name' table.");
#endif
							return -1;
						}
					}
					break;
				case POSTSCRIPT_NAME_ID:
					if (psName != NULL && !inPSNameCase)
					{
						nameRecord.string_length = wReverse(nameRecord.string_length);
		                nameRecord.string_offset = wReverse(nameRecord.string_offset);

						/* Save current position if someting wrong with postscript name */
						curPos = ftell(tt_file);
						size = fseek(   tt_file,
							    offset + tableName.storage_offset + nameRecord.string_offset,
								SEEK_SET);
				        if (size != 0){
#ifdef DEBUG
				            printf("Error executing fseek() for 'name' table.");
#endif
							return -1;
						}

						ufshort *fontPSName = new ufshort[nameRecord.string_length/2+1];

						size = (fint)fread(fontPSName, sizeof(ufshort), nameRecord.string_length/2, tt_file);
						if (size != nameRecord.string_length/2)
						{
#ifdef DEBUG
						    printf("Error reading PostScript Name from file.");
#endif
							delete[] fontPSName;
							fontPSName = NULL;
							return -1;
						}

						for(j=0; j < nameRecord.string_length/2; j++)
						{
							(fontPSName)[j] = wReverse((fontPSName)[j]);
						}
						(fontPSName)[j] = 0;
						inPSNameCase = true;

//#ifdef WIN32
						*psName = (fwchar_t*)fontPSName;
/*#else

						*psName = new fwchar_t[nameRecord.string_length/2+1];
//TODO: To unify this cycle and previous
						for(j=0; j < nameRecord.string_length/2+1; j++)
						{
                            (*psName)[j] = (fwchar_t)fontPSName[j];
						}
						delete fontPSName;
#endif*/

						size = fseek( tt_file, curPos, SEEK_SET);
				        if (size != 0){
#ifdef DEBUG 
							printf("Error executing fseek() for 'name' table.");
#endif
							return -1;
						}
					}
					break;
				case SUBFAMILY_NAME_ID:
					if(fontStyle != NULL && !inSubfamilyNameCase)
					{
						nameRecord.string_length = wReverse(nameRecord.string_length);
						nameRecord.string_offset = wReverse(nameRecord.string_offset);

					/* Save current position if someting wrong with subfamily name */
						curPos = ftell(tt_file);
						size = fseek(   tt_file,
							    offset + tableName.storage_offset + nameRecord.string_offset,
								SEEK_SET);
					    if (size != 0){
#ifdef DEBUG
						    printf("Error executing fseek() for 'name' table.");
#endif
							return -1;
						}

//					    ZeroMemory(&fontName, nameRecord.string_length + sizeof(ufshort));

						subFamilyName = new ufshort[nameRecord.string_length/2+1];

//						ZeroMemory(&(fontName[nameRecord.string_length]),1);

						size = (fint)fread(subFamilyName, sizeof(ufshort), nameRecord.string_length/2, tt_file);
					    if (size != nameRecord.string_length/2)
						{
#ifdef DEBUG
						    printf("Error reading SubFamily Name from file.");
#endif
							delete[] subFamilyName;
							subFamilyName = NULL;
							return -1;
						}

						for(j=0; j < nameRecord.string_length/2; j++)
						{
							subFamilyName[j] = wReverse(subFamilyName[j]);
						}
						subFamilyName[j] = 0;

#define COMPARE_IT		(!fwcscmp((ufshort *)subFamilyName,"Italic") || !fwcscmp((ufshort *)subFamilyName,"Oblique"))
#define COMPARE_BD		(!fwcscmp((ufshort *)subFamilyName,"Bold"))
#define COMPARE_BDIT	(!fwcscmp((ufshort *)subFamilyName,"Bold Italic") || !fwcscmp((ufshort *)subFamilyName,"Bold Oblique"))
#define COMPARE_REG		(!fwcscmp((ufshort *)subFamilyName,"Regular") || !fwcscmp((fwchar_t *)subFamilyName,"Normal") || !fwcscmp((fwchar_t *)subFamilyName,"Book"))

						if COMPARE_IT
						{
							*fontStyle = Italic;
							inSubfamilyNameCase = true;
						}
						else if COMPARE_BD
						{
							*fontStyle = Bold;
							inSubfamilyNameCase = true;
						}
						else if COMPARE_BDIT
						{
							*fontStyle = BoldItalic;
							inSubfamilyNameCase = true;
						}
						else if COMPARE_REG
						{
							*fontStyle = Regular;
							inSubfamilyNameCase = true;
						}

						delete[] subFamilyName;
						subFamilyName = NULL;

						size = fseek( tt_file, curPos, SEEK_SET);
				        if (size != 0){
#ifdef DEBUG
				            printf("Error executing fseek() for 'name' table.");
#endif
							return -1;
						}

					}

				}
			}
		}


     /* Close font file stream */
/*     if( fclose( tt_file ) ){
        printf("Error closing TrueType font file.");
        return 0;
     }
*/
//	fclose(tt_file);
	
	if  (!inSubfamilyNameCase)
		fontStyle = NULL;
	return 0;
}