|
| 1 | +/* Copyright (C) 2020 LiveCode Ltd. |
| 2 | +
|
| 3 | +This file is part of LiveCode. |
| 4 | +
|
| 5 | +LiveCode is free software; you can redistribute it and/or modify it under |
| 6 | +the terms of the GNU General Public License v3 as published by the Free |
| 7 | +Software Foundation. |
| 8 | +
|
| 9 | +LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY |
| 10 | +WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | +for more details. |
| 13 | +
|
| 14 | +You should have received a copy of the GNU General Public License |
| 15 | +along with LiveCode. If not see <http://www.gnu.org/licenses/>. */ |
| 16 | + |
| 17 | +#include <foundation.h> |
| 18 | +#include <foundation-auto.h> |
| 19 | + |
| 20 | +#include "customfont.h" |
| 21 | + |
| 22 | +static MCCustomFont *s_custom_font_list = nil; |
| 23 | + |
| 24 | +bool MCCustomFontListInitialize() |
| 25 | +{ |
| 26 | + s_custom_font_list = nil; |
| 27 | + |
| 28 | + return true; |
| 29 | +} |
| 30 | + |
| 31 | +void MCCustomFontListFinalize() |
| 32 | +{ |
| 33 | + for (MCCustomFont *t_font = s_custom_font_list; t_font != nil; ) |
| 34 | + { |
| 35 | + MCCustomFont *t_next_font; |
| 36 | + t_next_font = t_font->next; |
| 37 | + MCCustomFontDelete(t_font); |
| 38 | + t_font = t_next_font; |
| 39 | + } |
| 40 | + s_custom_font_list = nil; |
| 41 | +} |
| 42 | + |
| 43 | +bool MCCustomFontCreate(MCStringRef p_path, MCStringRef p_name, MCStringRef p_family, MCCustomFontStyle p_style, MCCustomFont* &r_font) |
| 44 | +{ |
| 45 | + if (!MCMemoryNew(r_font)) |
| 46 | + return false; |
| 47 | + |
| 48 | + r_font->path = MCValueRetain(p_path); |
| 49 | + r_font->name = MCValueRetain(p_name); |
| 50 | + r_font->family = MCValueRetain(p_family); |
| 51 | + r_font->style = p_style; |
| 52 | + |
| 53 | + r_font->next = nil; |
| 54 | + |
| 55 | + return true; |
| 56 | +} |
| 57 | + |
| 58 | +void MCCustomFontDelete(MCCustomFont *p_font) |
| 59 | +{ |
| 60 | + if (p_font != nil) |
| 61 | + { |
| 62 | + MCValueRelease(p_font->path); |
| 63 | + MCValueRelease(p_font->name); |
| 64 | + MCValueRelease(p_font->family); |
| 65 | + MCMemoryDelete(p_font); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +void MCCustomFontListAddFont(MCCustomFont *p_font) |
| 70 | +{ |
| 71 | + if (s_custom_font_list == nil) |
| 72 | + s_custom_font_list = p_font; |
| 73 | + else |
| 74 | + { |
| 75 | + MCCustomFont *t_font = s_custom_font_list; |
| 76 | + while (t_font->next != nil) |
| 77 | + t_font = t_font->next; |
| 78 | + t_font->next = p_font; |
| 79 | + } |
| 80 | + p_font->next = nil; |
| 81 | +} |
| 82 | + |
| 83 | +bool MCCustomFontListGetNames(MCStringRef& r_names) |
| 84 | +{ |
| 85 | + bool t_success; |
| 86 | + t_success = true; |
| 87 | + |
| 88 | + MCAutoListRef t_list; |
| 89 | + if (!MCListCreateMutable('\n', &t_list)) |
| 90 | + return false; |
| 91 | + |
| 92 | + for (MCCustomFont *t_font = s_custom_font_list; t_success && t_font != nil; t_font = t_font->next) |
| 93 | + t_success = MCListAppend(*t_list, t_font->name); |
| 94 | + |
| 95 | + if (t_success) |
| 96 | + return MCListCopyAsString(*t_list, r_names); |
| 97 | + |
| 98 | + return false; |
| 99 | +} |
| 100 | + |
| 101 | +bool MCCustomFontListLookupFontByName(MCStringRef p_name, MCCustomFont* &r_font) |
| 102 | +{ |
| 103 | + for (MCCustomFont *t_font = s_custom_font_list; t_font != nil; t_font = t_font->next) |
| 104 | + { |
| 105 | + if (MCStringIsEqualTo(p_name, t_font->name, kMCStringOptionCompareCaseless)) |
| 106 | + { |
| 107 | + r_font = t_font; |
| 108 | + return true; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return false; |
| 113 | +} |
| 114 | + |
| 115 | +bool MCCustomFontListLookupFontByFamilyAndStyle(MCStringRef p_family, bool p_bold, bool p_italic, MCCustomFont* &r_font) |
| 116 | +{ |
| 117 | + MCCustomFont *t_closest_font; |
| 118 | + t_closest_font = nil; |
| 119 | + for (MCCustomFont *t_font = s_custom_font_list; t_font != nil; t_font = t_font->next) |
| 120 | + { |
| 121 | + if (MCStringIsEqualTo(p_family, t_font->family, kMCStringOptionCompareCaseless)) |
| 122 | + { |
| 123 | + if ((p_bold && p_italic && t_font->style == kMCCustomFontStyleBoldItalic) || |
| 124 | + (p_bold && t_font->style == kMCCustomFontStyleBold) || |
| 125 | + (p_italic && t_font->style == kMCCustomFontStyleItalic)) |
| 126 | + { |
| 127 | + r_font = t_font; |
| 128 | + return true; |
| 129 | + } |
| 130 | + else if (t_closest_font == nil) |
| 131 | + t_closest_font = t_font; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + if (t_closest_font != nil) |
| 136 | + { |
| 137 | + r_font = t_closest_font; |
| 138 | + return true; |
| 139 | + } |
| 140 | + |
| 141 | + return false; |
| 142 | +} |
| 143 | + |
| 144 | +bool MCCustomFontListLookupFont(MCStringRef p_name, bool p_bold, bool p_italic, MCCustomFont* &r_font) |
| 145 | +{ |
| 146 | + MCAutoStringRef t_styled_name; |
| 147 | + if (!MCStringMutableCopy(p_name, &t_styled_name)) |
| 148 | + return false; |
| 149 | + if (p_bold && !MCStringAppend(*t_styled_name, MCSTR(" Bold"))) |
| 150 | + return false; |
| 151 | + if (p_italic && !MCStringAppend(*t_styled_name, MCSTR(" Italic"))) |
| 152 | + return false; |
| 153 | + |
| 154 | + // First of all, attempt to look the font up by taking into account its name and style. |
| 155 | + // e.g. textFont:Arial textStyle:Bold - look for a font named Arial Bold. |
| 156 | + // This will fail for textFonts which include style information e.g. textFont:Arial textStyle:Bold will search for Arial Bold Bold |
| 157 | + if (MCCustomFontListLookupFontByName(*t_styled_name, r_font)) |
| 158 | + return true; |
| 159 | + |
| 160 | + // If no font found, look up based purely on the name. This will solve cases where style |
| 161 | + // information is included in the name e.g. Arial Bold. |
| 162 | + if (p_bold || p_italic) |
| 163 | + { |
| 164 | + if (MCCustomFontListLookupFontByName(p_name, r_font)) |
| 165 | + return true; |
| 166 | + } |
| 167 | + |
| 168 | + // If we've still not found a matching font, look up based on the family and style. |
| 169 | + // This function will attempt to provide a closest match e.g. Arial Bold is requested but only Arial is installed. |
| 170 | + if (MCCustomFontListLookupFontByFamilyAndStyle(p_name, p_bold, p_italic, r_font)) |
| 171 | + return true; |
| 172 | + |
| 173 | + return false; |
| 174 | +} |
| 175 | + |
| 176 | +MCCustomFontStyle MCCustomFontListGetStylesForName(MCStringRef p_name) |
| 177 | +{ |
| 178 | + MCCustomFontStyle t_styles; |
| 179 | + t_styles = 0; |
| 180 | + |
| 181 | + for (MCCustomFont *t_font = s_custom_font_list; t_font != nil; t_font = t_font->next) |
| 182 | + { |
| 183 | + if (MCStringIsEqualTo(p_name, t_font->family, kMCStringOptionCompareCaseless)) |
| 184 | + t_styles |= t_font->style; |
| 185 | + } |
| 186 | + |
| 187 | + if (t_styles == 0) |
| 188 | + { |
| 189 | + MCCustomFont *t_font = nil; |
| 190 | + if (MCCustomFontListLookupFontByName(p_name, t_font)) |
| 191 | + t_styles |= t_font->style; |
| 192 | + } |
| 193 | + |
| 194 | + return t_styles; |
| 195 | +} |
0 commit comments