summaryrefslogtreecommitdiff
path: root/vcl/source/glyphs/graphite_adaptors.cxx
blob: 9b16318fdc40369ca58ccb4acbb8349d5f8d0d3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile:  $
 * $Revision:  $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

// Description: Implements the Graphite interfaces with access to the
//              platform's font and graphics systems.

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_vcl.hxx"

// We need this to enable namespace support in libgrengine headers.
#define GR_NAMESPACE

// Header files
//
// Standard Library
#include <string>
#include <cassert>
// Libraries
#include <rtl/string.hxx>
#include <rtl/ustring.hxx>
#include <i18npool/mslangid.hxx>
// Platform
#ifndef WNT
#include <saldisp.hxx>

#include <vcl/salgdi.hxx>

#include <freetype/ftsynth.h>

// Module
#include "gcach_ftyp.hxx"

#include <vcl/graphite_features.hxx>
#include <vcl/graphite_adaptors.hxx>

// Module private type definitions and forward declarations.
//
using gr::GrResult;
namespace
{
    inline float from_hinted(const int x) {
        return static_cast<float>(x + 32) / 64.0;
    }
    typedef std::hash_map<long,bool> SilfMap;
    SilfMap sSilfMap;
}

// class CharacterRenderProperties implentation.
//
FontProperties::FontProperties(const FreetypeServerFont &font) throw()
{
    clrFore = gr::kclrBlack;
    clrBack = gr::kclrTransparent;

    pixHeight = from_hinted(font.GetMetricsFT().height);

    switch (font.GetFontSelData().meWeight)
    {
        case WEIGHT_SEMIBOLD: case WEIGHT_BOLD:
        case WEIGHT_ULTRABOLD: case WEIGHT_BLACK:
            fBold = true;
            break;
        default :
            fBold = false;
    }

    switch (font.GetFontSelData().meItalic)
    {
        case ITALIC_NORMAL: case ITALIC_OBLIQUE:
            fItalic = true;
            break;
        default :
            fItalic = false;
    }

    // Get the font name.
    const sal_Unicode    * name = font.GetFontSelData().maName.GetBuffer();
    const size_t          name_sz = std::min(sizeof szFaceName/sizeof(wchar_t)-1,
                    size_t(font.GetFontSelData().maName.Len()));

    std::copy(name, name + name_sz, szFaceName);
    szFaceName[name_sz] = '\0';
}

// class GraphiteFontAdaptor implementaion.
//
GraphiteFontAdaptor::GraphiteFontAdaptor(ServerFont & sfont, const sal_Int32 dpiX, const sal_Int32 dpiY)
  :    mrFont(static_cast<FreetypeServerFont &>(sfont)),
    maFontProperties(static_cast<FreetypeServerFont &>(sfont)),
    mnDpiX(dpiX),
    mnDpiY(dpiY),
    mfAscent(from_hinted(static_cast<FreetypeServerFont &>(sfont).GetMetricsFT().ascender)),
    mfDescent(from_hinted(static_cast<FreetypeServerFont &>(sfont).GetMetricsFT().descender)),
    mfEmUnits(static_cast<FreetypeServerFont &>(sfont).GetMetricsFT().y_ppem),
    mpFeatures(NULL)
{
    //std::wstring face_name(maFontProperties.szFaceName);
    const rtl::OString aLang = MsLangId::convertLanguageToIsoByteString( sfont.GetFontSelData().meLanguage );
#ifdef DEBUG
    printf("GraphiteFontAdaptor %lx\n", (long)this);
#endif
    rtl::OString name = rtl::OUStringToOString(
        sfont.GetFontSelData().maTargetName, RTL_TEXTENCODING_UTF8 );
    sal_Int32 nFeat = name.indexOf(grutils::GrFeatureParser::FEAT_PREFIX) + 1;
    if (nFeat > 0)
    {
        rtl::OString aFeat = name.copy(nFeat, name.getLength() - nFeat);
        mpFeatures = new grutils::GrFeatureParser(*this, aFeat.getStr(), aLang.getStr());
#ifdef DEBUG
        printf("GraphiteFontAdaptor %s/%s/%s %x language %d features %d errors\n",
            rtl::OUStringToOString( sfont.GetFontSelData().maName,
            RTL_TEXTENCODING_UTF8 ).getStr(),
            rtl::OUStringToOString( sfont.GetFontSelData().maTargetName,
            RTL_TEXTENCODING_UTF8 ).getStr(),
            rtl::OUStringToOString( sfont.GetFontSelData().maSearchName,
            RTL_TEXTENCODING_UTF8 ).getStr(),
            sfont.GetFontSelData().meLanguage,
            (int)mpFeatures->getFontFeatures(NULL), mpFeatures->parseErrors());
#endif
    }
    else
    {
        mpFeatures = new grutils::GrFeatureParser(*this, aLang.getStr());
    }
}

GraphiteFontAdaptor::GraphiteFontAdaptor(const GraphiteFontAdaptor &rhs) throw()
 :    Font(rhs),
     mrFont (rhs.mrFont), maFontProperties(rhs.maFontProperties),
    mnDpiX(rhs.mnDpiX), mnDpiY(rhs.mnDpiY),
    mfAscent(rhs.mfAscent), mfDescent(rhs.mfDescent), mfEmUnits(rhs.mfEmUnits),
    mpFeatures(NULL)
{
    if (rhs.mpFeatures) mpFeatures = new grutils::GrFeatureParser(*(rhs.mpFeatures));
}


GraphiteFontAdaptor::~GraphiteFontAdaptor() throw()
{
    maGlyphMetricMap.clear();
    if (mpFeatures) delete mpFeatures;
    mpFeatures = NULL;
}

void GraphiteFontAdaptor::UniqueCacheInfo(std::wstring & face_name_out, bool & bold_out, bool & italic_out)
{
    face_name_out = maFontProperties.szFaceName;
    bold_out = maFontProperties.fBold;
    italic_out = maFontProperties.fItalic;
}

bool GraphiteFontAdaptor::IsGraphiteEnabledFont(ServerFont & font) throw()
{
    // NOTE: this assumes that the same FTFace pointer won't be reused,
    // so FtFontInfo::ReleaseFaceFT must only be called at shutdown.
    FreetypeServerFont & aFtFont = dynamic_cast<FreetypeServerFont &>(font);
    FT_Face aFace = reinterpret_cast<FT_FaceRec_*>(aFtFont.GetFtFace());
    SilfMap::iterator i = sSilfMap.find(reinterpret_cast<long>(aFace));
    if (i != sSilfMap.end())
    {
#ifdef DEBUG
        if (static_cast<bool>(aFtFont.GetTable("Silf", 0)) != (*i).second)
            printf("Silf cache font mismatch\n");
#endif
        return (*i).second;
    }
    bool bHasSilf = aFtFont.GetTable("Silf", 0);
    sSilfMap[reinterpret_cast<long>(aFace)] = bHasSilf;
    return bHasSilf;
}


gr::Font * GraphiteFontAdaptor::copyThis() {
    return new GraphiteFontAdaptor(*this);
}


unsigned int GraphiteFontAdaptor::getDPIx() {
    return mnDpiX;
}


unsigned int GraphiteFontAdaptor::getDPIy() {
    return mnDpiY;
}


float GraphiteFontAdaptor::ascent() {
    return mfAscent;
}


float GraphiteFontAdaptor::descent() {
    return mfDescent;
}


bool GraphiteFontAdaptor::bold() {
    return maFontProperties.fBold;
}


bool GraphiteFontAdaptor::italic() {
    return maFontProperties.fItalic;
}


float GraphiteFontAdaptor::height() {
    return maFontProperties.pixHeight;
}


void GraphiteFontAdaptor::getFontMetrics(float * ascent_out, float * descent_out, float * em_square_out) {
    if (ascent_out)        *ascent_out    = mfAscent;
    if (descent_out)    *descent_out   = mfDescent;
    if (em_square_out)    *em_square_out = mfEmUnits;
}


const void * GraphiteFontAdaptor::getTable(gr::fontTableId32 table_id, size_t * buffer_sz)
{
    char tag_name[5] = {char(table_id >> 24), char(table_id >> 16), char(table_id >> 8), char(table_id), 0};
    ULONG temp = *buffer_sz;

    const void * const tbl_buf = static_cast<FreetypeServerFont &>(mrFont).GetTable(tag_name, &temp);
    *buffer_sz = temp;

    return tbl_buf;
}

#define fix26_6(x) (x >> 6) + (x & 32 ? (x > 0 ? 1 : 0) : (x < 0 ? -1 : 0))

// Return the glyph's metrics in pixels.
void GraphiteFontAdaptor::getGlyphMetrics(gr::gid16 nGlyphId, gr::Rect & aBounding, gr::Point & advances)
{
    // Graphite gets really confused if the glyphs have been transformed, so
    // if orientation has been set we can't use the font's glyph cache
    // unfortunately the font selection data, doesn't always have the orientation
    // set, even if it was when the glyphs were cached, so we use our own cache.

//         const GlyphMetric & metric = mrFont.GetGlyphMetric(nGlyphId);
//
//         aBounding.right  = aBounding.left = metric.GetOffset().X();
//         aBounding.bottom = aBounding.top  = -metric.GetOffset().Y();
//         aBounding.right  += metric.GetSize().Width();
//         aBounding.bottom -= metric.GetSize().Height();
//
//         advances.x = metric.GetDelta().X();
//         advances.y = -metric.GetDelta().Y();

    GlyphMetricMap::const_iterator gm_itr = maGlyphMetricMap.find(nGlyphId);
    if (gm_itr != maGlyphMetricMap.end())
    {
        // We've cached the results from last time.
        aBounding = gm_itr->second.first;
        advances    = gm_itr->second.second;
    }
    else
    {
        // We need to look up the glyph.
        FT_Int nLoadFlags = mrFont.GetLoadFlags();

        FT_Face aFace = reinterpret_cast<FT_Face>(mrFont.GetFtFace());
        if (!aFace)
        {
            aBounding.top = aBounding.bottom = aBounding.left = aBounding.right = 0;
            advances.x = advances.y = 0;
            return;
        }
        FT_Error aStatus = -1;
        aStatus = FT_Load_Glyph(aFace, nGlyphId, nLoadFlags);
        if( aStatus != FT_Err_Ok || (!aFace->glyph))
        {
            aBounding.top = aBounding.bottom = aBounding.left = aBounding.right = 0;
            advances.x = advances.y = 0;
            return;
        }
        // check whether we need synthetic bold/italic otherwise metric is wrong
        if (mrFont.NeedsArtificialBold())
            FT_GlyphSlot_Embolden(aFace->glyph);

        if (mrFont.NeedsArtificialItalic())
            FT_GlyphSlot_Oblique(aFace->glyph);

        const FT_Glyph_Metrics &gm = aFace->glyph->metrics;

        // Fill out the bounding box an advances.
        aBounding.top = aBounding.bottom = fix26_6(gm.horiBearingY);
        aBounding.bottom -= fix26_6(gm.height);
        aBounding.left = aBounding.right = fix26_6(gm.horiBearingX);
        aBounding.right += fix26_6(gm.width);
        advances.x = fix26_6(gm.horiAdvance);
        advances.y = 0;

        // Now add an entry to our metrics map.
        maGlyphMetricMap[nGlyphId] = std::make_pair(aBounding, advances);
    }
}

#endif