src/java.desktop/share/native/libfontmanager/freetypeScaler.c
author mbaesken
Fri, 18 Oct 2019 14:56:01 +0200
changeset 58685 4b13a908c2d0
parent 58634 da71aa13f86b
child 59176 f5adbf111424
permissions -rw-r--r--
8232381: add result NULL-checking to freetypeScaler.c Reviewed-by: clanger, prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54239
0804f29e8be7 8218854: FontMetrics.getMaxAdvance may be less than the maximum FontMetrics.charWidth
mbalao
parents: 53454
diff changeset
     2
 * Copyright (c) 2007, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4243
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4243
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4243
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4243
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4243
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include "jlong.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "sunfontids.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "sun_font_FreetypeFontScaler.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
55369
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
    32
#include <stdlib.h>
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
    33
#if !defined(_WIN32) && !defined(__APPLE_)
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
    34
#include <dlfcn.h>
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
    35
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include <math.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include "ft2build.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include FT_FREETYPE_H
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#include FT_GLYPH_H
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#include FT_BBOX_H
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#include FT_SIZES_H
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#include FT_OUTLINE_H
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#include FT_SYNTHESIS_H
52258
02e4b0ab0f97 8212071: Need to set the FreeType LCD Filter to reduce fringing.
prr
parents: 51932
diff changeset
    44
#include FT_LCD_FILTER_H
55369
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
    45
#include FT_MODULE_H
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
#include "fontscaler.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
#define  ftFixed1  (FT_Fixed) (1 << 16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
#define  FloatToFTFixed(f) (FT_Fixed)((f) * (float)(ftFixed1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
#define  FTFixedToFloat(x) ((x) / (float)(ftFixed1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
#define  FT26Dot6ToFloat(x)  ((x) / ((float) (1<<6)))
50346
9e530b150333 8203485: [freetype] text rotated on 180 degrees is too narrow
bae
parents: 47216
diff changeset
    53
#define  FT26Dot6ToInt(x) (((int)(x)) >> 6)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
typedef struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /* Important note:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
         JNI forbids sharing same env between different threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
         We are safe, because pointer is overwritten every time we get into
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
         JNI call (see setupFTContext).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
         Pointer is used by font data reading callbacks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
         such as ReadTTFontFileFunc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
         NB: We may consider switching to JNI_GetEnv. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    JNIEnv* env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    FT_Library library;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    FT_Face face;
33250
047311011afe 8132985: Crash in freetypescaler.c due to double free
psadhukhan
parents: 30494
diff changeset
    68
    FT_Stream faceStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    jobject font2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    jobject directBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    unsigned char* fontData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    unsigned fontDataOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    unsigned fontDataLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    unsigned fileSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
} FTScalerInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
typedef struct FTScalerContext {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    FT_Matrix  transform;     /* glyph transform, including device transform */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    jboolean   useSbits;      /* sbit usage enabled? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    jint       aaType;        /* antialiasing mode (off/on/grey/lcd) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    jint       fmType;        /* fractional metrics - on/off */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    jboolean   doBold;        /* perform algorithmic bolding? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    jboolean   doItalize;     /* perform algorithmic italicizing? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    int        renderFlags;   /* configuration specific to particular engine */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    int        pathType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    int        ptsz;          /* size in points */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
} FTScalerContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
/* These are referenced in the freetype sources if DEBUG macro is defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
   To simplify work with debuging version of freetype we define
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
   them here. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
int z_verbose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
void z_error(char *s) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
/**************** Error handling utilities *****************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
static jmethodID invalidateScalerMID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
Java_sun_font_FreetypeFontScaler_initIDs(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        JNIEnv *env, jobject scaler, jclass FFSClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    invalidateScalerMID =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        (*env)->GetMethodID(env, FFSClass, "invalidateScaler", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
static void freeNativeResources(JNIEnv *env, FTScalerInfo* scalerInfo) {
4243
657b5136de1a 6887292: memory leak in freetypeScaler.c
igor
parents: 2369
diff changeset
   110
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    if (scalerInfo == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
33250
047311011afe 8132985: Crash in freetypescaler.c due to double free
psadhukhan
parents: 30494
diff changeset
   114
    // FT_Done_Face always closes the stream, but only frees the memory
047311011afe 8132985: Crash in freetypescaler.c due to double free
psadhukhan
parents: 30494
diff changeset
   115
    // of the data structure if it was internally allocated by FT.
047311011afe 8132985: Crash in freetypescaler.c due to double free
psadhukhan
parents: 30494
diff changeset
   116
    // We hold on to a pointer to the stream structure if we provide it
047311011afe 8132985: Crash in freetypescaler.c due to double free
psadhukhan
parents: 30494
diff changeset
   117
    // ourselves, so that we can free it here.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    FT_Done_Face(scalerInfo->face);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    FT_Done_FreeType(scalerInfo->library);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    if (scalerInfo->directBuffer != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        (*env)->DeleteGlobalRef(env, scalerInfo->directBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    if (scalerInfo->fontData != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        free(scalerInfo->fontData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
33250
047311011afe 8132985: Crash in freetypescaler.c due to double free
psadhukhan
parents: 30494
diff changeset
   129
    if (scalerInfo->faceStream != NULL) {
047311011afe 8132985: Crash in freetypescaler.c due to double free
psadhukhan
parents: 30494
diff changeset
   130
        free(scalerInfo->faceStream);
047311011afe 8132985: Crash in freetypescaler.c due to double free
psadhukhan
parents: 30494
diff changeset
   131
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    free(scalerInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
/* invalidates state of java scaler object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
static void invalidateJavaScaler(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                                 jobject scaler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                                 FTScalerInfo* scalerInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    freeNativeResources(env, scalerInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    (*env)->CallVoidMethod(env, scaler, invalidateScalerMID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
/******************* I/O handlers ***************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
#define FILEDATACACHESIZE 1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
static unsigned long ReadTTFontFileFunc(FT_Stream stream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                                        unsigned long offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                                        unsigned char* destBuffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                                        unsigned long numBytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    FTScalerInfo *scalerInfo = (FTScalerInfo *) stream->pathname.pointer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    JNIEnv* env = scalerInfo->env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    jobject bBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    int bread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
58613
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   157
    /* A call with numBytes == 0 is a seek. It should return 0 if the
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   158
     * seek position is within the file and non-zero otherwise.
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   159
     * For all other cases, ie numBytes !=0, return the number of bytes
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   160
     * actually read. This applies to truncated reads and also failed reads.
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   161
     */
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   162
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   163
    if (numBytes == 0) {
58634
da71aa13f86b 8227662: freetype seeks to index at the end of the font data
prr
parents: 58629
diff changeset
   164
        if (offset > scalerInfo->fileSize) {
58613
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   165
            return -1;
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   166
        } else {
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   167
            return 0;
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   168
       }
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   169
    }
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   170
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   171
    if (offset + numBytes < offset) {
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   172
        return 0; // ft should not do this, but just in case.
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   173
    }
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   174
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   175
    if (offset >= scalerInfo->fileSize) {
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   176
        return 0;
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   177
    }
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   178
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   179
    if (offset + numBytes > scalerInfo->fileSize) {
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   180
        numBytes = scalerInfo->fileSize - offset;
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   181
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /* Large reads will bypass the cache and data copying */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    if (numBytes > FILEDATACACHESIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        bBuffer = (*env)->NewDirectByteBuffer(env, destBuffer, numBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (bBuffer != NULL) {
25528
5c0c16556c1b 8034267: Probabilistic native crash`
prr
parents: 23010
diff changeset
   187
            bread = (*env)->CallIntMethod(env,
5c0c16556c1b 8034267: Probabilistic native crash`
prr
parents: 23010
diff changeset
   188
                                          scalerInfo->font2D,
5c0c16556c1b 8034267: Probabilistic native crash`
prr
parents: 23010
diff changeset
   189
                                          sunFontIDs.ttReadBlockMID,
5c0c16556c1b 8034267: Probabilistic native crash`
prr
parents: 23010
diff changeset
   190
                                          bBuffer, offset, numBytes);
58613
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   191
            if (bread < 0) {
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   192
                return 0;
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   193
            } else {
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   194
               return bread;
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   195
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        } else {
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 27053
diff changeset
   197
            /* We probably hit bug 4845371. For reasons that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
             * are currently unclear, the call stacks after the initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
             * createScaler call that read large amounts of data seem to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
             * be OK and can create the byte buffer above, but this code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
             * is here just in case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
             * 4845371 is fixed now so I don't expect this code path to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
             * ever get called but its harmless to leave it here on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
             * small chance its needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            jbyteArray byteArray = (jbyteArray)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            (*env)->CallObjectMethod(env, scalerInfo->font2D,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                                     sunFontIDs.ttReadBytesMID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                                     offset, numBytes);
58613
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   210
            /* If there's an OutofMemoryError then byteArray will be null */
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   211
            if (byteArray == NULL) {
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   212
                return 0;
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   213
            } else {
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   214
                jsize len = (*env)->GetArrayLength(env, byteArray);
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   215
                if (len < numBytes) {
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   216
                    numBytes = len; // don't get more bytes than there are ..
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   217
                }
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   218
                (*env)->GetByteArrayRegion(env, byteArray,
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   219
                                           0, numBytes, (jbyte*)destBuffer);
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   220
                return numBytes;
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   221
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    } /* Do we have a cache hit? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
      else if (scalerInfo->fontDataOffset <= offset &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        scalerInfo->fontDataOffset + scalerInfo->fontDataLength >=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                                                         offset + numBytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        unsigned cacheOffset = offset - scalerInfo->fontDataOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        memcpy(destBuffer, scalerInfo->fontData+(size_t)cacheOffset, numBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        return numBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        /* Must fill the cache */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        scalerInfo->fontDataOffset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        scalerInfo->fontDataLength =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                 (offset + FILEDATACACHESIZE > scalerInfo->fileSize) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                 scalerInfo->fileSize - offset : FILEDATACACHESIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        bBuffer = scalerInfo->directBuffer;
25528
5c0c16556c1b 8034267: Probabilistic native crash`
prr
parents: 23010
diff changeset
   239
        bread = (*env)->CallIntMethod(env, scalerInfo->font2D,
5c0c16556c1b 8034267: Probabilistic native crash`
prr
parents: 23010
diff changeset
   240
                                      sunFontIDs.ttReadBlockMID,
5c0c16556c1b 8034267: Probabilistic native crash`
prr
parents: 23010
diff changeset
   241
                                      bBuffer, offset,
5c0c16556c1b 8034267: Probabilistic native crash`
prr
parents: 23010
diff changeset
   242
                                      scalerInfo->fontDataLength);
58613
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   243
        if (bread <= 0) {
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   244
            return 0;
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   245
        } else if (bread < numBytes) {
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   246
           numBytes = bread;
eb09ad30eccb 8220186: Improve use of font temporary files
prr
parents: 55369
diff changeset
   247
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        memcpy(destBuffer, scalerInfo->fontData, numBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        return numBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
55369
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   253
typedef FT_Error (*FT_Prop_Set_Func)(FT_Library library,
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   254
                                     const FT_String*  module_name,
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   255
                                     const FT_String*  property_name,
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   256
                                     const void*       value );
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   257
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   258
/**
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   259
 * Prefer the older v35 freetype byte code interpreter.
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   260
 */
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   261
static void setInterpreterVersion(FT_Library library) {
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   262
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   263
    char* props = getenv("FREETYPE_PROPERTIES");
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   264
    int version = 35;
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   265
    const char* module = "truetype";
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   266
    const char* property = "interpreter-version";
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   267
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   268
    /* If some one is setting this, don't override it */
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   269
    if (props != NULL && strstr(property, props)) {
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   270
        return;
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   271
    }
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   272
    /*
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   273
     * FT_Property_Set was introduced in 2.4.11.
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   274
     * Some older supported Linux OSes may not include it so look
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   275
     * this up dynamically.
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   276
     * And if its not available it doesn't matter, since the reason
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   277
     * we need it dates from 2.7.
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   278
     * On Windows & Mac the library is always bundled so it is safe
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   279
     * to use directly in those cases.
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   280
     */
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   281
#if defined(_WIN32) || defined(__APPLE__)
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   282
    FT_Property_Set(library, module, property, (void*)(&version));
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   283
#else
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   284
    void *lib = dlopen("libfreetype.so", RTLD_LOCAL|RTLD_LAZY);
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   285
    if (lib == NULL) {
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   286
        lib = dlopen("libfreetype.so.6", RTLD_LOCAL|RTLD_LAZY);
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   287
        if (lib == NULL) {
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   288
            return;
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   289
        }
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   290
    }
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   291
    FT_Prop_Set_Func func = (FT_Prop_Set_Func)dlsym(lib, "FT_Property_Set");
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   292
    if (func != NULL) {
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   293
        func(library, module, property, (void*)(&version));
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   294
    }
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   295
    dlclose(lib);
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   296
#endif
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   297
}
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   298
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
 * Class:     sun_font_FreetypeFontScaler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
 * Method:    initNativeScaler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
 * Signature: (Lsun/font/Font2D;IIZI)J
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
Java_sun_font_FreetypeFontScaler_initNativeScaler(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        JNIEnv *env, jobject scaler, jobject font2D, jint type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        jint indexInCollection, jboolean supportsCJK, jint filesize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    FTScalerInfo* scalerInfo = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    FT_Open_Args ft_open_args;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    int error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    jobject bBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    scalerInfo = (FTScalerInfo*) calloc(1, sizeof(FTScalerInfo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    if (scalerInfo == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    scalerInfo->env = env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    scalerInfo->font2D = font2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    scalerInfo->fontDataOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    scalerInfo->fontDataLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    scalerInfo->fileSize = filesize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
       We can consider sharing freetype library between different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
       scalers. However, Freetype docs suggest to use different libraries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
       for different threads. Also, our architecture implies that single
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 27053
diff changeset
   327
       FontScaler object is shared for different sizes/transforms/styles
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
       of the same font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
       On other hand these methods can not be concurrently executed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
       becaused they are "synchronized" in java.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    error = FT_Init_FreeType(&scalerInfo->library);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        free(scalerInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
55369
18629738b64b 8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents: 54590
diff changeset
   338
    setInterpreterVersion(scalerInfo->library);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
#define TYPE1_FROM_JAVA        2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    error = 1; /* triggers memory freeing unless we clear it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    if (type == TYPE1_FROM_JAVA) { /* TYPE1 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        scalerInfo->fontData = (unsigned char*) malloc(filesize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        scalerInfo->directBuffer = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        scalerInfo->fontDataLength = filesize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if (scalerInfo->fontData != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            bBuffer = (*env)->NewDirectByteBuffer(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                                              scalerInfo->fontData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                                              scalerInfo->fontDataLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            if (bBuffer != NULL) {
34786
36c3d93671f8 7063986: Wrong JNi method call in font scaler
psadhukhan
parents: 33851
diff changeset
   353
                (*env)->CallVoidMethod(env, font2D,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                                   sunFontIDs.readFileMID, bBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                error = FT_New_Memory_Face(scalerInfo->library,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                                   scalerInfo->fontData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                                   scalerInfo->fontDataLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                                   indexInCollection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                                   &scalerInfo->face);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    } else { /* Truetype */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        scalerInfo->fontData = (unsigned char*) malloc(FILEDATACACHESIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
20424
eace1945b974 8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents: 20414
diff changeset
   366
        if (scalerInfo->fontData != NULL) {
eace1945b974 8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents: 20414
diff changeset
   367
            FT_Stream ftstream = (FT_Stream) calloc(1, sizeof(FT_StreamRec));
eace1945b974 8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents: 20414
diff changeset
   368
            if (ftstream != NULL) {
eace1945b974 8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents: 20414
diff changeset
   369
                scalerInfo->directBuffer = (*env)->NewDirectByteBuffer(env,
eace1945b974 8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents: 20414
diff changeset
   370
                                           scalerInfo->fontData,
eace1945b974 8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents: 20414
diff changeset
   371
                                           FILEDATACACHESIZE);
eace1945b974 8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents: 20414
diff changeset
   372
                if (scalerInfo->directBuffer != NULL) {
eace1945b974 8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents: 20414
diff changeset
   373
                    scalerInfo->directBuffer = (*env)->NewGlobalRef(env,
eace1945b974 8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents: 20414
diff changeset
   374
                                               scalerInfo->directBuffer);
eace1945b974 8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents: 20414
diff changeset
   375
                    ftstream->base = NULL;
eace1945b974 8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents: 20414
diff changeset
   376
                    ftstream->size = filesize;
eace1945b974 8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents: 20414
diff changeset
   377
                    ftstream->pos = 0;
eace1945b974 8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents: 20414
diff changeset
   378
                    ftstream->read = (FT_Stream_IoFunc) ReadTTFontFileFunc;
30494
a245f4430b4c 8078654: CloseTTFontFileFunc callback should be removed
adinn
parents: 28059
diff changeset
   379
                    ftstream->close = NULL;
20424
eace1945b974 8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents: 20414
diff changeset
   380
                    ftstream->pathname.pointer = (void *) scalerInfo;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
20424
eace1945b974 8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents: 20414
diff changeset
   382
                    memset(&ft_open_args, 0, sizeof(FT_Open_Args));
eace1945b974 8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents: 20414
diff changeset
   383
                    ft_open_args.flags = FT_OPEN_STREAM;
eace1945b974 8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents: 20414
diff changeset
   384
                    ft_open_args.stream = ftstream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
20424
eace1945b974 8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents: 20414
diff changeset
   386
                    error = FT_Open_Face(scalerInfo->library,
eace1945b974 8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents: 20414
diff changeset
   387
                                         &ft_open_args,
eace1945b974 8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents: 20414
diff changeset
   388
                                         indexInCollection,
eace1945b974 8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents: 20414
diff changeset
   389
                                         &scalerInfo->face);
33250
047311011afe 8132985: Crash in freetypescaler.c due to double free
psadhukhan
parents: 30494
diff changeset
   390
                    if (!error) {
047311011afe 8132985: Crash in freetypescaler.c due to double free
psadhukhan
parents: 30494
diff changeset
   391
                        scalerInfo->faceStream = ftstream;
047311011afe 8132985: Crash in freetypescaler.c due to double free
psadhukhan
parents: 30494
diff changeset
   392
                    }
20424
eace1945b974 8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents: 20414
diff changeset
   393
                }
eace1945b974 8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents: 20414
diff changeset
   394
                if (error || scalerInfo->directBuffer == NULL) {
eace1945b974 8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents: 20414
diff changeset
   395
                    free(ftstream);
eace1945b974 8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents: 20414
diff changeset
   396
                }
eace1945b974 8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents: 20414
diff changeset
   397
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        FT_Done_FreeType(scalerInfo->library);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if (scalerInfo->directBuffer != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            (*env)->DeleteGlobalRef(env, scalerInfo->directBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        if (scalerInfo->fontData != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            free(scalerInfo->fontData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        free(scalerInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    return ptr_to_jlong(scalerInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
static double euclidianDistance(double a, double b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    if (a < 0) a=-a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    if (b < 0) b=-b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    if (a == 0) return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    if (b == 0) return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    return sqrt(a*a+b*b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
Java_sun_font_FreetypeFontScaler_createScalerContextNative(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        JNIEnv *env, jobject scaler, jlong pScaler, jdoubleArray matrix,
9761
a9315b1a26e9 7049874: OpenJDK Build breakage fix: freetypescaler.c needs to match updated signature
prr
parents: 7668
diff changeset
   428
        jint aa, jint fm, jfloat boldness, jfloat italic) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    double dmat[4], ptsz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    FTScalerContext *context =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            (FTScalerContext*) calloc(1, sizeof(FTScalerContext));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    FTScalerInfo *scalerInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
             (FTScalerInfo*) jlong_to_ptr(pScaler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    if (context == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        invalidateJavaScaler(env, scaler, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        return (jlong) 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    (*env)->GetDoubleArrayRegion(env, matrix, 0, 4, dmat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    ptsz = euclidianDistance(dmat[2], dmat[3]); //i.e. y-size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    if (ptsz < 1.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        //text can not be smaller than 1 point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        ptsz = 1.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    }
554
88eb2812c2a4 6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents: 2
diff changeset
   445
    context->ptsz = (int)(ptsz * 64);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    context->transform.xx =  FloatToFTFixed((float)dmat[0]/ptsz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    context->transform.yx = -FloatToFTFixed((float)dmat[1]/ptsz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    context->transform.xy = -FloatToFTFixed((float)dmat[2]/ptsz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    context->transform.yy =  FloatToFTFixed((float)dmat[3]/ptsz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    context->aaType = aa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    context->fmType = fm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    /* If using algorithmic styling, the base values are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * boldness = 1.0, italic = 0.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    context->doBold = (boldness != 1.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    context->doItalize = (italic != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
50658
db4f24a6cf34 8204929: Fonts with embedded bitmaps are not always rotated
prr
parents: 50346
diff changeset
   459
    /* freetype is very keen to use embedded bitmaps, even if it knows
db4f24a6cf34 8204929: Fonts with embedded bitmaps are not always rotated
prr
parents: 50346
diff changeset
   460
     * there is a rotation or you asked for antialiasing.
db4f24a6cf34 8204929: Fonts with embedded bitmaps are not always rotated
prr
parents: 50346
diff changeset
   461
     * In the rendering path we will check useSBits and disable
db4f24a6cf34 8204929: Fonts with embedded bitmaps are not always rotated
prr
parents: 50346
diff changeset
   462
     * bitmaps unless it is set. And here we set it only if none
db4f24a6cf34 8204929: Fonts with embedded bitmaps are not always rotated
prr
parents: 50346
diff changeset
   463
     * of the conditions invalidate using it.
db4f24a6cf34 8204929: Fonts with embedded bitmaps are not always rotated
prr
parents: 50346
diff changeset
   464
     * Note that we allow embedded bitmaps for the LCD case.
db4f24a6cf34 8204929: Fonts with embedded bitmaps are not always rotated
prr
parents: 50346
diff changeset
   465
     */
db4f24a6cf34 8204929: Fonts with embedded bitmaps are not always rotated
prr
parents: 50346
diff changeset
   466
    if ((aa != TEXT_AA_ON) && (fm != TEXT_FM_ON) &&
db4f24a6cf34 8204929: Fonts with embedded bitmaps are not always rotated
prr
parents: 50346
diff changeset
   467
        !context->doBold && !context->doItalize &&
db4f24a6cf34 8204929: Fonts with embedded bitmaps are not always rotated
prr
parents: 50346
diff changeset
   468
        (context->transform.yx == 0) && (context->transform.xy == 0))
db4f24a6cf34 8204929: Fonts with embedded bitmaps are not always rotated
prr
parents: 50346
diff changeset
   469
    {
db4f24a6cf34 8204929: Fonts with embedded bitmaps are not always rotated
prr
parents: 50346
diff changeset
   470
        context->useSbits = 1;
db4f24a6cf34 8204929: Fonts with embedded bitmaps are not always rotated
prr
parents: 50346
diff changeset
   471
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    return ptr_to_jlong(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
static int setupFTContext(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                          jobject font2D,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                          FTScalerInfo *scalerInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                          FTScalerContext *context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    int errCode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    scalerInfo->env = env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    scalerInfo->font2D = font2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
2369
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
   484
    if (context != NULL) {
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
   485
        FT_Set_Transform(scalerInfo->face, &context->transform, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
2369
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
   487
        errCode = FT_Set_Char_Size(scalerInfo->face, 0, context->ptsz, 72, 72);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
2369
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
   489
        if (errCode == 0) {
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
   490
            errCode = FT_Activate_Size(scalerInfo->face->size);
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
   491
        }
52258
02e4b0ab0f97 8212071: Need to set the FreeType LCD Filter to reduce fringing.
prr
parents: 51932
diff changeset
   492
02e4b0ab0f97 8212071: Need to set the FreeType LCD Filter to reduce fringing.
prr
parents: 51932
diff changeset
   493
        FT_Library_SetLcdFilter(scalerInfo->library, FT_LCD_FILTER_DEFAULT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    return errCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
54239
0804f29e8be7 8218854: FontMetrics.getMaxAdvance may be less than the maximum FontMetrics.charWidth
mbalao
parents: 53454
diff changeset
   499
/* ftsynth.c uses (0x10000, 0x0366A, 0x0, 0x10000) matrix to get oblique
0804f29e8be7 8218854: FontMetrics.getMaxAdvance may be less than the maximum FontMetrics.charWidth
mbalao
parents: 53454
diff changeset
   500
   outline.  Therefore x coordinate will change by 0x0366A*y.
0804f29e8be7 8218854: FontMetrics.getMaxAdvance may be less than the maximum FontMetrics.charWidth
mbalao
parents: 53454
diff changeset
   501
   Note that y coordinate does not change. These values are based on
0804f29e8be7 8218854: FontMetrics.getMaxAdvance may be less than the maximum FontMetrics.charWidth
mbalao
parents: 53454
diff changeset
   502
   libfreetype version 2.9.1. */
0804f29e8be7 8218854: FontMetrics.getMaxAdvance may be less than the maximum FontMetrics.charWidth
mbalao
parents: 53454
diff changeset
   503
#define OBLIQUE_MODIFIER(y)  (context->doItalize ? ((y)*0x366A/0x10000) : 0)
0804f29e8be7 8218854: FontMetrics.getMaxAdvance may be less than the maximum FontMetrics.charWidth
mbalao
parents: 53454
diff changeset
   504
0804f29e8be7 8218854: FontMetrics.getMaxAdvance may be less than the maximum FontMetrics.charWidth
mbalao
parents: 53454
diff changeset
   505
/* FT_GlyphSlot_Embolden (ftsynth.c) uses FT_MulFix(units_per_EM, y_scale) / 24
0804f29e8be7 8218854: FontMetrics.getMaxAdvance may be less than the maximum FontMetrics.charWidth
mbalao
parents: 53454
diff changeset
   506
 * strength value when glyph format is FT_GLYPH_FORMAT_OUTLINE. This value has
0804f29e8be7 8218854: FontMetrics.getMaxAdvance may be less than the maximum FontMetrics.charWidth
mbalao
parents: 53454
diff changeset
   507
 * been taken from libfreetype version 2.6 and remain valid at least up to
0804f29e8be7 8218854: FontMetrics.getMaxAdvance may be less than the maximum FontMetrics.charWidth
mbalao
parents: 53454
diff changeset
   508
 * 2.9.1. */
0804f29e8be7 8218854: FontMetrics.getMaxAdvance may be less than the maximum FontMetrics.charWidth
mbalao
parents: 53454
diff changeset
   509
#define BOLD_MODIFIER(units_per_EM, y_scale) \
0804f29e8be7 8218854: FontMetrics.getMaxAdvance may be less than the maximum FontMetrics.charWidth
mbalao
parents: 53454
diff changeset
   510
    (context->doBold ? FT_MulFix(units_per_EM, y_scale) / 24 : 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
 * Class:     sun_font_FreetypeFontScaler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
 * Method:    getFontMetricsNative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
 * Signature: (Lsun/font/Font2D;J)Lsun/font/StrikeMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
JNIEXPORT jobject JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
Java_sun_font_FreetypeFontScaler_getFontMetricsNative(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        JNIEnv *env, jobject scaler, jobject font2D,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        jlong pScalerContext, jlong pScaler) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    jobject metrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    jfloat ax, ay, dx, dy, bx, by, lx, ly, mx, my;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    jfloat f0 = 0.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    FTScalerContext *context =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        (FTScalerContext*) jlong_to_ptr(pScalerContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    FTScalerInfo *scalerInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
             (FTScalerInfo*) jlong_to_ptr(pScaler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    int errCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    if (isNullScalerContext(context) || scalerInfo == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        return (*env)->NewObject(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                                 sunFontIDs.strikeMetricsClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                                 sunFontIDs.strikeMetricsCtr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                                 f0, f0, f0, f0, f0, f0, f0, f0, f0, f0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    errCode = setupFTContext(env, font2D, scalerInfo, context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    if (errCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        metrics = (*env)->NewObject(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                                 sunFontIDs.strikeMetricsClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                                 sunFontIDs.strikeMetricsCtr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                                 f0, f0, f0, f0, f0, f0, f0, f0, f0, f0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        invalidateJavaScaler(env, scaler, scalerInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        return metrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    /* This is ugly and has to be reworked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
       Freetype provide means to add style to glyph but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
       it seems there is no way to adjust metrics accordingly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
       So, we have to do adust them explicitly and stay consistent with what
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
       freetype does to outlines. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    /**** Note: only some metrics are affected by styling ***/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
27053
b8def728da69 8017773: OpenJDK7 returns incorrect TrueType font metrics
ysuenaga
parents: 26617
diff changeset
   560
    /* See https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=657854 */
b8def728da69 8017773: OpenJDK7 returns incorrect TrueType font metrics
ysuenaga
parents: 26617
diff changeset
   561
#define FT_MulFixFloatShift6(a, b) (((float) (a)) * ((float) (b)) / 65536.0 / 64.0)
b8def728da69 8017773: OpenJDK7 returns incorrect TrueType font metrics
ysuenaga
parents: 26617
diff changeset
   562
52838
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents: 52258
diff changeset
   563
#define contextAwareMetricsX(x, y) \
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents: 52258
diff changeset
   564
    (FTFixedToFloat(context->transform.xx) * (x) - \
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents: 52258
diff changeset
   565
     FTFixedToFloat(context->transform.xy) * (y))
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents: 52258
diff changeset
   566
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents: 52258
diff changeset
   567
#define contextAwareMetricsY(x, y) \
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents: 52258
diff changeset
   568
    (-FTFixedToFloat(context->transform.yx) * (x) + \
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents: 52258
diff changeset
   569
     FTFixedToFloat(context->transform.yy) * (y))
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents: 52258
diff changeset
   570
27053
b8def728da69 8017773: OpenJDK7 returns incorrect TrueType font metrics
ysuenaga
parents: 26617
diff changeset
   571
    /*
b8def728da69 8017773: OpenJDK7 returns incorrect TrueType font metrics
ysuenaga
parents: 26617
diff changeset
   572
     * See FreeType source code: src/base/ftobjs.c ft_recompute_scaled_metrics()
b8def728da69 8017773: OpenJDK7 returns incorrect TrueType font metrics
ysuenaga
parents: 26617
diff changeset
   573
     * http://icedtea.classpath.org/bugzilla/show_bug.cgi?id=1659
b8def728da69 8017773: OpenJDK7 returns incorrect TrueType font metrics
ysuenaga
parents: 26617
diff changeset
   574
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    /* ascent */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    ax = 0;
27053
b8def728da69 8017773: OpenJDK7 returns incorrect TrueType font metrics
ysuenaga
parents: 26617
diff changeset
   577
    ay = -(jfloat) (FT_MulFixFloatShift6(
b8def728da69 8017773: OpenJDK7 returns incorrect TrueType font metrics
ysuenaga
parents: 26617
diff changeset
   578
                       ((jlong) scalerInfo->face->ascender),
5754
ef19a58b3c42 6961732: FontMetrics.getLeading() may be negative in freetype-based OpenJDK builds.
neugens
parents: 5580
diff changeset
   579
                       (jlong) scalerInfo->face->size->metrics.y_scale));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    /* descent */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    dx = 0;
27053
b8def728da69 8017773: OpenJDK7 returns incorrect TrueType font metrics
ysuenaga
parents: 26617
diff changeset
   582
    dy = -(jfloat) (FT_MulFixFloatShift6(
b8def728da69 8017773: OpenJDK7 returns incorrect TrueType font metrics
ysuenaga
parents: 26617
diff changeset
   583
                       ((jlong) scalerInfo->face->descender),
5754
ef19a58b3c42 6961732: FontMetrics.getLeading() may be negative in freetype-based OpenJDK builds.
neugens
parents: 5580
diff changeset
   584
                       (jlong) scalerInfo->face->size->metrics.y_scale));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    /* baseline */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    bx = by = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    /* leading */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    lx = 0;
27053
b8def728da69 8017773: OpenJDK7 returns incorrect TrueType font metrics
ysuenaga
parents: 26617
diff changeset
   590
    ly = (jfloat) (FT_MulFixFloatShift6(
b8def728da69 8017773: OpenJDK7 returns incorrect TrueType font metrics
ysuenaga
parents: 26617
diff changeset
   591
                      (jlong) scalerInfo->face->height,
5754
ef19a58b3c42 6961732: FontMetrics.getLeading() may be negative in freetype-based OpenJDK builds.
neugens
parents: 5580
diff changeset
   592
                      (jlong) scalerInfo->face->size->metrics.y_scale))
ef19a58b3c42 6961732: FontMetrics.getLeading() may be negative in freetype-based OpenJDK builds.
neugens
parents: 5580
diff changeset
   593
                  + ay - dy;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    /* max advance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    mx = (jfloat) FT26Dot6ToFloat(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                     scalerInfo->face->size->metrics.max_advance +
54239
0804f29e8be7 8218854: FontMetrics.getMaxAdvance may be less than the maximum FontMetrics.charWidth
mbalao
parents: 53454
diff changeset
   597
                     OBLIQUE_MODIFIER(scalerInfo->face->size->metrics.height) +
0804f29e8be7 8218854: FontMetrics.getMaxAdvance may be less than the maximum FontMetrics.charWidth
mbalao
parents: 53454
diff changeset
   598
                     BOLD_MODIFIER(scalerInfo->face->units_per_EM,
0804f29e8be7 8218854: FontMetrics.getMaxAdvance may be less than the maximum FontMetrics.charWidth
mbalao
parents: 53454
diff changeset
   599
                             scalerInfo->face->size->metrics.y_scale));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    my = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    metrics = (*env)->NewObject(env,
52838
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents: 52258
diff changeset
   603
        sunFontIDs.strikeMetricsClass,
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents: 52258
diff changeset
   604
        sunFontIDs.strikeMetricsCtr,
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents: 52258
diff changeset
   605
        contextAwareMetricsX(ax, ay), contextAwareMetricsY(ax, ay),
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents: 52258
diff changeset
   606
        contextAwareMetricsX(dx, dy), contextAwareMetricsY(dx, dy),
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents: 52258
diff changeset
   607
        bx, by,
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents: 52258
diff changeset
   608
        contextAwareMetricsX(lx, ly), contextAwareMetricsY(lx, ly),
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents: 52258
diff changeset
   609
        contextAwareMetricsX(mx, my), contextAwareMetricsY(mx, my));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    return metrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
 * Class:     sun_font_FreetypeFontScaler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
 * Method:    getGlyphAdvanceNative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
 * Signature: (Lsun/font/Font2D;JI)F
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
JNIEXPORT jfloat JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
Java_sun_font_FreetypeFontScaler_getGlyphAdvanceNative(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        JNIEnv *env, jobject scaler, jobject font2D,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        jlong pScalerContext, jlong pScaler, jint glyphCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
   /* This method is rarely used because requests for metrics are usually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
      coupled with request for bitmap and to large extend work can be reused
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
      (to find out metrics we need to hint glyph).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
      So, we typically go through getGlyphImage code path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
      For initial freetype implementation we delegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
      all work to getGlyphImage but drop result image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
      This is waste of work related to scan conversion and conversion from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
      freetype format to our format but for now this seems to be ok.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
      NB: investigate performance benefits of refactoring code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
      to avoid unnecesary work with bitmaps. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    GlyphInfo *info;
58615
d5ea3bde1ebe 8222690: Better Glyph Images
prr
parents: 58613
diff changeset
   638
    jfloat advance = 0.0f;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    jlong image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    image = Java_sun_font_FreetypeFontScaler_getGlyphImageNative(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                 env, scaler, font2D, pScalerContext, pScaler, glyphCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    info = (GlyphInfo*) jlong_to_ptr(image);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
58615
d5ea3bde1ebe 8222690: Better Glyph Images
prr
parents: 58613
diff changeset
   645
    if (info != NULL) {
d5ea3bde1ebe 8222690: Better Glyph Images
prr
parents: 58613
diff changeset
   646
        advance = info->advanceX;
d5ea3bde1ebe 8222690: Better Glyph Images
prr
parents: 58613
diff changeset
   647
        free(info);
d5ea3bde1ebe 8222690: Better Glyph Images
prr
parents: 58613
diff changeset
   648
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    return advance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
 * Class:     sun_font_FreetypeFontScaler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
 * Method:    getGlyphMetricsNative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
 * Signature: (Lsun/font/Font2D;JILjava/awt/geom/Point2D/Float;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
Java_sun_font_FreetypeFontScaler_getGlyphMetricsNative(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        JNIEnv *env, jobject scaler, jobject font2D, jlong pScalerContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        jlong pScaler, jint glyphCode, jobject metrics) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     /* As initial implementation we delegate all work to getGlyphImage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        but drop result image. This is clearly waste of resorces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        TODO: investigate performance benefits of refactoring code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
              by avoiding bitmap generation and conversion from FT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
              bitmap format. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     GlyphInfo *info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     jlong image = Java_sun_font_FreetypeFontScaler_getGlyphImageNative(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                                 env, scaler, font2D,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                                 pScalerContext, pScaler, glyphCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     info = (GlyphInfo*) jlong_to_ptr(image);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
58685
4b13a908c2d0 8232381: add result NULL-checking to freetypeScaler.c
mbaesken
parents: 58634
diff changeset
   676
     if (info != NULL) {
4b13a908c2d0 8232381: add result NULL-checking to freetypeScaler.c
mbaesken
parents: 58634
diff changeset
   677
         (*env)->SetFloatField(env, metrics, sunFontIDs.xFID, info->advanceX);
4b13a908c2d0 8232381: add result NULL-checking to freetypeScaler.c
mbaesken
parents: 58634
diff changeset
   678
         (*env)->SetFloatField(env, metrics, sunFontIDs.yFID, info->advanceY);
4b13a908c2d0 8232381: add result NULL-checking to freetypeScaler.c
mbaesken
parents: 58634
diff changeset
   679
         free(info);
4b13a908c2d0 8232381: add result NULL-checking to freetypeScaler.c
mbaesken
parents: 58634
diff changeset
   680
     } else {
4b13a908c2d0 8232381: add result NULL-checking to freetypeScaler.c
mbaesken
parents: 58634
diff changeset
   681
         (*env)->SetFloatField(env, metrics, sunFontIDs.xFID, 0.0f);
4b13a908c2d0 8232381: add result NULL-checking to freetypeScaler.c
mbaesken
parents: 58634
diff changeset
   682
         (*env)->SetFloatField(env, metrics, sunFontIDs.yFID, 0.0f);
4b13a908c2d0 8232381: add result NULL-checking to freetypeScaler.c
mbaesken
parents: 58634
diff changeset
   683
     }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
static GlyphInfo* getNullGlyphImage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    GlyphInfo *glyphInfo =  (GlyphInfo*) calloc(1, sizeof(GlyphInfo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    return glyphInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
static void CopyBW2Grey8(const void* srcImage, int srcRowBytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                         void* dstImage, int dstRowBytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                         int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    const UInt8* srcRow = (UInt8*)srcImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    UInt8* dstRow = (UInt8*)dstImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    int wholeByteCount = width >> 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    int remainingBitsCount = width & 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    int i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    while (height--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        const UInt8* src8 = srcRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        UInt8* dstByte = dstRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        unsigned srcValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        srcRow += srcRowBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        dstRow += dstRowBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        for (i = 0; i < wholeByteCount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            srcValue = *src8++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            for (j = 0; j < 8; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                *dstByte++ = (srcValue & 0x80) ? 0xFF : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                srcValue <<= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        if (remainingBitsCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            srcValue = *src8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            for (j = 0; j < remainingBitsCount; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                *dstByte++ = (srcValue & 0x80) ? 0xFF : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                srcValue <<= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
#define Grey4ToAlpha255(value) (((value) << 4) + ((value) >> 3))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
static void CopyGrey4ToGrey8(const void* srcImage, int srcRowBytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                void* dstImage, int dstRowBytes, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     const UInt8* srcRow = (UInt8*) srcImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     UInt8* dstRow = (UInt8*) dstImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     while (height--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
         const UInt8* src8 = srcRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
         UInt8* dstByte = dstRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
         unsigned srcValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
         srcRow += srcRowBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
         dstRow += dstRowBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
         for (i = 0; i < width; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
             srcValue = *src8++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
             *dstByte++ = Grey4ToAlpha255(srcValue & 0x0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
             *dstByte++ = Grey4ToAlpha255(srcValue >> 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
/* We need it because FT rows are often padded to 4 byte boundaries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    and our internal format is not padded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
static void CopyFTSubpixelToSubpixel(const void* srcImage, int srcRowBytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                                     void* dstImage, int dstRowBytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                                     int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    unsigned char *srcRow = (unsigned char *) srcImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    unsigned char *dstRow = (unsigned char *) dstImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    while (height--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        memcpy(dstRow, srcRow, width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        srcRow += srcRowBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        dstRow += dstRowBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
/* We need it because FT rows are often padded to 4 byte boundaries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
   and our internal format is not padded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
static void CopyFTSubpixelVToSubpixel(const void* srcImage, int srcRowBytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                                      void* dstImage, int dstRowBytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                                      int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    unsigned char *srcRow = (unsigned char *) srcImage, *srcByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    unsigned char *dstRow = (unsigned char *) dstImage, *dstByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    while (height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        srcByte = srcRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        dstByte = dstRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        for (i = 0; i < width; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            *dstByte++ = *srcByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            *dstByte++ = *(srcByte + srcRowBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            *dstByte++ = *(srcByte + 2*srcRowBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            srcByte++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        srcRow += 3*srcRowBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        dstRow += dstRowBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        height -= 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
58629
2ede50a0f67d 8225286: Better rendering of native glyphs
prr
parents: 58615
diff changeset
   790
/* JDK does not use glyph images for fonts with a
2ede50a0f67d 8225286: Better rendering of native glyphs
prr
parents: 58615
diff changeset
   791
 * pixel size > 100 (see THRESHOLD in OutlineTextRenderer.java)
2ede50a0f67d 8225286: Better rendering of native glyphs
prr
parents: 58615
diff changeset
   792
 * so if the glyph bitmap image dimension is > 1024 pixels,
2ede50a0f67d 8225286: Better rendering of native glyphs
prr
parents: 58615
diff changeset
   793
 * something is up.
2ede50a0f67d 8225286: Better rendering of native glyphs
prr
parents: 58615
diff changeset
   794
 */
2ede50a0f67d 8225286: Better rendering of native glyphs
prr
parents: 58615
diff changeset
   795
#define MAX_GLYPH_DIM 1024
2ede50a0f67d 8225286: Better rendering of native glyphs
prr
parents: 58615
diff changeset
   796
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
 * Class:     sun_font_FreetypeFontScaler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
 * Method:    getGlyphImageNative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
 * Signature: (Lsun/font/Font2D;JI)J
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
Java_sun_font_FreetypeFontScaler_getGlyphImageNative(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        JNIEnv *env, jobject scaler, jobject font2D,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        jlong pScalerContext, jlong pScaler, jint glyphCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    int error, imageSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    UInt16 width, height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    GlyphInfo *glyphInfo;
52966
7ed5edf6ba0c 8214002: Cannot use italic font style if the font has embedded bitmap
itakiguchi
parents: 52838
diff changeset
   810
    int renderFlags = FT_LOAD_DEFAULT, target;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    FT_GlyphSlot ftglyph;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    FTScalerContext* context =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        (FTScalerContext*) jlong_to_ptr(pScalerContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    FTScalerInfo *scalerInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
             (FTScalerInfo*) jlong_to_ptr(pScaler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    if (isNullScalerContext(context) || scalerInfo == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        return ptr_to_jlong(getNullGlyphImage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    error = setupFTContext(env, font2D, scalerInfo, context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        invalidateJavaScaler(env, scaler, scalerInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        return ptr_to_jlong(getNullGlyphImage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
50658
db4f24a6cf34 8204929: Fonts with embedded bitmaps are not always rotated
prr
parents: 50346
diff changeset
   828
    if (!context->useSbits) {
db4f24a6cf34 8204929: Fonts with embedded bitmaps are not always rotated
prr
parents: 50346
diff changeset
   829
        renderFlags |= FT_LOAD_NO_BITMAP;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    /* NB: in case of non identity transform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     we might also prefer to disable transform before hinting,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     and apply it explicitly after hinting is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     Or we can disable hinting. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    /* select appropriate hinting mode */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    if (context->aaType == TEXT_AA_OFF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        target = FT_LOAD_TARGET_MONO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    } else if (context->aaType == TEXT_AA_ON) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        target = FT_LOAD_TARGET_NORMAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    } else if (context->aaType == TEXT_AA_LCD_HRGB ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
               context->aaType == TEXT_AA_LCD_HBGR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        target = FT_LOAD_TARGET_LCD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        target = FT_LOAD_TARGET_LCD_V;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    renderFlags |= target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    error = FT_Load_Glyph(scalerInfo->face, glyphCode, renderFlags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        //do not destroy scaler yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        //this can be problem of particular context (e.g. with bad transform)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        return ptr_to_jlong(getNullGlyphImage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    ftglyph = scalerInfo->face->glyph;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    /* apply styles */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    if (context->doBold) { /* if bold style */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        FT_GlyphSlot_Embolden(ftglyph);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    if (context->doItalize) { /* if oblique */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        FT_GlyphSlot_Oblique(ftglyph);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    /* generate bitmap if it is not done yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     e.g. if algorithmic styling is performed and style was added to outline */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    if (ftglyph->format == FT_GLYPH_FORMAT_OUTLINE) {
58629
2ede50a0f67d 8225286: Better rendering of native glyphs
prr
parents: 58615
diff changeset
   870
        FT_BBox bbox;
2ede50a0f67d 8225286: Better rendering of native glyphs
prr
parents: 58615
diff changeset
   871
        FT_Outline_Get_CBox(&(ftglyph->outline), &bbox);
2ede50a0f67d 8225286: Better rendering of native glyphs
prr
parents: 58615
diff changeset
   872
        int w = (int)((bbox.xMax>>6)-(bbox.xMin>>6));
2ede50a0f67d 8225286: Better rendering of native glyphs
prr
parents: 58615
diff changeset
   873
        int h = (int)((bbox.yMax>>6)-(bbox.yMin>>6));
2ede50a0f67d 8225286: Better rendering of native glyphs
prr
parents: 58615
diff changeset
   874
        if (w > MAX_GLYPH_DIM || h > MAX_GLYPH_DIM) {
2ede50a0f67d 8225286: Better rendering of native glyphs
prr
parents: 58615
diff changeset
   875
            glyphInfo = getNullGlyphImage();
2ede50a0f67d 8225286: Better rendering of native glyphs
prr
parents: 58615
diff changeset
   876
            return ptr_to_jlong(glyphInfo);
2ede50a0f67d 8225286: Better rendering of native glyphs
prr
parents: 58615
diff changeset
   877
        }
53454
ba547b9b4996 8216965: crash in freetypeScaler.c CopyBW2Grey8
prr
parents: 52966
diff changeset
   878
        error = FT_Render_Glyph(ftglyph, FT_LOAD_TARGET_MODE(target));
ba547b9b4996 8216965: crash in freetypeScaler.c CopyBW2Grey8
prr
parents: 52966
diff changeset
   879
        if (error != 0) {
ba547b9b4996 8216965: crash in freetypeScaler.c CopyBW2Grey8
prr
parents: 52966
diff changeset
   880
            return ptr_to_jlong(getNullGlyphImage());
ba547b9b4996 8216965: crash in freetypeScaler.c CopyBW2Grey8
prr
parents: 52966
diff changeset
   881
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    width  = (UInt16) ftglyph->bitmap.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    height = (UInt16) ftglyph->bitmap.rows;
58629
2ede50a0f67d 8225286: Better rendering of native glyphs
prr
parents: 58615
diff changeset
   886
    if (width > MAX_GLYPH_DIM || height > MAX_GLYPH_DIM) {
2ede50a0f67d 8225286: Better rendering of native glyphs
prr
parents: 58615
diff changeset
   887
        glyphInfo = getNullGlyphImage();
2ede50a0f67d 8225286: Better rendering of native glyphs
prr
parents: 58615
diff changeset
   888
        return ptr_to_jlong(glyphInfo);
2ede50a0f67d 8225286: Better rendering of native glyphs
prr
parents: 58615
diff changeset
   889
    }
2ede50a0f67d 8225286: Better rendering of native glyphs
prr
parents: 58615
diff changeset
   890
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    imageSize = width*height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    glyphInfo = (GlyphInfo*) malloc(sizeof(GlyphInfo) + imageSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    if (glyphInfo == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        glyphInfo = getNullGlyphImage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        return ptr_to_jlong(glyphInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    glyphInfo->cellInfo  = NULL;
5579
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4243
diff changeset
   899
    glyphInfo->managed   = UNMANAGED_GLYPH;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    glyphInfo->rowBytes  = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    glyphInfo->width     = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    glyphInfo->height    = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    glyphInfo->topLeftX  = (float)  ftglyph->bitmap_left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    glyphInfo->topLeftY  = (float) -ftglyph->bitmap_top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
881
6aefebba2393 6587560: OpenJDK problem handling bitmaps returned when LCD text is requested
igor
parents: 561
diff changeset
   906
    if (ftglyph->bitmap.pixel_mode ==  FT_PIXEL_MODE_LCD) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        glyphInfo->width = width/3;
881
6aefebba2393 6587560: OpenJDK problem handling bitmaps returned when LCD text is requested
igor
parents: 561
diff changeset
   908
    } else if (ftglyph->bitmap.pixel_mode ==  FT_PIXEL_MODE_LCD_V) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        glyphInfo->height = glyphInfo->height/3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    if (context->fmType == TEXT_FM_ON) {
554
88eb2812c2a4 6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents: 2
diff changeset
   913
        double advh = FTFixedToFloat(ftglyph->linearHoriAdvance);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        glyphInfo->advanceX =
554
88eb2812c2a4 6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents: 2
diff changeset
   915
            (float) (advh * FTFixedToFloat(context->transform.xx));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        glyphInfo->advanceY =
554
88eb2812c2a4 6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents: 2
diff changeset
   917
            (float) (advh * FTFixedToFloat(context->transform.xy));
88eb2812c2a4 6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents: 2
diff changeset
   918
    } else {
88eb2812c2a4 6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents: 2
diff changeset
   919
        if (!ftglyph->advance.y) {
88eb2812c2a4 6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents: 2
diff changeset
   920
            glyphInfo->advanceX =
50346
9e530b150333 8203485: [freetype] text rotated on 180 degrees is too narrow
bae
parents: 47216
diff changeset
   921
                (float) FT26Dot6ToInt(ftglyph->advance.x);
554
88eb2812c2a4 6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents: 2
diff changeset
   922
            glyphInfo->advanceY = 0;
88eb2812c2a4 6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents: 2
diff changeset
   923
        } else if (!ftglyph->advance.x) {
88eb2812c2a4 6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents: 2
diff changeset
   924
            glyphInfo->advanceX = 0;
88eb2812c2a4 6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents: 2
diff changeset
   925
            glyphInfo->advanceY =
50346
9e530b150333 8203485: [freetype] text rotated on 180 degrees is too narrow
bae
parents: 47216
diff changeset
   926
                (float) FT26Dot6ToInt(-ftglyph->advance.y);
554
88eb2812c2a4 6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents: 2
diff changeset
   927
        } else {
88eb2812c2a4 6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents: 2
diff changeset
   928
            glyphInfo->advanceX = FT26Dot6ToFloat(ftglyph->advance.x);
88eb2812c2a4 6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents: 2
diff changeset
   929
            glyphInfo->advanceY = FT26Dot6ToFloat(-ftglyph->advance.y);
88eb2812c2a4 6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents: 2
diff changeset
   930
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    if (imageSize == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        glyphInfo->image = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        glyphInfo->image = (unsigned char*) glyphInfo + sizeof(GlyphInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        //convert result to output format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        //output format is either 3 bytes per pixel (for subpixel modes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        // or 1 byte per pixel for AA and B&W
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        if (ftglyph->bitmap.pixel_mode ==  FT_PIXEL_MODE_MONO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            /* convert from 8 pixels per byte to 1 byte per pixel */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            CopyBW2Grey8(ftglyph->bitmap.buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                         ftglyph->bitmap.pitch,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                         (void *) glyphInfo->image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                         width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                         width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                         height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        } else if (ftglyph->bitmap.pixel_mode ==  FT_PIXEL_MODE_GRAY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            /* byte per pixel to byte per pixel => just copy */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            memcpy(glyphInfo->image, ftglyph->bitmap.buffer, imageSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        } else if (ftglyph->bitmap.pixel_mode ==  FT_PIXEL_MODE_GRAY4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            /* 4 bits per pixel to byte per pixel */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            CopyGrey4ToGrey8(ftglyph->bitmap.buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                             ftglyph->bitmap.pitch,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                             (void *) glyphInfo->image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                             width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                             width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                             height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        } else if (ftglyph->bitmap.pixel_mode ==  FT_PIXEL_MODE_LCD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            /* 3 bytes per pixel to 3 bytes per pixel */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            CopyFTSubpixelToSubpixel(ftglyph->bitmap.buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                                     ftglyph->bitmap.pitch,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                                     (void *) glyphInfo->image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                                     width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                                     width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                                     height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        } else if (ftglyph->bitmap.pixel_mode ==  FT_PIXEL_MODE_LCD_V) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            /* 3 bytes per pixel to 3 bytes per pixel */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            CopyFTSubpixelVToSubpixel(ftglyph->bitmap.buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                                      ftglyph->bitmap.pitch,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                                      (void *) glyphInfo->image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                                      width*3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                                      width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                                      height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            glyphInfo->rowBytes *=3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            free(glyphInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            glyphInfo = getNullGlyphImage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
    return ptr_to_jlong(glyphInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
 * Class:     sun_font_FreetypeFontScaler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
 * Method:    disposeNativeScaler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
 * Signature: (J)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
Java_sun_font_FreetypeFontScaler_disposeNativeScaler(
20414
c43f5228a1f9 8020190: Fatal: Bug in native code: jfieldID must match object
prr
parents: 18198
diff changeset
   992
        JNIEnv *env, jobject scaler, jobject font2D, jlong pScaler) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    FTScalerInfo* scalerInfo = (FTScalerInfo *) jlong_to_ptr(pScaler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
2369
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
   995
    /* Freetype functions *may* cause callback to java
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
   996
       that can use cached values. Make sure our cache is up to date.
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
   997
       NB: scaler context is not important at this point, can use NULL. */
20414
c43f5228a1f9 8020190: Fatal: Bug in native code: jfieldID must match object
prr
parents: 18198
diff changeset
   998
    int errCode = setupFTContext(env, font2D, scalerInfo, NULL);
2369
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
   999
    if (errCode) {
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
  1000
        return;
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
  1001
    }
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
  1002
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
    freeNativeResources(env, scalerInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
 * Class:     sun_font_FreetypeFontScaler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
 * Method:    getNumGlyphsNative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
 * Signature: ()I
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
Java_sun_font_FreetypeFontScaler_getNumGlyphsNative(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        JNIEnv *env, jobject scaler, jlong pScaler) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    FTScalerInfo* scalerInfo = (FTScalerInfo *) jlong_to_ptr(pScaler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
    if (scalerInfo == NULL || scalerInfo->face == NULL) { /* bad/null scaler */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        /* null scaler can render 1 glyph - "missing glyph" with code 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
           (all glyph codes requested by user are mapped to code 0 at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
           validation step) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        invalidateJavaScaler(env, scaler, scalerInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        return (jint) 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    return (jint) scalerInfo->face->num_glyphs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
 * Class:     sun_font_FreetypeFontScaler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
 * Method:    getMissingGlyphCodeNative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
 * Signature: ()I
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
Java_sun_font_FreetypeFontScaler_getMissingGlyphCodeNative(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        JNIEnv *env, jobject scaler, jlong pScaler) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    /* Is it always 0 for freetype? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
 * Class:     sun_font_FreetypeFontScaler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
 * Method:    getGlyphCodeNative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
 * Signature: (C)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
Java_sun_font_FreetypeFontScaler_getGlyphCodeNative(
20414
c43f5228a1f9 8020190: Fatal: Bug in native code: jfieldID must match object
prr
parents: 18198
diff changeset
  1047
        JNIEnv *env, jobject scaler,
c43f5228a1f9 8020190: Fatal: Bug in native code: jfieldID must match object
prr
parents: 18198
diff changeset
  1048
        jobject font2D, jlong pScaler, jchar charCode) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    FTScalerInfo* scalerInfo = (FTScalerInfo *) jlong_to_ptr(pScaler);
2369
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
  1051
    int errCode;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    if (scaler == NULL || scalerInfo->face == NULL) { /* bad/null scaler */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        invalidateJavaScaler(env, scaler, scalerInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
2369
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
  1058
    /* Freetype functions *may* cause callback to java
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
  1059
       that can use cached values. Make sure our cache is up to date.
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
  1060
       Scaler context is not important here, can use NULL. */
20414
c43f5228a1f9 8020190: Fatal: Bug in native code: jfieldID must match object
prr
parents: 18198
diff changeset
  1061
    errCode = setupFTContext(env, font2D, scalerInfo, NULL);
2369
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
  1062
    if (errCode) {
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
  1063
        return 0;
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
  1064
    }
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
  1065
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
    return FT_Get_Char_Index(scalerInfo->face, charCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
#define FloatToF26Dot6(x) ((unsigned int) ((x)*64))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
static FT_Outline* getFTOutline(JNIEnv* env, jobject font2D,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        FTScalerContext *context, FTScalerInfo* scalerInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        jint glyphCode, jfloat xpos, jfloat ypos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    int renderFlags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
    FT_Error error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    FT_GlyphSlot ftglyph;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
    if (glyphCode >= INVISIBLE_GLYPHS ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            isNullScalerContext(context) || scalerInfo == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
    error = setupFTContext(env, font2D, scalerInfo, context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
    if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
    renderFlags = FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
    error = FT_Load_Glyph(scalerInfo->face, glyphCode, renderFlags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
    if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
    ftglyph = scalerInfo->face->glyph;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    /* apply styles */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
    if (context->doBold) { /* if bold style */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        FT_GlyphSlot_Embolden(ftglyph);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
    if (context->doItalize) { /* if oblique */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        FT_GlyphSlot_Oblique(ftglyph);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
    FT_Outline_Translate(&ftglyph->outline,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                         FloatToF26Dot6(xpos),
561
977a2ff62fc9 6703377: freetype: glyph vector outline is not translated correctly
igor
parents: 554
diff changeset
  1108
                         -FloatToF26Dot6(ypos));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
    return &ftglyph->outline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
#define F26Dot6ToFloat(n) (((float)(n))/((float) 64))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
/* Types of GeneralPath segments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
   TODO: pull constants from other place? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
#define SEG_UNKNOWN -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
#define SEG_MOVETO   0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
#define SEG_LINETO   1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
#define SEG_QUADTO   2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
#define SEG_CUBICTO  3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
#define SEG_CLOSE    4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
#define WIND_NON_ZERO 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
#define WIND_EVEN_ODD 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
/* Placeholder to accumulate GeneralPath data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
typedef struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
    jint numTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    jint numCoords;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
    jint lenTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    jint lenCoords;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
    jint wr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
    jbyte* pointTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
    jfloat* pointCoords;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
} GPData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
/* returns 0 on failure */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
static int allocateSpaceForGP(GPData* gpdata, int npoints, int ncontours) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
    int maxTypes, maxCoords;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
    /* we may have up to N intermediate points per contour
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
       (and for each point can actually cause new curve to be generated)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
       In addition we can also have 2 extra point per outline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    maxTypes  = 2*npoints  + 2*ncontours;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
    maxCoords = 4*(npoints + 2*ncontours); //we may need to insert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
                                           //up to n-1 intermediate points
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
    /* first usage - allocate space and intialize all fields */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
    if (gpdata->pointTypes == NULL || gpdata->pointCoords == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        gpdata->lenTypes  = maxTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        gpdata->lenCoords = maxCoords;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        gpdata->pointTypes  = (jbyte*)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
             malloc(gpdata->lenTypes*sizeof(jbyte));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        gpdata->pointCoords = (jfloat*)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
             malloc(gpdata->lenCoords*sizeof(jfloat));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        gpdata->numTypes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        gpdata->numCoords = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        gpdata->wr = WIND_NON_ZERO; /* By default, outlines are filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                                       using the non-zero winding rule. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        /* do we have enough space? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        if (gpdata->lenTypes - gpdata->numTypes < maxTypes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            gpdata->lenTypes  += maxTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
            gpdata->pointTypes  = (jbyte*)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
              realloc(gpdata->pointTypes, gpdata->lenTypes*sizeof(jbyte));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        if (gpdata->lenCoords - gpdata->numCoords < maxCoords) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            gpdata->lenCoords += maxCoords;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            gpdata->pointCoords = (jfloat*)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
              realloc(gpdata->pointCoords, gpdata->lenCoords*sizeof(jfloat));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
    /* failure if any of mallocs failed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
    if (gpdata->pointTypes == NULL ||  gpdata->pointCoords == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
26617
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1185
static void addSeg(GPData *gp, jbyte type) {
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1186
    gp->pointTypes[gp->numTypes++] = type;
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1187
}
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1188
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1189
static void addCoords(GPData *gp, FT_Vector *p) {
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1190
    gp->pointCoords[gp->numCoords++] =  F26Dot6ToFloat(p->x);
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1191
    gp->pointCoords[gp->numCoords++] = -F26Dot6ToFloat(p->y);
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1192
}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
26617
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1194
static int moveTo(FT_Vector *to, GPData *gp) {
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1195
    if (gp->numCoords)
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1196
        addSeg(gp, SEG_CLOSE);
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1197
    addCoords(gp, to);
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1198
    addSeg(gp, SEG_MOVETO);
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1199
    return FT_Err_Ok;
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1200
}
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1201
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1202
static int lineTo(FT_Vector *to, GPData *gp) {
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1203
    addCoords(gp, to);
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1204
    addSeg(gp, SEG_LINETO);
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1205
    return FT_Err_Ok;
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1206
}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
26617
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1208
static int conicTo(FT_Vector *control, FT_Vector *to, GPData *gp) {
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1209
    addCoords(gp, control);
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1210
    addCoords(gp, to);
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1211
    addSeg(gp, SEG_QUADTO);
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1212
    return FT_Err_Ok;
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1213
}
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1214
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1215
static int cubicTo(FT_Vector *control1,
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1216
                   FT_Vector *control2,
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1217
                   FT_Vector *to,
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1218
                   GPData    *gp) {
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1219
    addCoords(gp, control1);
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1220
    addCoords(gp, control2);
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1221
    addCoords(gp, to);
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1222
    addSeg(gp, SEG_CUBICTO);
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1223
    return FT_Err_Ok;
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1224
}
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1225
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1226
static void addToGP(GPData* gpdata, FT_Outline*outline) {
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1227
    static const FT_Outline_Funcs outline_funcs = {
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1228
        (FT_Outline_MoveToFunc) moveTo,
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1229
        (FT_Outline_LineToFunc) lineTo,
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1230
        (FT_Outline_ConicToFunc) conicTo,
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1231
        (FT_Outline_CubicToFunc) cubicTo,
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1232
        0, /* shift */
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1233
        0, /* delta */
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1234
    };
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1235
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1236
    FT_Outline_Decompose(outline, &outline_funcs, gpdata);
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1237
    if (gpdata->numCoords)
ca6dcc52ff1c 8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents: 25859
diff changeset
  1238
        addSeg(gpdata, SEG_CLOSE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
    /* If set to 1, the outline will be filled using the even-odd fill rule */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
    if (outline->flags & FT_OUTLINE_EVEN_ODD_FILL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        gpdata->wr = WIND_EVEN_ODD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
static void freeGP(GPData* gpdata) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
    if (gpdata->pointCoords != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        free(gpdata->pointCoords);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        gpdata->pointCoords = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        gpdata->numCoords = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
        gpdata->lenCoords = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
    if (gpdata->pointTypes != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        free(gpdata->pointTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        gpdata->pointTypes = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        gpdata->numTypes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        gpdata->lenTypes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
static jobject getGlyphGeneralPath(JNIEnv* env, jobject font2D,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        FTScalerContext *context, FTScalerInfo *scalerInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        jint glyphCode, jfloat xpos, jfloat ypos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
    FT_Outline* outline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
    jobject gp = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
    jbyteArray types;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
    jfloatArray coords;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
    GPData gpdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
    outline = getFTOutline(env, font2D, context, scalerInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
                           glyphCode, xpos, ypos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
    if (outline == NULL || outline->n_points == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        return gp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
    gpdata.pointTypes  = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
    gpdata.pointCoords = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
    if (!allocateSpaceForGP(&gpdata, outline->n_points, outline->n_contours)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        return gp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
    addToGP(&gpdata, outline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
    types  = (*env)->NewByteArray(env, gpdata.numTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
    coords = (*env)->NewFloatArray(env, gpdata.numCoords);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
    if (types && coords) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        (*env)->SetByteArrayRegion(env, types, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                                   gpdata.numTypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                                   gpdata.pointTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        (*env)->SetFloatArrayRegion(env, coords, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                                    gpdata.numCoords,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                                    gpdata.pointCoords);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        gp = (*env)->NewObject(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                               sunFontIDs.gpClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
                               sunFontIDs.gpCtr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                               gpdata.wr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
                               types,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
                               gpdata.numTypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                               coords,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
                               gpdata.numCoords);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
    freeGP(&gpdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
    return gp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
 * Class:     sun_font_FreetypeFontScaler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
 * Method:    getGlyphOutlineNative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
 * Signature: (Lsun/font/Font2D;JIFF)Ljava/awt/geom/GeneralPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
JNIEXPORT jobject JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
Java_sun_font_FreetypeFontScaler_getGlyphOutlineNative(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
      JNIEnv *env, jobject scaler, jobject font2D, jlong pScalerContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
      jlong pScaler, jint glyphCode, jfloat xpos, jfloat ypos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
    FTScalerContext *context =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
         (FTScalerContext*) jlong_to_ptr(pScalerContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
    FTScalerInfo* scalerInfo = (FTScalerInfo *) jlong_to_ptr(pScaler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
    jobject gp = getGlyphGeneralPath(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
                               font2D,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                               context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                               scalerInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                               glyphCode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                               xpos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                               ypos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
    if (gp == NULL) { /* can be legal */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
        gp = (*env)->NewObject(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
                               sunFontIDs.gpClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
                               sunFontIDs.gpCtrEmpty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
    return gp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
 * Class:     sun_font_FreetypeFontScaler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
 * Method:    getGlyphOutlineBoundsNative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
 * Signature: (Lsun/font/Font2D;JI)Ljava/awt/geom/Rectangle2D/Float;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
JNIEXPORT jobject JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
Java_sun_font_FreetypeFontScaler_getGlyphOutlineBoundsNative(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        JNIEnv *env, jobject scaler, jobject font2D,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
        jlong pScalerContext, jlong pScaler, jint glyphCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
    FT_Outline *outline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
    FT_BBox bbox;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
    int error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
    jobject bounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
    FTScalerContext *context =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
         (FTScalerContext*) jlong_to_ptr(pScalerContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
    FTScalerInfo* scalerInfo = (FTScalerInfo *) jlong_to_ptr(pScaler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
    outline = getFTOutline(env, font2D, context, scalerInfo, glyphCode, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
    if (outline == NULL || outline->n_points == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        /* it is legal case, e.g. invisible glyph */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        bounds = (*env)->NewObject(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                                 sunFontIDs.rect2DFloatClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                                 sunFontIDs.rect2DFloatCtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
        return bounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
    error = FT_Outline_Get_BBox(outline, &bbox);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
    //convert bbox
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
    if (error || bbox.xMin >= bbox.xMax || bbox.yMin >= bbox.yMax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        bounds = (*env)->NewObject(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                                   sunFontIDs.rect2DFloatClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                                   sunFontIDs.rect2DFloatCtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        bounds = (*env)->NewObject(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                                   sunFontIDs.rect2DFloatClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                                   sunFontIDs.rect2DFloatCtr4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                                   F26Dot6ToFloat(bbox.xMin),
1733
95c41a86eac9 6761856: OpenJDK: vertical text metrics may be significanly different from those returned by Sun JDK
igor
parents: 881
diff changeset
  1380
                                   F26Dot6ToFloat(-bbox.yMax),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                                   F26Dot6ToFloat(bbox.xMax-bbox.xMin),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                                   F26Dot6ToFloat(bbox.yMax-bbox.yMin));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
    return bounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
 * Class:     sun_font_FreetypeFontScaler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
 * Method:    getGlyphVectorOutlineNative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
 * Signature: (Lsun/font/Font2D;J[IIFF)Ljava/awt/geom/GeneralPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
JNIEXPORT jobject
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
Java_sun_font_FreetypeFontScaler_getGlyphVectorOutlineNative(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        JNIEnv *env, jobject scaler, jobject font2D,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        jlong pScalerContext, jlong pScaler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        jintArray glyphArray, jint numGlyphs, jfloat xpos, jfloat ypos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
    FT_Outline* outline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
    jobject gp = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
    jbyteArray types;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
    jfloatArray coords;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
    GPData gpdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
    jint *glyphs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
    FTScalerContext *context =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
         (FTScalerContext*) jlong_to_ptr(pScalerContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
    FTScalerInfo *scalerInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
             (FTScalerInfo*) jlong_to_ptr(pScaler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
18198
eeec61029574 8009013: Better handling of T2K glyphs
jgodinez
parents: 9761
diff changeset
  1413
    glyphs = NULL;
eeec61029574 8009013: Better handling of T2K glyphs
jgodinez
parents: 9761
diff changeset
  1414
    if (numGlyphs > 0 && 0xffffffffu / sizeof(jint) >= numGlyphs) {
eeec61029574 8009013: Better handling of T2K glyphs
jgodinez
parents: 9761
diff changeset
  1415
        glyphs = (jint*) malloc(numGlyphs*sizeof(jint));
eeec61029574 8009013: Better handling of T2K glyphs
jgodinez
parents: 9761
diff changeset
  1416
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
    if (glyphs == NULL) {
18198
eeec61029574 8009013: Better handling of T2K glyphs
jgodinez
parents: 9761
diff changeset
  1418
        // We reach here if:
eeec61029574 8009013: Better handling of T2K glyphs
jgodinez
parents: 9761
diff changeset
  1419
        // 1. numGlyphs <= 0,
eeec61029574 8009013: Better handling of T2K glyphs
jgodinez
parents: 9761
diff changeset
  1420
        // 2. overflow check failed, or
eeec61029574 8009013: Better handling of T2K glyphs
jgodinez
parents: 9761
diff changeset
  1421
        // 3. malloc failed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        gp = (*env)->NewObject(env, sunFontIDs.gpClass, sunFontIDs.gpCtrEmpty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        return gp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
    (*env)->GetIntArrayRegion(env, glyphArray, 0, numGlyphs, glyphs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
18198
eeec61029574 8009013: Better handling of T2K glyphs
jgodinez
parents: 9761
diff changeset
  1428
    gpdata.numCoords = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
    for (i=0; i<numGlyphs;i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        if (glyphs[i] >= INVISIBLE_GLYPHS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        outline = getFTOutline(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
                               font2D,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
                               context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
                               scalerInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
                               glyphs[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
                               xpos, ypos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
        if (outline == NULL || outline->n_points == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
        gpdata.pointTypes  = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
        gpdata.pointCoords = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
        if (!allocateSpaceForGP(&gpdata, outline->n_points,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
                                outline->n_contours)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
        addToGP(&gpdata, outline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
    free(glyphs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
    if (gpdata.numCoords != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
      types = (*env)->NewByteArray(env, gpdata.numTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
      coords = (*env)->NewFloatArray(env, gpdata.numCoords);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
      if (types && coords) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
        (*env)->SetByteArrayRegion(env, types, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
                                   gpdata.numTypes, gpdata.pointTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
        (*env)->SetFloatArrayRegion(env, coords, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
                                    gpdata.numCoords, gpdata.pointCoords);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
        gp=(*env)->NewObject(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
                             sunFontIDs.gpClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
                             sunFontIDs.gpCtr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
                             gpdata.wr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
                             types,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
                             gpdata.numTypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
                             coords,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
                             gpdata.numCoords);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        return gp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
    return (*env)->NewObject(env, sunFontIDs.gpClass, sunFontIDs.gpCtrEmpty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
Java_sun_font_FreetypeFontScaler_getUnitsPerEMNative(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
        JNIEnv *env, jobject scaler, jlong pScaler) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
    FTScalerInfo *s = (FTScalerInfo* ) jlong_to_ptr(pScaler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
    /* Freetype doc says:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
     The number of font units per EM square for this face.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
     This is typically 2048 for TrueType fonts, and 1000 for Type 1 fonts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
     Only relevant for scalable formats.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
     However, layout engine might be not tested with anything but 2048.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
     NB: test it! */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
    if (s != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
        return s->face->units_per_EM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
    return 2048;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
/* This native method is called by the OpenType layout engine. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
JNIEXPORT jobject JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
Java_sun_font_FreetypeFontScaler_getGlyphPointNative(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
        JNIEnv *env, jobject scaler, jobject font2D, jlong pScalerContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
        jlong pScaler, jint glyphCode, jint pointNumber) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
    FT_Outline* outline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
    jobject point = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
    jfloat x=0, y=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
    FTScalerContext *context =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
         (FTScalerContext*) jlong_to_ptr(pScalerContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
    FTScalerInfo *scalerInfo = (FTScalerInfo*) jlong_to_ptr(pScaler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
    outline = getFTOutline(env, font2D, context, scalerInfo, glyphCode, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
    if (outline != NULL && outline->n_points > pointNumber) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
        x =  F26Dot6ToFloat(outline->points[pointNumber].x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
        y = -F26Dot6ToFloat(outline->points[pointNumber].y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
    return (*env)->NewObject(env, sunFontIDs.pt2DFloatClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
                             sunFontIDs.pt2DFloatCtr, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
}