Back to index

texmacs  1.0.7.15
tt_post.c
Go to the documentation of this file.
00001 /*  $Header: /home/cvsroot/dvipdfmx/src/tt_post.c,v 1.3 2009/08/17 00:49:45 chofchof Exp $
00002     
00003     This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.
00004 
00005     Copyright (C) 2002 by Jin-Hwan Cho and Shunsaku Hirata,
00006     the dvipdfmx project team <dvipdfmx@project.ktug.or.kr>
00007     
00008     This program is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012     
00013     This program is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU General Public License for more details.
00017     
00018     You should have received a copy of the GNU General Public License
00019     along with this program; if not, write to the Free Software
00020     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00021 */
00022 
00023 #include "system.h"
00024 #include "error.h"
00025 #include "mem.h"
00026 
00027 #include "dpxfile.h"
00028 
00029 #include "sfnt.h"
00030 
00031 #include "tt_post.h"
00032 
00033 static const char *const macglyphorder[258];
00034 
00035 /* offset from begenning of the post table */
00036 #define NAME_STR_OFFSET  32
00037 
00038 static int
00039 read_v2_post_names (struct tt_post_table *post, sfnt *sfont)
00040 {
00041   USHORT i, idx, *indices;
00042   int    len;
00043 
00044   post->numberOfGlyphs = sfnt_get_ushort(sfont);
00045 
00046   indices     = NEW(post->numberOfGlyphs, USHORT);
00047   post->count = 0;
00048   for (i = 0;
00049        i < post->numberOfGlyphs; i++) {
00050     idx = sfnt_get_ushort(sfont);
00051     if (idx >= 258) {
00052       if (idx > 32767) {
00053        WARN("TrueTypes post table name index %u > 32767", idx);
00054        idx = 0;
00055       }
00056       post->count++;
00057     }
00058     indices[i] = idx;
00059   }
00060 
00061   if (post->count < 1) {
00062     post->names = NULL;
00063   } else {
00064     post->names = NEW(post->count, char *);
00065     for (i = 0; i < post->count; i++) { /* read Pascal strings */
00066       len = sfnt_get_byte(sfont);
00067       if (len > 0) {
00068        post->names[i] = NEW(len + 1, char);
00069        sfnt_read(post->names[i], len, sfont);
00070        post->names[i][len] = 0;
00071       } else {
00072        post->names[i] = NULL;
00073       }
00074     }
00075   }
00076 
00077   post->glyphNamePtr = NEW(post->numberOfGlyphs, char *);
00078   for (i = 0; i < post->numberOfGlyphs; i++) {
00079     idx = indices[i];
00080     if (idx < 258) {
00081       post->glyphNamePtr[i] = (char *) macglyphorder[idx];
00082     } else if (idx - 258 < post->count) {
00083       post->glyphNamePtr[i] = post->names[idx - 258];
00084     } else {
00085       WARN("Invalid glyph name index number: %u (>= %u)",
00086           idx, post->count + 258);
00087       RELEASE(indices);
00088       return -1;
00089     }
00090   }
00091   RELEASE(indices);
00092 
00093   return 0;
00094 }
00095 
00096 struct tt_post_table *
00097 tt_read_post_table (sfnt *sfont)
00098 {
00099   struct tt_post_table *post;
00100   ULONG  offset;
00101 
00102   offset = sfnt_locate_table(sfont, "post");
00103 
00104   post   = NEW(1, struct tt_post_table);
00105 
00106   post->Version            = sfnt_get_ulong(sfont); /* Fixed */
00107   post->italicAngle        = sfnt_get_ulong(sfont); /* Fixed */
00108   post->underlinePosition  = sfnt_get_short(sfont); /* FWord */
00109   post->underlineThickness = sfnt_get_short(sfont); /* FWord */
00110   post->isFixedPitch       = sfnt_get_ulong(sfont);
00111   post->minMemType42       = sfnt_get_ulong(sfont);
00112   post->maxMemType42       = sfnt_get_ulong(sfont);
00113   post->minMemType1        = sfnt_get_ulong(sfont);
00114   post->maxMemType1        = sfnt_get_ulong(sfont);
00115 
00116   post->numberOfGlyphs    = 0;
00117   post->glyphNamePtr      = NULL;
00118   post->count             = 0;
00119   post->names             = NULL;
00120 
00121   if (post->Version == 0x00010000UL) {
00122     post->numberOfGlyphs  = 258; /* wrong */
00123     post->glyphNamePtr    = (char **) macglyphorder;
00124     post->count           = 0;
00125     post->names           = NULL;
00126   } else if (post->Version == 0x00025000UL) {
00127     WARN("TrueType 'post' version 2.5 found (deprecated)");
00128     post->numberOfGlyphs  = 0; /* wrong */
00129     post->glyphNamePtr    = NULL;
00130     post->count           = 0;
00131     post->names           = NULL;
00132   } else if (post->Version == 0x00020000UL) {
00133     if (read_v2_post_names(post, sfont) < 0) {
00134       WARN("Invalid TrueType 'post' table...");
00135       tt_release_post_table(post);
00136       post = NULL;
00137     }
00138   } else if (post->Version == 0x00030000UL) {
00139     post->numberOfGlyphs  = 0; /* wrong */
00140     post->glyphNamePtr    = NULL;
00141     post->count           = 0;
00142     post->names           = NULL;
00143   } else {
00144     WARN("Unknown 'post' version: %08X", post->Version);
00145     tt_release_post_table(post);
00146     post = NULL;
00147   }
00148 
00149   return post;
00150 }
00151 
00152 USHORT
00153 tt_lookup_post_table (struct tt_post_table *post, const char *glyphname)
00154 {
00155   USHORT  gid;
00156 
00157   ASSERT(post && glyphname);
00158 
00159   for (gid = 0; gid < post->count; gid++) {
00160     if (post->glyphNamePtr[gid] &&
00161        !strcmp(glyphname, post->glyphNamePtr[gid])) {
00162       return  gid;
00163     }
00164   }
00165 
00166   return 0;
00167 }
00168 
00169 void
00170 tt_release_post_table (struct tt_post_table *post)
00171 {
00172   USHORT i;
00173 
00174   ASSERT(post);
00175 
00176   if (post->glyphNamePtr && post->Version != 0x00010000UL)
00177     RELEASE(post->glyphNamePtr);
00178   if (post->names) {
00179     for (i = 0; i < post->count; i++) {
00180       if (post->names[i])
00181        RELEASE(post->names[i]);
00182     }
00183     RELEASE(post->names);
00184   }
00185   post->count        = 0;
00186   post->glyphNamePtr = NULL;
00187   post->names        = NULL;
00188 
00189   RELEASE(post);
00190 
00191   return;
00192 }
00193 
00194 /* Macintosh glyph order - from apple's TTRefMan */
00195 static const char *const
00196 macglyphorder[258] = {
00197   /* 0x0000 */
00198   ".notdef", ".null", "nonmarkingreturn", "space", "exclam", "quotedbl",
00199   "numbersign", "dollar", "percent", "ampersand", "quotesingle",
00200   "parenleft", "parenright", "asterisk", "plus", "comma",
00201   /* 0x0010 */
00202   "hyphen", "period", "slash", "zero", "one", "two", "three", "four",
00203   "five", "six", "seven", "eight", "nine", "colon", "semicolon", "less",
00204   /* 0x0020 */
00205   "equal", "greater", "question", "at", "A", "B", "C", "D",
00206   "E", "F", "G", "H", "I", "J", "K", "L",
00207   /* 0x0030 */
00208   "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X",
00209   "Y", "Z", "bracketleft", "backslash",
00210   /* 0x0040 */
00211   "bracketright", "asciicircum", "underscore", "grave",
00212   "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
00213   /* 0x0050 */
00214   "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x",
00215   "y", "z", "braceleft", "bar",
00216   /* 0x0060 */
00217   "braceright", "asciitilde", "Adieresis", "Aring", "Ccedilla",
00218   "Eacute", "Ntilde", "Odieresis", "Udieresis", "aacute", "agrave",
00219   "acircumflex", "adieresis", "atilde", "aring", "ccedilla",
00220   /* 0x0070 */
00221   "eacute", "egrave", "ecircumflex", "edieresis", "iacute", "igrave",
00222   "icircumflex", "idieresis", "ntilde", "oacute", "ograve", "ocircumflex",
00223   "odieresis", "otilde", "uacute", "ugrave",
00224   /* 0x0080 */
00225   "ucircumflex", "udieresis", "dagger", "degree", "cent", "sterling",
00226   "section", "bullet", "paragraph", "germandbls", "registered",
00227   "copyright", "trademark", "acute", "dieresis", "notequal",
00228   /* 0x0090 */
00229   "AE", "Oslash", "infinity", "plusminus", "lessequal", "greaterequal",
00230   "yen", "mu", "partialdiff", "summation", "product", "pi", "integral",
00231   "ordfeminine", "ordmasculine", "Omega",
00232   /* 0x00a0 */
00233   "ae", "oslash", "questiondown", "exclamdown", "logicalnot", "radical",
00234   "florin", "approxequal", "Delta", "guillemotleft", "guillemotright",
00235   "ellipsis", "nonbreakingspace", "Agrave", "Atilde", "Otilde",
00236   /* 0x00b0 */
00237   "OE", "oe", "endash", "emdash", "quotedblleft", "quotedblright",
00238   "quoteleft", "quoteright", "divide", "lozenge", "ydieresis",
00239   "Ydieresis", "fraction", "currency", "guilsinglleft", "guilsinglright",
00240   /* 0x00c0 */
00241   "fi", "fl", "daggerdbl", "periodcentered", "quotesinglbase",
00242   "quotedblbase", "perthousand", "Acircumflex",   "Ecircumflex", "Aacute",
00243   "Edieresis", "Egrave", "Iacute", "Icircumflex", "Idieresis", "Igrave",
00244   /* 0x00d0 */
00245   "Oacute", "Ocircumflex", "apple", "Ograve", "Uacute", "Ucircumflex",
00246   "Ugrave", "dotlessi", "circumflex", "tilde", "macron", "breve",
00247   "dotaccent", "ring", "cedilla", "hungarumlaut",
00248   /* 0x00e0 */
00249   "ogonek", "caron", "Lslash", "lslash", "Scaron", "scaron", "Zcaron",
00250   "zcaron", "brokenbar", "Eth", "eth", "Yacute", "yacute", "Thorn",
00251   "thorn", "minus",
00252   /* 0x00f0 */
00253   "multiply", "onesuperior", "twosuperior", "threesuperior", "onehalf",
00254   "onequarter", "threequarters", "franc", "Gbreve", "gbreve", "Idotaccent",
00255   "Scedilla", "scedilla", "Cacute", "cacute", "Ccaron",
00256   /* 0x0100 */
00257   "ccaron", "dcroat"
00258 };