Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Typedefs | Functions | Variables
mozilla-decoder.cpp File Reference
#include "mozilla-decoder.h"
#include <pango/pangofc-fontmap.h>
#include <pango/pangofc-font.h>
#include <gdk/gdkpango.h>
#include "nsString.h"
#include "nsIPersistentProperties2.h"
#include "nsNetUtil.h"
#include "nsReadableUtils.h"
#include "nsICharsetConverterManager.h"
#include "nsICharRepresentable.h"
#include "nsCompressedCharMap.h"

Go to the source code of this file.

Classes

struct  _MozillaDecoderPrivate

Defines

#define PANGO_ENABLE_BACKEND
#define PANGO_ENABLE_ENGINE
#define MOZILLA_DECODER_GET_PRIVATE(obj)   (G_TYPE_INSTANCE_GET_PRIVATE ((obj), MOZILLA_TYPE_DECODER, MozillaDecoderPrivate))

Typedefs

typedef struct _MozillaDecoderPrivate

Functions

MozillaDecoder * mozilla_decoder_new (void)
static FcCharSet * mozilla_decoder_get_charset (PangoFcDecoder *decoder, PangoFcFont *fcfont)
static PangoGlyph mozilla_decoder_get_glyph (PangoFcDecoder *decoder, PangoFcFont *fcfont, guint32 wc)
static PangoFcDecoder * mozilla_find_decoder (FcPattern *pattern, gpointer user_data)
static NS_DEFINE_CID (kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID)
void mozilla_decoder_init (MozillaDecoder *decoder)
void mozilla_decoder_class_init (MozillaDecoderClass *klass)
int mozilla_decoders_init (void)
 mozilla_decoders_init:

Variables

static nsICharsetConverterManagergCharsetManager = NULL
GHashTable * encoder_hash = NULL
GHashTable * cmap_hash = NULL
GHashTable * wide_hash = NULL

Class Documentation

struct _MozillaDecoderPrivate

Definition at line 74 of file mozilla-decoder.cpp.

Collaboration diagram for _MozillaDecoderPrivate:
Class Members
FcCharSet * charset
char * cmap
char * encoder
char * family
gboolean is_wide
nsCOMPtr< nsIUnicodeEncoder > uEncoder

Define Documentation

#define MOZILLA_DECODER_GET_PRIVATE (   obj)    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), MOZILLA_TYPE_DECODER, MozillaDecoderPrivate))

Definition at line 72 of file mozilla-decoder.cpp.

Definition at line 39 of file mozilla-decoder.cpp.

Definition at line 40 of file mozilla-decoder.cpp.


Typedef Documentation

typedef struct _MozillaDecoderPrivate

Definition at line 70 of file mozilla-decoder.cpp.


Function Documentation

void mozilla_decoder_class_init ( MozillaDecoderClass *  klass)

Definition at line 100 of file mozilla-decoder.cpp.

{
    GObjectClass *object_class = G_OBJECT_CLASS(klass);
    PangoFcDecoderClass *parent_class = PANGO_FC_DECODER_CLASS (klass);

    /*   object_class->finalize = test_finalize; */

    parent_class->get_charset = mozilla_decoder_get_charset;
    parent_class->get_glyph = mozilla_decoder_get_glyph;

    g_type_class_add_private (object_class, sizeof (MozillaDecoderPrivate));
}

Here is the call graph for this function:

FcCharSet * mozilla_decoder_get_charset ( PangoFcDecoder *  decoder,
PangoFcFont *  fcfont 
) [static]

Definition at line 239 of file mozilla-decoder.cpp.

{
    MozillaDecoderPrivate *priv = MOZILLA_DECODER_GET_PRIVATE(decoder);

    if (priv->charset)
        return priv->charset;

    // First time this has been accessed.  Populate the charset.
    priv->charset = FcCharSetCreate();

    if (!gCharsetManager) {
        CallGetService(kCharsetConverterManagerCID, &gCharsetManager);
    }

    nsCOMPtr<nsIUnicodeEncoder> encoder;
    nsCOMPtr<nsICharRepresentable> represent;

    if (!gCharsetManager)
        goto end;

    gCharsetManager->GetUnicodeEncoderRaw(priv->encoder, getter_AddRefs(encoder));
    if (!encoder)
        goto end;
    
    encoder->SetOutputErrorBehavior(encoder->kOnError_Replace, nsnull, '?');

    priv->uEncoder = encoder;

    represent = do_QueryInterface(encoder);
    if (!represent)
        goto end;

    PRUint32 map[UCS2_MAP_LEN];
    memset(map, 0, sizeof(map));

    represent->FillInfo(map);

    for (int i = 0; i < NUM_UNICODE_CHARS; i++) {
        if (IS_REPRESENTABLE(map, i))
            FcCharSetAddChar(priv->charset, i);
    }

 end:
    return priv->charset;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PangoGlyph mozilla_decoder_get_glyph ( PangoFcDecoder *  decoder,
PangoFcFont *  fcfont,
guint32  wc 
) [static]

Definition at line 287 of file mozilla-decoder.cpp.

{
    MozillaDecoderPrivate *priv = MOZILLA_DECODER_GET_PRIVATE(decoder);

    PangoGlyph retval = 0;
    PRUnichar inchar = wc;
    PRInt32 inlen = 1;
    char outchar[2] = {0,0};
    PRInt32 outlen = 2;

    priv->uEncoder->Convert(&inchar, &inlen, outchar, &outlen);
    if (outlen != 1) {
        printf("Warning: mozilla_decoder_get_glyph doesn't support more than one character conversions.\n");
        return 0;
    }

    FT_Face face = pango_fc_font_lock_face(fcfont);

#ifdef DEBUG_CUSTOM_ENCODER
    char *filename;
    FcPatternGetString(fcfont->font_pattern, FC_FILE, 0, (FcChar8 **)&filename);
    printf("filename is %s\n", filename);
#endif

    // Make sure to set the right charmap before trying to get the
    // glyph
    if (priv->cmap) {
        if (!strcmp(priv->cmap, "mac_roman")) {
            FT_Select_Charmap(face, ft_encoding_apple_roman);
        }
        else if (!strcmp(priv->cmap, "unicode")) {
            FT_Select_Charmap(face, ft_encoding_unicode);
        }
        else {
            printf("Warning: Invalid charmap entry for family %s\n",
                   priv->family);
        }
    }

    // Standard 8 bit to glyph translation
    if (!priv->is_wide) {
        FcChar32 blah = PRUint8(outchar[0]);
        retval = FT_Get_Char_Index(face, blah);
#ifdef DEBUG_CUSTOM_ENCODER
        printf("wc 0x%x outchar[0] 0x%x index 0x%x retval 0x%x face %p\n",
               wc, outchar[0], blah, retval, (void *)face);
#endif
    }
    else {
        printf("Warning: We don't support .wide fonts!\n");
        retval = 0;
    }

    pango_fc_font_unlock_face(fcfont);

    return retval;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void mozilla_decoder_init ( MozillaDecoder *  decoder)

Definition at line 95 of file mozilla-decoder.cpp.

{
}
MozillaDecoder * mozilla_decoder_new ( void  )

Definition at line 114 of file mozilla-decoder.cpp.

{
    return (MozillaDecoder *)g_object_new(MOZILLA_TYPE_DECODER, NULL);
}

Here is the caller graph for this function:

mozilla_decoders_init:

mozilla_decoders_init:

This initializes all of the application-specific custom decoders that Mozilla uses. This should only be called once during the lifetime of the application.

Return value: zero on success, not zero on failure.

Definition at line 141 of file mozilla-decoder.cpp.

{
    static PRBool initialized = PR_FALSE;
    if (initialized)
        return 0;

    PangoContext* context = gdk_pango_context_get ();
    PangoFontMap* fontmap = pango_context_get_font_map (context);
    g_object_unref (context);
    
    if (!PANGO_IS_FC_FONT_MAP (fontmap))
        return -1;

    encoder_hash = g_hash_table_new(g_str_hash, g_str_equal);
    cmap_hash = g_hash_table_new(g_str_hash, g_str_equal);
    wide_hash = g_hash_table_new(g_str_hash, g_str_equal);

    PRBool dumb = PR_FALSE;
    nsCOMPtr<nsIPersistentProperties> props;
    nsCOMPtr<nsISimpleEnumerator> encodeEnum;

    NS_LoadPersistentPropertiesFromURISpec(getter_AddRefs(props),
        NS_LITERAL_CSTRING("resource://gre/res/fonts/pangoFontEncoding.properties"));

    if (!props)
        goto loser;

    // Enumerate the properties in this file and figure out all of the
    // fonts for which we have custom encodings.
    props->Enumerate(getter_AddRefs(encodeEnum));
    if (!encodeEnum)
        goto loser;

    while (encodeEnum->HasMoreElements(&dumb), dumb) {
        nsCOMPtr<nsIPropertyElement> prop;
        encodeEnum->GetNext(getter_AddRefs(prop));
        if (!prop)
            goto loser;

        nsCAutoString name;
        prop->GetKey(name);
        nsAutoString value;
        prop->GetValue(value);

        if (!StringBeginsWith(name, NS_LITERAL_CSTRING("encoding."))) {
            printf("string doesn't begin with encoding?\n");
            continue;
        }

        name = Substring(name, 9);

        if (StringEndsWith(name, NS_LITERAL_CSTRING(".ttf"))) {
            name = Substring(name, 0, name.Length() - 4);

            // Strip off a .wide if it's there.
            if (StringEndsWith(value, NS_LITERAL_STRING(".wide"))) {
                g_hash_table_insert(wide_hash, g_strdup(name.get()),
                                    g_strdup("wide"));
                value = Substring(value, 0, name.Length() - 5);
            }

            g_hash_table_insert(encoder_hash,
                                g_strdup(name.get()),
                                g_strdup(NS_ConvertUTF16toUTF8(value).get()));
        }
        else if (StringEndsWith(name, NS_LITERAL_CSTRING(".ftcmap"))) {
            name = Substring(name, 0, name.Length() - 7);
            g_hash_table_insert(cmap_hash,
                                g_strdup(name.get()),
                                g_strdup(NS_ConvertUTF16toUTF8(value).get()));
        }
        else {
            printf("unknown suffix used for mapping\n");
        }
    }

    pango_fc_font_map_add_decoder_find_func(PANGO_FC_FONT_MAP(fontmap),
                                            mozilla_find_decoder,
                                            NULL,
                                            NULL);

    initialized = PR_TRUE;

#ifdef DEBUG_CUSTOM_ENCODER
    printf("*** encoders\n");
    g_hash_table_foreach(encoder_hash, (GHFunc)dump_hash, NULL);

    printf("*** cmaps\n");
    g_hash_table_foreach(cmap_hash, (GHFunc)dump_hash, NULL);
#endif

    return 0;

 loser:
    return -1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PangoFcDecoder * mozilla_find_decoder ( FcPattern *  pattern,
gpointer  user_data 
) [static]

Definition at line 348 of file mozilla-decoder.cpp.

{
    // Compare the family name of the font that's been opened to see
    // if we have a custom decoder.
    const char *orig = NULL;
    FcPatternGetString(pattern, FC_FAMILY, 0, (FcChar8 **)&orig);

    nsCAutoString family;
    family.Assign(orig);

    family.StripWhitespace();
    ToLowerCase(family);

    char *encoder = (char *)g_hash_table_lookup(encoder_hash, family.get());
    if (!encoder)
        return NULL;

    MozillaDecoder *decoder = mozilla_decoder_new();

    MozillaDecoderPrivate *priv = MOZILLA_DECODER_GET_PRIVATE(decoder);

    priv->family = g_strdup(family.get());
    priv->encoder = g_strdup(encoder);

    char *cmap = (char *)g_hash_table_lookup(cmap_hash, family.get());
    if (cmap)
        priv->cmap = g_strdup(cmap);

    char *wide = (char *)g_hash_table_lookup(wide_hash, family.get());
    if (wide)
        priv->is_wide = TRUE;

    return PANGO_FC_DECODER(decoder);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static NS_DEFINE_CID ( kCharsetConverterManagerCID  ,
NS_ICHARSETCONVERTERMANAGER_CID   
) [static]

Variable Documentation

GHashTable* cmap_hash = NULL

Definition at line 91 of file mozilla-decoder.cpp.

GHashTable* encoder_hash = NULL

Definition at line 90 of file mozilla-decoder.cpp.

Definition at line 83 of file mozilla-decoder.cpp.

GHashTable* wide_hash = NULL

Definition at line 92 of file mozilla-decoder.cpp.