lib/gifLabel.c

Go to the documentation of this file.
00001 /* gifLabel - create labels as GIF files. */
00002 
00003 #include "common.h"
00004 #include "memgfx.h"
00005 #include "gifLabel.h"
00006 
00007 static char const rcsid[] = "$Id: gifLabel.c,v 1.7 2005/06/06 20:20:10 galt Exp $";
00008 
00009 int gifLabelMaxWidth(char **labels, int labelCount)
00010 /* Return maximum pixel width of labels.  It's ok to have
00011  * NULLs in labels array. */
00012 {
00013 int width = 0, w, i;
00014 MgFont *font = mgMediumFont();
00015 for (i=0; i<labelCount; ++i)
00016     {
00017     char *label = labels[i];
00018     if (label != NULL)
00019         {
00020         w = mgFontStringWidth(font, labels[i]);
00021         if (w > width)
00022             width = w;
00023         }
00024     }
00025 width += 2;
00026 return width;
00027 }
00028 
00029 static struct memGfx *altColorLabels(char **labels, int labelCount, int width)
00030 /* Return a memory image with alternating colors. */
00031 {
00032 struct memGfx *mg = NULL;
00033 Color c1,c2;
00034 MgFont *font = mgMediumFont();
00035 int lineHeight = mgFontLineHeight(font)-1;
00036 int height = lineHeight * labelCount, i;
00037 int y = 0;
00038 
00039 /* Allocate picture and set up colors. */
00040 mg = mgNew(width, height);
00041 c1 = mgFindColor(mg, 0xE0, 0xE0, 0xFF);
00042 c2 = mgFindColor(mg, 0xFF, 0xC8, 0xC8);
00043 
00044 /* Draw text. */
00045 for (i=labelCount-1; i >= 0; --i)
00046     {
00047     Color c = ((i&1) ? c2 : c1);
00048     mgDrawBox(mg, 0, y, width, lineHeight, c);
00049     mgTextRight(mg, 0+1, y+1, width-1, lineHeight, MG_BLACK, font, labels[i]);
00050     y += lineHeight;
00051     }
00052 
00053 return mg;
00054 }
00055 
00056 
00057 boolean sameFileContents(char *n1, char *n2)
00058 /* compare two files and return true if their contents are identical using binary compare */
00059 {
00060 int r1 = 0, r2 = 0;
00061 char buf1[4096];
00062 char buf2[4096];
00063 FILE *f1 = NULL, *f2 = NULL;
00064 boolean result = TRUE;
00065 f1 = fopen(n1,"r"); if (f1 == NULL) { return FALSE; }
00066 f2 = fopen(n2,"r"); if (f2 == NULL) { fclose(f1); return FALSE; }
00067 while (TRUE)
00068     {
00069     r1 = fread(buf1, 1, sizeof(buf1), f1);
00070     r2 = fread(buf2, 1, sizeof(buf2), f2);
00071     if (r1 != r2) { result = FALSE; break; } /* diff file size */
00072     if (r1 == 0) { break; }  /* eof */
00073     if (memcmp(buf1,buf2,r1)!=0) { result = FALSE; break; } /* file contents differ */
00074     }
00075 fclose(f2);
00076 fclose(f1);
00077 return result;
00078 }
00079 
00080 
00081 void gifLabelVerticalText(char *fileName, char **labels, int labelCount, 
00082         int height)
00083 /* Make a gif file with given labels.  This will check to see if fileName
00084  * exists already, and if so do nothing. */
00085 {
00086 struct memGfx *straight = altColorLabels(labels, labelCount, height);
00087 struct memGfx *rotated = mgRotate90(straight);
00088 char tempName[512];
00089 safef(tempName, sizeof(tempName), "%s_trash", fileName);
00090 mgSaveGif(rotated, tempName);  /* note we cannot delete this later because of permissions */
00091 /* the savings here is in the user's own browser cache - not updated if no change */
00092 if (!sameFileContents(tempName,fileName))  
00093     mgSaveGif(rotated, fileName);
00094 mgFree(&straight);
00095 mgFree(&rotated);
00096 }
00097 
00098 
00099 #ifdef DEBUG
00100 void gifTest()
00101 {
00102 static char *labels[] = {"cerebellum", "thymus", "breast", "heart",
00103                          "stomach", "cartilage", "kidney", "liver",
00104                          "lung", "testis", "black hole" };
00105 int size = gifLabelMaxWidth(labels, ArraySize(labels));
00106 int gifLabelMaxWidth(char **labels, int labelCount)
00107 gifLabelVerticalText("../trash/foo.gif", labels, ArraySize(labels), size);
00108 uglyf("<IMG SRC=\"../trash/foo.gif\">");
00109 }
00110 #endif /* DEBUG */

Generated on Tue Dec 25 18:39:31 2007 for blat by  doxygen 1.5.2