/** \file * \brief Font mapping * * See Copyright Notice in "iup.h" */ #include #include #include #include "iup.h" #include "iup_str.h" #include "iup_drvfont.h" #include "iup_assert.h" #include "iup_attrib.h" #include "iup_class.h" typedef struct _IfontNameMap { const char* pango; const char* x; const char* win; } IfontNameMap; #define IFONT_NAME_MAP_SIZE 7 static IfontNameMap ifont_name_map[IFONT_NAME_MAP_SIZE] = { {"sans", "helvetica", "arial"}, {NULL, "new century schoolbook", "century schoolbook"}, {"monospace", "courier", "courier new"}, {NULL, "lucida", "lucida sans unicode"}, {NULL, "lucidabright", "lucida bright"}, {NULL, "lucidatypewriter", "lucida console"}, {"serif", "times", "times new roman"} }; const char* iupFontGetPangoName(const char* name) { int i; if (!name) return NULL; for (i=0; i str && isspace(*(last - 1))) last--; result = last; while (result > str && !isspace(*(result - 1))) result--; *wordlen = last - result; return result; } int iupFontParsePango(const char *standardfont, char *typeface, int *size, int *bold, int *italic, int *underline, int *strikeout) { const char *p, *last; int len, wordlen, style = 0; if (standardfont[0] == '-') /* X font, abort */ return 0; len = (int)strlen(standardfont); last = standardfont + len; p = iFontGetWord(standardfont, last, &wordlen); /* Look for a size at the end of the string */ if (wordlen != 0) { int new_size = atoi(p); if (new_size != 0) { *size = new_size; last = p; } } /* Now parse style words */ p = iFontGetWord(standardfont, last, &wordlen); while (wordlen != 0) { int new_style = 0; if (!iFontFindStyleName(p, wordlen, &new_style)) break; else { style |= new_style; last = p; p = iFontGetWord(standardfont, last, &wordlen); } } *bold = 0; *italic = 0; *underline = 0; *strikeout = 0; if (style&FONT_BOLD) *bold = 1; if (style&FONT_ITALIC) *italic = 1; if (style&FONT_UNDERLINE) *underline = 1; if (style&FONT_STRIKEOUT) *strikeout = 1; /* Remainder is font family list. */ /* Trim off trailing white space */ while (last > standardfont && isspace(*(last - 1))) last--; /* Trim off trailing commas */ if (last > standardfont && *(last - 1) == ',') last--; /* Again, trim off trailing white space */ while (last > standardfont && isspace(*(last - 1))) last--; /* Trim off leading white space */ while (last > standardfont && isspace(*standardfont)) standardfont++; if (standardfont != last) { len = (last - standardfont); strncpy(typeface, standardfont, len); typeface[len] = 0; return 1; } else return 0; } int iupFontParseWin(const char *value, char *fontname, int *height, int *bold, int *italic, int *underline, int *strikeout) { int c; if (value[0] == '-') /* X font, abort */ return 0; if (strstr(value, ":") == NULL) return 0; if (value[0] == ':') /* check if it has the typeface */ value++; /* jump separator */ else { c = (int)strcspn(value, ":"); /* extract typeface */ if (c == 0) return 0; strncpy(fontname, value, c); fontname[c]='\0'; value += c+1; /* jump typeface and separator */ } *bold = 0; *italic = 0; *underline = 0; *strikeout = 0; if (value[0] == ':') /* check if it has attributes */ value++; /* jump separator */ else { do /* extract style (bold/italic etc) */ { char style[30]; c = (int)strcspn(value, ":,"); if (c == 0) break; strncpy(style, value, c); style[c] = '\0'; if(iupStrEqual(style, "BOLD")) *bold = 1; else if(iupStrEqual(style,"ITALIC")) *italic = 1; else if(iupStrEqual(style,"UNDERLINE")) *underline = 1; else if(iupStrEqual(style,"STRIKEOUT")) *strikeout = 1; value += c; /* jump only the attribute */ if(value[0] == ':') /* end of attribute list */ { value++; break; } value++; /* jump separator */ } while (value[0]); } /* extract size in points */ if (!iupStrToInt(value, height)) return 0; if (height == 0) return 0; return 1; } int iupFontParseX(const char *standardfont, char *typeface, int *size, int *bold, int *italic, int *underline, int *strikeout) { char style1[30], style2[30]; char* token; char font[1024]; if (standardfont[0] != '-') return 0; strcpy(font, standardfont+1); /* skip first '-' */ *bold = 0; *italic = 0; *underline = 0; *strikeout = 0; /* fndry */ token = strtok(font, "-"); if (!token) return 0; /* fmly */ token = strtok(NULL, "-"); if (!token) return 0; strcpy(typeface, token); /* wght */ token = strtok(NULL, "-"); if (!token) return 0; strcpy(style1, token); if (strstr("bold", style1)) *bold = 1; /* slant */ token = strtok(NULL, "-"); if (!token) return 0; strcpy(style2, token); if (*style2 == 'i' || *style2 == 'o') *italic = 1; /* sWdth */ token = strtok(NULL, "-"); if (!token) return 0; /* adstyl */ token = strtok(NULL, "-"); if (!token) return 0; /* pxlsz */ token = strtok(NULL, "-"); if (!token) return 0; *size = -atoi(token); /* size in pixels */ if (*size < 0) return 1; /* ptSz */ token = strtok(NULL, "-"); if (!token) return 0; *size = atoi(token)/10; /* size in deci-points */ if (*size > 0) return 1; return 0; }