Back to index

scribus-ng  1.3.4.dfsg+svn20071115
gtfont.cpp
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 /***************************************************************************
00008  *   Copyright (C) 2004 by Riku Leino                                      *
00009  *   tsoots@gmail.com                                                      *
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  *   This program is distributed in the hope that it will be useful,       *
00017  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00018  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00019  *   GNU General Public License for more details.                          *
00020  *                                                                         *
00021  *   You should have received a copy of the GNU General Public License     *
00022  *   along with this program; if not, write to the                         *
00023  *   Free Software Foundation, Inc.,                                       *
00024  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00025  ***************************************************************************/
00026 
00027 #include "gtfont.h"
00028 
00029 const QString gtFont::fontWeights[FontWeightMAX] = 
00030 {
00031        "",
00032        "Demi Bold",
00033        "Extra Black",
00034        "Extra Bold",
00035        "Extra Heavy",
00036        "Extra Light",
00037        "Semi Bold",
00038        "Black",
00039        "Bold",
00040        "Book",
00041        "Demi",
00042        "Heavy",
00043        "Light",
00044        "Lite",
00045        "Medium",
00046        "Regular",
00047        "Roman"
00048 };
00049 
00050 const QString gtFont::fontSlants[FontSlantMAX] =
00051 {
00052        "",
00053        "Italic",
00054        "Oblique"
00055 };
00056 
00057 const QString gtFont::fontWidths[FontWidthMAX] =
00058 {
00059        "",
00060        "Extra Condensed",
00061        "Semi Condensed",
00062        "Ultra Condensed",
00063        "Extra Compressed",
00064        "Semi Compressed",
00065        "Ultra Compressed",
00066        "Condensed",
00067        "Compressed"
00068 };
00069 
00070 gtFont::gtFont()
00071 {
00072        noEffects();
00073        name        = "";
00074        family      = "";
00075        weight      = "";
00076        slant       = "";
00077        width       = "";
00078        append      = "";
00079        size        = 12;
00080        color       = "Black";
00081        shade       = 100;
00082        strokeColor = "Black";
00083        strokeShade = 100;
00084        hscale      = 1000;
00085        kerning     = 0;
00086        useFullName = true;
00087        weightIndex = 0;
00088        slantIndex  = 1;
00089        widthIndex  = 2;
00090        smallestIndex = -1;
00091        biggestIndex = - 1;
00092        index = -1;
00093        tmpWeightIndex = -1;
00094        tmpSlantIndex = -1;
00095        tmpWidthIndex = -1;
00096 }
00097 
00098 gtFont::gtFont(const gtFont& f)
00099 {
00100        name        = f.name;
00101        family      = f.family;
00102        weight      = f.weight;
00103        slant       = f.slant;
00104        width       = f.width;
00105        append      = f.append;
00106        size        = f.size;
00107        color       = f.color;
00108        shade       = f.shade;
00109        strokeColor = f.strokeColor;
00110        strokeShade = f.strokeShade;
00111        hscale      = f.hscale;
00112        kerning     = f.kerning;
00113        useFullName = f.useFullName;
00114        weightIndex = f.weightIndex;
00115        slantIndex  = f.slantIndex;
00116        widthIndex  = f.widthIndex;
00117        smallestIndex  = f.smallestIndex;
00118        biggestIndex   = f.biggestIndex;
00119        index          = f.index;
00120        tmpWeightIndex = f.tmpWeightIndex;
00121        tmpSlantIndex  = f.tmpSlantIndex;
00122        tmpWidthIndex  = f.tmpWidthIndex;
00123        fontEffects[NORMAL]        = f.fontEffects[NORMAL];
00124        fontEffects[UNDERLINE]     = f.fontEffects[UNDERLINE];
00125        fontEffects[STRIKETHROUGH] = f.fontEffects[STRIKETHROUGH];
00126        fontEffects[SMALL_CAPS]    = f.fontEffects[SMALL_CAPS];
00127        fontEffects[SUPERSCRIPT]   = f.fontEffects[SUPERSCRIPT];
00128        fontEffects[SUBSCRIPT]     = f.fontEffects[SUBSCRIPT];
00129        fontEffects[OUTLINE]       = f.fontEffects[OUTLINE];
00130 }
00131 
00132 bool gtFont::isToggled(FontEffect fe)
00133 {
00134        return fontEffects[fe];
00135 }
00136 
00137 bool gtFont::toggleEffect(FontEffect fe)
00138 {
00139        switch (fe)
00140        {
00141               case NORMAL:
00142                      if (!fontEffects[NORMAL])
00143                             noEffects();
00144                      break;
00145               case SUPERSCRIPT:
00146                      fontEffects[SUPERSCRIPT] = !fontEffects[SUPERSCRIPT];
00147                      if (fontEffects[SUPERSCRIPT])
00148                      {
00149                             fontEffects[SUBSCRIPT] = false;
00150                             fontEffects[NORMAL] = false;       
00151                      }
00152                      break;
00153               case SUBSCRIPT:
00154                      fontEffects[SUBSCRIPT] = !fontEffects[SUBSCRIPT];
00155                      if (fontEffects[SUBSCRIPT])
00156                      {
00157                             fontEffects[SUPERSCRIPT] = false;
00158                             fontEffects[NORMAL] = false;       
00159                      }
00160                      break;
00161               default:
00162                      fontEffects[fe] = !fontEffects[fe];
00163                      if (fontEffects[fe])
00164                             fontEffects[NORMAL] = false;
00165        }
00166        return fontEffects[fe];
00167 }
00168 
00169 int gtFont::getEffectsValue()
00170 {
00171        int b = 0;
00172        if (isToggled(NORMAL))
00173               b = 0;
00174        if (isToggled(UNDERLINE))
00175               b |= 8;
00176        if (isToggled(STRIKETHROUGH))
00177               b |= 16;
00178        if (isToggled(SMALL_CAPS))
00179               b |= 64;
00180        if (isToggled(SUPERSCRIPT))
00181               b |= 1;
00182        if (isToggled(SUBSCRIPT))
00183               b |= 2;
00184        if (isToggled(OUTLINE))
00185               b |= 4;
00186        return b;
00187 }
00188 
00189 void gtFont::setName(QString newName)
00190 {
00191        name = newName;
00192        setWeight(NO_WEIGHT);
00193        setSlant(NO_SLANT);
00194        setWidth(NO_WIDTH);
00195        parseName();
00196        useFullName = true;
00197 }
00198 
00199 void gtFont::setFamily(QString newFamily)
00200 {
00201        family = newFamily;
00202        useFullName = false;
00203 }
00204 
00205 QString gtFont::getFamily()
00206 {
00207        return family;
00208 }
00209 
00210 void gtFont::setWeight(FontWeight newWeight)
00211 {
00212        weight = fontWeights[newWeight];
00213        useFullName = false;
00214        if ((newWeight == ROMAN) || (newWeight == REGULAR))
00215        {
00216               setSlant(NO_SLANT);
00217               setWidth(NO_WIDTH);
00218        }
00219        if (weightIndex < 0)
00220        {
00221               weightIndex = 0;
00222               slantIndex = 1;
00223               widthIndex = 2;
00224        }
00225 }
00226 
00227 void gtFont::setWeight(QString newWeight)
00228 {
00229        weight = newWeight;
00230        useFullName = false;
00231        if ((newWeight == fontWeights[ROMAN]) || 
00232               (newWeight == fontWeights[REGULAR]))
00233        {
00234               setSlant(NO_SLANT);
00235               setWidth(NO_WIDTH);
00236        }
00237        if (weightIndex < 0)
00238        {
00239               weightIndex = 0;
00240               slantIndex = 1;
00241               widthIndex = 2;
00242        }
00243 }
00244 
00245 QString gtFont::getWeight()
00246 {
00247        return weight;
00248 }
00249 
00250 void gtFont::setSlant(FontSlant newSlant)
00251 {
00252        slant = fontSlants[newSlant];
00253        useFullName = false;
00254        if (newSlant != NO_SLANT)
00255        {
00256               if (weight == fontWeights[REGULAR])
00257                      setWeight(NO_WEIGHT);
00258               else if (weight == fontWeights[ROMAN])
00259                      setWeight(NO_WEIGHT);
00260        }
00261        if (slantIndex < 0)
00262        {
00263               weightIndex = 0;
00264               slantIndex = 1;
00265               widthIndex = 2;
00266        }
00267 }
00268 
00269 void gtFont::setSlant(QString newSlant)
00270 {
00271        slant = newSlant;
00272        useFullName = false;
00273        if (!newSlant.isEmpty())
00274        {
00275               if (weight == fontWeights[REGULAR])
00276                      setWeight(NO_WEIGHT);
00277               else if (weight == fontWeights[ROMAN])
00278                      setWeight(NO_WEIGHT);
00279        }
00280        if (slantIndex < 0)
00281        {
00282               weightIndex = 0;
00283               slantIndex = 1;
00284               widthIndex = 2;
00285        }
00286 }
00287 
00288 QString gtFont::getSlant()
00289 {
00290        return slant;
00291 }
00292 
00293 void gtFont::setWidth(FontWidth newWidth)
00294 {
00295        width = fontWidths[newWidth];
00296        useFullName = false;
00297        if (newWidth != NO_WIDTH)
00298        {
00299               if (weight == fontWeights[REGULAR])
00300                      setWeight(NO_WEIGHT);
00301               else if (weight == fontWeights[ROMAN])
00302                      setWeight(NO_WEIGHT);
00303        }
00304        if (widthIndex < 0)
00305        {
00306               weightIndex = 0;
00307               slantIndex = 1;
00308               widthIndex = 2;
00309        }
00310 }
00311 
00312 void gtFont::setWidth(QString newWidth)
00313 {
00314        width = newWidth;
00315        useFullName = false;
00316        if (!newWidth.isEmpty())
00317        {
00318               if (weight == fontWeights[REGULAR])
00319                      setWeight(NO_WEIGHT);
00320               else if (weight == fontWeights[ROMAN])
00321                      setWeight(NO_WEIGHT);
00322        }
00323        if (widthIndex < 0)
00324        {
00325               weightIndex = 0;
00326               slantIndex = 1;
00327               widthIndex = 2;
00328        }
00329 }
00330 
00331 QString gtFont::getWidth()
00332 {
00333        return width;
00334 }
00335 
00336 void gtFont::setSize(int newSize)
00337 {
00338        size = newSize;
00339 }
00340 
00341 void gtFont::setSize(double newSize)
00342 {
00343        double tmp = newSize * 10;
00344        size = static_cast<int>(tmp);
00345 }
00346 
00347 void gtFont::setColor(QString newColor)
00348 {
00349        color = newColor;
00350 }
00351 
00352 void gtFont::setShade(int newShade)
00353 {
00354        shade = newShade;
00355 }
00356 
00357 void gtFont::setStrokeColor(QString newColor)
00358 {
00359        strokeColor = newColor;
00360 }
00361 
00362 void gtFont::setStrokeShade(int newShade)
00363 {
00364        strokeShade = newShade;
00365 }
00366 
00367 QString gtFont::getName()
00368 {
00369        if (useFullName)
00370               return name;
00371 
00372        QString name2 = family + " ";
00373 
00374        if (weightIndex == 0)
00375               name2 += weight + " ";
00376        else if (slantIndex == 0) 
00377               name2 += slant + " ";
00378        else if (widthIndex == 0)
00379               name2 += width + " ";
00380 
00381        if (weightIndex == 1)
00382               name2 += weight + " ";
00383        else if (slantIndex == 1) 
00384               name2 += slant + " ";
00385        else if (widthIndex == 1)
00386               name2 += width + " ";
00387 
00388        if (weightIndex == 2)
00389               name2 += weight + " ";
00390        else if (slantIndex == 2) 
00391               name2 += slant + " ";
00392        else if (widthIndex == 2)
00393               name2 += width + " ";
00394 
00395        name2 += append;
00396        name2 = name2.simplifyWhiteSpace();
00397        return name2;
00398 }
00399 
00400 QString gtFont::getName(uint i)
00401 {
00402        QString fname = family + " ";
00403        switch (i)
00404        {
00405               case 0:
00406                      fname = fname + weight + " " + slant + " " + width + " " + append;
00407                      break;
00408               case 1:
00409                      fname = fname + weight + " " + width + " " + slant + " " + append;
00410                      break;
00411               case 2:
00412                      fname = fname + slant + " " + weight + " " + width + " " + append;
00413                      break;
00414               case 3:
00415                      fname = fname + slant + " " + width + " " + weight + " " + append;
00416                      break;
00417               case 4:
00418                      fname = fname + width + " " + weight + " " + slant + " " + append;
00419                      break;
00420               case 5:
00421                      fname = fname + width + " " + slant + " " + weight + " " + append;
00422                      break;
00423               case 6:
00424                      fname = fname + " " + append + " " + weight + " " + slant + " " + width;
00425                      break;
00426               case 7:
00427                      fname = fname + " " + append + " " + weight + " " + width + " " + slant;
00428                      break;
00429               case 8:
00430                      fname = fname + " " + append + " " + slant + " " + weight + " " + width;
00431                      break;
00432               case 9:
00433                      fname = fname + " " + append + " " + slant + " " + width + " " + weight;
00434                      break;
00435               case 10:
00436                      fname = fname + " " + append + " " + width + " " + weight + " " + slant;
00437                      break;
00438               case 11:
00439                      fname = fname + " " + append + " " + width + " " + slant + " " + weight;
00440                      break;
00441               case 12:
00442                      if ((append.isEmpty()) && (weight.isEmpty()) && (slant.isEmpty()) && (width.isEmpty()))
00443                             fname = fname + " " + fontWeights[REGULAR];
00444                      break;
00445               case 13:
00446                      if ((append.isEmpty()) && (weight.isEmpty()) && (slant.isEmpty()) && (width.isEmpty()))
00447                             fname = fname + " " + fontWeights[ROMAN];
00448                      break;
00449        }
00450        fname = fname.simplifyWhiteSpace();
00451        return fname;
00452 }
00453 
00454 int gtFont::getSize()
00455 {
00456        return size;
00457 }
00458 
00459 QString gtFont::getColor()
00460 {
00461        return color;
00462 }
00463 
00464 int gtFont::getShade()
00465 {
00466        return shade;
00467 }
00468 
00469 QString gtFont::getStrokeColor()
00470 {
00471        return strokeColor;
00472 }
00473 
00474 int gtFont::getStrokeShade()
00475 {
00476        return strokeShade;
00477 }
00478 
00479 void gtFont::noEffects()
00480 {
00481        fontEffects[NORMAL]        = true;
00482        fontEffects[UNDERLINE]     = false;
00483        fontEffects[STRIKETHROUGH] = false;
00484        fontEffects[SMALL_CAPS]    = false;
00485        fontEffects[SUPERSCRIPT]   = false;
00486        fontEffects[SUBSCRIPT]     = false;
00487        fontEffects[OUTLINE]       = false;
00488 }
00489 
00490 int gtFont::getHscale()
00491 {
00492        return hscale;
00493 }
00494 
00495 void    gtFont::setHscale(int newHscale)
00496 {
00497        hscale = newHscale;
00498 }
00499 
00500 int gtFont::getKerning()
00501 {
00502        return kerning;
00503 }
00504 
00505 void gtFont::setKerning(int newKerning)
00506 {
00507        kerning = newKerning;
00508 }
00509 
00510 void gtFont::parseName()
00511 {
00512        smallestIndex = -1;
00513        biggestIndex = - 1;
00514        index = -1;
00515        tmpWeightIndex = -1;
00516        tmpSlantIndex = -1;
00517        tmpWidthIndex = -1;
00518        parseWeight();
00519        parseSlant();
00520        parseWidth();
00521        parseFamily();
00522 
00523 
00524 }
00525 
00526 void gtFont::parseWeight()
00527 {
00528        bool found = false;
00529        for (int i = 1; i < FontWeightMAX; ++i)
00530        {
00531               index = name.find(fontWeights[i]); // f.e. Demi Bold
00532               QString tmpWeight = "";
00533               if ((index == -1) && (fontWeights[i].find(" ") != -1) && (fontWeights[i].find(" ") != 1))
00534               {
00535                      QString fw2 = fontWeights[i];
00536                      fw2.replace(" ", "-"); // f.e. Demi-Bold
00537                      index = name.find(fw2);
00538                      if (index == -1)
00539                      {
00540                             fw2 = fontWeights[i];
00541                             fw2.replace(" ", ""); // f.e. DemiBold
00542                             index = name.find(fw2);
00543                             if (index == -1)
00544                             {
00545                                    fw2 = fontWeights[i];
00546                                    fw2.replace(" B", " b"); // f.e. Demibold
00547                                    fw2.replace(" C", " c");
00548                                    fw2.replace(" H", " h");
00549                                    fw2.replace(" L", " l");
00550                                    fw2.replace(" ", "");
00551                                    index = name.find(fw2);
00552                                    if (index != -1)
00553                                           tmpWeight = fw2;
00554                             }
00555                             else
00556                                    tmpWeight = fw2;
00557                      }
00558                      else
00559                      {
00560                             tmpWeight = fw2;
00561                      }
00562               }
00563               else
00564                      tmpWeight = fontWeights[i];
00565               if (index != -1)
00566               {
00567                      weight = tmpWeight;
00568                      if (smallestIndex == -1 || smallestIndex > index)
00569                             smallestIndex = index;
00570                      if ((biggestIndex == -1) || (biggestIndex < index + static_cast<int>(tmpWeight.length()) - 1))
00571                             biggestIndex = index + tmpWeight.length();
00572                      found = true;
00573                      tmpWeightIndex = index;
00574                      break;
00575               }
00576        }
00577        if (!found)
00578               weight = fontWeights[NO_WEIGHT];
00579 }
00580 
00581 void gtFont::parseSlant()
00582 {
00583        bool found = false;
00584        for (int i = 1; i < FontSlantMAX; ++i)
00585        {
00586               index = name.find(fontSlants[i]);
00587               if (index != -1)
00588               {
00589                      slant = fontSlants[i];
00590                      if (smallestIndex == -1 || smallestIndex > index)
00591                             smallestIndex = index;
00592                      if ((biggestIndex == -1) || (biggestIndex < index + static_cast<int>(slant.length()) - 1))
00593                             biggestIndex = index + slant.length();
00594                      found = true;
00595                      tmpSlantIndex = index;
00596                      break;
00597               }
00598        }
00599        if (!found)
00600               slant = fontSlants[NO_SLANT];
00601 }
00602 
00603 void gtFont::parseWidth()
00604 {
00605        bool found = false;
00606        for (int i = 1; i < FontWidthMAX; ++i)
00607        {
00608               index = name.find(fontWidths[i]);
00609               QString tmpWidth = "";
00610               if ((index == -1) && (fontWidths[i].find(" ") != -1) && (fontWidths[i].find(" ") != 1))
00611               {
00612                      QString fw2 = fontWidths[i];
00613                      fw2.replace(" ", "-");
00614                      index = name.find(fw2);
00615                      if (index == -1)
00616                      {
00617                             fw2 = fontWidths[i];
00618                             fw2.replace(" ", "");
00619                             index = name.find(fw2);
00620                             if (index == -1)
00621                             {
00622                                    fw2 = fontWidths[i];
00623                                    fw2.replace(" B", " b");
00624                                    fw2.replace(" C", " c");
00625                                    fw2.replace(" H", " h");
00626                                    fw2.replace(" L", " l");
00627                                    fw2.replace(" ", "");
00628                                    index = name.find(fw2);
00629                                    if (index != -1)
00630                                           tmpWidth = fw2;
00631                             }
00632                             else
00633                                    tmpWidth = fw2;
00634                      }
00635                      else
00636                      {
00637                             tmpWidth = fw2;
00638                      }
00639               }
00640               else
00641                      tmpWidth = fontWidths[i];
00642               if (index != -1)
00643               {
00644                      width = tmpWidth;
00645                      if (smallestIndex == -1 || smallestIndex > index)
00646                             smallestIndex = index;
00647                      if ((biggestIndex == -1) || (biggestIndex < index + static_cast<int>(tmpWidth.length()) - 1))
00648                             biggestIndex = index + tmpWidth.length();
00649                      found = true;
00650                      tmpWidthIndex = index;
00651                      break;
00652               }
00653        }
00654        if (!found)
00655               width = fontWidths[NO_WIDTH];
00656 }
00657        
00658 void gtFont::parseFamily()
00659 {
00660        if (tmpWeightIndex < tmpSlantIndex)
00661        {
00662               weightIndex = 0;
00663               slantIndex  = 1;
00664               if (tmpWidthIndex < tmpWeightIndex)
00665               {
00666                      widthIndex = 0;
00667                      weightIndex = 1;
00668                      slantIndex = 2;
00669               }
00670               else if (tmpWidthIndex < tmpSlantIndex)
00671               {
00672                      widthIndex = 1;
00673                      slantIndex = 2;
00674               }
00675               else
00676                      widthIndex = 2;
00677        }
00678        else if (tmpWeightIndex > tmpSlantIndex)
00679        {
00680               slantIndex = 0;
00681               weightIndex = 1;
00682               if (tmpWidthIndex < tmpSlantIndex)
00683               {
00684                      widthIndex = 0;
00685                      slantIndex = 1;
00686                      weightIndex = 2;
00687               }
00688               else if (tmpWidthIndex < tmpWeightIndex)
00689               {
00690                      widthIndex = 1;
00691                      weightIndex = 2;
00692               }
00693               else
00694                      widthIndex = 2;
00695        }
00696        else
00697        {
00698               weightIndex = -2;
00699               widthIndex = -1;
00700               slantIndex = 0;
00701        }
00702 
00703        if (smallestIndex == -1)
00704               family = name;
00705        else
00706               family = name.left(smallestIndex);
00707        if (biggestIndex == -1 || biggestIndex >= (int) name.length())
00708               append = "";
00709        else
00710               append = name.right(name.length() - biggestIndex - 1);
00711        family = family.stripWhiteSpace();
00712 }
00713 
00714 int gtFont::find(const QString& where, const QString& what)
00715 {
00716        QString realWhat = " " + what;
00717        int index = where.findRev(realWhat); // f.e. Demi Bold
00718        if (index != -1)
00719        {
00720               if (index + realWhat.length() != where.length())
00721                      if (where[index + realWhat.length() + 1] != " ")
00722                             index = -1;
00723        }
00724        return index;
00725 }
00726 
00727 gtFont::~gtFont()
00728 {
00729 
00730 }