jdk/src/share/native/sun/font/freetypeScaler.c
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 4243 657b5136de1a
child 5580 629274e4c99f
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4243
diff changeset
     2
 * Copyright (c) 2007, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include<stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <math.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include "ft2build.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include FT_FREETYPE_H
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include FT_GLYPH_H
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include FT_BBOX_H
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include FT_SIZES_H
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#include FT_OUTLINE_H
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#include FT_SYNTHESIS_H
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#include "fontscaler.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#define  ftFixed1  (FT_Fixed) (1 << 16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#define  FloatToFTFixed(f) (FT_Fixed)((f) * (float)(ftFixed1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
#define  FTFixedToFloat(x) ((x) / (float)(ftFixed1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
#define  FT26Dot6ToFloat(x)  ((x) / ((float) (1<<6)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
#define  ROUND(x) ((int) (x+0.5))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
typedef struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /* Important note:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
         JNI forbids sharing same env between different threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
         We are safe, because pointer is overwritten every time we get into
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
         JNI call (see setupFTContext).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
         Pointer is used by font data reading callbacks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
         such as ReadTTFontFileFunc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
         NB: We may consider switching to JNI_GetEnv. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    JNIEnv* env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    FT_Library library;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    FT_Face face;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    jobject font2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    jobject directBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    unsigned char* fontData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    unsigned fontDataOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    unsigned fontDataLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    unsigned fileSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    TTLayoutTableCache* layoutTables;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
} FTScalerInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
typedef struct FTScalerContext {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    FT_Matrix  transform;     /* glyph transform, including device transform */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    jboolean   useSbits;      /* sbit usage enabled? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    jint       aaType;        /* antialiasing mode (off/on/grey/lcd) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    jint       fmType;        /* fractional metrics - on/off */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    jboolean   doBold;        /* perform algorithmic bolding? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    jboolean   doItalize;     /* perform algorithmic italicizing? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    int        renderFlags;   /* configuration specific to particular engine */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    int        pathType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    int        ptsz;          /* size in points */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
} FTScalerContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
/* These are referenced in the freetype sources if DEBUG macro is defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
   To simplify work with debuging version of freetype we define
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
   them here. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
int z_verbose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
void z_error(char *s) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
/**************** Error handling utilities *****************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
static jmethodID invalidateScalerMID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
Java_sun_font_FreetypeFontScaler_initIDs(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        JNIEnv *env, jobject scaler, jclass FFSClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    invalidateScalerMID =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        (*env)->GetMethodID(env, FFSClass, "invalidateScaler", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
static void freeNativeResources(JNIEnv *env, FTScalerInfo* scalerInfo) {
4243
657b5136de1a 6887292: memory leak in freetypeScaler.c
igor
parents: 2369
diff changeset
   105
    void *stream;
657b5136de1a 6887292: memory leak in freetypeScaler.c
igor
parents: 2369
diff changeset
   106
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    if (scalerInfo == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
4243
657b5136de1a 6887292: memory leak in freetypeScaler.c
igor
parents: 2369
diff changeset
   110
    //apparently Done_Face will only close the stream
657b5136de1a 6887292: memory leak in freetypeScaler.c
igor
parents: 2369
diff changeset
   111
    // but will not relase the memory of stream structure.
657b5136de1a 6887292: memory leak in freetypeScaler.c
igor
parents: 2369
diff changeset
   112
    // We need to free it explicitly to avoid leak.
657b5136de1a 6887292: memory leak in freetypeScaler.c
igor
parents: 2369
diff changeset
   113
    //Direct access to the stream field might be not ideal solution as
657b5136de1a 6887292: memory leak in freetypeScaler.c
igor
parents: 2369
diff changeset
   114
    // it is considred to be "private".
657b5136de1a 6887292: memory leak in freetypeScaler.c
igor
parents: 2369
diff changeset
   115
    //Alternatively we could have stored pointer to the structure
657b5136de1a 6887292: memory leak in freetypeScaler.c
igor
parents: 2369
diff changeset
   116
    // in the scalerInfo but this will increase size of the structure
657b5136de1a 6887292: memory leak in freetypeScaler.c
igor
parents: 2369
diff changeset
   117
    // for no good reason
657b5136de1a 6887292: memory leak in freetypeScaler.c
igor
parents: 2369
diff changeset
   118
    stream = scalerInfo->face->stream;
657b5136de1a 6887292: memory leak in freetypeScaler.c
igor
parents: 2369
diff changeset
   119
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    FT_Done_Face(scalerInfo->face);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    FT_Done_FreeType(scalerInfo->library);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    if (scalerInfo->directBuffer != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        (*env)->DeleteGlobalRef(env, scalerInfo->directBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    if (scalerInfo->fontData != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        free(scalerInfo->fontData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
4243
657b5136de1a 6887292: memory leak in freetypeScaler.c
igor
parents: 2369
diff changeset
   131
   if (stream != NULL) {
657b5136de1a 6887292: memory leak in freetypeScaler.c
igor
parents: 2369
diff changeset
   132
        free(stream);
657b5136de1a 6887292: memory leak in freetypeScaler.c
igor
parents: 2369
diff changeset
   133
   }
657b5136de1a 6887292: memory leak in freetypeScaler.c
igor
parents: 2369
diff changeset
   134
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    free(scalerInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
/* invalidates state of java scaler object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
static void invalidateJavaScaler(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                                 jobject scaler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                 FTScalerInfo* scalerInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    freeNativeResources(env, scalerInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    (*env)->CallVoidMethod(env, scaler, invalidateScalerMID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
/******************* I/O handlers ***************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
#define FILEDATACACHESIZE 1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
/* NB: is it ever called? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
static void CloseTTFontFileFunc(FT_Stream stream) {
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
    jclass tmpClass = (*env)->FindClass(env, "sun/font/TrueTypeFont");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    jfieldID platNameField =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
         (*env)->GetFieldID(env, tmpClass, "platName", "Ljava/lang/String;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    jstring platName = (*env)->GetObjectField(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                                              scalerInfo->font2D,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                                              platNameField);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    const char *name = JNU_GetStringPlatformChars(env, platName, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    JNU_ReleaseStringPlatformChars(env, platName, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
static unsigned long ReadTTFontFileFunc(FT_Stream stream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                                        unsigned long offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                                        unsigned char* destBuffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                                        unsigned long numBytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    FTScalerInfo *scalerInfo = (FTScalerInfo *) stream->pathname.pointer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    JNIEnv* env = scalerInfo->env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    jobject bBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    int bread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    if (numBytes == 0) return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /* Large reads will bypass the cache and data copying */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    if (numBytes > FILEDATACACHESIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        bBuffer = (*env)->NewDirectByteBuffer(env, destBuffer, numBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (bBuffer != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            /* Loop until the read succeeds (or EOF).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
             * This should improve robustness in the event of a problem in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
             * the I/O system. If we find that we ever end up spinning here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
             * we are going to have to do some serious work to recover.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
             * Just returning without reading the data will cause a crash.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            while (bread == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                bread = (*env)->CallIntMethod(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                              scalerInfo->font2D,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                                              sunFontIDs.ttReadBlockMID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                                              bBuffer, offset, numBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            return bread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            /* We probably hit bug bug 4845371. For reasons that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
             * are currently unclear, the call stacks after the initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
             * createScaler call that read large amounts of data seem to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
             * be OK and can create the byte buffer above, but this code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
             * is here just in case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
             * 4845371 is fixed now so I don't expect this code path to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
             * ever get called but its harmless to leave it here on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
             * small chance its needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            jbyteArray byteArray = (jbyteArray)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            (*env)->CallObjectMethod(env, scalerInfo->font2D,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                     sunFontIDs.ttReadBytesMID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                                     offset, numBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            (*env)->GetByteArrayRegion(env, byteArray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                                       0, numBytes, (jbyte*)destBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            return numBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    } /* Do we have a cache hit? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
      else if (scalerInfo->fontDataOffset <= offset &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        scalerInfo->fontDataOffset + scalerInfo->fontDataLength >=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                                         offset + numBytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        unsigned cacheOffset = offset - scalerInfo->fontDataOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        memcpy(destBuffer, scalerInfo->fontData+(size_t)cacheOffset, numBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        return numBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        /* Must fill the cache */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        scalerInfo->fontDataOffset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        scalerInfo->fontDataLength =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                 (offset + FILEDATACACHESIZE > scalerInfo->fileSize) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                 scalerInfo->fileSize - offset : FILEDATACACHESIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        bBuffer = scalerInfo->directBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        /* Loop until all the read succeeds (or EOF).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
         * This should improve robustness in the event of a problem in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
         * the I/O system. If we find that we ever end up spinning here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
         * we are going to have to do some serious work to recover.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
         * Just returning without reading the data will cause a crash.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        while (bread == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            bread = (*env)->CallIntMethod(env, scalerInfo->font2D,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                                          sunFontIDs.ttReadBlockMID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                                          bBuffer, offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                                          scalerInfo->fontDataLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        memcpy(destBuffer, scalerInfo->fontData, numBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return numBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
 * Class:     sun_font_FreetypeFontScaler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
 * Method:    initNativeScaler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 * Signature: (Lsun/font/Font2D;IIZI)J
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
Java_sun_font_FreetypeFontScaler_initNativeScaler(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        JNIEnv *env, jobject scaler, jobject font2D, jint type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        jint indexInCollection, jboolean supportsCJK, jint filesize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    FTScalerInfo* scalerInfo = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    FT_Stream ftstream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    FT_Open_Args ft_open_args;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    int error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    jobject bBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    scalerInfo = (FTScalerInfo*) calloc(1, sizeof(FTScalerInfo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    if (scalerInfo == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    scalerInfo->env = env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    scalerInfo->font2D = font2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    scalerInfo->fontDataOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    scalerInfo->fontDataLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    scalerInfo->fileSize = filesize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
       We can consider sharing freetype library between different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
       scalers. However, Freetype docs suggest to use different libraries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
       for different threads. Also, our architecture implies that single
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
       FontScaler object is shared for for different sizes/transforms/styles
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
       of the same font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
       On other hand these methods can not be concurrently executed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
       becaused they are "synchronized" in java.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    error = FT_Init_FreeType(&scalerInfo->library);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        free(scalerInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
#define TYPE1_FROM_JAVA        2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    error = 1; /* triggers memory freeing unless we clear it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    if (type == TYPE1_FROM_JAVA) { /* TYPE1 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        scalerInfo->fontData = (unsigned char*) malloc(filesize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        scalerInfo->directBuffer = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        scalerInfo->layoutTables = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        scalerInfo->fontDataLength = filesize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if (scalerInfo->fontData != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            bBuffer = (*env)->NewDirectByteBuffer(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                                              scalerInfo->fontData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                                              scalerInfo->fontDataLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            if (bBuffer != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                (*env)->CallObjectMethod(env, font2D,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                                   sunFontIDs.readFileMID, bBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                error = FT_New_Memory_Face(scalerInfo->library,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                                   scalerInfo->fontData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                                   scalerInfo->fontDataLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                                   indexInCollection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                                   &scalerInfo->face);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    } else { /* Truetype */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        scalerInfo->fontData = (unsigned char*) malloc(FILEDATACACHESIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        ftstream = (FT_Stream) calloc(1, sizeof(FT_StreamRec));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        if (ftstream != NULL && scalerInfo->fontData != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            scalerInfo->directBuffer = (*env)->NewDirectByteBuffer(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                                        scalerInfo->fontData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                                        FILEDATACACHESIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            if (scalerInfo->directBuffer != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                scalerInfo->directBuffer = (*env)->NewGlobalRef(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                                            scalerInfo->directBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                ftstream->base = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                ftstream->size = filesize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                ftstream->pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                ftstream->read = (FT_Stream_IoFunc) ReadTTFontFileFunc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                ftstream->close = (FT_Stream_CloseFunc) CloseTTFontFileFunc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                ftstream->pathname.pointer = (void *) scalerInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                memset(&ft_open_args, 0, sizeof(FT_Open_Args));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                ft_open_args.flags = FT_OPEN_STREAM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                ft_open_args.stream = ftstream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                error = FT_Open_Face(scalerInfo->library,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                                     &ft_open_args,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                                     indexInCollection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                                     &scalerInfo->face);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
           if (error || scalerInfo->directBuffer == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
               free(ftstream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        FT_Done_FreeType(scalerInfo->library);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        if (scalerInfo->directBuffer != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            (*env)->DeleteGlobalRef(env, scalerInfo->directBuffer);
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
            free(scalerInfo->fontData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        free(scalerInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    return ptr_to_jlong(scalerInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
static double euclidianDistance(double a, double b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    if (a < 0) a=-a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    if (b < 0) b=-b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    if (a == 0) return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    if (b == 0) return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    return sqrt(a*a+b*b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
Java_sun_font_FreetypeFontScaler_createScalerContextNative(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        JNIEnv *env, jobject scaler, jlong pScaler, jdoubleArray matrix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        jboolean ttFont, jint aa, jint fm, jfloat boldness, jfloat italic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    double dmat[4], ptsz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    FTScalerContext *context =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            (FTScalerContext*) calloc(1, sizeof(FTScalerContext));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    FTScalerInfo *scalerInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
             (FTScalerInfo*) jlong_to_ptr(pScaler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    if (context == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        invalidateJavaScaler(env, scaler, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        return (jlong) 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    (*env)->GetDoubleArrayRegion(env, matrix, 0, 4, dmat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    ptsz = euclidianDistance(dmat[2], dmat[3]); //i.e. y-size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    if (ptsz < 1.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        //text can not be smaller than 1 point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        ptsz = 1.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
554
88eb2812c2a4 6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents: 2
diff changeset
   387
    context->ptsz = (int)(ptsz * 64);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    context->transform.xx =  FloatToFTFixed((float)dmat[0]/ptsz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    context->transform.yx = -FloatToFTFixed((float)dmat[1]/ptsz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    context->transform.xy = -FloatToFTFixed((float)dmat[2]/ptsz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    context->transform.yy =  FloatToFTFixed((float)dmat[3]/ptsz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    context->aaType = aa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    context->fmType = fm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /* If using algorithmic styling, the base values are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * boldness = 1.0, italic = 0.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    context->doBold = (boldness != 1.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    context->doItalize = (italic != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    return ptr_to_jlong(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
static int setupFTContext(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                          jobject font2D,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                          FTScalerInfo *scalerInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                          FTScalerContext *context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    int errCode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    scalerInfo->env = env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    scalerInfo->font2D = font2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
2369
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
   413
    if (context != NULL) {
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
   414
        FT_Set_Transform(scalerInfo->face, &context->transform, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
2369
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
   416
        errCode = FT_Set_Char_Size(scalerInfo->face, 0, context->ptsz, 72, 72);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
2369
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
   418
        if (errCode == 0) {
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
   419
            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
   420
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    return errCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
/* ftsynth.c uses (0x10000, 0x06000, 0x0, 0x10000) matrix to get oblique
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
   outline.  Therefore x coordinate will change by 0x06000*y.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
   Note that y coordinate does not change. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
#define OBLIQUE_MODIFIER(y)  (context->doItalize ? ((y)*6/16) : 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
 * Class:     sun_font_FreetypeFontScaler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
 * Method:    getFontMetricsNative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
 * Signature: (Lsun/font/Font2D;J)Lsun/font/StrikeMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
JNIEXPORT jobject JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
Java_sun_font_FreetypeFontScaler_getFontMetricsNative(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        JNIEnv *env, jobject scaler, jobject font2D,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        jlong pScalerContext, jlong pScaler) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    jobject metrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    jfloat ax, ay, dx, dy, bx, by, lx, ly, mx, my;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    jfloat f0 = 0.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    FT_Pos bmodifier = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    FTScalerContext *context =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        (FTScalerContext*) jlong_to_ptr(pScalerContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    FTScalerInfo *scalerInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
             (FTScalerInfo*) jlong_to_ptr(pScaler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    int errCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    if (isNullScalerContext(context) || scalerInfo == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        return (*env)->NewObject(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                                 sunFontIDs.strikeMetricsClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                                 sunFontIDs.strikeMetricsCtr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                                 f0, f0, f0, f0, f0, f0, f0, f0, f0, f0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    errCode = setupFTContext(env, font2D, scalerInfo, context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    if (errCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        metrics = (*env)->NewObject(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                                 sunFontIDs.strikeMetricsClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                                 sunFontIDs.strikeMetricsCtr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                                 f0, f0, f0, f0, f0, f0, f0, f0, f0, f0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        invalidateJavaScaler(env, scaler, scalerInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        return metrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    /* This is ugly and has to be reworked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
       Freetype provide means to add style to glyph but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
       it seems there is no way to adjust metrics accordingly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
       So, we have to do adust them explicitly and stay consistent with what
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
       freetype does to outlines. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    /* For bolding glyphs are not just widened. Height is also changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
       (see ftsynth.c).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
       TODO: In vertical direction we could do better job and adjust metrics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
       proportionally to glyoh shape. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    if (context->doBold) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        bmodifier = FT_MulFix(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                       scalerInfo->face->units_per_EM,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                       scalerInfo->face->size->metrics.y_scale)/24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    /**** Note: only some metrics are affected by styling ***/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    /* ascent */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    ax = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    ay = -(jfloat) FT26Dot6ToFloat(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                       scalerInfo->face->size->metrics.ascender +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                       bmodifier/2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    /* descent */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    dx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    dy = -(jfloat) FT26Dot6ToFloat(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                       scalerInfo->face->size->metrics.descender +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                       bmodifier/2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    /* baseline */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    bx = by = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    /* leading */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    lx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    ly = (jfloat) FT26Dot6ToFloat(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                      scalerInfo->face->size->metrics.height +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                      bmodifier) + ay - dy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    /* max advance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    mx = (jfloat) FT26Dot6ToFloat(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                     scalerInfo->face->size->metrics.max_advance +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                     2*bmodifier +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                     OBLIQUE_MODIFIER(scalerInfo->face->size->metrics.height));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    my = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    metrics = (*env)->NewObject(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                                sunFontIDs.strikeMetricsClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                                sunFontIDs.strikeMetricsCtr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                                ax, ay, dx, dy, bx, by, lx, ly, mx, my);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    return metrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
 * Class:     sun_font_FreetypeFontScaler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
 * Method:    getGlyphAdvanceNative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
 * Signature: (Lsun/font/Font2D;JI)F
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
JNIEXPORT jfloat JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
Java_sun_font_FreetypeFontScaler_getGlyphAdvanceNative(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        JNIEnv *env, jobject scaler, jobject font2D,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        jlong pScalerContext, jlong pScaler, jint glyphCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
   /* This method is rarely used because requests for metrics are usually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
      coupled with request for bitmap and to large extend work can be reused
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
      (to find out metrics we need to hint glyph).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
      So, we typically go through getGlyphImage code path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
      For initial freetype implementation we delegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
      all work to getGlyphImage but drop result image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
      This is waste of work related to scan conversion and conversion from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
      freetype format to our format but for now this seems to be ok.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
      NB: investigate performance benefits of refactoring code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
      to avoid unnecesary work with bitmaps. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    GlyphInfo *info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    jfloat advance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    jlong image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    image = Java_sun_font_FreetypeFontScaler_getGlyphImageNative(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                 env, scaler, font2D, pScalerContext, pScaler, glyphCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    info = (GlyphInfo*) jlong_to_ptr(image);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    advance = info->advanceX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    free(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    return advance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
 * Class:     sun_font_FreetypeFontScaler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
 * Method:    getGlyphMetricsNative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
 * Signature: (Lsun/font/Font2D;JILjava/awt/geom/Point2D/Float;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
Java_sun_font_FreetypeFontScaler_getGlyphMetricsNative(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        JNIEnv *env, jobject scaler, jobject font2D, jlong pScalerContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        jlong pScaler, jint glyphCode, jobject metrics) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     /* As initial implementation we delegate all work to getGlyphImage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        but drop result image. This is clearly waste of resorces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        TODO: investigate performance benefits of refactoring code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
              by avoiding bitmap generation and conversion from FT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
              bitmap format. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     GlyphInfo *info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     jlong image = Java_sun_font_FreetypeFontScaler_getGlyphImageNative(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                                 env, scaler, font2D,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                                 pScalerContext, pScaler, glyphCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     info = (GlyphInfo*) jlong_to_ptr(image);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     (*env)->SetFloatField(env, metrics, sunFontIDs.xFID, info->advanceX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     (*env)->SetFloatField(env, metrics, sunFontIDs.yFID, info->advanceY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     free(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
static GlyphInfo* getNullGlyphImage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    GlyphInfo *glyphInfo =  (GlyphInfo*) calloc(1, sizeof(GlyphInfo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    return glyphInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
static void CopyBW2Grey8(const void* srcImage, int srcRowBytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                         void* dstImage, int dstRowBytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                         int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    const UInt8* srcRow = (UInt8*)srcImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    UInt8* dstRow = (UInt8*)dstImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    int wholeByteCount = width >> 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    int remainingBitsCount = width & 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    int i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    while (height--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        const UInt8* src8 = srcRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        UInt8* dstByte = dstRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        unsigned srcValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        srcRow += srcRowBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        dstRow += dstRowBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        for (i = 0; i < wholeByteCount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            srcValue = *src8++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            for (j = 0; j < 8; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                *dstByte++ = (srcValue & 0x80) ? 0xFF : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                srcValue <<= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        if (remainingBitsCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            srcValue = *src8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            for (j = 0; j < remainingBitsCount; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                *dstByte++ = (srcValue & 0x80) ? 0xFF : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                srcValue <<= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
#define Grey4ToAlpha255(value) (((value) << 4) + ((value) >> 3))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
static void CopyGrey4ToGrey8(const void* srcImage, int srcRowBytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                void* dstImage, int dstRowBytes, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     const UInt8* srcRow = (UInt8*) srcImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     UInt8* dstRow = (UInt8*) dstImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     while (height--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
         const UInt8* src8 = srcRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
         UInt8* dstByte = dstRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
         unsigned srcValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
         srcRow += srcRowBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
         dstRow += dstRowBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
         for (i = 0; i < width; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
             srcValue = *src8++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
             *dstByte++ = Grey4ToAlpha255(srcValue & 0x0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
             *dstByte++ = Grey4ToAlpha255(srcValue >> 4);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
/* We need it because FT rows are often padded to 4 byte boundaries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    and our internal format is not padded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
static void CopyFTSubpixelToSubpixel(const void* srcImage, int srcRowBytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                                     void* dstImage, int dstRowBytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                                     int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    unsigned char *srcRow = (unsigned char *) srcImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    unsigned char *dstRow = (unsigned char *) dstImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    while (height--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        memcpy(dstRow, srcRow, width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        srcRow += srcRowBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        dstRow += dstRowBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
/* We need it because FT rows are often padded to 4 byte boundaries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
   and our internal format is not padded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
static void CopyFTSubpixelVToSubpixel(const void* srcImage, int srcRowBytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                                      void* dstImage, int dstRowBytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                                      int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    unsigned char *srcRow = (unsigned char *) srcImage, *srcByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    unsigned char *dstRow = (unsigned char *) dstImage, *dstByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    while (height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        srcByte = srcRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        dstByte = dstRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        for (i = 0; i < width; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            *dstByte++ = *srcByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            *dstByte++ = *(srcByte + srcRowBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            *dstByte++ = *(srcByte + 2*srcRowBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            srcByte++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        srcRow += 3*srcRowBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        dstRow += dstRowBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        height -= 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
 * Class:     sun_font_FreetypeFontScaler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
 * Method:    getGlyphImageNative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
 * Signature: (Lsun/font/Font2D;JI)J
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
Java_sun_font_FreetypeFontScaler_getGlyphImageNative(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        JNIEnv *env, jobject scaler, jobject font2D,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        jlong pScalerContext, jlong pScaler, jint glyphCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    int error, imageSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    UInt16 width, height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    GlyphInfo *glyphInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    int glyph_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    int renderFlags = FT_LOAD_RENDER, target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    FT_GlyphSlot ftglyph;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    FTScalerContext* context =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        (FTScalerContext*) jlong_to_ptr(pScalerContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    FTScalerInfo *scalerInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
             (FTScalerInfo*) jlong_to_ptr(pScaler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    if (isNullScalerContext(context) || scalerInfo == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        return ptr_to_jlong(getNullGlyphImage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    error = setupFTContext(env, font2D, scalerInfo, context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        invalidateJavaScaler(env, scaler, scalerInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        return ptr_to_jlong(getNullGlyphImage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    /* if algorithmic styling is required then we do not request bitmap */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    if (context->doBold || context->doItalize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        renderFlags =  FT_LOAD_DEFAULT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    /* NB: in case of non identity transform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     we might also prefer to disable transform before hinting,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     and apply it explicitly after hinting is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     Or we can disable hinting. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    /* select appropriate hinting mode */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    if (context->aaType == TEXT_AA_OFF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        target = FT_LOAD_TARGET_MONO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    } else if (context->aaType == TEXT_AA_ON) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        target = FT_LOAD_TARGET_NORMAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    } else if (context->aaType == TEXT_AA_LCD_HRGB ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
               context->aaType == TEXT_AA_LCD_HBGR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        target = FT_LOAD_TARGET_LCD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        target = FT_LOAD_TARGET_LCD_V;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    renderFlags |= target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    glyph_index = FT_Get_Char_Index(scalerInfo->face, glyphCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    error = FT_Load_Glyph(scalerInfo->face, glyphCode, renderFlags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        //do not destroy scaler yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        //this can be problem of particular context (e.g. with bad transform)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        return ptr_to_jlong(getNullGlyphImage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    ftglyph = scalerInfo->face->glyph;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    /* apply styles */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    if (context->doBold) { /* if bold style */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        FT_GlyphSlot_Embolden(ftglyph);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    if (context->doItalize) { /* if oblique */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        FT_GlyphSlot_Oblique(ftglyph);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    /* generate bitmap if it is not done yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     e.g. if algorithmic styling is performed and style was added to outline */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    if (ftglyph->format == FT_GLYPH_FORMAT_OUTLINE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        FT_Render_Glyph(ftglyph, FT_LOAD_TARGET_MODE(target));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    width  = (UInt16) ftglyph->bitmap.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    height = (UInt16) ftglyph->bitmap.rows;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    imageSize = width*height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    glyphInfo = (GlyphInfo*) malloc(sizeof(GlyphInfo) + imageSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    if (glyphInfo == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        glyphInfo = getNullGlyphImage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        return ptr_to_jlong(glyphInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    glyphInfo->cellInfo  = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    glyphInfo->rowBytes  = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    glyphInfo->width     = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    glyphInfo->height    = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    glyphInfo->topLeftX  = (float)  ftglyph->bitmap_left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    glyphInfo->topLeftY  = (float) -ftglyph->bitmap_top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
881
6aefebba2393 6587560: OpenJDK problem handling bitmaps returned when LCD text is requested
igor
parents: 561
diff changeset
   791
    if (ftglyph->bitmap.pixel_mode ==  FT_PIXEL_MODE_LCD) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        glyphInfo->width = width/3;
881
6aefebba2393 6587560: OpenJDK problem handling bitmaps returned when LCD text is requested
igor
parents: 561
diff changeset
   793
    } else if (ftglyph->bitmap.pixel_mode ==  FT_PIXEL_MODE_LCD_V) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        glyphInfo->height = glyphInfo->height/3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    if (context->fmType == TEXT_FM_ON) {
554
88eb2812c2a4 6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents: 2
diff changeset
   798
        double advh = FTFixedToFloat(ftglyph->linearHoriAdvance);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        glyphInfo->advanceX =
554
88eb2812c2a4 6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents: 2
diff changeset
   800
            (float) (advh * FTFixedToFloat(context->transform.xx));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        glyphInfo->advanceY =
554
88eb2812c2a4 6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents: 2
diff changeset
   802
            (float) (advh * FTFixedToFloat(context->transform.xy));
88eb2812c2a4 6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents: 2
diff changeset
   803
    } else {
88eb2812c2a4 6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents: 2
diff changeset
   804
        if (!ftglyph->advance.y) {
88eb2812c2a4 6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents: 2
diff changeset
   805
            glyphInfo->advanceX =
88eb2812c2a4 6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents: 2
diff changeset
   806
                (float) ROUND(FT26Dot6ToFloat(ftglyph->advance.x));
88eb2812c2a4 6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents: 2
diff changeset
   807
            glyphInfo->advanceY = 0;
88eb2812c2a4 6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents: 2
diff changeset
   808
        } else if (!ftglyph->advance.x) {
88eb2812c2a4 6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents: 2
diff changeset
   809
            glyphInfo->advanceX = 0;
88eb2812c2a4 6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents: 2
diff changeset
   810
            glyphInfo->advanceY =
88eb2812c2a4 6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents: 2
diff changeset
   811
                (float) ROUND(FT26Dot6ToFloat(-ftglyph->advance.y));
88eb2812c2a4 6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents: 2
diff changeset
   812
        } else {
88eb2812c2a4 6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents: 2
diff changeset
   813
            glyphInfo->advanceX = FT26Dot6ToFloat(ftglyph->advance.x);
88eb2812c2a4 6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents: 2
diff changeset
   814
            glyphInfo->advanceY = FT26Dot6ToFloat(-ftglyph->advance.y);
88eb2812c2a4 6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents: 2
diff changeset
   815
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    if (imageSize == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        glyphInfo->image = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        glyphInfo->image = (unsigned char*) glyphInfo + sizeof(GlyphInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        //convert result to output format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        //output format is either 3 bytes per pixel (for subpixel modes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        // or 1 byte per pixel for AA and B&W
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        if (ftglyph->bitmap.pixel_mode ==  FT_PIXEL_MODE_MONO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            /* convert from 8 pixels per byte to 1 byte per pixel */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            CopyBW2Grey8(ftglyph->bitmap.buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                         ftglyph->bitmap.pitch,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                         (void *) glyphInfo->image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                         width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                         width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                         height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        } else if (ftglyph->bitmap.pixel_mode ==  FT_PIXEL_MODE_GRAY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            /* byte per pixel to byte per pixel => just copy */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            memcpy(glyphInfo->image, ftglyph->bitmap.buffer, imageSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        } else if (ftglyph->bitmap.pixel_mode ==  FT_PIXEL_MODE_GRAY4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            /* 4 bits per pixel to byte per pixel */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            CopyGrey4ToGrey8(ftglyph->bitmap.buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                             ftglyph->bitmap.pitch,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                             (void *) glyphInfo->image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                             width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                             width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                             height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        } else if (ftglyph->bitmap.pixel_mode ==  FT_PIXEL_MODE_LCD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            /* 3 bytes per pixel to 3 bytes per pixel */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            CopyFTSubpixelToSubpixel(ftglyph->bitmap.buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                                     ftglyph->bitmap.pitch,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                                     (void *) glyphInfo->image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                                     width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                                     width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                                     height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        } else if (ftglyph->bitmap.pixel_mode ==  FT_PIXEL_MODE_LCD_V) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            /* 3 bytes per pixel to 3 bytes per pixel */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            CopyFTSubpixelVToSubpixel(ftglyph->bitmap.buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                                      ftglyph->bitmap.pitch,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                                      (void *) glyphInfo->image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                                      width*3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                                      width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                                      height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            glyphInfo->rowBytes *=3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            free(glyphInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            glyphInfo = getNullGlyphImage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    return ptr_to_jlong(glyphInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
 * Class:     sun_font_FreetypeFontScaler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
 * Method:    getLayoutTableCacheNative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
 * Signature: (J)J
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
Java_sun_font_FreetypeFontScaler_getLayoutTableCacheNative(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        JNIEnv *env, jobject scaler, jlong pScaler) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    FTScalerInfo *scalerInfo = (FTScalerInfo*) jlong_to_ptr(pScaler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    if (scalerInfo == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        invalidateJavaScaler(env, scaler, scalerInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        return 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
    // init layout table cache in font
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    // we're assuming the font is a file font and moreover it is Truetype font
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    // otherwise we shouldn't be able to get here...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    if (scalerInfo->layoutTables == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        scalerInfo->layoutTables = newLayoutTableCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    return ptr_to_jlong(scalerInfo->layoutTables);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
 * Class:     sun_font_FreetypeFontScaler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
 * Method:    disposeNativeScaler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
 * Signature: (J)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
Java_sun_font_FreetypeFontScaler_disposeNativeScaler(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        JNIEnv *env, jobject scaler, jlong pScaler) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    FTScalerInfo* scalerInfo = (FTScalerInfo *) jlong_to_ptr(pScaler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
2369
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
   906
    /* 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
   907
       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
   908
       NB: scaler context is not important at this point, can use NULL. */
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
   909
    int errCode = setupFTContext(env, scaler, scalerInfo, NULL);
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
   910
    if (errCode) {
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
   911
        return;
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
   912
    }
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
   913
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    freeNativeResources(env, scalerInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
 * Class:     sun_font_FreetypeFontScaler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
 * Method:    getNumGlyphsNative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
 * Signature: ()I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
Java_sun_font_FreetypeFontScaler_getNumGlyphsNative(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        JNIEnv *env, jobject scaler, jlong pScaler) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    FTScalerInfo* scalerInfo = (FTScalerInfo *) jlong_to_ptr(pScaler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    if (scalerInfo == NULL || scalerInfo->face == NULL) { /* bad/null scaler */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        /* null scaler can render 1 glyph - "missing glyph" with code 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
           (all glyph codes requested by user are mapped to code 0 at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
           validation step) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        invalidateJavaScaler(env, scaler, scalerInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        return (jint) 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    return (jint) scalerInfo->face->num_glyphs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
 * Class:     sun_font_FreetypeFontScaler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
 * Method:    getMissingGlyphCodeNative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
 * Signature: ()I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
Java_sun_font_FreetypeFontScaler_getMissingGlyphCodeNative(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        JNIEnv *env, jobject scaler, jlong pScaler) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
    /* Is it always 0 for freetype? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
 * Class:     sun_font_FreetypeFontScaler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
 * Method:    getGlyphCodeNative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
 * Signature: (C)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
Java_sun_font_FreetypeFontScaler_getGlyphCodeNative(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        JNIEnv *env, jobject scaler, jlong pScaler, jchar charCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    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
   961
    int errCode;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    if (scaler == NULL || scalerInfo->face == NULL) { /* bad/null scaler */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        invalidateJavaScaler(env, scaler, scalerInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
2369
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
   968
    /* 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
   969
       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
   970
       Scaler context is not important here, can use NULL. */
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
   971
    errCode = setupFTContext(env, scaler, scalerInfo, NULL);
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
   972
    if (errCode) {
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
   973
        return 0;
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
   974
    }
b58cdc0321f5 6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents: 1733
diff changeset
   975
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
    return FT_Get_Char_Index(scalerInfo->face, charCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
#define FloatToF26Dot6(x) ((unsigned int) ((x)*64))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
static FT_Outline* getFTOutline(JNIEnv* env, jobject font2D,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        FTScalerContext *context, FTScalerInfo* scalerInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        jint glyphCode, jfloat xpos, jfloat ypos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    int renderFlags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
    int glyph_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    FT_Error error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    FT_GlyphSlot ftglyph;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
    if (glyphCode >= INVISIBLE_GLYPHS ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
            isNullScalerContext(context) || scalerInfo == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    error = setupFTContext(env, font2D, scalerInfo, context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    renderFlags = FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
    glyph_index = FT_Get_Char_Index(scalerInfo->face, glyphCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
    error = FT_Load_Glyph(scalerInfo->face, glyphCode, renderFlags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
    if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    ftglyph = scalerInfo->face->glyph;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    /* apply styles */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    if (context->doBold) { /* if bold style */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        FT_GlyphSlot_Embolden(ftglyph);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    if (context->doItalize) { /* if oblique */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        FT_GlyphSlot_Oblique(ftglyph);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
    FT_Outline_Translate(&ftglyph->outline,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                         FloatToF26Dot6(xpos),
561
977a2ff62fc9 6703377: freetype: glyph vector outline is not translated correctly
igor
parents: 554
diff changeset
  1021
                         -FloatToF26Dot6(ypos));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
    return &ftglyph->outline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
#define F26Dot6ToFloat(n) (((float)(n))/((float) 64))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
/* Types of GeneralPath segments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
   TODO: pull constants from other place? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
#define SEG_UNKNOWN -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
#define SEG_MOVETO   0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
#define SEG_LINETO   1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
#define SEG_QUADTO   2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
#define SEG_CUBICTO  3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
#define SEG_CLOSE    4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
#define WIND_NON_ZERO 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
#define WIND_EVEN_ODD 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
/* Placeholder to accumulate GeneralPath data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
typedef struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    jint numTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
    jint numCoords;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    jint lenTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
    jint lenCoords;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
    jint wr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    jbyte* pointTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    jfloat* pointCoords;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
} GPData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
/* returns 0 on failure */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
static int allocateSpaceForGP(GPData* gpdata, int npoints, int ncontours) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
    int maxTypes, maxCoords;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
    /* we may have up to N intermediate points per contour
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
       (and for each point can actually cause new curve to be generated)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
       In addition we can also have 2 extra point per outline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
    maxTypes  = 2*npoints  + 2*ncontours;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
    maxCoords = 4*(npoints + 2*ncontours); //we may need to insert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                                           //up to n-1 intermediate points
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
    /* first usage - allocate space and intialize all fields */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
    if (gpdata->pointTypes == NULL || gpdata->pointCoords == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        gpdata->lenTypes  = maxTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        gpdata->lenCoords = maxCoords;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        gpdata->pointTypes  = (jbyte*)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
             malloc(gpdata->lenTypes*sizeof(jbyte));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        gpdata->pointCoords = (jfloat*)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
             malloc(gpdata->lenCoords*sizeof(jfloat));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        gpdata->numTypes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        gpdata->numCoords = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        gpdata->wr = WIND_NON_ZERO; /* By default, outlines are filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                                       using the non-zero winding rule. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        /* do we have enough space? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        if (gpdata->lenTypes - gpdata->numTypes < maxTypes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
            gpdata->lenTypes  += maxTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            gpdata->pointTypes  = (jbyte*)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
              realloc(gpdata->pointTypes, gpdata->lenTypes*sizeof(jbyte));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        if (gpdata->lenCoords - gpdata->numCoords < maxCoords) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            gpdata->lenCoords += maxCoords;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            gpdata->pointCoords = (jfloat*)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
              realloc(gpdata->pointCoords, gpdata->lenCoords*sizeof(jfloat));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
    /* failure if any of mallocs failed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
    if (gpdata->pointTypes == NULL ||  gpdata->pointCoords == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
static void addToGP(GPData* gpdata, FT_Outline*outline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
    jbyte current_type=SEG_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
    int i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
    jfloat x, y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
    j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
    for(i=0; i<outline->n_points; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        x =  F26Dot6ToFloat(outline->points[i].x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        y = -F26Dot6ToFloat(outline->points[i].y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        if (FT_CURVE_TAG(outline->tags[i]) == FT_CURVE_TAG_ON) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            /* If bit 0 is unset, the point is "off" the curve,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
             i.e., a Bezier control point, while it is "on" when set. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            if (current_type == SEG_UNKNOWN) { /* special case:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                                                  very first point */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                /* add segment */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                gpdata->pointTypes[gpdata->numTypes++] = SEG_MOVETO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                current_type = SEG_LINETO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                gpdata->pointTypes[gpdata->numTypes++] = current_type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                current_type = SEG_LINETO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            if (current_type == SEG_UNKNOWN) { /* special case:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                                                   very first point */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                if (FT_CURVE_TAG(outline->tags[i+1]) == FT_CURVE_TAG_ON) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                    /* just skip first point. Adhoc heuristic? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                    x = (x + F26Dot6ToFloat(outline->points[i+1].x))/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                    y = (y - F26Dot6ToFloat(outline->points[i+1].y))/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                    gpdata->pointTypes[gpdata->numTypes++] = SEG_MOVETO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                    current_type = SEG_LINETO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            } else if (FT_CURVE_TAG(outline->tags[i]) == FT_CURVE_TAG_CUBIC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                /* Bit 1 is meaningful for ‘off’ points only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                   If set, it indicates a third-order Bezier arc control
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                   point; and a second-order control point if unset.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                current_type = SEG_CUBICTO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                /* two successive conic "off" points forces the rasterizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                   to create (during the scan-line conversion process
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                   exclusively) a virtual "on" point amidst them, at their
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
                   exact middle. This greatly facilitates the definition of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                   successive conic Bezier arcs.  Moreover, it is the way
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                   outlines are described in the TrueType specification. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                if (current_type == SEG_QUADTO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                    gpdata->pointCoords[gpdata->numCoords++] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                        F26Dot6ToFloat(outline->points[i].x +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                        outline->points[i-1].x)/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                    gpdata->pointCoords[gpdata->numCoords++] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
                        - F26Dot6ToFloat(outline->points[i].y +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                        outline->points[i-1].y)/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                    gpdata->pointTypes[gpdata->numTypes++] = SEG_QUADTO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                current_type = SEG_QUADTO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        gpdata->pointCoords[gpdata->numCoords++] = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        gpdata->pointCoords[gpdata->numCoords++] = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        if (outline->contours[j] == i) { //end of contour
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            int start = j > 0 ? outline->contours[j-1]+1 : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            gpdata->pointTypes[gpdata->numTypes++] = current_type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            if (current_type == SEG_QUADTO &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
            FT_CURVE_TAG(outline->tags[start]) != FT_CURVE_TAG_ON) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                gpdata->pointCoords[gpdata->numCoords++] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                            (F26Dot6ToFloat(outline->points[start].x) + x)/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                gpdata->pointCoords[gpdata->numCoords++] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
                            (-F26Dot6ToFloat(outline->points[start].y) + y)/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                gpdata->pointCoords[gpdata->numCoords++] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                            F26Dot6ToFloat(outline->points[start].x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
                gpdata->pointCoords[gpdata->numCoords++] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
                            -F26Dot6ToFloat(outline->points[start].y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            gpdata->pointTypes[gpdata->numTypes++] = SEG_CLOSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            current_type = SEG_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
            j++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
    /* If set to 1, the outline will be filled using the even-odd fill rule */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
    if (outline->flags & FT_OUTLINE_EVEN_ODD_FILL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        gpdata->wr = WIND_EVEN_ODD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
static void freeGP(GPData* gpdata) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    if (gpdata->pointCoords != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        free(gpdata->pointCoords);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        gpdata->pointCoords = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        gpdata->numCoords = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        gpdata->lenCoords = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
    if (gpdata->pointTypes != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        free(gpdata->pointTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        gpdata->pointTypes = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        gpdata->numTypes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        gpdata->lenTypes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
static jobject getGlyphGeneralPath(JNIEnv* env, jobject font2D,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        FTScalerContext *context, FTScalerInfo *scalerInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        jint glyphCode, jfloat xpos, jfloat ypos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
    FT_Outline* outline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
    jobject gp = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
    jbyteArray types;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
    jfloatArray coords;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    GPData gpdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
    outline = getFTOutline(env, font2D, context, scalerInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                           glyphCode, xpos, ypos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
    if (outline == NULL || outline->n_points == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        return gp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
    gpdata.pointTypes  = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
    gpdata.pointCoords = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
    if (!allocateSpaceForGP(&gpdata, outline->n_points, outline->n_contours)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        return gp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
    addToGP(&gpdata, outline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
    types  = (*env)->NewByteArray(env, gpdata.numTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
    coords = (*env)->NewFloatArray(env, gpdata.numCoords);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
    if (types && coords) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        (*env)->SetByteArrayRegion(env, types, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                                   gpdata.numTypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                                   gpdata.pointTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        (*env)->SetFloatArrayRegion(env, coords, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                                    gpdata.numCoords,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
                                    gpdata.pointCoords);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        gp = (*env)->NewObject(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                               sunFontIDs.gpClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                               sunFontIDs.gpCtr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                               gpdata.wr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
                               types,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
                               gpdata.numTypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
                               coords,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
                               gpdata.numCoords);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
    freeGP(&gpdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
    return gp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
 * Class:     sun_font_FreetypeFontScaler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
 * Method:    getGlyphOutlineNative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
 * Signature: (Lsun/font/Font2D;JIFF)Ljava/awt/geom/GeneralPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
JNIEXPORT jobject JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
Java_sun_font_FreetypeFontScaler_getGlyphOutlineNative(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
      JNIEnv *env, jobject scaler, jobject font2D, jlong pScalerContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
      jlong pScaler, jint glyphCode, jfloat xpos, jfloat ypos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
    FTScalerContext *context =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
         (FTScalerContext*) jlong_to_ptr(pScalerContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
    FTScalerInfo* scalerInfo = (FTScalerInfo *) jlong_to_ptr(pScaler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
    jobject gp = getGlyphGeneralPath(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
                               font2D,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
                               context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                               scalerInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                               glyphCode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
                               xpos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                               ypos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
    if (gp == NULL) { /* can be legal */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        gp = (*env)->NewObject(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                               sunFontIDs.gpClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                               sunFontIDs.gpCtrEmpty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
    return gp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
 * Class:     sun_font_FreetypeFontScaler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
 * Method:    getGlyphOutlineBoundsNative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
 * Signature: (Lsun/font/Font2D;JI)Ljava/awt/geom/Rectangle2D/Float;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
JNIEXPORT jobject JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
Java_sun_font_FreetypeFontScaler_getGlyphOutlineBoundsNative(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        JNIEnv *env, jobject scaler, jobject font2D,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        jlong pScalerContext, jlong pScaler, jint glyphCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
    FT_Outline *outline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
    FT_BBox bbox;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
    int error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
    jobject bounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
    FTScalerContext *context =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
         (FTScalerContext*) jlong_to_ptr(pScalerContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
    FTScalerInfo* scalerInfo = (FTScalerInfo *) jlong_to_ptr(pScaler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
    outline = getFTOutline(env, font2D, context, scalerInfo, glyphCode, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
    if (outline == NULL || outline->n_points == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        /* it is legal case, e.g. invisible glyph */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        bounds = (*env)->NewObject(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                                 sunFontIDs.rect2DFloatClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
                                 sunFontIDs.rect2DFloatCtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        return bounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
    error = FT_Outline_Get_BBox(outline, &bbox);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
    //convert bbox
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
    if (error || bbox.xMin >= bbox.xMax || bbox.yMin >= bbox.yMax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
        bounds = (*env)->NewObject(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                                   sunFontIDs.rect2DFloatClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                                   sunFontIDs.rect2DFloatCtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        bounds = (*env)->NewObject(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                                   sunFontIDs.rect2DFloatClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
                                   sunFontIDs.rect2DFloatCtr4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                                   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
  1319
                                   F26Dot6ToFloat(-bbox.yMax),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                                   F26Dot6ToFloat(bbox.xMax-bbox.xMin),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                                   F26Dot6ToFloat(bbox.yMax-bbox.yMin));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
    return bounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
 * Class:     sun_font_FreetypeFontScaler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
 * Method:    getGlyphVectorOutlineNative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
 * Signature: (Lsun/font/Font2D;J[IIFF)Ljava/awt/geom/GeneralPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
JNIEXPORT jobject
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
Java_sun_font_FreetypeFontScaler_getGlyphVectorOutlineNative(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        JNIEnv *env, jobject scaler, jobject font2D,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        jlong pScalerContext, jlong pScaler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        jintArray glyphArray, jint numGlyphs, jfloat xpos, jfloat ypos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
    FT_Outline* outline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
    jobject gp = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
    jbyteArray types;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
    jfloatArray coords;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
    GPData gpdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
    jint *glyphs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
    FTScalerContext *context =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
         (FTScalerContext*) jlong_to_ptr(pScalerContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
    FTScalerInfo *scalerInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
             (FTScalerInfo*) jlong_to_ptr(pScaler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
    glyphs = (jint*) malloc(numGlyphs*sizeof(jint));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
    if (glyphs == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        gp = (*env)->NewObject(env, sunFontIDs.gpClass, sunFontIDs.gpCtrEmpty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        if (!isNullScalerContext(context) && scalerInfo != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
            invalidateJavaScaler(env, scaler, scalerInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        return gp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
    (*env)->GetIntArrayRegion(env, glyphArray, 0, numGlyphs, glyphs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
    for (i=0; i<numGlyphs;i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
        if (glyphs[i] >= INVISIBLE_GLYPHS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        outline = getFTOutline(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                               font2D,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
                               context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                               scalerInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
                               glyphs[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
                               xpos, ypos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        if (outline == NULL || outline->n_points == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        gpdata.pointTypes  = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
        gpdata.pointCoords = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        if (!allocateSpaceForGP(&gpdata, outline->n_points,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                                outline->n_contours)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
        addToGP(&gpdata, outline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
    free(glyphs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
    if (gpdata.numCoords != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
      types = (*env)->NewByteArray(env, gpdata.numTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
      coords = (*env)->NewFloatArray(env, gpdata.numCoords);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
      if (types && coords) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        (*env)->SetByteArrayRegion(env, types, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
                                   gpdata.numTypes, gpdata.pointTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        (*env)->SetFloatArrayRegion(env, coords, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
                                    gpdata.numCoords, gpdata.pointCoords);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        gp=(*env)->NewObject(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
                             sunFontIDs.gpClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
                             sunFontIDs.gpCtr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
                             gpdata.wr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
                             types,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                             gpdata.numTypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                             coords,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                             gpdata.numCoords);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
        return gp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
    return (*env)->NewObject(env, sunFontIDs.gpClass, sunFontIDs.gpCtrEmpty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
Java_sun_font_FreetypeFontScaler_getUnitsPerEMNative(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        JNIEnv *env, jobject scaler, jlong pScaler) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
    FTScalerInfo *s = (FTScalerInfo* ) jlong_to_ptr(pScaler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
    /* Freetype doc says:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
     The number of font units per EM square for this face.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
     This is typically 2048 for TrueType fonts, and 1000 for Type 1 fonts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     Only relevant for scalable formats.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     However, layout engine might be not tested with anything but 2048.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     NB: test it! */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
    if (s != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        return s->face->units_per_EM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
    return 2048;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
/* This native method is called by the OpenType layout engine. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
JNIEXPORT jobject JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
Java_sun_font_FreetypeFontScaler_getGlyphPointNative(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        JNIEnv *env, jobject scaler, jobject font2D, jlong pScalerContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        jlong pScaler, jint glyphCode, jint pointNumber) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
    FT_Outline* outline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
    jobject point = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
    jfloat x=0, y=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
    FTScalerContext *context =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
         (FTScalerContext*) jlong_to_ptr(pScalerContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
    FTScalerInfo *scalerInfo = (FTScalerInfo*) jlong_to_ptr(pScaler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
    outline = getFTOutline(env, font2D, context, scalerInfo, glyphCode, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
    if (outline != NULL && outline->n_points > pointNumber) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
        x =  F26Dot6ToFloat(outline->points[pointNumber].x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
        y = -F26Dot6ToFloat(outline->points[pointNumber].y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
    return (*env)->NewObject(env, sunFontIDs.pt2DFloatClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
                             sunFontIDs.pt2DFloatCtr, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
}