]> granicus.if.org Git - imagemagick/blobdiff - magick/type.c
(no commit message)
[imagemagick] / magick / type.c
index d599358ed72572a951ef0bc23c78e87b96dc6471..6efa28630fa8872bd2f1c19d88cd3b696047c92c 100644 (file)
@@ -53,6 +53,7 @@
 #include "magick/semaphore.h"
 #include "magick/splay-tree.h"
 #include "magick/string_.h"
+#include "magick/string-private.h"
 #include "magick/type.h"
 #include "magick/token.h"
 #include "magick/utility.h"
@@ -89,7 +90,7 @@
 #define FC_WEIGHT_HEAVY            FC_WEIGHT_BLACK
 #endif
 #endif
-#if defined(__WINDOWS__)
+#if defined(MAGICKCORE_WINDOWS_SUPPORT)
 # include "magick/nt-feature.h"
 #endif
 \f
@@ -133,34 +134,6 @@ static MagickBooleanType
 %                                                                             %
 %                                                                             %
 %                                                                             %
-+   D e s t r o y T y p e C o m p o n e n t                                   %
-%                                                                             %
-%                                                                             %
-%                                                                             %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%
-%  DestroyTypeComponent() destroy type component.
-%
-%  The format of the DestroyTypeComponent method is:
-%
-%      void DestroyTypeComponent(void)
-%
-*/
-MagickExport void DestroyTypeComponent(void)
-{
-  AcquireSemaphoreInfo(&type_semaphore);
-  if (type_list != (SplayTreeInfo *) NULL)
-    type_list=DestroySplayTree(type_list);
-  instantiate_type=MagickFalse;
-  RelinquishSemaphoreInfo(type_semaphore);
-  DestroySemaphoreInfo(&type_semaphore);
-}
-\f
-/*
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%                                                                             %
-%                                                                             %
-%                                                                             %
 +   G e t T y p e I n f o                                                     %
 %                                                                             %
 %                                                                             %
@@ -220,7 +193,7 @@ MagickExport const TypeInfo *GetTypeInfo(const char *name,
 %
 %      const TypeInfo *GetTypeInfoByFamily(const char *family,
 %        const StyleType style,const StretchType stretch,
-%        const unsigned long weight,ExceptionInfo *exception)
+%        const size_t weight,ExceptionInfo *exception)
 %
 %  A description of each parameter follows:
 %
@@ -236,16 +209,16 @@ MagickExport const TypeInfo *GetTypeInfo(const char *name,
 %
 */
 
-static inline unsigned long MagickMax(const unsigned long x,
-  const unsigned long y)
+static inline size_t MagickMax(const size_t x,
+  const size_t y)
 {
   if (x > y)
     return(x);
   return(y);
 }
 
-static inline unsigned long MagickMin(const unsigned long x,
-  const unsigned long y)
+static inline size_t MagickMin(const size_t x,
+  const size_t y)
 {
   if (x < y)
     return(x);
@@ -253,7 +226,7 @@ static inline unsigned long MagickMin(const unsigned long x,
 }
 
 MagickExport const TypeInfo *GetTypeInfoByFamily(const char *family,
-  const StyleType style,const StretchType stretch,const unsigned long weight,
+  const StyleType style,const StretchType stretch,const size_t weight,
   ExceptionInfo *exception)
 {
   typedef struct _Fontmap
@@ -266,16 +239,16 @@ MagickExport const TypeInfo *GetTypeInfoByFamily(const char *family,
   const TypeInfo
     *type_info;
 
-  long
+  ssize_t
     range;
 
   register const TypeInfo
     *p;
 
-  register long
+  register ssize_t
     i;
 
-  static Fontmap
+  static const Fontmap
     fontmap[] =
     {
       { "fixed", "courier" },
@@ -288,7 +261,7 @@ MagickExport const TypeInfo *GetTypeInfoByFamily(const char *family,
       { NULL, NULL }
     };
 
-  unsigned long
+  size_t
     max_score,
     score;
 
@@ -298,7 +271,7 @@ MagickExport const TypeInfo *GetTypeInfoByFamily(const char *family,
   (void) GetTypeInfo("*",exception);
   if (type_list == (SplayTreeInfo *) NULL)
     return((TypeInfo *) NULL);
-  AcquireSemaphoreInfo(&type_semaphore);
+  LockSemaphoreInfo(type_semaphore);
   ResetSplayTreeIterator(type_list);
   type_info=(const TypeInfo *) NULL;
   p=(const TypeInfo *) GetNextValueInSplayTree(type_list);
@@ -343,14 +316,14 @@ MagickExport const TypeInfo *GetTypeInfoByFamily(const char *family,
     type_info=p;
     break;
   }
-  RelinquishSemaphoreInfo(type_semaphore);
+  UnlockSemaphoreInfo(type_semaphore);
   if (type_info != (const TypeInfo *) NULL)
     return(type_info);
   /*
     Check for types in the same family.
   */
   max_score=0;
-  AcquireSemaphoreInfo(&type_semaphore);
+  LockSemaphoreInfo(type_semaphore);
   ResetSplayTreeIterator(type_list);
   p=(const TypeInfo *) GetNextValueInSplayTree(type_list);
   while (p != (const TypeInfo *) NULL)
@@ -385,15 +358,15 @@ MagickExport const TypeInfo *GetTypeInfoByFamily(const char *family,
     if (weight == 0)
       score+=16;
     else
-      score+=(16*(800-((long) MagickMax(MagickMin(weight,900),p->weight)-
-        (long) MagickMin(MagickMin(weight,900),p->weight))))/800;
+      score+=(16*(800-((ssize_t) MagickMax(MagickMin(weight,900),p->weight)-
+        (ssize_t) MagickMin(MagickMin(weight,900),p->weight))))/800;
     if ((stretch == UndefinedStretch) || (stretch == AnyStretch))
       score+=8;
     else
       {
-        range=(long) UltraExpandedStretch-(long) NormalStretch;
-        score+=(8*(range-((long) MagickMax(stretch,p->stretch)-
-          (long) MagickMin(stretch,p->stretch))))/range;
+        range=(ssize_t) UltraExpandedStretch-(ssize_t) NormalStretch;
+        score+=(8*(range-((ssize_t) MagickMax(stretch,p->stretch)-
+          (ssize_t) MagickMin(stretch,p->stretch))))/range;
       }
     if (score > max_score)
       {
@@ -402,7 +375,7 @@ MagickExport const TypeInfo *GetTypeInfoByFamily(const char *family,
       }
     p=(const TypeInfo *) GetNextValueInSplayTree(type_list);
   }
-  RelinquishSemaphoreInfo(type_semaphore);
+  UnlockSemaphoreInfo(type_semaphore);
   if (type_info != (const TypeInfo *) NULL)
     return(type_info);
   /*
@@ -451,7 +424,7 @@ MagickExport const TypeInfo *GetTypeInfoByFamily(const char *family,
 %  The format of the GetTypeInfoList function is:
 %
 %      const TypeInfo **GetTypeInfoList(const char *pattern,
-%        unsigned long *number_fonts,ExceptionInfo *exception)
+%        size_t *number_fonts,ExceptionInfo *exception)
 %
 %  A description of each parameter follows:
 %
@@ -485,7 +458,7 @@ static int TypeInfoCompare(const void *x,const void *y)
 #endif
 
 MagickExport const TypeInfo **GetTypeInfoList(const char *pattern,
-  unsigned long *number_fonts,ExceptionInfo *exception)
+  size_t *number_fonts,ExceptionInfo *exception)
 {
   const TypeInfo
     **fonts;
@@ -493,7 +466,7 @@ MagickExport const TypeInfo **GetTypeInfoList(const char *pattern,
   register const TypeInfo
     *p;
 
-  register long
+  register ssize_t
     i;
 
   /*
@@ -501,7 +474,7 @@ MagickExport const TypeInfo **GetTypeInfoList(const char *pattern,
   */
   assert(pattern != (char *) NULL);
   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
-  assert(number_fonts != (unsigned long *) NULL);
+  assert(number_fonts != (size_t *) NULL);
   *number_fonts=0;
   p=GetTypeInfo("*",exception);
   if (p == (const TypeInfo *) NULL)
@@ -513,7 +486,7 @@ MagickExport const TypeInfo **GetTypeInfoList(const char *pattern,
   /*
     Generate type list.
   */
-  AcquireSemaphoreInfo(&type_semaphore);
+  LockSemaphoreInfo(type_semaphore);
   ResetSplayTreeIterator(type_list);
   p=(const TypeInfo *) GetNextValueInSplayTree(type_list);
   for (i=0; p != (const TypeInfo *) NULL; )
@@ -523,10 +496,10 @@ MagickExport const TypeInfo **GetTypeInfoList(const char *pattern,
       fonts[i++]=p;
     p=(const TypeInfo *) GetNextValueInSplayTree(type_list);
   }
-  RelinquishSemaphoreInfo(type_semaphore);
+  UnlockSemaphoreInfo(type_semaphore);
   qsort((void *) fonts,(size_t) i,sizeof(*fonts),TypeInfoCompare);
   fonts[i]=(TypeInfo *) NULL;
-  *number_fonts=(unsigned long) i;
+  *number_fonts=(size_t) i;
   return(fonts);
 }
 \f
@@ -545,7 +518,7 @@ MagickExport const TypeInfo **GetTypeInfoList(const char *pattern,
 %
 %  The format of the GetTypeList function is:
 %
-%      char **GetTypeList(const char *pattern,unsigned long *number_fonts,
+%      char **GetTypeList(const char *pattern,size_t *number_fonts,
 %        ExceptionInfo *exception)
 %
 %  A description of each parameter follows:
@@ -577,7 +550,7 @@ static int TypeCompare(const void *x,const void *y)
 }
 #endif
 
-MagickExport char **GetTypeList(const char *pattern,unsigned long *number_fonts,
+MagickExport char **GetTypeList(const char *pattern,size_t *number_fonts,
   ExceptionInfo *exception)
 {
   char
@@ -586,7 +559,7 @@ MagickExport char **GetTypeList(const char *pattern,unsigned long *number_fonts,
   register const TypeInfo
     *p;
 
-  register long
+  register ssize_t
     i;
 
   /*
@@ -594,7 +567,7 @@ MagickExport char **GetTypeList(const char *pattern,unsigned long *number_fonts,
   */
   assert(pattern != (char *) NULL);
   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
-  assert(number_fonts != (unsigned long *) NULL);
+  assert(number_fonts != (size_t *) NULL);
   *number_fonts=0;
   p=GetTypeInfo("*",exception);
   if (p == (const TypeInfo *) NULL)
@@ -606,7 +579,7 @@ MagickExport char **GetTypeList(const char *pattern,unsigned long *number_fonts,
   /*
     Generate type list.
   */
-  AcquireSemaphoreInfo(&type_semaphore);
+  LockSemaphoreInfo(type_semaphore);
   ResetSplayTreeIterator(type_list);
   p=(const TypeInfo *) GetNextValueInSplayTree(type_list);
   for (i=0; p != (const TypeInfo *) NULL; )
@@ -616,10 +589,10 @@ MagickExport char **GetTypeList(const char *pattern,unsigned long *number_fonts,
       fonts[i++]=ConstantString(p->name);
     p=(const TypeInfo *) GetNextValueInSplayTree(type_list);
   }
-  RelinquishSemaphoreInfo(type_semaphore);
+  UnlockSemaphoreInfo(type_semaphore);
   qsort((void *) fonts,(size_t) i,sizeof(*fonts),TypeCompare);
   fonts[i]=(char *) NULL;
-  *number_fonts=(unsigned long) i;
+  *number_fonts=(size_t) i;
   return(fonts);
 }
 \f
@@ -679,7 +652,7 @@ MagickExport MagickBooleanType LoadFontConfigFonts(SplayTreeInfo *type_list,
     width,
     weight;
 
-  register long
+  register ssize_t
     i;
 
   TypeInfo
@@ -710,7 +683,7 @@ MagickExport MagickBooleanType LoadFontConfigFonts(SplayTreeInfo *type_list,
       FcConfigDestroy(font_config);
       return(MagickFalse);
     }
-  for (i=0; i < (long) font_set->nfont; i++)
+  for (i=0; i < (ssize_t) font_set->nfont; i++)
   {
     status=FcPatternGetString(font_set->fonts[i],FC_FAMILY,0,&family);
     if (status != FcResultMatch)
@@ -722,7 +695,7 @@ MagickExport MagickBooleanType LoadFontConfigFonts(SplayTreeInfo *type_list,
     GetPathComponent((const char *) file,ExtensionPath,extension);
     if ((*extension != '\0') && (LocaleCompare(extension,"gz") == 0))
       continue;
-    type_info=(TypeInfo *) AcquireMagickMemory(sizeof(*type_info));
+    type_info=(TypeInfo *) AcquireAlignedMemory(1,sizeof(*type_info));
     if (type_info == (TypeInfo *) NULL)
       continue;
     (void) ResetMagickMemory(type_info,0,sizeof(*type_info));
@@ -799,12 +772,14 @@ static MagickBooleanType InitializeTypeList(ExceptionInfo *exception)
   if ((type_list == (SplayTreeInfo *) NULL) &&
       (instantiate_type == MagickFalse))
     {
-      AcquireSemaphoreInfo(&type_semaphore);
+      if (type_semaphore == (SemaphoreInfo *) NULL)
+        AcquireSemaphoreInfo(&type_semaphore);
+      LockSemaphoreInfo(type_semaphore);
       if ((type_list == (SplayTreeInfo *) NULL) &&
           (instantiate_type == MagickFalse))
         {
           (void) LoadTypeLists(MagickTypeFilename,exception);
-#if defined(__WINDOWS__)
+#if defined(MAGICKCORE_WINDOWS_SUPPORT)
           (void) NTLoadTypeLists(type_list,exception);
 #endif
 #if defined(MAGICKCORE_FONTCONFIG_DELEGATE)
@@ -812,7 +787,7 @@ static MagickBooleanType InitializeTypeList(ExceptionInfo *exception)
 #endif
           instantiate_type=MagickTrue;
         }
-      RelinquishSemaphoreInfo(type_semaphore);
+      UnlockSemaphoreInfo(type_semaphore);
     }
   return(type_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
 }
@@ -822,31 +797,6 @@ static MagickBooleanType InitializeTypeList(ExceptionInfo *exception)
 %                                                                             %
 %                                                                             %
 %                                                                             %
-+   I n s t a n t i a t e T y p e C o m p o n e n t                           %
-%                                                                             %
-%                                                                             %
-%                                                                             %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%
-%  InstantiateTypeComponent() instantiates the type component.
-%
-%  The format of the InstantiateTypeComponent method is:
-%
-%      MagickBooleanType InstantiateTypeComponent(void)
-%
-*/
-MagickExport MagickBooleanType InstantiateTypeComponent(void)
-{
-  AcquireSemaphoreInfo(&type_semaphore);
-  RelinquishSemaphoreInfo(type_semaphore);
-  return(MagickTrue);
-}
-\f
-/*
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%                                                                             %
-%                                                                             %
-%                                                                             %
 %  L i s t T y p e I n f o                                                    %
 %                                                                             %
 %                                                                             %
@@ -882,10 +832,10 @@ MagickExport MagickBooleanType ListTypeInfo(FILE *file,ExceptionInfo *exception)
   const TypeInfo
     **type_info;
 
-  register long
+  register ssize_t
     i;
 
-  unsigned long
+  size_t
     number_fonts;
 
   if (file == (FILE *) NULL)
@@ -896,7 +846,7 @@ MagickExport MagickBooleanType ListTypeInfo(FILE *file,ExceptionInfo *exception)
     return(MagickFalse);
   *weight='\0';
   path=(const char *) NULL;
-  for (i=0; i < (long) number_fonts; i++)
+  for (i=0; i < (ssize_t) number_fonts; i++)
   {
     if (type_info[i]->stealth != MagickFalse)
       continue;
@@ -916,7 +866,8 @@ MagickExport MagickBooleanType ListTypeInfo(FILE *file,ExceptionInfo *exception)
     glyphs="unknown";
     if (type_info[i]->glyphs != (char *) NULL)
       glyphs=type_info[i]->glyphs;
-    (void) FormatMagickString(weight,MaxTextExtent,"%lu",type_info[i]->weight);
+    (void) FormatMagickString(weight,MaxTextExtent,"%.20g",(double)
+      type_info[i]->weight);
     (void) fprintf(file,"  Font: %s\n",name);
     (void) fprintf(file,"    family: %s\n",family);
     (void) fprintf(file,"    style: %s\n",style);
@@ -946,7 +897,7 @@ MagickExport MagickBooleanType ListTypeInfo(FILE *file,ExceptionInfo *exception)
 %  The format of the LoadTypeList method is:
 %
 %      MagickBooleanType LoadTypeList(const char *xml,const char *filename,
-%        const unsigned long depth,ExceptionInfo *exception)
+%        const size_t depth,ExceptionInfo *exception)
 %
 %  A description of each parameter follows:
 %
@@ -988,7 +939,7 @@ static void *DestroyTypeNode(void *type_info)
 }
 
 static MagickBooleanType LoadTypeList(const char *xml,const char *filename,
-  const unsigned long depth,ExceptionInfo *exception)
+  const size_t depth,ExceptionInfo *exception)
 {
   char
     font_path[MaxTextExtent],
@@ -1025,7 +976,7 @@ static MagickBooleanType LoadTypeList(const char *xml,const char *filename,
   status=MagickTrue;
   type_info=(TypeInfo *) NULL;
   token=AcquireString(xml);
-#if defined(__WINDOWS__)
+#if defined(MAGICKCORE_WINDOWS_SUPPORT)
   /*
     Determine the Ghostscript font path.
   */
@@ -1113,7 +1064,7 @@ static MagickBooleanType LoadTypeList(const char *xml,const char *filename,
         /*
           Type element.
         */
-        type_info=(TypeInfo *) AcquireMagickMemory(sizeof(*type_info));
+        type_info=(TypeInfo *) AcquireAlignedMemory(1,sizeof(*type_info));
         if (type_info == (TypeInfo *) NULL)
           ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
         (void) ResetMagickMemory(type_info,0,sizeof(*type_info));
@@ -1153,7 +1104,7 @@ static MagickBooleanType LoadTypeList(const char *xml,const char *filename,
       {
         if (LocaleCompare((char *) keyword,"face") == 0)
           {
-            type_info->face=(unsigned long) atol(token);
+            type_info->face=StringToUnsignedLong(token);
             break;
           }
         if (LocaleCompare((char *) keyword,"family") == 0)
@@ -1187,7 +1138,7 @@ static MagickBooleanType LoadTypeList(const char *xml,const char *filename,
               *path;
 
             path=ConstantString(token);
-#if defined(__WINDOWS__)
+#if defined(MAGICKCORE_WINDOWS_SUPPORT)
             if (strchr(path,'@') != (char *) NULL)
               SubstituteString(&path,"@ghostscript_font_path@",font_path);
 #endif
@@ -1202,6 +1153,11 @@ static MagickBooleanType LoadTypeList(const char *xml,const char *filename,
                   MaxTextExtent);
                 (void) ConcatenateMagickString(font_path,token,MaxTextExtent);
                 path=ConstantString(font_path);
+                if (IsPathAccessible(path) == MagickFalse)
+                  {
+                    path=DestroyString(path);
+                    path=ConstantString(token);
+                  }
               }
             type_info->glyphs=path;
             break;
@@ -1217,7 +1173,7 @@ static MagickBooleanType LoadTypeList(const char *xml,const char *filename,
               *path;
 
             path=ConstantString(token);
-#if defined(__WINDOWS__)
+#if defined(MAGICKCORE_WINDOWS_SUPPORT)
             if (strchr(path,'@') != (char *) NULL)
               SubstituteString(&path,"@ghostscript_font_path@",font_path);
 #endif
@@ -1275,7 +1231,7 @@ static MagickBooleanType LoadTypeList(const char *xml,const char *filename,
       {
         if (LocaleCompare((char *) keyword,"weight") == 0)
           {
-            type_info->weight=(unsigned long) atol(token);
+            type_info->weight=StringToUnsignedLong(token);
             if (LocaleCompare(token,"bold") == 0)
               type_info->weight=700;
             if (LocaleCompare(token,"normal") == 0)
@@ -1374,3 +1330,57 @@ static MagickBooleanType LoadTypeLists(const char *filename,
   return(status != 0 ? MagickTrue : MagickFalse);
 #endif
 }
+\f
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%                                                                             %
+%                                                                             %
+%                                                                             %
++   T y p e C o m p o n e n t G e n e s i s                                   %
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+%  TypeComponentGenesis() instantiates the type component.
+%
+%  The format of the TypeComponentGenesis method is:
+%
+%      MagickBooleanType TypeComponentGenesis(void)
+%
+*/
+MagickExport MagickBooleanType TypeComponentGenesis(void)
+{
+  AcquireSemaphoreInfo(&type_semaphore);
+  return(MagickTrue);
+}
+\f
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%                                                                             %
+%                                                                             %
+%                                                                             %
++   T y p e C o m p o n e n t T e r m i n u s                                 %
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+%  TypeComponentTerminus() destroy type component.
+%
+%  The format of the TypeComponentTerminus method is:
+%
+%      void TypeComponentTerminus(void)
+%
+*/
+MagickExport void TypeComponentTerminus(void)
+{
+  if (type_semaphore == (SemaphoreInfo *) NULL)
+    AcquireSemaphoreInfo(&type_semaphore);
+  LockSemaphoreInfo(type_semaphore);
+  if (type_list != (SplayTreeInfo *) NULL)
+    type_list=DestroySplayTree(type_list);
+  instantiate_type=MagickFalse;
+  UnlockSemaphoreInfo(type_semaphore);
+  DestroySemaphoreInfo(&type_semaphore);
+}