Commits

Anonymous committed 6bbec88

introduced gdBBox.

  • Participants
  • Parent commits 0365ea0

Comments (0)

Files changed (3)

  * Pango enabled replacement for gdImageStringFT.
  *
  * @param *im  gdImagePtr
- * @param brect int array of layout bounds
+ * @param *bbox gdBBox layout the resulting bounds
  * @param fg foreground color
  * @param *fontlist path to font file
  * @param ptsize font size
  * @param *string the text to draw
  * @return A null char* on success, or an error string on failure
  */
-char *gdImageStringPangoFT(gdImagePtr im, int *brect, int fg, char *fontlist,
+char *gdImageStringPangoFT(gdImagePtr im, gdBBox *bbox, int fg, char *fontlist,
 		double ptsize, double angle, int x, int y, char *string)
 {
 	int r;
 		context->matrix = &affined_matrix;
 	}
 
-	if (brect) {
+	if (bbox) {
 		int w = gdPangoGetLayoutWidth(context);
 		int h = gdPangoGetLayoutHeight(context);
 		if (angle == 0.) {
-			brect[0] = x;
-			brect[1] = y + h;
-			brect[2] = x + w;
-			brect[3] = y + h;
-			brect[4] = x + w;
-			brect[5] = y;
+			bbox->bottom_left.x  = x;
+			bbox->bottom_left.y  = y + h;
+			bbox->bottom_right.x = x + w;
+			bbox->bottom_right.y = y + h;
+			bbox->top_right.x    = x + w;
+			bbox->top_right.y    = y;
 		} else { /* rotated */
 			double sin_a = sin(angle);
 			double cos_a = cos(angle);
 			int h_cos_a = (int)ceil(h * cos_a);
 			int w_sin_a = (int)ceil(w * sin_a);
 			int w_cos_a = (int)ceil(w * cos_a);
-			brect[0] = x + h_sin_a;
-			brect[1] = y + h_cos_a;
-			brect[2] = x + h_sin_a + w_cos_a;
-			brect[3] = y + h_cos_a - w_sin_a;
-			brect[4] = x + w_cos_a;
-			brect[5] = y - w_sin_a;
+			bbox->bottom_left.x  = x + h_sin_a;
+			bbox->bottom_left.y  = y + h_cos_a;
+			bbox->bottom_right.x = x + h_sin_a + w_cos_a;
+			bbox->bottom_right.y = y + h_cos_a - w_sin_a;
+			bbox->top_right.x    = x + w_cos_a;
+			bbox->top_right.y    = y - w_sin_a;
 		}
-		brect[6] = x;
-		brect[7] = y;
+		bbox->top_left.x = x;
+		bbox->top_left.y = y;
 	}
 
 	if (im) gdPangoRenderTo(context, im, x, y);
 };
 
 /**
+ * Defines a bounding box. Note that the box includes all of the corners.
+ */
+typedef struct { /* bounding box */
+	gdPoint bottom_left;
+	gdPoint bottom_right;
+	gdPoint top_right;
+	gdPoint top_left;
+} gdBBox;
+
+/**
  * Defines a colors set. The foreground and background can be
  * defined using a integer value returned by gdTrueColor(r,g,b).
  * For example, you can use when you create a gdPangoContext.
 
 extern char *gdImageStringPangoFT(
 	gdImagePtr im,
-	int *brect,
+	gdBBox *bbox,
 	int fg,
 	char *fontlist,
 	double ptsize,

tests/pango/units.c

 	gdPangoFreeContext(context);
 }
 
+static int gdBBoxEqual(gdBBox *bbox1, gdBBox *bbox2)
+{
+	return (bbox1->bottom_left.x == bbox2->bottom_left.x &&
+			bbox1->bottom_left.y == bbox2->bottom_left.y &&
+			bbox1->bottom_right.x == bbox2->bottom_right.x &&
+			bbox1->bottom_right.y == bbox2->bottom_right.y &&
+			bbox1->top_right.x == bbox2->top_right.x &&
+			bbox1->top_right.y == bbox2->top_right.y &&
+			bbox1->top_left.x == bbox2->top_left.x &&
+			bbox1->top_left.y == bbox2->top_left.y);
+}
+
 TEST(gdImageStringPangoFT)
 {
 	gdPangoContext *context;
 	context = gdPangoCreateContext();
 	{
-		int brect1[8], brect2[8];
+		gdBBox bbox1, bbox2;
 		int fg = gdTrueColorAlpha(0xFF, 0xFF, 0xFF, gdAlphaOpaque);
 		int k;
 		for (k=0; ttf_paths[k]; k++) {
 			char *r1, *r2;
-			int i;
-			r1 = gdImageStringPangoFT(NULL, brect1, fg, ttf_paths[k], 12., 0., 0, 0, "abc");
-			r2 = gdImageStringPangoFT(NULL, brect2, fg, ttf_paths[k], 12., 0., 0, 0, "<span foreground='green'>a</span>bc");
+			r1 = gdImageStringPangoFT(NULL, &bbox1, fg, ttf_paths[k], 12., 0., 0, 0, "abc");
+			r2 = gdImageStringPangoFT(NULL, &bbox2, fg, ttf_paths[k], 12., 0., 0, 0, "<span foreground='green'>a</span>bc");
 			gdTestAssert(r1 == r2);
 			if (r1) continue;
-			for (i=0; i<8; i++) gdTestAssert(brect1[i] == brect2[i]);
-			r1 = gdImageStringPangoFT(NULL, brect1, fg, ttf_paths[k], 40., -G_PI/6, 0, 0, "abc");
-			r2 = gdImageStringPangoFT(NULL, brect2, fg, ttf_paths[k], 40., -G_PI/6, 0, 0, "<span foreground='green'>a</span>bc");
+			gdTestAssert(gdBBoxEqual(&bbox1, &bbox2));
+			r1 = gdImageStringPangoFT(NULL, &bbox1, fg, ttf_paths[k], 40., -G_PI/6, 0, 0, "abc");
+			r2 = gdImageStringPangoFT(NULL, &bbox2, fg, ttf_paths[k], 40., -G_PI/6, 0, 0, "<span foreground='green'>a</span>bc");
 			gdTestAssert(r1 == r2);
 			if (r1) continue;
-			for (i=0; i<8; i++) gdTestAssert(brect1[i] == brect2[i]);
+			gdTestAssert(gdBBoxEqual(&bbox1, &bbox2));
 		}
 	}
 	/* TODO */