/**
 * pango_win32_font_cache_loadw:
 * @cache: a #PangoWin32FontCache
 * @logfont: a pointer to a LOGFONTW structure describing the font to load.
 *
 * Creates a HFONT from a LOGFONTW. The
 * result may be newly loaded, or it may have been previously
 * stored
 *
 * Return value: The font structure, or %NULL if the font could
 * not be loaded. In order to free this structure, you must call
 * pango_win32_font_cache_unload().
 *
 * Since: 1.16
 **/
HFONT
pango_win32_font_cache_loadw (PangoWin32FontCache *cache,
			      const LOGFONTW      *lfp)
{
  CacheEntry *entry;
  LOGFONTW lf;
  HFONT hfont;
  int tries;

  g_return_val_if_fail (cache != NULL, NULL);
  g_return_val_if_fail (lfp != NULL, NULL);

  entry = g_hash_table_lookup (cache->forward, lfp);

  if (entry)
    {
      g_atomic_int_inc (&entry->ref_count);
      PING (("increased refcount for cache entry %p: %d", entry->hfont, entry->ref_count));
    }
  else
    {
      BOOL font_smoothing;
      lf = *lfp;
      SystemParametersInfo (SPI_GETFONTSMOOTHING, 0, &font_smoothing, 0);
      /* If on XP or better, try to use ClearType if the global system
       * settings ask for it.
       */
      if (font_smoothing &&
	  (_pango_win32_os_version_info.dwMajorVersion > 5 ||
	   (_pango_win32_os_version_info.dwMajorVersion == 5 &&
	    _pango_win32_os_version_info.dwMinorVersion >= 1)))
	{
	  UINT smoothing_type;

#ifndef SPI_GETFONTSMOOTHINGTYPE
#define SPI_GETFONTSMOOTHINGTYPE 0x200a
#endif
#ifndef FE_FONTSMOOTHINGCLEARTYPE
#define FE_FONTSMOOTHINGCLEARTYPE 2
#endif
#ifndef CLEARTYPE_QUALITY
#define CLEARTYPE_QUALITY 5
#endif
	  SystemParametersInfo (SPI_GETFONTSMOOTHINGTYPE, 0, &smoothing_type, 0);
	  lf.lfQuality =
	    (font_smoothing ?
	     (smoothing_type == FE_FONTSMOOTHINGCLEARTYPE ?
	      CLEARTYPE_QUALITY : ANTIALIASED_QUALITY) :
	     DEFAULT_QUALITY);
	}
      else
	lf.lfQuality = (font_smoothing ? ANTIALIASED_QUALITY : DEFAULT_QUALITY);
      lf.lfCharSet = DEFAULT_CHARSET;
      for (tries = 0; ; tries++)
	{
	  PING (("... trying CreateFontIndirect "
		 "height=%ld,width=%ld,escapement=%ld,orientation=%ld,"
		 "weight=%ld,%s%s%s"
		 "charset=%d,outprecision=%d,clipprecision=%d,"
		 "quality=%d,pitchandfamily=%#.02x,facename=\"%S\")",
		 lf.lfHeight, lf.lfWidth, lf.lfEscapement, lf.lfOrientation,
		 lf.lfWeight, (lf.lfItalic ? "italic," : ""),
		 (lf.lfUnderline ? "underline," : ""),
		 (lf.lfStrikeOut ? "strikeout," : ""),
		 lf.lfCharSet, lf.lfOutPrecision, lf.lfClipPrecision,
		 lf.lfQuality, lf.lfPitchAndFamily, lf.lfFaceName));
	  hfont = CreateFontIndirectW (&lf);

	  if (hfont != NULL)
	    {
	      PING (("Success! hfont=%p", hfont));
	      break;
	    }

	  /* If we fail, try some similar fonts often found on Windows. */
	  if (tries == 0)
	    {
	      gchar *p = g_utf16_to_utf8 (lf.lfFaceName, -1, NULL, NULL, NULL);
	      if (!p)
		; /* Nothing */
	      else if (g_ascii_strcasecmp (p, "helvetica") == 0)
		wcscpy (lf.lfFaceName, L"arial");
	      else if (g_ascii_strcasecmp (p, "new century schoolbook") == 0)
		wcscpy (lf.lfFaceName, L"century schoolbook");
	      else if (g_ascii_strcasecmp (p, "courier") == 0)
		wcscpy (lf.lfFaceName, L"courier new");
	      else if (g_ascii_strcasecmp (p, "lucida") == 0)
		wcscpy (lf.lfFaceName, L"lucida sans unicode");
	      else if (g_ascii_strcasecmp (p, "lucidatypewriter") == 0)
		wcscpy (lf.lfFaceName, L"lucida console");
	      else if (g_ascii_strcasecmp (p, "times") == 0)
		wcscpy (lf.lfFaceName, L"times new roman");
	      g_free (p);
	    }
	  else if (tries == 1)
	    {
	      gchar *p = g_utf16_to_utf8 (lf.lfFaceName, -1, NULL, NULL, NULL);
	      if (!p)
		; /* Nothing */
	      else if (g_ascii_strcasecmp (p, "courier") == 0)
		{
		  wcscpy (lf.lfFaceName, L"");
		  lf.lfPitchAndFamily |= FF_MODERN;
		}
	      else if (g_ascii_strcasecmp (p, "times new roman") == 0)
		{
		  wcscpy (lf.lfFaceName, L"");
		  lf.lfPitchAndFamily |= FF_ROMAN;
		}
	      else if (g_ascii_strcasecmp (p, "helvetica") == 0
		       || g_ascii_strcasecmp (p, "lucida") == 0)
		{
		  wcscpy (lf.lfFaceName, L"");
		  lf.lfPitchAndFamily |= FF_SWISS;
		}
	      else
		{
		  wcscpy (lf.lfFaceName, L"");
		  lf.lfPitchAndFamily = (lf.lfPitchAndFamily & 0x0F) | FF_DONTCARE;
		}
	      g_free (p);
	    }
	  else
	    break;
	  tries++;
	}

      if (!hfont)
	return NULL;

      entry = g_slice_new (CacheEntry);

      entry->logfontw = lf;
      entry->hfont = hfont;

      entry->ref_count = 1;
      entry->mru = NULL;

      g_hash_table_insert (cache->forward, &entry->logfontw, entry);
      g_hash_table_insert (cache->back, entry->hfont, entry);
    }

  if (entry->mru)
    {
      if (cache->mru_count > 1 && entry->mru->prev)
	{
	  /* Move to the head of the mru list */

	  if (entry->mru == cache->mru_tail)
	    {
	      cache->mru_tail = cache->mru_tail->prev;
	      cache->mru_tail->next = NULL;
	    }
	  else
	    {
	      entry->mru->prev->next = entry->mru->next;
	      entry->mru->next->prev = entry->mru->prev;
	    }

	  entry->mru->next = cache->mru;
	  entry->mru->prev = NULL;
	  cache->mru->prev = entry->mru;
	  cache->mru = entry->mru;
	}
    }
  else
    {
      g_atomic_int_inc (&entry->ref_count);

      /* Insert into the mru list */

      if (cache->mru_count == CACHE_SIZE)
	{
	  CacheEntry *old_entry = cache->mru_tail->data;

	  cache->mru_tail = cache->mru_tail->prev;
	  cache->mru_tail->next = NULL;

	  g_list_free_1 (old_entry->mru);
	  old_entry->mru = NULL;
	  cache_entry_unref (cache, old_entry);
	}
      else
	cache->mru_count++;

      cache->mru = g_list_prepend (cache->mru, entry);
      if (!cache->mru_tail)
	cache->mru_tail = cache->mru;
      entry->mru = cache->mru;
    }

  return entry->hfont;
}
Exemplo n.º 2
0
GList *filesel_select_multiple_files_open_with_filter(const gchar *title,
		const gchar *path, const gchar *filter)
{
	GList *file_list = NULL;
	gchar *dir = NULL;
	gchar *file = NULL;
	gunichar2 *f;
	GError *error = NULL;

	o.lpstrFile = g_malloc0(MAXPATHLEN);
	if (!_file_open_dialog(path, title, filter, TRUE)) {
		g_free(o.lpstrFile);
		return NULL;
	}

	/* Now convert the returned directory and file names back from UTF-16.
	 * The content of o.lpstrFile is:
	 * "directory\0file\0file\0...\0file\0\0" for multiple files selected,
	 * "fullfilepath\0" for single file. */
	dir = g_utf16_to_utf8(o.lpstrFile, o.nMaxFile, NULL, NULL, &error);
	if (error != NULL) {
		alertpanel_error(_("Could not convert file path back to UTF-8:\n\n%s"),
				error->message);
		debug_print("returned file path conversion to UTF-8 failed\n");
		g_error_free(error);
	}

	f = o.lpstrFile + g_utf8_strlen(dir, -1) + 1;

	do {
		file = g_utf16_to_utf8(f, o.nMaxFile, NULL, NULL, &error);
		if (error != NULL) {
			alertpanel_error(_("Could not convert file path back to UTF-8:\n\n%s"),
					error->message);
			debug_print("returned file path conversion to UTF-8 failed\n");
			g_error_free(error);
		}

		if (file == NULL || strlen(file) == 0) {
			g_free(file);
			break;
		}

		debug_print("Selected file '%s%c%s'\n",
				dir, G_DIR_SEPARATOR, file);
		file_list = g_list_append(file_list,
				g_strconcat(dir, G_DIR_SEPARATOR_S, file, NULL));

		f = f + g_utf8_strlen(file, -1) + 1;
		g_free(file);
	} while (TRUE);

	if (file_list == NULL) {
		debug_print("Selected single file '%s'\n", dir);
		file_list = g_list_append(file_list, dir);
	} else {
		g_free(dir);
	}

	g_free(o.lpstrFile);
	return file_list;
}
Exemplo n.º 3
0
int main(int argc, char * argv[]){
    int i = 1;
    bool gen_extra_enter = false;

    setlocale(LC_ALL, "");
    //deal with options.
    while ( i < argc ){
        if ( strcmp ("--help", argv[i]) == 0) {
            print_help();
            exit(0);
        } else if (strcmp("--generate-extra-enter", argv[i]) == 0) {
            gen_extra_enter = true;
        } else {
            print_help();
            exit(EINVAL);
        }
        ++i;
    }

    //init phrase table
    PhraseLargeTable phrase_table;
    MemoryChunk * chunk = new MemoryChunk;
    chunk->load("phrase_index.bin");
    phrase_table.load(chunk);

    char * linebuf = NULL;
    size_t size = 0;
    ssize_t read;
    while( (read = getline(&linebuf, &size, stdin)) != -1 ){
        if ( '\n' ==  linebuf[strlen(linebuf) - 1] ) {
            linebuf[strlen(linebuf) - 1] = '\0';
        }

        //check non-ucs2 characters
        const glong num_of_chars = g_utf8_strlen(linebuf, -1);
        glong len = 0;
        utf16_t * sentence = g_utf8_to_utf16(linebuf, -1, NULL, &len, NULL);
        if ( len != num_of_chars ) {
            fprintf(stderr, "non-ucs2 characters encountered:%s.\n", linebuf);
            printf("\n");
            continue;
        }

        //do segment stuff
        GArray * strings = g_array_new(TRUE, TRUE, sizeof(SegmentStep));
        segment(&phrase_table, sentence, len, strings);

        //print out the split phrase
        for ( glong i = 0; i < strings->len; ++i ) {
            SegmentStep * step = &g_array_index(strings, SegmentStep, i);
            char * string = g_utf16_to_utf8( step->m_phrase, step->m_phrase_len, NULL, NULL, NULL);
            printf("%s\n", string);
            g_free(string);
        }

        /* print extra enter */
        if ( gen_extra_enter )
            printf("\n");

        g_array_free(strings, TRUE);
        g_free(sentence);
    }

    /* print enter at file tail */
    printf("\n");
    return 0;
}