1 /* 2 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.javafx.font.freetype; 27 28 import com.sun.javafx.font.CompositeFontResource; 29 import com.sun.javafx.font.CompositeGlyphMapper; 30 import com.sun.javafx.font.FontResource; 31 import com.sun.javafx.font.FontStrike; 32 import com.sun.javafx.font.PGFont; 33 import com.sun.javafx.font.PrismFontFactory; 34 import com.sun.javafx.text.GlyphLayout; 35 import com.sun.javafx.text.TextRun; 36 37 class PangoGlyphLayout extends GlyphLayout { 38 private static long fontmap; 39 40 static { 41 fontmap = OSPango.pango_ft2_font_map_new(); 42 } 43 44 private int getSlot(PGFont font, PangoGlyphString glyphString) { 45 CompositeFontResource fr = (CompositeFontResource)font.getFontResource(); 46 long fallbackFont = glyphString.font; 47 long fallbackFd = OSPango.pango_font_describe(fallbackFont); 48 String fallbackFamily = OSPango.pango_font_description_get_family(fallbackFd); 49 int fallbackStyle = OSPango.pango_font_description_get_style(fallbackFd); 50 int fallbackWeight = OSPango.pango_font_description_get_weight(fallbackFd); 51 OSPango.pango_font_description_free(fallbackFd); 52 boolean bold = fallbackWeight == OSPango.PANGO_WEIGHT_BOLD; 53 boolean italic = fallbackStyle != OSPango.PANGO_STYLE_NORMAL; 54 55 PrismFontFactory prismFactory = PrismFontFactory.getFontFactory(); 56 PGFont fallbackPGFont = prismFactory.createFont(fallbackFamily, bold, 57 italic, font.getSize()); 58 String fallbackFullname = fallbackPGFont.getFullName(); 59 String primaryFullname = fr.getSlotResource(0).getFullName(); 60 61 int slot = 0; 62 if (!fallbackFullname.equalsIgnoreCase(primaryFullname)) { 63 slot = fr.getSlotForFont(fallbackFullname); 64 if (PrismFontFactory.debugFonts) { 65 System.err.println("\tFallback font= "+ fallbackFullname + " slot=" + (slot>>24)); 66 } 67 } 68 return slot; 69 } 70 71 private boolean check(long checkValue, String message, long context, long desc, long attrList) { 72 if (checkValue != 0) return false; 73 if (message != null && PrismFontFactory.debugFonts) { 74 System.err.println(message); 75 } 76 /* pango_attr_list_unref() also frees the attributes it contains */ 77 if (attrList != 0) OSPango.pango_attr_list_unref(attrList); 78 if (desc != 0) OSPango.pango_font_description_free(desc); 79 if (context != 0) OSPango.g_object_unref(context); 80 return true; 81 } 82 83 private long str = 0L; 84 public void layout(TextRun run, PGFont font, FontStrike strike, char[] text) { 85 86 /* Create the pango font and attribute list */ 87 FontResource fr = font.getFontResource(); 88 boolean composite = fr instanceof CompositeFontResource; 89 if (composite) { 90 fr = ((CompositeFontResource)fr).getSlotResource(0); 91 } 92 if (check(fontmap, "Failed allocating PangoFontMap.", 0, 0, 0)) { 93 return; 94 } 95 long context = OSPango.pango_font_map_create_context(fontmap); 96 if (check(context, "Failed allocating PangoContext.", 0, 0, 0)) { 97 return; 98 } 99 boolean rtl = (run.getLevel() & 1) != 0; 100 if (rtl) { 101 OSPango.pango_context_set_base_dir(context, OSPango.PANGO_DIRECTION_RTL); 102 } 103 float size = font.getSize(); 104 int style = fr.isItalic() ? OSPango.PANGO_STYLE_ITALIC : OSPango.PANGO_STYLE_NORMAL; 105 int weight = fr.isBold() ? OSPango.PANGO_WEIGHT_BOLD : OSPango.PANGO_WEIGHT_NORMAL; 106 long desc = OSPango.pango_font_description_new(); 107 if (check(desc, "Failed allocating FontDescription.", context, 0, 0)) { 108 return; 109 } 110 OSPango.pango_font_description_set_family(desc, fr.getFamilyName()); 111 OSPango.pango_font_description_set_absolute_size(desc, size * OSPango.PANGO_SCALE); 112 OSPango.pango_font_description_set_stretch(desc, OSPango.PANGO_STRETCH_NORMAL); 113 OSPango.pango_font_description_set_style(desc, style); 114 OSPango.pango_font_description_set_weight(desc, weight); 115 long attrList = OSPango.pango_attr_list_new(); 116 if (check(attrList, "Failed allocating PangoAttributeList.", context, desc, 0)) { 117 return; 118 } 119 long attr = OSPango.pango_attr_font_desc_new(desc); 120 if (check(attr, "Failed allocating PangoAttribute.", context, desc, attrList)) { 121 return; 122 } 123 OSPango.pango_attr_list_insert(attrList, attr); 124 if (!composite) { 125 attr = OSPango.pango_attr_fallback_new(false); 126 OSPango.pango_attr_list_insert(attrList, attr); 127 } 128 129 if (str == 0L) { 130 str = OSPango.g_utf16_to_utf8(text); 131 if (check(str, "Failed allocating UTF-8 buffer.", context, desc, attrList)) { 132 return; 133 } 134 } 135 136 /* Itemize */ 137 long start = OSPango.g_utf8_offset_to_pointer(str, run.getStart()); 138 long end = OSPango.g_utf8_offset_to_pointer(str, run.getEnd()); 139 long runs = OSPango.pango_itemize(context, str, (int)(start - str), (int)(end - start), attrList, 0); 140 141 if (runs != 0) { 142 /* Shape all PangoItem into PangoGlyphString */ 143 int runsCount = OSPango.g_list_length(runs); 144 PangoGlyphString[] pangoGlyphs = new PangoGlyphString[runsCount]; 145 for (int i = 0; i < runsCount; i++) { 146 long pangoItem = OSPango.g_list_nth_data(runs, i); 147 if (pangoItem != 0) { 148 pangoGlyphs[i] = OSPango.pango_shape(str, pangoItem); 149 OSPango.pango_item_free(pangoItem); 150 } 151 } 152 OSPango.g_list_free(runs); 153 154 int glyphCount = 0; 155 for (PangoGlyphString g : pangoGlyphs) { 156 if (g != null) { 157 glyphCount += g.num_glyphs; 158 } 159 } 160 int[] glyphs = new int[glyphCount]; 161 float[] pos = new float[glyphCount * 2 + 2]; 162 int[] indices = new int[glyphCount]; 163 int gi = 0; 164 int ci = rtl ? run.getLength() : 0; 165 int width = 0; 166 for (PangoGlyphString g : pangoGlyphs) { 167 if (g != null) { 168 int slot = composite ? getSlot(font, g) : 0; 169 if (rtl) ci -= g.num_chars; 170 for (int i = 0; i < g.num_glyphs; i++) { 171 int gii = gi + i; 172 if (slot != -1) { 173 int gg = g.glyphs[i]; 174 175 /* Ignoring any glyphs outside the GLYPHMASK range. 176 * Note that Pango uses PANGO_GLYPH_EMPTY (0x0FFFFFFF), PANGO_GLYPH_INVALID_INPUT (0xFFFFFFFF), 177 * and other values with special meaning. 178 */ 179 if (0 <= gg && gg <= CompositeGlyphMapper.GLYPHMASK) { 180 glyphs[gii] = (slot << 24) | gg; 181 } 182 } 183 if (size != 0) { 184 width += g.widths[i]; 185 pos[2 + (gii << 1)] = ((float)width) / OSPango.PANGO_SCALE; 186 } 187 indices[gii] = g.log_clusters[i] + ci; 188 } 189 if (!rtl) ci += g.num_chars; 190 gi += g.num_glyphs; 191 } 192 } 193 run.shape(glyphCount, glyphs, pos, indices); 194 } 195 196 check(0, null, context, desc, attrList); 197 } 198 199 @Override 200 public void dispose() { 201 super.dispose(); 202 if (str != 0L) { 203 OSPango.g_free(str); 204 str = 0L; 205 } 206 } 207 }