os/graphics/graphicstools/gdi_tools/fontcomp/FONTCOMP.CPP
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 #include "FONTCOMP.H"
    17 
    18 FontCompiler::FontCompiler():
    19 	iInputFile(),
    20 	iOutputFile(),
    21 	iFxf(NULL),
    22 	iFontSpace(NULL),
    23 	iWorkSpace(NULL),
    24 	iMapSpace(NULL),
    25 	iWorkSpaceSize(0),
    26 	iHeaderDataLen(0),
    27 	iHeaderData()
    28 	{}
    29 
    30 int FontCompiler::Init(char* infile,char* outfile,char* mapfile)
    31 	{
    32 	if(mapfile)
    33 		{
    34 		iMapSpace=new short int[256];
    35 		if(!iMapSpace) return(NoMemory);
    36 
    37 #ifdef __MSVCDOTNET__
    38 		fstream tempfile(mapfile, ios::in | ios::binary);
    39 #else //!__MSVCDOTNET__
    40 		fstream tempfile(mapfile, ios::in | ios::binary | ios::nocreate);
    41 #endif //__MSVCDOTNET__
    42 
    43 		if(!tempfile.is_open())
    44 			return(NoFile);
    45 		tempfile.read((char*)iMapSpace,512);
    46 		tempfile.close();
    47 		}
    48 	iOutputFile.open(outfile, ios::out);
    49 
    50 #ifdef __MSVCDOTNET__
    51 	iInputFile.open(infile, ios::in | ios::binary);
    52 #else //!__MSVCDOTNET__
    53 	iInputFile.open(infile, ios::in | ios::binary | ios::nocreate);
    54 #endif //__MSVCDOTNET__
    55 
    56 	if(!(iOutputFile.is_open() && iInputFile.is_open()))
    57 		return(NoFile);
    58 	iFxf=new Fxf;
    59 	if(!iFxf) return(NoMemory);
    60 	iFxf->iBold=0;
    61 	iFxf->iItalic=0;
    62 	iFxf->iProportional=0;
    63 	iFxf->iSerif=0;
    64 	iFxf->iSymbol=0;
    65 	iFxf->iUid=0;
    66 	iFontSpace=new char[0x100000];
    67 	if(!iFontSpace) return(NoMemory);
    68 	return(NoError);
    69 	}
    70 
    71 char* FontCompiler::FontStore() const
    72 	{
    73 	return(iFontSpace);
    74 	}
    75 
    76 int FontCompiler::Read(FontType aInputType)
    77 	{
    78 	FontRead *read=NULL;
    79 	switch(aInputType)
    80 		{
    81 		case EFontTypeFsc:
    82 			read=new FscRead(iInputFile,*this,iFxf);
    83 			break;
    84 		case EFontTypeEff:
    85 			read=new EffRead(iInputFile,*this,iFxf,iMapSpace);
    86 			break;
    87 		default:
    88 			return(Parameter);
    89 		}
    90 	if(!read)
    91 		return(NoMemory);
    92 	int error=read->ReadFont();
    93 	delete read;
    94 	return(error);
    95 	}
    96 
    97 void FontCompiler::RemoveBlankSpace()
    98 	{
    99 	int count=0;
   100 	const int maxbytewidth=(MAX_CHAR_WID+15)>>3;
   101 	unsigned char buf[maxbytewidth];
   102 	unsigned char zbuf[maxbytewidth];
   103 
   104 	for(count=0;count<maxbytewidth;count++)
   105 			zbuf[count]=0;
   106 	for(int chNum=iFxf->FirstChr;chNum<iFxf->n_chars;chNum++)
   107 		{
   108 		FcmCharHead *fChar=iFxf->chr[chNum];
   109 		if(fChar)
   110 		    {
   111 		    int LastNonBlank=0;
   112 		    int TopCount=0;
   113 		    int MinLeftBlank=fChar->width;
   114 		    int MinRightBlank=0;
   115 		    
   116        	    // DEF102183: Graphics tools fail to build using MS VC8.
   117 		    int row;
   118 		    int x;
   119 		    for(row=0;row<fChar->height;)
   120 			{
   121 				memcpy(buf,iFontSpace+fChar->offset+fChar->ByteWid*row,fChar->ByteWid);
   122 			unsigned char bit=1;
   123 			unsigned char* pb=buf;
   124 			for(x=0;x<MinLeftBlank;x++)
   125 			    {
   126 			    if ((*pb)&bit)
   127 				{
   128 				MinLeftBlank=x;
   129 				break;
   130 				}
   131 			    bit<<=1;
   132 			    if (bit==0)
   133 				{
   134 				bit=1;
   135 				pb++;
   136 				}
   137 			    }
   138 			bit=(unsigned char)(1<<((fChar->width-1)%8));
   139 			pb=&buf[((fChar->width-1)>>3)];
   140 			for(x=fChar->width;x>MinRightBlank;x--)
   141 			    {
   142 			    if ((*pb)&bit)
   143 				{
   144 				MinRightBlank=x;
   145 				break;
   146 				}
   147 			    bit>>=1;
   148 			    if (bit==0)
   149 				{
   150 				bit=unsigned char(0x80);
   151 				pb--;
   152 				}
   153 			    }
   154 			row++;
   155 				if(memcmp(zbuf,buf,fChar->ByteWid))
   156 			    {
   157 			    if (TopCount==0)
   158 				TopCount=row;   /* Count of blank rows at the top */
   159 			    LastNonBlank=row;
   160 			    }
   161 			}
   162 		    if (TopCount==0)
   163 			{
   164 			fChar->height=0;
   165 			fChar->width=0;
   166 			fChar->xOffset=0;
   167 			fChar->yOffset=0;
   168 			}
   169 		    else
   170 			{
   171 			TopCount--;
   172 			fChar->height=LastNonBlank-TopCount;
   173 			fChar->width=MinRightBlank-MinLeftBlank;
   174 			fChar->yOffset-=TopCount;
   175 			fChar->offset+=TopCount*fChar->ByteWid;
   176 			fChar->xOffset+=MinLeftBlank;
   177 			if (MinLeftBlank)
   178 			    {
   179 			    int byte_step=MinLeftBlank/8;
   180 			    int bit_shift=MinLeftBlank%8;
   181 			    unsigned char mask=(unsigned char)(0xFF>>(7-((fChar->width-1)%8)));
   182 			    for(row=0;row<fChar->height;row++)
   183 						{
   184 						memcpy(buf,iFontSpace+fChar->offset+fChar->ByteWid*row,fChar->ByteWid);
   185 						for(x=0;x<(fChar->ByteWid-byte_step);x++)
   186 							buf[x]=(unsigned char)((buf[x+byte_step]>>bit_shift)+
   187 								(buf[x+byte_step+1]<<(8-bit_shift)));
   188 						buf[x-1]&=mask;
   189 						memcpy(iFontSpace+fChar->offset+fChar->ByteWid*row,buf,fChar->ByteWid);
   190 						}
   191 			    }
   192 			}
   193 		    }
   194 		}
   195 	}
   196