aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/ports/SkFontHost_FreeType.cpp
diff options
context:
space:
mode:
authorGravatar vandebo@chromium.org <vandebo@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>2011-05-09 18:13:47 +0000
committerGravatar vandebo@chromium.org <vandebo@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>2011-05-09 18:13:47 +0000
commit6744d498fcbbbcf503ec80c4d43dd8f118a88597 (patch)
tree855c458a26e42d13ff29f9aa51266f5e151b9569 /src/ports/SkFontHost_FreeType.cpp
parent339ac3d0a7650c98de35afbcff4ac1d5b47199c0 (diff)
[PDF] Add a ToUnicode mapping for fonts.
This makes text in PDFs searchable and copy&paste-able. Code from arthurhsu@chromium.org. Original review: http://codereview.appspot.com/4428082/ Review URL: http://codereview.appspot.com/4525042 git-svn-id: http://skia.googlecode.com/svn/trunk@1280 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'src/ports/SkFontHost_FreeType.cpp')
-rw-r--r--src/ports/SkFontHost_FreeType.cpp56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/ports/SkFontHost_FreeType.cpp b/src/ports/SkFontHost_FreeType.cpp
index 5ed66c883a..b3cc7832e5 100644
--- a/src/ports/SkFontHost_FreeType.cpp
+++ b/src/ports/SkFontHost_FreeType.cpp
@@ -339,6 +339,56 @@ static bool getWidthAdvance(FT_Face face, int gId, int16_t* data) {
return true;
}
+static void populate_glyph_to_unicode(FT_Face& face,
+ SkTDArray<SkUnichar>* glyphToUnicode) {
+ // Check and see if we have Unicode cmaps.
+ for (int i = 0; i < face->num_charmaps; ++i) {
+ // CMaps known to support Unicode:
+ // Platform ID Encoding ID Name
+ // ----------- ----------- -----------------------------------
+ // 0 0,1 Apple Unicode
+ // 0 3 Apple Unicode 2.0 (preferred)
+ // 3 1 Microsoft Unicode UCS-2
+ // 3 10 Microsoft Unicode UCS-4 (preferred)
+ //
+ // See Apple TrueType Reference Manual
+ // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6cmap.html
+ // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6name.html#ID
+ // Microsoft OpenType Specification
+ // http://www.microsoft.com/typography/otspec/cmap.htm
+
+ FT_UShort platformId = face->charmaps[i]->platform_id;
+ FT_UShort encodingId = face->charmaps[i]->encoding_id;
+
+ if (platformId != 0 && platformId != 3) {
+ continue;
+ }
+ if (platformId == 3 && encodingId != 1 && encodingId != 10) {
+ continue;
+ }
+ bool preferredMap = ((platformId == 3 && encodingId == 10) ||
+ (platformId == 0 && encodingId == 3));
+
+ FT_Set_Charmap(face, face->charmaps[i]);
+ if (glyphToUnicode->isEmpty()) {
+ glyphToUnicode->setCount(face->num_glyphs);
+ memset(glyphToUnicode->begin(), 0,
+ sizeof(SkUnichar) * face->num_glyphs);
+ }
+
+ // Iterate through each cmap entry.
+ FT_UInt glyphIndex;
+ for (SkUnichar charCode = FT_Get_First_Char(face, &glyphIndex);
+ glyphIndex != 0;
+ charCode = FT_Get_Next_Char(face, charCode, &glyphIndex)) {
+ if (charCode &&
+ ((*glyphToUnicode)[glyphIndex] == 0 || preferredMap)) {
+ (*glyphToUnicode)[glyphIndex] = charCode;
+ }
+ }
+ }
+}
+
// static
SkAdvancedTypefaceMetrics* SkFontHost::GetAdvancedTypefaceMetrics(
uint32_t fontID,
@@ -509,6 +559,12 @@ SkAdvancedTypefaceMetrics* SkFontHost::GetAdvancedTypefaceMetrics(
}
}
+ if (perGlyphInfo & SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo &&
+ info->fType != SkAdvancedTypefaceMetrics::kType1_Font &&
+ face->num_charmaps) {
+ populate_glyph_to_unicode(face, &(info->fGlyphToUnicode));
+ }
+
if (!canEmbed(face))
info->fType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;