1 /**************************************************************************\
2  * Copyright (c) Kongsberg Oil & Gas Technologies AS
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 \**************************************************************************/
32 
33 #include "fonts/fontspec.h"
34 
35 #include <cstring>
36 
37 #include "glue/freetype.h"
38 
39 void
cc_fontspec_construct(cc_font_specification * spec,const char * name_style,float size,float complexity)40 cc_fontspec_construct(cc_font_specification * spec,
41                       const char * name_style, float size, float complexity)
42 {
43   const char * tmpstr, * tmpptr;
44 
45   spec->size = size;
46   spec->complexity = complexity;
47 
48   cc_string_construct(&spec->name);
49   cc_string_set_text(&spec->name, name_style);
50 
51   cc_string_construct(&spec->style);
52 
53   /* handle the previously allowed ':Bold Italic' case for fontconfig */
54   /* FIXME: this is an ugly non robust workaround. it would be better
55      to agree on an abstract fontname matching schema that is then
56      consistently applied upon all font backend
57      implementations. 20040929 tamer. */
58   if (cc_fcglue_available()) {
59     tmpstr = cc_string_get_text(&spec->name);
60 
61     if ((tmpptr = strchr(tmpstr, ':'))) {
62       char * tmpptrspace;
63       if ((tmpptrspace = (char *) strchr(tmpptr, ' '))) {
64         *tmpptrspace = ':';
65       }
66     }
67 
68     return;
69   }
70 
71   /* Check if style is included in the fontname using the
72      "family:style" syntax. */
73   tmpstr = cc_string_get_text(&spec->name);
74   if ((tmpptr = strchr(tmpstr, ':'))) {
75     const int pos = (int)(tmpptr - tmpstr);
76     const int namelen = cc_string_length(&spec->name);
77 
78     int trimstyleend, trimnamestart;
79     int trimposstyle = pos + 1;
80     int trimposname = pos - 1;
81 
82     while (tmpstr[trimposstyle] == ' ') {
83       ++trimposstyle;
84     }
85 
86     while (tmpstr[trimposname] == ' ') {
87       --trimposname;
88     }
89 
90     cc_string_set_text(&spec->style, cc_string_get_text(&spec->name));
91     cc_string_remove_substring(&spec->style, 0, trimposstyle - 1);
92     cc_string_remove_substring(&spec->name, trimposname + 1, namelen-1);
93 
94     trimstyleend = cc_string_length(&spec->style);
95     trimposstyle = trimstyleend;
96     tmpstr = cc_string_get_text(&spec->style);
97 
98     while (tmpstr[trimstyleend-1] == ' ') {
99       --trimstyleend;
100     }
101 
102     if(trimstyleend !=  trimposstyle) {
103       cc_string_remove_substring(&spec->style, trimstyleend, cc_string_length(&spec->style) - 1);
104     }
105 
106     tmpstr = cc_string_get_text(&spec->name);
107     trimnamestart = 0;
108     while (tmpstr[trimnamestart] == ' ') {
109       ++trimnamestart;
110     }
111 
112     if (trimnamestart != 0) {
113       cc_string_remove_substring(&spec->name, 0, trimnamestart-1);
114     }
115 
116   }
117 }
118 
119 void
cc_fontspec_copy(const cc_font_specification * from,cc_font_specification * to)120 cc_fontspec_copy(const cc_font_specification * from,
121                  cc_font_specification * to)
122 {
123   to->size = from->size;
124   to->complexity = from->complexity;
125 
126   cc_string_construct(&to->name);
127   cc_string_set_string(&to->name, &from->name);
128   cc_string_construct(&to->style);
129   cc_string_set_string(&to->style, &from->style);
130 }
131 
132 void
cc_fontspec_clean(cc_font_specification * spec)133 cc_fontspec_clean(cc_font_specification * spec)
134 {
135   cc_string_clean(&spec->name);
136   cc_string_clean(&spec->style);
137 }
138