Commits

tabe  committed 3ef5aa2

- export gdPangoSetPangoFontDescriptionFromFile()
- elaborate messages on error
- some cosmetic fixes

  • Participants
  • Parent commits 6c65d82

Comments (0)

Files changed (4)

File examples/rotated.c

 
 	fseek(file, 0, SEEK_END);
 	file_size = ftell(file);
+	if (file_size == -1) {
+		printf("Cannot tell file <%s>\n", filename);
+		exit(2);
+	}
 	fseek(file, 0, SEEK_SET);
 	text = (char *)malloc(file_size + 1);
+	if (!text) {
+		printf("Cannot allocate\n");
+		exit(3);
+	}
 	fread(text, file_size, 1, file);
 	text[file_size] = '\0';
 

File examples/simple.c

 
 	fseek(file, 0, SEEK_END);
 	file_size = ftell(file);
+	if (file_size == -1) {
+		printf("Cannot tell file <%s>\n", filename);
+		exit(2);
+	}
 	fseek(file, 0, SEEK_SET);
 	text = (char *)malloc(file_size + 1);
+	if (!text) {
+		printf("Cannot allocate\n");
+		exit(3);
+	}
 	fread(text, file_size, 1, file);
 	text[file_size] = '\0';
 
  *
  * @return always GD_SUCCESS.
 */
-int gdPangoInit() {
+int gdPangoInit(void) {
 	g_type_init();
 	GD_PANGO_IS_INITIALIZED = 1;
 	return GD_SUCCESS;
  *
  * @return positive if it was initialized, otherwise zero.
  */
-int gdPangoIsInitialized()
+int gdPangoIsInitialized(void)
 {
 	return GD_PANGO_IS_INITIALIZED;
 }
  *
  * @return A pointer to the context as a gdPangoContext*.
  */
-gdPangoContext* gdPangoCreateContext()
+gdPangoContext* gdPangoCreateContext(void)
 {
 	gdPangoContext *context = g_malloc(sizeof(gdPangoContext));
 	G_CONST_RETURN char *charset;
  * Define the default foreground, background and alpha component.
  *
  * @param *context	gdPangoContext context
- * @param *colors		a gdPangoColors ptr, defines fg, bgd or alpha default
+ * @param *colors		a gdPangoColors ptr, defines fg, bg or alpha default
  */
 void gdPangoSetDefaultColor(gdPangoContext *context,
 	const gdPangoColors  *colors)
 }
 
 /**
- * Set font description from a ttf file
+ * Set font description from a ttf file.
  *
  * @param *context Context
  * @param *fontlist path to ttf file
+ * @return A null char* on success, or an error string on failure
  */
-void gdPangoSetPangoFontDescriptionFromFile(gdPangoContext *context, char
+char *gdPangoSetPangoFontDescriptionFromFile(gdPangoContext *context, const char
 		*fontlist, double ptsize)
 {
 	FcPattern *fcPattern;
 	FcBlanks *fcBlanks;
 	FcValue fcFamilyName;
+	FcResult fcResult;
 	int numFonts;
 	char *font_desc;
+	char *r = (char *)NULL;
 
 	fcBlanks = FcBlanksCreate();
 	fcPattern = FcFreeTypeQuery(fontlist, 0, fcBlanks, &numFonts);
-	FcPatternGet(fcPattern, FC_FAMILY, 0, &fcFamilyName);
+	if (!fcPattern) {
+		r = "font not found";
+		goto fail0;
+	}
+	fcResult = FcPatternGet(fcPattern, FC_FAMILY, 0, &fcFamilyName);
+	if (fcResult != FcResultMatch) {
+		r = "could not get font family";
+		goto fail1;
+	}
 
 	font_desc = g_strdup_printf("%s %d", fcFamilyName.u.s, (int) ptsize);
 	context->font_desc = pango_font_description_from_string(font_desc);
 	g_free(font_desc);
 	gdPangoSetDpi(context, ptsize, ptsize);
+ fail1:
+	FcPatternDestroy(fcPattern);
+ fail0:
+	FcBlanksDestroy(fcBlanks);
+	return r;
 }
 
 PangoFontMap* gdPangoGetPangoFontMap(gdPangoContext *context)
 		double ptsize, double angle, int x, int y, char *string)
 {
 	int w, h;
+	char *r;
 	gdPangoContext *context;
 	gdPangoColors default_colors;
 	PangoContext *pangocontext;
 	angle *= 180 / G_PI;
 
 	context = gdPangoCreateContext();
-	gdPangoSetPangoFontDescriptionFromFile(context, fontlist, ptsize);
+	r = gdPangoSetPangoFontDescriptionFromFile(context, fontlist, ptsize);
+	if (r) {
+		gdPangoFreeContext(context);
+		return r;
+	}
 	gdPangoSetDefaultColor(context, &default_colors);
 	gdPangoSetMarkup(context, string, -1);
 	pangocontext = gdPangoGetPangoContext(context);
 	double angle;
 } gdPangoContext;
 
-extern int gdPangoInit();
-extern int gdPangoIsInitialized();
-extern gdPangoContext* gdPangoCreateContext();
+extern int gdPangoInit(void);
+extern int gdPangoIsInitialized(void);
+extern gdPangoContext* gdPangoCreateContext(void);
 extern void gdPangoFreeContext(gdPangoContext *context);
 
 extern gdImagePtr gdPangoCreateSurfaceDraw(
 extern  void gdPangoSetBaseDirection(
 	gdPangoContext *context, PangoDirection pango_dir);
 
+extern char *gdPangoSetPangoFontDescriptionFromFile(
+	gdPangoContext *context, const char *fontlist, double ptsize);
 
 #ifdef __FT2_BUILD_UNIX_H__