[cairo-commit] 3 commits - src/cairo-truetype-subset.c src/cairo-truetype-subset-private.h src/Makefile.am test/Makefile.am test/truetype-tables.c

Behdad Esfahbod behdad at kemper.freedesktop.org
Mon Aug 7 18:38:25 PDT 2006


 src/Makefile.am                     |    1 
 src/cairo-truetype-subset-private.h |  159 ++++++++++++++++++++++++++++++++++++
 src/cairo-truetype-subset.c         |  116 +-------------------------
 test/Makefile.am                    |    1 
 test/truetype-tables.c              |   59 +++++++++++++
 5 files changed, 225 insertions(+), 111 deletions(-)

New commits:
diff-tree c2ea56e5e075f9e75350b3c54956a70e6fb9a58c (from 50be7951c981468a3a2acf2cd77931a46ca1e824)
Author: Behdad Esfahbod <behdad at behdad.org>
Date:   Mon Aug 7 21:37:16 2006 -0400

    Add test truetype-tables that checks sizeof truetype tables

diff --git a/test/Makefile.am b/test/Makefile.am
index 99c6d55..3334556 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -76,6 +76,7 @@ text-rotate			\
 transforms			\
 translate-show-surface		\
 trap-clip			\
+truetype-tables			\
 unantialiased-shapes		\
 unbounded-operator		\
 user-data			\
diff --git a/test/truetype-tables.c b/test/truetype-tables.c
new file mode 100644
index 0000000..32cc5c2
--- /dev/null
+++ b/test/truetype-tables.c
@@ -0,0 +1,59 @@
+/*
+ * Copyright © 2006 Red Hat, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software
+ * and its documentation for any purpose is hereby granted without
+ * fee, provided that the above copyright notice appear in all copies
+ * and that both that copyright notice and this permission notice
+ * appear in supporting documentation, and that the name of
+ * Red Hat, Inc. not be used in advertising or publicity pertaining to
+ * distribution of the software without specific, written prior
+ * permission. Red Hat, Inc. makes no representations about the
+ * suitability of this software for any purpose.  It is provided "as
+ * is" without express or implied warranty.
+ *
+ * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
+ * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
+ * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
+ * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * Author: Behdad Esfahbod <behdad at behdad.org>
+ */
+/*
+ * Test that the structs we define for TrueType tables have the
+ * correct size, ie. they are not padded.
+ */
+
+#include "cairo-test.h"
+
+#include <cairo-truetype-subset-private.h>
+
+int
+main (void)
+{
+    cairo_test_status_t ret = CAIRO_TEST_SUCCESS;
+
+    cairo_test_init ("truetype-tables");
+
+    cairo_test_log ("Test that the size of TrueType table structs is correct\n");
+
+#define check(st, sz) \
+	if (sizeof (st) != (sz)) { \
+	    cairo_test_log ("sizeof (%s): got %d, expected %d", #st, sizeof (st), sz); \
+	    ret = CAIRO_TEST_FAILURE; \
+	}
+
+    check (tt_head_t,	54);
+    check (tt_hhea_t,	36);
+    check (tt_maxp_t,	32);
+    check (tt_name_record_t,	12);
+    check (tt_name_t,	18);
+    check (tt_name_t,	18);
+    check (tt_composite_glyph_t,	18);
+    check (tt_glyph_data_t,	28);
+
+    return ret;
+}
diff-tree 50be7951c981468a3a2acf2cd77931a46ca1e824 (from ec895202e008356492ae0c9e40c9f40c6bf8cbb3)
Author: Behdad Esfahbod <behdad at behdad.org>
Date:   Mon Aug 7 21:23:08 2006 -0400

    Split the TrueType table definitions into a private header file
    such that we can test the struct sizes in a test.

diff --git a/src/Makefile.am b/src/Makefile.am
index d94fe53..b993fb5 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,5 +1,6 @@
 font_subset_sources =				\
 	cairo-truetype-subset.c			\
+	cairo-truetype-subset-private.h		\
 	cairo-scaled-font-subsets.c		\
 	cairo-scaled-font-subsets-private.h
 if CAIRO_HAS_FT_FONT
diff --git a/src/cairo-truetype-subset-private.h b/src/cairo-truetype-subset-private.h
new file mode 100644
index 0000000..35fe95f
--- /dev/null
+++ b/src/cairo-truetype-subset-private.h
@@ -0,0 +1,159 @@
+/* cairo - a vector graphics library with display and print output
+ *
+ * Copyright © 2006 Red Hat, Inc
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it either under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation
+ * (the "LGPL") or, at your option, under the terms of the Mozilla
+ * Public License Version 1.1 (the "MPL"). If you do not alter this
+ * notice, a recipient may use your version of this file under either
+ * the MPL or the LGPL.
+ *
+ * You should have received a copy of the LGPL along with this library
+ * in the file COPYING-LGPL-2.1; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * You should have received a copy of the MPL along with this library
+ * in the file COPYING-MPL-1.1
+ *
+ * The contents of this file are subject to the Mozilla Public License
+ * Version 1.1 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
+ * OF ANY KIND, either express or implied. See the LGPL or the MPL for
+ * the specific language governing rights and limitations.
+ *
+ * The Original Code is the cairo graphics library.
+ *
+ * The Initial Developer of the Original Code is Red Hat, Inc.
+ *
+ * Contributor(s):
+ *	Kristian Høgsberg <krh at redhat.com>
+ *	Adrian Johnson <ajohnson at redneon.com>
+ */
+
+#ifndef CAIRO_TRUETYPE_SUBSET_PRIVATE_H
+#define CAIRO_TRUETYPE_SUBSET_PRIVATE_H
+
+#include "cairoint.h"
+
+#define MAKE_TT_TAG(a, b, c, d)    (a<<24 | b<<16 | c<<8 | d)
+#define TT_TAG_cmap   MAKE_TT_TAG('c','m','a','p')
+#define TT_TAG_cvt    MAKE_TT_TAG('c','v','t',' ')
+#define TT_TAG_fpgm   MAKE_TT_TAG('f','p','g','m')
+#define TT_TAG_glyf   MAKE_TT_TAG('g','l','y','f')
+#define TT_TAG_head   MAKE_TT_TAG('h','e','a','d')
+#define TT_TAG_hhea   MAKE_TT_TAG('h','h','e','a')
+#define TT_TAG_hmtx   MAKE_TT_TAG('h','m','t','x')
+#define TT_TAG_loca   MAKE_TT_TAG('l','o','c','a')
+#define TT_TAG_maxp   MAKE_TT_TAG('m','a','x','p')
+#define TT_TAG_name   MAKE_TT_TAG('n','a','m','e')
+#define TT_TAG_prep   MAKE_TT_TAG('p','r','e','p')
+
+/* All tt_* structs are big-endian */
+typedef struct _tt_head {
+    int16_t     version_1;              /* FIXED */
+    int16_t     version_2;              /* FIXED */
+    int16_t     revision_1;             /* FIXED */
+    int16_t     revision_2;             /* FIXED */
+    uint16_t    checksum_1;
+    uint16_t    checksum_2;
+    uint16_t    magic_1;
+    uint16_t    magic_2;
+    uint16_t    flags;
+    uint16_t    units_per_em;
+    int16_t     created_1;
+    int16_t     created_2;
+    int16_t     created_3;
+    int16_t     created_4;
+    int16_t     modified_1;
+    int16_t     modified_2;
+    int16_t     modified_3;
+    int16_t     modified_4;
+    int16_t     x_min;                  /* FWORD */
+    int16_t     y_min;                  /* FWORD */
+    int16_t     x_max;                  /* FWORD */
+    int16_t     y_max;                  /* FWORD */
+    uint16_t    mac_style;
+    uint16_t    lowest_rec_pppem;
+    int16_t     font_direction_hint;
+    int16_t     index_to_loc_format;
+    int16_t     glyph_data_format;
+} tt_head_t;
+
+typedef struct _tt_hhea {
+    int16_t     version_1;              /* FIXED */
+    int16_t     version_2;              /* FIXED */
+    int16_t     ascender;               /* FWORD */
+    int16_t     descender;              /* FWORD */
+    int16_t     line_gap;               /* FWORD */
+    uint16_t    advance_max_width;      /* UFWORD */
+    int16_t     min_left_side_bearing;  /* FWORD */
+    int16_t     min_right_side_bearing; /* FWORD */
+    int16_t     x_max_extent;           /* FWORD */
+    int16_t     caret_slope_rise;
+    int16_t     caret_slope_run;
+    int16_t     reserved[5];
+    int16_t     metric_data_format;
+    uint16_t    num_hmetrics;
+} tt_hhea_t;
+
+typedef struct _tt_maxp {
+    int16_t     version_1;              /* FIXED */
+    int16_t     version_2;              /* FIXED */
+    uint16_t    num_glyphs;
+    uint16_t    max_points;
+    uint16_t    max_contours;
+    uint16_t    max_composite_points;
+    uint16_t    max_composite_contours;
+    uint16_t    max_zones;
+    uint16_t    max_twilight_points;
+    uint16_t    max_storage;
+    uint16_t    max_function_defs;
+    uint16_t    max_instruction_defs;
+    uint16_t    max_stack_elements;
+    uint16_t    max_size_of_instructions;
+    uint16_t    max_component_elements;
+    uint16_t    max_component_depth;
+} tt_maxp_t;
+
+typedef struct _tt_name_record {
+    uint16_t platform;
+    uint16_t encoding;
+    uint16_t language;
+    uint16_t name;
+    uint16_t length;
+    uint16_t offset;
+} tt_name_record_t;
+
+typedef struct _tt_name {
+    uint16_t   format;
+    uint16_t   num_records;
+    uint16_t   strings_offset;
+    tt_name_record_t records[1];
+} tt_name_t;
+
+
+
+/* composite_glyph_t flags */
+#define TT_ARG_1_AND_2_ARE_WORDS     0x0001
+#define TT_WE_HAVE_A_SCALE           0x0008
+#define TT_MORE_COMPONENTS           0x0020
+#define TT_WE_HAVE_AN_X_AND_Y_SCALE  0x0040
+#define TT_WE_HAVE_A_TWO_BY_TWO      0x0080
+
+typedef struct _tt_composite_glyph {
+    uint16_t flags;
+    uint16_t index;
+    uint16_t args[7]; /* 1 to 7 arguments depending on value of flags */
+} tt_composite_glyph_t;
+
+typedef struct _tt_glyph_data {
+    int16_t           num_contours;
+    int8_t            data[8];
+    tt_composite_glyph_t glyph;
+} tt_glyph_data_t;
+
+#endif /* CAIRO_TRUETYPE_SUBSET_PRIVATE_H */
diff --git a/src/cairo-truetype-subset.c b/src/cairo-truetype-subset.c
index 34be974..6753b23 100644
--- a/src/cairo-truetype-subset.c
+++ b/src/cairo-truetype-subset.c
@@ -37,6 +37,7 @@
 #include <string.h>
 #include "cairoint.h"
 #include "cairo-scaled-font-subsets-private.h"
+#include "cairo-truetype-subset-private.h"
 
 
 typedef struct subset_glyph subset_glyph_t;
@@ -70,104 +71,6 @@ typedef struct _cairo_truetype_font {
 
 } cairo_truetype_font_t;
 
-
-
-#define MAKE_TT_TAG(a, b, c, d)    (a<<24 | b<<16 | c<<8 | d)
-#define TT_TAG_cmap   MAKE_TT_TAG('c','m','a','p')
-#define TT_TAG_cvt    MAKE_TT_TAG('c','v','t',' ')
-#define TT_TAG_fpgm   MAKE_TT_TAG('f','p','g','m')
-#define TT_TAG_glyf   MAKE_TT_TAG('g','l','y','f')
-#define TT_TAG_head   MAKE_TT_TAG('h','e','a','d')
-#define TT_TAG_hhea   MAKE_TT_TAG('h','h','e','a')
-#define TT_TAG_hmtx   MAKE_TT_TAG('h','m','t','x')
-#define TT_TAG_loca   MAKE_TT_TAG('l','o','c','a')
-#define TT_TAG_maxp   MAKE_TT_TAG('m','a','x','p')
-#define TT_TAG_name   MAKE_TT_TAG('n','a','m','e')
-#define TT_TAG_prep   MAKE_TT_TAG('p','r','e','p')
-
-/* All tt_* structs are big-endian */
-typedef struct _tt_head {
-    int16_t     version_1;              /* FIXED */
-    int16_t     version_2;              /* FIXED */
-    int16_t     revision_1;             /* FIXED */
-    int16_t     revision_2;             /* FIXED */
-    uint16_t    checksum_1;
-    uint16_t    checksum_2;
-    uint16_t    magic_1;
-    uint16_t    magic_2;
-    uint16_t    flags;
-    uint16_t    units_per_em;
-    int16_t     created_1;
-    int16_t     created_2;
-    int16_t     created_3;
-    int16_t     created_4;
-    int16_t     modified_1;
-    int16_t     modified_2;
-    int16_t     modified_3;
-    int16_t     modified_4;
-    int16_t     x_min;                  /* FWORD */
-    int16_t     y_min;                  /* FWORD */
-    int16_t     x_max;                  /* FWORD */
-    int16_t     y_max;                  /* FWORD */
-    uint16_t    mac_style;
-    uint16_t    lowest_rec_pppem;
-    int16_t     font_direction_hint;
-    int16_t     index_to_loc_format;
-    int16_t     glyph_data_format;
-} tt_head_t;
-
-typedef struct _tt_hhea {
-    int16_t     version_1;              /* FIXED */
-    int16_t     version_2;              /* FIXED */
-    int16_t     ascender;               /* FWORD */
-    int16_t     descender;              /* FWORD */
-    int16_t     line_gap;               /* FWORD */
-    uint16_t    advance_max_width;      /* UFWORD */
-    int16_t     min_left_side_bearing;  /* FWORD */
-    int16_t     min_right_side_bearing; /* FWORD */
-    int16_t     x_max_extent;           /* FWORD */
-    int16_t     caret_slope_rise;
-    int16_t     caret_slope_run;
-    int16_t     reserved[5];
-    int16_t     metric_data_format;
-    uint16_t    num_hmetrics;
-} tt_hhea_t;
-
-typedef struct _tt_maxp {
-    int16_t     version_1;              /* FIXED */
-    int16_t     version_2;              /* FIXED */
-    uint16_t    num_glyphs;
-    uint16_t    max_points;
-    uint16_t    max_contours;
-    uint16_t    max_composite_points;
-    uint16_t    max_composite_contours;
-    uint16_t    max_zones;
-    uint16_t    max_twilight_points;
-    uint16_t    max_storage;
-    uint16_t    max_function_defs;
-    uint16_t    max_instruction_defs;
-    uint16_t    max_stack_elements;
-    uint16_t    max_size_of_instructions;
-    uint16_t    max_component_elements;
-    uint16_t    max_component_depth;
-} tt_maxp_t;
-
-typedef struct _tt_name_record {
-    uint16_t platform;
-    uint16_t encoding;
-    uint16_t language;
-    uint16_t name;
-    uint16_t length;
-    uint16_t offset;
-} tt_name_record_t;
-
-typedef struct _tt_name {
-    uint16_t   format;
-    uint16_t   num_records;
-    uint16_t   strings_offset;
-    tt_name_record_t records[1];
-} tt_name_t;
-
 static int
 cairo_truetype_font_use_glyph (cairo_truetype_font_t *font, int glyph);
 
@@ -497,37 +400,18 @@ cairo_truetype_font_write_generic_table 
     return 0;
 }
 
-typedef struct _composite_glyph {
-    uint16_t flags;
-    uint16_t index;
-    uint16_t args[7]; /* 1 to 7 arguments depending on value of flags */
-} composite_glyph_t;
-
-typedef struct _glyph_data {
-    int16_t           num_contours;
-    int8_t            data[8];
-    composite_glyph_t glyph;
-} glyph_data_t;
-
-/* composite_glyph_t flags */
-#define TT_ARG_1_AND_2_ARE_WORDS     0x0001
-#define TT_WE_HAVE_A_SCALE           0x0008
-#define TT_MORE_COMPONENTS           0x0020
-#define TT_WE_HAVE_AN_X_AND_Y_SCALE  0x0040
-#define TT_WE_HAVE_A_TWO_BY_TWO      0x0080
-
 static void
 cairo_truetype_font_remap_composite_glyph (cairo_truetype_font_t *font,
 					   unsigned char         *buffer)
 {
-    glyph_data_t *glyph_data;
-    composite_glyph_t *composite_glyph;
+    tt_glyph_data_t *glyph_data;
+    tt_composite_glyph_t *composite_glyph;
     int num_args;
     int has_more_components;
     unsigned short flags;
     unsigned short index;
 
-    glyph_data = (glyph_data_t *) buffer;
+    glyph_data = (tt_glyph_data_t *) buffer;
     if ((int16_t)be16_to_cpu (glyph_data->num_contours) >= 0)
         return;
 
@@ -546,7 +430,7 @@ cairo_truetype_font_remap_composite_glyp
             num_args += 2;
         else if (flags & TT_WE_HAVE_A_TWO_BY_TWO)
             num_args += 3;
-        composite_glyph = (composite_glyph_t *) &(composite_glyph->args[num_args]);
+        composite_glyph = (tt_composite_glyph_t *) &(composite_glyph->args[num_args]);
     } while (has_more_components);
 }
 
diff-tree ec895202e008356492ae0c9e40c9f40c6bf8cbb3 (from 20c3ff96de05365ee65301ccd3c43e7bcb19f49b)
Author: Behdad Esfahbod <behdad at behdad.org>
Date:   Mon Aug 7 21:13:48 2006 -0400

    Remove the __attribute__ ((packed)) and the need for it
    as it's not portable and lack of it could seriously break the truetype
    subsetting code.  Now, instead of using int32_t and int64_t, we use
    multiple int16_t items, to avoid any alignments.  Fortunately, we are
    not using any of the fields involved, so no code changes necessary.

diff --git a/src/cairo-truetype-subset.c b/src/cairo-truetype-subset.c
index 5a3b2e1..34be974 100644
--- a/src/cairo-truetype-subset.c
+++ b/src/cairo-truetype-subset.c
@@ -86,15 +86,25 @@ typedef struct _cairo_truetype_font {
 #define TT_TAG_prep   MAKE_TT_TAG('p','r','e','p')
 
 /* All tt_* structs are big-endian */
-typedef struct tt_head {
-    int32_t     version;                /* FIXED */
-    int32_t     revision;               /* FIXED */
-    uint32_t    checksum;
-    uint32_t    magic;
+typedef struct _tt_head {
+    int16_t     version_1;              /* FIXED */
+    int16_t     version_2;              /* FIXED */
+    int16_t     revision_1;             /* FIXED */
+    int16_t     revision_2;             /* FIXED */
+    uint16_t    checksum_1;
+    uint16_t    checksum_2;
+    uint16_t    magic_1;
+    uint16_t    magic_2;
     uint16_t    flags;
     uint16_t    units_per_em;
-    int64_t     created;
-    int64_t     modified;
+    int16_t     created_1;
+    int16_t     created_2;
+    int16_t     created_3;
+    int16_t     created_4;
+    int16_t     modified_1;
+    int16_t     modified_2;
+    int16_t     modified_3;
+    int16_t     modified_4;
     int16_t     x_min;                  /* FWORD */
     int16_t     y_min;                  /* FWORD */
     int16_t     x_max;                  /* FWORD */
@@ -104,10 +114,11 @@ typedef struct tt_head {
     int16_t     font_direction_hint;
     int16_t     index_to_loc_format;
     int16_t     glyph_data_format;
-} __attribute__ ((packed)) tt_head_t;
+} tt_head_t;
 
-typedef struct tt_hhea {
-    int32_t     version;                /* FIXED */
+typedef struct _tt_hhea {
+    int16_t     version_1;              /* FIXED */
+    int16_t     version_2;              /* FIXED */
     int16_t     ascender;               /* FWORD */
     int16_t     descender;              /* FWORD */
     int16_t     line_gap;               /* FWORD */
@@ -120,10 +131,11 @@ typedef struct tt_hhea {
     int16_t     reserved[5];
     int16_t     metric_data_format;
     uint16_t    num_hmetrics;
-} __attribute__ ((packed)) tt_hhea_t;
+} tt_hhea_t;
 
-typedef struct tt_maxp {
-    int32_t     version;      /* FIXED */
+typedef struct _tt_maxp {
+    int16_t     version_1;              /* FIXED */
+    int16_t     version_2;              /* FIXED */
     uint16_t    num_glyphs;
     uint16_t    max_points;
     uint16_t    max_contours;
@@ -138,23 +150,23 @@ typedef struct tt_maxp {
     uint16_t    max_size_of_instructions;
     uint16_t    max_component_elements;
     uint16_t    max_component_depth;
-} __attribute__ ((packed)) tt_maxp_t;
+} tt_maxp_t;
 
-typedef struct tt_name_record {
+typedef struct _tt_name_record {
     uint16_t platform;
     uint16_t encoding;
     uint16_t language;
     uint16_t name;
     uint16_t length;
     uint16_t offset;
-} __attribute__ ((packed)) tt_name_record_t;
+} tt_name_record_t;
 
-typedef struct tt_name {
+typedef struct _tt_name {
     uint16_t   format;
     uint16_t   num_records;
     uint16_t   strings_offset;
     tt_name_record_t records[1];
-} __attribute__ ((packed)) tt_name_t;
+} tt_name_t;
 
 static int
 cairo_truetype_font_use_glyph (cairo_truetype_font_t *font, int glyph);
@@ -485,19 +497,17 @@ cairo_truetype_font_write_generic_table 
     return 0;
 }
 
-typedef struct composite_glyph composite_glyph_t;
-struct composite_glyph {
+typedef struct _composite_glyph {
     uint16_t flags;
     uint16_t index;
     uint16_t args[7]; /* 1 to 7 arguments depending on value of flags */
-} __attribute__ ((packed));
+} composite_glyph_t;
 
-typedef struct glyph_data glyph_data_t;
-struct glyph_data {
+typedef struct _glyph_data {
     int16_t           num_contours;
     int8_t            data[8];
     composite_glyph_t glyph;
-} __attribute__ ((packed));
+} glyph_data_t;
 
 /* composite_glyph_t flags */
 #define TT_ARG_1_AND_2_ARE_WORDS     0x0001


More information about the cairo-commit mailing list