Main Page | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

cr-fonts.c

Go to the documentation of this file.
00001 /* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset: 8-*- */
00002 
00003 /*
00004  * This file is part of The Croco Library
00005  *
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of version 2.1 of 
00009  * the GNU Lesser General Public
00010  * License as published by the Free Software Foundation.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the 
00018  * GNU Lesser General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00021  * USA
00022  *
00023  *See COPYRIGHTS file for copyright information
00024  */
00025 
00026 #include "cr-fonts.h"
00027 #include <string.h>
00028 
00029 static enum CRStatus
00030 cr_font_family_to_string_real (CRFontFamily * a_this,
00031                                gboolean a_walk_list, GString ** a_string)
00032 {
00033         guchar *name = NULL;
00034         enum CRStatus result = CR_OK;
00035 
00036         if (!*a_string) {
00037                 *a_string = g_string_new (NULL);
00038                 g_return_val_if_fail (*a_string,
00039                                       CR_INSTANCIATION_FAILED_ERROR);
00040         }
00041 
00042         if (!a_this) {
00043                 g_string_append_printf (*a_string, "NULL");
00044                 return CR_OK;
00045         }
00046 
00047         switch (a_this->type) {
00048         case FONT_FAMILY_SANS_SERIF:
00049                 name = (guchar *) "sans-serif";
00050                 break;
00051 
00052         case FONT_FAMILY_SERIF:
00053                 name = (guchar *) "sans-serif";
00054                 break;
00055 
00056         case FONT_FAMILY_CURSIVE:
00057                 name = (guchar *) "cursive";
00058                 break;
00059 
00060         case FONT_FAMILY_FANTASY:
00061                 name = (guchar *) "fantasy";
00062                 break;
00063 
00064         case FONT_FAMILY_MONOSPACE:
00065                 name = (guchar *) "monospace";
00066                 break;
00067 
00068         case FONT_FAMILY_NON_GENERIC:
00069                 name = (guchar *) a_this->name;
00070                 break;
00071 
00072         default:
00073                 name = (guchar *) NULL;
00074                 break;
00075         }
00076 
00077         if (name) {
00078                 if (a_this->prev) {
00079                         g_string_append_printf (*a_string, ", %s", name);
00080                 } else {
00081                         g_string_append (*a_string, name);
00082                 }
00083         }
00084         if (a_walk_list == TRUE && a_this->next) {
00085                 result = cr_font_family_to_string_real (a_this->next,
00086                                                         TRUE, a_string);
00087         }
00088         return result;
00089 }
00090 
00091 static const gchar *
00092 cr_predefined_absolute_font_size_to_string (enum CRPredefinedAbsoluteFontSize
00093                                             a_code)
00094 {
00095         gchar *str = NULL;
00096 
00097         switch (a_code) {
00098         case FONT_SIZE_XX_SMALL:
00099                 str = (gchar *) "font-size-xx-small";
00100                 break;
00101         case FONT_SIZE_X_SMALL:
00102                 str = (gchar *) "font-size-x-small";
00103                 break;
00104         case FONT_SIZE_SMALL:
00105                 str = (gchar *) "font-size-small";
00106                 break;
00107         case FONT_SIZE_MEDIUM:
00108                 str = (gchar *) "font-size-medium";
00109                 break;
00110         case FONT_SIZE_LARGE:
00111                 str = (gchar *) "font-size-large";
00112                 break;
00113         case FONT_SIZE_X_LARGE:
00114                 str = (gchar *) "font-size-x-large";
00115                 break;
00116         case FONT_SIZE_XX_LARGE:
00117                 str = (gchar *) "font-size-xx-large";
00118                 break;
00119         default:
00120                 str = (gchar *) "unknown predefined absolute font size value";
00121         }
00122         return str;
00123 }
00124 
00125 static const gchar *
00126 cr_relative_font_size_to_string (enum CRRelativeFontSize a_code)
00127 {
00128         gchar *str = NULL;
00129 
00130         switch (a_code) {
00131         case FONT_SIZE_LARGER:
00132                 str = (gchar *) "font-size-larger";
00133                 break;
00134         case FONT_SIZE_SMALLER:
00135                 str = (gchar *) "font-size-smaller";
00136                 break;
00137         default:
00138                 str = (gchar *) "unknown relative font size value";
00139                 break;
00140         }
00141         return str;
00142 }
00143 
00144 CRFontFamily *
00145 cr_font_family_new (enum CRFontFamilyType a_type, guchar * a_name)
00146 {
00147         CRFontFamily *result = NULL;
00148 
00149         result = g_try_malloc (sizeof (CRFontFamily));
00150 
00151         if (!result) {
00152                 cr_utils_trace_info ("Out of memory");
00153                 return NULL;
00154         }
00155 
00156         memset (result, 0, sizeof (CRFontFamily));
00157         result->type = a_type;
00158 
00159         cr_font_family_set_name (result, a_name);
00160 
00161         return result;
00162 }
00163 
00164 guchar *
00165 cr_font_family_to_string (CRFontFamily * a_this,
00166                           gboolean a_walk_font_family_list)
00167 {
00168         enum CRStatus status = CR_OK;
00169         guchar *result = NULL;
00170         GString *stringue = NULL;
00171 
00172         if (!a_this) {
00173                 result = g_strdup ("NULL");
00174                 g_return_val_if_fail (result, NULL);
00175                 return result;
00176         }
00177         status = cr_font_family_to_string_real (a_this,
00178                                                 a_walk_font_family_list,
00179                                                 &stringue);
00180 
00181         if (status == CR_OK && stringue) {
00182                 result = stringue->str;
00183                 g_string_free (stringue, FALSE);
00184                 stringue = NULL;
00185 
00186         } else {
00187                 if (stringue) {
00188                         g_string_free (stringue, TRUE);
00189                         stringue = NULL;
00190                 }
00191         }
00192 
00193         return result;
00194 }
00195 enum CRStatus
00196 cr_font_family_set_name (CRFontFamily * a_this, guchar * a_name)
00197 {
00198         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00199 
00200         /*
00201          *only non generic font families can have a name
00202          */
00203 
00204         if (a_this->type != FONT_FAMILY_NON_GENERIC) {
00205                 return CR_BAD_PARAM_ERROR;
00206         }
00207 
00208         if (a_this->name) {
00209                 g_free (a_this->name);
00210                 a_this->name = NULL;
00211         }
00212 
00213         a_this->name = a_name;
00214         return CR_OK;
00215 }
00216 
00217 CRFontFamily *
00218 cr_font_family_append (CRFontFamily * a_this,
00219                        CRFontFamily * a_family_to_append)
00220 {
00221         CRFontFamily *cur_ff = NULL;
00222 
00223         g_return_val_if_fail (a_family_to_append, NULL);
00224 
00225         if (!a_this)
00226                 return a_family_to_append;
00227 
00228         for (cur_ff = a_this; cur_ff && cur_ff->next; cur_ff = cur_ff->next) ;
00229 
00230         cur_ff->next = a_family_to_append;
00231         a_family_to_append->prev = cur_ff;
00232 
00233         return a_this;
00234 
00235 }
00236 
00237 CRFontFamily *
00238 cr_font_family_prepend (CRFontFamily * a_this,
00239                         CRFontFamily * a_family_to_prepend)
00240 {
00241         g_return_val_if_fail (a_this && a_family_to_prepend, NULL);
00242 
00243         if (!a_this)
00244                 return a_family_to_prepend;
00245 
00246         a_family_to_prepend->next = a_this;
00247         a_this->prev = a_family_to_prepend;
00248 
00249         return CR_OK;
00250 }
00251 
00252 enum CRStatus
00253 cr_font_family_destroy (CRFontFamily * a_this)
00254 {
00255         CRFontFamily *cur_ff = NULL;
00256 
00257         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00258 
00259         for (cur_ff = a_this; cur_ff && cur_ff->next; cur_ff = cur_ff->next) ;
00260 
00261         for (; cur_ff; cur_ff = cur_ff->prev) {
00262                 if (a_this->name) {
00263                         g_free (a_this->name);
00264                         a_this->name = NULL;
00265                 }
00266 
00267                 if (cur_ff->next) {
00268                         g_free (cur_ff->next);
00269 
00270                 }
00271 
00272                 if (cur_ff->prev == NULL) {
00273                         g_free (a_this);
00274                 }
00275         }
00276 
00277         return CR_OK;
00278 }
00279 
00280 /***************************************************
00281  *'font-size' manipulation functions definitions
00282  ***************************************************/
00283 
00284 CRFontSize *
00285 cr_font_size_new (void)
00286 {
00287         CRFontSize *result = NULL;
00288 
00289         result = g_try_malloc (sizeof (CRFontSize));
00290         if (!result) {
00291                 cr_utils_trace_info ("Out of memory");
00292                 return NULL;
00293         }
00294         memset (result, 0, sizeof (CRFontSize));
00295 
00296         return result;
00297 }
00298 
00299 enum CRStatus
00300 cr_font_size_clear (CRFontSize * a_this)
00301 {
00302         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00303 
00304         switch (a_this->type) {
00305         case PREDEFINED_ABSOLUTE_FONT_SIZE:
00306         case RELATIVE_FONT_SIZE:
00307         case INHERITED_FONT_SIZE:
00308                 memset (a_this, 0, sizeof (CRFontSize));
00309                 break;
00310 
00311         case ABSOLUTE_FONT_SIZE:
00312                 if (a_this->value.absolute) {
00313                         cr_num_destroy (a_this->value.absolute);
00314                 }
00315                 memset (a_this, 0, sizeof (CRFontSize));
00316                 break;
00317 
00318         default:
00319                 return CR_UNKNOWN_TYPE_ERROR;
00320         }
00321 
00322         return CR_OK;
00323 }
00324 
00325 enum CRStatus
00326 cr_font_size_copy (CRFontSize * a_dst, CRFontSize * a_src)
00327 {
00328         g_return_val_if_fail (a_dst && a_src, CR_BAD_PARAM_ERROR);
00329 
00330         switch (a_src->type) {
00331         case PREDEFINED_ABSOLUTE_FONT_SIZE:
00332         case RELATIVE_FONT_SIZE:
00333         case INHERITED_FONT_SIZE:
00334                 cr_font_size_clear (a_dst);
00335                 memcpy (a_dst, a_src, sizeof (CRFontSize));
00336                 break;
00337 
00338         case ABSOLUTE_FONT_SIZE:
00339                 if (a_src->value.absolute) {
00340                         cr_font_size_clear (a_dst);
00341                         if (!a_dst->value.absolute) {
00342                                 a_dst->value.absolute = cr_num_new ();
00343                         }
00344                         cr_num_copy (a_dst->value.absolute,
00345                                      a_src->value.absolute);
00346                         a_dst->type = a_src->type;
00347                 }
00348                 break;
00349 
00350         default:
00351                 return CR_UNKNOWN_TYPE_ERROR;
00352         }
00353         return CR_OK;
00354 }
00355 
00356 gchar *
00357 cr_font_size_to_string (CRFontSize * a_this)
00358 {
00359         gchar *str = NULL;
00360 
00361         if (!a_this) {
00362                 str = g_strdup ("NULL");
00363                 g_return_val_if_fail (str, NULL);
00364                 return str;
00365         }
00366         switch (a_this->type) {
00367         case PREDEFINED_ABSOLUTE_FONT_SIZE:
00368                 str = g_strdup (cr_predefined_absolute_font_size_to_string
00369                                 (a_this->value.predefined));
00370                 break;
00371         case ABSOLUTE_FONT_SIZE:
00372                 str = cr_num_to_string (a_this->value.absolute);
00373                 break;
00374         case RELATIVE_FONT_SIZE:
00375                 str = g_strdup (cr_relative_font_size_to_string
00376                                 (a_this->value.relative));
00377                 break;
00378         case INHERITED_FONT_SIZE:
00379                 str = g_strdup ("inherited");
00380                 break;
00381         default:
00382                 break;
00383         }
00384         return str;
00385 }
00386 
00387 gchar *
00388 cr_font_size_adjust_to_string (CRFontSizeAdjust * a_this)
00389 {
00390         gchar *str = NULL;
00391 
00392         if (!a_this) {
00393                 str = g_strdup ("NULL");
00394                 g_return_val_if_fail (str, NULL);
00395                 return str;
00396         }
00397 
00398         switch (a_this->type) {
00399         case FONT_SIZE_ADJUST_NONE:
00400                 str = g_strdup ("font-size-adjust-none");
00401                 break;
00402         case FONT_SIZE_ADJUST_NUMBER:
00403                 if (a_this->num)
00404                         str = cr_num_to_string (a_this->num);
00405                 else
00406                         str = g_strdup ("font-size-adjust-NULL");
00407                 break;
00408         case FONT_SIZE_ADJUST_INHERIT:
00409                 str = g_strdup ("font-size-adjust-inherit");
00410         }
00411         return str;
00412 }
00413 
00414 const gchar *
00415 cr_font_style_to_string (enum CRFontStyle a_code)
00416 {
00417         gchar *str = NULL;
00418 
00419         switch (a_code) {
00420         case FONT_STYLE_NORMAL:
00421                 str = (gchar *) "font-style-normal";
00422                 break;
00423         case FONT_STYLE_ITALIC:
00424                 str = (gchar *) "font-style-italic";
00425                 break;
00426         case FONT_STYLE_OBLIQUE:
00427                 str = (gchar *) "font-style-oblique";
00428                 break;
00429         case FONT_STYLE_INHERIT:
00430                 str = (gchar *) "font-style-inherit";
00431                 break;
00432         default:
00433                 str = (gchar *) "font-style";
00434                 break;
00435         }
00436         return str;
00437 }
00438 
00439 const gchar *
00440 cr_font_variant_to_string (enum CRFontVariant a_code)
00441 {
00442         gchar *str = NULL;
00443 
00444         switch (a_code) {
00445         case FONT_VARIANT_NORMAL:
00446                 str = (gchar *) "font-variant-normal";
00447                 break;
00448         case FONT_VARIANT_SMALL_CAPS:
00449                 str = (gchar *) "font-variant-small-caps";
00450                 break;
00451         case FONT_VARIANT_INHERIT:
00452                 str = (gchar *) "font-variant-inherent";
00453                 break;
00454         }
00455         return str;
00456 }
00457 
00458 const gchar *
00459 cr_font_weight_to_string (enum CRFontWeight a_code)
00460 {
00461         gchar *str = NULL;
00462 
00463         switch (a_code) {
00464         case FONT_WEIGHT_NORMAL:
00465                 str = (gchar *) "font-weight-normal";
00466                 break;
00467         case FONT_WEIGHT_BOLD:
00468                 str = (gchar *) "font-weight-bold";
00469                 break;
00470         case FONT_WEIGHT_BOLDER:
00471                 str = (gchar *) "font-weight-bolder";
00472                 break;
00473         case FONT_WEIGHT_LIGHTER:
00474                 str = (gchar *) "font-weight-lighter";
00475                 break;
00476         case FONT_WEIGHT_100:
00477                 str = (gchar *) "font-weight-100";
00478                 break;
00479         case FONT_WEIGHT_200:
00480                 str = (gchar *) "font-weight-200";
00481                 break;
00482         case FONT_WEIGHT_300:
00483                 str = (gchar *) "font-weight-300";
00484                 break;
00485         case FONT_WEIGHT_400:
00486                 str = (gchar *) "font-weight-400";
00487                 break;
00488         case FONT_WEIGHT_500:
00489                 str = (gchar *) "font-weight-500";
00490                 break;
00491         case FONT_WEIGHT_600:
00492                 str = (gchar *) "font-weight-600";
00493                 break;
00494         case FONT_WEIGHT_700:
00495                 str = (gchar *) "font-weight-700";
00496                 break;
00497         case FONT_WEIGHT_800:
00498                 str = (gchar *) "font-weight-800";
00499                 break;
00500         case FONT_WEIGHT_900:
00501                 str = (gchar *) "font-weight-900";
00502                 break;
00503         case FONT_WEIGHT_INHERIT:
00504                 str = (gchar *) "font-weight-inherit";
00505                 break;
00506         default:
00507                 str = (gchar *) "unknown font-weight property value";
00508                 break;
00509         }
00510         return str;
00511 }
00512 
00513 const gchar *
00514 cr_font_stretch_to_string (enum CRFontStretch a_code)
00515 {
00516         gchar *str = NULL;
00517 
00518         switch (a_code) {
00519         case FONT_STRETCH_NORMAL:
00520                 str = (gchar *) "font-stretch-normal";
00521                 break;
00522         case FONT_STRETCH_WIDER:
00523                 str = (gchar *) "font-stretch-wider";
00524                 break;
00525         case FONT_STRETCH_NARROWER:
00526                 str = (gchar *) "font-stretch-narrower";
00527                 break;
00528         case FONT_STRETCH_ULTRA_CONDENSED:
00529                 str = (gchar *) "font-stretch-ultra-condensed";
00530                 break;
00531         case FONT_STRETCH_EXTRA_CONDENSED:
00532                 str = (gchar *) "font-stretch-extra-condensed";
00533                 break;
00534         case FONT_STRETCH_CONDENSED:
00535                 str = (gchar *) "font-stretch-condensed";
00536                 break;
00537         case FONT_STRETCH_SEMI_CONDENSED:
00538                 str = (gchar *) "font-stretch-semi-condensed";
00539                 break;
00540         case FONT_STRETCH_SEMI_EXPANDED:
00541                 str = (gchar *) "font-stretch-semi-expanded";
00542                 break;
00543         case FONT_STRETCH_EXPANDED:
00544                 str = (gchar *) "font-stretch-expanded";
00545                 break;
00546         case FONT_STRETCH_EXTRA_EXPANDED:
00547                 str = (gchar *) "font-stretch-extra-expaned";
00548                 break;
00549         case FONT_STRETCH_ULTRA_EXPANDED:
00550                 str = (gchar *) "font-stretch-ultra-expanded";
00551                 break;
00552         case FONT_STRETCH_INHERIT:
00553                 str = (gchar *) "font-stretch-inherit";
00554                 break;
00555         }
00556         return str;
00557 }
00558 
00559 void
00560 cr_font_size_destroy (CRFontSize * a_font_size)
00561 {
00562         g_return_if_fail (a_font_size);
00563 
00564         if (a_font_size->type == ABSOLUTE_FONT_SIZE
00565             && a_font_size->value.absolute) {
00566                 cr_num_destroy (a_font_size->value.absolute);
00567                 a_font_size->value.absolute = NULL;
00568         }
00569 }
00570 
00571 /*******************************************************
00572  *'font-size-adjust' manipulation function definition
00573  *******************************************************/
00574 
00575 CRFontSizeAdjust *
00576 cr_font_size_adjust_new (void)
00577 {
00578         CRFontSizeAdjust *result = NULL;
00579 
00580         result = g_try_malloc (sizeof (CRFontSizeAdjust));
00581         if (!result) {
00582                 cr_utils_trace_info ("Out of memory");
00583                 return NULL;
00584         }
00585         memset (result, 0, sizeof (CRFontSizeAdjust));
00586 
00587         return result;
00588 }
00589 
00590 void
00591 cr_font_size_adjust_destroy (CRFontSizeAdjust * a_this)
00592 {
00593         g_return_if_fail (a_this);
00594 
00595         if (a_this->type == FONT_SIZE_ADJUST_NUMBER && a_this->num) {
00596                 cr_num_destroy (a_this->num);
00597                 a_this->num = NULL;
00598         }
00599 }

Generated on Sat Mar 20 02:38:42 2004 for Libcroco by doxygen 1.3.5