~ubuntu-branches/ubuntu/precise/xterm/precise-updates

« back to all changes in this revision

Viewing changes to fontutils.c

  • Committer: Bazaar Package Importer
  • Author(s): Bhavani Shankar
  • Date: 2009-05-06 05:13:35 UTC
  • mfrom: (1.1.14 upstream) (11.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20090506051335-wq7afxwrm5ir33xr
Tags: 242-1ubuntu1
* Merge from debian unstable, remaining changes: LP: #372492
  - Enabled URL hilighting
  - rm -rf for .pc patches

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $XTermId: fontutils.c,v 1.287 2009/01/27 01:57:22 tom Exp $ */
 
1
/* $XTermId: fontutils.c,v 1.300 2009/02/13 01:45:01 tom Exp $ */
2
2
 
3
3
/************************************************************
4
4
 
198
198
    return str;
199
199
}
200
200
 
 
201
static Boolean
 
202
check_fontname(const char *name)
 
203
{
 
204
    Boolean result = True;
 
205
 
 
206
    if (name == 0) {
 
207
        TRACE(("fontname missing\n"));
 
208
        result = False;
 
209
    } else if (strlen(name) >= MAX_FONTNAME - 1) {
 
210
        TRACE(("fontname too large: %s\n", name));
 
211
        result = False;
 
212
    }
 
213
    return result;
 
214
}
 
215
 
201
216
/*
202
217
 * Gets the font properties from a given font structure.  We use the FONT name
203
218
 * to find them out, since that seems easier.
237
252
    last_name = name;
238
253
 
239
254
    if (result != 0) {
240
 
        if (strlen(name) < MAX_FONTNAME - 1) {
241
 
            strcpy(result, name);
242
 
        } else {
243
 
            TRACE(("fontname too large: %s\n", name));
 
255
        if (!check_fontname(name))
244
256
            return 0;
245
 
        }
 
257
        strcpy(result, name);
246
258
    }
247
259
 
248
260
    /*
492
504
static Bool
493
505
same_font_name(char *pattern, char *match)
494
506
{
495
 
    while (*pattern && *match) {
496
 
        if (*pattern == *match) {
497
 
            pattern++;
498
 
            match++;
499
 
        } else if (*pattern == '*' || *match == '*') {
500
 
            if (same_font_name(pattern + 1, match)) {
501
 
                return True;
502
 
            } else if (same_font_name(pattern, match + 1)) {
503
 
                return True;
 
507
    Bool result = False;
 
508
 
 
509
    if (pattern && match) {
 
510
        while (*pattern && *match) {
 
511
            if (*pattern == *match) {
 
512
                pattern++;
 
513
                match++;
 
514
            } else if (*pattern == '*' || *match == '*') {
 
515
                if (same_font_name(pattern + 1, match)) {
 
516
                    return True;
 
517
                } else if (same_font_name(pattern, match + 1)) {
 
518
                    return True;
 
519
                } else {
 
520
                    return False;
 
521
                }
504
522
            } else {
505
 
                return False;
 
523
                int p = x_toupper(*pattern++);
 
524
                int m = x_toupper(*match++);
 
525
                if (p != m)
 
526
                    return False;
506
527
            }
507
 
        } else {
508
 
            int p = x_toupper(*pattern++);
509
 
            int m = x_toupper(*match++);
510
 
            if (p != m)
511
 
                return False;
512
528
        }
 
529
        result = (*pattern == *match);  /* both should be NUL */
513
530
    }
514
 
    return (*pattern == *match);        /* both should be NUL */
 
531
    return result;
515
532
}
516
533
 
517
534
/*
682
699
 * failure.
683
700
 */
684
701
Bool
685
 
xtermOpenFont(XtermWidget xw, char *name, XTermFonts * result)
 
702
xtermOpenFont(XtermWidget xw,
 
703
              const char *name,
 
704
              XTermFonts * result,
 
705
              fontWarningTypes warn,
 
706
              Bool force)
686
707
{
687
708
    Bool code = False;
688
709
    TScreen *screen = TScreenOf(xw);
697
718
                result->fn = x_strdup(name);
698
719
            }
699
720
        } else if (strcmp(name, DEFFONT)) {
 
721
            if (warn <= xw->misc.fontWarnings
700
722
#if OPT_RENDERFONT
701
 
            if (!UsingRenderFont(xw))
 
723
                && !UsingRenderFont(xw)
702
724
#endif
 
725
                ) {
 
726
                TRACE(("OOPS: cannot load font %s\n", name));
703
727
                fprintf(stderr, "%s: cannot load font %s\n", ProgramName, name);
704
 
            code = xtermOpenFont(xw, DEFFONT, result);
 
728
            } else {
 
729
                TRACE(("xtermOpenFont: cannot load font %s\n", name));
 
730
            }
 
731
            if (force) {
 
732
                code = xtermOpenFont(xw, DEFFONT, result, fwAlways, True);
 
733
            }
705
734
        }
706
735
    }
707
736
    return code;
708
737
}
709
738
 
710
739
/*
711
 
 * Close the font and Free the font info
 
740
 * Close the font and free the font info.
712
741
 */
713
742
XTermFonts *
714
743
xtermCloseFont(XtermWidget xw, XTermFonts * fnt)
789
818
    char *tmpname = NULL;
790
819
    char normal[MAX_FONTNAME];
791
820
    Boolean proportional = False;
 
821
    fontWarningTypes warn[fMAX];
 
822
    int j;
792
823
 
793
824
    memset(&myfonts, 0, sizeof(myfonts));
794
825
    memset(fnts, 0, sizeof(fnts));
795
826
 
796
827
    if (fonts != 0)
797
828
        myfonts = *fonts;
798
 
    if (myfonts.f_n == 0)
 
829
    if (!check_fontname(myfonts.f_n))
799
830
        return 0;
800
831
 
 
832
    /*
 
833
     * Check the font names against the resource values, to see which were
 
834
     * derived in a previous call.  If so, we'll only warn about those if
 
835
     * the warning level is set to "always".
 
836
     */
 
837
    for (j = 0; j < fMAX; ++j) {
 
838
        warn[j] = fwAlways;
 
839
    }
 
840
#define CmpResource(field, index) \
 
841
    if (same_font_name(screen->menu_font_names[fontnum][index], myfonts.field)) \
 
842
        warn[index] = fwResource
 
843
 
 
844
    CmpResource(f_n, fNorm);
 
845
    if (fontnum == fontMenu_default) {
 
846
        CmpResource(f_b, fBold);
 
847
#if OPT_WIDE_CHARS
 
848
        CmpResource(f_b, fWide);
 
849
        CmpResource(f_b, fWBold);
 
850
#endif
 
851
    }
 
852
 
801
853
    if (fontnum == fontMenu_fontescape
802
854
        && myfonts.f_n != screen->MenuFontName(fontnum)) {
803
855
        if ((tmpname = x_strdup(myfonts.f_n)) == 0)
807
859
    TRACE(("Begin Cgs - xtermLoadFont(%s)\n", myfonts.f_n));
808
860
    releaseWindowGCs(xw, win);
809
861
 
810
 
    TRACE(("xtermLoadFont #%d normal %s\n", fontnum, NonNull(myfonts.f_n)));
811
 
    TRACE(("xtermLoadFont #%d bold   %s\n", fontnum, NonNull(myfonts.f_b)));
 
862
#define DbgResource(name, field, index) \
 
863
    TRACE(("xtermLoadFont #%d "name" %s%s\n", \
 
864
           fontnum, \
 
865
           (warn[index] == fwResource) ? "*" : " ", \
 
866
           NonNull(myfonts.field)));
 
867
    DbgResource("normal", f_n, fNorm);
 
868
    DbgResource("bold  ", f_b, fBold);
812
869
#if OPT_WIDE_CHARS
813
 
    TRACE(("xtermLoadFont #%d wide   %s\n", fontnum, NonNull(myfonts.f_w)));
814
 
    TRACE(("xtermLoadFont #%d w/bold %s\n", fontnum, NonNull(myfonts.f_wb)));
 
870
    DbgResource("wide  ", f_w, fWide);
 
871
    DbgResource("w/bold", f_wb, fWBold);
815
872
#endif
816
873
 
817
 
    if (!xtermOpenFont(xw, myfonts.f_n, &fnts[fNorm]))
 
874
    if (!xtermOpenFont(xw, myfonts.f_n, &fnts[fNorm], warn[fNorm], True))
818
875
        goto bad;
819
876
 
820
877
    strcpy(normal, myfonts.f_n);
821
 
    if (myfonts.f_b == 0) {
 
878
    if (!check_fontname(myfonts.f_b)) {
 
879
        warn[fBold] = fwAlways;
822
880
        fp = get_font_name_props(screen->display, fnts[fNorm].fs, normal);
823
881
        if (fp != 0) {
824
882
            myfonts.f_b = bold_font_name(fp, fp->average_width);
825
 
            if (!xtermOpenFont(xw, myfonts.f_b, &fnts[fBold])) {
 
883
            if (!xtermOpenFont(xw, myfonts.f_b, &fnts[fBold], fwAlways, False)) {
826
884
                myfonts.f_b = bold_font_name(fp, -1);
827
 
                (void) xtermOpenFont(xw, myfonts.f_b, &fnts[fBold]);
 
885
                xtermOpenFont(xw, myfonts.f_b, &fnts[fBold], fwAlways, False);
828
886
            }
829
887
            TRACE(("...derived bold %s\n", NonNull(myfonts.f_b)));
830
888
        }
840
898
            fnts[fBold] = fnts[fNorm];
841
899
            TRACE(("...did not get a matching bold font\n"));
842
900
        }
843
 
    } else if (!xtermOpenFont(xw, myfonts.f_b, &fnts[fBold])) {
 
901
    } else if (!xtermOpenFont(xw, myfonts.f_b, &fnts[fBold], warn[fBold], False)) {
844
902
        xtermCopyFontInfo(&fnts[fBold], &fnts[fNorm]);
 
903
        warn[fBold] = fwAlways;
845
904
        TRACE(("...cannot load bold font %s\n", NonNull(myfonts.f_b)));
846
905
    } else {
847
906
        cache_menu_font_name(screen, fontnum, fBold, myfonts.f_b);
856
915
        Bool derived;
857
916
        char bold[MAX_FONTNAME];
858
917
 
859
 
        if (myfonts.f_w != 0) {
 
918
        if (check_fontname(myfonts.f_w)) {
860
919
            cache_menu_font_name(screen, fontnum, fWide, myfonts.f_w);
861
920
        } else if (!is_double_width_font(fnts[fNorm].fs)) {
862
921
            fp = get_font_name_props(screen->display, fnts[fNorm].fs, normal);
863
922
            if (fp != 0) {
864
923
                myfonts.f_w = wide_font_name(fp);
 
924
                warn[fWide] = fwAlways;
865
925
                TRACE(("...derived wide %s\n", NonNull(myfonts.f_w)));
866
926
                cache_menu_font_name(screen, fontnum, fWide, myfonts.f_w);
867
927
            }
868
928
        }
869
929
 
870
 
        if (myfonts.f_w) {
871
 
            (void) xtermOpenFont(xw, myfonts.f_w, &fnts[fWide]);
 
930
        if (check_fontname(myfonts.f_w)) {
 
931
            (void) xtermOpenFont(xw, myfonts.f_w, &fnts[fWide], warn[fWide], False);
872
932
        } else {
873
933
            xtermCopyFontInfo(&fnts[fWide], &fnts[fNorm]);
 
934
            warn[fWide] = fwAlways;
874
935
        }
875
936
 
876
937
        derived = False;
877
 
        if (myfonts.f_wb == 0) {
 
938
        if (!check_fontname(myfonts.f_wb)) {
878
939
            fp = get_font_name_props(screen->display, fnts[fBold].fs, bold);
879
940
            if (fp != 0) {
880
941
                myfonts.f_wb = widebold_font_name(fp);
 
942
                warn[fWBold] = fwAlways;
881
943
                derived = True;
882
944
            }
883
945
        }
884
946
 
885
 
        if (myfonts.f_wb) {
 
947
        if (check_fontname(myfonts.f_wb)) {
886
948
 
887
 
            (void) xtermOpenFont(xw, myfonts.f_wb, &fnts[fWBold]);
 
949
            xtermOpenFont(xw, myfonts.f_wb, &fnts[fWBold], warn[fWBold], False);
888
950
 
889
951
            if (derived
890
952
                && !compatibleWideCounts(fnts[fWide].fs, fnts[fWBold].fs)) {
892
954
            }
893
955
            if (fnts[fWBold].fs == 0) {
894
956
                myfonts.f_wb = myfonts.f_w;
 
957
                warn[fWBold] = fwAlways;
895
958
                xtermCopyFontInfo(&fnts[fWBold], &fnts[fWide]);
896
959
                TRACE(("...cannot load wide-bold, use wide %s\n", NonNull(myfonts.f_w)));
897
960
            } else {
902
965
            }
903
966
        } else if (is_double_width_font(fnts[fBold].fs)) {
904
967
            xtermCopyFontInfo(&fnts[fWBold], &fnts[fBold]);
 
968
            warn[fWBold] = fwAlways;
905
969
            TRACE(("...bold font is double-width, use it %s\n", NonNull(myfonts.f_b)));
906
970
        } else {
907
971
            xtermCopyFontInfo(&fnts[fWBold], &fnts[fWide]);
 
972
            warn[fWBold] = fwAlways;
908
973
            TRACE(("...cannot load wide bold font, use wide %s\n", NonNull(myfonts.f_w)));
909
974
        }
910
975
 
1371
1436
 
1372
1437
#if OPT_RENDERFONT
1373
1438
static XftFont *
1374
 
xtermOpenXft(Display * dpy, XftPattern * pat, const char *tag GCC_UNUSED)
 
1439
xtermOpenXft(XtermWidget xw, const char *name, XftPattern * pat, const char *tag)
1375
1440
{
 
1441
    TScreen *screen = TScreenOf(xw);
 
1442
    Display *dpy = screen->display;
1376
1443
    XftPattern *match;
1377
1444
    XftResult status;
1378
1445
    XftFont *result = 0;
1386
1453
            } else {
1387
1454
                TRACE(("...could did not open %s font\n", tag));
1388
1455
                XftPatternDestroy(match);
 
1456
                if (xw->misc.fontWarnings >= fwAlways) {
 
1457
                    TRACE(("OOPS cannot open %s font \"%s\"\n", tag, name));
 
1458
                    fprintf(stderr, "%s: cannot open %s font \"%s\"\n",
 
1459
                            ProgramName, tag, name);
 
1460
                }
1389
1461
            }
1390
1462
        } else {
1391
1463
            TRACE(("...did not match %s font\n", tag));
 
1464
            if (xw->misc.fontWarnings >= fwResource) {
 
1465
                TRACE(("OOPS: cannot match %s font \"%s\"\n", tag, name));
 
1466
                fprintf(stderr, "%s: cannot match %s font \"%s\"\n",
 
1467
                        ProgramName, tag, name);
 
1468
            }
1392
1469
        }
1393
1470
    }
1394
1471
    return result;
1495
1572
     * overrides it.
1496
1573
     */
1497
1574
    if (xw->misc.render_font && !IsIconWin(screen, win)) {
1498
 
        Display *dpy = screen->display;
1499
1575
        int fontnum = screen->menu_font_number;
1500
1576
        XftFont *norm = screen->renderFontNorm[fontnum];
1501
1577
        XftFont *bold = screen->renderFontBold[fontnum];
1588
1664
            XFT_CHAR_WIDTH, XftTypeInteger, norm->max_advance_width
1589
1665
 
1590
1666
            if ((pat = XftNameParse(xw->misc.face_name)) != 0) {
 
1667
#define OPEN_XFT(tag) xtermOpenXft(xw, xw->misc.face_name, pat, tag)
1591
1668
                XftPatternBuild(pat,
1592
1669
                                NormXftPattern,
1593
1670
                                (void *) 0);
1594
 
                norm = xtermOpenXft(dpy, pat, "normal");
 
1671
                norm = OPEN_XFT("normal");
1595
1672
 
1596
1673
                if (norm != 0) {
1597
1674
                    XftPatternBuild(pat,
1598
1675
                                    BoldXftPattern(norm),
1599
1676
                                    (void *) 0);
1600
 
                    bold = xtermOpenXft(dpy, pat, "bold");
 
1677
                    bold = OPEN_XFT("bold");
1601
1678
 
1602
1679
#if OPT_ISO_COLORS
1603
1680
                    if (screen->italicULMode
1606
1683
                                        NormXftPattern,
1607
1684
                                        ItalXftPattern(norm),
1608
1685
                                        (void *) 0);
1609
 
                        ital = xtermOpenXft(dpy, pat, "italic");
 
1686
                        ital = OPEN_XFT("italic");
1610
1687
                    }
1611
1688
#endif /* OPT_ISO_COLORS */
 
1689
#undef OPEN_XFT
1612
1690
 
1613
1691
                    /*
1614
1692
                     * FIXME:  just assume that the corresponding font has no
1648
1726
                XFT_SPACING, XftTypeInteger, XFT_MONO
1649
1727
 
1650
1728
                if ((pat = XftNameParse(face_name)) != 0) {
 
1729
#define OPEN_XFT(tag) xtermOpenXft(xw, face_name, pat, tag)
1651
1730
                    XftPatternBuild(pat,
1652
1731
                                    WideXftPattern,
1653
1732
                                    XFT_CHAR_WIDTH, XftTypeInteger, char_width,
1654
1733
                                    (void *) 0);
1655
 
                    wnorm = xtermOpenXft(dpy, pat, "wide");
 
1734
                    wnorm = OPEN_XFT("wide");
1656
1735
 
1657
1736
                    if (wnorm != 0) {
1658
1737
                        XftPatternBuild(pat,
1659
1738
                                        WideXftPattern,
1660
1739
                                        BoldXftPattern(wnorm),
1661
1740
                                        (void *) 0);
1662
 
                        wbold = xtermOpenXft(dpy, pat, "wide-bold");
 
1741
                        wbold = OPEN_XFT("wide-bold");
1663
1742
 
1664
1743
#if OPT_ISO_COLORS
1665
1744
                        if (screen->italicULMode
1668
1747
                                            WideXftPattern,
1669
1748
                                            ItalXftPattern(wnorm),
1670
1749
                                            (void *) 0);
1671
 
                            wital = xtermOpenXft(dpy, pat, "wide-italic");
 
1750
                            wital = OPEN_XFT("wide-italic");
1672
1751
                        }
1673
1752
#endif
 
1753
#undef OPEN_XFT
1674
1754
                    }
1675
1755
                    XftPatternDestroy(pat);
1676
1756
                }
2321
2401
 
2322
2402
        memset(&fnt, 0, sizeof(fnt));
2323
2403
        screen->menu_font_sizes[fontnum] = -1;
2324
 
        if (xtermOpenFont(xw, screen->MenuFontName(fontnum), &fnt)) {
 
2404
        if (xtermOpenFont(xw, screen->MenuFontName(fontnum), &fnt, fwAlways, True)) {
2325
2405
            screen->menu_font_sizes[fontnum] = FontSize(fnt.fs);
2326
2406
            TRACE(("menu_font_sizes[%d] = %ld\n", fontnum,
2327
2407
                   screen->menu_font_sizes[fontnum]));