summaryrefslogtreecommitdiff
path: root/src/sfnt/ttsvg.c
blob: fced19337b2837ebe2fc6c16e76720cb8028c1d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/****************************************************************************
 *
 * ttsvg.c
 *
 *   OpenType SVG Color (specification).
 *
 * Copyright (C) 2018-2019 by
 * David Turner, Robert Wilhelm, Werner Lemberg and Moazin Khatti.
 *
 * This file is part of the FreeType project, and may only be used,
 * modified, and distributed under the terms of the FreeType project
 * license, LICENSE.TXT.  By continuing to use, modify, or distribute
 * this file you indicate that you have read the license and
 * understand and accept it fully.
 *
 */


  /**************************************************************************
   *
   * `SVG' table specification:
   *
   *    https://docs.microsoft.com/en-us/typography/opentype/spec/svg
   *
   */

#include <ft2build.h>
#include FT_INTERNAL_STREAM_H
#include FT_INTERNAL_OBJECTS_H
#include FT_TRUETYPE_TAGS_H
#include FT_GZIP_H
#include FT_OTSVG_H

#ifdef FT_CONFIG_OPTION_SVG

#include "ttsvg.h"

  typedef struct Svg_
  {
    FT_UShort  version;        /* Table version (starting at 0)  */
    FT_UShort  num_entries;    /* Number of SVG document records */
    /* Pointer to the starting of SVG Document List */
    FT_Byte*   svg_doc_list;
    void*     table;           /* Memory that backs up SVG */
    FT_ULong  table_size;
  } Svg;


  FT_LOCAL_DEF( FT_Error )
  tt_face_load_svg( TT_Face    face,
                    FT_Stream  stream )
  {
    FT_Error   error;
    FT_Memory  memory = face->root.memory;

    FT_ULong   table_size;
    FT_Byte*   table = NULL;
    FT_Byte*   p     = NULL;

    Svg*       svg   = NULL;

    FT_ULong  offsetToSVGDocumentList;


    error = face->goto_table( face, TTAG_SVG, stream, &table_size );
    if( error )
      goto NoSVG;

    if( FT_FRAME_EXTRACT( table_size, table ))
      goto NoSVG;

    /* Allocate the memory for the Svg object */
    if( FT_NEW( svg ) )
      goto NoSVG;

    p = table;
    svg->version =            FT_NEXT_USHORT( p );
    offsetToSVGDocumentList = FT_NEXT_ULONG( p );

    if( offsetToSVGDocumentList == 0 )
      goto InvalidTable;

    svg->svg_doc_list = (FT_Byte*)( table + offsetToSVGDocumentList );

    p = svg->svg_doc_list;
    svg->num_entries = FT_NEXT_USHORT( p );

    FT_TRACE3(( "version: %d\n", svg->version ));
    FT_TRACE3(( "num entiries: %d\n", svg->num_entries ));

    svg->table =      table;
    svg->table_size = table_size;

    face->svg = svg;

    face->root.face_flags |= FT_FACE_FLAG_SVG;

    return FT_Err_Ok;

  InvalidTable:
    error = FT_THROW( Invalid_Table );

  NoSVG:
    FT_FRAME_RELEASE( table );
    FT_FREE( svg );
    face->svg = NULL;

    return error;
  }

  FT_LOCAL_DEF( void )
  tt_face_free_svg( TT_Face  face )
  {
    FT_Memory  memory = face->root.memory;
    FT_Stream  stream = face->root.stream;
    Svg*       svg    = (Svg*) face->svg;


    if( svg )
    {
      FT_FRAME_RELEASE( svg->table );
      FT_FREE( svg );
    }
  }

  typedef struct Svg_doc_
  {
    FT_UShort  start_glyph_id;
    FT_UShort  end_glyph_id;
    FT_ULong   offset;
    FT_ULong   length;
  } Svg_doc;

  static Svg_doc
  extract_svg_doc( FT_Byte*  stream )
  {
    Svg_doc  doc;
    doc.start_glyph_id = FT_NEXT_USHORT( stream );
    doc.end_glyph_id   = FT_NEXT_USHORT( stream );
    doc.offset = FT_NEXT_ULONG( stream );
    doc.length = FT_NEXT_ULONG( stream );
    return doc;
  }

  static FT_Int
  compare_svg_doc( Svg_doc  doc,
                   FT_UInt  glyph_index )
  {
    if ( glyph_index < doc.start_glyph_id )
      return -1;
    else if ( glyph_index > doc.end_glyph_id )
      return 1;
    else
      return 0;
  }

  static FT_Error
  find_doc( FT_Byte*   stream,
            FT_UShort  num_entries,
            FT_UInt    glyph_index,
            FT_ULong   *doc_offset,
            FT_ULong   *doc_length,
            FT_UShort  *start_glyph,
            FT_UShort  *end_glyph )
  {
    FT_Error   error;
    Svg_doc    start_doc;
    Svg_doc    mid_doc;
    Svg_doc    end_doc;

    FT_Bool  found       = FALSE;
    FT_UInt  i           = 0;
    FT_UInt  start_index = 0;
    FT_UInt  end_index   = num_entries - 1;
    FT_Int   comp_res;


    /* search algo */
    if ( num_entries == 0 )
    {
      error = FT_THROW( Invalid_Table );
      return error;
    }

    FT_TRACE6(( "--- binary search glyph id: %d ---\n", glyph_index ));

    start_doc = extract_svg_doc( stream + start_index * 12 );
    end_doc   = extract_svg_doc( stream + end_index * 12 );

    FT_TRACE6(( "--- start glyph ---\n" ));
    FT_TRACE6(( "start_id: %d\n", start_doc.start_glyph_id ));
    FT_TRACE6(( "end_id: %d\n", start_doc.end_glyph_id ));
    FT_TRACE6(( "--- end glyph ---\n" ));
    FT_TRACE6(( "start_id: %d\n", end_doc.start_glyph_id ));
    FT_TRACE6(( "end_id: %d\n", end_doc.end_glyph_id ));
    if ( ( compare_svg_doc( start_doc, glyph_index ) == -1 ) ||
         ( compare_svg_doc( end_doc, glyph_index ) == 1 ) )
    {
      error = FT_THROW( Invalid_Glyph_Index );
      return error;
    }

    while ( start_index <= end_index )
    {
      i = ( start_index + end_index ) / 2;
      mid_doc = extract_svg_doc( stream + i * 12 );
      FT_TRACE6(( "--- current glyph ---\n" ));
      FT_TRACE6(( "start_id: %d\n", mid_doc.start_glyph_id ));
      FT_TRACE6(( "end_id: %d\n", mid_doc.end_glyph_id ));
      comp_res = compare_svg_doc( mid_doc, glyph_index );
      if ( comp_res == 1 )
      {
        start_index = i + 1;
        start_doc = extract_svg_doc( stream + start_index * 4 );
        FT_TRACE6(( "RIGHT\n" ));
      }
      else if ( comp_res == -1 )
      {
        end_index = i - 1;
        end_doc = extract_svg_doc( stream + end_index * 4 );
        FT_TRACE6(( "LEFT\n" ));
      }
      else
      {
        found = TRUE;
        FT_TRACE5(( "FOUND\n" ));
        break;
      }
    }

    FT_TRACE5(( "--- binary search end ---\n" ));
    /* search algo end */
    if ( found != TRUE )
    {
      FT_TRACE5(( "NOT FOUND\n" ));
      error = FT_THROW( Invalid_Glyph_Index );
    }
    else
    {
      *doc_offset = mid_doc.offset;
      *doc_length = mid_doc.length;
      *start_glyph = mid_doc.start_glyph_id;
      *end_glyph   = mid_doc.end_glyph_id;
      error = FT_Err_Ok;
    }
    return error;
  }

  FT_LOCAL_DEF( FT_Error )
  tt_face_load_svg_doc( FT_GlyphSlot  glyph,
                        FT_UInt       glyph_index )
  {
    FT_Byte*   doc_list;             /* Pointer to the Svg Document List */
    FT_UShort  num_entries;          /* Total no of entires in doc list  */

    FT_ULong   doc_offset;
    FT_ULong   doc_length;
    FT_UShort  start_glyph_id;
    FT_UShort  end_glyph_id;

    FT_ULong   uncomp_size;
    FT_Byte*   uncomp_buffer;

    FT_Error   error  = FT_Err_Ok;
    TT_Face    face   = (TT_Face)glyph->face;
    FT_Memory  memory = face->root.memory;
    Svg*       svg    = face->svg;

    FT_SVG_Document  svg_document = glyph->other;

    FT_ASSERT( !( svg == NULL ) );

    doc_list     = svg->svg_doc_list;
    num_entries  = FT_NEXT_USHORT( doc_list );

    error = find_doc( doc_list, num_entries, glyph_index,
                                &doc_offset, &doc_length,
                                &start_glyph_id, &end_glyph_id );
    if ( error != FT_Err_Ok )
      goto Exit;

    doc_list = svg->svg_doc_list;   /* Reset to so we can use it again */
    doc_list = (FT_Byte*)( doc_list + doc_offset );

    if( ( doc_list[0] == 0x1F ) && ( doc_list[1] == 0x8B )
                                && ( doc_list[2] == 0x08 ) )
    {
      /* get the size of the orignal document. This helps in alotting the
       * buffer to accomodate the uncompressed version. The last 4 bytes
       * of the compressed document are equal to orignal_size modulo 2^32.
       * Since SVG docs will be lesser in size then 2^32, we can use this
       * accurately. The four bytes are stored in little-endian format.
       */
      FT_TRACE4(( "SVG document found is GZIP compressed\n" ));
      uncomp_size = (FT_ULong)doc_list[doc_length - 1] << 24 |
                    (FT_ULong)doc_list[doc_length - 2] << 16 |
                    (FT_ULong)doc_list[doc_length - 3] << 8  |
                    (FT_ULong)doc_list[doc_length - 4];

      uncomp_buffer = (FT_Byte*) memory->alloc( memory, uncomp_size );
      error = FT_Gzip_Uncompress( memory, uncomp_buffer, &uncomp_size,
                                          doc_list,      doc_length  );
      if ( error != FT_Err_Ok )
      {
        memory->free( memory, uncomp_buffer );
        error = FT_THROW( Invalid_Table );
        goto Exit;
      }
      glyph->internal->flags |= FT_GLYPH_OWN_GZIP_SVG;
      doc_list   = uncomp_buffer;
      doc_length = uncomp_size;
    }

    svg_document->svg_document        = doc_list;
    svg_document->svg_document_length = doc_length;
    svg_document->metrics             = glyph->face->size->metrics;
    svg_document->units_per_EM        = glyph->face->units_per_EM;
    svg_document->start_glyph_id      = start_glyph_id;
    svg_document->end_glyph_id        = end_glyph_id;

    svg_document->transform.xx = 0x10000;
    svg_document->transform.xy = 0;
    svg_document->transform.yx = 0;
    svg_document->transform.yy = 0x10000;
    svg_document->delta.x      = 0;
    svg_document->delta.y      = 0;

    FT_TRACE5(( "start_glyph_id: %d\n", start_glyph_id ));
    FT_TRACE5(( "end_glyph_id:   %d\n", end_glyph_id ));
    FT_TRACE5(( "svg_document:\n%.*s\n", doc_length, doc_list ));

    glyph->other = svg_document;

Exit:
    return error;
  }

#else /* !FT_CONFIG_OPTION_SVG */

  /* ANSI C doesn't like empty source files */
  typedef int  _tt_svg_dummy;

#endif /* !FT_CONFIG_OPTION_SVG */