nsresult FileReader::GetAsText(Blob *aBlob, const nsACString &aCharset, const char *aFileData, uint32_t aDataLen, nsAString& aResult) { // Try the API argument. const Encoding* encoding = Encoding::ForLabel(aCharset); if (!encoding) { // API argument failed. Try the type property of the blob. nsAutoString type16; aBlob->GetType(type16); NS_ConvertUTF16toUTF8 type(type16); nsAutoCString specifiedCharset; bool haveCharset; int32_t charsetStart, charsetEnd; NS_ExtractCharsetFromContentType(type, specifiedCharset, &haveCharset, &charsetStart, &charsetEnd); encoding = Encoding::ForLabel(specifiedCharset); if (!encoding) { // Type property failed. Use UTF-8. encoding = UTF_8_ENCODING; } } auto data = MakeSpan(reinterpret_cast<const uint8_t*>(aFileData), aDataLen); nsresult rv; Tie(rv, encoding) = encoding->Decode(data, aResult); return NS_FAILED(rv) ? rv : NS_OK; }
void TextEncoder::Encode(JSContext* aCx, JS::Handle<JSObject*> aObj, const nsAString& aString, JS::MutableHandle<JSObject*> aRetval, ErrorResult& aRv) { nsAutoCString utf8; nsresult rv; const Encoding* ignored; Tie(rv, ignored) = UTF_8_ENCODING->Encode(aString, utf8); if (NS_FAILED(rv)) { aRv.Throw(rv); return; } JSAutoCompartment ac(aCx, aObj); JSObject* outView = Uint8Array::Create( aCx, utf8.Length(), reinterpret_cast<const uint8_t*>(utf8.BeginReading())); if (!outView) { aRv.Throw(NS_ERROR_OUT_OF_MEMORY); return; } aRetval.set(outView); }
RasterImage::GetImageContainer(LayerManager* aManager, uint32_t aFlags) { MOZ_ASSERT(NS_IsMainThread()); MOZ_ASSERT(aManager); MOZ_ASSERT((aFlags & ~(FLAG_SYNC_DECODE | FLAG_SYNC_DECODE_IF_FAST | FLAG_ASYNC_NOTIFY)) == FLAG_NONE, "Unsupported flag passed to GetImageContainer"); int32_t maxTextureSize = aManager->GetMaxTextureSize(); if (!mHasSize || mSize.width > maxTextureSize || mSize.height > maxTextureSize) { return nullptr; } if (IsUnlocked() && mProgressTracker) { mProgressTracker->OnUnlockedDraw(); } RefPtr<layers::ImageContainer> container = mImageContainer.get(); bool mustRedecode = (aFlags & (FLAG_SYNC_DECODE | FLAG_SYNC_DECODE_IF_FAST)) && mLastImageContainerDrawResult != DrawResult::SUCCESS && mLastImageContainerDrawResult != DrawResult::BAD_IMAGE; if (container && !mustRedecode) { return container.forget(); } // We need a new ImageContainer, so create one. container = LayerManager::CreateImageContainer(); DrawResult drawResult; RefPtr<layers::Image> image; Tie(drawResult, image) = GetCurrentImage(container, aFlags); if (!image) { return nullptr; } // |image| holds a reference to a SourceSurface which in turn holds a lock on // the current frame's VolatileBuffer, ensuring that it doesn't get freed as // long as the layer system keeps this ImageContainer alive. AutoTArray<ImageContainer::NonOwningImage, 1> imageList; imageList.AppendElement(ImageContainer::NonOwningImage(image, TimeStamp(), mLastFrameID++, mImageProducerID)); container->SetCurrentImagesInTransaction(imageList); mLastImageContainerDrawResult = drawResult; mImageContainer = container; return container.forget(); }
Pair<DrawResult, RefPtr<layers::Image>> RasterImage::GetCurrentImage(ImageContainer* aContainer, uint32_t aFlags) { MOZ_ASSERT(NS_IsMainThread()); MOZ_ASSERT(aContainer); DrawResult drawResult; RefPtr<SourceSurface> surface; Tie(drawResult, surface) = GetFrameInternal(mSize, FRAME_CURRENT, aFlags | FLAG_ASYNC_NOTIFY); if (!surface) { // The OS threw out some or all of our buffer. We'll need to wait for the // redecode (which was automatically triggered by GetFrame) to complete. return MakePair(drawResult, RefPtr<layers::Image>()); } RefPtr<layers::Image> image = new layers::SourceSurfaceImage(surface); return MakePair(drawResult, Move(image)); }
ClippedImage::Draw(gfxContext* aContext, const nsIntSize& aSize, const ImageRegion& aRegion, uint32_t aWhichFrame, SamplingFilter aSamplingFilter, const Maybe<SVGImageContext>& aSVGContext, uint32_t aFlags, float aOpacity) { if (!ShouldClip()) { return InnerImage()->Draw(aContext, aSize, aRegion, aWhichFrame, aSamplingFilter, aSVGContext, aFlags, aOpacity); } // Check for tiling. If we need to tile then we need to create a // gfxCallbackDrawable to handle drawing for us. if (MustCreateSurface(aContext, aSize, aRegion, aFlags)) { // Create a temporary surface containing a single tile of this image. // GetFrame will call DrawSingleTile internally. DrawResult result; RefPtr<SourceSurface> surface; Tie(result, surface) = GetFrameInternal(aSize, aSVGContext, aWhichFrame, aFlags, aOpacity); if (!surface) { MOZ_ASSERT(result != DrawResult::SUCCESS); return result; } // Create a drawable from that surface. RefPtr<gfxSurfaceDrawable> drawable = new gfxSurfaceDrawable(surface, aSize); // Draw. gfxUtils::DrawPixelSnapped(aContext, drawable, aSize, aRegion, SurfaceFormat::B8G8R8A8, aSamplingFilter, aOpacity); return result; } return DrawSingleTile(aContext, aSize, aRegion, aWhichFrame, aSamplingFilter, aSVGContext, aFlags, aOpacity); }
void RasterImage::UpdateImageContainer() { MOZ_ASSERT(NS_IsMainThread()); RefPtr<layers::ImageContainer> container = mImageContainer.get(); if (!container) { return; } DrawResult drawResult; RefPtr<layers::Image> image; Tie(drawResult, image) = GetCurrentImage(container, FLAG_NONE); if (!image) { return; } mLastImageContainerDrawResult = drawResult; AutoTArray<ImageContainer::NonOwningImage, 1> imageList; imageList.AppendElement(ImageContainer::NonOwningImage(image, TimeStamp(), mLastFrameID++, mImageProducerID)); container->SetCurrentImages(imageList); }
inline static constexpr Tie createTie(void *ptr) noexcept {return Tie(ptr);}
void TupleTutorial() { /// . Tuples /// Template class `Tuple` allows combining 2-4 values with /// different types. These are principally similar to `std::tuple`, with some advantages. /// Unlike `std::tuple`, individual elements are directly accessible as member variables /// `a`..`d`, `Tuple` supports persistent storage patterns (`Serialize`, `Jsonize`, `Xmlize`), hash /// code (`GetHashValue`), conversion to `String` and Value conversions. /// To create a `Tuple` value, you can use the `MakeTuple` function. Tuple<int, String, String> x = MakeTuple(12, "hello", "world"); /// Individual values are accessible as members `a` .. `d`: DUMP(x.a); DUMP(x.b); DUMP(x.c); /// Or using `Get`: DUMP(x.Get<1>()); DUMP(x.Get<int>()); /// As long as all individual types have conversion to `String` (`AsString`), the tuple also /// has such conversion and thus can e.g. be easily logged: DUMP(x); /// As long as individual types have defined `GetHashValue`, so does `Tuple`: DUMP(GetHashValue(x)); /// As long as individual types have defined `operator==`, `Tuple` has defined `operator==` /// and `operator!=`: Tuple<int, String, String> y = x; DUMP(x == y); DUMP(x != y); y.a++; DUMP(x == y); DUMP(x != y); /// As long as all individual types have defined `SgnCompare`, /// Tuple has SgnCompare, Compare method and operators <, <=, >, >=: DUMP(x.Compare(y)); DUMP(SgnCompare(x, y)); DUMP(x < y); /// GetCount returns the width of `Tuple`: DUMP(x.GetCount()); /// Elements that are directly convertible with `Value` can be 'Get'/'Set': for(int i = 0; i < x.GetCount(); i++) DUMP(x.Get(i)); /// x.Set(1, "Hi"); DUMP(x); /// As long as all individual types are convertible with `Value`, you can convert Tuple to /// `ValueArray` and back: ValueArray va = x.GetArray(); DUMP(va); va.Set(2, "Joe"); x.SetArray(va); /// It is OK to assign `Tuple` to `Tuple` with different individual types, as long as types /// are directly convertible: Tuple<double, String, String> d = x; DUMP(d); /// Tie can be used to assign tuple to l-values: int i; String s1, s2; Tie(i, s1, s2) = x; DUMP(i); DUMP(s1); DUMP(s2); /// U++ Tuples are carefully designed as POD type, which allows POD arrays to be intialized /// with classic C style: static Tuple2<int, const char *> map[] = { { 1, "one" }, { 2, "one" }, { 3, "one" }, }; /// Simple FindTuple template function is provided to search for tuple based on the first /// value (`a`) (linear O(n) search): DUMP(FindTuple(map, __countof(map), 3)->b); /// }
void CEgIStream::Tie( const UtilStr* inSource ) { Tie( inSource -> getCStr(), inSource -> length() ); }