OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkPDFDevice.h" | 8 #include "SkPDFDevice.h" |
9 | 9 |
10 #include "SkAnnotation.h" | 10 #include "SkAnnotation.h" |
(...skipping 15 matching lines...) Expand all Loading... | |
26 #include "SkPDFStream.h" | 26 #include "SkPDFStream.h" |
27 #include "SkPDFTypes.h" | 27 #include "SkPDFTypes.h" |
28 #include "SkPDFUtils.h" | 28 #include "SkPDFUtils.h" |
29 #include "SkRect.h" | 29 #include "SkRect.h" |
30 #include "SkString.h" | 30 #include "SkString.h" |
31 #include "SkTextFormatParams.h" | 31 #include "SkTextFormatParams.h" |
32 #include "SkTemplates.h" | 32 #include "SkTemplates.h" |
33 #include "SkTypefacePriv.h" | 33 #include "SkTypefacePriv.h" |
34 #include "SkTSet.h" | 34 #include "SkTSet.h" |
35 | 35 |
36 #ifdef SK_BUILD_FOR_ANDROID | |
37 #include "SkTypeface_android.h" | |
38 | |
39 struct TypefaceFallbackData { | |
40 SkTypeface* typeface; | |
41 int lowerBounds; | |
42 int upperBounds; | |
43 | |
44 bool operator==(const TypefaceFallbackData& b) const { | |
45 return typeface == b.typeface && | |
46 lowerBounds == b.lowerBounds && | |
47 upperBounds == b.upperBounds; | |
48 } | |
49 }; | |
50 #endif | |
51 | |
36 // Utility functions | 52 // Utility functions |
37 | 53 |
38 static void emit_pdf_color(SkColor color, SkWStream* result) { | 54 static void emit_pdf_color(SkColor color, SkWStream* result) { |
39 SkASSERT(SkColorGetA(color) == 0xFF); // We handle alpha elsewhere. | 55 SkASSERT(SkColorGetA(color) == 0xFF); // We handle alpha elsewhere. |
40 SkScalar colorMax = SkIntToScalar(0xFF); | 56 SkScalar colorMax = SkIntToScalar(0xFF); |
41 SkPDFScalar::Append( | 57 SkPDFScalar::Append( |
42 SkScalarDiv(SkIntToScalar(SkColorGetR(color)), colorMax), result); | 58 SkScalarDiv(SkIntToScalar(SkColorGetR(color)), colorMax), result); |
43 result->writeText(" "); | 59 result->writeText(" "); |
44 SkPDFScalar::Append( | 60 SkPDFScalar::Append( |
45 SkScalarDiv(SkIntToScalar(SkColorGetG(color)), colorMax), result); | 61 SkScalarDiv(SkIntToScalar(SkColorGetG(color)), colorMax), result); |
(...skipping 1062 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1108 // making text unreadable (e.g. same text twice when using CSS shadows). | 1124 // making text unreadable (e.g. same text twice when using CSS shadows). |
1109 return; | 1125 return; |
1110 } | 1126 } |
1111 SkASSERT(1 == scalarsPerPos || 2 == scalarsPerPos); | 1127 SkASSERT(1 == scalarsPerPos || 2 == scalarsPerPos); |
1112 SkPaint textPaint = calculate_text_paint(paint); | 1128 SkPaint textPaint = calculate_text_paint(paint); |
1113 ScopedContentEntry content(this, d, textPaint, true); | 1129 ScopedContentEntry content(this, d, textPaint, true); |
1114 if (!content.entry()) { | 1130 if (!content.entry()) { |
1115 return; | 1131 return; |
1116 } | 1132 } |
1117 | 1133 |
1134 #ifdef SK_BUILD_FOR_ANDROID | |
1135 /* | |
1136 * In the case that we have enabled fallback fonts on Android we need to | |
1137 * take the following steps to ensure that the PDF draws all characters, | |
1138 * regardless of their underlying font file, correctly. | |
1139 * | |
1140 * 1. Convert input into GlyphID encoding if it currently is not | |
1141 * 2. Iterate over the glyphIDs and identify the actual typeface that each | |
1142 * glyph resolves to | |
1143 * 3. Iterate over those typefaces and recursively call this function with | |
1144 * only the glyphs (and their positions) that the typeface is capable of | |
1145 * resolving. | |
1146 */ | |
1147 if (paint.getPaintOptionsAndroid().isUsingFontFallbacks()) { | |
1148 uint16_t* glyphIDs = NULL; | |
1149 SkGlyphStorage tmpStorage(0); | |
1150 size_t numGlyphs = 0; | |
1151 | |
1152 // convert to glyphIDs | |
1153 if (paint.getTextEncoding() != SkPaint::kGlyphID_TextEncoding) { | |
scroggo
2013/10/01 21:00:14
nit: I find it clearer if when there is an else st
| |
1154 numGlyphs = paint.textToGlyphs(text, len, NULL); | |
1155 tmpStorage.reset(numGlyphs); | |
1156 paint.textToGlyphs(text, len, tmpStorage.get()); | |
1157 glyphIDs = tmpStorage.get(); | |
1158 } else { | |
1159 numGlyphs = len / 2; | |
1160 glyphIDs = reinterpret_cast<uint16_t*>(const_cast<void*>(text)); | |
1161 } | |
1162 | |
1163 // if no typeface is provided in the paint get the default | |
1164 SkAutoTUnref<SkTypeface> origFace(SkSafeRef(paint.getTypeface())); | |
1165 if (!origFace.get()) { | |
scroggo
2013/10/01 21:00:14
The style guide prefers explicit NULL checks.
| |
1166 origFace.reset(SkTypeface::RefDefault()); | |
1167 } | |
1168 const uint16_t origGlyphCount = origFace->countGlyphs(); | |
1169 | |
1170 // keep a list of the already visited typefaces and some data about them | |
1171 SkTDArray<TypefaceFallbackData> visitedTypefaces; | |
1172 | |
1173 // find all the typefaces needed to resolve this run of text | |
1174 bool usesOriginalTypeface = false; | |
1175 for (uint16_t x = 0; x < numGlyphs; ++x) { | |
1176 // optimization that checks to see if original typeface can resolve the glyph | |
1177 if (glyphIDs[x] < origGlyphCount) { | |
1178 usesOriginalTypeface = true; | |
1179 continue; | |
1180 } | |
1181 | |
1182 // find the fallback typeface that supports this glyph | |
1183 TypefaceFallbackData data; | |
1184 data.typeface = SkGetTypefaceForGlyphID(glyphIDs[x], origFace.get(), | |
1185 paint.getPaintOptionsAndroid (), | |
1186 &data.lowerBounds, &data.upp erBounds); | |
1187 // add the typeface and its data if we don't have it | |
1188 if (data.typeface && !visitedTypefaces.contains(data)) { | |
1189 visitedTypefaces.push(data); | |
1190 } | |
1191 } | |
1192 | |
1193 // if the original font was used then add it to the list as well | |
1194 if (usesOriginalTypeface) { | |
1195 TypefaceFallbackData* data = visitedTypefaces.push(); | |
1196 data->typeface = origFace.get(); | |
1197 data->lowerBounds = 0; | |
1198 data->upperBounds = origGlyphCount; | |
1199 } | |
1200 | |
1201 // keep a scratch glyph and pos storage | |
1202 SkAutoTMalloc<SkScalar> posStorage(len * scalarsPerPos); | |
1203 SkScalar* tmpPos = posStorage.get(); | |
1204 SkGlyphStorage glyphStorage(numGlyphs); | |
1205 uint16_t* tmpGlyphIDs = glyphStorage.get(); | |
1206 | |
1207 // loop through all the valid typefaces, trim the glyphs to only those | |
1208 // resolved by the typeface, and then draw that run of glyphs | |
1209 for (int x = 0; x < visitedTypefaces.count(); ++x) { | |
1210 const TypefaceFallbackData& data = visitedTypefaces[x]; | |
1211 | |
1212 int tmpGlyphCount = 0; | |
1213 for (uint16_t y = 0; y < numGlyphs; ++y) { | |
1214 if (glyphIDs[y] >= data.lowerBounds && glyphIDs[y] < data.upperB ounds) { | |
1215 tmpGlyphIDs[tmpGlyphCount] = glyphIDs[y] - data.lowerBounds; | |
1216 memcpy(&(tmpPos[tmpGlyphCount * scalarsPerPos]), | |
1217 &(pos[y * scalarsPerPos]), | |
1218 scalarsPerPos * sizeof(SkScalar)); | |
1219 tmpGlyphCount++; | |
1220 } | |
1221 } | |
1222 | |
1223 // recursively call this function with the right typeface | |
1224 SkPaint tmpPaint = paint; | |
1225 tmpPaint.setTypeface(data.typeface); | |
1226 tmpPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); | |
1227 | |
1228 // turn off fallback chaining | |
1229 SkPaintOptionsAndroid paintOpts = tmpPaint.getPaintOptionsAndroid(); | |
1230 paintOpts.setUseFontFallbacks(false); | |
1231 tmpPaint.setPaintOptionsAndroid(paintOpts); | |
1232 | |
1233 this->drawPosText(d, tmpGlyphIDs, tmpGlyphCount * 2, tmpPos, constY, | |
1234 scalarsPerPos, tmpPaint); | |
1235 } | |
1236 return; | |
1237 } | |
1238 #endif | |
1239 | |
1118 SkGlyphStorage storage(0); | 1240 SkGlyphStorage storage(0); |
1119 uint16_t* glyphIDs = NULL; | 1241 uint16_t* glyphIDs = NULL; |
1120 size_t numGlyphs = force_glyph_encoding(paint, text, len, &storage, | 1242 size_t numGlyphs = force_glyph_encoding(paint, text, len, &storage, |
1121 &glyphIDs); | 1243 &glyphIDs); |
1122 textPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); | 1244 textPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
1123 | 1245 |
1124 SkDrawCacheProc glyphCacheProc = textPaint.getDrawCacheProc(); | 1246 SkDrawCacheProc glyphCacheProc = textPaint.getDrawCacheProc(); |
1125 content.entry()->fContent.writeText("BT\n"); | 1247 content.entry()->fContent.writeText("BT\n"); |
1126 updateFont(textPaint, glyphIDs[0], content.entry()); | 1248 updateFont(textPaint, glyphIDs[0], content.entry()); |
1127 for (size_t i = 0; i < numGlyphs; i++) { | 1249 for (size_t i = 0; i < numGlyphs; i++) { |
(...skipping 808 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1936 } | 2058 } |
1937 | 2059 |
1938 bool SkPDFDevice::onReadPixels(const SkBitmap& bitmap, int x, int y, | 2060 bool SkPDFDevice::onReadPixels(const SkBitmap& bitmap, int x, int y, |
1939 SkCanvas::Config8888) { | 2061 SkCanvas::Config8888) { |
1940 return false; | 2062 return false; |
1941 } | 2063 } |
1942 | 2064 |
1943 bool SkPDFDevice::allowImageFilter(SkImageFilter*) { | 2065 bool SkPDFDevice::allowImageFilter(SkImageFilter*) { |
1944 return false; | 2066 return false; |
1945 } | 2067 } |
OLD | NEW |