static bool replace(std::string fn, std::string const &s1, std::string const &s2) { if (!fn.length() || !s1.length() || !s2.length()) { std::cerr << "Error: empty arg." << std::endl; return false; } std::ifstream rStream(fn.c_str()); fn += ".replace"; std::ofstream wStream(fn.c_str()); if (!rStream || !wStream) { std::cerr << "Error: could not open file." << std::endl; return false; } size_t i; while (getline(rStream, fn)) { i = 0; while ((i = fn.find(s1, i)) != std::string::npos) { fn.replace(i, s1.length(), s2); i += s2.length(); } wStream << fn << std::endl; } return true; }
// Make sure we excercise the multi-page functionality without problems. // Add this to args.gn to output the PDF to a file: // extra_cflags = [ "-DSK_PDF_TEST_MULTIPAGE=\"/tmp/skpdf_test_multipage.pdf\"" ] DEF_TEST(SkPDF_multiple_pages, r) { REQUIRE_PDF_DOCUMENT(SkPDF_multiple_pages, r); int n = 100; #ifdef SK_PDF_TEST_MULTIPAGE SkFILEWStream wStream(SK_PDF_TEST_MULTIPAGE); #else SkDynamicMemoryWStream wStream; #endif auto doc = SkPDF::MakeDocument(&wStream); for (int i = 0; i < n; ++i) { doc->beginPage(612, 792)->drawColor( SkColorSetARGB(0xFF, 0x00, (uint8_t)(255.0f * i / (n - 1)), 0x00)); } }
/* ** public */ void ShrubberyCreationForm::execute(Bureaucrat const &executor) const { this->checkExec(executor.getGrade()); std::ofstream wStream(this->_target.c_str()); if (!wStream) { std::cerr << "Error: could not open file." << std::endl; return ; } wStream << "This is a tiny ascii tree with two branches : Y" << std::endl << "Proof I'm not so lazzy, here another one : T" << std::endl; }
// This test uses file system operations that don't work out of the // box on iOS. It's likely that we don't need them on iOS. Ignoring for now. // TODO(stephana): Re-evaluate if we need this in the future. DEF_TEST(StreamPeek, reporter) { // Test a memory stream. const char gAbcs[] = "abcdefghijklmnopqrstuvwxyz"; SkMemoryStream memStream(gAbcs, strlen(gAbcs), false); test_fully_peekable_stream(reporter, &memStream, memStream.getLength()); // Test an arbitrary file stream. file streams do not support peeking. auto tmpdir = skiatest::GetTmpDir(); if (tmpdir.isEmpty()) { ERRORF(reporter, "no tmp dir!"); return; } auto path = SkOSPath::Join(tmpdir.c_str(), "file"); { SkFILEWStream wStream(path.c_str()); constexpr char filename[] = "images/baby_tux.webp"; auto data = GetResourceAsData(filename); if (!data || data->size() == 0) { ERRORF(reporter, "resource missing: %s\n", filename); return; } if (!wStream.isValid() || !wStream.write(data->data(), data->size())) { ERRORF(reporter, "error wrtiting to file %s", path.c_str()); return; } } SkFILEStream fileStream(path.c_str()); REPORTER_ASSERT(reporter, fileStream.isValid()); if (!fileStream.isValid()) { return; } SkAutoMalloc storage(fileStream.getLength()); for (size_t i = 1; i < fileStream.getLength(); i++) { REPORTER_ASSERT(reporter, fileStream.peek(storage.get(), i) == 0); } // Now test some FrontBufferedStreams for (size_t i = 1; i < memStream.getLength(); i++) { test_peeking_front_buffered_stream(reporter, memStream, i); } }
int main(int argc, char **argv) { Config config(argc, argv); SkFILEWStream wStream(config.output_file_name.value.c_str()); sk_sp<SkDocument> doc = MakePDFDocument(config, &wStream); assert(doc); Placement placement(&config, doc.get()); const std::string &font_file = config.font_file.value; sk_sp<SkTypeface> typeface; if (font_file.size() > 0) { typeface = SkTypeface::MakeFromFile(font_file.c_str(), 0 /* index */); } SkShaper shaper(typeface); assert(shaper.good()); for (std::string line; std::getline(std::cin, line);) { placement.WriteLine(shaper, line.c_str(), line.size()); } doc->close(); return 0; }
void TestResult::testOne() { sk_sp<SkPicture> pic; { SkString d; d.printf(" {%d, \"%s\"},", fDirNo, fFilename); SkString path = make_filepath(fDirNo, IN_DIR, fFilename); SkFILEStream stream(path.c_str()); if (!stream.isValid()) { SkDebugf("invalid stream %s\n", path.c_str()); goto finish; } if (fTestStep == kEncodeFiles) { size_t length = stream.getLength(); SkTArray<char, true> bytes; bytes.push_back_n(length); stream.read(&bytes[0], length); stream.rewind(); SkString wPath = make_filepath(0, outSkpDir, fFilename); SkFILEWStream wStream(wPath.c_str()); wStream.write(&bytes[0], length); wStream.flush(); } pic = SkPicture::MakeFromStream(&stream); if (!pic) { SkDebugf("unable to decode %s\n", fFilename); goto finish; } int pWidth = pic->width(); int pHeight = pic->height(); int pLargerWH = SkTMax(pWidth, pHeight); GrContextFactory contextFactory; #ifdef SK_BUILD_FOR_WIN GrContext* context = contextFactory.get(kAngle); #else GrContext* context = contextFactory.get(kNative); #endif if (nullptr == context) { SkDebugf("unable to allocate context for %s\n", fFilename); goto finish; } int maxWH = context->getMaxRenderTargetSize(); int scale = 1; while (pLargerWH / scale > maxWH) { scale *= 2; } SkBitmap bitmap; SkIPoint dim; do { dim.fX = (pWidth + scale - 1) / scale; dim.fY = (pHeight + scale - 1) / scale; bool success = bitmap.allocN32Pixels(dim.fX, dim.fY); if (success) { break; } SkDebugf("-%d-", scale); } while ((scale *= 2) < 256); if (scale >= 256) { SkDebugf("unable to allocate bitmap for %s (w=%d h=%d) (sw=%d sh=%d)\n", fFilename, pWidth, pHeight, dim.fX, dim.fY); return; } SkCanvas skCanvas(bitmap); drawPict(pic, &skCanvas, fScaleOversized ? scale : 1); GrTextureDesc desc; desc.fConfig = kRGBA_8888_GrPixelConfig; desc.fFlags = kRenderTarget_GrTextureFlagBit; desc.fWidth = dim.fX; desc.fHeight = dim.fY; desc.fSampleCnt = 0; sk_sp<GrTexture> texture(context->createUncachedTexture(desc, nullptr, 0)); if (!texture) { SkDebugf("unable to allocate texture for %s (w=%d h=%d)\n", fFilename, dim.fX, dim.fY); return; } SkGpuDevice grDevice(context, texture.get()); SkCanvas grCanvas(&grDevice); drawPict(pic.get(), &grCanvas, fScaleOversized ? scale : 1); SkBitmap grBitmap; grBitmap.allocPixels(grCanvas.imageInfo()); grCanvas.readPixels(&grBitmap, 0, 0); if (fTestStep == kCompareBits) { fPixelError = similarBits(grBitmap, bitmap); SkMSec skTime = timePict(pic, &skCanvas); SkMSec grTime = timePict(pic, &grCanvas); fTime = skTime - grTime; } else if (fTestStep == kEncodeFiles) { SkString pngStr = make_png_name(fFilename); const char* pngName = pngStr.c_str(); writePict(grBitmap, outGrDir, pngName); writePict(bitmap, outSkDir, pngName); } } }