| 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 22 matching lines...) Expand all Loading... |
| 33 #include "SkTemplates.h" | 33 #include "SkTemplates.h" |
| 34 #include "SkTypefacePriv.h" | 34 #include "SkTypefacePriv.h" |
| 35 | 35 |
| 36 #define DPI_FOR_RASTER_SCALE_ONE 72 | 36 #define DPI_FOR_RASTER_SCALE_ONE 72 |
| 37 | 37 |
| 38 // Utility functions | 38 // Utility functions |
| 39 | 39 |
| 40 static void emit_pdf_color(SkColor color, SkWStream* result) { | 40 static void emit_pdf_color(SkColor color, SkWStream* result) { |
| 41 SkASSERT(SkColorGetA(color) == 0xFF); // We handle alpha elsewhere. | 41 SkASSERT(SkColorGetA(color) == 0xFF); // We handle alpha elsewhere. |
| 42 SkScalar colorMax = SkIntToScalar(0xFF); | 42 SkScalar colorMax = SkIntToScalar(0xFF); |
| 43 SkPDFScalar::Append( | 43 SkPDFUtils::AppendScalar( |
| 44 SkScalarDiv(SkIntToScalar(SkColorGetR(color)), colorMax), result); | 44 SkScalarDiv(SkIntToScalar(SkColorGetR(color)), colorMax), result); |
| 45 result->writeText(" "); | 45 result->writeText(" "); |
| 46 SkPDFScalar::Append( | 46 SkPDFUtils::AppendScalar( |
| 47 SkScalarDiv(SkIntToScalar(SkColorGetG(color)), colorMax), result); | 47 SkScalarDiv(SkIntToScalar(SkColorGetG(color)), colorMax), result); |
| 48 result->writeText(" "); | 48 result->writeText(" "); |
| 49 SkPDFScalar::Append( | 49 SkPDFUtils::AppendScalar( |
| 50 SkScalarDiv(SkIntToScalar(SkColorGetB(color)), colorMax), result); | 50 SkScalarDiv(SkIntToScalar(SkColorGetB(color)), colorMax), result); |
| 51 result->writeText(" "); | 51 result->writeText(" "); |
| 52 } | 52 } |
| 53 | 53 |
| 54 static SkPaint calculate_text_paint(const SkPaint& paint) { | 54 static SkPaint calculate_text_paint(const SkPaint& paint) { |
| 55 SkPaint result = paint; | 55 SkPaint result = paint; |
| 56 if (result.isFakeBoldText()) { | 56 if (result.isFakeBoldText()) { |
| 57 SkScalar fakeBoldScale = SkScalarInterpFunc(result.getTextSize(), | 57 SkScalar fakeBoldScale = SkScalarInterpFunc(result.getTextSize(), |
| 58 kStdFakeBoldInterpKeys, | 58 kStdFakeBoldInterpKeys, |
| 59 kStdFakeBoldInterpValues, | 59 kStdFakeBoldInterpValues, |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 } | 157 } |
| 158 *glyphIDs = storage->get(); | 158 *glyphIDs = storage->get(); |
| 159 return numGlyphs; | 159 return numGlyphs; |
| 160 } | 160 } |
| 161 | 161 |
| 162 static void set_text_transform(SkScalar x, SkScalar y, SkScalar textSkewX, | 162 static void set_text_transform(SkScalar x, SkScalar y, SkScalar textSkewX, |
| 163 SkWStream* content) { | 163 SkWStream* content) { |
| 164 // Flip the text about the x-axis to account for origin swap and include | 164 // Flip the text about the x-axis to account for origin swap and include |
| 165 // the passed parameters. | 165 // the passed parameters. |
| 166 content->writeText("1 0 "); | 166 content->writeText("1 0 "); |
| 167 SkPDFScalar::Append(0 - textSkewX, content); | 167 SkPDFUtils::AppendScalar(0 - textSkewX, content); |
| 168 content->writeText(" -1 "); | 168 content->writeText(" -1 "); |
| 169 SkPDFScalar::Append(x, content); | 169 SkPDFUtils::AppendScalar(x, content); |
| 170 content->writeText(" "); | 170 content->writeText(" "); |
| 171 SkPDFScalar::Append(y, content); | 171 SkPDFUtils::AppendScalar(y, content); |
| 172 content->writeText(" Tm\n"); | 172 content->writeText(" Tm\n"); |
| 173 } | 173 } |
| 174 | 174 |
| 175 // It is important to not confuse GraphicStateEntry with SkPDFGraphicState, the | 175 // It is important to not confuse GraphicStateEntry with SkPDFGraphicState, the |
| 176 // later being our representation of an object in the PDF file. | 176 // later being our representation of an object in the PDF file. |
| 177 struct GraphicStateEntry { | 177 struct GraphicStateEntry { |
| 178 GraphicStateEntry(); | 178 GraphicStateEntry(); |
| 179 | 179 |
| 180 // Compare the fields we care about when setting up a new content entry. | 180 // Compare the fields we care about when setting up a new content entry. |
| 181 bool compareInitialState(const GraphicStateEntry& b); | 181 bool compareInitialState(const GraphicStateEntry& b); |
| (...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 540 | 540 |
| 541 if (state.fGraphicStateIndex != currentEntry()->fGraphicStateIndex) { | 541 if (state.fGraphicStateIndex != currentEntry()->fGraphicStateIndex) { |
| 542 SkPDFUtils::ApplyGraphicState(state.fGraphicStateIndex, fContentStream); | 542 SkPDFUtils::ApplyGraphicState(state.fGraphicStateIndex, fContentStream); |
| 543 currentEntry()->fGraphicStateIndex = state.fGraphicStateIndex; | 543 currentEntry()->fGraphicStateIndex = state.fGraphicStateIndex; |
| 544 } | 544 } |
| 545 | 545 |
| 546 if (state.fTextScaleX) { | 546 if (state.fTextScaleX) { |
| 547 if (state.fTextScaleX != currentEntry()->fTextScaleX) { | 547 if (state.fTextScaleX != currentEntry()->fTextScaleX) { |
| 548 SkScalar pdfScale = SkScalarMul(state.fTextScaleX, | 548 SkScalar pdfScale = SkScalarMul(state.fTextScaleX, |
| 549 SkIntToScalar(100)); | 549 SkIntToScalar(100)); |
| 550 SkPDFScalar::Append(pdfScale, fContentStream); | 550 SkPDFUtils::AppendScalar(pdfScale, fContentStream); |
| 551 fContentStream->writeText(" Tz\n"); | 551 fContentStream->writeText(" Tz\n"); |
| 552 currentEntry()->fTextScaleX = state.fTextScaleX; | 552 currentEntry()->fTextScaleX = state.fTextScaleX; |
| 553 } | 553 } |
| 554 if (state.fTextFill != currentEntry()->fTextFill) { | 554 if (state.fTextFill != currentEntry()->fTextFill) { |
| 555 SK_COMPILE_ASSERT(SkPaint::kFill_Style == 0, enum_must_match_value); | 555 SK_COMPILE_ASSERT(SkPaint::kFill_Style == 0, enum_must_match_value); |
| 556 SK_COMPILE_ASSERT(SkPaint::kStroke_Style == 1, | 556 SK_COMPILE_ASSERT(SkPaint::kStroke_Style == 1, |
| 557 enum_must_match_value); | 557 enum_must_match_value); |
| 558 SK_COMPILE_ASSERT(SkPaint::kStrokeAndFill_Style == 2, | 558 SK_COMPILE_ASSERT(SkPaint::kStrokeAndFill_Style == 2, |
| 559 enum_must_match_value); | 559 enum_must_match_value); |
| 560 fContentStream->writeDecAsText(state.fTextFill); | 560 fContentStream->writeDecAsText(state.fTextFill); |
| (...skipping 512 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1073 } | 1073 } |
| 1074 result[4 * len + 1] = '>'; | 1074 result[4 * len + 1] = '>'; |
| 1075 return result; | 1075 return result; |
| 1076 } else { | 1076 } else { |
| 1077 SkASSERT(len <= 65535); | 1077 SkASSERT(len <= 65535); |
| 1078 SkString tmp(len); | 1078 SkString tmp(len); |
| 1079 for (size_t i = 0; i < len; i++) { | 1079 for (size_t i = 0; i < len; i++) { |
| 1080 SkASSERT(0 == input[i] >> 8); | 1080 SkASSERT(0 == input[i] >> 8); |
| 1081 tmp[i] = static_cast<uint8_t>(input[i]); | 1081 tmp[i] = static_cast<uint8_t>(input[i]); |
| 1082 } | 1082 } |
| 1083 return SkPDFString::FormatString(tmp.c_str(), tmp.size()); | 1083 return SkPDFUtils::FormatString(tmp.c_str(), tmp.size()); |
| 1084 } | 1084 } |
| 1085 } | 1085 } |
| 1086 | 1086 |
| 1087 void SkPDFDevice::drawText(const SkDraw& d, const void* text, size_t len, | 1087 void SkPDFDevice::drawText(const SkDraw& d, const void* text, size_t len, |
| 1088 SkScalar x, SkScalar y, const SkPaint& paint) { | 1088 SkScalar x, SkScalar y, const SkPaint& paint) { |
| 1089 NOT_IMPLEMENTED(paint.getMaskFilter() != NULL, false); | 1089 NOT_IMPLEMENTED(paint.getMaskFilter() != NULL, false); |
| 1090 if (paint.getMaskFilter() != NULL) { | 1090 if (paint.getMaskFilter() != NULL) { |
| 1091 // Don't pretend we support drawing MaskFilters, it makes for artifacts | 1091 // Don't pretend we support drawing MaskFilters, it makes for artifacts |
| 1092 // making text unreadable (e.g. same text twice when using CSS shadows). | 1092 // making text unreadable (e.g. same text twice when using CSS shadows). |
| 1093 return; | 1093 return; |
| (...skipping 887 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1981 SkTypeface* typeface = paint.getTypeface(); | 1981 SkTypeface* typeface = paint.getTypeface(); |
| 1982 if (contentEntry->fState.fFont == NULL || | 1982 if (contentEntry->fState.fFont == NULL || |
| 1983 contentEntry->fState.fTextSize != paint.getTextSize() || | 1983 contentEntry->fState.fTextSize != paint.getTextSize() || |
| 1984 !contentEntry->fState.fFont->hasGlyph(glyphID)) { | 1984 !contentEntry->fState.fFont->hasGlyph(glyphID)) { |
| 1985 int fontIndex = getFontResourceIndex(typeface, glyphID); | 1985 int fontIndex = getFontResourceIndex(typeface, glyphID); |
| 1986 contentEntry->fContent.writeText("/"); | 1986 contentEntry->fContent.writeText("/"); |
| 1987 contentEntry->fContent.writeText(SkPDFResourceDict::getResourceName( | 1987 contentEntry->fContent.writeText(SkPDFResourceDict::getResourceName( |
| 1988 SkPDFResourceDict::kFont_ResourceType, | 1988 SkPDFResourceDict::kFont_ResourceType, |
| 1989 fontIndex).c_str()); | 1989 fontIndex).c_str()); |
| 1990 contentEntry->fContent.writeText(" "); | 1990 contentEntry->fContent.writeText(" "); |
| 1991 SkPDFScalar::Append(paint.getTextSize(), &contentEntry->fContent); | 1991 SkPDFUtils::AppendScalar(paint.getTextSize(), &contentEntry->fContent); |
| 1992 contentEntry->fContent.writeText(" Tf\n"); | 1992 contentEntry->fContent.writeText(" Tf\n"); |
| 1993 contentEntry->fState.fFont = fFontResources[fontIndex]; | 1993 contentEntry->fState.fFont = fFontResources[fontIndex]; |
| 1994 } | 1994 } |
| 1995 } | 1995 } |
| 1996 | 1996 |
| 1997 int SkPDFDevice::getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID) { | 1997 int SkPDFDevice::getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID) { |
| 1998 SkAutoTUnref<SkPDFFont> newFont( | 1998 SkAutoTUnref<SkPDFFont> newFont( |
| 1999 SkPDFFont::GetFontResource(fCanon, typeface, glyphID)); | 1999 SkPDFFont::GetFontResource(fCanon, typeface, glyphID)); |
| 2000 int resourceIndex = fFontResources.find(newFont.get()); | 2000 int resourceIndex = fFontResources.find(newFont.get()); |
| 2001 if (resourceIndex < 0) { | 2001 if (resourceIndex < 0) { |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2144 return; | 2144 return; |
| 2145 } | 2145 } |
| 2146 SkAutoTUnref<SkPDFObject> image(SkPDFBitmap::Create(fCanon, subsetBitmap)); | 2146 SkAutoTUnref<SkPDFObject> image(SkPDFBitmap::Create(fCanon, subsetBitmap)); |
| 2147 if (!image) { | 2147 if (!image) { |
| 2148 return; | 2148 return; |
| 2149 } | 2149 } |
| 2150 | 2150 |
| 2151 SkPDFUtils::DrawFormXObject(this->addXObjectResource(image.get()), | 2151 SkPDFUtils::DrawFormXObject(this->addXObjectResource(image.get()), |
| 2152 &content.entry()->fContent); | 2152 &content.entry()->fContent); |
| 2153 } | 2153 } |
| OLD | NEW |