summaryrefslogtreecommitdiff
path: root/src/sfnt/ttsvg.c
blob: 27e82968d323eda057837e08c40eb153712c75b3 (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
/****************************************************************************
 *
 * ttsvg.c
 *
 *   OpenType SVG Color (specification).
 *
 * Copyright (C) 2018-2019 by
 * David Turner, Robert Wilhelm, and Werner Lemberg.
 *
 * 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_TRUETYPE_TAGS_H
#include FT_GZIP_H


#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;      
    /* Memory that backs up SVG */
    void*     table;
    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 );

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

    face->svg = 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 );
    }   
  }

  FT_Error
  find_doc( FT_Byte*   stream,
            FT_UShort  num_entries,
            FT_UInt    glyph_index,
            FT_ULong   *doc_offset,
            FT_ULong   *doc_length  )
  {
    FT_Error   error;
    FT_UShort  start_glyph_id;
    FT_UShort  end_glyph_id;
    FT_ULong   cur_doc_offset;
    FT_ULong   cur_doc_length;

    FT_Bool    found = FALSE;
    FT_UInt    i     = 0;

    /* For now it's linear search, later convert to binary search */
    for ( i = 0; i < num_entries; i++)
    {
      start_glyph_id = FT_NEXT_USHORT( stream );
      end_glyph_id   = FT_NEXT_USHORT( stream );
      cur_doc_offset = FT_NEXT_ULONG( stream );
      cur_doc_length = FT_NEXT_ULONG( stream );
      
      if ( ( glyph_index >= start_glyph_id) && 
           ( glyph_index <= end_glyph_id  ) )
      {
        found = TRUE;
        *doc_offset = cur_doc_offset;
        *doc_length = cur_doc_length;
        break;
      }
    }
    if ( found != TRUE )
      error = FT_THROW( Invalid_Glyph_Index );
    else
      error = FT_Err_Ok;
    return error;
  }

  FT_LOCAL_DEF(FT_Error)
  tt_face_load_svg_doc( FT_GlyphSlot  glyph_,
                        FT_UInt       glyph_index )
  {

    TT_GlyphSlot glyph = (TT_GlyphSlot) glyph_;

    /* TODO: properly clean stuff here on errors */

    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_ULong   uncomp_size;
    FT_Byte*   uncomp_buffer;

    FT_Bool    is_gzip_encoded = FALSE;

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

    /* handle svg being 0x0 situation here */
    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 );
    if ( error != FT_Err_Ok )
      return error;

    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 ) )
    {
      is_gzip_encoded = TRUE;
      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];
      
      /* TODO: memory allocated here needs to be freed somewhere */
      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 )
      {
        error = FT_THROW( Invalid_Table );
        return error;
      }

      glyph->svg_document        = uncomp_buffer;
      glyph->svg_document_length = uncomp_size;
      return FT_Err_Ok;
    } 

    glyph->svg_document        = doc_list;
    glyph->svg_document_length = doc_length;
    return FT_Err_Ok;
  }