JNIEXPORT jboolean JNICALL Java_org_gnome_pango_PangoFontDescription_pango_1font_1description_1better_1match ( JNIEnv* env, jclass cls, jlong _self, jlong _oldMatch, jlong _newMatch ) { gboolean result; jboolean _result; PangoFontDescription* self; const PangoFontDescription* oldMatch; const PangoFontDescription* newMatch; // convert parameter self self = (PangoFontDescription*) _self; // convert parameter oldMatch oldMatch = (const PangoFontDescription*) _oldMatch; // convert parameter newMatch newMatch = (const PangoFontDescription*) _newMatch; // call function result = pango_font_description_better_match(self, oldMatch, newMatch); // cleanup parameter self // cleanup parameter oldMatch // cleanup parameter newMatch // translate return value to JNI type _result = (jboolean) result; // and finally return _result; }
/* * Returns the index of the fonts closest style match from the provided list of styles * Used in both the Text dialog and the Text toolbar to set the style combo on selection change */ unsigned int sp_font_selector_get_best_style (font_instance *font, GList *list) { font_instance *tempFont = NULL; unsigned int currentStyleNumber = 0; unsigned int bestStyleNumber = 0; Glib::ustring family = font_factory::Default()->GetUIFamilyString(font->descr); PangoFontDescription *incomingFont = pango_font_description_copy(font->descr); pango_font_description_unset_fields(incomingFont, PANGO_FONT_MASK_SIZE); char *incomingFontString = pango_font_description_to_string(incomingFont); tempFont = (font_factory::Default())->FaceFromUIStrings(family.c_str(), (char*)list->data); PangoFontDescription *bestMatchForFont = NULL; if (tempFont) { bestMatchForFont = pango_font_description_copy(tempFont->descr); tempFont->Unref(); tempFont = NULL; } pango_font_description_unset_fields(bestMatchForFont, PANGO_FONT_MASK_SIZE); list = list->next; while (list) { currentStyleNumber++; tempFont = font_factory::Default()->FaceFromUIStrings(family.c_str(), (char*)list->data); PangoFontDescription *currentMatchForFont = NULL; if (tempFont) { currentMatchForFont = pango_font_description_copy(tempFont->descr); tempFont->Unref(); tempFont = NULL; } if (currentMatchForFont) { pango_font_description_unset_fields(currentMatchForFont, PANGO_FONT_MASK_SIZE); char *currentMatchString = pango_font_description_to_string(currentMatchForFont); if (!strcmp(incomingFontString, currentMatchString) || pango_font_description_better_match(incomingFont, bestMatchForFont, currentMatchForFont)) { // Found a better match for the font we are looking for pango_font_description_free(bestMatchForFont); bestMatchForFont = pango_font_description_copy(currentMatchForFont); bestStyleNumber = currentStyleNumber; } g_free(currentMatchString); pango_font_description_free(currentMatchForFont); } list = list->next; } if (bestMatchForFont) pango_font_description_free(bestMatchForFont); if (incomingFont) pango_font_description_free(incomingFont); g_free(incomingFontString); return bestStyleNumber; }
// http://library.gnome.org/devel/pango/1.28/pango-Fonts.html#pango-font-description-better-match Glib::ustring font_factory::FontSpecificationBestMatch(const Glib::ustring & fontSpec ) { Glib::ustring newFontSpec; // Look for exact match PangoStringToDescrMap::iterator it = fontInstanceMap.find(fontSpec); // If there is no exact match, look for the best match. if (it != fontInstanceMap.end()) { newFontSpec = fontSpec; } else { PangoFontDescription *fontDescr = pango_font_description_from_string(fontSpec.c_str()); PangoFontDescription *bestMatchDescr = NULL; // Grab the UI Family string from the descr Glib::ustring family = GetUIFamilyString(fontDescr); Glib::ustring bestMatchDescription; bool setFirstFamilyMatch = false; for (it = fontInstanceMap.begin(); it != fontInstanceMap.end(); ++it) { Glib::ustring currentFontSpec = (*it).first; Glib::ustring currentFamily = GetUIFamilyString((*it).second); // Save some time by only looking at the right family. // Must use family name rather than fontSpec // (otherwise DejaVu Sans matches DejaVu Sans Mono). if (currentFamily == family) { if (!setFirstFamilyMatch) { // This ensures that the closest match is at least within the correct // family rather than the first font in the list bestMatchDescr = pango_font_description_copy((*it).second); bestMatchDescription = currentFontSpec; setFirstFamilyMatch = true; } else { // Get the font description that corresponds, and // then see if we've found a better match PangoFontDescription *possibleMatch = pango_font_description_copy((*it).second); if (pango_font_description_better_match( fontDescr, bestMatchDescr, possibleMatch)) { pango_font_description_free(bestMatchDescr); bestMatchDescr = possibleMatch; bestMatchDescription = currentFontSpec; } else { pango_font_description_free(possibleMatch); } } } } // for newFontSpec = bestMatchDescription; // If NULL, then no match found pango_font_description_free(fontDescr); pango_font_description_free(bestMatchDescr); } return newFontSpec; }