aknlayoutcompiler/src/LayScale.cpp
changeset 0 f58d6ec98e88
child 1 b700e12870ca
equal deleted inserted replaced
-1:000000000000 0:f58d6ec98e88
       
     1 /*
       
     2 * Copyright (c) 2002-2004 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // disable "identifier was truncated to '255' characters in the browser information" warning
       
    21 #pragma warning (disable:4786)
       
    22 
       
    23 #include "LayScale.h"
       
    24 #include "LayoutParse.h"
       
    25 #include "LayoutCompilerErr.h"
       
    26 #include <cdlcompilertoolkit/cdltkprocess.h>
       
    27 #include <fstream>
       
    28 #include <iostream>
       
    29 #include <algorithm>
       
    30 #include "LayoutWriter.h"
       
    31 
       
    32 typedef LayoutProcessArgsErr<LayoutScale> LayoutScaleArgsErr;
       
    33 
       
    34 
       
    35 int LayoutScale::Process(const vector<string>& args)
       
    36 	{
       
    37 	if (args.size() != 8)
       
    38 		throw LayoutScaleArgsErr();
       
    39 
       
    40 	auto_ptr<TLayParseLayout> sourceLayout(TLayParseLayout::Parse(args[2]));
       
    41 	int sourceWidth = CdlTkUtil::ParseInt(args[3]);
       
    42 	int sourceHeight = CdlTkUtil::ParseInt(args[4]);
       
    43 
       
    44 	string destLayout = args[5];
       
    45 	int destWidth = CdlTkUtil::ParseInt(args[6]);
       
    46 	int destHeight = CdlTkUtil::ParseInt(args[7]);
       
    47 
       
    48 	LayoutScale scale(*sourceLayout, sourceWidth, sourceHeight, destLayout, destWidth, destHeight);
       
    49 	scale.Run();
       
    50 
       
    51 	return 0;
       
    52 	}
       
    53 
       
    54 void LayoutScale::ShowHelp(ostream& stream)
       
    55 	{
       
    56 	stream << "LayScale <sourceLayout> <sourceWidth> <sourceHeight> <destLayout> <destWidth> <destHeight>" << endl;
       
    57 	stream << "  This performs an arithmetic scaling on all dimensional values in a layout." << endl;
       
    58 	stream << "  This will not produce a pixel perfect scaling effect due to rounding errors," << endl;
       
    59 	stream << "  however it may be sufficient for prototyping and demonstration purposes." << endl;
       
    60 	}
       
    61 
       
    62 LayoutScale::LayoutScale(TLayout& aSourceLayout, int aSourceWidth, int aSourceHeight, const string& aDestLayoutName, int aDestWidth, int aDestHeight)
       
    63 :	iLayout(aSourceLayout),
       
    64 	iSourceWidth(aSourceWidth),
       
    65 	iSourceHeight(aSourceHeight),
       
    66 	iDestLayoutName(aDestLayoutName),
       
    67 	iDestWidth(aDestWidth),
       
    68 	iDestHeight(aDestHeight)
       
    69 	{
       
    70 	}
       
    71 
       
    72 void LayoutScale::Run()
       
    73 	{
       
    74 	RescaleLayout();
       
    75 	WriteLayout();
       
    76 	}
       
    77 
       
    78 const string KHoriz = "lrW";
       
    79 const string KVert = "tbBH";
       
    80 const string KFont = "Font";
       
    81 const string KScaledSuffix = "_Scaled";
       
    82 
       
    83 void LayoutScale::RescaleLayout()
       
    84 	{
       
    85 	for (TLayout::iterator pTab = iLayout.begin(); pTab != iLayout.end(); ++pTab)
       
    86 		{
       
    87 		TLayoutTable& table = **pTab;
       
    88 		for (TLayoutTable::iterator pLine = table.begin(); pLine != table.end(); ++pLine)
       
    89 			{
       
    90 			TLayoutLine& line = **pLine;
       
    91 			for (TLayoutLine::iterator pCell = line.begin(); pCell != line.end(); ++pCell)
       
    92 				{
       
    93 				const string& cellName = pCell->first;
       
    94 				TValues& values = pCell->second;
       
    95                 if ( cellName == KFont)
       
    96                     {
       
    97 				    for (TValues::iterator pVal = values.begin(); pVal != values.end(); ++pVal)
       
    98 					    {
       
    99 					    TagScaledToken(*pVal);
       
   100 					    }                   
       
   101                     }
       
   102                 else
       
   103                     {
       
   104    				    TScale dir = EHoriz;
       
   105 				    if (KVert.find(cellName) != string::npos)
       
   106 				        dir = EVert;
       
   107 				    else if (KHoriz.find(cellName) == string::npos)
       
   108 				        continue;
       
   109 
       
   110 				    for (TValues::iterator pVal = values.begin(); pVal != values.end(); ++pVal)
       
   111 					    {
       
   112 					    RescaleValue(*pVal, dir);
       
   113 					    }
       
   114                     }
       
   115 				}
       
   116 			}
       
   117 		}
       
   118 	}
       
   119 
       
   120 void LayoutScale::RescaleValue(string& aVal, TScale aDir)
       
   121 	{
       
   122 	// find all numbers in this value and rescale them, copy non-numeric text directly.
       
   123 	string result;
       
   124 	string num;
       
   125 	int count = aVal.size();
       
   126 	for (int pos = 0; pos < count; pos++)
       
   127 		{
       
   128 		char c = aVal[pos];
       
   129 		if (CdlTkUtil::IsNumeric(c))
       
   130 			{
       
   131 			num += c;
       
   132 			}
       
   133 		else
       
   134 			{
       
   135 			result += RescaleNum(num, aDir);
       
   136 			num = "";
       
   137 			result += c;
       
   138 			}
       
   139 		}
       
   140 	result += RescaleNum(num, aDir);
       
   141 	aVal = result;
       
   142 	}
       
   143 
       
   144 void LayoutScale::TagScaledToken(string& aToken)
       
   145 	{
       
   146 	string result(aToken);
       
   147 	result += KScaledSuffix;
       
   148 	aToken = result;
       
   149 	}
       
   150 
       
   151 string LayoutScale::RescaleNum(string& aNum, TScale aDir)
       
   152 	{
       
   153 	if (aNum.empty())
       
   154 		return aNum;
       
   155 
       
   156 	int num = CdlTkUtil::ParseInt(aNum);
       
   157 	if (aDir == EHoriz)
       
   158 		num = Scale(num, iDestWidth, iSourceWidth);
       
   159 	else
       
   160 		num = Scale(num, iDestHeight, iSourceHeight);
       
   161 	return CdlTkUtil::IntToString(num);
       
   162 	}
       
   163 
       
   164 void LayoutScale::WriteLayout()
       
   165 	{
       
   166 	TLayoutWriter writer(iLayout, iDestLayoutName);
       
   167 	writer.Write("");
       
   168 	}
       
   169 
       
   170 int LayoutScale::Scale(int aNumber, int aNumerator, int aDenominator)
       
   171 	{
       
   172 	// round to nearest whole number, rounding up on .5
       
   173 	int num = (aNumber * aNumerator) / aDenominator;		// rounded down
       
   174 	int remainder = (aNumber * aNumerator) % aDenominator;	// remainder
       
   175 	
       
   176 	if ((remainder*2) >= aDenominator)
       
   177 		num++;				// round up if remainder >= half
       
   178 
       
   179 	return num;
       
   180 	}
       
   181 
       
   182