23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
25 * POSSIBILITY OF SUCH DAMAGE. |
25 * POSSIBILITY OF SUCH DAMAGE. |
26 */ |
26 */ |
27 |
27 |
28 #include "ascension/ui/font.h" |
|
29 #include "ascension/context.h" |
28 #include "ascension/context.h" |
30 #include "ascension/error.h" |
29 #include "ascension/error.h" |
|
30 #include "ascension/filesystem.h" |
|
31 #include "ascension/ui/font.h" |
31 |
32 |
32 #include <assert.h> |
33 #include <assert.h> |
33 #include <cx/array_list.h> |
34 #include <cx/array_list.h> |
34 |
35 |
35 void asc_font(enum AscFontStyle style, int size) { |
36 void asc_font(enum AscFontStyle style, int size) { |
38 } |
39 } |
39 |
40 |
40 static char const *asc_font_filename(enum AscFontStyle style) { |
41 static char const *asc_font_filename(enum AscFontStyle style) { |
41 switch (style) { |
42 switch (style) { |
42 case ASC_FONT_REGULAR: |
43 case ASC_FONT_REGULAR: |
43 return "fonts/OpenSans-Regular.ttf"; |
44 return "OpenSans-Regular.ttf"; |
44 case ASC_FONT_BOLD: |
45 case ASC_FONT_BOLD: |
45 return "fonts/OpenSans-Bold.ttf"; |
46 return "OpenSans-Bold.ttf"; |
46 case ASC_FONT_ITALIC: |
47 case ASC_FONT_ITALIC: |
47 return "fonts/OpenSans-Italic.ttf"; |
48 return "OpenSans-Italic.ttf"; |
48 case ASC_FONT_BOLD_ITALIC: |
49 case ASC_FONT_BOLD_ITALIC: |
49 return "fonts/OpenSans-BoldItalic.ttf"; |
50 return "OpenSans-BoldItalic.ttf"; |
50 default: |
51 default: |
51 assert(false); |
52 assert(false); |
52 return NULL; |
53 return NULL; |
53 } |
54 } |
54 } |
55 } |
76 void asc_font_cache_destroy(void) { |
77 void asc_font_cache_destroy(void) { |
77 assert(asc_font_cache != NULL); |
78 assert(asc_font_cache != NULL); |
78 cxListFree(asc_font_cache); |
79 cxListFree(asc_font_cache); |
79 } |
80 } |
80 |
81 |
81 |
|
82 TTF_Font *asc_font_load(AscFont font) { |
82 TTF_Font *asc_font_load(AscFont font) { |
83 CxIterator iter = cxListIterator(asc_font_cache); |
83 CxIterator iter = cxListIterator(asc_font_cache); |
84 cx_foreach(struct asc_font_cache_entry*, cache, iter) { |
84 cx_foreach(struct asc_font_cache_entry*, cache, iter) { |
85 if (cache->font.style == font.style && cache->font.size == font.size) { |
85 if (cache->font.style == font.style && cache->font.size == font.size) { |
86 return cache->ttf; |
86 return cache->ttf; |
87 } |
87 } |
88 } |
88 } |
89 |
89 |
90 struct asc_font_cache_entry entry; |
90 struct asc_font_cache_entry entry; |
91 entry.font = font; |
91 entry.font = font; |
92 entry.ttf = TTF_OpenFont(asc_font_filename(font.style), font.size); |
92 cxmutstr fpath = asc_filesystem_combine_paths(cx_strcast(asc_context.font_path), cx_str(asc_font_filename(font.style))); |
|
93 entry.ttf = TTF_OpenFont(fpath.ptr, font.size); |
|
94 cx_strfree(&fpath); |
93 if (entry.ttf == NULL) { |
95 if (entry.ttf == NULL) { |
94 asc_error(TTF_GetError()); |
96 asc_error(TTF_GetError()); |
95 return NULL; |
97 return NULL; |
96 } else { |
98 } else { |
97 cxListAdd(asc_font_cache, &entry); |
99 cxListAdd(asc_font_cache, &entry); |